blob: 87f88abbe53bd9fb98194d4173380a18180cb941 [file] [log] [blame]
Guido van Rossum2bc13791999-03-24 19:06:42 +00001/* Dictionary object implementation using a hash table */
Guido van Rossum9bfef441993-03-29 10:43:31 +00002
Raymond Hettinger930427b2003-05-03 06:51:59 +00003/* The distribution includes a separate file, Objects/dictnotes.txt,
Tim Peters60b29962006-01-01 01:19:23 +00004 describing explorations into dictionary design and optimization.
Raymond Hettinger930427b2003-05-03 06:51:59 +00005 It covers typical dictionary use patterns, the parameters for
6 tuning dictionaries, and several ideas for possible optimizations.
7*/
8
Victor Stinner742da042016-09-07 17:40:12 -07009/* PyDictKeysObject
10
11This implements the dictionary's hashtable.
12
Raymond Hettingerb12785d2016-10-22 09:58:14 -070013As of Python 3.6, this is compact and ordered. Basic idea is described here:
14* https://mail.python.org/pipermail/python-dev/2012-December/123028.html
15* https://morepypy.blogspot.com/2015/01/faster-more-memory-efficient-and-more.html
Victor Stinner742da042016-09-07 17:40:12 -070016
17layout:
18
19+---------------+
20| dk_refcnt |
21| dk_size |
22| dk_lookup |
23| dk_usable |
24| dk_nentries |
25+---------------+
26| dk_indices |
27| |
28+---------------+
29| dk_entries |
30| |
31+---------------+
32
33dk_indices is actual hashtable. It holds index in entries, or DKIX_EMPTY(-1)
34or DKIX_DUMMY(-2).
35Size of indices is dk_size. Type of each index in indices is vary on dk_size:
36
37* int8 for dk_size <= 128
38* int16 for 256 <= dk_size <= 2**15
39* int32 for 2**16 <= dk_size <= 2**31
40* int64 for 2**32 <= dk_size
41
dalgarno359143c2019-09-10 10:45:07 +010042dk_entries is array of PyDictKeyEntry. Its size is USABLE_FRACTION(dk_size).
Victor Stinner742da042016-09-07 17:40:12 -070043DK_ENTRIES(dk) can be used to get pointer to entries.
44
45NOTE: Since negative value is used for DKIX_EMPTY and DKIX_DUMMY, type of
46dk_indices entry is signed integer and int16 is used for table which
47dk_size == 256.
48*/
49
Benjamin Peterson7d95e402012-04-23 11:24:50 -040050
51/*
Benjamin Peterson7d95e402012-04-23 11:24:50 -040052The DictObject can be in one of two forms.
Victor Stinner742da042016-09-07 17:40:12 -070053
Benjamin Peterson7d95e402012-04-23 11:24:50 -040054Either:
55 A combined table:
56 ma_values == NULL, dk_refcnt == 1.
57 Values are stored in the me_value field of the PyDictKeysObject.
Benjamin Peterson7d95e402012-04-23 11:24:50 -040058Or:
59 A split table:
60 ma_values != NULL, dk_refcnt >= 1
61 Values are stored in the ma_values array.
Victor Stinner742da042016-09-07 17:40:12 -070062 Only string (unicode) keys are allowed.
63 All dicts sharing same key must have same insertion order.
Benjamin Peterson7d95e402012-04-23 11:24:50 -040064
Victor Stinner742da042016-09-07 17:40:12 -070065There are four kinds of slots in the table (slot is index, and
66DK_ENTRIES(keys)[index] if index >= 0):
67
681. Unused. index == DKIX_EMPTY
69 Does not hold an active (key, value) pair now and never did. Unused can
70 transition to Active upon key insertion. This is each slot's initial state.
71
722. Active. index >= 0, me_key != NULL and me_value != NULL
73 Holds an active (key, value) pair. Active can transition to Dummy or
74 Pending upon key deletion (for combined and split tables respectively).
75 This is the only case in which me_value != NULL.
76
773. Dummy. index == DKIX_DUMMY (combined only)
78 Previously held an active (key, value) pair, but that was deleted and an
79 active pair has not yet overwritten the slot. Dummy can transition to
80 Active upon key insertion. Dummy slots cannot be made Unused again
81 else the probe sequence in case of collision would have no way to know
82 they were once active.
83
844. Pending. index >= 0, key != NULL, and value == NULL (split only)
85 Not yet inserted in split-table.
Benjamin Peterson7d95e402012-04-23 11:24:50 -040086*/
87
Victor Stinner742da042016-09-07 17:40:12 -070088/*
89Preserving insertion order
Benjamin Peterson7d95e402012-04-23 11:24:50 -040090
Victor Stinner742da042016-09-07 17:40:12 -070091It's simple for combined table. Since dk_entries is mostly append only, we can
92get insertion order by just iterating dk_entries.
93
94One exception is .popitem(). It removes last item in dk_entries and decrement
95dk_nentries to achieve amortized O(1). Since there are DKIX_DUMMY remains in
96dk_indices, we can't increment dk_usable even though dk_nentries is
97decremented.
98
99In split table, inserting into pending entry is allowed only for dk_entries[ix]
100where ix == mp->ma_used. Inserting into other index and deleting item cause
101converting the dict to the combined table.
102*/
103
104/* PyDict_MINSIZE is the starting size for any new dict.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400105 * 8 allows dicts with no more than 5 active entries; experiments suggested
106 * this suffices for the majority of dicts (consisting mostly of usually-small
107 * dicts created to pass keyword arguments).
108 * Making this 8, rather than 4 reduces the number of resizes for most
109 * dictionaries, without any significant extra memory use.
110 */
Victor Stinner742da042016-09-07 17:40:12 -0700111#define PyDict_MINSIZE 8
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400112
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000113#include "Python.h"
Victor 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
Victor Stinnerbed48172019-08-27 00:12:32 +0200285_PyDict_Fini(void)
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100286{
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
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200452#ifdef DEBUG_PYDICT
453# define ASSERT_CONSISTENT(op) assert(_PyDict_CheckConsistency((PyObject *)(op), 1))
454#else
455# define ASSERT_CONSISTENT(op) assert(_PyDict_CheckConsistency((PyObject *)(op), 0))
456#endif
Victor Stinner611b0fa2016-09-14 15:02:01 +0200457
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200458
459int
460_PyDict_CheckConsistency(PyObject *op, int check_content)
Victor Stinner611b0fa2016-09-14 15:02:01 +0200461{
Victor Stinner68762572019-10-07 18:42:01 +0200462#define CHECK(expr) \
463 do { if (!(expr)) { _PyObject_ASSERT_FAILED_MSG(op, Py_STRINGIFY(expr)); } } while (0)
464
465 assert(op != NULL);
466 CHECK(PyDict_Check(op));
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200467 PyDictObject *mp = (PyDictObject *)op;
Victor Stinner50fe3f82018-10-26 18:47:15 +0200468
Victor Stinner611b0fa2016-09-14 15:02:01 +0200469 PyDictKeysObject *keys = mp->ma_keys;
470 int splitted = _PyDict_HasSplitTable(mp);
471 Py_ssize_t usable = USABLE_FRACTION(keys->dk_size);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200472
Victor Stinner68762572019-10-07 18:42:01 +0200473 CHECK(0 <= mp->ma_used && mp->ma_used <= usable);
474 CHECK(IS_POWER_OF_2(keys->dk_size));
475 CHECK(0 <= keys->dk_usable && keys->dk_usable <= usable);
476 CHECK(0 <= keys->dk_nentries && keys->dk_nentries <= usable);
477 CHECK(keys->dk_usable + keys->dk_nentries <= usable);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200478
479 if (!splitted) {
480 /* combined table */
Victor Stinner68762572019-10-07 18:42:01 +0200481 CHECK(keys->dk_refcnt == 1);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200482 }
483
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200484 if (check_content) {
485 PyDictKeyEntry *entries = DK_ENTRIES(keys);
486 Py_ssize_t i;
Victor Stinner611b0fa2016-09-14 15:02:01 +0200487
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200488 for (i=0; i < keys->dk_size; i++) {
489 Py_ssize_t ix = dictkeys_get_index(keys, i);
Victor Stinner68762572019-10-07 18:42:01 +0200490 CHECK(DKIX_DUMMY <= ix && ix <= usable);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200491 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200492
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200493 for (i=0; i < usable; i++) {
494 PyDictKeyEntry *entry = &entries[i];
495 PyObject *key = entry->me_key;
496
497 if (key != NULL) {
498 if (PyUnicode_CheckExact(key)) {
499 Py_hash_t hash = ((PyASCIIObject *)key)->hash;
Victor Stinner68762572019-10-07 18:42:01 +0200500 CHECK(hash != -1);
501 CHECK(entry->me_hash == hash);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200502 }
503 else {
504 /* test_dict fails if PyObject_Hash() is called again */
Victor Stinner68762572019-10-07 18:42:01 +0200505 CHECK(entry->me_hash != -1);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200506 }
507 if (!splitted) {
Victor Stinner68762572019-10-07 18:42:01 +0200508 CHECK(entry->me_value != NULL);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200509 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200510 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200511
512 if (splitted) {
Victor Stinner68762572019-10-07 18:42:01 +0200513 CHECK(entry->me_value == NULL);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200514 }
515 }
516
517 if (splitted) {
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200518 /* splitted table */
519 for (i=0; i < mp->ma_used; i++) {
Victor Stinner68762572019-10-07 18:42:01 +0200520 CHECK(mp->ma_values[i] != NULL);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200521 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200522 }
523 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200524 return 1;
Victor Stinner68762572019-10-07 18:42:01 +0200525
526#undef CHECK
Victor Stinner611b0fa2016-09-14 15:02:01 +0200527}
Victor Stinner611b0fa2016-09-14 15:02:01 +0200528
529
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400530static PyDictKeysObject *new_keys_object(Py_ssize_t size)
531{
532 PyDictKeysObject *dk;
Victor Stinner742da042016-09-07 17:40:12 -0700533 Py_ssize_t es, usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400534
Victor Stinner742da042016-09-07 17:40:12 -0700535 assert(size >= PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400536 assert(IS_POWER_OF_2(size));
Victor Stinner742da042016-09-07 17:40:12 -0700537
538 usable = USABLE_FRACTION(size);
539 if (size <= 0xff) {
540 es = 1;
541 }
542 else if (size <= 0xffff) {
543 es = 2;
544 }
545#if SIZEOF_VOID_P > 4
546 else if (size <= 0xffffffff) {
547 es = 4;
548 }
549#endif
550 else {
551 es = sizeof(Py_ssize_t);
552 }
553
554 if (size == PyDict_MINSIZE && numfreekeys > 0) {
555 dk = keys_free_list[--numfreekeys];
556 }
557 else {
Victor Stinner98ee9d52016-09-08 09:33:56 -0700558 dk = PyObject_MALLOC(sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -0700559 + es * size
560 + sizeof(PyDictKeyEntry) * usable);
Victor Stinner742da042016-09-07 17:40:12 -0700561 if (dk == NULL) {
562 PyErr_NoMemory();
563 return NULL;
564 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400565 }
INADA Naokia7576492018-11-14 18:39:27 +0900566 _Py_INC_REFTOTAL;
567 dk->dk_refcnt = 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400568 dk->dk_size = size;
Victor Stinner742da042016-09-07 17:40:12 -0700569 dk->dk_usable = usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400570 dk->dk_lookup = lookdict_unicode_nodummy;
Victor Stinner742da042016-09-07 17:40:12 -0700571 dk->dk_nentries = 0;
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700572 memset(&dk->dk_indices[0], 0xff, es * size);
Victor Stinner742da042016-09-07 17:40:12 -0700573 memset(DK_ENTRIES(dk), 0, sizeof(PyDictKeyEntry) * usable);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400574 return dk;
575}
576
577static void
578free_keys_object(PyDictKeysObject *keys)
579{
Victor Stinner742da042016-09-07 17:40:12 -0700580 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400581 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -0700582 for (i = 0, n = keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400583 Py_XDECREF(entries[i].me_key);
584 Py_XDECREF(entries[i].me_value);
585 }
Victor Stinner742da042016-09-07 17:40:12 -0700586 if (keys->dk_size == PyDict_MINSIZE && numfreekeys < PyDict_MAXFREELIST) {
587 keys_free_list[numfreekeys++] = keys;
588 return;
589 }
Raymond Hettingerce5179f2016-01-31 08:56:21 -0800590 PyObject_FREE(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400591}
592
593#define new_values(size) PyMem_NEW(PyObject *, size)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400594#define free_values(values) PyMem_FREE(values)
595
596/* Consumes a reference to the keys object */
597static PyObject *
598new_dict(PyDictKeysObject *keys, PyObject **values)
599{
600 PyDictObject *mp;
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200601 assert(keys != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 if (numfree) {
603 mp = free_list[--numfree];
604 assert (mp != NULL);
605 assert (Py_TYPE(mp) == &PyDict_Type);
606 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400608 else {
609 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
610 if (mp == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +0900611 dictkeys_decref(keys);
Zackery Spytz3d07c1e2019-03-23 20:23:29 -0600612 if (values != empty_values) {
613 free_values(values);
614 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400615 return NULL;
616 }
617 }
618 mp->ma_keys = keys;
619 mp->ma_values = values;
620 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700621 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200622 ASSERT_CONSISTENT(mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000624}
625
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400626/* Consumes a reference to the keys object */
627static PyObject *
628new_dict_with_shared_keys(PyDictKeysObject *keys)
629{
630 PyObject **values;
631 Py_ssize_t i, size;
632
Victor Stinner742da042016-09-07 17:40:12 -0700633 size = USABLE_FRACTION(DK_SIZE(keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400634 values = new_values(size);
635 if (values == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +0900636 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400637 return PyErr_NoMemory();
638 }
639 for (i = 0; i < size; i++) {
640 values[i] = NULL;
641 }
642 return new_dict(keys, values);
643}
644
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500645
646static PyObject *
647clone_combined_dict(PyDictObject *orig)
648{
649 assert(PyDict_CheckExact(orig));
650 assert(orig->ma_values == NULL);
651 assert(orig->ma_keys->dk_refcnt == 1);
652
653 Py_ssize_t keys_size = _PyDict_KeysSize(orig->ma_keys);
654 PyDictKeysObject *keys = PyObject_Malloc(keys_size);
655 if (keys == NULL) {
656 PyErr_NoMemory();
657 return NULL;
658 }
659
660 memcpy(keys, orig->ma_keys, keys_size);
661
662 /* After copying key/value pairs, we need to incref all
663 keys and values and they are about to be co-owned by a
664 new dict object. */
665 PyDictKeyEntry *ep0 = DK_ENTRIES(keys);
666 Py_ssize_t n = keys->dk_nentries;
667 for (Py_ssize_t i = 0; i < n; i++) {
668 PyDictKeyEntry *entry = &ep0[i];
669 PyObject *value = entry->me_value;
670 if (value != NULL) {
671 Py_INCREF(value);
672 Py_INCREF(entry->me_key);
673 }
674 }
675
676 PyDictObject *new = (PyDictObject *)new_dict(keys, NULL);
677 if (new == NULL) {
678 /* In case of an error, `new_dict()` takes care of
679 cleaning up `keys`. */
680 return NULL;
681 }
682 new->ma_used = orig->ma_used;
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200683 ASSERT_CONSISTENT(new);
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500684 if (_PyObject_GC_IS_TRACKED(orig)) {
685 /* Maintain tracking. */
686 _PyObject_GC_TRACK(new);
687 }
Yury Selivanov0b752282018-07-06 12:20:07 -0400688
689 /* Since we copied the keys table we now have an extra reference
690 in the system. Manually call _Py_INC_REFTOTAL to signal that
INADA Naokia7576492018-11-14 18:39:27 +0900691 we have it now; calling dictkeys_incref would be an error as
Yury Selivanov0b752282018-07-06 12:20:07 -0400692 keys->dk_refcnt is already set to 1 (after memcpy). */
693 _Py_INC_REFTOTAL;
694
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500695 return (PyObject *)new;
696}
697
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400698PyObject *
699PyDict_New(void)
700{
Inada Naokif2a18672019-03-12 17:25:44 +0900701 dictkeys_incref(Py_EMPTY_KEYS);
702 return new_dict(Py_EMPTY_KEYS, empty_values);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400703}
704
Victor Stinner742da042016-09-07 17:40:12 -0700705/* Search index of hash table from offset of entry table */
706static Py_ssize_t
707lookdict_index(PyDictKeysObject *k, Py_hash_t hash, Py_ssize_t index)
708{
Victor Stinner742da042016-09-07 17:40:12 -0700709 size_t mask = DK_MASK(k);
INADA Naoki073ae482017-06-23 15:22:50 +0900710 size_t perturb = (size_t)hash;
711 size_t i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700712
INADA Naoki073ae482017-06-23 15:22:50 +0900713 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900714 Py_ssize_t ix = dictkeys_get_index(k, i);
Victor Stinner742da042016-09-07 17:40:12 -0700715 if (ix == index) {
716 return i;
717 }
718 if (ix == DKIX_EMPTY) {
719 return DKIX_EMPTY;
720 }
INADA Naoki073ae482017-06-23 15:22:50 +0900721 perturb >>= PERTURB_SHIFT;
722 i = mask & (i*5 + perturb + 1);
Victor Stinner742da042016-09-07 17:40:12 -0700723 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700724 Py_UNREACHABLE();
Victor Stinner742da042016-09-07 17:40:12 -0700725}
726
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000727/*
728The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000729This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000730Open addressing is preferred over chaining since the link overhead for
731chaining would be substantial (100% with typical malloc overhead).
732
Tim Peterseb28ef22001-06-02 05:27:19 +0000733The initial probe index is computed as hash mod the table size. Subsequent
734probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000735
736All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000737
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000738The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000739contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000740Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000741
Victor Stinner742da042016-09-07 17:40:12 -0700742lookdict() is general-purpose, and may return DKIX_ERROR if (and only if) a
Victor Stinnera4348cc2016-09-08 12:01:25 -0700743comparison raises an exception.
Guido van Rossum89d8c602007-09-18 17:26:56 +0000744lookdict_unicode() below is specialized to string keys, comparison of which can
INADA Naoki1b8df102017-02-20 22:48:10 +0900745never raise an exception; that function can never return DKIX_ERROR when key
746is string. Otherwise, it falls back to lookdict().
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400747lookdict_unicode_nodummy is further specialized for string keys that cannot be
748the <dummy> value.
INADA Naoki778928b2017-08-03 23:45:15 +0900749For both, when the key isn't found a DKIX_EMPTY is returned.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000750*/
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100751static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400752lookdict(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900753 Py_hash_t hash, PyObject **value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000754{
INADA Naoki778928b2017-08-03 23:45:15 +0900755 size_t i, mask, perturb;
Victor Stinner742da042016-09-07 17:40:12 -0700756 PyDictKeysObject *dk;
INADA Naoki778928b2017-08-03 23:45:15 +0900757 PyDictKeyEntry *ep0;
Tim Peterseb28ef22001-06-02 05:27:19 +0000758
Antoine Pitrou9a234902012-05-13 20:48:01 +0200759top:
Victor Stinner742da042016-09-07 17:40:12 -0700760 dk = mp->ma_keys;
Victor Stinner742da042016-09-07 17:40:12 -0700761 ep0 = DK_ENTRIES(dk);
INADA Naoki778928b2017-08-03 23:45:15 +0900762 mask = DK_MASK(dk);
763 perturb = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700765
INADA Naoki778928b2017-08-03 23:45:15 +0900766 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900767 Py_ssize_t ix = dictkeys_get_index(dk, i);
Victor Stinner742da042016-09-07 17:40:12 -0700768 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700769 *value_addr = NULL;
770 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400771 }
INADA Naoki778928b2017-08-03 23:45:15 +0900772 if (ix >= 0) {
773 PyDictKeyEntry *ep = &ep0[ix];
774 assert(ep->me_key != NULL);
775 if (ep->me_key == key) {
776 *value_addr = ep->me_value;
777 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700778 }
INADA Naoki778928b2017-08-03 23:45:15 +0900779 if (ep->me_hash == hash) {
780 PyObject *startkey = ep->me_key;
781 Py_INCREF(startkey);
782 int cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
783 Py_DECREF(startkey);
784 if (cmp < 0) {
785 *value_addr = NULL;
786 return DKIX_ERROR;
787 }
788 if (dk == mp->ma_keys && ep->me_key == startkey) {
789 if (cmp > 0) {
790 *value_addr = ep->me_value;
791 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700792 }
INADA Naoki778928b2017-08-03 23:45:15 +0900793 }
794 else {
795 /* The dict was mutated, restart */
796 goto top;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400797 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 }
INADA Naoki778928b2017-08-03 23:45:15 +0900800 perturb >>= PERTURB_SHIFT;
801 i = (i*5 + perturb + 1) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700803 Py_UNREACHABLE();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000804}
805
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400806/* Specialized version for string-only keys */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100807static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400808lookdict_unicode(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900809 Py_hash_t hash, PyObject **value_addr)
Fred Drake1bff34a2000-08-31 19:31:38 +0000810{
Victor Stinner742da042016-09-07 17:40:12 -0700811 assert(mp->ma_values == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 /* Make sure this function doesn't have to handle non-unicode keys,
813 including subclasses of str; e.g., one reason to subclass
814 unicodes is to override __eq__, and for speed we don't cater to
815 that here. */
816 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400817 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900818 return lookdict(mp, key, hash, value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 }
Tim Peters15d49292001-05-27 07:39:22 +0000820
INADA Naoki778928b2017-08-03 23:45:15 +0900821 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
822 size_t mask = DK_MASK(mp->ma_keys);
823 size_t perturb = (size_t)hash;
824 size_t i = (size_t)hash & mask;
825
826 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900827 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700828 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700829 *value_addr = NULL;
830 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400831 }
INADA Naoki778928b2017-08-03 23:45:15 +0900832 if (ix >= 0) {
833 PyDictKeyEntry *ep = &ep0[ix];
834 assert(ep->me_key != NULL);
835 assert(PyUnicode_CheckExact(ep->me_key));
836 if (ep->me_key == key ||
837 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
838 *value_addr = ep->me_value;
839 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700840 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400841 }
INADA Naoki778928b2017-08-03 23:45:15 +0900842 perturb >>= PERTURB_SHIFT;
843 i = mask & (i*5 + perturb + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700845 Py_UNREACHABLE();
Fred Drake1bff34a2000-08-31 19:31:38 +0000846}
847
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400848/* Faster version of lookdict_unicode when it is known that no <dummy> keys
849 * will be present. */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100850static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400851lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900852 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400853{
Victor Stinner742da042016-09-07 17:40:12 -0700854 assert(mp->ma_values == NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400855 /* Make sure this function doesn't have to handle non-unicode keys,
856 including subclasses of str; e.g., one reason to subclass
857 unicodes is to override __eq__, and for speed we don't cater to
858 that here. */
859 if (!PyUnicode_CheckExact(key)) {
860 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900861 return lookdict(mp, key, hash, value_addr);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400862 }
INADA Naoki778928b2017-08-03 23:45:15 +0900863
864 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
865 size_t mask = DK_MASK(mp->ma_keys);
866 size_t perturb = (size_t)hash;
867 size_t i = (size_t)hash & mask;
868
869 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900870 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700871 assert (ix != DKIX_DUMMY);
872 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700873 *value_addr = NULL;
874 return DKIX_EMPTY;
875 }
INADA Naoki778928b2017-08-03 23:45:15 +0900876 PyDictKeyEntry *ep = &ep0[ix];
877 assert(ep->me_key != NULL);
878 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700879 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400880 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900881 *value_addr = ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700882 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400883 }
INADA Naoki778928b2017-08-03 23:45:15 +0900884 perturb >>= PERTURB_SHIFT;
885 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400886 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700887 Py_UNREACHABLE();
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400888}
889
890/* Version of lookdict for split tables.
891 * All split tables and only split tables use this lookup function.
892 * Split tables only contain unicode keys and no dummy keys,
893 * so algorithm is the same as lookdict_unicode_nodummy.
894 */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100895static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400896lookdict_split(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900897 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400898{
Victor Stinner742da042016-09-07 17:40:12 -0700899 /* mp must split table */
900 assert(mp->ma_values != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400901 if (!PyUnicode_CheckExact(key)) {
INADA Naoki778928b2017-08-03 23:45:15 +0900902 Py_ssize_t ix = lookdict(mp, key, hash, value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700903 if (ix >= 0) {
INADA Naokiba609772016-12-07 20:41:42 +0900904 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700905 }
906 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400907 }
Victor Stinner742da042016-09-07 17:40:12 -0700908
INADA Naoki778928b2017-08-03 23:45:15 +0900909 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
910 size_t mask = DK_MASK(mp->ma_keys);
911 size_t perturb = (size_t)hash;
912 size_t i = (size_t)hash & mask;
913
914 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900915 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
INADA Naoki778928b2017-08-03 23:45:15 +0900916 assert (ix != DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -0700917 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700918 *value_addr = NULL;
919 return DKIX_EMPTY;
920 }
INADA Naoki778928b2017-08-03 23:45:15 +0900921 PyDictKeyEntry *ep = &ep0[ix];
922 assert(ep->me_key != NULL);
923 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700924 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400925 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900926 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700927 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400928 }
INADA Naoki778928b2017-08-03 23:45:15 +0900929 perturb >>= PERTURB_SHIFT;
930 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400931 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700932 Py_UNREACHABLE();
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400933}
934
Benjamin Petersonfb886362010-04-24 18:21:17 +0000935int
936_PyDict_HasOnlyStringKeys(PyObject *dict)
937{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 Py_ssize_t pos = 0;
939 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000940 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400942 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 return 1;
944 while (PyDict_Next(dict, &pos, &key, &value))
945 if (!PyUnicode_Check(key))
946 return 0;
947 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000948}
949
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000950#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 do { \
952 if (!_PyObject_GC_IS_TRACKED(mp)) { \
953 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
954 _PyObject_GC_MAY_BE_TRACKED(value)) { \
955 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 } \
957 } \
958 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000959
960void
961_PyDict_MaybeUntrack(PyObject *op)
962{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 PyDictObject *mp;
964 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -0700965 Py_ssize_t i, numentries;
966 PyDictKeyEntry *ep0;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
969 return;
970
971 mp = (PyDictObject *) op;
Victor Stinner742da042016-09-07 17:40:12 -0700972 ep0 = DK_ENTRIES(mp->ma_keys);
973 numentries = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400974 if (_PyDict_HasSplitTable(mp)) {
Victor Stinner742da042016-09-07 17:40:12 -0700975 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400976 if ((value = mp->ma_values[i]) == NULL)
977 continue;
978 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
Victor Stinner742da042016-09-07 17:40:12 -0700979 assert(!_PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400980 return;
981 }
982 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400984 else {
Victor Stinner742da042016-09-07 17:40:12 -0700985 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400986 if ((value = ep0[i].me_value) == NULL)
987 continue;
988 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
989 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
990 return;
991 }
992 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000994}
995
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400996/* Internal function to find slot for an item from its hash
Victor Stinner3c336c52016-09-12 14:17:40 +0200997 when it is known that the key is not present in the dict.
998
999 The dict must be combined. */
INADA Naokiba609772016-12-07 20:41:42 +09001000static Py_ssize_t
INADA Naoki778928b2017-08-03 23:45:15 +09001001find_empty_slot(PyDictKeysObject *keys, Py_hash_t hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001002{
INADA Naoki778928b2017-08-03 23:45:15 +09001003 assert(keys != NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001004
INADA Naoki778928b2017-08-03 23:45:15 +09001005 const size_t mask = DK_MASK(keys);
1006 size_t i = hash & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001007 Py_ssize_t ix = dictkeys_get_index(keys, i);
INADA Naoki778928b2017-08-03 23:45:15 +09001008 for (size_t perturb = hash; ix >= 0;) {
INADA Naoki267941c2016-10-06 15:19:07 +09001009 perturb >>= PERTURB_SHIFT;
INADA Naoki778928b2017-08-03 23:45:15 +09001010 i = (i*5 + perturb + 1) & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001011 ix = dictkeys_get_index(keys, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 }
INADA Naoki778928b2017-08-03 23:45:15 +09001013 return i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001014}
1015
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001016static int
1017insertion_resize(PyDictObject *mp)
1018{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -07001019 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001020}
Antoine Pitroue965d972012-02-27 00:45:12 +01001021
1022/*
1023Internal routine to insert a new item into the table.
1024Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +01001025Returns -1 if an error occurred, or 0 on success.
1026*/
1027static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001028insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001029{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001030 PyObject *old_value;
INADA Naokiba609772016-12-07 20:41:42 +09001031 PyDictKeyEntry *ep;
Antoine Pitroue965d972012-02-27 00:45:12 +01001032
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001033 Py_INCREF(key);
1034 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001035 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
1036 if (insertion_resize(mp) < 0)
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001037 goto Fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001038 }
1039
INADA Naoki778928b2017-08-03 23:45:15 +09001040 Py_ssize_t ix = mp->ma_keys->dk_lookup(mp, key, hash, &old_value);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001041 if (ix == DKIX_ERROR)
1042 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001043
Antoine Pitroud6967322014-10-18 00:35:00 +02001044 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001045 MAINTAIN_TRACKING(mp, key, value);
Victor Stinner742da042016-09-07 17:40:12 -07001046
1047 /* When insertion order is different from shared key, we can't share
1048 * the key anymore. Convert this instance to combine table.
1049 */
1050 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09001051 ((ix >= 0 && old_value == NULL && mp->ma_used != ix) ||
Victor Stinner742da042016-09-07 17:40:12 -07001052 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001053 if (insertion_resize(mp) < 0)
1054 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001055 ix = DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001056 }
Victor Stinner742da042016-09-07 17:40:12 -07001057
1058 if (ix == DKIX_EMPTY) {
1059 /* Insert into new slot. */
INADA Naokiba609772016-12-07 20:41:42 +09001060 assert(old_value == NULL);
Victor Stinner742da042016-09-07 17:40:12 -07001061 if (mp->ma_keys->dk_usable <= 0) {
1062 /* Need to resize. */
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001063 if (insertion_resize(mp) < 0)
1064 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001065 }
INADA Naoki778928b2017-08-03 23:45:15 +09001066 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naokiba609772016-12-07 20:41:42 +09001067 ep = &DK_ENTRIES(mp->ma_keys)[mp->ma_keys->dk_nentries];
INADA Naokia7576492018-11-14 18:39:27 +09001068 dictkeys_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Victor Stinner742da042016-09-07 17:40:12 -07001069 ep->me_key = key;
1070 ep->me_hash = hash;
1071 if (mp->ma_values) {
1072 assert (mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
1073 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001074 }
1075 else {
Victor Stinner742da042016-09-07 17:40:12 -07001076 ep->me_value = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001077 }
1078 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001079 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07001080 mp->ma_keys->dk_usable--;
1081 mp->ma_keys->dk_nentries++;
1082 assert(mp->ma_keys->dk_usable >= 0);
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001083 ASSERT_CONSISTENT(mp);
Victor Stinner742da042016-09-07 17:40:12 -07001084 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001085 }
Victor Stinner742da042016-09-07 17:40:12 -07001086
Inada Naoki91234a12019-06-03 21:30:58 +09001087 if (old_value != value) {
1088 if (_PyDict_HasSplitTable(mp)) {
1089 mp->ma_values[ix] = value;
1090 if (old_value == NULL) {
1091 /* pending state */
1092 assert(ix == mp->ma_used);
1093 mp->ma_used++;
1094 }
INADA Naokiba609772016-12-07 20:41:42 +09001095 }
Inada Naoki91234a12019-06-03 21:30:58 +09001096 else {
1097 assert(old_value != NULL);
1098 DK_ENTRIES(mp->ma_keys)[ix].me_value = value;
1099 }
1100 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokiba609772016-12-07 20:41:42 +09001101 }
INADA Naokiba609772016-12-07 20:41:42 +09001102 Py_XDECREF(old_value); /* which **CAN** re-enter (see issue #22653) */
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001103 ASSERT_CONSISTENT(mp);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001104 Py_DECREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001105 return 0;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001106
1107Fail:
1108 Py_DECREF(value);
1109 Py_DECREF(key);
1110 return -1;
Antoine Pitroue965d972012-02-27 00:45:12 +01001111}
1112
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001113// Same to insertdict but specialized for ma_keys = Py_EMPTY_KEYS.
1114static int
1115insert_to_emptydict(PyDictObject *mp, PyObject *key, Py_hash_t hash,
1116 PyObject *value)
1117{
1118 assert(mp->ma_keys == Py_EMPTY_KEYS);
1119
1120 PyDictKeysObject *newkeys = new_keys_object(PyDict_MINSIZE);
1121 if (newkeys == NULL) {
1122 return -1;
1123 }
1124 if (!PyUnicode_CheckExact(key)) {
1125 newkeys->dk_lookup = lookdict;
1126 }
1127 dictkeys_decref(Py_EMPTY_KEYS);
1128 mp->ma_keys = newkeys;
1129 mp->ma_values = NULL;
1130
1131 Py_INCREF(key);
1132 Py_INCREF(value);
1133 MAINTAIN_TRACKING(mp, key, value);
1134
1135 size_t hashpos = (size_t)hash & (PyDict_MINSIZE-1);
Dong-hee Nac39d1dd2019-10-11 17:43:11 +09001136 PyDictKeyEntry *ep = DK_ENTRIES(mp->ma_keys);
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001137 dictkeys_set_index(mp->ma_keys, hashpos, 0);
1138 ep->me_key = key;
1139 ep->me_hash = hash;
1140 ep->me_value = value;
1141 mp->ma_used++;
1142 mp->ma_version_tag = DICT_NEXT_VERSION();
1143 mp->ma_keys->dk_usable--;
1144 mp->ma_keys->dk_nentries++;
1145 return 0;
1146}
1147
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001148/*
luzpaza5293b42017-11-05 07:37:50 -06001149Internal routine used by dictresize() to build a hashtable of entries.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001150*/
1151static void
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001152build_indices(PyDictKeysObject *keys, PyDictKeyEntry *ep, Py_ssize_t n)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001153{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001154 size_t mask = (size_t)DK_SIZE(keys) - 1;
1155 for (Py_ssize_t ix = 0; ix != n; ix++, ep++) {
1156 Py_hash_t hash = ep->me_hash;
1157 size_t i = hash & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001158 for (size_t perturb = hash; dictkeys_get_index(keys, i) != DKIX_EMPTY;) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001159 perturb >>= PERTURB_SHIFT;
INADA Naoki870c2862017-06-24 09:03:19 +09001160 i = mask & (i*5 + perturb + 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001161 }
INADA Naokia7576492018-11-14 18:39:27 +09001162 dictkeys_set_index(keys, i, ix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001164}
1165
1166/*
1167Restructure the table by allocating a new table and reinserting all
1168items again. When entries have been deleted, the new table may
1169actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001170If a table is split (its keys and hashes are shared, its values are not),
1171then the values are temporarily copied into the table, it is resized as
1172a combined table, then the me_value slots in the old table are NULLed out.
1173After resizing a table is always combined,
1174but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001175*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001176static int
Victor Stinner3d3f2642016-12-15 17:21:23 +01001177dictresize(PyDictObject *mp, Py_ssize_t minsize)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001178{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001179 Py_ssize_t newsize, numentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001180 PyDictKeysObject *oldkeys;
1181 PyObject **oldvalues;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001182 PyDictKeyEntry *oldentries, *newentries;
Tim Peters91a364d2001-05-19 07:04:38 +00001183
Victor Stinner742da042016-09-07 17:40:12 -07001184 /* Find the smallest table size > minused. */
1185 for (newsize = PyDict_MINSIZE;
Victor Stinner3d3f2642016-12-15 17:21:23 +01001186 newsize < minsize && newsize > 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 newsize <<= 1)
1188 ;
1189 if (newsize <= 0) {
1190 PyErr_NoMemory();
1191 return -1;
1192 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001193
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001194 oldkeys = mp->ma_keys;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001195
1196 /* NOTE: Current odict checks mp->ma_keys to detect resize happen.
1197 * So we can't reuse oldkeys even if oldkeys->dk_size == newsize.
1198 * TODO: Try reusing oldkeys when reimplement odict.
1199 */
1200
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001201 /* Allocate a new table. */
1202 mp->ma_keys = new_keys_object(newsize);
1203 if (mp->ma_keys == NULL) {
1204 mp->ma_keys = oldkeys;
1205 return -1;
1206 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01001207 // New table must be large enough.
1208 assert(mp->ma_keys->dk_usable >= mp->ma_used);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001209 if (oldkeys->dk_lookup == lookdict)
1210 mp->ma_keys->dk_lookup = lookdict;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001211
1212 numentries = mp->ma_used;
1213 oldentries = DK_ENTRIES(oldkeys);
1214 newentries = DK_ENTRIES(mp->ma_keys);
1215 oldvalues = mp->ma_values;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001216 if (oldvalues != NULL) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001217 /* Convert split table into new combined table.
1218 * We must incref keys; we can transfer values.
1219 * Note that values of split table is always dense.
1220 */
1221 for (Py_ssize_t i = 0; i < numentries; i++) {
1222 assert(oldvalues[i] != NULL);
1223 PyDictKeyEntry *ep = &oldentries[i];
1224 PyObject *key = ep->me_key;
1225 Py_INCREF(key);
1226 newentries[i].me_key = key;
1227 newentries[i].me_hash = ep->me_hash;
1228 newentries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001230
INADA Naokia7576492018-11-14 18:39:27 +09001231 dictkeys_decref(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001232 mp->ma_values = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001233 if (oldvalues != empty_values) {
1234 free_values(oldvalues);
1235 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001236 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001237 else { // combined table.
1238 if (oldkeys->dk_nentries == numentries) {
1239 memcpy(newentries, oldentries, numentries * sizeof(PyDictKeyEntry));
1240 }
1241 else {
1242 PyDictKeyEntry *ep = oldentries;
1243 for (Py_ssize_t i = 0; i < numentries; i++) {
1244 while (ep->me_value == NULL)
1245 ep++;
1246 newentries[i] = *ep++;
1247 }
1248 }
1249
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001250 assert(oldkeys->dk_lookup != lookdict_split);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001251 assert(oldkeys->dk_refcnt == 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001252 if (oldkeys->dk_size == PyDict_MINSIZE &&
1253 numfreekeys < PyDict_MAXFREELIST) {
INADA Naokia7576492018-11-14 18:39:27 +09001254 _Py_DEC_REFTOTAL;
1255 keys_free_list[numfreekeys++] = oldkeys;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001256 }
1257 else {
INADA Naokia7576492018-11-14 18:39:27 +09001258 _Py_DEC_REFTOTAL;
1259 PyObject_FREE(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001260 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001261 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001262
1263 build_indices(mp->ma_keys, newentries, numentries);
1264 mp->ma_keys->dk_usable -= numentries;
1265 mp->ma_keys->dk_nentries = numentries;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001267}
1268
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001269/* Returns NULL if unable to split table.
1270 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001271static PyDictKeysObject *
1272make_keys_shared(PyObject *op)
1273{
1274 Py_ssize_t i;
1275 Py_ssize_t size;
1276 PyDictObject *mp = (PyDictObject *)op;
1277
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001278 if (!PyDict_CheckExact(op))
1279 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001280 if (!_PyDict_HasSplitTable(mp)) {
1281 PyDictKeyEntry *ep0;
1282 PyObject **values;
1283 assert(mp->ma_keys->dk_refcnt == 1);
1284 if (mp->ma_keys->dk_lookup == lookdict) {
1285 return NULL;
1286 }
1287 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1288 /* Remove dummy keys */
1289 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1290 return NULL;
1291 }
1292 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1293 /* Copy values into a new array */
Victor Stinner742da042016-09-07 17:40:12 -07001294 ep0 = DK_ENTRIES(mp->ma_keys);
1295 size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001296 values = new_values(size);
1297 if (values == NULL) {
1298 PyErr_SetString(PyExc_MemoryError,
1299 "Not enough memory to allocate new values array");
1300 return NULL;
1301 }
1302 for (i = 0; i < size; i++) {
1303 values[i] = ep0[i].me_value;
1304 ep0[i].me_value = NULL;
1305 }
1306 mp->ma_keys->dk_lookup = lookdict_split;
1307 mp->ma_values = values;
1308 }
INADA Naokia7576492018-11-14 18:39:27 +09001309 dictkeys_incref(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001310 return mp->ma_keys;
1311}
Christian Heimes99170a52007-12-19 02:07:34 +00001312
1313PyObject *
1314_PyDict_NewPresized(Py_ssize_t minused)
1315{
INADA Naoki92c50ee2016-11-22 00:57:02 +09001316 const Py_ssize_t max_presize = 128 * 1024;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001317 Py_ssize_t newsize;
1318 PyDictKeysObject *new_keys;
INADA Naoki92c50ee2016-11-22 00:57:02 +09001319
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001320 if (minused <= USABLE_FRACTION(PyDict_MINSIZE)) {
Inada Naokif2a18672019-03-12 17:25:44 +09001321 return PyDict_New();
1322 }
INADA Naoki92c50ee2016-11-22 00:57:02 +09001323 /* There are no strict guarantee that returned dict can contain minused
1324 * items without resize. So we create medium size dict instead of very
1325 * large dict or MemoryError.
1326 */
1327 if (minused > USABLE_FRACTION(max_presize)) {
1328 newsize = max_presize;
1329 }
1330 else {
1331 Py_ssize_t minsize = ESTIMATE_SIZE(minused);
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001332 newsize = PyDict_MINSIZE*2;
INADA Naoki92c50ee2016-11-22 00:57:02 +09001333 while (newsize < minsize) {
1334 newsize <<= 1;
1335 }
1336 }
1337 assert(IS_POWER_OF_2(newsize));
1338
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001339 new_keys = new_keys_object(newsize);
1340 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001342 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001343}
1344
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001345/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1346 * that may occur (originally dicts supported only string keys, and exceptions
1347 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001348 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001349 * (suppressed) occurred while computing the key's hash, or that some error
1350 * (suppressed) occurred when comparing keys in the dict's internal probe
1351 * sequence. A nasty example of the latter is when a Python-coded comparison
1352 * function hits a stack-depth error, which can cause this to return NULL
1353 * even if the key is present.
1354 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001355PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001356PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001357{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001358 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07001359 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 PyDictObject *mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 PyThreadState *tstate;
INADA Naokiba609772016-12-07 20:41:42 +09001362 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 if (!PyDict_Check(op))
1365 return NULL;
1366 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001367 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 {
1369 hash = PyObject_Hash(key);
1370 if (hash == -1) {
1371 PyErr_Clear();
1372 return NULL;
1373 }
1374 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 /* We can arrive here with a NULL tstate during initialization: try
1377 running "python -Wi" for an example related to string interning.
1378 Let's just hope that no exception occurs then... This must be
Victor Stinner50b48572018-11-01 01:51:40 +01001379 _PyThreadState_GET() and not PyThreadState_Get() because the latter
Victor Stinner9204fb82018-10-30 15:13:17 +01001380 abort Python if tstate is NULL. */
Victor Stinner50b48572018-11-01 01:51:40 +01001381 tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 if (tstate != NULL && tstate->curexc_type != NULL) {
1383 /* preserve the existing exception */
1384 PyObject *err_type, *err_value, *err_tb;
1385 PyErr_Fetch(&err_type, &err_value, &err_tb);
INADA Naoki778928b2017-08-03 23:45:15 +09001386 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 /* ignore errors */
1388 PyErr_Restore(err_type, err_value, err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001389 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 return NULL;
1391 }
1392 else {
INADA Naoki778928b2017-08-03 23:45:15 +09001393 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001394 if (ix < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 PyErr_Clear();
1396 return NULL;
1397 }
1398 }
INADA Naokiba609772016-12-07 20:41:42 +09001399 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001400}
1401
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001402/* Same as PyDict_GetItemWithError() but with hash supplied by caller.
1403 This returns NULL *with* an exception set if an exception occurred.
1404 It returns NULL *without* an exception set if the key wasn't present.
1405*/
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001406PyObject *
1407_PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1408{
Victor Stinner742da042016-09-07 17:40:12 -07001409 Py_ssize_t ix;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001410 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001411 PyObject *value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001412
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001413 if (!PyDict_Check(op)) {
1414 PyErr_BadInternalCall();
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001415 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001416 }
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001417
INADA Naoki778928b2017-08-03 23:45:15 +09001418 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001419 if (ix < 0) {
1420 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001421 }
INADA Naokiba609772016-12-07 20:41:42 +09001422 return value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001423}
1424
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001425/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1426 This returns NULL *with* an exception set if an exception occurred.
1427 It returns NULL *without* an exception set if the key wasn't present.
1428*/
1429PyObject *
1430PyDict_GetItemWithError(PyObject *op, PyObject *key)
1431{
Victor Stinner742da042016-09-07 17:40:12 -07001432 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001433 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 PyDictObject*mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001435 PyObject *value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 if (!PyDict_Check(op)) {
1438 PyErr_BadInternalCall();
1439 return NULL;
1440 }
1441 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001442 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 {
1444 hash = PyObject_Hash(key);
1445 if (hash == -1) {
1446 return NULL;
1447 }
1448 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001449
INADA Naoki778928b2017-08-03 23:45:15 +09001450 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001451 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001453 return value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001454}
1455
Brett Cannonfd074152012-04-14 14:10:13 -04001456PyObject *
1457_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1458{
1459 PyObject *kv;
1460 kv = _PyUnicode_FromId(key); /* borrowed */
1461 if (kv == NULL)
1462 return NULL;
1463 return PyDict_GetItemWithError(dp, kv);
1464}
1465
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001466PyObject *
1467_PyDict_GetItemStringWithError(PyObject *v, const char *key)
1468{
1469 PyObject *kv, *rv;
1470 kv = PyUnicode_FromString(key);
1471 if (kv == NULL) {
1472 return NULL;
1473 }
1474 rv = PyDict_GetItemWithError(v, kv);
1475 Py_DECREF(kv);
1476 return rv;
1477}
1478
Victor Stinnerb4efc962015-11-20 09:24:02 +01001479/* Fast version of global value lookup (LOAD_GLOBAL).
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001480 * Lookup in globals, then builtins.
Victor Stinnerb4efc962015-11-20 09:24:02 +01001481 *
1482 * Raise an exception and return NULL if an error occurred (ex: computing the
1483 * key hash failed, key comparison failed, ...). Return NULL if the key doesn't
1484 * exist. Return the value if the key exists.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001485 */
1486PyObject *
1487_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001488{
Victor Stinner742da042016-09-07 17:40:12 -07001489 Py_ssize_t ix;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001490 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09001491 PyObject *value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001492
1493 if (!PyUnicode_CheckExact(key) ||
1494 (hash = ((PyASCIIObject *) key)->hash) == -1)
1495 {
1496 hash = PyObject_Hash(key);
1497 if (hash == -1)
1498 return NULL;
Antoine Pitroue965d972012-02-27 00:45:12 +01001499 }
Victor Stinnerb4efc962015-11-20 09:24:02 +01001500
1501 /* namespace 1: globals */
INADA Naoki778928b2017-08-03 23:45:15 +09001502 ix = globals->ma_keys->dk_lookup(globals, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001503 if (ix == DKIX_ERROR)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001504 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001505 if (ix != DKIX_EMPTY && value != NULL)
1506 return value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001507
1508 /* namespace 2: builtins */
INADA Naoki778928b2017-08-03 23:45:15 +09001509 ix = builtins->ma_keys->dk_lookup(builtins, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001510 if (ix < 0)
Victor Stinnerb4efc962015-11-20 09:24:02 +01001511 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001512 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001513}
1514
Antoine Pitroue965d972012-02-27 00:45:12 +01001515/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1516 * dictionary if it's merely replacing the value for an existing key.
1517 * This means that it's safe to loop over a dictionary with PyDict_Next()
1518 * and occasionally replace a value -- but you can't insert new keys or
1519 * remove them.
1520 */
1521int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001522PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001523{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001524 PyDictObject *mp;
1525 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001526 if (!PyDict_Check(op)) {
1527 PyErr_BadInternalCall();
1528 return -1;
1529 }
1530 assert(key);
1531 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001532 mp = (PyDictObject *)op;
1533 if (!PyUnicode_CheckExact(key) ||
1534 (hash = ((PyASCIIObject *) key)->hash) == -1)
1535 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001536 hash = PyObject_Hash(key);
1537 if (hash == -1)
1538 return -1;
1539 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001540
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001541 if (mp->ma_keys == Py_EMPTY_KEYS) {
1542 return insert_to_emptydict(mp, key, hash, value);
1543 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001544 /* insertdict() handles any resizing that might be necessary */
1545 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001546}
1547
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001548int
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001549_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1550 Py_hash_t hash)
1551{
1552 PyDictObject *mp;
1553
1554 if (!PyDict_Check(op)) {
1555 PyErr_BadInternalCall();
1556 return -1;
1557 }
1558 assert(key);
1559 assert(value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001560 assert(hash != -1);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001561 mp = (PyDictObject *)op;
1562
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001563 if (mp->ma_keys == Py_EMPTY_KEYS) {
1564 return insert_to_emptydict(mp, key, hash, value);
1565 }
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001566 /* insertdict() handles any resizing that might be necessary */
1567 return insertdict(mp, key, hash, value);
1568}
1569
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001570static int
INADA Naoki778928b2017-08-03 23:45:15 +09001571delitem_common(PyDictObject *mp, Py_hash_t hash, Py_ssize_t ix,
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001572 PyObject *old_value)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001573{
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001574 PyObject *old_key;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001575 PyDictKeyEntry *ep;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001576
INADA Naoki778928b2017-08-03 23:45:15 +09001577 Py_ssize_t hashpos = lookdict_index(mp->ma_keys, hash, ix);
1578 assert(hashpos >= 0);
1579
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001580 mp->ma_used--;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001581 mp->ma_version_tag = DICT_NEXT_VERSION();
1582 ep = &DK_ENTRIES(mp->ma_keys)[ix];
INADA Naokia7576492018-11-14 18:39:27 +09001583 dictkeys_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001584 ENSURE_ALLOWS_DELETIONS(mp);
1585 old_key = ep->me_key;
1586 ep->me_key = NULL;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001587 ep->me_value = NULL;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001588 Py_DECREF(old_key);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001589 Py_DECREF(old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001590
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001591 ASSERT_CONSISTENT(mp);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001592 return 0;
1593}
1594
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001595int
Tim Peters1f5871e2000-07-04 17:44:48 +00001596PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001597{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001598 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 assert(key);
1600 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001601 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001602 hash = PyObject_Hash(key);
1603 if (hash == -1)
1604 return -1;
1605 }
Victor Stinner742da042016-09-07 17:40:12 -07001606
1607 return _PyDict_DelItem_KnownHash(op, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001608}
1609
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001610int
1611_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1612{
INADA Naoki778928b2017-08-03 23:45:15 +09001613 Py_ssize_t ix;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001614 PyDictObject *mp;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001615 PyObject *old_value;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001616
1617 if (!PyDict_Check(op)) {
1618 PyErr_BadInternalCall();
1619 return -1;
1620 }
1621 assert(key);
1622 assert(hash != -1);
1623 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001624 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001625 if (ix == DKIX_ERROR)
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001626 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09001627 if (ix == DKIX_EMPTY || old_value == NULL) {
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001628 _PyErr_SetKeyError(key);
1629 return -1;
1630 }
Victor Stinner78601a32016-09-09 19:28:36 -07001631
1632 // Split table doesn't allow deletion. Combine it.
1633 if (_PyDict_HasSplitTable(mp)) {
1634 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1635 return -1;
1636 }
INADA Naoki778928b2017-08-03 23:45:15 +09001637 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001638 assert(ix >= 0);
1639 }
1640
INADA Naoki778928b2017-08-03 23:45:15 +09001641 return delitem_common(mp, hash, ix, old_value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001642}
1643
Antoine Pitroud741ed42016-12-27 14:23:43 +01001644/* This function promises that the predicate -> deletion sequence is atomic
1645 * (i.e. protected by the GIL), assuming the predicate itself doesn't
1646 * release the GIL.
1647 */
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001648int
1649_PyDict_DelItemIf(PyObject *op, PyObject *key,
1650 int (*predicate)(PyObject *value))
1651{
Antoine Pitroud741ed42016-12-27 14:23:43 +01001652 Py_ssize_t hashpos, ix;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001653 PyDictObject *mp;
1654 Py_hash_t hash;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001655 PyObject *old_value;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001656 int res;
1657
1658 if (!PyDict_Check(op)) {
1659 PyErr_BadInternalCall();
1660 return -1;
1661 }
1662 assert(key);
1663 hash = PyObject_Hash(key);
1664 if (hash == -1)
1665 return -1;
1666 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001667 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001668 if (ix == DKIX_ERROR)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001669 return -1;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001670 if (ix == DKIX_EMPTY || old_value == NULL) {
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001671 _PyErr_SetKeyError(key);
1672 return -1;
1673 }
Antoine Pitroud741ed42016-12-27 14:23:43 +01001674
1675 // Split table doesn't allow deletion. Combine it.
1676 if (_PyDict_HasSplitTable(mp)) {
1677 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1678 return -1;
1679 }
INADA Naoki778928b2017-08-03 23:45:15 +09001680 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001681 assert(ix >= 0);
1682 }
1683
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001684 res = predicate(old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001685 if (res == -1)
1686 return -1;
INADA Naoki778928b2017-08-03 23:45:15 +09001687
1688 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1689 assert(hashpos >= 0);
1690
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001691 if (res > 0)
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001692 return delitem_common(mp, hashpos, ix, old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001693 else
1694 return 0;
1695}
1696
1697
Guido van Rossum25831651993-05-19 14:50:45 +00001698void
Tim Peters1f5871e2000-07-04 17:44:48 +00001699PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001700{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001702 PyDictKeysObject *oldkeys;
1703 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001706 if (!PyDict_Check(op))
1707 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001708 mp = ((PyDictObject *)op);
1709 oldkeys = mp->ma_keys;
1710 oldvalues = mp->ma_values;
1711 if (oldvalues == empty_values)
1712 return;
1713 /* Empty the dict... */
INADA Naokia7576492018-11-14 18:39:27 +09001714 dictkeys_incref(Py_EMPTY_KEYS);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001715 mp->ma_keys = Py_EMPTY_KEYS;
1716 mp->ma_values = empty_values;
1717 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001718 mp->ma_version_tag = DICT_NEXT_VERSION();
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001719 /* ...then clear the keys and values */
1720 if (oldvalues != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001721 n = oldkeys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001722 for (i = 0; i < n; i++)
1723 Py_CLEAR(oldvalues[i]);
1724 free_values(oldvalues);
INADA Naokia7576492018-11-14 18:39:27 +09001725 dictkeys_decref(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001726 }
1727 else {
1728 assert(oldkeys->dk_refcnt == 1);
INADA Naokia7576492018-11-14 18:39:27 +09001729 dictkeys_decref(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001730 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001731 ASSERT_CONSISTENT(mp);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001732}
1733
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001734/* Internal version of PyDict_Next that returns a hash value in addition
1735 * to the key and value.
1736 * Return 1 on success, return 0 when the reached the end of the dictionary
1737 * (or if op is not a dictionary)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001738 */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001739int
1740_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1741 PyObject **pvalue, Py_hash_t *phash)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001742{
INADA Naokica2d8be2016-11-04 16:59:10 +09001743 Py_ssize_t i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001744 PyDictObject *mp;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001745 PyDictKeyEntry *entry_ptr;
1746 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001747
1748 if (!PyDict_Check(op))
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001749 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 mp = (PyDictObject *)op;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001751 i = *ppos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001752 if (mp->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09001753 if (i < 0 || i >= mp->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001754 return 0;
INADA Naokica2d8be2016-11-04 16:59:10 +09001755 /* values of split table is always dense */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001756 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
INADA Naokica2d8be2016-11-04 16:59:10 +09001757 value = mp->ma_values[i];
1758 assert(value != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001760 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09001761 Py_ssize_t n = mp->ma_keys->dk_nentries;
1762 if (i < 0 || i >= n)
1763 return 0;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001764 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
1765 while (i < n && entry_ptr->me_value == NULL) {
1766 entry_ptr++;
1767 i++;
Victor Stinner742da042016-09-07 17:40:12 -07001768 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001769 if (i >= n)
1770 return 0;
1771 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001773 *ppos = i+1;
1774 if (pkey)
1775 *pkey = entry_ptr->me_key;
1776 if (phash)
1777 *phash = entry_ptr->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001778 if (pvalue)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001779 *pvalue = value;
1780 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001781}
1782
Tim Peters080c88b2003-02-15 03:01:11 +00001783/*
1784 * Iterate over a dict. Use like so:
1785 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001786 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001787 * PyObject *key, *value;
1788 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001789 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001790 * Refer to borrowed references in key and value.
Tim Peters080c88b2003-02-15 03:01:11 +00001791 * }
1792 *
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001793 * Return 1 on success, return 0 when the reached the end of the dictionary
1794 * (or if op is not a dictionary)
1795 *
Tim Peters080c88b2003-02-15 03:01:11 +00001796 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001797 * mutates the dict. One exception: it is safe if the loop merely changes
1798 * the values associated with the keys (but doesn't insert new keys or
1799 * delete keys), via PyDict_SetItem().
1800 */
Guido van Rossum25831651993-05-19 14:50:45 +00001801int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001802PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001803{
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001804 return _PyDict_Next(op, ppos, pkey, pvalue, NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001805}
1806
Eric Snow96c6af92015-05-29 22:21:39 -06001807/* Internal version of dict.pop(). */
1808PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001809_PyDict_Pop_KnownHash(PyObject *dict, PyObject *key, Py_hash_t hash, PyObject *deflt)
Eric Snow96c6af92015-05-29 22:21:39 -06001810{
Victor Stinner742da042016-09-07 17:40:12 -07001811 Py_ssize_t ix, hashpos;
Eric Snow96c6af92015-05-29 22:21:39 -06001812 PyObject *old_value, *old_key;
1813 PyDictKeyEntry *ep;
Yury Selivanov684ef2c2016-10-28 19:01:21 -04001814 PyDictObject *mp;
1815
1816 assert(PyDict_Check(dict));
1817 mp = (PyDictObject *)dict;
Eric Snow96c6af92015-05-29 22:21:39 -06001818
1819 if (mp->ma_used == 0) {
1820 if (deflt) {
1821 Py_INCREF(deflt);
1822 return deflt;
1823 }
1824 _PyErr_SetKeyError(key);
1825 return NULL;
1826 }
INADA Naoki778928b2017-08-03 23:45:15 +09001827 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001828 if (ix == DKIX_ERROR)
Eric Snow96c6af92015-05-29 22:21:39 -06001829 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001830 if (ix == DKIX_EMPTY || old_value == NULL) {
Eric Snow96c6af92015-05-29 22:21:39 -06001831 if (deflt) {
1832 Py_INCREF(deflt);
1833 return deflt;
1834 }
1835 _PyErr_SetKeyError(key);
1836 return NULL;
1837 }
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001838
Victor Stinner78601a32016-09-09 19:28:36 -07001839 // Split table doesn't allow deletion. Combine it.
1840 if (_PyDict_HasSplitTable(mp)) {
1841 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1842 return NULL;
1843 }
INADA Naoki778928b2017-08-03 23:45:15 +09001844 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001845 assert(ix >= 0);
1846 }
1847
INADA Naoki778928b2017-08-03 23:45:15 +09001848 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1849 assert(hashpos >= 0);
Victor Stinner78601a32016-09-09 19:28:36 -07001850 assert(old_value != NULL);
Eric Snow96c6af92015-05-29 22:21:39 -06001851 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001852 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokia7576492018-11-14 18:39:27 +09001853 dictkeys_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
Victor Stinner78601a32016-09-09 19:28:36 -07001854 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1855 ENSURE_ALLOWS_DELETIONS(mp);
1856 old_key = ep->me_key;
1857 ep->me_key = NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001858 ep->me_value = NULL;
Victor Stinner78601a32016-09-09 19:28:36 -07001859 Py_DECREF(old_key);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001860
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001861 ASSERT_CONSISTENT(mp);
Eric Snow96c6af92015-05-29 22:21:39 -06001862 return old_value;
1863}
1864
Serhiy Storchaka67796522017-01-12 18:34:33 +02001865PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001866_PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt)
Serhiy Storchaka67796522017-01-12 18:34:33 +02001867{
1868 Py_hash_t hash;
1869
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001870 if (((PyDictObject *)dict)->ma_used == 0) {
Serhiy Storchaka67796522017-01-12 18:34:33 +02001871 if (deflt) {
1872 Py_INCREF(deflt);
1873 return deflt;
1874 }
1875 _PyErr_SetKeyError(key);
1876 return NULL;
1877 }
1878 if (!PyUnicode_CheckExact(key) ||
1879 (hash = ((PyASCIIObject *) key)->hash) == -1) {
1880 hash = PyObject_Hash(key);
1881 if (hash == -1)
1882 return NULL;
1883 }
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001884 return _PyDict_Pop_KnownHash(dict, key, hash, deflt);
Serhiy Storchaka67796522017-01-12 18:34:33 +02001885}
1886
Eric Snow96c6af92015-05-29 22:21:39 -06001887/* Internal version of dict.from_keys(). It is subclass-friendly. */
1888PyObject *
1889_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1890{
1891 PyObject *it; /* iter(iterable) */
1892 PyObject *key;
1893 PyObject *d;
1894 int status;
1895
Victor Stinnera5ed5f02016-12-06 18:45:50 +01001896 d = _PyObject_CallNoArg(cls);
Eric Snow96c6af92015-05-29 22:21:39 -06001897 if (d == NULL)
1898 return NULL;
1899
1900 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
1901 if (PyDict_CheckExact(iterable)) {
1902 PyDictObject *mp = (PyDictObject *)d;
1903 PyObject *oldvalue;
1904 Py_ssize_t pos = 0;
1905 PyObject *key;
1906 Py_hash_t hash;
1907
Serhiy Storchakac61ac162017-03-21 08:52:38 +02001908 if (dictresize(mp, ESTIMATE_SIZE(PyDict_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001909 Py_DECREF(d);
1910 return NULL;
1911 }
1912
1913 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
1914 if (insertdict(mp, key, hash, value)) {
1915 Py_DECREF(d);
1916 return NULL;
1917 }
1918 }
1919 return d;
1920 }
1921 if (PyAnySet_CheckExact(iterable)) {
1922 PyDictObject *mp = (PyDictObject *)d;
1923 Py_ssize_t pos = 0;
1924 PyObject *key;
1925 Py_hash_t hash;
1926
Victor Stinner742da042016-09-07 17:40:12 -07001927 if (dictresize(mp, ESTIMATE_SIZE(PySet_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001928 Py_DECREF(d);
1929 return NULL;
1930 }
1931
1932 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
1933 if (insertdict(mp, key, hash, value)) {
1934 Py_DECREF(d);
1935 return NULL;
1936 }
1937 }
1938 return d;
1939 }
1940 }
1941
1942 it = PyObject_GetIter(iterable);
1943 if (it == NULL){
1944 Py_DECREF(d);
1945 return NULL;
1946 }
1947
1948 if (PyDict_CheckExact(d)) {
1949 while ((key = PyIter_Next(it)) != NULL) {
1950 status = PyDict_SetItem(d, key, value);
1951 Py_DECREF(key);
1952 if (status < 0)
1953 goto Fail;
1954 }
1955 } else {
1956 while ((key = PyIter_Next(it)) != NULL) {
1957 status = PyObject_SetItem(d, key, value);
1958 Py_DECREF(key);
1959 if (status < 0)
1960 goto Fail;
1961 }
1962 }
1963
1964 if (PyErr_Occurred())
1965 goto Fail;
1966 Py_DECREF(it);
1967 return d;
1968
1969Fail:
1970 Py_DECREF(it);
1971 Py_DECREF(d);
1972 return NULL;
1973}
1974
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001975/* Methods */
1976
1977static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001978dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001979{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001980 PyObject **values = mp->ma_values;
1981 PyDictKeysObject *keys = mp->ma_keys;
1982 Py_ssize_t i, n;
INADA Naokia6296d32017-08-24 14:55:17 +09001983
1984 /* bpo-31095: UnTrack is needed before calling any callbacks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001985 PyObject_GC_UnTrack(mp);
Jeroen Demeyer351c6742019-05-10 19:21:11 +02001986 Py_TRASHCAN_BEGIN(mp, dict_dealloc)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001987 if (values != NULL) {
1988 if (values != empty_values) {
Victor Stinner742da042016-09-07 17:40:12 -07001989 for (i = 0, n = mp->ma_keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001990 Py_XDECREF(values[i]);
1991 }
1992 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001993 }
INADA Naokia7576492018-11-14 18:39:27 +09001994 dictkeys_decref(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02001996 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001997 assert(keys->dk_refcnt == 1);
INADA Naokia7576492018-11-14 18:39:27 +09001998 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001999 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
2001 free_list[numfree++] = mp;
2002 else
2003 Py_TYPE(mp)->tp_free((PyObject *)mp);
Jeroen Demeyer351c6742019-05-10 19:21:11 +02002004 Py_TRASHCAN_END
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002005}
2006
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002007
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002008static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002009dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002010{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002011 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01002012 PyObject *key = NULL, *value = NULL;
2013 _PyUnicodeWriter writer;
2014 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00002015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016 i = Py_ReprEnter((PyObject *)mp);
2017 if (i != 0) {
2018 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
2019 }
Guido van Rossum255443b1998-04-10 22:47:14 +00002020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002021 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01002022 Py_ReprLeave((PyObject *)mp);
2023 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002024 }
Tim Petersa7259592001-06-16 05:11:17 +00002025
Victor Stinnerf91929b2013-11-19 13:07:38 +01002026 _PyUnicodeWriter_Init(&writer);
2027 writer.overallocate = 1;
2028 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
2029 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00002030
Victor Stinnerf91929b2013-11-19 13:07:38 +01002031 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
2032 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00002033
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002034 /* Do repr() on each key+value pair, and insert ": " between them.
2035 Note that repr may mutate the dict. */
2036 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01002037 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002038 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01002039 PyObject *s;
2040 int res;
2041
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002042 /* Prevent repr from deleting key or value during key format. */
2043 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002044 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02002045
Victor Stinnerf91929b2013-11-19 13:07:38 +01002046 if (!first) {
2047 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
2048 goto error;
2049 }
2050 first = 0;
2051
2052 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01002054 goto error;
2055 res = _PyUnicodeWriter_WriteStr(&writer, s);
2056 Py_DECREF(s);
2057 if (res < 0)
2058 goto error;
2059
2060 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
2061 goto error;
2062
2063 s = PyObject_Repr(value);
2064 if (s == NULL)
2065 goto error;
2066 res = _PyUnicodeWriter_WriteStr(&writer, s);
2067 Py_DECREF(s);
2068 if (res < 0)
2069 goto error;
2070
2071 Py_CLEAR(key);
2072 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002073 }
Tim Petersa7259592001-06-16 05:11:17 +00002074
Victor Stinnerf91929b2013-11-19 13:07:38 +01002075 writer.overallocate = 0;
2076 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
2077 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00002078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002079 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01002080
2081 return _PyUnicodeWriter_Finish(&writer);
2082
2083error:
2084 Py_ReprLeave((PyObject *)mp);
2085 _PyUnicodeWriter_Dealloc(&writer);
2086 Py_XDECREF(key);
2087 Py_XDECREF(value);
2088 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002089}
2090
Martin v. Löwis18e16552006-02-15 17:27:45 +00002091static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002092dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002093{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002094 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002095}
2096
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002097static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002098dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002099{
Victor Stinner742da042016-09-07 17:40:12 -07002100 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002101 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09002102 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002104 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002105 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002106 hash = PyObject_Hash(key);
2107 if (hash == -1)
2108 return NULL;
2109 }
INADA Naoki778928b2017-08-03 23:45:15 +09002110 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002111 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002113 if (ix == DKIX_EMPTY || value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114 if (!PyDict_CheckExact(mp)) {
2115 /* Look up __missing__ method if we're a subclass. */
2116 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05002117 _Py_IDENTIFIER(__missing__);
2118 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002119 if (missing != NULL) {
Jeroen Demeyer196a5302019-07-04 12:31:34 +02002120 res = _PyObject_CallOneArg(missing, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 Py_DECREF(missing);
2122 return res;
2123 }
2124 else if (PyErr_Occurred())
2125 return NULL;
2126 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07002127 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002128 return NULL;
2129 }
INADA Naokiba609772016-12-07 20:41:42 +09002130 Py_INCREF(value);
2131 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002132}
2133
2134static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002135dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002136{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002137 if (w == NULL)
2138 return PyDict_DelItem((PyObject *)mp, v);
2139 else
2140 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002141}
2142
Guido van Rossuma9e7a811997-05-13 21:02:11 +00002143static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002144 (lenfunc)dict_length, /*mp_length*/
2145 (binaryfunc)dict_subscript, /*mp_subscript*/
2146 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002147};
2148
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002149static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002150dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002151{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002152 PyObject *v;
2153 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002154 PyDictKeyEntry *ep;
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002155 Py_ssize_t n, offset;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002156 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002157
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002158 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002159 n = mp->ma_used;
2160 v = PyList_New(n);
2161 if (v == NULL)
2162 return NULL;
2163 if (n != mp->ma_used) {
2164 /* Durnit. The allocations caused the dict to resize.
2165 * Just start over, this shouldn't normally happen.
2166 */
2167 Py_DECREF(v);
2168 goto again;
2169 }
Victor Stinner742da042016-09-07 17:40:12 -07002170 ep = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002171 if (mp->ma_values) {
2172 value_ptr = mp->ma_values;
2173 offset = sizeof(PyObject *);
2174 }
2175 else {
2176 value_ptr = &ep[0].me_value;
2177 offset = sizeof(PyDictKeyEntry);
2178 }
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002179 for (i = 0, j = 0; j < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002180 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002181 PyObject *key = ep[i].me_key;
2182 Py_INCREF(key);
2183 PyList_SET_ITEM(v, j, key);
2184 j++;
2185 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002186 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002187 }
2188 assert(j == n);
2189 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002190}
2191
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002192static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002193dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002194{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002195 PyObject *v;
2196 Py_ssize_t i, j;
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002197 PyDictKeyEntry *ep;
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002198 Py_ssize_t n, offset;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002199 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002200
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002201 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002202 n = mp->ma_used;
2203 v = PyList_New(n);
2204 if (v == NULL)
2205 return NULL;
2206 if (n != mp->ma_used) {
2207 /* Durnit. The allocations caused the dict to resize.
2208 * Just start over, this shouldn't normally happen.
2209 */
2210 Py_DECREF(v);
2211 goto again;
2212 }
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002213 ep = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002214 if (mp->ma_values) {
2215 value_ptr = mp->ma_values;
2216 offset = sizeof(PyObject *);
2217 }
2218 else {
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002219 value_ptr = &ep[0].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002220 offset = sizeof(PyDictKeyEntry);
2221 }
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002222 for (i = 0, j = 0; j < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002223 PyObject *value = *value_ptr;
2224 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2225 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002226 Py_INCREF(value);
2227 PyList_SET_ITEM(v, j, value);
2228 j++;
2229 }
2230 }
2231 assert(j == n);
2232 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002233}
2234
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002235static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002236dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002237{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002238 PyObject *v;
2239 Py_ssize_t i, j, n;
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002240 Py_ssize_t offset;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002241 PyObject *item, *key;
2242 PyDictKeyEntry *ep;
2243 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 /* Preallocate the list of tuples, to avoid allocations during
2246 * the loop over the items, which could trigger GC, which
2247 * could resize the dict. :-(
2248 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002249 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002250 n = mp->ma_used;
2251 v = PyList_New(n);
2252 if (v == NULL)
2253 return NULL;
2254 for (i = 0; i < n; i++) {
2255 item = PyTuple_New(2);
2256 if (item == NULL) {
2257 Py_DECREF(v);
2258 return NULL;
2259 }
2260 PyList_SET_ITEM(v, i, item);
2261 }
2262 if (n != mp->ma_used) {
2263 /* Durnit. The allocations caused the dict to resize.
2264 * Just start over, this shouldn't normally happen.
2265 */
2266 Py_DECREF(v);
2267 goto again;
2268 }
2269 /* Nothing we do below makes any function calls. */
Victor Stinner742da042016-09-07 17:40:12 -07002270 ep = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002271 if (mp->ma_values) {
2272 value_ptr = mp->ma_values;
2273 offset = sizeof(PyObject *);
2274 }
2275 else {
2276 value_ptr = &ep[0].me_value;
2277 offset = sizeof(PyDictKeyEntry);
2278 }
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002279 for (i = 0, j = 0; j < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002280 PyObject *value = *value_ptr;
2281 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2282 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002283 key = ep[i].me_key;
2284 item = PyList_GET_ITEM(v, j);
2285 Py_INCREF(key);
2286 PyTuple_SET_ITEM(item, 0, key);
2287 Py_INCREF(value);
2288 PyTuple_SET_ITEM(item, 1, value);
2289 j++;
2290 }
2291 }
2292 assert(j == n);
2293 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002294}
2295
Larry Hastings5c661892014-01-24 06:17:25 -08002296/*[clinic input]
2297@classmethod
2298dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08002299 iterable: object
2300 value: object=None
2301 /
2302
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002303Create a new dictionary with keys from iterable and values set to value.
Larry Hastings5c661892014-01-24 06:17:25 -08002304[clinic start generated code]*/
2305
Larry Hastings5c661892014-01-24 06:17:25 -08002306static PyObject *
2307dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002308/*[clinic end generated code: output=8fb98e4b10384999 input=382ba4855d0f74c3]*/
Larry Hastings5c661892014-01-24 06:17:25 -08002309{
Eric Snow96c6af92015-05-29 22:21:39 -06002310 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002311}
2312
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002313static int
Victor Stinner742da042016-09-07 17:40:12 -07002314dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
2315 const char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002316{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002317 PyObject *arg = NULL;
2318 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002319
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002320 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002321 result = -1;
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002322 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002323 else if (arg != NULL) {
Serhiy Storchakaf163aea2019-09-25 09:47:00 +03002324 if (PyDict_CheckExact(arg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002325 result = PyDict_Merge(self, arg, 1);
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002326 }
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002327 else {
Serhiy Storchakaf163aea2019-09-25 09:47:00 +03002328 _Py_IDENTIFIER(keys);
2329 PyObject *func;
2330 if (_PyObject_LookupAttrId(arg, &PyId_keys, &func) < 0) {
2331 result = -1;
2332 }
2333 else if (func != NULL) {
2334 Py_DECREF(func);
2335 result = PyDict_Merge(self, arg, 1);
2336 }
2337 else {
2338 result = PyDict_MergeFromSeq2(self, arg, 1);
2339 }
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002340 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002341 }
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002343 if (result == 0 && kwds != NULL) {
2344 if (PyArg_ValidateKeywordArguments(kwds))
2345 result = PyDict_Merge(self, kwds, 1);
2346 else
2347 result = -1;
2348 }
2349 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002350}
2351
Victor Stinner91f0d4a2017-01-19 12:45:06 +01002352/* Note: dict.update() uses the METH_VARARGS|METH_KEYWORDS calling convention.
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002353 Using METH_FASTCALL|METH_KEYWORDS would make dict.update(**dict2) calls
2354 slower, see the issue #29312. */
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002355static PyObject *
2356dict_update(PyObject *self, PyObject *args, PyObject *kwds)
2357{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 if (dict_update_common(self, args, kwds, "update") != -1)
2359 Py_RETURN_NONE;
2360 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002361}
2362
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002363/* Update unconditionally replaces existing items.
2364 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00002365 otherwise it leaves existing items unchanged.
2366
2367 PyDict_{Update,Merge} update/merge from a mapping object.
2368
Tim Petersf582b822001-12-11 18:51:08 +00002369 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00002370 producing iterable objects of length 2.
2371*/
2372
Tim Petersf582b822001-12-11 18:51:08 +00002373int
Tim Peters1fc240e2001-10-26 05:06:50 +00002374PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
2375{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 PyObject *it; /* iter(seq2) */
2377 Py_ssize_t i; /* index into seq2 of current element */
2378 PyObject *item; /* seq2[i] */
2379 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00002380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002381 assert(d != NULL);
2382 assert(PyDict_Check(d));
2383 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00002384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002385 it = PyObject_GetIter(seq2);
2386 if (it == NULL)
2387 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 for (i = 0; ; ++i) {
2390 PyObject *key, *value;
2391 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00002392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 fast = NULL;
2394 item = PyIter_Next(it);
2395 if (item == NULL) {
2396 if (PyErr_Occurred())
2397 goto Fail;
2398 break;
2399 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 /* Convert item to sequence, and verify length 2. */
2402 fast = PySequence_Fast(item, "");
2403 if (fast == NULL) {
2404 if (PyErr_ExceptionMatches(PyExc_TypeError))
2405 PyErr_Format(PyExc_TypeError,
2406 "cannot convert dictionary update "
2407 "sequence element #%zd to a sequence",
2408 i);
2409 goto Fail;
2410 }
2411 n = PySequence_Fast_GET_SIZE(fast);
2412 if (n != 2) {
2413 PyErr_Format(PyExc_ValueError,
2414 "dictionary update sequence element #%zd "
2415 "has length %zd; 2 is required",
2416 i, n);
2417 goto Fail;
2418 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002420 /* Update/merge with this (key, value) pair. */
2421 key = PySequence_Fast_GET_ITEM(fast, 0);
2422 value = PySequence_Fast_GET_ITEM(fast, 1);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002423 Py_INCREF(key);
2424 Py_INCREF(value);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002425 if (override) {
2426 if (PyDict_SetItem(d, key, value) < 0) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002427 Py_DECREF(key);
2428 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 goto Fail;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002430 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002431 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002432 else if (PyDict_GetItemWithError(d, key) == NULL) {
2433 if (PyErr_Occurred() || PyDict_SetItem(d, key, value) < 0) {
2434 Py_DECREF(key);
2435 Py_DECREF(value);
2436 goto Fail;
2437 }
2438 }
2439
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002440 Py_DECREF(key);
2441 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002442 Py_DECREF(fast);
2443 Py_DECREF(item);
2444 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002446 i = 0;
Victor Stinner0fc91ee2019-04-12 21:51:34 +02002447 ASSERT_CONSISTENT(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002448 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002449Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002450 Py_XDECREF(item);
2451 Py_XDECREF(fast);
2452 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002453Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002454 Py_DECREF(it);
2455 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002456}
2457
doko@ubuntu.comc96df682016-10-11 08:04:02 +02002458static int
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002459dict_merge(PyObject *a, PyObject *b, int override)
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002460{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002461 PyDictObject *mp, *other;
2462 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -07002463 PyDictKeyEntry *entry, *ep0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002464
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002465 assert(0 <= override && override <= 2);
2466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002467 /* We accept for the argument either a concrete dictionary object,
2468 * or an abstract "mapping" object. For the former, we can do
2469 * things quite efficiently. For the latter, we only require that
2470 * PyMapping_Keys() and PyObject_GetItem() be supported.
2471 */
2472 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2473 PyErr_BadInternalCall();
2474 return -1;
2475 }
2476 mp = (PyDictObject*)a;
INADA Naoki2aaf98c2018-09-26 12:59:00 +09002477 if (PyDict_Check(b) && (Py_TYPE(b)->tp_iter == (getiterfunc)dict_iter)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002478 other = (PyDictObject*)b;
2479 if (other == mp || other->ma_used == 0)
2480 /* a.update(a) or a.update({}); nothing to do */
2481 return 0;
2482 if (mp->ma_used == 0)
2483 /* Since the target dict is empty, PyDict_GetItem()
2484 * always returns NULL. Setting override to 1
2485 * skips the unnecessary test.
2486 */
2487 override = 1;
2488 /* Do one big resize at the start, rather than
2489 * incrementally resizing as we insert new items. Expect
2490 * that there will be no (or few) overlapping keys.
2491 */
INADA Naokib1152be2016-10-27 19:26:50 +09002492 if (USABLE_FRACTION(mp->ma_keys->dk_size) < other->ma_used) {
2493 if (dictresize(mp, ESTIMATE_SIZE(mp->ma_used + other->ma_used))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002494 return -1;
INADA Naokib1152be2016-10-27 19:26:50 +09002495 }
2496 }
Victor Stinner742da042016-09-07 17:40:12 -07002497 ep0 = DK_ENTRIES(other->ma_keys);
2498 for (i = 0, n = other->ma_keys->dk_nentries; i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002499 PyObject *key, *value;
2500 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002501 entry = &ep0[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002502 key = entry->me_key;
2503 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002504 if (other->ma_values)
2505 value = other->ma_values[i];
2506 else
2507 value = entry->me_value;
2508
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002509 if (value != NULL) {
2510 int err = 0;
2511 Py_INCREF(key);
2512 Py_INCREF(value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002513 if (override == 1)
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002514 err = insertdict(mp, key, hash, value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002515 else if (_PyDict_GetItem_KnownHash(a, key, hash) == NULL) {
2516 if (PyErr_Occurred()) {
2517 Py_DECREF(value);
2518 Py_DECREF(key);
2519 return -1;
2520 }
2521 err = insertdict(mp, key, hash, value);
2522 }
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002523 else if (override != 0) {
2524 _PyErr_SetKeyError(key);
2525 Py_DECREF(value);
2526 Py_DECREF(key);
2527 return -1;
2528 }
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002529 Py_DECREF(value);
2530 Py_DECREF(key);
2531 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002532 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002533
Victor Stinner742da042016-09-07 17:40:12 -07002534 if (n != other->ma_keys->dk_nentries) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002535 PyErr_SetString(PyExc_RuntimeError,
2536 "dict mutated during update");
2537 return -1;
2538 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002539 }
2540 }
2541 }
2542 else {
2543 /* Do it the generic, slower way */
2544 PyObject *keys = PyMapping_Keys(b);
2545 PyObject *iter;
2546 PyObject *key, *value;
2547 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002549 if (keys == NULL)
2550 /* Docstring says this is equivalent to E.keys() so
2551 * if E doesn't have a .keys() method we want
2552 * AttributeError to percolate up. Might as well
2553 * do the same for any other error.
2554 */
2555 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002557 iter = PyObject_GetIter(keys);
2558 Py_DECREF(keys);
2559 if (iter == NULL)
2560 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002562 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002563 if (override != 1) {
2564 if (PyDict_GetItemWithError(a, key) != NULL) {
2565 if (override != 0) {
2566 _PyErr_SetKeyError(key);
2567 Py_DECREF(key);
2568 Py_DECREF(iter);
2569 return -1;
2570 }
2571 Py_DECREF(key);
2572 continue;
2573 }
2574 else if (PyErr_Occurred()) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002575 Py_DECREF(key);
2576 Py_DECREF(iter);
2577 return -1;
2578 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002579 }
2580 value = PyObject_GetItem(b, key);
2581 if (value == NULL) {
2582 Py_DECREF(iter);
2583 Py_DECREF(key);
2584 return -1;
2585 }
2586 status = PyDict_SetItem(a, key, value);
2587 Py_DECREF(key);
2588 Py_DECREF(value);
2589 if (status < 0) {
2590 Py_DECREF(iter);
2591 return -1;
2592 }
2593 }
2594 Py_DECREF(iter);
2595 if (PyErr_Occurred())
2596 /* Iterator completed, via error */
2597 return -1;
2598 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +02002599 ASSERT_CONSISTENT(a);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002600 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002601}
2602
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002603int
2604PyDict_Update(PyObject *a, PyObject *b)
2605{
2606 return dict_merge(a, b, 1);
2607}
2608
2609int
2610PyDict_Merge(PyObject *a, PyObject *b, int override)
2611{
2612 /* XXX Deprecate override not in (0, 1). */
2613 return dict_merge(a, b, override != 0);
2614}
2615
2616int
2617_PyDict_MergeEx(PyObject *a, PyObject *b, int override)
2618{
2619 return dict_merge(a, b, override);
2620}
2621
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002622static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302623dict_copy(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002624{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002625 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002626}
2627
2628PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002629PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002631 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002632 PyDictObject *mp;
2633 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002635 if (o == NULL || !PyDict_Check(o)) {
2636 PyErr_BadInternalCall();
2637 return NULL;
2638 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002639
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002640 mp = (PyDictObject *)o;
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002641 if (mp->ma_used == 0) {
2642 /* The dict is empty; just return a new dict. */
2643 return PyDict_New();
2644 }
2645
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002646 if (_PyDict_HasSplitTable(mp)) {
2647 PyDictObject *split_copy;
Victor Stinner742da042016-09-07 17:40:12 -07002648 Py_ssize_t size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
2649 PyObject **newvalues;
2650 newvalues = new_values(size);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002651 if (newvalues == NULL)
2652 return PyErr_NoMemory();
2653 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2654 if (split_copy == NULL) {
2655 free_values(newvalues);
2656 return NULL;
2657 }
2658 split_copy->ma_values = newvalues;
2659 split_copy->ma_keys = mp->ma_keys;
2660 split_copy->ma_used = mp->ma_used;
INADA Naokid1c82c52018-04-03 11:43:53 +09002661 split_copy->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokia7576492018-11-14 18:39:27 +09002662 dictkeys_incref(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002663 for (i = 0, n = size; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002664 PyObject *value = mp->ma_values[i];
2665 Py_XINCREF(value);
2666 split_copy->ma_values[i] = value;
2667 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002668 if (_PyObject_GC_IS_TRACKED(mp))
2669 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002670 return (PyObject *)split_copy;
2671 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002672
2673 if (PyDict_CheckExact(mp) && mp->ma_values == NULL &&
2674 (mp->ma_used >= (mp->ma_keys->dk_nentries * 2) / 3))
2675 {
2676 /* Use fast-copy if:
2677
2678 (1) 'mp' is an instance of a subclassed dict; and
2679
2680 (2) 'mp' is not a split-dict; and
2681
2682 (3) if 'mp' is non-compact ('del' operation does not resize dicts),
2683 do fast-copy only if it has at most 1/3 non-used keys.
2684
Ville Skyttä61f82e02018-04-20 23:08:45 +03002685 The last condition (3) is important to guard against a pathological
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002686 case when a large dict is almost emptied with multiple del/pop
2687 operations and copied after that. In cases like this, we defer to
2688 PyDict_Merge, which produces a compacted copy.
2689 */
2690 return clone_combined_dict(mp);
2691 }
2692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002693 copy = PyDict_New();
2694 if (copy == NULL)
2695 return NULL;
2696 if (PyDict_Merge(copy, o, 1) == 0)
2697 return copy;
2698 Py_DECREF(copy);
2699 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002700}
2701
Martin v. Löwis18e16552006-02-15 17:27:45 +00002702Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002703PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002704{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002705 if (mp == NULL || !PyDict_Check(mp)) {
2706 PyErr_BadInternalCall();
2707 return -1;
2708 }
2709 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002710}
2711
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002712PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002713PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002714{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002715 if (mp == NULL || !PyDict_Check(mp)) {
2716 PyErr_BadInternalCall();
2717 return NULL;
2718 }
2719 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002720}
2721
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002722PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002723PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002724{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002725 if (mp == NULL || !PyDict_Check(mp)) {
2726 PyErr_BadInternalCall();
2727 return NULL;
2728 }
2729 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002730}
2731
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002732PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002733PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002734{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002735 if (mp == NULL || !PyDict_Check(mp)) {
2736 PyErr_BadInternalCall();
2737 return NULL;
2738 }
2739 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002740}
2741
Tim Peterse63415e2001-05-08 04:38:29 +00002742/* Return 1 if dicts equal, 0 if not, -1 if error.
2743 * Gets out as soon as any difference is detected.
2744 * Uses only Py_EQ comparison.
2745 */
2746static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002747dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002748{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002749 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 if (a->ma_used != b->ma_used)
2752 /* can't be equal if # of entries differ */
2753 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002754 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Victor Stinner742da042016-09-07 17:40:12 -07002755 for (i = 0; i < a->ma_keys->dk_nentries; i++) {
2756 PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002757 PyObject *aval;
2758 if (a->ma_values)
2759 aval = a->ma_values[i];
2760 else
2761 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002762 if (aval != NULL) {
2763 int cmp;
2764 PyObject *bval;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002765 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002766 /* temporarily bump aval's refcount to ensure it stays
2767 alive until we're done with it */
2768 Py_INCREF(aval);
2769 /* ditto for key */
2770 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002771 /* reuse the known hash value */
INADA Naoki778928b2017-08-03 23:45:15 +09002772 b->ma_keys->dk_lookup(b, key, ep->me_hash, &bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002773 if (bval == NULL) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002774 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002775 Py_DECREF(aval);
2776 if (PyErr_Occurred())
2777 return -1;
2778 return 0;
2779 }
Dong-hee Na2d5bf562019-12-31 10:04:22 +09002780 Py_INCREF(bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002781 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002782 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002783 Py_DECREF(aval);
Dong-hee Na2d5bf562019-12-31 10:04:22 +09002784 Py_DECREF(bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002785 if (cmp <= 0) /* error or not equal */
2786 return cmp;
2787 }
2788 }
2789 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002790}
Tim Peterse63415e2001-05-08 04:38:29 +00002791
2792static PyObject *
2793dict_richcompare(PyObject *v, PyObject *w, int op)
2794{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002795 int cmp;
2796 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002798 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2799 res = Py_NotImplemented;
2800 }
2801 else if (op == Py_EQ || op == Py_NE) {
2802 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2803 if (cmp < 0)
2804 return NULL;
2805 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2806 }
2807 else
2808 res = Py_NotImplemented;
2809 Py_INCREF(res);
2810 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002811}
Tim Peterse63415e2001-05-08 04:38:29 +00002812
Larry Hastings61272b72014-01-07 12:41:53 -08002813/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002814
2815@coexist
2816dict.__contains__
2817
2818 key: object
2819 /
2820
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002821True if the dictionary has the specified key, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002822[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002823
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002824static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002825dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka19d25972017-02-04 08:05:07 +02002826/*[clinic end generated code: output=a3d03db709ed6e6b input=fe1cb42ad831e820]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002827{
Larry Hastingsc2047262014-01-25 20:43:29 -08002828 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002829 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002830 Py_ssize_t ix;
INADA Naokiba609772016-12-07 20:41:42 +09002831 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002833 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002834 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002835 hash = PyObject_Hash(key);
2836 if (hash == -1)
2837 return NULL;
2838 }
INADA Naoki778928b2017-08-03 23:45:15 +09002839 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002840 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002841 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002842 if (ix == DKIX_EMPTY || value == NULL)
Victor Stinner742da042016-09-07 17:40:12 -07002843 Py_RETURN_FALSE;
2844 Py_RETURN_TRUE;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002845}
2846
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002847/*[clinic input]
2848dict.get
2849
2850 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002851 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002852 /
2853
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002854Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002855[clinic start generated code]*/
2856
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002857static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002858dict_get_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002859/*[clinic end generated code: output=bba707729dee05bf input=279ddb5790b6b107]*/
Barry Warsawc38c5da1997-10-06 17:49:20 +00002860{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002861 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002862 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002863 Py_ssize_t ix;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002865 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002866 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002867 hash = PyObject_Hash(key);
2868 if (hash == -1)
2869 return NULL;
2870 }
INADA Naoki778928b2017-08-03 23:45:15 +09002871 ix = (self->ma_keys->dk_lookup) (self, key, hash, &val);
Victor Stinner742da042016-09-07 17:40:12 -07002872 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002873 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002874 if (ix == DKIX_EMPTY || val == NULL) {
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002875 val = default_value;
INADA Naokiba609772016-12-07 20:41:42 +09002876 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002877 Py_INCREF(val);
2878 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002879}
2880
Benjamin Peterson00e98862013-03-07 22:16:29 -05002881PyObject *
2882PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002883{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002884 PyDictObject *mp = (PyDictObject *)d;
INADA Naoki93f26f72016-11-02 18:45:16 +09002885 PyObject *value;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002886 Py_hash_t hash;
Guido van Rossum164452c2000-08-08 16:12:54 +00002887
Benjamin Peterson00e98862013-03-07 22:16:29 -05002888 if (!PyDict_Check(d)) {
2889 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002890 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002891 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002893 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002894 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002895 hash = PyObject_Hash(key);
2896 if (hash == -1)
2897 return NULL;
2898 }
Inada Naoki2ddc7f62019-03-18 20:38:33 +09002899 if (mp->ma_keys == Py_EMPTY_KEYS) {
2900 if (insert_to_emptydict(mp, key, hash, defaultobj) < 0) {
2901 return NULL;
2902 }
2903 return defaultobj;
2904 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002905
2906 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
2907 if (insertion_resize(mp) < 0)
2908 return NULL;
2909 }
2910
INADA Naoki778928b2017-08-03 23:45:15 +09002911 Py_ssize_t ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002912 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 return NULL;
INADA Naoki93f26f72016-11-02 18:45:16 +09002914
2915 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09002916 ((ix >= 0 && value == NULL && mp->ma_used != ix) ||
INADA Naoki93f26f72016-11-02 18:45:16 +09002917 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
2918 if (insertion_resize(mp) < 0) {
2919 return NULL;
2920 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002921 ix = DKIX_EMPTY;
2922 }
2923
2924 if (ix == DKIX_EMPTY) {
2925 PyDictKeyEntry *ep, *ep0;
2926 value = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002927 if (mp->ma_keys->dk_usable <= 0) {
Victor Stinner3c336c52016-09-12 14:17:40 +02002928 if (insertion_resize(mp) < 0) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002929 return NULL;
Victor Stinner3c336c52016-09-12 14:17:40 +02002930 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002931 }
INADA Naoki778928b2017-08-03 23:45:15 +09002932 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naoki93f26f72016-11-02 18:45:16 +09002933 ep0 = DK_ENTRIES(mp->ma_keys);
2934 ep = &ep0[mp->ma_keys->dk_nentries];
INADA Naokia7576492018-11-14 18:39:27 +09002935 dictkeys_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002936 Py_INCREF(key);
INADA Naoki93f26f72016-11-02 18:45:16 +09002937 Py_INCREF(value);
2938 MAINTAIN_TRACKING(mp, key, value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002939 ep->me_key = key;
2940 ep->me_hash = hash;
INADA Naokiba609772016-12-07 20:41:42 +09002941 if (_PyDict_HasSplitTable(mp)) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002942 assert(mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
2943 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Victor Stinner742da042016-09-07 17:40:12 -07002944 }
2945 else {
INADA Naoki93f26f72016-11-02 18:45:16 +09002946 ep->me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07002947 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002948 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002949 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naoki93f26f72016-11-02 18:45:16 +09002950 mp->ma_keys->dk_usable--;
2951 mp->ma_keys->dk_nentries++;
2952 assert(mp->ma_keys->dk_usable >= 0);
2953 }
INADA Naokiba609772016-12-07 20:41:42 +09002954 else if (value == NULL) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002955 value = defaultobj;
2956 assert(_PyDict_HasSplitTable(mp));
2957 assert(ix == mp->ma_used);
2958 Py_INCREF(value);
2959 MAINTAIN_TRACKING(mp, key, value);
INADA Naokiba609772016-12-07 20:41:42 +09002960 mp->ma_values[ix] = value;
INADA Naoki93f26f72016-11-02 18:45:16 +09002961 mp->ma_used++;
2962 mp->ma_version_tag = DICT_NEXT_VERSION();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002963 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002964
Victor Stinner0fc91ee2019-04-12 21:51:34 +02002965 ASSERT_CONSISTENT(mp);
INADA Naoki93f26f72016-11-02 18:45:16 +09002966 return value;
Guido van Rossum164452c2000-08-08 16:12:54 +00002967}
2968
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002969/*[clinic input]
2970dict.setdefault
2971
2972 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002973 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002974 /
2975
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002976Insert key with a value of default if key is not in the dictionary.
2977
2978Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002979[clinic start generated code]*/
2980
Benjamin Peterson00e98862013-03-07 22:16:29 -05002981static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002982dict_setdefault_impl(PyDictObject *self, PyObject *key,
2983 PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002984/*[clinic end generated code: output=f8c1101ebf69e220 input=0f063756e815fd9d]*/
Benjamin Peterson00e98862013-03-07 22:16:29 -05002985{
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002986 PyObject *val;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002987
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002988 val = PyDict_SetDefault((PyObject *)self, key, default_value);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002989 Py_XINCREF(val);
2990 return val;
2991}
Guido van Rossum164452c2000-08-08 16:12:54 +00002992
2993static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302994dict_clear(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002995{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996 PyDict_Clear((PyObject *)mp);
2997 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002998}
2999
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003000/*[clinic input]
3001dict.pop
3002
3003 key: object
3004 default: object = NULL
3005 /
3006
Serhiy Storchaka279f4462019-09-14 12:24:05 +03003007D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003008
3009If key is not found, default is returned if given, otherwise KeyError is raised
3010[clinic start generated code]*/
3011
Guido van Rossumba6ab842000-12-12 22:02:18 +00003012static PyObject *
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003013dict_pop_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
Serhiy Storchaka279f4462019-09-14 12:24:05 +03003014/*[clinic end generated code: output=3abb47b89f24c21c input=eeebec7812190348]*/
Guido van Rossume027d982002-04-12 15:11:59 +00003015{
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003016 return _PyDict_Pop((PyObject*)self, key, default_value);
Guido van Rossume027d982002-04-12 15:11:59 +00003017}
3018
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003019/*[clinic input]
3020dict.popitem
3021
3022Remove and return a (key, value) pair as a 2-tuple.
3023
3024Pairs are returned in LIFO (last-in, first-out) order.
3025Raises KeyError if the dict is empty.
3026[clinic start generated code]*/
3027
Guido van Rossume027d982002-04-12 15:11:59 +00003028static PyObject *
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003029dict_popitem_impl(PyDictObject *self)
3030/*[clinic end generated code: output=e65fcb04420d230d input=1c38a49f21f64941]*/
Guido van Rossumba6ab842000-12-12 22:02:18 +00003031{
Victor Stinner742da042016-09-07 17:40:12 -07003032 Py_ssize_t i, j;
3033 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003034 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00003035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003036 /* Allocate the result tuple before checking the size. Believe it
3037 * or not, this allocation could trigger a garbage collection which
3038 * could empty the dict, so if we checked the size first and that
3039 * happened, the result would be an infinite loop (searching for an
3040 * entry that no longer exists). Note that the usual popitem()
3041 * idiom is "while d: k, v = d.popitem()". so needing to throw the
3042 * tuple away if the dict *is* empty isn't a significant
3043 * inefficiency -- possible, but unlikely in practice.
3044 */
3045 res = PyTuple_New(2);
3046 if (res == NULL)
3047 return NULL;
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003048 if (self->ma_used == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003049 Py_DECREF(res);
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003050 PyErr_SetString(PyExc_KeyError, "popitem(): dictionary is empty");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003051 return NULL;
3052 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003053 /* Convert split table to combined table */
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003054 if (self->ma_keys->dk_lookup == lookdict_split) {
3055 if (dictresize(self, DK_SIZE(self->ma_keys))) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003056 Py_DECREF(res);
3057 return NULL;
3058 }
3059 }
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003060 ENSURE_ALLOWS_DELETIONS(self);
Victor Stinner742da042016-09-07 17:40:12 -07003061
3062 /* Pop last item */
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003063 ep0 = DK_ENTRIES(self->ma_keys);
3064 i = self->ma_keys->dk_nentries - 1;
Victor Stinner742da042016-09-07 17:40:12 -07003065 while (i >= 0 && ep0[i].me_value == NULL) {
3066 i--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003067 }
Victor Stinner742da042016-09-07 17:40:12 -07003068 assert(i >= 0);
3069
3070 ep = &ep0[i];
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003071 j = lookdict_index(self->ma_keys, ep->me_hash, i);
Victor Stinner742da042016-09-07 17:40:12 -07003072 assert(j >= 0);
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003073 assert(dictkeys_get_index(self->ma_keys, j) == i);
3074 dictkeys_set_index(self->ma_keys, j, DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -07003075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003076 PyTuple_SET_ITEM(res, 0, ep->me_key);
3077 PyTuple_SET_ITEM(res, 1, ep->me_value);
Victor Stinner742da042016-09-07 17:40:12 -07003078 ep->me_key = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003079 ep->me_value = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07003080 /* We can't dk_usable++ since there is DKIX_DUMMY in indices */
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003081 self->ma_keys->dk_nentries = i;
3082 self->ma_used--;
3083 self->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner0fc91ee2019-04-12 21:51:34 +02003084 ASSERT_CONSISTENT(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003085 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00003086}
3087
Jeremy Hylton8caad492000-06-23 14:18:11 +00003088static int
3089dict_traverse(PyObject *op, visitproc visit, void *arg)
3090{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003091 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson55f44522016-09-05 12:12:59 -07003092 PyDictKeysObject *keys = mp->ma_keys;
Serhiy Storchaka46825d22016-09-26 21:29:34 +03003093 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Victor Stinner742da042016-09-07 17:40:12 -07003094 Py_ssize_t i, n = keys->dk_nentries;
3095
Benjamin Peterson55f44522016-09-05 12:12:59 -07003096 if (keys->dk_lookup == lookdict) {
3097 for (i = 0; i < n; i++) {
3098 if (entries[i].me_value != NULL) {
3099 Py_VISIT(entries[i].me_value);
3100 Py_VISIT(entries[i].me_key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003101 }
3102 }
Victor Stinner742da042016-09-07 17:40:12 -07003103 }
3104 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003105 if (mp->ma_values != NULL) {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003106 for (i = 0; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003107 Py_VISIT(mp->ma_values[i]);
3108 }
3109 }
3110 else {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003111 for (i = 0; i < n; i++) {
3112 Py_VISIT(entries[i].me_value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003113 }
3114 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003115 }
3116 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003117}
3118
3119static int
3120dict_tp_clear(PyObject *op)
3121{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003122 PyDict_Clear(op);
3123 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003124}
3125
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003126static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003127
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003128Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003129_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003130{
Victor Stinner742da042016-09-07 17:40:12 -07003131 Py_ssize_t size, usable, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003132
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003133 size = DK_SIZE(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07003134 usable = USABLE_FRACTION(size);
3135
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02003136 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003137 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -07003138 res += usable * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003139 /* If the dictionary is split, the keys portion is accounted-for
3140 in the type object. */
3141 if (mp->ma_keys->dk_refcnt == 1)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003142 res += (sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003143 + DK_IXSIZE(mp->ma_keys) * size
3144 + sizeof(PyDictKeyEntry) * usable);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003145 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003146}
3147
3148Py_ssize_t
3149_PyDict_KeysSize(PyDictKeysObject *keys)
3150{
Victor Stinner98ee9d52016-09-08 09:33:56 -07003151 return (sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003152 + DK_IXSIZE(keys) * DK_SIZE(keys)
3153 + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003154}
3155
doko@ubuntu.com17210f52016-01-14 14:04:59 +01003156static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303157dict_sizeof(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003158{
3159 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
3160}
3161
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00003162PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
3163
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003164PyDoc_STRVAR(sizeof__doc__,
3165"D.__sizeof__() -> size of D in memory, in bytes");
3166
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003167PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04003168"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
3169If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
3170If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
3171In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00003172
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003173PyDoc_STRVAR(clear__doc__,
3174"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003175
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003176PyDoc_STRVAR(copy__doc__,
3177"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00003178
Guido van Rossumb90c8482007-02-10 01:11:45 +00003179/* Forward */
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303180static PyObject *dictkeys_new(PyObject *, PyObject *);
3181static PyObject *dictitems_new(PyObject *, PyObject *);
3182static PyObject *dictvalues_new(PyObject *, PyObject *);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003183
Guido van Rossum45c85d12007-07-27 16:31:40 +00003184PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003185 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003186PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003187 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003188PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003189 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00003190
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003191static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07003192 DICT___CONTAINS___METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003193 {"__getitem__", (PyCFunction)(void(*)(void))dict_subscript, METH_O | METH_COEXIST,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003194 getitem__doc__},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003195 {"__sizeof__", (PyCFunction)(void(*)(void))dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003196 sizeof__doc__},
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003197 DICT_GET_METHODDEF
3198 DICT_SETDEFAULT_METHODDEF
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003199 DICT_POP_METHODDEF
3200 DICT_POPITEM_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303201 {"keys", dictkeys_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003202 keys__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303203 {"items", dictitems_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003204 items__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303205 {"values", dictvalues_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003206 values__doc__},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003207 {"update", (PyCFunction)(void(*)(void))dict_update, METH_VARARGS | METH_KEYWORDS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003208 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08003209 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003210 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
3211 clear__doc__},
3212 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
3213 copy__doc__},
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003214 DICT___REVERSED___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003215 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003216};
3217
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003218/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00003219int
3220PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003221{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003222 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07003223 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003224 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003225 PyObject *value;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003227 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003228 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003229 hash = PyObject_Hash(key);
3230 if (hash == -1)
3231 return -1;
3232 }
INADA Naoki778928b2017-08-03 23:45:15 +09003233 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003234 if (ix == DKIX_ERROR)
3235 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003236 return (ix != DKIX_EMPTY && value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003237}
3238
Thomas Wouterscf297e42007-02-23 15:07:44 +00003239/* Internal version of PyDict_Contains used when the hash value is already known */
3240int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003241_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00003242{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003243 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003244 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07003245 Py_ssize_t ix;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003246
INADA Naoki778928b2017-08-03 23:45:15 +09003247 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003248 if (ix == DKIX_ERROR)
3249 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003250 return (ix != DKIX_EMPTY && value != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00003251}
3252
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003253/* Hack to implement "key in dict" */
3254static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003255 0, /* sq_length */
3256 0, /* sq_concat */
3257 0, /* sq_repeat */
3258 0, /* sq_item */
3259 0, /* sq_slice */
3260 0, /* sq_ass_item */
3261 0, /* sq_ass_slice */
3262 PyDict_Contains, /* sq_contains */
3263 0, /* sq_inplace_concat */
3264 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003265};
3266
Guido van Rossum09e563a2001-05-01 12:10:21 +00003267static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003268dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003270 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003271 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003273 assert(type != NULL && type->tp_alloc != NULL);
3274 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02003275 if (self == NULL)
3276 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003277 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003278
Victor Stinnera9f61a52013-07-16 22:17:26 +02003279 /* The object has been implicitly tracked by tp_alloc */
3280 if (type == &PyDict_Type)
3281 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003282
3283 d->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003284 d->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07003285 d->ma_keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003286 if (d->ma_keys == NULL) {
3287 Py_DECREF(self);
3288 return NULL;
3289 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +02003290 ASSERT_CONSISTENT(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003291 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003292}
3293
Tim Peters25786c02001-09-02 08:22:48 +00003294static int
3295dict_init(PyObject *self, PyObject *args, PyObject *kwds)
3296{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003297 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00003298}
3299
Tim Peters6d6c1a32001-08-02 04:15:00 +00003300static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003301dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00003302{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003303 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003304}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003305
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003306PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00003307"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00003308"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003309" (key, value) pairs\n"
3310"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00003311" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003312" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00003313" d[k] = v\n"
3314"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
3315" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00003316
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003317PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003318 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3319 "dict",
3320 sizeof(PyDictObject),
3321 0,
3322 (destructor)dict_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003323 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003324 0, /* tp_getattr */
3325 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003326 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003327 (reprfunc)dict_repr, /* tp_repr */
3328 0, /* tp_as_number */
3329 &dict_as_sequence, /* tp_as_sequence */
3330 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003331 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003332 0, /* tp_call */
3333 0, /* tp_str */
3334 PyObject_GenericGetAttr, /* tp_getattro */
3335 0, /* tp_setattro */
3336 0, /* tp_as_buffer */
3337 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3338 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
3339 dictionary_doc, /* tp_doc */
3340 dict_traverse, /* tp_traverse */
3341 dict_tp_clear, /* tp_clear */
3342 dict_richcompare, /* tp_richcompare */
3343 0, /* tp_weaklistoffset */
3344 (getiterfunc)dict_iter, /* tp_iter */
3345 0, /* tp_iternext */
3346 mapp_methods, /* tp_methods */
3347 0, /* tp_members */
3348 0, /* tp_getset */
3349 0, /* tp_base */
3350 0, /* tp_dict */
3351 0, /* tp_descr_get */
3352 0, /* tp_descr_set */
3353 0, /* tp_dictoffset */
3354 dict_init, /* tp_init */
3355 PyType_GenericAlloc, /* tp_alloc */
3356 dict_new, /* tp_new */
3357 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003358};
3359
Victor Stinner3c1e4812012-03-26 22:10:51 +02003360PyObject *
3361_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
3362{
3363 PyObject *kv;
3364 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02003365 if (kv == NULL) {
3366 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003367 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02003368 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02003369 return PyDict_GetItem(dp, kv);
3370}
3371
Guido van Rossum3cca2451997-05-16 14:23:33 +00003372/* For backward compatibility with old dictionary interface */
3373
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003374PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003375PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003376{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003377 PyObject *kv, *rv;
3378 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003379 if (kv == NULL) {
3380 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003381 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003382 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003383 rv = PyDict_GetItem(v, kv);
3384 Py_DECREF(kv);
3385 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003386}
3387
3388int
Victor Stinner3c1e4812012-03-26 22:10:51 +02003389_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
3390{
3391 PyObject *kv;
3392 kv = _PyUnicode_FromId(key); /* borrowed */
3393 if (kv == NULL)
3394 return -1;
3395 return PyDict_SetItem(v, kv, item);
3396}
3397
3398int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003399PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003400{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003401 PyObject *kv;
3402 int err;
3403 kv = PyUnicode_FromString(key);
3404 if (kv == NULL)
3405 return -1;
3406 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
3407 err = PyDict_SetItem(v, kv, item);
3408 Py_DECREF(kv);
3409 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003410}
3411
3412int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01003413_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
3414{
3415 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3416 if (kv == NULL)
3417 return -1;
3418 return PyDict_DelItem(v, kv);
3419}
3420
3421int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003422PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003423{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003424 PyObject *kv;
3425 int err;
3426 kv = PyUnicode_FromString(key);
3427 if (kv == NULL)
3428 return -1;
3429 err = PyDict_DelItem(v, kv);
3430 Py_DECREF(kv);
3431 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003432}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003433
Raymond Hettinger019a1482004-03-18 02:41:19 +00003434/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003435
3436typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003437 PyObject_HEAD
3438 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
3439 Py_ssize_t di_used;
3440 Py_ssize_t di_pos;
3441 PyObject* di_result; /* reusable result tuple for iteritems */
3442 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003443} dictiterobject;
3444
3445static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003446dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003447{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003448 dictiterobject *di;
3449 di = PyObject_GC_New(dictiterobject, itertype);
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003450 if (di == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003451 return NULL;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003452 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003453 Py_INCREF(dict);
3454 di->di_dict = dict;
3455 di->di_used = dict->ma_used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003456 di->len = dict->ma_used;
Dong-hee Na24dc2f82019-10-20 05:01:08 +09003457 if (itertype == &PyDictRevIterKey_Type ||
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003458 itertype == &PyDictRevIterItem_Type ||
Dong-hee Na24dc2f82019-10-20 05:01:08 +09003459 itertype == &PyDictRevIterValue_Type) {
3460 if (dict->ma_values) {
3461 di->di_pos = dict->ma_used - 1;
3462 }
3463 else {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003464 di->di_pos = dict->ma_keys->dk_nentries - 1;
Dong-hee Na24dc2f82019-10-20 05:01:08 +09003465 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003466 }
3467 else {
3468 di->di_pos = 0;
3469 }
3470 if (itertype == &PyDictIterItem_Type ||
3471 itertype == &PyDictRevIterItem_Type) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003472 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
3473 if (di->di_result == NULL) {
3474 Py_DECREF(di);
3475 return NULL;
3476 }
3477 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003478 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003479 di->di_result = NULL;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003480 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003481 _PyObject_GC_TRACK(di);
3482 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003483}
3484
3485static void
3486dictiter_dealloc(dictiterobject *di)
3487{
INADA Naokia6296d32017-08-24 14:55:17 +09003488 /* bpo-31095: UnTrack is needed before calling any callbacks */
3489 _PyObject_GC_UNTRACK(di);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003490 Py_XDECREF(di->di_dict);
3491 Py_XDECREF(di->di_result);
3492 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003493}
3494
3495static int
3496dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
3497{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003498 Py_VISIT(di->di_dict);
3499 Py_VISIT(di->di_result);
3500 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003501}
3502
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003503static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303504dictiter_len(dictiterobject *di, PyObject *Py_UNUSED(ignored))
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003505{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003506 Py_ssize_t len = 0;
3507 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
3508 len = di->len;
3509 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003510}
3511
Guido van Rossumb90c8482007-02-10 01:11:45 +00003512PyDoc_STRVAR(length_hint_doc,
3513 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003514
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003515static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303516dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003517
3518PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3519
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003520static PyMethodDef dictiter_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003521 {"__length_hint__", (PyCFunction)(void(*)(void))dictiter_len, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003522 length_hint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003523 {"__reduce__", (PyCFunction)(void(*)(void))dictiter_reduce, METH_NOARGS,
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003524 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003525 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003526};
3527
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003528static PyObject*
3529dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00003530{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003531 PyObject *key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003532 Py_ssize_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003533 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003534 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00003535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003536 if (d == NULL)
3537 return NULL;
3538 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00003539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003540 if (di->di_used != d->ma_used) {
3541 PyErr_SetString(PyExc_RuntimeError,
3542 "dictionary changed size during iteration");
3543 di->di_used = -1; /* Make this state sticky */
3544 return NULL;
3545 }
Guido van Rossum2147df72002-07-16 20:30:22 +00003546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003547 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003548 k = d->ma_keys;
INADA Naokica2d8be2016-11-04 16:59:10 +09003549 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003550 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003551 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003552 goto fail;
3553 key = DK_ENTRIES(k)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003554 assert(d->ma_values[i] != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003555 }
3556 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003557 Py_ssize_t n = k->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003558 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3559 while (i < n && entry_ptr->me_value == NULL) {
3560 entry_ptr++;
3561 i++;
3562 }
3563 if (i >= n)
3564 goto fail;
3565 key = entry_ptr->me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003566 }
Thomas Perl796cc6e2019-03-28 07:03:25 +01003567 // We found an element (key), but did not expect it
3568 if (di->len == 0) {
3569 PyErr_SetString(PyExc_RuntimeError,
3570 "dictionary keys changed during iteration");
3571 goto fail;
3572 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003573 di->di_pos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003574 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003575 Py_INCREF(key);
3576 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003577
3578fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003579 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003580 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003581 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003582}
3583
Raymond Hettinger019a1482004-03-18 02:41:19 +00003584PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003585 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3586 "dict_keyiterator", /* tp_name */
3587 sizeof(dictiterobject), /* tp_basicsize */
3588 0, /* tp_itemsize */
3589 /* methods */
3590 (destructor)dictiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003591 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003592 0, /* tp_getattr */
3593 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003594 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003595 0, /* tp_repr */
3596 0, /* tp_as_number */
3597 0, /* tp_as_sequence */
3598 0, /* tp_as_mapping */
3599 0, /* tp_hash */
3600 0, /* tp_call */
3601 0, /* tp_str */
3602 PyObject_GenericGetAttr, /* tp_getattro */
3603 0, /* tp_setattro */
3604 0, /* tp_as_buffer */
3605 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3606 0, /* tp_doc */
3607 (traverseproc)dictiter_traverse, /* tp_traverse */
3608 0, /* tp_clear */
3609 0, /* tp_richcompare */
3610 0, /* tp_weaklistoffset */
3611 PyObject_SelfIter, /* tp_iter */
3612 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3613 dictiter_methods, /* tp_methods */
3614 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003615};
3616
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003617static PyObject *
3618dictiter_iternextvalue(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003619{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003620 PyObject *value;
INADA Naokica2d8be2016-11-04 16:59:10 +09003621 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003622 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003624 if (d == NULL)
3625 return NULL;
3626 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003628 if (di->di_used != d->ma_used) {
3629 PyErr_SetString(PyExc_RuntimeError,
3630 "dictionary changed size during iteration");
3631 di->di_used = -1; /* Make this state sticky */
3632 return NULL;
3633 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003635 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003636 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003637 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003638 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003639 goto fail;
INADA Naokica2d8be2016-11-04 16:59:10 +09003640 value = d->ma_values[i];
3641 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003642 }
3643 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003644 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003645 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3646 while (i < n && entry_ptr->me_value == NULL) {
3647 entry_ptr++;
3648 i++;
3649 }
3650 if (i >= n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003651 goto fail;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003652 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003653 }
Thomas Perlb8311cf2019-04-02 11:30:10 +02003654 // We found an element, but did not expect it
3655 if (di->len == 0) {
3656 PyErr_SetString(PyExc_RuntimeError,
3657 "dictionary keys changed during iteration");
3658 goto fail;
3659 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003660 di->di_pos = i+1;
3661 di->len--;
3662 Py_INCREF(value);
3663 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003664
3665fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003666 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003667 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003668 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003669}
3670
3671PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003672 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3673 "dict_valueiterator", /* tp_name */
3674 sizeof(dictiterobject), /* tp_basicsize */
3675 0, /* tp_itemsize */
3676 /* methods */
3677 (destructor)dictiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003678 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003679 0, /* tp_getattr */
3680 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003681 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003682 0, /* tp_repr */
3683 0, /* tp_as_number */
3684 0, /* tp_as_sequence */
3685 0, /* tp_as_mapping */
3686 0, /* tp_hash */
3687 0, /* tp_call */
3688 0, /* tp_str */
3689 PyObject_GenericGetAttr, /* tp_getattro */
3690 0, /* tp_setattro */
3691 0, /* tp_as_buffer */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003692 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003693 0, /* tp_doc */
3694 (traverseproc)dictiter_traverse, /* tp_traverse */
3695 0, /* tp_clear */
3696 0, /* tp_richcompare */
3697 0, /* tp_weaklistoffset */
3698 PyObject_SelfIter, /* tp_iter */
3699 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3700 dictiter_methods, /* tp_methods */
3701 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003702};
3703
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003704static PyObject *
3705dictiter_iternextitem(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003706{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003707 PyObject *key, *value, *result;
INADA Naokica2d8be2016-11-04 16:59:10 +09003708 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003709 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003711 if (d == NULL)
3712 return NULL;
3713 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003715 if (di->di_used != d->ma_used) {
3716 PyErr_SetString(PyExc_RuntimeError,
3717 "dictionary changed size during iteration");
3718 di->di_used = -1; /* Make this state sticky */
3719 return NULL;
3720 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003722 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003723 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003724 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003725 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003726 goto fail;
3727 key = DK_ENTRIES(d->ma_keys)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003728 value = d->ma_values[i];
3729 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003730 }
3731 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003732 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003733 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3734 while (i < n && entry_ptr->me_value == NULL) {
3735 entry_ptr++;
3736 i++;
3737 }
3738 if (i >= n)
3739 goto fail;
3740 key = entry_ptr->me_key;
3741 value = entry_ptr->me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003742 }
Thomas Perlb8311cf2019-04-02 11:30:10 +02003743 // We found an element, but did not expect it
3744 if (di->len == 0) {
3745 PyErr_SetString(PyExc_RuntimeError,
3746 "dictionary keys changed during iteration");
3747 goto fail;
3748 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003749 di->di_pos = i+1;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003750 di->len--;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003751 Py_INCREF(key);
3752 Py_INCREF(value);
3753 result = di->di_result;
3754 if (Py_REFCNT(result) == 1) {
3755 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3756 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3757 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3758 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003759 Py_INCREF(result);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003760 Py_DECREF(oldkey);
3761 Py_DECREF(oldvalue);
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003762 }
3763 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003764 result = PyTuple_New(2);
3765 if (result == NULL)
3766 return NULL;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003767 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3768 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003769 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003770 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003771
3772fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003773 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003774 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003775 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003776}
3777
3778PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003779 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3780 "dict_itemiterator", /* tp_name */
3781 sizeof(dictiterobject), /* tp_basicsize */
3782 0, /* tp_itemsize */
3783 /* methods */
3784 (destructor)dictiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003785 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003786 0, /* tp_getattr */
3787 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003788 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003789 0, /* tp_repr */
3790 0, /* tp_as_number */
3791 0, /* tp_as_sequence */
3792 0, /* tp_as_mapping */
3793 0, /* tp_hash */
3794 0, /* tp_call */
3795 0, /* tp_str */
3796 PyObject_GenericGetAttr, /* tp_getattro */
3797 0, /* tp_setattro */
3798 0, /* tp_as_buffer */
3799 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3800 0, /* tp_doc */
3801 (traverseproc)dictiter_traverse, /* tp_traverse */
3802 0, /* tp_clear */
3803 0, /* tp_richcompare */
3804 0, /* tp_weaklistoffset */
3805 PyObject_SelfIter, /* tp_iter */
3806 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3807 dictiter_methods, /* tp_methods */
3808 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003809};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003810
3811
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003812/* dictreviter */
3813
3814static PyObject *
3815dictreviter_iternext(dictiterobject *di)
3816{
3817 PyDictObject *d = di->di_dict;
3818
3819 if (d == NULL) {
3820 return NULL;
3821 }
3822 assert (PyDict_Check(d));
3823
3824 if (di->di_used != d->ma_used) {
3825 PyErr_SetString(PyExc_RuntimeError,
3826 "dictionary changed size during iteration");
3827 di->di_used = -1; /* Make this state sticky */
3828 return NULL;
3829 }
3830
3831 Py_ssize_t i = di->di_pos;
3832 PyDictKeysObject *k = d->ma_keys;
3833 PyObject *key, *value, *result;
3834
Serhiy Storchaka2e3d8732019-10-23 14:48:08 +03003835 if (i < 0) {
3836 goto fail;
3837 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003838 if (d->ma_values) {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003839 key = DK_ENTRIES(k)[i].me_key;
3840 value = d->ma_values[i];
3841 assert (value != NULL);
3842 }
3843 else {
3844 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
Serhiy Storchaka2e3d8732019-10-23 14:48:08 +03003845 while (entry_ptr->me_value == NULL) {
3846 if (--i < 0) {
3847 goto fail;
3848 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003849 entry_ptr--;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003850 }
3851 key = entry_ptr->me_key;
3852 value = entry_ptr->me_value;
3853 }
3854 di->di_pos = i-1;
3855 di->len--;
3856
3857 if (Py_TYPE(di) == &PyDictRevIterKey_Type) {
3858 Py_INCREF(key);
3859 return key;
3860 }
3861 else if (Py_TYPE(di) == &PyDictRevIterValue_Type) {
3862 Py_INCREF(value);
3863 return value;
3864 }
3865 else if (Py_TYPE(di) == &PyDictRevIterItem_Type) {
3866 Py_INCREF(key);
3867 Py_INCREF(value);
3868 result = di->di_result;
3869 if (Py_REFCNT(result) == 1) {
3870 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3871 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3872 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3873 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
3874 Py_INCREF(result);
3875 Py_DECREF(oldkey);
3876 Py_DECREF(oldvalue);
3877 }
3878 else {
3879 result = PyTuple_New(2);
3880 if (result == NULL) {
3881 return NULL;
3882 }
3883 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3884 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
3885 }
3886 return result;
3887 }
3888 else {
3889 Py_UNREACHABLE();
3890 }
3891
3892fail:
3893 di->di_dict = NULL;
3894 Py_DECREF(d);
3895 return NULL;
3896}
3897
3898PyTypeObject PyDictRevIterKey_Type = {
3899 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3900 "dict_reversekeyiterator",
3901 sizeof(dictiterobject),
3902 .tp_dealloc = (destructor)dictiter_dealloc,
3903 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
3904 .tp_traverse = (traverseproc)dictiter_traverse,
3905 .tp_iter = PyObject_SelfIter,
3906 .tp_iternext = (iternextfunc)dictreviter_iternext,
3907 .tp_methods = dictiter_methods
3908};
3909
3910
3911/*[clinic input]
3912dict.__reversed__
3913
3914Return a reverse iterator over the dict keys.
3915[clinic start generated code]*/
3916
3917static PyObject *
3918dict___reversed___impl(PyDictObject *self)
3919/*[clinic end generated code: output=e674483336d1ed51 input=23210ef3477d8c4d]*/
3920{
3921 assert (PyDict_Check(self));
3922 return dictiter_new(self, &PyDictRevIterKey_Type);
3923}
3924
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003925static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303926dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003927{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003928 _Py_IDENTIFIER(iter);
Sergey Fedoseev63958442018-10-20 05:43:33 +05003929 /* copy the iterator state */
3930 dictiterobject tmp = *di;
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003931 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003932
Sergey Fedoseev63958442018-10-20 05:43:33 +05003933 PyObject *list = PySequence_List((PyObject*)&tmp);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003934 Py_XDECREF(tmp.di_dict);
Sergey Fedoseev63958442018-10-20 05:43:33 +05003935 if (list == NULL) {
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003936 return NULL;
3937 }
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02003938 return Py_BuildValue("N(N)", _PyEval_GetBuiltinId(&PyId_iter), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003939}
3940
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003941PyTypeObject PyDictRevIterItem_Type = {
3942 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3943 "dict_reverseitemiterator",
3944 sizeof(dictiterobject),
3945 .tp_dealloc = (destructor)dictiter_dealloc,
3946 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
3947 .tp_traverse = (traverseproc)dictiter_traverse,
3948 .tp_iter = PyObject_SelfIter,
3949 .tp_iternext = (iternextfunc)dictreviter_iternext,
3950 .tp_methods = dictiter_methods
3951};
3952
3953PyTypeObject PyDictRevIterValue_Type = {
3954 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3955 "dict_reversevalueiterator",
3956 sizeof(dictiterobject),
3957 .tp_dealloc = (destructor)dictiter_dealloc,
3958 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
3959 .tp_traverse = (traverseproc)dictiter_traverse,
3960 .tp_iter = PyObject_SelfIter,
3961 .tp_iternext = (iternextfunc)dictreviter_iternext,
3962 .tp_methods = dictiter_methods
3963};
3964
Guido van Rossum3ac67412007-02-10 18:55:06 +00003965/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003966/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003967/***********************************************/
3968
Guido van Rossumb90c8482007-02-10 01:11:45 +00003969/* The instance lay-out is the same for all three; but the type differs. */
3970
Guido van Rossumb90c8482007-02-10 01:11:45 +00003971static void
Eric Snow96c6af92015-05-29 22:21:39 -06003972dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003973{
INADA Naokia6296d32017-08-24 14:55:17 +09003974 /* bpo-31095: UnTrack is needed before calling any callbacks */
3975 _PyObject_GC_UNTRACK(dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003976 Py_XDECREF(dv->dv_dict);
3977 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003978}
3979
3980static int
Eric Snow96c6af92015-05-29 22:21:39 -06003981dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003982{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003983 Py_VISIT(dv->dv_dict);
3984 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003985}
3986
Guido van Rossum83825ac2007-02-10 04:54:19 +00003987static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003988dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003989{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003990 Py_ssize_t len = 0;
3991 if (dv->dv_dict != NULL)
3992 len = dv->dv_dict->ma_used;
3993 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003994}
3995
Eric Snow96c6af92015-05-29 22:21:39 -06003996PyObject *
3997_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003998{
Eric Snow96c6af92015-05-29 22:21:39 -06003999 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004000 if (dict == NULL) {
4001 PyErr_BadInternalCall();
4002 return NULL;
4003 }
4004 if (!PyDict_Check(dict)) {
4005 /* XXX Get rid of this restriction later */
4006 PyErr_Format(PyExc_TypeError,
4007 "%s() requires a dict argument, not '%s'",
4008 type->tp_name, dict->ob_type->tp_name);
4009 return NULL;
4010 }
Eric Snow96c6af92015-05-29 22:21:39 -06004011 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004012 if (dv == NULL)
4013 return NULL;
4014 Py_INCREF(dict);
4015 dv->dv_dict = (PyDictObject *)dict;
4016 _PyObject_GC_TRACK(dv);
4017 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004018}
4019
Neal Norwitze36f2ba2007-02-26 23:12:28 +00004020/* TODO(guido): The views objects are not complete:
4021
4022 * support more set operations
4023 * support arbitrary mappings?
4024 - either these should be static or exported in dictobject.h
4025 - if public then they should probably be in builtins
4026*/
4027
Guido van Rossumaac530c2007-08-24 22:33:45 +00004028/* Return 1 if self is a subset of other, iterating over self;
4029 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00004030static int
4031all_contained_in(PyObject *self, PyObject *other)
4032{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004033 PyObject *iter = PyObject_GetIter(self);
4034 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004036 if (iter == NULL)
4037 return -1;
4038 for (;;) {
4039 PyObject *next = PyIter_Next(iter);
4040 if (next == NULL) {
4041 if (PyErr_Occurred())
4042 ok = -1;
4043 break;
4044 }
4045 ok = PySequence_Contains(other, next);
4046 Py_DECREF(next);
4047 if (ok <= 0)
4048 break;
4049 }
4050 Py_DECREF(iter);
4051 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004052}
4053
4054static PyObject *
4055dictview_richcompare(PyObject *self, PyObject *other, int op)
4056{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004057 Py_ssize_t len_self, len_other;
4058 int ok;
4059 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004061 assert(self != NULL);
4062 assert(PyDictViewSet_Check(self));
4063 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00004064
Brian Curtindfc80e32011-08-10 20:28:54 -05004065 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
4066 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004068 len_self = PyObject_Size(self);
4069 if (len_self < 0)
4070 return NULL;
4071 len_other = PyObject_Size(other);
4072 if (len_other < 0)
4073 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004075 ok = 0;
4076 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00004077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004078 case Py_NE:
4079 case Py_EQ:
4080 if (len_self == len_other)
4081 ok = all_contained_in(self, other);
4082 if (op == Py_NE && ok >= 0)
4083 ok = !ok;
4084 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004086 case Py_LT:
4087 if (len_self < len_other)
4088 ok = all_contained_in(self, other);
4089 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004091 case Py_LE:
4092 if (len_self <= len_other)
4093 ok = all_contained_in(self, other);
4094 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004096 case Py_GT:
4097 if (len_self > len_other)
4098 ok = all_contained_in(other, self);
4099 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004100
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004101 case Py_GE:
4102 if (len_self >= len_other)
4103 ok = all_contained_in(other, self);
4104 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004106 }
4107 if (ok < 0)
4108 return NULL;
4109 result = ok ? Py_True : Py_False;
4110 Py_INCREF(result);
4111 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004112}
4113
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004114static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004115dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004116{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004117 PyObject *seq;
bennorthd7773d92018-01-26 15:46:01 +00004118 PyObject *result = NULL;
4119 Py_ssize_t rc;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004120
bennorthd7773d92018-01-26 15:46:01 +00004121 rc = Py_ReprEnter((PyObject *)dv);
4122 if (rc != 0) {
4123 return rc > 0 ? PyUnicode_FromString("...") : NULL;
4124 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004125 seq = PySequence_List((PyObject *)dv);
bennorthd7773d92018-01-26 15:46:01 +00004126 if (seq == NULL) {
4127 goto Done;
4128 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004129 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
4130 Py_DECREF(seq);
bennorthd7773d92018-01-26 15:46:01 +00004131
4132Done:
4133 Py_ReprLeave((PyObject *)dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004134 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004135}
4136
Guido van Rossum3ac67412007-02-10 18:55:06 +00004137/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004138
4139static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004140dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004141{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004142 if (dv->dv_dict == NULL) {
4143 Py_RETURN_NONE;
4144 }
4145 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004146}
4147
4148static int
Eric Snow96c6af92015-05-29 22:21:39 -06004149dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004150{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004151 if (dv->dv_dict == NULL)
4152 return 0;
4153 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004154}
4155
Guido van Rossum83825ac2007-02-10 04:54:19 +00004156static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004157 (lenfunc)dictview_len, /* sq_length */
4158 0, /* sq_concat */
4159 0, /* sq_repeat */
4160 0, /* sq_item */
4161 0, /* sq_slice */
4162 0, /* sq_ass_item */
4163 0, /* sq_ass_slice */
4164 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004165};
4166
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004167// Create an set object from dictviews object.
4168// Returns a new reference.
4169// This utility function is used by set operations.
Guido van Rossum523259b2007-08-24 23:41:22 +00004170static PyObject*
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004171dictviews_to_set(PyObject *self)
Guido van Rossum523259b2007-08-24 23:41:22 +00004172{
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004173 PyObject *left = self;
4174 if (PyDictKeys_Check(self)) {
4175 // PySet_New() has fast path for the dict object.
4176 PyObject *dict = (PyObject *)((_PyDictViewObject *)self)->dv_dict;
4177 if (PyDict_CheckExact(dict)) {
4178 left = dict;
4179 }
4180 }
4181 return PySet_New(left);
4182}
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004183
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004184static PyObject*
4185dictviews_sub(PyObject *self, PyObject *other)
4186{
4187 PyObject *result = dictviews_to_set(self);
4188 if (result == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004189 return NULL;
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004190 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004191
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004192 _Py_IDENTIFIER(difference_update);
4193 PyObject *tmp = _PyObject_CallMethodIdOneArg(
4194 result, &PyId_difference_update, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004195 if (tmp == NULL) {
4196 Py_DECREF(result);
4197 return NULL;
4198 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004200 Py_DECREF(tmp);
4201 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004202}
4203
Forest Gregg998cf1f2019-08-26 02:17:43 -05004204static int
4205dictitems_contains(_PyDictViewObject *dv, PyObject *obj);
4206
4207PyObject *
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004208_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00004209{
Forest Gregg998cf1f2019-08-26 02:17:43 -05004210 PyObject *result;
4211 PyObject *it;
4212 PyObject *key;
4213 Py_ssize_t len_self;
4214 int rv;
4215 int (*dict_contains)(_PyDictViewObject *, PyObject *);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004216
Forest Gregg998cf1f2019-08-26 02:17:43 -05004217 /* Python interpreter swaps parameters when dict view
4218 is on right side of & */
4219 if (!PyDictViewSet_Check(self)) {
4220 PyObject *tmp = other;
4221 other = self;
4222 self = tmp;
4223 }
4224
4225 len_self = dictview_len((_PyDictViewObject *)self);
4226
4227 /* if other is a set and self is smaller than other,
4228 reuse set intersection logic */
4229 if (Py_TYPE(other) == &PySet_Type && len_self <= PyObject_Size(other)) {
4230 _Py_IDENTIFIER(intersection);
4231 return _PyObject_CallMethodIdObjArgs(other, &PyId_intersection, self, NULL);
4232 }
4233
4234 /* if other is another dict view, and it is bigger than self,
4235 swap them */
4236 if (PyDictViewSet_Check(other)) {
4237 Py_ssize_t len_other = dictview_len((_PyDictViewObject *)other);
4238 if (len_other > len_self) {
4239 PyObject *tmp = other;
4240 other = self;
4241 self = tmp;
4242 }
4243 }
4244
4245 /* at this point, two things should be true
4246 1. self is a dictview
4247 2. if other is a dictview then it is smaller than self */
4248 result = PySet_New(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004249 if (result == NULL)
4250 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004251
Forest Gregg998cf1f2019-08-26 02:17:43 -05004252 it = PyObject_GetIter(other);
Zackery Spytzb16e3822019-10-13 05:49:05 -06004253 if (it == NULL) {
4254 Py_DECREF(result);
4255 return NULL;
4256 }
Forest Gregg998cf1f2019-08-26 02:17:43 -05004257
Forest Gregg998cf1f2019-08-26 02:17:43 -05004258 if (PyDictKeys_Check(self)) {
4259 dict_contains = dictkeys_contains;
4260 }
4261 /* else PyDictItems_Check(self) */
4262 else {
4263 dict_contains = dictitems_contains;
4264 }
4265
4266 while ((key = PyIter_Next(it)) != NULL) {
4267 rv = dict_contains((_PyDictViewObject *)self, key);
4268 if (rv < 0) {
4269 goto error;
4270 }
4271 if (rv) {
4272 if (PySet_Add(result, key)) {
4273 goto error;
4274 }
4275 }
4276 Py_DECREF(key);
4277 }
4278 Py_DECREF(it);
4279 if (PyErr_Occurred()) {
4280 Py_DECREF(result);
4281 return NULL;
4282 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004283 return result;
Forest Gregg998cf1f2019-08-26 02:17:43 -05004284
4285error:
4286 Py_DECREF(it);
4287 Py_DECREF(result);
4288 Py_DECREF(key);
4289 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004290}
4291
4292static PyObject*
4293dictviews_or(PyObject* self, PyObject *other)
4294{
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004295 PyObject *result = dictviews_to_set(self);
4296 if (result == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004297 return NULL;
4298 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004299
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004300 if (_PySet_Update(result, other) < 0) {
4301 Py_DECREF(result);
4302 return NULL;
4303 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004304 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004305}
4306
4307static PyObject*
4308dictviews_xor(PyObject* self, PyObject *other)
4309{
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004310 PyObject *result = dictviews_to_set(self);
4311 if (result == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004312 return NULL;
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004313 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004314
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004315 _Py_IDENTIFIER(symmetric_difference_update);
4316 PyObject *tmp = _PyObject_CallMethodIdOneArg(
4317 result, &PyId_symmetric_difference_update, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004318 if (tmp == NULL) {
4319 Py_DECREF(result);
4320 return NULL;
4321 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004323 Py_DECREF(tmp);
4324 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004325}
4326
4327static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004328 0, /*nb_add*/
4329 (binaryfunc)dictviews_sub, /*nb_subtract*/
4330 0, /*nb_multiply*/
4331 0, /*nb_remainder*/
4332 0, /*nb_divmod*/
4333 0, /*nb_power*/
4334 0, /*nb_negative*/
4335 0, /*nb_positive*/
4336 0, /*nb_absolute*/
4337 0, /*nb_bool*/
4338 0, /*nb_invert*/
4339 0, /*nb_lshift*/
4340 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004341 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004342 (binaryfunc)dictviews_xor, /*nb_xor*/
4343 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00004344};
4345
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004346static PyObject*
4347dictviews_isdisjoint(PyObject *self, PyObject *other)
4348{
4349 PyObject *it;
4350 PyObject *item = NULL;
4351
4352 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06004353 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004354 Py_RETURN_TRUE;
4355 else
4356 Py_RETURN_FALSE;
4357 }
4358
4359 /* Iterate over the shorter object (only if other is a set,
4360 * because PySequence_Contains may be expensive otherwise): */
4361 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06004362 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004363 Py_ssize_t len_other = PyObject_Size(other);
4364 if (len_other == -1)
4365 return NULL;
4366
4367 if ((len_other > len_self)) {
4368 PyObject *tmp = other;
4369 other = self;
4370 self = tmp;
4371 }
4372 }
4373
4374 it = PyObject_GetIter(other);
4375 if (it == NULL)
4376 return NULL;
4377
4378 while ((item = PyIter_Next(it)) != NULL) {
4379 int contains = PySequence_Contains(self, item);
4380 Py_DECREF(item);
4381 if (contains == -1) {
4382 Py_DECREF(it);
4383 return NULL;
4384 }
4385
4386 if (contains) {
4387 Py_DECREF(it);
4388 Py_RETURN_FALSE;
4389 }
4390 }
4391 Py_DECREF(it);
4392 if (PyErr_Occurred())
4393 return NULL; /* PyIter_Next raised an exception. */
4394 Py_RETURN_TRUE;
4395}
4396
4397PyDoc_STRVAR(isdisjoint_doc,
4398"Return True if the view and the given iterable have a null intersection.");
4399
Serhiy Storchaka81524022018-11-27 13:05:02 +02004400static PyObject* dictkeys_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored));
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004401
4402PyDoc_STRVAR(reversed_keys_doc,
4403"Return a reverse iterator over the dict keys.");
4404
Guido van Rossumb90c8482007-02-10 01:11:45 +00004405static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004406 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4407 isdisjoint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004408 {"__reversed__", (PyCFunction)(void(*)(void))dictkeys_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004409 reversed_keys_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004410 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004411};
4412
4413PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004414 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4415 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004416 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004417 0, /* tp_itemsize */
4418 /* methods */
4419 (destructor)dictview_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004420 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004421 0, /* tp_getattr */
4422 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004423 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004424 (reprfunc)dictview_repr, /* tp_repr */
4425 &dictviews_as_number, /* tp_as_number */
4426 &dictkeys_as_sequence, /* tp_as_sequence */
4427 0, /* tp_as_mapping */
4428 0, /* tp_hash */
4429 0, /* tp_call */
4430 0, /* tp_str */
4431 PyObject_GenericGetAttr, /* tp_getattro */
4432 0, /* tp_setattro */
4433 0, /* tp_as_buffer */
4434 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4435 0, /* tp_doc */
4436 (traverseproc)dictview_traverse, /* tp_traverse */
4437 0, /* tp_clear */
4438 dictview_richcompare, /* tp_richcompare */
4439 0, /* tp_weaklistoffset */
4440 (getiterfunc)dictkeys_iter, /* tp_iter */
4441 0, /* tp_iternext */
4442 dictkeys_methods, /* tp_methods */
4443 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004444};
4445
4446static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304447dictkeys_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004448{
Eric Snow96c6af92015-05-29 22:21:39 -06004449 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004450}
4451
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004452static PyObject *
Serhiy Storchaka81524022018-11-27 13:05:02 +02004453dictkeys_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored))
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004454{
4455 if (dv->dv_dict == NULL) {
4456 Py_RETURN_NONE;
4457 }
4458 return dictiter_new(dv->dv_dict, &PyDictRevIterKey_Type);
4459}
4460
Guido van Rossum3ac67412007-02-10 18:55:06 +00004461/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004462
4463static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004464dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004465{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004466 if (dv->dv_dict == NULL) {
4467 Py_RETURN_NONE;
4468 }
4469 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004470}
4471
4472static int
Eric Snow96c6af92015-05-29 22:21:39 -06004473dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004474{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004475 int result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004476 PyObject *key, *value, *found;
4477 if (dv->dv_dict == NULL)
4478 return 0;
4479 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
4480 return 0;
4481 key = PyTuple_GET_ITEM(obj, 0);
4482 value = PyTuple_GET_ITEM(obj, 1);
Raymond Hettinger6692f012016-09-18 21:46:08 -07004483 found = PyDict_GetItemWithError((PyObject *)dv->dv_dict, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004484 if (found == NULL) {
4485 if (PyErr_Occurred())
4486 return -1;
4487 return 0;
4488 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004489 Py_INCREF(found);
Serhiy Storchaka18b711c2019-08-04 14:12:48 +03004490 result = PyObject_RichCompareBool(found, value, Py_EQ);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004491 Py_DECREF(found);
4492 return result;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004493}
4494
Guido van Rossum83825ac2007-02-10 04:54:19 +00004495static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004496 (lenfunc)dictview_len, /* sq_length */
4497 0, /* sq_concat */
4498 0, /* sq_repeat */
4499 0, /* sq_item */
4500 0, /* sq_slice */
4501 0, /* sq_ass_item */
4502 0, /* sq_ass_slice */
4503 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004504};
4505
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004506static PyObject* dictitems_reversed(_PyDictViewObject *dv);
4507
4508PyDoc_STRVAR(reversed_items_doc,
4509"Return a reverse iterator over the dict items.");
4510
Guido van Rossumb90c8482007-02-10 01:11:45 +00004511static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004512 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4513 isdisjoint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004514 {"__reversed__", (PyCFunction)(void(*)(void))dictitems_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004515 reversed_items_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004516 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004517};
4518
4519PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004520 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4521 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004522 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004523 0, /* tp_itemsize */
4524 /* methods */
4525 (destructor)dictview_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004526 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004527 0, /* tp_getattr */
4528 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004529 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004530 (reprfunc)dictview_repr, /* tp_repr */
4531 &dictviews_as_number, /* tp_as_number */
4532 &dictitems_as_sequence, /* tp_as_sequence */
4533 0, /* tp_as_mapping */
4534 0, /* tp_hash */
4535 0, /* tp_call */
4536 0, /* tp_str */
4537 PyObject_GenericGetAttr, /* tp_getattro */
4538 0, /* tp_setattro */
4539 0, /* tp_as_buffer */
4540 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4541 0, /* tp_doc */
4542 (traverseproc)dictview_traverse, /* tp_traverse */
4543 0, /* tp_clear */
4544 dictview_richcompare, /* tp_richcompare */
4545 0, /* tp_weaklistoffset */
4546 (getiterfunc)dictitems_iter, /* tp_iter */
4547 0, /* tp_iternext */
4548 dictitems_methods, /* tp_methods */
4549 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004550};
4551
4552static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304553dictitems_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004554{
Eric Snow96c6af92015-05-29 22:21:39 -06004555 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004556}
4557
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004558static PyObject *
4559dictitems_reversed(_PyDictViewObject *dv)
4560{
4561 if (dv->dv_dict == NULL) {
4562 Py_RETURN_NONE;
4563 }
4564 return dictiter_new(dv->dv_dict, &PyDictRevIterItem_Type);
4565}
4566
Guido van Rossum3ac67412007-02-10 18:55:06 +00004567/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004568
4569static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004570dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004571{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004572 if (dv->dv_dict == NULL) {
4573 Py_RETURN_NONE;
4574 }
4575 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004576}
4577
Guido van Rossum83825ac2007-02-10 04:54:19 +00004578static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004579 (lenfunc)dictview_len, /* sq_length */
4580 0, /* sq_concat */
4581 0, /* sq_repeat */
4582 0, /* sq_item */
4583 0, /* sq_slice */
4584 0, /* sq_ass_item */
4585 0, /* sq_ass_slice */
4586 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004587};
4588
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004589static PyObject* dictvalues_reversed(_PyDictViewObject *dv);
4590
4591PyDoc_STRVAR(reversed_values_doc,
4592"Return a reverse iterator over the dict values.");
4593
Guido van Rossumb90c8482007-02-10 01:11:45 +00004594static PyMethodDef dictvalues_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004595 {"__reversed__", (PyCFunction)(void(*)(void))dictvalues_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004596 reversed_values_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004597 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004598};
4599
4600PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004601 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4602 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004603 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004604 0, /* tp_itemsize */
4605 /* methods */
4606 (destructor)dictview_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004607 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004608 0, /* tp_getattr */
4609 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004610 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004611 (reprfunc)dictview_repr, /* tp_repr */
4612 0, /* tp_as_number */
4613 &dictvalues_as_sequence, /* tp_as_sequence */
4614 0, /* tp_as_mapping */
4615 0, /* tp_hash */
4616 0, /* tp_call */
4617 0, /* tp_str */
4618 PyObject_GenericGetAttr, /* tp_getattro */
4619 0, /* tp_setattro */
4620 0, /* tp_as_buffer */
4621 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4622 0, /* tp_doc */
4623 (traverseproc)dictview_traverse, /* tp_traverse */
4624 0, /* tp_clear */
4625 0, /* tp_richcompare */
4626 0, /* tp_weaklistoffset */
4627 (getiterfunc)dictvalues_iter, /* tp_iter */
4628 0, /* tp_iternext */
4629 dictvalues_methods, /* tp_methods */
4630 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004631};
4632
4633static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304634dictvalues_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004635{
Eric Snow96c6af92015-05-29 22:21:39 -06004636 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004637}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004638
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004639static PyObject *
4640dictvalues_reversed(_PyDictViewObject *dv)
4641{
4642 if (dv->dv_dict == NULL) {
4643 Py_RETURN_NONE;
4644 }
4645 return dictiter_new(dv->dv_dict, &PyDictRevIterValue_Type);
4646}
4647
4648
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004649/* Returns NULL if cannot allocate a new PyDictKeysObject,
4650 but does not set an error */
4651PyDictKeysObject *
4652_PyDict_NewKeysForClass(void)
4653{
Victor Stinner742da042016-09-07 17:40:12 -07004654 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004655 if (keys == NULL)
4656 PyErr_Clear();
4657 else
4658 keys->dk_lookup = lookdict_split;
4659 return keys;
4660}
4661
4662#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
4663
4664PyObject *
4665PyObject_GenericGetDict(PyObject *obj, void *context)
4666{
4667 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
4668 if (dictptr == NULL) {
4669 PyErr_SetString(PyExc_AttributeError,
4670 "This object has no __dict__");
4671 return NULL;
4672 }
4673 dict = *dictptr;
4674 if (dict == NULL) {
4675 PyTypeObject *tp = Py_TYPE(obj);
4676 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
INADA Naokia7576492018-11-14 18:39:27 +09004677 dictkeys_incref(CACHED_KEYS(tp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004678 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
4679 }
4680 else {
4681 *dictptr = dict = PyDict_New();
4682 }
4683 }
4684 Py_XINCREF(dict);
4685 return dict;
4686}
4687
4688int
4689_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
Victor Stinner742da042016-09-07 17:40:12 -07004690 PyObject *key, PyObject *value)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004691{
4692 PyObject *dict;
4693 int res;
4694 PyDictKeysObject *cached;
4695
4696 assert(dictptr != NULL);
4697 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
4698 assert(dictptr != NULL);
4699 dict = *dictptr;
4700 if (dict == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +09004701 dictkeys_incref(cached);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004702 dict = new_dict_with_shared_keys(cached);
4703 if (dict == NULL)
4704 return -1;
4705 *dictptr = dict;
4706 }
4707 if (value == NULL) {
4708 res = PyDict_DelItem(dict, key);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004709 // Since key sharing dict doesn't allow deletion, PyDict_DelItem()
4710 // always converts dict to combined form.
4711 if ((cached = CACHED_KEYS(tp)) != NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004712 CACHED_KEYS(tp) = NULL;
INADA Naokia7576492018-11-14 18:39:27 +09004713 dictkeys_decref(cached);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004714 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01004715 }
4716 else {
INADA Naoki2294f3a2017-02-12 13:51:30 +09004717 int was_shared = (cached == ((PyDictObject *)dict)->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004718 res = PyDict_SetItem(dict, key, value);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004719 if (was_shared &&
4720 (cached = CACHED_KEYS(tp)) != NULL &&
4721 cached != ((PyDictObject *)dict)->ma_keys) {
Victor Stinner3d3f2642016-12-15 17:21:23 +01004722 /* PyDict_SetItem() may call dictresize and convert split table
4723 * into combined table. In such case, convert it to split
4724 * table again and update type's shared key only when this is
4725 * the only dict sharing key with the type.
4726 *
4727 * This is to allow using shared key in class like this:
4728 *
4729 * class C:
4730 * def __init__(self):
4731 * # one dict resize happens
4732 * self.a, self.b, self.c = 1, 2, 3
4733 * self.d, self.e, self.f = 4, 5, 6
4734 * a = C()
4735 */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004736 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004737 CACHED_KEYS(tp) = make_keys_shared(dict);
Victor Stinner742da042016-09-07 17:40:12 -07004738 }
4739 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004740 CACHED_KEYS(tp) = NULL;
4741 }
INADA Naokia7576492018-11-14 18:39:27 +09004742 dictkeys_decref(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004743 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
4744 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004745 }
4746 }
4747 } else {
4748 dict = *dictptr;
4749 if (dict == NULL) {
4750 dict = PyDict_New();
4751 if (dict == NULL)
4752 return -1;
4753 *dictptr = dict;
4754 }
4755 if (value == NULL) {
4756 res = PyDict_DelItem(dict, key);
4757 } else {
4758 res = PyDict_SetItem(dict, key, value);
4759 }
4760 }
4761 return res;
4762}
4763
4764void
4765_PyDictKeys_DecRef(PyDictKeysObject *keys)
4766{
INADA Naokia7576492018-11-14 18:39:27 +09004767 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004768}