blob: 48e96a09a5f87c419c9bafe0223d61b7c1f734de [file] [log] [blame]
Guido van Rossum2bc13791999-03-24 19:06:42 +00001/* Dictionary object implementation using a hash table */
Guido van Rossum9bfef441993-03-29 10:43:31 +00002
Raymond Hettinger930427b2003-05-03 06:51:59 +00003/* The distribution includes a separate file, Objects/dictnotes.txt,
Tim Peters60b29962006-01-01 01:19:23 +00004 describing explorations into dictionary design and optimization.
Raymond Hettinger930427b2003-05-03 06:51:59 +00005 It covers typical dictionary use patterns, the parameters for
6 tuning dictionaries, and several ideas for possible optimizations.
7*/
8
Victor Stinner742da042016-09-07 17:40:12 -07009/* PyDictKeysObject
10
11This implements the dictionary's hashtable.
12
Raymond Hettingerb12785d2016-10-22 09:58:14 -070013As of Python 3.6, this is compact and ordered. Basic idea is described here:
14* https://mail.python.org/pipermail/python-dev/2012-December/123028.html
15* https://morepypy.blogspot.com/2015/01/faster-more-memory-efficient-and-more.html
Victor Stinner742da042016-09-07 17:40:12 -070016
17layout:
18
19+---------------+
20| dk_refcnt |
21| dk_size |
22| dk_lookup |
23| dk_usable |
24| dk_nentries |
25+---------------+
26| dk_indices |
27| |
28+---------------+
29| dk_entries |
30| |
31+---------------+
32
33dk_indices is actual hashtable. It holds index in entries, or DKIX_EMPTY(-1)
34or DKIX_DUMMY(-2).
35Size of indices is dk_size. Type of each index in indices is vary on dk_size:
36
37* int8 for dk_size <= 128
38* int16 for 256 <= dk_size <= 2**15
39* int32 for 2**16 <= dk_size <= 2**31
40* int64 for 2**32 <= dk_size
41
dalgarno359143c2019-09-10 10:45:07 +010042dk_entries is array of PyDictKeyEntry. Its size is USABLE_FRACTION(dk_size).
Victor Stinner742da042016-09-07 17:40:12 -070043DK_ENTRIES(dk) can be used to get pointer to entries.
44
45NOTE: Since negative value is used for DKIX_EMPTY and DKIX_DUMMY, type of
46dk_indices entry is signed integer and int16 is used for table which
47dk_size == 256.
48*/
49
Benjamin Peterson7d95e402012-04-23 11:24:50 -040050
51/*
Benjamin Peterson7d95e402012-04-23 11:24:50 -040052The DictObject can be in one of two forms.
Victor Stinner742da042016-09-07 17:40:12 -070053
Benjamin Peterson7d95e402012-04-23 11:24:50 -040054Either:
55 A combined table:
56 ma_values == NULL, dk_refcnt == 1.
57 Values are stored in the me_value field of the PyDictKeysObject.
Benjamin Peterson7d95e402012-04-23 11:24:50 -040058Or:
59 A split table:
60 ma_values != NULL, dk_refcnt >= 1
61 Values are stored in the ma_values array.
Victor Stinner742da042016-09-07 17:40:12 -070062 Only string (unicode) keys are allowed.
63 All dicts sharing same key must have same insertion order.
Benjamin Peterson7d95e402012-04-23 11:24:50 -040064
Victor Stinner742da042016-09-07 17:40:12 -070065There are four kinds of slots in the table (slot is index, and
66DK_ENTRIES(keys)[index] if index >= 0):
67
681. Unused. index == DKIX_EMPTY
69 Does not hold an active (key, value) pair now and never did. Unused can
70 transition to Active upon key insertion. This is each slot's initial state.
71
722. Active. index >= 0, me_key != NULL and me_value != NULL
73 Holds an active (key, value) pair. Active can transition to Dummy or
74 Pending upon key deletion (for combined and split tables respectively).
75 This is the only case in which me_value != NULL.
76
773. Dummy. index == DKIX_DUMMY (combined only)
78 Previously held an active (key, value) pair, but that was deleted and an
79 active pair has not yet overwritten the slot. Dummy can transition to
80 Active upon key insertion. Dummy slots cannot be made Unused again
81 else the probe sequence in case of collision would have no way to know
82 they were once active.
83
844. Pending. index >= 0, key != NULL, and value == NULL (split only)
85 Not yet inserted in split-table.
Benjamin Peterson7d95e402012-04-23 11:24:50 -040086*/
87
Victor Stinner742da042016-09-07 17:40:12 -070088/*
89Preserving insertion order
Benjamin Peterson7d95e402012-04-23 11:24:50 -040090
Victor Stinner742da042016-09-07 17:40:12 -070091It's simple for combined table. Since dk_entries is mostly append only, we can
92get insertion order by just iterating dk_entries.
93
94One exception is .popitem(). It removes last item in dk_entries and decrement
95dk_nentries to achieve amortized O(1). Since there are DKIX_DUMMY remains in
96dk_indices, we can't increment dk_usable even though dk_nentries is
97decremented.
98
99In split table, inserting into pending entry is allowed only for dk_entries[ix]
100where ix == mp->ma_used. Inserting into other index and deleting item cause
101converting the dict to the combined table.
102*/
103
104/* PyDict_MINSIZE is the starting size for any new dict.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400105 * 8 allows dicts with no more than 5 active entries; experiments suggested
106 * this suffices for the majority of dicts (consisting mostly of usually-small
107 * dicts created to pass keyword arguments).
108 * Making this 8, rather than 4 reduces the number of resizes for most
109 * dictionaries, without any significant extra memory use.
110 */
Victor Stinner742da042016-09-07 17:40:12 -0700111#define PyDict_MINSIZE 8
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400112
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000113#include "Python.h"
Victor Stinnere5014be2020-04-14 17:52:15 +0200114#include "pycore_gc.h" // _PyObject_GC_IS_TRACKED()
Victor Stinner59d3dce2020-06-02 14:03:25 +0200115#include "pycore_object.h" // _PyObject_GC_TRACK()
116#include "pycore_pyerrors.h" // _PyErr_Fetch()
Victor Stinnere5014be2020-04-14 17:52:15 +0200117#include "pycore_pystate.h" // _PyThreadState_GET()
Eric Snow96c6af92015-05-29 22:21:39 -0600118#include "dict-common.h"
Victor Stinnere5014be2020-04-14 17:52:15 +0200119#include "stringlib/eq.h" // unicode_eq()
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000120
Larry Hastings61272b72014-01-07 12:41:53 -0800121/*[clinic input]
Larry Hastingsc2047262014-01-25 20:43:29 -0800122class dict "PyDictObject *" "&PyDict_Type"
Larry Hastings61272b72014-01-07 12:41:53 -0800123[clinic start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -0800124/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f157a5a0ce9589d6]*/
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800125
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400126
127/*
128To ensure the lookup algorithm terminates, there must be at least one Unused
129slot (NULL key) in the table.
130To avoid slowing down lookups on a near-full table, we resize the table when
131it's USABLE_FRACTION (currently two-thirds) full.
132*/
Guido van Rossum16e93a81997-01-28 00:00:11 +0000133
Tim Peterseb28ef22001-06-02 05:27:19 +0000134#define PERTURB_SHIFT 5
135
Guido van Rossum16e93a81997-01-28 00:00:11 +0000136/*
Tim Peterseb28ef22001-06-02 05:27:19 +0000137Major subtleties ahead: Most hash schemes depend on having a "good" hash
138function, in the sense of simulating randomness. Python doesn't: its most
R David Murray537ad7a2016-07-10 12:33:18 -0400139important hash functions (for ints) are very regular in common
Tim Peterseb28ef22001-06-02 05:27:19 +0000140cases:
Tim Peters15d49292001-05-27 07:39:22 +0000141
R David Murray537ad7a2016-07-10 12:33:18 -0400142 >>>[hash(i) for i in range(4)]
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000143 [0, 1, 2, 3]
Tim Peters15d49292001-05-27 07:39:22 +0000144
Tim Peterseb28ef22001-06-02 05:27:19 +0000145This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
146the low-order i bits as the initial table index is extremely fast, and there
R David Murray537ad7a2016-07-10 12:33:18 -0400147are no collisions at all for dicts indexed by a contiguous range of ints. So
148this gives better-than-random behavior in common cases, and that's very
149desirable.
Tim Peters15d49292001-05-27 07:39:22 +0000150
Tim Peterseb28ef22001-06-02 05:27:19 +0000151OTOH, when collisions occur, the tendency to fill contiguous slices of the
152hash table makes a good collision resolution strategy crucial. Taking only
153the last i bits of the hash code is also vulnerable: for example, consider
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000155their own hash codes, and this fits in a dict of size 2**15, the last 15 bits
156 of every hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +0000157
Tim Peterseb28ef22001-06-02 05:27:19 +0000158But catering to unusual cases should not slow the usual ones, so we just take
159the last i bits anyway. It's up to collision resolution to do the rest. If
160we *usually* find the key we're looking for on the first try (and, it turns
161out, we usually do -- the table load factor is kept under 2/3, so the odds
162are solidly in our favor), then it makes best sense to keep the initial index
163computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +0000164
Tim Peterseb28ef22001-06-02 05:27:19 +0000165The first half of collision resolution is to visit table indices via this
166recurrence:
Tim Peters15d49292001-05-27 07:39:22 +0000167
Tim Peterseb28ef22001-06-02 05:27:19 +0000168 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +0000169
Tim Peterseb28ef22001-06-02 05:27:19 +0000170For any initial j in range(2**i), repeating that 2**i times generates each
171int in range(2**i) exactly once (see any text on random-number generation for
172proof). By itself, this doesn't help much: like linear probing (setting
173j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
174order. This would be bad, except that's not the only thing we do, and it's
175actually *good* in the common cases where hash keys are consecutive. In an
176example that's really too small to make this entirely clear, for a table of
177size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +0000178
Tim Peterseb28ef22001-06-02 05:27:19 +0000179 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
180
181If two things come in at index 5, the first place we look after is index 2,
182not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
183Linear probing is deadly in this case because there the fixed probe order
184is the *same* as the order consecutive keys are likely to arrive. But it's
185extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
186and certain that consecutive hash codes do not.
187
188The other half of the strategy is to get the other bits of the hash code
189into play. This is done by initializing a (unsigned) vrbl "perturb" to the
190full hash code, and changing the recurrence to:
191
Tim Peterseb28ef22001-06-02 05:27:19 +0000192 perturb >>= PERTURB_SHIFT;
INADA Naoki267941c2016-10-06 15:19:07 +0900193 j = (5*j) + 1 + perturb;
Tim Peterseb28ef22001-06-02 05:27:19 +0000194 use j % 2**i as the next table index;
195
196Now the probe sequence depends (eventually) on every bit in the hash code,
197and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
198because it quickly magnifies small differences in the bits that didn't affect
199the initial index. Note that because perturb is unsigned, if the recurrence
200is executed often enough perturb eventually becomes and remains 0. At that
201point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
202that's certain to find an empty slot eventually (since it generates every int
203in range(2**i), and we make sure there's always at least one empty slot).
204
205Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
206small so that the high bits of the hash code continue to affect the probe
207sequence across iterations; but you want it large so that in really bad cases
208the high-order hash bits have an effect on early iterations. 5 was "the
209best" in minimizing total collisions across experiments Tim Peters ran (on
210both normal and pathological cases), but 4 and 6 weren't significantly worse.
211
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000212Historical: Reimer Behrends contributed the idea of using a polynomial-based
Tim Peterseb28ef22001-06-02 05:27:19 +0000213approach, using repeated multiplication by x in GF(2**n) where an irreducible
214polynomial for each table size was chosen such that x was a primitive root.
215Christian Tismer later extended that to use division by x instead, as an
216efficient way to get the high bits of the hash code into play. This scheme
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000217also gave excellent collision statistics, but was more expensive: two
218if-tests were required inside the loop; computing "the next" index took about
219the same number of operations but without as much potential parallelism
220(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
221above, and then shifting perturb can be done while the table index is being
222masked); and the PyDictObject struct required a member to hold the table's
223polynomial. In Tim's experiments the current scheme ran faster, produced
224equally good collision statistics, needed less code & used less memory.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000225
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000226*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000227
Fred Drake1bff34a2000-08-31 19:31:38 +0000228/* forward declarations */
Victor Stinner742da042016-09-07 17:40:12 -0700229static Py_ssize_t lookdict(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 lookdict_unicode(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900232 Py_hash_t hash, PyObject **value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700233static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400234lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900235 Py_hash_t hash, PyObject **value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700236static Py_ssize_t lookdict_split(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900237 Py_hash_t hash, PyObject **value_addr);
Fred Drake1bff34a2000-08-31 19:31:38 +0000238
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400239static int dictresize(PyDictObject *mp, Py_ssize_t minused);
Tim Petersdea48ec2001-05-22 20:40:22 +0000240
INADA Naoki2aaf98c2018-09-26 12:59:00 +0900241static PyObject* dict_iter(PyDictObject *dict);
242
Benjamin Peterson3c569292016-09-08 13:16:41 -0700243/*Global counter used to set ma_version_tag field of dictionary.
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700244 * It is incremented each time that a dictionary is created and each
245 * time that a dictionary is modified. */
246static uint64_t pydict_global_version = 0;
247
248#define DICT_NEXT_VERSION() (++pydict_global_version)
249
Victor Stinner742da042016-09-07 17:40:12 -0700250/* Dictionary reuse scheme to save calls to malloc and free */
Christian Heimes2202f872008-02-06 14:31:34 +0000251#ifndef PyDict_MAXFREELIST
252#define PyDict_MAXFREELIST 80
253#endif
Victor Stinnerb4b53862020-05-05 19:55:29 +0200254
255/* bpo-40521: dict free lists are shared by all interpreters. */
256#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
257# undef PyDict_MAXFREELIST
258# define PyDict_MAXFREELIST 0
259#endif
260
261#if PyDict_MAXFREELIST > 0
Christian Heimes2202f872008-02-06 14:31:34 +0000262static PyDictObject *free_list[PyDict_MAXFREELIST];
263static int numfree = 0;
Victor Stinner742da042016-09-07 17:40:12 -0700264static PyDictKeysObject *keys_free_list[PyDict_MAXFREELIST];
265static int numfreekeys = 0;
Victor Stinnerb4b53862020-05-05 19:55:29 +0200266#endif
Raymond Hettinger43442782004-03-17 21:55:03 +0000267
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300268#include "clinic/dictobject.c.h"
269
Victor Stinnerae00a5a2020-04-29 02:29:20 +0200270void
271_PyDict_ClearFreeList(void)
Christian Heimes77c02eb2008-02-09 02:18:51 +0000272{
Victor Stinnerb4b53862020-05-05 19:55:29 +0200273#if PyDict_MAXFREELIST > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 while (numfree) {
Victor Stinnerae00a5a2020-04-29 02:29:20 +0200275 PyDictObject *op = free_list[--numfree];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 assert(PyDict_CheckExact(op));
277 PyObject_GC_Del(op);
278 }
Victor Stinner742da042016-09-07 17:40:12 -0700279 while (numfreekeys) {
280 PyObject_FREE(keys_free_list[--numfreekeys]);
281 }
Victor Stinnerb4b53862020-05-05 19:55:29 +0200282#endif
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100283}
284
David Malcolm49526f42012-06-22 14:55:41 -0400285/* Print summary info about the state of the optimized allocator */
286void
287_PyDict_DebugMallocStats(FILE *out)
288{
Victor Stinnerb4b53862020-05-05 19:55:29 +0200289#if PyDict_MAXFREELIST > 0
David Malcolm49526f42012-06-22 14:55:41 -0400290 _PyDebugAllocatorStats(out,
291 "free PyDictObject", numfree, sizeof(PyDictObject));
Victor Stinnerb4b53862020-05-05 19:55:29 +0200292#endif
David Malcolm49526f42012-06-22 14:55:41 -0400293}
294
295
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100296void
Victor Stinnerbed48172019-08-27 00:12:32 +0200297_PyDict_Fini(void)
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100298{
Victor Stinnerae00a5a2020-04-29 02:29:20 +0200299 _PyDict_ClearFreeList();
Christian Heimes77c02eb2008-02-09 02:18:51 +0000300}
301
Victor Stinner742da042016-09-07 17:40:12 -0700302#define DK_SIZE(dk) ((dk)->dk_size)
303#if SIZEOF_VOID_P > 4
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700304#define DK_IXSIZE(dk) \
305 (DK_SIZE(dk) <= 0xff ? \
306 1 : DK_SIZE(dk) <= 0xffff ? \
307 2 : DK_SIZE(dk) <= 0xffffffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700308 4 : sizeof(int64_t))
Victor Stinner742da042016-09-07 17:40:12 -0700309#else
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700310#define DK_IXSIZE(dk) \
311 (DK_SIZE(dk) <= 0xff ? \
312 1 : DK_SIZE(dk) <= 0xffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700313 2 : sizeof(int32_t))
Victor Stinner742da042016-09-07 17:40:12 -0700314#endif
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700315#define DK_ENTRIES(dk) \
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700316 ((PyDictKeyEntry*)(&((int8_t*)((dk)->dk_indices))[DK_SIZE(dk) * DK_IXSIZE(dk)]))
Victor Stinner742da042016-09-07 17:40:12 -0700317
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400318#define DK_MASK(dk) (((dk)->dk_size)-1)
319#define IS_POWER_OF_2(x) (((x) & (x-1)) == 0)
320
INADA Naokia7576492018-11-14 18:39:27 +0900321static void free_keys_object(PyDictKeysObject *keys);
322
323static inline void
324dictkeys_incref(PyDictKeysObject *dk)
325{
Victor Stinner49932fe2020-02-03 17:55:05 +0100326#ifdef Py_REF_DEBUG
327 _Py_RefTotal++;
328#endif
INADA Naokia7576492018-11-14 18:39:27 +0900329 dk->dk_refcnt++;
330}
331
332static inline void
333dictkeys_decref(PyDictKeysObject *dk)
334{
335 assert(dk->dk_refcnt > 0);
Victor Stinner49932fe2020-02-03 17:55:05 +0100336#ifdef Py_REF_DEBUG
337 _Py_RefTotal--;
338#endif
INADA Naokia7576492018-11-14 18:39:27 +0900339 if (--dk->dk_refcnt == 0) {
340 free_keys_object(dk);
341 }
342}
343
Victor Stinner742da042016-09-07 17:40:12 -0700344/* lookup indices. returns DKIX_EMPTY, DKIX_DUMMY, or ix >=0 */
Benjamin Peterson73222252016-09-08 09:58:47 -0700345static inline Py_ssize_t
Andy Lester62d21c92020-03-25 23:13:01 -0500346dictkeys_get_index(const PyDictKeysObject *keys, Py_ssize_t i)
Victor Stinner742da042016-09-07 17:40:12 -0700347{
348 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700349 Py_ssize_t ix;
350
Victor Stinner742da042016-09-07 17:40:12 -0700351 if (s <= 0xff) {
Andy Lester62d21c92020-03-25 23:13:01 -0500352 const int8_t *indices = (const int8_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700353 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700354 }
355 else if (s <= 0xffff) {
Andy Lester62d21c92020-03-25 23:13:01 -0500356 const int16_t *indices = (const int16_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700357 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700358 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700359#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300360 else if (s > 0xffffffff) {
Andy Lester62d21c92020-03-25 23:13:01 -0500361 const int64_t *indices = (const int64_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700362 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700363 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700364#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300365 else {
Andy Lester62d21c92020-03-25 23:13:01 -0500366 const int32_t *indices = (const int32_t*)(keys->dk_indices);
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300367 ix = indices[i];
368 }
Victor Stinner71211e32016-09-08 10:52:46 -0700369 assert(ix >= DKIX_DUMMY);
370 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700371}
372
373/* write to indices. */
Benjamin Peterson73222252016-09-08 09:58:47 -0700374static inline void
INADA Naokia7576492018-11-14 18:39:27 +0900375dictkeys_set_index(PyDictKeysObject *keys, Py_ssize_t i, Py_ssize_t ix)
Victor Stinner742da042016-09-07 17:40:12 -0700376{
377 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700378
379 assert(ix >= DKIX_DUMMY);
380
Victor Stinner742da042016-09-07 17:40:12 -0700381 if (s <= 0xff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700382 int8_t *indices = (int8_t*)(keys->dk_indices);
Victor Stinner71211e32016-09-08 10:52:46 -0700383 assert(ix <= 0x7f);
Victor Stinner208857e2016-09-08 11:35:46 -0700384 indices[i] = (char)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700385 }
386 else if (s <= 0xffff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700387 int16_t *indices = (int16_t*)(keys->dk_indices);
Victor Stinner71211e32016-09-08 10:52:46 -0700388 assert(ix <= 0x7fff);
Victor Stinner208857e2016-09-08 11:35:46 -0700389 indices[i] = (int16_t)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700390 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700391#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300392 else if (s > 0xffffffff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700393 int64_t *indices = (int64_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700394 indices[i] = ix;
Victor Stinner742da042016-09-07 17:40:12 -0700395 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700396#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300397 else {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700398 int32_t *indices = (int32_t*)(keys->dk_indices);
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300399 assert(ix <= 0x7fffffff);
400 indices[i] = (int32_t)ix;
401 }
Victor Stinner742da042016-09-07 17:40:12 -0700402}
403
404
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200405/* USABLE_FRACTION is the maximum dictionary load.
Victor Stinner742da042016-09-07 17:40:12 -0700406 * Increasing this ratio makes dictionaries more dense resulting in more
407 * collisions. Decreasing it improves sparseness at the expense of spreading
408 * indices over more cache lines and at the cost of total memory consumed.
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200409 *
410 * USABLE_FRACTION must obey the following:
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400411 * (0 < USABLE_FRACTION(n) < n) for all n >= 2
412 *
Victor Stinner742da042016-09-07 17:40:12 -0700413 * USABLE_FRACTION should be quick to calculate.
414 * Fractions around 1/2 to 2/3 seem to work well in practice.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400415 */
Victor Stinner742da042016-09-07 17:40:12 -0700416#define USABLE_FRACTION(n) (((n) << 1)/3)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400417
Victor Stinner742da042016-09-07 17:40:12 -0700418/* ESTIMATE_SIZE is reverse function of USABLE_FRACTION.
419 * This can be used to reserve enough size to insert n entries without
420 * resizing.
421 */
INADA Naoki92c50ee2016-11-22 00:57:02 +0900422#define ESTIMATE_SIZE(n) (((n)*3+1) >> 1)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400423
Victor Stinner742da042016-09-07 17:40:12 -0700424/* Alternative fraction that is otherwise close enough to 2n/3 to make
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400425 * little difference. 8 * 2/3 == 8 * 5/8 == 5. 16 * 2/3 == 16 * 5/8 == 10.
426 * 32 * 2/3 = 21, 32 * 5/8 = 20.
427 * Its advantage is that it is faster to compute on machines with slow division.
428 * #define USABLE_FRACTION(n) (((n) >> 1) + ((n) >> 2) - ((n) >> 3))
Victor Stinner742da042016-09-07 17:40:12 -0700429 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400430
Victor Stinnera9f61a52013-07-16 22:17:26 +0200431/* GROWTH_RATE. Growth rate upon hitting maximum load.
INADA Naoki5fbc5112018-04-17 15:53:34 +0900432 * Currently set to used*3.
Victor Stinnera9f61a52013-07-16 22:17:26 +0200433 * This means that dicts double in size when growing without deletions,
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700434 * but have more head room when the number of deletions is on a par with the
INADA Naoki5fbc5112018-04-17 15:53:34 +0900435 * number of insertions. See also bpo-17563 and bpo-33205.
436 *
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700437 * GROWTH_RATE was set to used*4 up to version 3.2.
438 * GROWTH_RATE was set to used*2 in version 3.3.0
INADA Naoki5fbc5112018-04-17 15:53:34 +0900439 * GROWTH_RATE was set to used*2 + capacity/2 in 3.4.0-3.6.0.
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200440 */
INADA Naoki5fbc5112018-04-17 15:53:34 +0900441#define GROWTH_RATE(d) ((d)->ma_used*3)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400442
443#define ENSURE_ALLOWS_DELETIONS(d) \
444 if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \
445 (d)->ma_keys->dk_lookup = lookdict_unicode; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400447
448/* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
449 * (which cannot fail and thus can do no allocation).
450 */
451static PyDictKeysObject empty_keys_struct = {
Serhiy Storchaka97932e42016-09-26 23:01:23 +0300452 1, /* dk_refcnt */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400453 1, /* dk_size */
454 lookdict_split, /* dk_lookup */
455 0, /* dk_usable (immutable) */
Victor Stinner742da042016-09-07 17:40:12 -0700456 0, /* dk_nentries */
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700457 {DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY,
458 DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY}, /* dk_indices */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400459};
460
461static PyObject *empty_values[1] = { NULL };
462
463#define Py_EMPTY_KEYS &empty_keys_struct
464
Victor Stinner611b0fa2016-09-14 15:02:01 +0200465/* Uncomment to check the dict content in _PyDict_CheckConsistency() */
466/* #define DEBUG_PYDICT */
467
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200468#ifdef DEBUG_PYDICT
469# define ASSERT_CONSISTENT(op) assert(_PyDict_CheckConsistency((PyObject *)(op), 1))
470#else
471# define ASSERT_CONSISTENT(op) assert(_PyDict_CheckConsistency((PyObject *)(op), 0))
472#endif
Victor Stinner611b0fa2016-09-14 15:02:01 +0200473
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200474
475int
476_PyDict_CheckConsistency(PyObject *op, int check_content)
Victor Stinner611b0fa2016-09-14 15:02:01 +0200477{
Victor Stinner68762572019-10-07 18:42:01 +0200478#define CHECK(expr) \
479 do { if (!(expr)) { _PyObject_ASSERT_FAILED_MSG(op, Py_STRINGIFY(expr)); } } while (0)
480
481 assert(op != NULL);
482 CHECK(PyDict_Check(op));
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200483 PyDictObject *mp = (PyDictObject *)op;
Victor Stinner50fe3f82018-10-26 18:47:15 +0200484
Victor Stinner611b0fa2016-09-14 15:02:01 +0200485 PyDictKeysObject *keys = mp->ma_keys;
486 int splitted = _PyDict_HasSplitTable(mp);
487 Py_ssize_t usable = USABLE_FRACTION(keys->dk_size);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200488
Victor Stinner68762572019-10-07 18:42:01 +0200489 CHECK(0 <= mp->ma_used && mp->ma_used <= usable);
490 CHECK(IS_POWER_OF_2(keys->dk_size));
491 CHECK(0 <= keys->dk_usable && keys->dk_usable <= usable);
492 CHECK(0 <= keys->dk_nentries && keys->dk_nentries <= usable);
493 CHECK(keys->dk_usable + keys->dk_nentries <= usable);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200494
495 if (!splitted) {
496 /* combined table */
Victor Stinner68762572019-10-07 18:42:01 +0200497 CHECK(keys->dk_refcnt == 1);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200498 }
499
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200500 if (check_content) {
501 PyDictKeyEntry *entries = DK_ENTRIES(keys);
502 Py_ssize_t i;
Victor Stinner611b0fa2016-09-14 15:02:01 +0200503
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200504 for (i=0; i < keys->dk_size; i++) {
505 Py_ssize_t ix = dictkeys_get_index(keys, i);
Victor Stinner68762572019-10-07 18:42:01 +0200506 CHECK(DKIX_DUMMY <= ix && ix <= usable);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200507 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200508
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200509 for (i=0; i < usable; i++) {
510 PyDictKeyEntry *entry = &entries[i];
511 PyObject *key = entry->me_key;
512
513 if (key != NULL) {
514 if (PyUnicode_CheckExact(key)) {
515 Py_hash_t hash = ((PyASCIIObject *)key)->hash;
Victor Stinner68762572019-10-07 18:42:01 +0200516 CHECK(hash != -1);
517 CHECK(entry->me_hash == hash);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200518 }
519 else {
520 /* test_dict fails if PyObject_Hash() is called again */
Victor Stinner68762572019-10-07 18:42:01 +0200521 CHECK(entry->me_hash != -1);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200522 }
523 if (!splitted) {
Victor Stinner68762572019-10-07 18:42:01 +0200524 CHECK(entry->me_value != NULL);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200525 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200526 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200527
528 if (splitted) {
Victor Stinner68762572019-10-07 18:42:01 +0200529 CHECK(entry->me_value == NULL);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200530 }
531 }
532
533 if (splitted) {
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200534 /* splitted table */
535 for (i=0; i < mp->ma_used; i++) {
Victor Stinner68762572019-10-07 18:42:01 +0200536 CHECK(mp->ma_values[i] != NULL);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200537 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200538 }
539 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200540 return 1;
Victor Stinner68762572019-10-07 18:42:01 +0200541
542#undef CHECK
Victor Stinner611b0fa2016-09-14 15:02:01 +0200543}
Victor Stinner611b0fa2016-09-14 15:02:01 +0200544
545
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400546static PyDictKeysObject *new_keys_object(Py_ssize_t size)
547{
548 PyDictKeysObject *dk;
Victor Stinner742da042016-09-07 17:40:12 -0700549 Py_ssize_t es, usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400550
Victor Stinner742da042016-09-07 17:40:12 -0700551 assert(size >= PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400552 assert(IS_POWER_OF_2(size));
Victor Stinner742da042016-09-07 17:40:12 -0700553
554 usable = USABLE_FRACTION(size);
555 if (size <= 0xff) {
556 es = 1;
557 }
558 else if (size <= 0xffff) {
559 es = 2;
560 }
561#if SIZEOF_VOID_P > 4
562 else if (size <= 0xffffffff) {
563 es = 4;
564 }
565#endif
566 else {
567 es = sizeof(Py_ssize_t);
568 }
569
Victor Stinnerb4b53862020-05-05 19:55:29 +0200570#if PyDict_MAXFREELIST > 0
Victor Stinner742da042016-09-07 17:40:12 -0700571 if (size == PyDict_MINSIZE && numfreekeys > 0) {
572 dk = keys_free_list[--numfreekeys];
573 }
Victor Stinnerb4b53862020-05-05 19:55:29 +0200574 else
575#endif
576 {
Victor Stinner98ee9d52016-09-08 09:33:56 -0700577 dk = PyObject_MALLOC(sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -0700578 + es * size
579 + sizeof(PyDictKeyEntry) * usable);
Victor Stinner742da042016-09-07 17:40:12 -0700580 if (dk == NULL) {
581 PyErr_NoMemory();
582 return NULL;
583 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400584 }
Victor Stinner49932fe2020-02-03 17:55:05 +0100585#ifdef Py_REF_DEBUG
586 _Py_RefTotal++;
587#endif
INADA Naokia7576492018-11-14 18:39:27 +0900588 dk->dk_refcnt = 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400589 dk->dk_size = size;
Victor Stinner742da042016-09-07 17:40:12 -0700590 dk->dk_usable = usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400591 dk->dk_lookup = lookdict_unicode_nodummy;
Victor Stinner742da042016-09-07 17:40:12 -0700592 dk->dk_nentries = 0;
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700593 memset(&dk->dk_indices[0], 0xff, es * size);
Victor Stinner742da042016-09-07 17:40:12 -0700594 memset(DK_ENTRIES(dk), 0, sizeof(PyDictKeyEntry) * usable);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400595 return dk;
596}
597
598static void
599free_keys_object(PyDictKeysObject *keys)
600{
Victor Stinner742da042016-09-07 17:40:12 -0700601 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400602 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -0700603 for (i = 0, n = keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400604 Py_XDECREF(entries[i].me_key);
605 Py_XDECREF(entries[i].me_value);
606 }
Victor Stinnerb4b53862020-05-05 19:55:29 +0200607#if PyDict_MAXFREELIST > 0
Victor Stinner742da042016-09-07 17:40:12 -0700608 if (keys->dk_size == PyDict_MINSIZE && numfreekeys < PyDict_MAXFREELIST) {
609 keys_free_list[numfreekeys++] = keys;
610 return;
611 }
Victor Stinnerb4b53862020-05-05 19:55:29 +0200612#endif
Raymond Hettingerce5179f2016-01-31 08:56:21 -0800613 PyObject_FREE(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400614}
615
616#define new_values(size) PyMem_NEW(PyObject *, size)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400617#define free_values(values) PyMem_FREE(values)
618
619/* Consumes a reference to the keys object */
620static PyObject *
621new_dict(PyDictKeysObject *keys, PyObject **values)
622{
623 PyDictObject *mp;
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200624 assert(keys != NULL);
Victor Stinnerb4b53862020-05-05 19:55:29 +0200625#if PyDict_MAXFREELIST > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 if (numfree) {
627 mp = free_list[--numfree];
628 assert (mp != NULL);
Dong-hee Na1b55b652020-02-17 19:09:15 +0900629 assert (Py_IS_TYPE(mp, &PyDict_Type));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 }
Victor Stinnerb4b53862020-05-05 19:55:29 +0200632 else
633#endif
634 {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400635 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
636 if (mp == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +0900637 dictkeys_decref(keys);
Zackery Spytz3d07c1e2019-03-23 20:23:29 -0600638 if (values != empty_values) {
639 free_values(values);
640 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400641 return NULL;
642 }
643 }
644 mp->ma_keys = keys;
645 mp->ma_values = values;
646 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700647 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200648 ASSERT_CONSISTENT(mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000650}
651
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400652/* Consumes a reference to the keys object */
653static PyObject *
654new_dict_with_shared_keys(PyDictKeysObject *keys)
655{
656 PyObject **values;
657 Py_ssize_t i, size;
658
Victor Stinner742da042016-09-07 17:40:12 -0700659 size = USABLE_FRACTION(DK_SIZE(keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400660 values = new_values(size);
661 if (values == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +0900662 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400663 return PyErr_NoMemory();
664 }
665 for (i = 0; i < size; i++) {
666 values[i] = NULL;
667 }
668 return new_dict(keys, values);
669}
670
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500671
672static PyObject *
673clone_combined_dict(PyDictObject *orig)
674{
675 assert(PyDict_CheckExact(orig));
676 assert(orig->ma_values == NULL);
677 assert(orig->ma_keys->dk_refcnt == 1);
678
679 Py_ssize_t keys_size = _PyDict_KeysSize(orig->ma_keys);
680 PyDictKeysObject *keys = PyObject_Malloc(keys_size);
681 if (keys == NULL) {
682 PyErr_NoMemory();
683 return NULL;
684 }
685
686 memcpy(keys, orig->ma_keys, keys_size);
687
688 /* After copying key/value pairs, we need to incref all
689 keys and values and they are about to be co-owned by a
690 new dict object. */
691 PyDictKeyEntry *ep0 = DK_ENTRIES(keys);
692 Py_ssize_t n = keys->dk_nentries;
693 for (Py_ssize_t i = 0; i < n; i++) {
694 PyDictKeyEntry *entry = &ep0[i];
695 PyObject *value = entry->me_value;
696 if (value != NULL) {
697 Py_INCREF(value);
698 Py_INCREF(entry->me_key);
699 }
700 }
701
702 PyDictObject *new = (PyDictObject *)new_dict(keys, NULL);
703 if (new == NULL) {
704 /* In case of an error, `new_dict()` takes care of
705 cleaning up `keys`. */
706 return NULL;
707 }
708 new->ma_used = orig->ma_used;
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200709 ASSERT_CONSISTENT(new);
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500710 if (_PyObject_GC_IS_TRACKED(orig)) {
711 /* Maintain tracking. */
712 _PyObject_GC_TRACK(new);
713 }
Yury Selivanov0b752282018-07-06 12:20:07 -0400714
715 /* Since we copied the keys table we now have an extra reference
Victor Stinner49932fe2020-02-03 17:55:05 +0100716 in the system. Manually call increment _Py_RefTotal to signal that
INADA Naokia7576492018-11-14 18:39:27 +0900717 we have it now; calling dictkeys_incref would be an error as
Yury Selivanov0b752282018-07-06 12:20:07 -0400718 keys->dk_refcnt is already set to 1 (after memcpy). */
Victor Stinner49932fe2020-02-03 17:55:05 +0100719#ifdef Py_REF_DEBUG
720 _Py_RefTotal++;
721#endif
Yury Selivanov0b752282018-07-06 12:20:07 -0400722
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500723 return (PyObject *)new;
724}
725
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400726PyObject *
727PyDict_New(void)
728{
Inada Naokif2a18672019-03-12 17:25:44 +0900729 dictkeys_incref(Py_EMPTY_KEYS);
730 return new_dict(Py_EMPTY_KEYS, empty_values);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400731}
732
Victor Stinner742da042016-09-07 17:40:12 -0700733/* Search index of hash table from offset of entry table */
734static Py_ssize_t
735lookdict_index(PyDictKeysObject *k, Py_hash_t hash, Py_ssize_t index)
736{
Victor Stinner742da042016-09-07 17:40:12 -0700737 size_t mask = DK_MASK(k);
INADA Naoki073ae482017-06-23 15:22:50 +0900738 size_t perturb = (size_t)hash;
739 size_t i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700740
INADA Naoki073ae482017-06-23 15:22:50 +0900741 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900742 Py_ssize_t ix = dictkeys_get_index(k, i);
Victor Stinner742da042016-09-07 17:40:12 -0700743 if (ix == index) {
744 return i;
745 }
746 if (ix == DKIX_EMPTY) {
747 return DKIX_EMPTY;
748 }
INADA Naoki073ae482017-06-23 15:22:50 +0900749 perturb >>= PERTURB_SHIFT;
750 i = mask & (i*5 + perturb + 1);
Victor Stinner742da042016-09-07 17:40:12 -0700751 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700752 Py_UNREACHABLE();
Victor Stinner742da042016-09-07 17:40:12 -0700753}
754
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000755/*
756The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000757This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000758Open addressing is preferred over chaining since the link overhead for
759chaining would be substantial (100% with typical malloc overhead).
760
Tim Peterseb28ef22001-06-02 05:27:19 +0000761The initial probe index is computed as hash mod the table size. Subsequent
762probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000763
764All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000765
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000766The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000767contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000768Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000769
Victor Stinner742da042016-09-07 17:40:12 -0700770lookdict() is general-purpose, and may return DKIX_ERROR if (and only if) a
Victor Stinnera4348cc2016-09-08 12:01:25 -0700771comparison raises an exception.
Guido van Rossum89d8c602007-09-18 17:26:56 +0000772lookdict_unicode() below is specialized to string keys, comparison of which can
INADA Naoki1b8df102017-02-20 22:48:10 +0900773never raise an exception; that function can never return DKIX_ERROR when key
774is string. Otherwise, it falls back to lookdict().
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400775lookdict_unicode_nodummy is further specialized for string keys that cannot be
776the <dummy> value.
INADA Naoki778928b2017-08-03 23:45:15 +0900777For both, when the key isn't found a DKIX_EMPTY is returned.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000778*/
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100779static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400780lookdict(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900781 Py_hash_t hash, PyObject **value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000782{
INADA Naoki778928b2017-08-03 23:45:15 +0900783 size_t i, mask, perturb;
Victor Stinner742da042016-09-07 17:40:12 -0700784 PyDictKeysObject *dk;
INADA Naoki778928b2017-08-03 23:45:15 +0900785 PyDictKeyEntry *ep0;
Tim Peterseb28ef22001-06-02 05:27:19 +0000786
Antoine Pitrou9a234902012-05-13 20:48:01 +0200787top:
Victor Stinner742da042016-09-07 17:40:12 -0700788 dk = mp->ma_keys;
Victor Stinner742da042016-09-07 17:40:12 -0700789 ep0 = DK_ENTRIES(dk);
INADA Naoki778928b2017-08-03 23:45:15 +0900790 mask = DK_MASK(dk);
791 perturb = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700793
INADA Naoki778928b2017-08-03 23:45:15 +0900794 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900795 Py_ssize_t ix = dictkeys_get_index(dk, i);
Victor Stinner742da042016-09-07 17:40:12 -0700796 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700797 *value_addr = NULL;
798 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400799 }
INADA Naoki778928b2017-08-03 23:45:15 +0900800 if (ix >= 0) {
801 PyDictKeyEntry *ep = &ep0[ix];
802 assert(ep->me_key != NULL);
803 if (ep->me_key == key) {
804 *value_addr = ep->me_value;
805 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700806 }
INADA Naoki778928b2017-08-03 23:45:15 +0900807 if (ep->me_hash == hash) {
808 PyObject *startkey = ep->me_key;
809 Py_INCREF(startkey);
810 int cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
811 Py_DECREF(startkey);
812 if (cmp < 0) {
813 *value_addr = NULL;
814 return DKIX_ERROR;
815 }
816 if (dk == mp->ma_keys && ep->me_key == startkey) {
817 if (cmp > 0) {
818 *value_addr = ep->me_value;
819 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700820 }
INADA Naoki778928b2017-08-03 23:45:15 +0900821 }
822 else {
823 /* The dict was mutated, restart */
824 goto top;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400825 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 }
INADA Naoki778928b2017-08-03 23:45:15 +0900828 perturb >>= PERTURB_SHIFT;
829 i = (i*5 + perturb + 1) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700831 Py_UNREACHABLE();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000832}
833
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400834/* Specialized version for string-only keys */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100835static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400836lookdict_unicode(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900837 Py_hash_t hash, PyObject **value_addr)
Fred Drake1bff34a2000-08-31 19:31:38 +0000838{
Victor Stinner742da042016-09-07 17:40:12 -0700839 assert(mp->ma_values == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 /* Make sure this function doesn't have to handle non-unicode keys,
841 including subclasses of str; e.g., one reason to subclass
842 unicodes is to override __eq__, and for speed we don't cater to
843 that here. */
844 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400845 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900846 return lookdict(mp, key, hash, value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 }
Tim Peters15d49292001-05-27 07:39:22 +0000848
INADA Naoki778928b2017-08-03 23:45:15 +0900849 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
850 size_t mask = DK_MASK(mp->ma_keys);
851 size_t perturb = (size_t)hash;
852 size_t i = (size_t)hash & mask;
853
854 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900855 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700856 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700857 *value_addr = NULL;
858 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400859 }
INADA Naoki778928b2017-08-03 23:45:15 +0900860 if (ix >= 0) {
861 PyDictKeyEntry *ep = &ep0[ix];
862 assert(ep->me_key != NULL);
863 assert(PyUnicode_CheckExact(ep->me_key));
864 if (ep->me_key == key ||
865 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
866 *value_addr = ep->me_value;
867 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700868 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400869 }
INADA Naoki778928b2017-08-03 23:45:15 +0900870 perturb >>= PERTURB_SHIFT;
871 i = mask & (i*5 + perturb + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700873 Py_UNREACHABLE();
Fred Drake1bff34a2000-08-31 19:31:38 +0000874}
875
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400876/* Faster version of lookdict_unicode when it is known that no <dummy> keys
877 * will be present. */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100878static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400879lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900880 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400881{
Victor Stinner742da042016-09-07 17:40:12 -0700882 assert(mp->ma_values == NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400883 /* Make sure this function doesn't have to handle non-unicode keys,
884 including subclasses of str; e.g., one reason to subclass
885 unicodes is to override __eq__, and for speed we don't cater to
886 that here. */
887 if (!PyUnicode_CheckExact(key)) {
888 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900889 return lookdict(mp, key, hash, value_addr);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400890 }
INADA Naoki778928b2017-08-03 23:45:15 +0900891
892 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
893 size_t mask = DK_MASK(mp->ma_keys);
894 size_t perturb = (size_t)hash;
895 size_t i = (size_t)hash & mask;
896
897 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900898 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700899 assert (ix != DKIX_DUMMY);
900 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700901 *value_addr = NULL;
902 return DKIX_EMPTY;
903 }
INADA Naoki778928b2017-08-03 23:45:15 +0900904 PyDictKeyEntry *ep = &ep0[ix];
905 assert(ep->me_key != NULL);
906 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700907 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400908 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900909 *value_addr = ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700910 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400911 }
INADA Naoki778928b2017-08-03 23:45:15 +0900912 perturb >>= PERTURB_SHIFT;
913 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400914 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700915 Py_UNREACHABLE();
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400916}
917
918/* Version of lookdict for split tables.
919 * All split tables and only split tables use this lookup function.
920 * Split tables only contain unicode keys and no dummy keys,
921 * so algorithm is the same as lookdict_unicode_nodummy.
922 */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100923static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400924lookdict_split(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900925 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400926{
Victor Stinner742da042016-09-07 17:40:12 -0700927 /* mp must split table */
928 assert(mp->ma_values != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400929 if (!PyUnicode_CheckExact(key)) {
INADA Naoki778928b2017-08-03 23:45:15 +0900930 Py_ssize_t ix = lookdict(mp, key, hash, value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700931 if (ix >= 0) {
INADA Naokiba609772016-12-07 20:41:42 +0900932 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700933 }
934 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400935 }
Victor Stinner742da042016-09-07 17:40:12 -0700936
INADA Naoki778928b2017-08-03 23:45:15 +0900937 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
938 size_t mask = DK_MASK(mp->ma_keys);
939 size_t perturb = (size_t)hash;
940 size_t i = (size_t)hash & mask;
941
942 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900943 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
INADA Naoki778928b2017-08-03 23:45:15 +0900944 assert (ix != DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -0700945 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700946 *value_addr = NULL;
947 return DKIX_EMPTY;
948 }
INADA Naoki778928b2017-08-03 23:45:15 +0900949 PyDictKeyEntry *ep = &ep0[ix];
950 assert(ep->me_key != NULL);
951 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700952 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400953 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900954 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700955 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400956 }
INADA Naoki778928b2017-08-03 23:45:15 +0900957 perturb >>= PERTURB_SHIFT;
958 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400959 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700960 Py_UNREACHABLE();
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400961}
962
Benjamin Petersonfb886362010-04-24 18:21:17 +0000963int
964_PyDict_HasOnlyStringKeys(PyObject *dict)
965{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 Py_ssize_t pos = 0;
967 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000968 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400970 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 return 1;
972 while (PyDict_Next(dict, &pos, &key, &value))
973 if (!PyUnicode_Check(key))
974 return 0;
975 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000976}
977
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000978#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 do { \
980 if (!_PyObject_GC_IS_TRACKED(mp)) { \
981 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
982 _PyObject_GC_MAY_BE_TRACKED(value)) { \
983 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 } \
985 } \
986 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000987
988void
989_PyDict_MaybeUntrack(PyObject *op)
990{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 PyDictObject *mp;
992 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -0700993 Py_ssize_t i, numentries;
994 PyDictKeyEntry *ep0;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
997 return;
998
999 mp = (PyDictObject *) op;
Victor Stinner742da042016-09-07 17:40:12 -07001000 ep0 = DK_ENTRIES(mp->ma_keys);
1001 numentries = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001002 if (_PyDict_HasSplitTable(mp)) {
Victor Stinner742da042016-09-07 17:40:12 -07001003 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001004 if ((value = mp->ma_values[i]) == NULL)
1005 continue;
1006 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
Victor Stinner742da042016-09-07 17:40:12 -07001007 assert(!_PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001008 return;
1009 }
1010 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001012 else {
Victor Stinner742da042016-09-07 17:40:12 -07001013 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001014 if ((value = ep0[i].me_value) == NULL)
1015 continue;
1016 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
1017 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
1018 return;
1019 }
1020 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001022}
1023
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001024/* Internal function to find slot for an item from its hash
Victor Stinner3c336c52016-09-12 14:17:40 +02001025 when it is known that the key is not present in the dict.
1026
1027 The dict must be combined. */
INADA Naokiba609772016-12-07 20:41:42 +09001028static Py_ssize_t
INADA Naoki778928b2017-08-03 23:45:15 +09001029find_empty_slot(PyDictKeysObject *keys, Py_hash_t hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001030{
INADA Naoki778928b2017-08-03 23:45:15 +09001031 assert(keys != NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001032
INADA Naoki778928b2017-08-03 23:45:15 +09001033 const size_t mask = DK_MASK(keys);
1034 size_t i = hash & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001035 Py_ssize_t ix = dictkeys_get_index(keys, i);
INADA Naoki778928b2017-08-03 23:45:15 +09001036 for (size_t perturb = hash; ix >= 0;) {
INADA Naoki267941c2016-10-06 15:19:07 +09001037 perturb >>= PERTURB_SHIFT;
INADA Naoki778928b2017-08-03 23:45:15 +09001038 i = (i*5 + perturb + 1) & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001039 ix = dictkeys_get_index(keys, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 }
INADA Naoki778928b2017-08-03 23:45:15 +09001041 return i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001042}
1043
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001044static int
1045insertion_resize(PyDictObject *mp)
1046{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -07001047 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001048}
Antoine Pitroue965d972012-02-27 00:45:12 +01001049
1050/*
1051Internal routine to insert a new item into the table.
1052Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +01001053Returns -1 if an error occurred, or 0 on success.
1054*/
1055static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001056insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001057{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001058 PyObject *old_value;
INADA Naokiba609772016-12-07 20:41:42 +09001059 PyDictKeyEntry *ep;
Antoine Pitroue965d972012-02-27 00:45:12 +01001060
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001061 Py_INCREF(key);
1062 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001063 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
1064 if (insertion_resize(mp) < 0)
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001065 goto Fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001066 }
1067
INADA Naoki778928b2017-08-03 23:45:15 +09001068 Py_ssize_t ix = mp->ma_keys->dk_lookup(mp, key, hash, &old_value);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001069 if (ix == DKIX_ERROR)
1070 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001071
Antoine Pitroud6967322014-10-18 00:35:00 +02001072 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001073 MAINTAIN_TRACKING(mp, key, value);
Victor Stinner742da042016-09-07 17:40:12 -07001074
1075 /* When insertion order is different from shared key, we can't share
1076 * the key anymore. Convert this instance to combine table.
1077 */
1078 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09001079 ((ix >= 0 && old_value == NULL && mp->ma_used != ix) ||
Victor Stinner742da042016-09-07 17:40:12 -07001080 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001081 if (insertion_resize(mp) < 0)
1082 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001083 ix = DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001084 }
Victor Stinner742da042016-09-07 17:40:12 -07001085
1086 if (ix == DKIX_EMPTY) {
1087 /* Insert into new slot. */
INADA Naokiba609772016-12-07 20:41:42 +09001088 assert(old_value == NULL);
Victor Stinner742da042016-09-07 17:40:12 -07001089 if (mp->ma_keys->dk_usable <= 0) {
1090 /* Need to resize. */
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001091 if (insertion_resize(mp) < 0)
1092 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001093 }
INADA Naoki778928b2017-08-03 23:45:15 +09001094 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naokiba609772016-12-07 20:41:42 +09001095 ep = &DK_ENTRIES(mp->ma_keys)[mp->ma_keys->dk_nentries];
INADA Naokia7576492018-11-14 18:39:27 +09001096 dictkeys_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Victor Stinner742da042016-09-07 17:40:12 -07001097 ep->me_key = key;
1098 ep->me_hash = hash;
1099 if (mp->ma_values) {
1100 assert (mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
1101 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001102 }
1103 else {
Victor Stinner742da042016-09-07 17:40:12 -07001104 ep->me_value = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001105 }
1106 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001107 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07001108 mp->ma_keys->dk_usable--;
1109 mp->ma_keys->dk_nentries++;
1110 assert(mp->ma_keys->dk_usable >= 0);
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001111 ASSERT_CONSISTENT(mp);
Victor Stinner742da042016-09-07 17:40:12 -07001112 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001113 }
Victor Stinner742da042016-09-07 17:40:12 -07001114
Inada Naoki91234a12019-06-03 21:30:58 +09001115 if (old_value != value) {
1116 if (_PyDict_HasSplitTable(mp)) {
1117 mp->ma_values[ix] = value;
1118 if (old_value == NULL) {
1119 /* pending state */
1120 assert(ix == mp->ma_used);
1121 mp->ma_used++;
1122 }
INADA Naokiba609772016-12-07 20:41:42 +09001123 }
Inada Naoki91234a12019-06-03 21:30:58 +09001124 else {
1125 assert(old_value != NULL);
1126 DK_ENTRIES(mp->ma_keys)[ix].me_value = value;
1127 }
1128 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokiba609772016-12-07 20:41:42 +09001129 }
INADA Naokiba609772016-12-07 20:41:42 +09001130 Py_XDECREF(old_value); /* which **CAN** re-enter (see issue #22653) */
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001131 ASSERT_CONSISTENT(mp);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001132 Py_DECREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001133 return 0;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001134
1135Fail:
1136 Py_DECREF(value);
1137 Py_DECREF(key);
1138 return -1;
Antoine Pitroue965d972012-02-27 00:45:12 +01001139}
1140
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001141// Same to insertdict but specialized for ma_keys = Py_EMPTY_KEYS.
1142static int
1143insert_to_emptydict(PyDictObject *mp, PyObject *key, Py_hash_t hash,
1144 PyObject *value)
1145{
1146 assert(mp->ma_keys == Py_EMPTY_KEYS);
1147
1148 PyDictKeysObject *newkeys = new_keys_object(PyDict_MINSIZE);
1149 if (newkeys == NULL) {
1150 return -1;
1151 }
1152 if (!PyUnicode_CheckExact(key)) {
1153 newkeys->dk_lookup = lookdict;
1154 }
1155 dictkeys_decref(Py_EMPTY_KEYS);
1156 mp->ma_keys = newkeys;
1157 mp->ma_values = NULL;
1158
1159 Py_INCREF(key);
1160 Py_INCREF(value);
1161 MAINTAIN_TRACKING(mp, key, value);
1162
1163 size_t hashpos = (size_t)hash & (PyDict_MINSIZE-1);
Dong-hee Nac39d1dd2019-10-11 17:43:11 +09001164 PyDictKeyEntry *ep = DK_ENTRIES(mp->ma_keys);
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001165 dictkeys_set_index(mp->ma_keys, hashpos, 0);
1166 ep->me_key = key;
1167 ep->me_hash = hash;
1168 ep->me_value = value;
1169 mp->ma_used++;
1170 mp->ma_version_tag = DICT_NEXT_VERSION();
1171 mp->ma_keys->dk_usable--;
1172 mp->ma_keys->dk_nentries++;
1173 return 0;
1174}
1175
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001176/*
luzpaza5293b42017-11-05 07:37:50 -06001177Internal routine used by dictresize() to build a hashtable of entries.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001178*/
1179static void
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001180build_indices(PyDictKeysObject *keys, PyDictKeyEntry *ep, Py_ssize_t n)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001181{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001182 size_t mask = (size_t)DK_SIZE(keys) - 1;
1183 for (Py_ssize_t ix = 0; ix != n; ix++, ep++) {
1184 Py_hash_t hash = ep->me_hash;
1185 size_t i = hash & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001186 for (size_t perturb = hash; dictkeys_get_index(keys, i) != DKIX_EMPTY;) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001187 perturb >>= PERTURB_SHIFT;
INADA Naoki870c2862017-06-24 09:03:19 +09001188 i = mask & (i*5 + perturb + 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001189 }
INADA Naokia7576492018-11-14 18:39:27 +09001190 dictkeys_set_index(keys, i, ix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001192}
1193
1194/*
1195Restructure the table by allocating a new table and reinserting all
1196items again. When entries have been deleted, the new table may
1197actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001198If a table is split (its keys and hashes are shared, its values are not),
1199then the values are temporarily copied into the table, it is resized as
1200a combined table, then the me_value slots in the old table are NULLed out.
1201After resizing a table is always combined,
1202but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001203*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001204static int
Victor Stinner3d3f2642016-12-15 17:21:23 +01001205dictresize(PyDictObject *mp, Py_ssize_t minsize)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001206{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001207 Py_ssize_t newsize, numentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001208 PyDictKeysObject *oldkeys;
1209 PyObject **oldvalues;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001210 PyDictKeyEntry *oldentries, *newentries;
Tim Peters91a364d2001-05-19 07:04:38 +00001211
Victor Stinner742da042016-09-07 17:40:12 -07001212 /* Find the smallest table size > minused. */
1213 for (newsize = PyDict_MINSIZE;
Victor Stinner3d3f2642016-12-15 17:21:23 +01001214 newsize < minsize && newsize > 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 newsize <<= 1)
1216 ;
1217 if (newsize <= 0) {
1218 PyErr_NoMemory();
1219 return -1;
1220 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001221
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001222 oldkeys = mp->ma_keys;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001223
1224 /* NOTE: Current odict checks mp->ma_keys to detect resize happen.
1225 * So we can't reuse oldkeys even if oldkeys->dk_size == newsize.
1226 * TODO: Try reusing oldkeys when reimplement odict.
1227 */
1228
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001229 /* Allocate a new table. */
1230 mp->ma_keys = new_keys_object(newsize);
1231 if (mp->ma_keys == NULL) {
1232 mp->ma_keys = oldkeys;
1233 return -1;
1234 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01001235 // New table must be large enough.
1236 assert(mp->ma_keys->dk_usable >= mp->ma_used);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001237 if (oldkeys->dk_lookup == lookdict)
1238 mp->ma_keys->dk_lookup = lookdict;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001239
1240 numentries = mp->ma_used;
1241 oldentries = DK_ENTRIES(oldkeys);
1242 newentries = DK_ENTRIES(mp->ma_keys);
1243 oldvalues = mp->ma_values;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001244 if (oldvalues != NULL) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001245 /* Convert split table into new combined table.
1246 * We must incref keys; we can transfer values.
1247 * Note that values of split table is always dense.
1248 */
1249 for (Py_ssize_t i = 0; i < numentries; i++) {
1250 assert(oldvalues[i] != NULL);
1251 PyDictKeyEntry *ep = &oldentries[i];
1252 PyObject *key = ep->me_key;
1253 Py_INCREF(key);
1254 newentries[i].me_key = key;
1255 newentries[i].me_hash = ep->me_hash;
1256 newentries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001258
INADA Naokia7576492018-11-14 18:39:27 +09001259 dictkeys_decref(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001260 mp->ma_values = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001261 if (oldvalues != empty_values) {
1262 free_values(oldvalues);
1263 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001264 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001265 else { // combined table.
1266 if (oldkeys->dk_nentries == numentries) {
1267 memcpy(newentries, oldentries, numentries * sizeof(PyDictKeyEntry));
1268 }
1269 else {
1270 PyDictKeyEntry *ep = oldentries;
1271 for (Py_ssize_t i = 0; i < numentries; i++) {
1272 while (ep->me_value == NULL)
1273 ep++;
1274 newentries[i] = *ep++;
1275 }
1276 }
1277
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001278 assert(oldkeys->dk_lookup != lookdict_split);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001279 assert(oldkeys->dk_refcnt == 1);
Victor Stinner49932fe2020-02-03 17:55:05 +01001280#ifdef Py_REF_DEBUG
1281 _Py_RefTotal--;
1282#endif
Victor Stinnerb4b53862020-05-05 19:55:29 +02001283#if PyDict_MAXFREELIST > 0
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001284 if (oldkeys->dk_size == PyDict_MINSIZE &&
Victor Stinner49932fe2020-02-03 17:55:05 +01001285 numfreekeys < PyDict_MAXFREELIST)
1286 {
INADA Naokia7576492018-11-14 18:39:27 +09001287 keys_free_list[numfreekeys++] = oldkeys;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001288 }
Victor Stinnerb4b53862020-05-05 19:55:29 +02001289 else
1290#endif
1291 {
INADA Naokia7576492018-11-14 18:39:27 +09001292 PyObject_FREE(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001293 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001294 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001295
1296 build_indices(mp->ma_keys, newentries, numentries);
1297 mp->ma_keys->dk_usable -= numentries;
1298 mp->ma_keys->dk_nentries = numentries;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001300}
1301
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001302/* Returns NULL if unable to split table.
1303 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001304static PyDictKeysObject *
1305make_keys_shared(PyObject *op)
1306{
1307 Py_ssize_t i;
1308 Py_ssize_t size;
1309 PyDictObject *mp = (PyDictObject *)op;
1310
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001311 if (!PyDict_CheckExact(op))
1312 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001313 if (!_PyDict_HasSplitTable(mp)) {
1314 PyDictKeyEntry *ep0;
1315 PyObject **values;
1316 assert(mp->ma_keys->dk_refcnt == 1);
1317 if (mp->ma_keys->dk_lookup == lookdict) {
1318 return NULL;
1319 }
1320 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1321 /* Remove dummy keys */
1322 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1323 return NULL;
1324 }
1325 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1326 /* Copy values into a new array */
Victor Stinner742da042016-09-07 17:40:12 -07001327 ep0 = DK_ENTRIES(mp->ma_keys);
1328 size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001329 values = new_values(size);
1330 if (values == NULL) {
1331 PyErr_SetString(PyExc_MemoryError,
1332 "Not enough memory to allocate new values array");
1333 return NULL;
1334 }
1335 for (i = 0; i < size; i++) {
1336 values[i] = ep0[i].me_value;
1337 ep0[i].me_value = NULL;
1338 }
1339 mp->ma_keys->dk_lookup = lookdict_split;
1340 mp->ma_values = values;
1341 }
INADA Naokia7576492018-11-14 18:39:27 +09001342 dictkeys_incref(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001343 return mp->ma_keys;
1344}
Christian Heimes99170a52007-12-19 02:07:34 +00001345
1346PyObject *
1347_PyDict_NewPresized(Py_ssize_t minused)
1348{
INADA Naoki92c50ee2016-11-22 00:57:02 +09001349 const Py_ssize_t max_presize = 128 * 1024;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001350 Py_ssize_t newsize;
1351 PyDictKeysObject *new_keys;
INADA Naoki92c50ee2016-11-22 00:57:02 +09001352
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001353 if (minused <= USABLE_FRACTION(PyDict_MINSIZE)) {
Inada Naokif2a18672019-03-12 17:25:44 +09001354 return PyDict_New();
1355 }
INADA Naoki92c50ee2016-11-22 00:57:02 +09001356 /* There are no strict guarantee that returned dict can contain minused
1357 * items without resize. So we create medium size dict instead of very
1358 * large dict or MemoryError.
1359 */
1360 if (minused > USABLE_FRACTION(max_presize)) {
1361 newsize = max_presize;
1362 }
1363 else {
1364 Py_ssize_t minsize = ESTIMATE_SIZE(minused);
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001365 newsize = PyDict_MINSIZE*2;
INADA Naoki92c50ee2016-11-22 00:57:02 +09001366 while (newsize < minsize) {
1367 newsize <<= 1;
1368 }
1369 }
1370 assert(IS_POWER_OF_2(newsize));
1371
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001372 new_keys = new_keys_object(newsize);
1373 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001375 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001376}
1377
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001378/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1379 * that may occur (originally dicts supported only string keys, and exceptions
1380 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001381 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001382 * (suppressed) occurred while computing the key's hash, or that some error
1383 * (suppressed) occurred when comparing keys in the dict's internal probe
1384 * sequence. A nasty example of the latter is when a Python-coded comparison
1385 * function hits a stack-depth error, which can cause this to return NULL
1386 * even if the key is present.
1387 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001388PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001389PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001390{
Victor Stinner59d3dce2020-06-02 14:03:25 +02001391 if (!PyDict_Check(op)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 return NULL;
Victor Stinner59d3dce2020-06-02 14:03:25 +02001393 }
1394 PyDictObject *mp = (PyDictObject *)op;
1395
1396 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001398 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 {
1400 hash = PyObject_Hash(key);
1401 if (hash == -1) {
1402 PyErr_Clear();
1403 return NULL;
1404 }
1405 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001406
Victor Stinner59d3dce2020-06-02 14:03:25 +02001407 PyThreadState *tstate = _PyThreadState_GET();
1408#ifdef Py_DEBUG
1409 // bpo-40839: Before Python 3.10, it was possible to call PyDict_GetItem()
1410 // with the GIL released.
1411 _Py_EnsureTstateNotNULL(tstate);
1412#endif
1413
1414 /* Preserve the existing exception */
1415 PyObject *exc_type, *exc_value, *exc_tb;
1416 PyObject *value;
1417 Py_ssize_t ix;
1418
1419 _PyErr_Fetch(tstate, &exc_type, &exc_value, &exc_tb);
1420 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
1421
1422 /* Ignore any exception raised by the lookup */
1423 _PyErr_Restore(tstate, exc_type, exc_value, exc_tb);
1424
1425 if (ix < 0) {
1426 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 }
INADA Naokiba609772016-12-07 20:41:42 +09001428 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001429}
1430
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001431/* Same as PyDict_GetItemWithError() but with hash supplied by caller.
1432 This returns NULL *with* an exception set if an exception occurred.
1433 It returns NULL *without* an exception set if the key wasn't present.
1434*/
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001435PyObject *
1436_PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1437{
Victor Stinner742da042016-09-07 17:40:12 -07001438 Py_ssize_t ix;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001439 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001440 PyObject *value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001441
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001442 if (!PyDict_Check(op)) {
1443 PyErr_BadInternalCall();
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001444 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001445 }
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001446
INADA Naoki778928b2017-08-03 23:45:15 +09001447 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001448 if (ix < 0) {
1449 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001450 }
INADA Naokiba609772016-12-07 20:41:42 +09001451 return value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001452}
1453
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001454/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1455 This returns NULL *with* an exception set if an exception occurred.
1456 It returns NULL *without* an exception set if the key wasn't present.
1457*/
1458PyObject *
1459PyDict_GetItemWithError(PyObject *op, PyObject *key)
1460{
Victor Stinner742da042016-09-07 17:40:12 -07001461 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001462 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 PyDictObject*mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001464 PyObject *value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 if (!PyDict_Check(op)) {
1467 PyErr_BadInternalCall();
1468 return NULL;
1469 }
1470 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001471 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 {
1473 hash = PyObject_Hash(key);
1474 if (hash == -1) {
1475 return NULL;
1476 }
1477 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001478
INADA Naoki778928b2017-08-03 23:45:15 +09001479 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001480 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001482 return value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001483}
1484
Brett Cannonfd074152012-04-14 14:10:13 -04001485PyObject *
1486_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1487{
1488 PyObject *kv;
1489 kv = _PyUnicode_FromId(key); /* borrowed */
1490 if (kv == NULL)
1491 return NULL;
scoder6067d4b2020-05-11 06:04:31 +02001492 Py_hash_t hash = ((PyASCIIObject *) kv)->hash;
1493 assert (hash != -1); /* interned strings have their hash value initialised */
1494 return _PyDict_GetItem_KnownHash(dp, kv, hash);
Brett Cannonfd074152012-04-14 14:10:13 -04001495}
1496
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001497PyObject *
1498_PyDict_GetItemStringWithError(PyObject *v, const char *key)
1499{
1500 PyObject *kv, *rv;
1501 kv = PyUnicode_FromString(key);
1502 if (kv == NULL) {
1503 return NULL;
1504 }
1505 rv = PyDict_GetItemWithError(v, kv);
1506 Py_DECREF(kv);
1507 return rv;
1508}
1509
Victor Stinnerb4efc962015-11-20 09:24:02 +01001510/* Fast version of global value lookup (LOAD_GLOBAL).
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001511 * Lookup in globals, then builtins.
Victor Stinnerb4efc962015-11-20 09:24:02 +01001512 *
1513 * Raise an exception and return NULL if an error occurred (ex: computing the
1514 * key hash failed, key comparison failed, ...). Return NULL if the key doesn't
1515 * exist. Return the value if the key exists.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001516 */
1517PyObject *
1518_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001519{
Victor Stinner742da042016-09-07 17:40:12 -07001520 Py_ssize_t ix;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001521 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09001522 PyObject *value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001523
1524 if (!PyUnicode_CheckExact(key) ||
1525 (hash = ((PyASCIIObject *) key)->hash) == -1)
1526 {
1527 hash = PyObject_Hash(key);
1528 if (hash == -1)
1529 return NULL;
Antoine Pitroue965d972012-02-27 00:45:12 +01001530 }
Victor Stinnerb4efc962015-11-20 09:24:02 +01001531
1532 /* namespace 1: globals */
INADA Naoki778928b2017-08-03 23:45:15 +09001533 ix = globals->ma_keys->dk_lookup(globals, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001534 if (ix == DKIX_ERROR)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001535 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001536 if (ix != DKIX_EMPTY && value != NULL)
1537 return value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001538
1539 /* namespace 2: builtins */
INADA Naoki778928b2017-08-03 23:45:15 +09001540 ix = builtins->ma_keys->dk_lookup(builtins, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001541 if (ix < 0)
Victor Stinnerb4efc962015-11-20 09:24:02 +01001542 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001543 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001544}
1545
Antoine Pitroue965d972012-02-27 00:45:12 +01001546/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1547 * dictionary if it's merely replacing the value for an existing key.
1548 * This means that it's safe to loop over a dictionary with PyDict_Next()
1549 * and occasionally replace a value -- but you can't insert new keys or
1550 * remove them.
1551 */
1552int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001553PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001554{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001555 PyDictObject *mp;
1556 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001557 if (!PyDict_Check(op)) {
1558 PyErr_BadInternalCall();
1559 return -1;
1560 }
1561 assert(key);
1562 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001563 mp = (PyDictObject *)op;
1564 if (!PyUnicode_CheckExact(key) ||
1565 (hash = ((PyASCIIObject *) key)->hash) == -1)
1566 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001567 hash = PyObject_Hash(key);
1568 if (hash == -1)
1569 return -1;
1570 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001571
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001572 if (mp->ma_keys == Py_EMPTY_KEYS) {
1573 return insert_to_emptydict(mp, key, hash, value);
1574 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001575 /* insertdict() handles any resizing that might be necessary */
1576 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001577}
1578
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001579int
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001580_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1581 Py_hash_t hash)
1582{
1583 PyDictObject *mp;
1584
1585 if (!PyDict_Check(op)) {
1586 PyErr_BadInternalCall();
1587 return -1;
1588 }
1589 assert(key);
1590 assert(value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001591 assert(hash != -1);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001592 mp = (PyDictObject *)op;
1593
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001594 if (mp->ma_keys == Py_EMPTY_KEYS) {
1595 return insert_to_emptydict(mp, key, hash, value);
1596 }
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001597 /* insertdict() handles any resizing that might be necessary */
1598 return insertdict(mp, key, hash, value);
1599}
1600
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001601static int
INADA Naoki778928b2017-08-03 23:45:15 +09001602delitem_common(PyDictObject *mp, Py_hash_t hash, Py_ssize_t ix,
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001603 PyObject *old_value)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001604{
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001605 PyObject *old_key;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001606 PyDictKeyEntry *ep;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001607
INADA Naoki778928b2017-08-03 23:45:15 +09001608 Py_ssize_t hashpos = lookdict_index(mp->ma_keys, hash, ix);
1609 assert(hashpos >= 0);
1610
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001611 mp->ma_used--;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001612 mp->ma_version_tag = DICT_NEXT_VERSION();
1613 ep = &DK_ENTRIES(mp->ma_keys)[ix];
INADA Naokia7576492018-11-14 18:39:27 +09001614 dictkeys_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001615 ENSURE_ALLOWS_DELETIONS(mp);
1616 old_key = ep->me_key;
1617 ep->me_key = NULL;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001618 ep->me_value = NULL;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001619 Py_DECREF(old_key);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001620 Py_DECREF(old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001621
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001622 ASSERT_CONSISTENT(mp);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001623 return 0;
1624}
1625
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001626int
Tim Peters1f5871e2000-07-04 17:44:48 +00001627PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001628{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001629 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001630 assert(key);
1631 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001632 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 hash = PyObject_Hash(key);
1634 if (hash == -1)
1635 return -1;
1636 }
Victor Stinner742da042016-09-07 17:40:12 -07001637
1638 return _PyDict_DelItem_KnownHash(op, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001639}
1640
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001641int
1642_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1643{
INADA Naoki778928b2017-08-03 23:45:15 +09001644 Py_ssize_t ix;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001645 PyDictObject *mp;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001646 PyObject *old_value;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001647
1648 if (!PyDict_Check(op)) {
1649 PyErr_BadInternalCall();
1650 return -1;
1651 }
1652 assert(key);
1653 assert(hash != -1);
1654 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001655 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001656 if (ix == DKIX_ERROR)
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001657 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09001658 if (ix == DKIX_EMPTY || old_value == NULL) {
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001659 _PyErr_SetKeyError(key);
1660 return -1;
1661 }
Victor Stinner78601a32016-09-09 19:28:36 -07001662
1663 // Split table doesn't allow deletion. Combine it.
1664 if (_PyDict_HasSplitTable(mp)) {
1665 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1666 return -1;
1667 }
INADA Naoki778928b2017-08-03 23:45:15 +09001668 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001669 assert(ix >= 0);
1670 }
1671
INADA Naoki778928b2017-08-03 23:45:15 +09001672 return delitem_common(mp, hash, ix, old_value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001673}
1674
Antoine Pitroud741ed42016-12-27 14:23:43 +01001675/* This function promises that the predicate -> deletion sequence is atomic
1676 * (i.e. protected by the GIL), assuming the predicate itself doesn't
1677 * release the GIL.
1678 */
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001679int
1680_PyDict_DelItemIf(PyObject *op, PyObject *key,
1681 int (*predicate)(PyObject *value))
1682{
Antoine Pitroud741ed42016-12-27 14:23:43 +01001683 Py_ssize_t hashpos, ix;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001684 PyDictObject *mp;
1685 Py_hash_t hash;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001686 PyObject *old_value;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001687 int res;
1688
1689 if (!PyDict_Check(op)) {
1690 PyErr_BadInternalCall();
1691 return -1;
1692 }
1693 assert(key);
1694 hash = PyObject_Hash(key);
1695 if (hash == -1)
1696 return -1;
1697 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001698 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001699 if (ix == DKIX_ERROR)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001700 return -1;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001701 if (ix == DKIX_EMPTY || old_value == NULL) {
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001702 _PyErr_SetKeyError(key);
1703 return -1;
1704 }
Antoine Pitroud741ed42016-12-27 14:23:43 +01001705
1706 // Split table doesn't allow deletion. Combine it.
1707 if (_PyDict_HasSplitTable(mp)) {
1708 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1709 return -1;
1710 }
INADA Naoki778928b2017-08-03 23:45:15 +09001711 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001712 assert(ix >= 0);
1713 }
1714
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001715 res = predicate(old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001716 if (res == -1)
1717 return -1;
INADA Naoki778928b2017-08-03 23:45:15 +09001718
1719 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1720 assert(hashpos >= 0);
1721
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001722 if (res > 0)
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001723 return delitem_common(mp, hashpos, ix, old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001724 else
1725 return 0;
1726}
1727
1728
Guido van Rossum25831651993-05-19 14:50:45 +00001729void
Tim Peters1f5871e2000-07-04 17:44:48 +00001730PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001731{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001733 PyDictKeysObject *oldkeys;
1734 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 if (!PyDict_Check(op))
1738 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001739 mp = ((PyDictObject *)op);
1740 oldkeys = mp->ma_keys;
1741 oldvalues = mp->ma_values;
1742 if (oldvalues == empty_values)
1743 return;
1744 /* Empty the dict... */
INADA Naokia7576492018-11-14 18:39:27 +09001745 dictkeys_incref(Py_EMPTY_KEYS);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001746 mp->ma_keys = Py_EMPTY_KEYS;
1747 mp->ma_values = empty_values;
1748 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001749 mp->ma_version_tag = DICT_NEXT_VERSION();
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001750 /* ...then clear the keys and values */
1751 if (oldvalues != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001752 n = oldkeys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001753 for (i = 0; i < n; i++)
1754 Py_CLEAR(oldvalues[i]);
1755 free_values(oldvalues);
INADA Naokia7576492018-11-14 18:39:27 +09001756 dictkeys_decref(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001757 }
1758 else {
1759 assert(oldkeys->dk_refcnt == 1);
INADA Naokia7576492018-11-14 18:39:27 +09001760 dictkeys_decref(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001761 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001762 ASSERT_CONSISTENT(mp);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001763}
1764
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001765/* Internal version of PyDict_Next that returns a hash value in addition
1766 * to the key and value.
1767 * Return 1 on success, return 0 when the reached the end of the dictionary
1768 * (or if op is not a dictionary)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001769 */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001770int
1771_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1772 PyObject **pvalue, Py_hash_t *phash)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001773{
INADA Naokica2d8be2016-11-04 16:59:10 +09001774 Py_ssize_t i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001775 PyDictObject *mp;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001776 PyDictKeyEntry *entry_ptr;
1777 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001778
1779 if (!PyDict_Check(op))
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001780 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 mp = (PyDictObject *)op;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001782 i = *ppos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001783 if (mp->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09001784 if (i < 0 || i >= mp->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001785 return 0;
INADA Naokica2d8be2016-11-04 16:59:10 +09001786 /* values of split table is always dense */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001787 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
INADA Naokica2d8be2016-11-04 16:59:10 +09001788 value = mp->ma_values[i];
1789 assert(value != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001791 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09001792 Py_ssize_t n = mp->ma_keys->dk_nentries;
1793 if (i < 0 || i >= n)
1794 return 0;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001795 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
1796 while (i < n && entry_ptr->me_value == NULL) {
1797 entry_ptr++;
1798 i++;
Victor Stinner742da042016-09-07 17:40:12 -07001799 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001800 if (i >= n)
1801 return 0;
1802 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001803 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001804 *ppos = i+1;
1805 if (pkey)
1806 *pkey = entry_ptr->me_key;
1807 if (phash)
1808 *phash = entry_ptr->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001809 if (pvalue)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001810 *pvalue = value;
1811 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001812}
1813
Tim Peters080c88b2003-02-15 03:01:11 +00001814/*
1815 * Iterate over a dict. Use like so:
1816 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001817 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001818 * PyObject *key, *value;
1819 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001820 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001821 * Refer to borrowed references in key and value.
Tim Peters080c88b2003-02-15 03:01:11 +00001822 * }
1823 *
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001824 * Return 1 on success, return 0 when the reached the end of the dictionary
1825 * (or if op is not a dictionary)
1826 *
Tim Peters080c88b2003-02-15 03:01:11 +00001827 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001828 * mutates the dict. One exception: it is safe if the loop merely changes
1829 * the values associated with the keys (but doesn't insert new keys or
1830 * delete keys), via PyDict_SetItem().
1831 */
Guido van Rossum25831651993-05-19 14:50:45 +00001832int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001833PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001834{
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001835 return _PyDict_Next(op, ppos, pkey, pvalue, NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001836}
1837
Eric Snow96c6af92015-05-29 22:21:39 -06001838/* Internal version of dict.pop(). */
1839PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001840_PyDict_Pop_KnownHash(PyObject *dict, PyObject *key, Py_hash_t hash, PyObject *deflt)
Eric Snow96c6af92015-05-29 22:21:39 -06001841{
Victor Stinner742da042016-09-07 17:40:12 -07001842 Py_ssize_t ix, hashpos;
Eric Snow96c6af92015-05-29 22:21:39 -06001843 PyObject *old_value, *old_key;
1844 PyDictKeyEntry *ep;
Yury Selivanov684ef2c2016-10-28 19:01:21 -04001845 PyDictObject *mp;
1846
1847 assert(PyDict_Check(dict));
1848 mp = (PyDictObject *)dict;
Eric Snow96c6af92015-05-29 22:21:39 -06001849
1850 if (mp->ma_used == 0) {
1851 if (deflt) {
1852 Py_INCREF(deflt);
1853 return deflt;
1854 }
1855 _PyErr_SetKeyError(key);
1856 return NULL;
1857 }
INADA Naoki778928b2017-08-03 23:45:15 +09001858 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001859 if (ix == DKIX_ERROR)
Eric Snow96c6af92015-05-29 22:21:39 -06001860 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001861 if (ix == DKIX_EMPTY || old_value == NULL) {
Eric Snow96c6af92015-05-29 22:21:39 -06001862 if (deflt) {
1863 Py_INCREF(deflt);
1864 return deflt;
1865 }
1866 _PyErr_SetKeyError(key);
1867 return NULL;
1868 }
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001869
Victor Stinner78601a32016-09-09 19:28:36 -07001870 // Split table doesn't allow deletion. Combine it.
1871 if (_PyDict_HasSplitTable(mp)) {
1872 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1873 return NULL;
1874 }
INADA Naoki778928b2017-08-03 23:45:15 +09001875 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001876 assert(ix >= 0);
1877 }
1878
INADA Naoki778928b2017-08-03 23:45:15 +09001879 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1880 assert(hashpos >= 0);
Victor Stinner78601a32016-09-09 19:28:36 -07001881 assert(old_value != NULL);
Eric Snow96c6af92015-05-29 22:21:39 -06001882 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001883 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokia7576492018-11-14 18:39:27 +09001884 dictkeys_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
Victor Stinner78601a32016-09-09 19:28:36 -07001885 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1886 ENSURE_ALLOWS_DELETIONS(mp);
1887 old_key = ep->me_key;
1888 ep->me_key = NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001889 ep->me_value = NULL;
Victor Stinner78601a32016-09-09 19:28:36 -07001890 Py_DECREF(old_key);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001891
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001892 ASSERT_CONSISTENT(mp);
Eric Snow96c6af92015-05-29 22:21:39 -06001893 return old_value;
1894}
1895
Serhiy Storchaka67796522017-01-12 18:34:33 +02001896PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001897_PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt)
Serhiy Storchaka67796522017-01-12 18:34:33 +02001898{
1899 Py_hash_t hash;
1900
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001901 if (((PyDictObject *)dict)->ma_used == 0) {
Serhiy Storchaka67796522017-01-12 18:34:33 +02001902 if (deflt) {
1903 Py_INCREF(deflt);
1904 return deflt;
1905 }
1906 _PyErr_SetKeyError(key);
1907 return NULL;
1908 }
1909 if (!PyUnicode_CheckExact(key) ||
1910 (hash = ((PyASCIIObject *) key)->hash) == -1) {
1911 hash = PyObject_Hash(key);
1912 if (hash == -1)
1913 return NULL;
1914 }
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001915 return _PyDict_Pop_KnownHash(dict, key, hash, deflt);
Serhiy Storchaka67796522017-01-12 18:34:33 +02001916}
1917
Eric Snow96c6af92015-05-29 22:21:39 -06001918/* Internal version of dict.from_keys(). It is subclass-friendly. */
1919PyObject *
1920_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1921{
1922 PyObject *it; /* iter(iterable) */
1923 PyObject *key;
1924 PyObject *d;
1925 int status;
1926
Victor Stinnera5ed5f02016-12-06 18:45:50 +01001927 d = _PyObject_CallNoArg(cls);
Eric Snow96c6af92015-05-29 22:21:39 -06001928 if (d == NULL)
1929 return NULL;
1930
1931 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
1932 if (PyDict_CheckExact(iterable)) {
1933 PyDictObject *mp = (PyDictObject *)d;
1934 PyObject *oldvalue;
1935 Py_ssize_t pos = 0;
1936 PyObject *key;
1937 Py_hash_t hash;
1938
Serhiy Storchakac61ac162017-03-21 08:52:38 +02001939 if (dictresize(mp, ESTIMATE_SIZE(PyDict_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001940 Py_DECREF(d);
1941 return NULL;
1942 }
1943
1944 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
1945 if (insertdict(mp, key, hash, value)) {
1946 Py_DECREF(d);
1947 return NULL;
1948 }
1949 }
1950 return d;
1951 }
1952 if (PyAnySet_CheckExact(iterable)) {
1953 PyDictObject *mp = (PyDictObject *)d;
1954 Py_ssize_t pos = 0;
1955 PyObject *key;
1956 Py_hash_t hash;
1957
Victor Stinner742da042016-09-07 17:40:12 -07001958 if (dictresize(mp, ESTIMATE_SIZE(PySet_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001959 Py_DECREF(d);
1960 return NULL;
1961 }
1962
1963 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
1964 if (insertdict(mp, key, hash, value)) {
1965 Py_DECREF(d);
1966 return NULL;
1967 }
1968 }
1969 return d;
1970 }
1971 }
1972
1973 it = PyObject_GetIter(iterable);
1974 if (it == NULL){
1975 Py_DECREF(d);
1976 return NULL;
1977 }
1978
1979 if (PyDict_CheckExact(d)) {
1980 while ((key = PyIter_Next(it)) != NULL) {
1981 status = PyDict_SetItem(d, key, value);
1982 Py_DECREF(key);
1983 if (status < 0)
1984 goto Fail;
1985 }
1986 } else {
1987 while ((key = PyIter_Next(it)) != NULL) {
1988 status = PyObject_SetItem(d, key, value);
1989 Py_DECREF(key);
1990 if (status < 0)
1991 goto Fail;
1992 }
1993 }
1994
1995 if (PyErr_Occurred())
1996 goto Fail;
1997 Py_DECREF(it);
1998 return d;
1999
2000Fail:
2001 Py_DECREF(it);
2002 Py_DECREF(d);
2003 return NULL;
2004}
2005
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002006/* Methods */
2007
2008static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002009dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002010{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002011 PyObject **values = mp->ma_values;
2012 PyDictKeysObject *keys = mp->ma_keys;
2013 Py_ssize_t i, n;
INADA Naokia6296d32017-08-24 14:55:17 +09002014
2015 /* bpo-31095: UnTrack is needed before calling any callbacks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016 PyObject_GC_UnTrack(mp);
Jeroen Demeyer351c6742019-05-10 19:21:11 +02002017 Py_TRASHCAN_BEGIN(mp, dict_dealloc)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002018 if (values != NULL) {
2019 if (values != empty_values) {
Victor Stinner742da042016-09-07 17:40:12 -07002020 for (i = 0, n = mp->ma_keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002021 Py_XDECREF(values[i]);
2022 }
2023 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002024 }
INADA Naokia7576492018-11-14 18:39:27 +09002025 dictkeys_decref(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002026 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002027 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02002028 assert(keys->dk_refcnt == 1);
INADA Naokia7576492018-11-14 18:39:27 +09002029 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002030 }
Victor Stinnerb4b53862020-05-05 19:55:29 +02002031#if PyDict_MAXFREELIST > 0
2032 if (numfree < PyDict_MAXFREELIST && Py_IS_TYPE(mp, &PyDict_Type)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002033 free_list[numfree++] = mp;
Victor Stinnerb4b53862020-05-05 19:55:29 +02002034 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002035 else
Victor Stinnerb4b53862020-05-05 19:55:29 +02002036#endif
2037 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002038 Py_TYPE(mp)->tp_free((PyObject *)mp);
Victor Stinnerb4b53862020-05-05 19:55:29 +02002039 }
Jeroen Demeyer351c6742019-05-10 19:21:11 +02002040 Py_TRASHCAN_END
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002041}
2042
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002043
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002044static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002045dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002046{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002047 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01002048 PyObject *key = NULL, *value = NULL;
2049 _PyUnicodeWriter writer;
2050 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00002051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002052 i = Py_ReprEnter((PyObject *)mp);
2053 if (i != 0) {
2054 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
2055 }
Guido van Rossum255443b1998-04-10 22:47:14 +00002056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002057 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01002058 Py_ReprLeave((PyObject *)mp);
2059 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002060 }
Tim Petersa7259592001-06-16 05:11:17 +00002061
Victor Stinnerf91929b2013-11-19 13:07:38 +01002062 _PyUnicodeWriter_Init(&writer);
2063 writer.overallocate = 1;
2064 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
2065 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00002066
Victor Stinnerf91929b2013-11-19 13:07:38 +01002067 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
2068 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00002069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002070 /* Do repr() on each key+value pair, and insert ": " between them.
2071 Note that repr may mutate the dict. */
2072 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01002073 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002074 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01002075 PyObject *s;
2076 int res;
2077
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002078 /* Prevent repr from deleting key or value during key format. */
2079 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002080 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02002081
Victor Stinnerf91929b2013-11-19 13:07:38 +01002082 if (!first) {
2083 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
2084 goto error;
2085 }
2086 first = 0;
2087
2088 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002089 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01002090 goto error;
2091 res = _PyUnicodeWriter_WriteStr(&writer, s);
2092 Py_DECREF(s);
2093 if (res < 0)
2094 goto error;
2095
2096 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
2097 goto error;
2098
2099 s = PyObject_Repr(value);
2100 if (s == NULL)
2101 goto error;
2102 res = _PyUnicodeWriter_WriteStr(&writer, s);
2103 Py_DECREF(s);
2104 if (res < 0)
2105 goto error;
2106
2107 Py_CLEAR(key);
2108 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002109 }
Tim Petersa7259592001-06-16 05:11:17 +00002110
Victor Stinnerf91929b2013-11-19 13:07:38 +01002111 writer.overallocate = 0;
2112 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
2113 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00002114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002115 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01002116
2117 return _PyUnicodeWriter_Finish(&writer);
2118
2119error:
2120 Py_ReprLeave((PyObject *)mp);
2121 _PyUnicodeWriter_Dealloc(&writer);
2122 Py_XDECREF(key);
2123 Py_XDECREF(value);
2124 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002125}
2126
Martin v. Löwis18e16552006-02-15 17:27:45 +00002127static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002128dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002129{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002130 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002131}
2132
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002133static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002134dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002135{
Victor Stinner742da042016-09-07 17:40:12 -07002136 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002137 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09002138 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002140 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002141 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002142 hash = PyObject_Hash(key);
2143 if (hash == -1)
2144 return NULL;
2145 }
INADA Naoki778928b2017-08-03 23:45:15 +09002146 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002147 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002148 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002149 if (ix == DKIX_EMPTY || value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002150 if (!PyDict_CheckExact(mp)) {
2151 /* Look up __missing__ method if we're a subclass. */
2152 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05002153 _Py_IDENTIFIER(__missing__);
2154 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002155 if (missing != NULL) {
Petr Viktorinffd97532020-02-11 17:46:57 +01002156 res = PyObject_CallOneArg(missing, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002157 Py_DECREF(missing);
2158 return res;
2159 }
2160 else if (PyErr_Occurred())
2161 return NULL;
2162 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07002163 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002164 return NULL;
2165 }
INADA Naokiba609772016-12-07 20:41:42 +09002166 Py_INCREF(value);
2167 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002168}
2169
2170static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002171dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002172{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002173 if (w == NULL)
2174 return PyDict_DelItem((PyObject *)mp, v);
2175 else
2176 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002177}
2178
Guido van Rossuma9e7a811997-05-13 21:02:11 +00002179static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002180 (lenfunc)dict_length, /*mp_length*/
2181 (binaryfunc)dict_subscript, /*mp_subscript*/
2182 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002183};
2184
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002185static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002186dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002187{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002188 PyObject *v;
2189 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002190 PyDictKeyEntry *ep;
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002191 Py_ssize_t n, offset;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002192 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002193
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002194 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 n = mp->ma_used;
2196 v = PyList_New(n);
2197 if (v == NULL)
2198 return NULL;
2199 if (n != mp->ma_used) {
2200 /* Durnit. The allocations caused the dict to resize.
2201 * Just start over, this shouldn't normally happen.
2202 */
2203 Py_DECREF(v);
2204 goto again;
2205 }
Victor Stinner742da042016-09-07 17:40:12 -07002206 ep = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002207 if (mp->ma_values) {
2208 value_ptr = mp->ma_values;
2209 offset = sizeof(PyObject *);
2210 }
2211 else {
2212 value_ptr = &ep[0].me_value;
2213 offset = sizeof(PyDictKeyEntry);
2214 }
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002215 for (i = 0, j = 0; j < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002216 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002217 PyObject *key = ep[i].me_key;
2218 Py_INCREF(key);
2219 PyList_SET_ITEM(v, j, key);
2220 j++;
2221 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002222 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002223 }
2224 assert(j == n);
2225 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002226}
2227
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002228static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002229dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002230{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002231 PyObject *v;
2232 Py_ssize_t i, j;
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002233 PyDictKeyEntry *ep;
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002234 Py_ssize_t n, offset;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002235 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002236
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002237 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002238 n = mp->ma_used;
2239 v = PyList_New(n);
2240 if (v == NULL)
2241 return NULL;
2242 if (n != mp->ma_used) {
2243 /* Durnit. The allocations caused the dict to resize.
2244 * Just start over, this shouldn't normally happen.
2245 */
2246 Py_DECREF(v);
2247 goto again;
2248 }
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002249 ep = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002250 if (mp->ma_values) {
2251 value_ptr = mp->ma_values;
2252 offset = sizeof(PyObject *);
2253 }
2254 else {
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002255 value_ptr = &ep[0].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002256 offset = sizeof(PyDictKeyEntry);
2257 }
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002258 for (i = 0, j = 0; j < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002259 PyObject *value = *value_ptr;
2260 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2261 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262 Py_INCREF(value);
2263 PyList_SET_ITEM(v, j, value);
2264 j++;
2265 }
2266 }
2267 assert(j == n);
2268 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002269}
2270
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002271static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002272dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002273{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002274 PyObject *v;
2275 Py_ssize_t i, j, n;
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002276 Py_ssize_t offset;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002277 PyObject *item, *key;
2278 PyDictKeyEntry *ep;
2279 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 /* Preallocate the list of tuples, to avoid allocations during
2282 * the loop over the items, which could trigger GC, which
2283 * could resize the dict. :-(
2284 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002285 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002286 n = mp->ma_used;
2287 v = PyList_New(n);
2288 if (v == NULL)
2289 return NULL;
2290 for (i = 0; i < n; i++) {
2291 item = PyTuple_New(2);
2292 if (item == NULL) {
2293 Py_DECREF(v);
2294 return NULL;
2295 }
2296 PyList_SET_ITEM(v, i, item);
2297 }
2298 if (n != mp->ma_used) {
2299 /* Durnit. The allocations caused the dict to resize.
2300 * Just start over, this shouldn't normally happen.
2301 */
2302 Py_DECREF(v);
2303 goto again;
2304 }
2305 /* Nothing we do below makes any function calls. */
Victor Stinner742da042016-09-07 17:40:12 -07002306 ep = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002307 if (mp->ma_values) {
2308 value_ptr = mp->ma_values;
2309 offset = sizeof(PyObject *);
2310 }
2311 else {
2312 value_ptr = &ep[0].me_value;
2313 offset = sizeof(PyDictKeyEntry);
2314 }
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002315 for (i = 0, j = 0; j < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002316 PyObject *value = *value_ptr;
2317 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2318 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002319 key = ep[i].me_key;
2320 item = PyList_GET_ITEM(v, j);
2321 Py_INCREF(key);
2322 PyTuple_SET_ITEM(item, 0, key);
2323 Py_INCREF(value);
2324 PyTuple_SET_ITEM(item, 1, value);
2325 j++;
2326 }
2327 }
2328 assert(j == n);
2329 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002330}
2331
Larry Hastings5c661892014-01-24 06:17:25 -08002332/*[clinic input]
2333@classmethod
2334dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08002335 iterable: object
2336 value: object=None
2337 /
2338
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002339Create a new dictionary with keys from iterable and values set to value.
Larry Hastings5c661892014-01-24 06:17:25 -08002340[clinic start generated code]*/
2341
Larry Hastings5c661892014-01-24 06:17:25 -08002342static PyObject *
2343dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002344/*[clinic end generated code: output=8fb98e4b10384999 input=382ba4855d0f74c3]*/
Larry Hastings5c661892014-01-24 06:17:25 -08002345{
Eric Snow96c6af92015-05-29 22:21:39 -06002346 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002347}
2348
Brandt Buchereb8ac572020-02-24 19:47:34 -08002349/* Single-arg dict update; used by dict_update_common and operators. */
2350static int
2351dict_update_arg(PyObject *self, PyObject *arg)
2352{
2353 if (PyDict_CheckExact(arg)) {
2354 return PyDict_Merge(self, arg, 1);
2355 }
2356 _Py_IDENTIFIER(keys);
2357 PyObject *func;
2358 if (_PyObject_LookupAttrId(arg, &PyId_keys, &func) < 0) {
2359 return -1;
2360 }
2361 if (func != NULL) {
2362 Py_DECREF(func);
2363 return PyDict_Merge(self, arg, 1);
2364 }
2365 return PyDict_MergeFromSeq2(self, arg, 1);
2366}
2367
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002368static int
Victor Stinner742da042016-09-07 17:40:12 -07002369dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
2370 const char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002371{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002372 PyObject *arg = NULL;
2373 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002374
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002375 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 result = -1;
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002377 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002378 else if (arg != NULL) {
Brandt Buchereb8ac572020-02-24 19:47:34 -08002379 result = dict_update_arg(self, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380 }
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002382 if (result == 0 && kwds != NULL) {
2383 if (PyArg_ValidateKeywordArguments(kwds))
2384 result = PyDict_Merge(self, kwds, 1);
2385 else
2386 result = -1;
2387 }
2388 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002389}
2390
Victor Stinner91f0d4a2017-01-19 12:45:06 +01002391/* Note: dict.update() uses the METH_VARARGS|METH_KEYWORDS calling convention.
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002392 Using METH_FASTCALL|METH_KEYWORDS would make dict.update(**dict2) calls
2393 slower, see the issue #29312. */
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002394static PyObject *
2395dict_update(PyObject *self, PyObject *args, PyObject *kwds)
2396{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002397 if (dict_update_common(self, args, kwds, "update") != -1)
2398 Py_RETURN_NONE;
2399 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002400}
2401
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002402/* Update unconditionally replaces existing items.
2403 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00002404 otherwise it leaves existing items unchanged.
2405
2406 PyDict_{Update,Merge} update/merge from a mapping object.
2407
Tim Petersf582b822001-12-11 18:51:08 +00002408 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00002409 producing iterable objects of length 2.
2410*/
2411
Tim Petersf582b822001-12-11 18:51:08 +00002412int
Tim Peters1fc240e2001-10-26 05:06:50 +00002413PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
2414{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002415 PyObject *it; /* iter(seq2) */
2416 Py_ssize_t i; /* index into seq2 of current element */
2417 PyObject *item; /* seq2[i] */
2418 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00002419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002420 assert(d != NULL);
2421 assert(PyDict_Check(d));
2422 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00002423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002424 it = PyObject_GetIter(seq2);
2425 if (it == NULL)
2426 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002428 for (i = 0; ; ++i) {
2429 PyObject *key, *value;
2430 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00002431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 fast = NULL;
2433 item = PyIter_Next(it);
2434 if (item == NULL) {
2435 if (PyErr_Occurred())
2436 goto Fail;
2437 break;
2438 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002440 /* Convert item to sequence, and verify length 2. */
2441 fast = PySequence_Fast(item, "");
2442 if (fast == NULL) {
2443 if (PyErr_ExceptionMatches(PyExc_TypeError))
2444 PyErr_Format(PyExc_TypeError,
2445 "cannot convert dictionary update "
2446 "sequence element #%zd to a sequence",
2447 i);
2448 goto Fail;
2449 }
2450 n = PySequence_Fast_GET_SIZE(fast);
2451 if (n != 2) {
2452 PyErr_Format(PyExc_ValueError,
2453 "dictionary update sequence element #%zd "
2454 "has length %zd; 2 is required",
2455 i, n);
2456 goto Fail;
2457 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002459 /* Update/merge with this (key, value) pair. */
2460 key = PySequence_Fast_GET_ITEM(fast, 0);
2461 value = PySequence_Fast_GET_ITEM(fast, 1);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002462 Py_INCREF(key);
2463 Py_INCREF(value);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002464 if (override) {
2465 if (PyDict_SetItem(d, key, value) < 0) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002466 Py_DECREF(key);
2467 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002468 goto Fail;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002469 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002470 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002471 else if (PyDict_GetItemWithError(d, key) == NULL) {
2472 if (PyErr_Occurred() || PyDict_SetItem(d, key, value) < 0) {
2473 Py_DECREF(key);
2474 Py_DECREF(value);
2475 goto Fail;
2476 }
2477 }
2478
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002479 Py_DECREF(key);
2480 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002481 Py_DECREF(fast);
2482 Py_DECREF(item);
2483 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002485 i = 0;
Victor Stinner0fc91ee2019-04-12 21:51:34 +02002486 ASSERT_CONSISTENT(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002487 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002488Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002489 Py_XDECREF(item);
2490 Py_XDECREF(fast);
2491 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002492Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002493 Py_DECREF(it);
2494 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002495}
2496
doko@ubuntu.comc96df682016-10-11 08:04:02 +02002497static int
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002498dict_merge(PyObject *a, PyObject *b, int override)
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002499{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002500 PyDictObject *mp, *other;
2501 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -07002502 PyDictKeyEntry *entry, *ep0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002503
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002504 assert(0 <= override && override <= 2);
2505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002506 /* We accept for the argument either a concrete dictionary object,
2507 * or an abstract "mapping" object. For the former, we can do
2508 * things quite efficiently. For the latter, we only require that
2509 * PyMapping_Keys() and PyObject_GetItem() be supported.
2510 */
2511 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2512 PyErr_BadInternalCall();
2513 return -1;
2514 }
2515 mp = (PyDictObject*)a;
INADA Naoki2aaf98c2018-09-26 12:59:00 +09002516 if (PyDict_Check(b) && (Py_TYPE(b)->tp_iter == (getiterfunc)dict_iter)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002517 other = (PyDictObject*)b;
2518 if (other == mp || other->ma_used == 0)
2519 /* a.update(a) or a.update({}); nothing to do */
2520 return 0;
2521 if (mp->ma_used == 0)
2522 /* Since the target dict is empty, PyDict_GetItem()
2523 * always returns NULL. Setting override to 1
2524 * skips the unnecessary test.
2525 */
2526 override = 1;
2527 /* Do one big resize at the start, rather than
2528 * incrementally resizing as we insert new items. Expect
2529 * that there will be no (or few) overlapping keys.
2530 */
INADA Naokib1152be2016-10-27 19:26:50 +09002531 if (USABLE_FRACTION(mp->ma_keys->dk_size) < other->ma_used) {
2532 if (dictresize(mp, ESTIMATE_SIZE(mp->ma_used + other->ma_used))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002533 return -1;
INADA Naokib1152be2016-10-27 19:26:50 +09002534 }
2535 }
Victor Stinner742da042016-09-07 17:40:12 -07002536 ep0 = DK_ENTRIES(other->ma_keys);
2537 for (i = 0, n = other->ma_keys->dk_nentries; i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002538 PyObject *key, *value;
2539 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002540 entry = &ep0[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002541 key = entry->me_key;
2542 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002543 if (other->ma_values)
2544 value = other->ma_values[i];
2545 else
2546 value = entry->me_value;
2547
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002548 if (value != NULL) {
2549 int err = 0;
2550 Py_INCREF(key);
2551 Py_INCREF(value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002552 if (override == 1)
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002553 err = insertdict(mp, key, hash, value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002554 else if (_PyDict_GetItem_KnownHash(a, key, hash) == NULL) {
2555 if (PyErr_Occurred()) {
2556 Py_DECREF(value);
2557 Py_DECREF(key);
2558 return -1;
2559 }
2560 err = insertdict(mp, key, hash, value);
2561 }
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002562 else if (override != 0) {
2563 _PyErr_SetKeyError(key);
2564 Py_DECREF(value);
2565 Py_DECREF(key);
2566 return -1;
2567 }
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002568 Py_DECREF(value);
2569 Py_DECREF(key);
2570 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002571 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002572
Victor Stinner742da042016-09-07 17:40:12 -07002573 if (n != other->ma_keys->dk_nentries) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002574 PyErr_SetString(PyExc_RuntimeError,
2575 "dict mutated during update");
2576 return -1;
2577 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002578 }
2579 }
2580 }
2581 else {
2582 /* Do it the generic, slower way */
2583 PyObject *keys = PyMapping_Keys(b);
2584 PyObject *iter;
2585 PyObject *key, *value;
2586 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002588 if (keys == NULL)
2589 /* Docstring says this is equivalent to E.keys() so
2590 * if E doesn't have a .keys() method we want
2591 * AttributeError to percolate up. Might as well
2592 * do the same for any other error.
2593 */
2594 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002596 iter = PyObject_GetIter(keys);
2597 Py_DECREF(keys);
2598 if (iter == NULL)
2599 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002601 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002602 if (override != 1) {
2603 if (PyDict_GetItemWithError(a, key) != NULL) {
2604 if (override != 0) {
2605 _PyErr_SetKeyError(key);
2606 Py_DECREF(key);
2607 Py_DECREF(iter);
2608 return -1;
2609 }
2610 Py_DECREF(key);
2611 continue;
2612 }
2613 else if (PyErr_Occurred()) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002614 Py_DECREF(key);
2615 Py_DECREF(iter);
2616 return -1;
2617 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002618 }
2619 value = PyObject_GetItem(b, key);
2620 if (value == NULL) {
2621 Py_DECREF(iter);
2622 Py_DECREF(key);
2623 return -1;
2624 }
2625 status = PyDict_SetItem(a, key, value);
2626 Py_DECREF(key);
2627 Py_DECREF(value);
2628 if (status < 0) {
2629 Py_DECREF(iter);
2630 return -1;
2631 }
2632 }
2633 Py_DECREF(iter);
2634 if (PyErr_Occurred())
2635 /* Iterator completed, via error */
2636 return -1;
2637 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +02002638 ASSERT_CONSISTENT(a);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002639 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002640}
2641
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002642int
2643PyDict_Update(PyObject *a, PyObject *b)
2644{
2645 return dict_merge(a, b, 1);
2646}
2647
2648int
2649PyDict_Merge(PyObject *a, PyObject *b, int override)
2650{
2651 /* XXX Deprecate override not in (0, 1). */
2652 return dict_merge(a, b, override != 0);
2653}
2654
2655int
2656_PyDict_MergeEx(PyObject *a, PyObject *b, int override)
2657{
2658 return dict_merge(a, b, override);
2659}
2660
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002661static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302662dict_copy(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002663{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002664 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002665}
2666
2667PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002668PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002669{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002670 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002671 PyDictObject *mp;
2672 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002674 if (o == NULL || !PyDict_Check(o)) {
2675 PyErr_BadInternalCall();
2676 return NULL;
2677 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002678
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002679 mp = (PyDictObject *)o;
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002680 if (mp->ma_used == 0) {
2681 /* The dict is empty; just return a new dict. */
2682 return PyDict_New();
2683 }
2684
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002685 if (_PyDict_HasSplitTable(mp)) {
2686 PyDictObject *split_copy;
Victor Stinner742da042016-09-07 17:40:12 -07002687 Py_ssize_t size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
2688 PyObject **newvalues;
2689 newvalues = new_values(size);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002690 if (newvalues == NULL)
2691 return PyErr_NoMemory();
2692 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2693 if (split_copy == NULL) {
2694 free_values(newvalues);
2695 return NULL;
2696 }
2697 split_copy->ma_values = newvalues;
2698 split_copy->ma_keys = mp->ma_keys;
2699 split_copy->ma_used = mp->ma_used;
INADA Naokid1c82c52018-04-03 11:43:53 +09002700 split_copy->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokia7576492018-11-14 18:39:27 +09002701 dictkeys_incref(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002702 for (i = 0, n = size; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002703 PyObject *value = mp->ma_values[i];
2704 Py_XINCREF(value);
2705 split_copy->ma_values[i] = value;
2706 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002707 if (_PyObject_GC_IS_TRACKED(mp))
2708 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002709 return (PyObject *)split_copy;
2710 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002711
2712 if (PyDict_CheckExact(mp) && mp->ma_values == NULL &&
2713 (mp->ma_used >= (mp->ma_keys->dk_nentries * 2) / 3))
2714 {
2715 /* Use fast-copy if:
2716
2717 (1) 'mp' is an instance of a subclassed dict; and
2718
2719 (2) 'mp' is not a split-dict; and
2720
2721 (3) if 'mp' is non-compact ('del' operation does not resize dicts),
2722 do fast-copy only if it has at most 1/3 non-used keys.
2723
Ville Skyttä61f82e02018-04-20 23:08:45 +03002724 The last condition (3) is important to guard against a pathological
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002725 case when a large dict is almost emptied with multiple del/pop
2726 operations and copied after that. In cases like this, we defer to
2727 PyDict_Merge, which produces a compacted copy.
2728 */
2729 return clone_combined_dict(mp);
2730 }
2731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002732 copy = PyDict_New();
2733 if (copy == NULL)
2734 return NULL;
2735 if (PyDict_Merge(copy, o, 1) == 0)
2736 return copy;
2737 Py_DECREF(copy);
2738 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002739}
2740
Martin v. Löwis18e16552006-02-15 17:27:45 +00002741Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002742PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002743{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002744 if (mp == NULL || !PyDict_Check(mp)) {
2745 PyErr_BadInternalCall();
2746 return -1;
2747 }
2748 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002749}
2750
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002751PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002752PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002753{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002754 if (mp == NULL || !PyDict_Check(mp)) {
2755 PyErr_BadInternalCall();
2756 return NULL;
2757 }
2758 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002759}
2760
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002761PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002762PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002763{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002764 if (mp == NULL || !PyDict_Check(mp)) {
2765 PyErr_BadInternalCall();
2766 return NULL;
2767 }
2768 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002769}
2770
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002771PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002772PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002773{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002774 if (mp == NULL || !PyDict_Check(mp)) {
2775 PyErr_BadInternalCall();
2776 return NULL;
2777 }
2778 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002779}
2780
Tim Peterse63415e2001-05-08 04:38:29 +00002781/* Return 1 if dicts equal, 0 if not, -1 if error.
2782 * Gets out as soon as any difference is detected.
2783 * Uses only Py_EQ comparison.
2784 */
2785static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002786dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002787{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002788 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002790 if (a->ma_used != b->ma_used)
2791 /* can't be equal if # of entries differ */
2792 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002793 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Victor Stinner742da042016-09-07 17:40:12 -07002794 for (i = 0; i < a->ma_keys->dk_nentries; i++) {
2795 PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002796 PyObject *aval;
2797 if (a->ma_values)
2798 aval = a->ma_values[i];
2799 else
2800 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002801 if (aval != NULL) {
2802 int cmp;
2803 PyObject *bval;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002804 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002805 /* temporarily bump aval's refcount to ensure it stays
2806 alive until we're done with it */
2807 Py_INCREF(aval);
2808 /* ditto for key */
2809 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002810 /* reuse the known hash value */
INADA Naoki778928b2017-08-03 23:45:15 +09002811 b->ma_keys->dk_lookup(b, key, ep->me_hash, &bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002812 if (bval == NULL) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002813 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002814 Py_DECREF(aval);
2815 if (PyErr_Occurred())
2816 return -1;
2817 return 0;
2818 }
Dong-hee Na2d5bf562019-12-31 10:04:22 +09002819 Py_INCREF(bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002820 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002821 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002822 Py_DECREF(aval);
Dong-hee Na2d5bf562019-12-31 10:04:22 +09002823 Py_DECREF(bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002824 if (cmp <= 0) /* error or not equal */
2825 return cmp;
2826 }
2827 }
2828 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002829}
Tim Peterse63415e2001-05-08 04:38:29 +00002830
2831static PyObject *
2832dict_richcompare(PyObject *v, PyObject *w, int op)
2833{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002834 int cmp;
2835 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002837 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2838 res = Py_NotImplemented;
2839 }
2840 else if (op == Py_EQ || op == Py_NE) {
2841 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2842 if (cmp < 0)
2843 return NULL;
2844 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2845 }
2846 else
2847 res = Py_NotImplemented;
2848 Py_INCREF(res);
2849 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002850}
Tim Peterse63415e2001-05-08 04:38:29 +00002851
Larry Hastings61272b72014-01-07 12:41:53 -08002852/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002853
2854@coexist
2855dict.__contains__
2856
2857 key: object
2858 /
2859
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002860True if the dictionary has the specified key, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002861[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002862
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002863static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002864dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka19d25972017-02-04 08:05:07 +02002865/*[clinic end generated code: output=a3d03db709ed6e6b input=fe1cb42ad831e820]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002866{
Larry Hastingsc2047262014-01-25 20:43:29 -08002867 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002868 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002869 Py_ssize_t ix;
INADA Naokiba609772016-12-07 20:41:42 +09002870 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002873 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 hash = PyObject_Hash(key);
2875 if (hash == -1)
2876 return NULL;
2877 }
INADA Naoki778928b2017-08-03 23:45:15 +09002878 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002879 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002881 if (ix == DKIX_EMPTY || value == NULL)
Victor Stinner742da042016-09-07 17:40:12 -07002882 Py_RETURN_FALSE;
2883 Py_RETURN_TRUE;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002884}
2885
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002886/*[clinic input]
2887dict.get
2888
2889 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002890 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002891 /
2892
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002893Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002894[clinic start generated code]*/
2895
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002896static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002897dict_get_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002898/*[clinic end generated code: output=bba707729dee05bf input=279ddb5790b6b107]*/
Barry Warsawc38c5da1997-10-06 17:49:20 +00002899{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002900 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002901 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002902 Py_ssize_t ix;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002904 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002905 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002906 hash = PyObject_Hash(key);
2907 if (hash == -1)
2908 return NULL;
2909 }
INADA Naoki778928b2017-08-03 23:45:15 +09002910 ix = (self->ma_keys->dk_lookup) (self, key, hash, &val);
Victor Stinner742da042016-09-07 17:40:12 -07002911 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002912 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002913 if (ix == DKIX_EMPTY || val == NULL) {
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002914 val = default_value;
INADA Naokiba609772016-12-07 20:41:42 +09002915 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002916 Py_INCREF(val);
2917 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002918}
2919
Benjamin Peterson00e98862013-03-07 22:16:29 -05002920PyObject *
2921PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002922{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002923 PyDictObject *mp = (PyDictObject *)d;
INADA Naoki93f26f72016-11-02 18:45:16 +09002924 PyObject *value;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002925 Py_hash_t hash;
Guido van Rossum164452c2000-08-08 16:12:54 +00002926
Benjamin Peterson00e98862013-03-07 22:16:29 -05002927 if (!PyDict_Check(d)) {
2928 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002929 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002930 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002932 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002933 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002934 hash = PyObject_Hash(key);
2935 if (hash == -1)
2936 return NULL;
2937 }
Inada Naoki2ddc7f62019-03-18 20:38:33 +09002938 if (mp->ma_keys == Py_EMPTY_KEYS) {
2939 if (insert_to_emptydict(mp, key, hash, defaultobj) < 0) {
2940 return NULL;
2941 }
2942 return defaultobj;
2943 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002944
2945 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
2946 if (insertion_resize(mp) < 0)
2947 return NULL;
2948 }
2949
INADA Naoki778928b2017-08-03 23:45:15 +09002950 Py_ssize_t ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002951 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002952 return NULL;
INADA Naoki93f26f72016-11-02 18:45:16 +09002953
2954 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09002955 ((ix >= 0 && value == NULL && mp->ma_used != ix) ||
INADA Naoki93f26f72016-11-02 18:45:16 +09002956 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
2957 if (insertion_resize(mp) < 0) {
2958 return NULL;
2959 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002960 ix = DKIX_EMPTY;
2961 }
2962
2963 if (ix == DKIX_EMPTY) {
2964 PyDictKeyEntry *ep, *ep0;
2965 value = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002966 if (mp->ma_keys->dk_usable <= 0) {
Victor Stinner3c336c52016-09-12 14:17:40 +02002967 if (insertion_resize(mp) < 0) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002968 return NULL;
Victor Stinner3c336c52016-09-12 14:17:40 +02002969 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002970 }
INADA Naoki778928b2017-08-03 23:45:15 +09002971 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naoki93f26f72016-11-02 18:45:16 +09002972 ep0 = DK_ENTRIES(mp->ma_keys);
2973 ep = &ep0[mp->ma_keys->dk_nentries];
INADA Naokia7576492018-11-14 18:39:27 +09002974 dictkeys_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002975 Py_INCREF(key);
INADA Naoki93f26f72016-11-02 18:45:16 +09002976 Py_INCREF(value);
2977 MAINTAIN_TRACKING(mp, key, value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002978 ep->me_key = key;
2979 ep->me_hash = hash;
INADA Naokiba609772016-12-07 20:41:42 +09002980 if (_PyDict_HasSplitTable(mp)) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002981 assert(mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
2982 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Victor Stinner742da042016-09-07 17:40:12 -07002983 }
2984 else {
INADA Naoki93f26f72016-11-02 18:45:16 +09002985 ep->me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07002986 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002987 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002988 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naoki93f26f72016-11-02 18:45:16 +09002989 mp->ma_keys->dk_usable--;
2990 mp->ma_keys->dk_nentries++;
2991 assert(mp->ma_keys->dk_usable >= 0);
2992 }
INADA Naokiba609772016-12-07 20:41:42 +09002993 else if (value == NULL) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002994 value = defaultobj;
2995 assert(_PyDict_HasSplitTable(mp));
2996 assert(ix == mp->ma_used);
2997 Py_INCREF(value);
2998 MAINTAIN_TRACKING(mp, key, value);
INADA Naokiba609772016-12-07 20:41:42 +09002999 mp->ma_values[ix] = value;
INADA Naoki93f26f72016-11-02 18:45:16 +09003000 mp->ma_used++;
3001 mp->ma_version_tag = DICT_NEXT_VERSION();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003002 }
INADA Naoki93f26f72016-11-02 18:45:16 +09003003
Victor Stinner0fc91ee2019-04-12 21:51:34 +02003004 ASSERT_CONSISTENT(mp);
INADA Naoki93f26f72016-11-02 18:45:16 +09003005 return value;
Guido van Rossum164452c2000-08-08 16:12:54 +00003006}
3007
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003008/*[clinic input]
3009dict.setdefault
3010
3011 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02003012 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003013 /
3014
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02003015Insert key with a value of default if key is not in the dictionary.
3016
3017Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003018[clinic start generated code]*/
3019
Benjamin Peterson00e98862013-03-07 22:16:29 -05003020static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02003021dict_setdefault_impl(PyDictObject *self, PyObject *key,
3022 PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02003023/*[clinic end generated code: output=f8c1101ebf69e220 input=0f063756e815fd9d]*/
Benjamin Peterson00e98862013-03-07 22:16:29 -05003024{
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003025 PyObject *val;
Benjamin Peterson00e98862013-03-07 22:16:29 -05003026
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02003027 val = PyDict_SetDefault((PyObject *)self, key, default_value);
Benjamin Peterson00e98862013-03-07 22:16:29 -05003028 Py_XINCREF(val);
3029 return val;
3030}
Guido van Rossum164452c2000-08-08 16:12:54 +00003031
3032static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303033dict_clear(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00003034{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003035 PyDict_Clear((PyObject *)mp);
3036 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00003037}
3038
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003039/*[clinic input]
3040dict.pop
3041
3042 key: object
3043 default: object = NULL
3044 /
3045
Serhiy Storchaka279f4462019-09-14 12:24:05 +03003046D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003047
3048If key is not found, default is returned if given, otherwise KeyError is raised
3049[clinic start generated code]*/
3050
Guido van Rossumba6ab842000-12-12 22:02:18 +00003051static PyObject *
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003052dict_pop_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
Serhiy Storchaka279f4462019-09-14 12:24:05 +03003053/*[clinic end generated code: output=3abb47b89f24c21c input=eeebec7812190348]*/
Guido van Rossume027d982002-04-12 15:11:59 +00003054{
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003055 return _PyDict_Pop((PyObject*)self, key, default_value);
Guido van Rossume027d982002-04-12 15:11:59 +00003056}
3057
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003058/*[clinic input]
3059dict.popitem
3060
3061Remove and return a (key, value) pair as a 2-tuple.
3062
3063Pairs are returned in LIFO (last-in, first-out) order.
3064Raises KeyError if the dict is empty.
3065[clinic start generated code]*/
3066
Guido van Rossume027d982002-04-12 15:11:59 +00003067static PyObject *
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003068dict_popitem_impl(PyDictObject *self)
3069/*[clinic end generated code: output=e65fcb04420d230d input=1c38a49f21f64941]*/
Guido van Rossumba6ab842000-12-12 22:02:18 +00003070{
Victor Stinner742da042016-09-07 17:40:12 -07003071 Py_ssize_t i, j;
3072 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003073 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00003074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003075 /* Allocate the result tuple before checking the size. Believe it
3076 * or not, this allocation could trigger a garbage collection which
3077 * could empty the dict, so if we checked the size first and that
3078 * happened, the result would be an infinite loop (searching for an
3079 * entry that no longer exists). Note that the usual popitem()
3080 * idiom is "while d: k, v = d.popitem()". so needing to throw the
3081 * tuple away if the dict *is* empty isn't a significant
3082 * inefficiency -- possible, but unlikely in practice.
3083 */
3084 res = PyTuple_New(2);
3085 if (res == NULL)
3086 return NULL;
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003087 if (self->ma_used == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003088 Py_DECREF(res);
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003089 PyErr_SetString(PyExc_KeyError, "popitem(): dictionary is empty");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003090 return NULL;
3091 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003092 /* Convert split table to combined table */
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003093 if (self->ma_keys->dk_lookup == lookdict_split) {
3094 if (dictresize(self, DK_SIZE(self->ma_keys))) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003095 Py_DECREF(res);
3096 return NULL;
3097 }
3098 }
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003099 ENSURE_ALLOWS_DELETIONS(self);
Victor Stinner742da042016-09-07 17:40:12 -07003100
3101 /* Pop last item */
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003102 ep0 = DK_ENTRIES(self->ma_keys);
3103 i = self->ma_keys->dk_nentries - 1;
Victor Stinner742da042016-09-07 17:40:12 -07003104 while (i >= 0 && ep0[i].me_value == NULL) {
3105 i--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003106 }
Victor Stinner742da042016-09-07 17:40:12 -07003107 assert(i >= 0);
3108
3109 ep = &ep0[i];
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003110 j = lookdict_index(self->ma_keys, ep->me_hash, i);
Victor Stinner742da042016-09-07 17:40:12 -07003111 assert(j >= 0);
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003112 assert(dictkeys_get_index(self->ma_keys, j) == i);
3113 dictkeys_set_index(self->ma_keys, j, DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -07003114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003115 PyTuple_SET_ITEM(res, 0, ep->me_key);
3116 PyTuple_SET_ITEM(res, 1, ep->me_value);
Victor Stinner742da042016-09-07 17:40:12 -07003117 ep->me_key = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003118 ep->me_value = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07003119 /* We can't dk_usable++ since there is DKIX_DUMMY in indices */
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003120 self->ma_keys->dk_nentries = i;
3121 self->ma_used--;
3122 self->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner0fc91ee2019-04-12 21:51:34 +02003123 ASSERT_CONSISTENT(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003124 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00003125}
3126
Jeremy Hylton8caad492000-06-23 14:18:11 +00003127static int
3128dict_traverse(PyObject *op, visitproc visit, void *arg)
3129{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003130 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson55f44522016-09-05 12:12:59 -07003131 PyDictKeysObject *keys = mp->ma_keys;
Serhiy Storchaka46825d22016-09-26 21:29:34 +03003132 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Victor Stinner742da042016-09-07 17:40:12 -07003133 Py_ssize_t i, n = keys->dk_nentries;
3134
Benjamin Peterson55f44522016-09-05 12:12:59 -07003135 if (keys->dk_lookup == lookdict) {
3136 for (i = 0; i < n; i++) {
3137 if (entries[i].me_value != NULL) {
3138 Py_VISIT(entries[i].me_value);
3139 Py_VISIT(entries[i].me_key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003140 }
3141 }
Victor Stinner742da042016-09-07 17:40:12 -07003142 }
3143 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003144 if (mp->ma_values != NULL) {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003145 for (i = 0; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003146 Py_VISIT(mp->ma_values[i]);
3147 }
3148 }
3149 else {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003150 for (i = 0; i < n; i++) {
3151 Py_VISIT(entries[i].me_value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003152 }
3153 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003154 }
3155 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003156}
3157
3158static int
3159dict_tp_clear(PyObject *op)
3160{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003161 PyDict_Clear(op);
3162 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003163}
3164
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003165static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003166
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003167Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003168_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003169{
Victor Stinner742da042016-09-07 17:40:12 -07003170 Py_ssize_t size, usable, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003171
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003172 size = DK_SIZE(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07003173 usable = USABLE_FRACTION(size);
3174
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02003175 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003176 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -07003177 res += usable * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003178 /* If the dictionary is split, the keys portion is accounted-for
3179 in the type object. */
3180 if (mp->ma_keys->dk_refcnt == 1)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003181 res += (sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003182 + DK_IXSIZE(mp->ma_keys) * size
3183 + sizeof(PyDictKeyEntry) * usable);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003184 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003185}
3186
3187Py_ssize_t
3188_PyDict_KeysSize(PyDictKeysObject *keys)
3189{
Victor Stinner98ee9d52016-09-08 09:33:56 -07003190 return (sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003191 + DK_IXSIZE(keys) * DK_SIZE(keys)
3192 + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003193}
3194
doko@ubuntu.com17210f52016-01-14 14:04:59 +01003195static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303196dict_sizeof(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003197{
3198 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
3199}
3200
Brandt Buchereb8ac572020-02-24 19:47:34 -08003201static PyObject *
3202dict_or(PyObject *self, PyObject *other)
3203{
3204 if (!PyDict_Check(self) || !PyDict_Check(other)) {
3205 Py_RETURN_NOTIMPLEMENTED;
3206 }
3207 PyObject *new = PyDict_Copy(self);
3208 if (new == NULL) {
3209 return NULL;
3210 }
3211 if (dict_update_arg(new, other)) {
3212 Py_DECREF(new);
3213 return NULL;
3214 }
3215 return new;
3216}
3217
3218static PyObject *
3219dict_ior(PyObject *self, PyObject *other)
3220{
3221 if (dict_update_arg(self, other)) {
3222 return NULL;
3223 }
3224 Py_INCREF(self);
3225 return self;
3226}
3227
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00003228PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
3229
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003230PyDoc_STRVAR(sizeof__doc__,
3231"D.__sizeof__() -> size of D in memory, in bytes");
3232
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003233PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04003234"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
3235If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
3236If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
3237In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00003238
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003239PyDoc_STRVAR(clear__doc__,
3240"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003241
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003242PyDoc_STRVAR(copy__doc__,
3243"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00003244
Guido van Rossumb90c8482007-02-10 01:11:45 +00003245/* Forward */
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303246static PyObject *dictkeys_new(PyObject *, PyObject *);
3247static PyObject *dictitems_new(PyObject *, PyObject *);
3248static PyObject *dictvalues_new(PyObject *, PyObject *);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003249
Guido van Rossum45c85d12007-07-27 16:31:40 +00003250PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003251 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003252PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003253 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003254PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003255 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00003256
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003257static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07003258 DICT___CONTAINS___METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003259 {"__getitem__", (PyCFunction)(void(*)(void))dict_subscript, METH_O | METH_COEXIST,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003260 getitem__doc__},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003261 {"__sizeof__", (PyCFunction)(void(*)(void))dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003262 sizeof__doc__},
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003263 DICT_GET_METHODDEF
3264 DICT_SETDEFAULT_METHODDEF
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003265 DICT_POP_METHODDEF
3266 DICT_POPITEM_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303267 {"keys", dictkeys_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003268 keys__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303269 {"items", dictitems_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003270 items__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303271 {"values", dictvalues_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003272 values__doc__},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003273 {"update", (PyCFunction)(void(*)(void))dict_update, METH_VARARGS | METH_KEYWORDS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003274 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08003275 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003276 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
3277 clear__doc__},
3278 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
3279 copy__doc__},
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003280 DICT___REVERSED___METHODDEF
Guido van Rossum48b069a2020-04-07 09:50:06 -07003281 {"__class_getitem__", (PyCFunction)Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003282 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003283};
3284
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003285/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00003286int
3287PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003288{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003289 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07003290 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003291 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003292 PyObject *value;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003294 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003295 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003296 hash = PyObject_Hash(key);
3297 if (hash == -1)
3298 return -1;
3299 }
INADA Naoki778928b2017-08-03 23:45:15 +09003300 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003301 if (ix == DKIX_ERROR)
3302 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003303 return (ix != DKIX_EMPTY && value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003304}
3305
Thomas Wouterscf297e42007-02-23 15:07:44 +00003306/* Internal version of PyDict_Contains used when the hash value is already known */
3307int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003308_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00003309{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003310 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003311 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07003312 Py_ssize_t ix;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003313
INADA Naoki778928b2017-08-03 23:45:15 +09003314 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003315 if (ix == DKIX_ERROR)
3316 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003317 return (ix != DKIX_EMPTY && value != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00003318}
3319
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003320/* Hack to implement "key in dict" */
3321static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003322 0, /* sq_length */
3323 0, /* sq_concat */
3324 0, /* sq_repeat */
3325 0, /* sq_item */
3326 0, /* sq_slice */
3327 0, /* sq_ass_item */
3328 0, /* sq_ass_slice */
3329 PyDict_Contains, /* sq_contains */
3330 0, /* sq_inplace_concat */
3331 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003332};
3333
Brandt Buchereb8ac572020-02-24 19:47:34 -08003334static PyNumberMethods dict_as_number = {
3335 .nb_or = dict_or,
3336 .nb_inplace_or = dict_ior,
3337};
3338
Guido van Rossum09e563a2001-05-01 12:10:21 +00003339static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003340dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3341{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003342 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003343 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003345 assert(type != NULL && type->tp_alloc != NULL);
3346 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02003347 if (self == NULL)
3348 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003349 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003350
Victor Stinnera9f61a52013-07-16 22:17:26 +02003351 /* The object has been implicitly tracked by tp_alloc */
3352 if (type == &PyDict_Type)
3353 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003354
3355 d->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003356 d->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07003357 d->ma_keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003358 if (d->ma_keys == NULL) {
3359 Py_DECREF(self);
3360 return NULL;
3361 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +02003362 ASSERT_CONSISTENT(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003363 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003364}
3365
Tim Peters25786c02001-09-02 08:22:48 +00003366static int
3367dict_init(PyObject *self, PyObject *args, PyObject *kwds)
3368{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003369 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00003370}
3371
Tim Peters6d6c1a32001-08-02 04:15:00 +00003372static PyObject *
Dong-hee Nae27916b2020-04-02 09:55:43 +09003373dict_vectorcall(PyObject *type, PyObject * const*args,
3374 size_t nargsf, PyObject *kwnames)
3375{
3376 assert(PyType_Check(type));
3377 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
3378 if (!_PyArg_CheckPositional("dict", nargs, 0, 1)) {
3379 return NULL;
3380 }
3381
3382 PyObject *self = dict_new((PyTypeObject *)type, NULL, NULL);
3383 if (self == NULL) {
3384 return NULL;
3385 }
3386 if (nargs == 1) {
3387 if (dict_update_arg(self, args[0]) < 0) {
3388 Py_DECREF(self);
3389 return NULL;
3390 }
3391 args++;
3392 }
3393 if (kwnames != NULL) {
3394 for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(kwnames); i++) {
3395 if (PyDict_SetItem(self, PyTuple_GET_ITEM(kwnames, i), args[i]) < 0) {
3396 Py_DECREF(self);
3397 return NULL;
3398 }
3399 }
3400 }
3401 return self;
3402}
3403
3404static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003405dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00003406{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003407 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003408}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003409
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003410PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00003411"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00003412"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003413" (key, value) pairs\n"
3414"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00003415" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003416" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00003417" d[k] = v\n"
3418"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
3419" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00003420
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003421PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003422 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3423 "dict",
3424 sizeof(PyDictObject),
3425 0,
3426 (destructor)dict_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003427 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003428 0, /* tp_getattr */
3429 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003430 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003431 (reprfunc)dict_repr, /* tp_repr */
Brandt Buchereb8ac572020-02-24 19:47:34 -08003432 &dict_as_number, /* tp_as_number */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003433 &dict_as_sequence, /* tp_as_sequence */
3434 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003435 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003436 0, /* tp_call */
3437 0, /* tp_str */
3438 PyObject_GenericGetAttr, /* tp_getattro */
3439 0, /* tp_setattro */
3440 0, /* tp_as_buffer */
3441 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3442 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
3443 dictionary_doc, /* tp_doc */
3444 dict_traverse, /* tp_traverse */
3445 dict_tp_clear, /* tp_clear */
3446 dict_richcompare, /* tp_richcompare */
3447 0, /* tp_weaklistoffset */
3448 (getiterfunc)dict_iter, /* tp_iter */
3449 0, /* tp_iternext */
3450 mapp_methods, /* tp_methods */
3451 0, /* tp_members */
3452 0, /* tp_getset */
3453 0, /* tp_base */
3454 0, /* tp_dict */
3455 0, /* tp_descr_get */
3456 0, /* tp_descr_set */
3457 0, /* tp_dictoffset */
3458 dict_init, /* tp_init */
3459 PyType_GenericAlloc, /* tp_alloc */
3460 dict_new, /* tp_new */
3461 PyObject_GC_Del, /* tp_free */
Dong-hee Nae27916b2020-04-02 09:55:43 +09003462 .tp_vectorcall = dict_vectorcall,
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003463};
3464
Victor Stinner3c1e4812012-03-26 22:10:51 +02003465PyObject *
3466_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
3467{
3468 PyObject *kv;
3469 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02003470 if (kv == NULL) {
3471 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003472 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02003473 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02003474 return PyDict_GetItem(dp, kv);
3475}
3476
Guido van Rossum3cca2451997-05-16 14:23:33 +00003477/* For backward compatibility with old dictionary interface */
3478
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003479PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003480PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003481{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003482 PyObject *kv, *rv;
3483 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003484 if (kv == NULL) {
3485 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003486 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003487 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003488 rv = PyDict_GetItem(v, kv);
3489 Py_DECREF(kv);
3490 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003491}
3492
3493int
Victor Stinner3c1e4812012-03-26 22:10:51 +02003494_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
3495{
3496 PyObject *kv;
3497 kv = _PyUnicode_FromId(key); /* borrowed */
3498 if (kv == NULL)
3499 return -1;
3500 return PyDict_SetItem(v, kv, item);
3501}
3502
3503int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003504PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003505{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003506 PyObject *kv;
3507 int err;
3508 kv = PyUnicode_FromString(key);
3509 if (kv == NULL)
3510 return -1;
3511 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
3512 err = PyDict_SetItem(v, kv, item);
3513 Py_DECREF(kv);
3514 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003515}
3516
3517int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01003518_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
3519{
3520 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3521 if (kv == NULL)
3522 return -1;
3523 return PyDict_DelItem(v, kv);
3524}
3525
3526int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003527PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003528{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003529 PyObject *kv;
3530 int err;
3531 kv = PyUnicode_FromString(key);
3532 if (kv == NULL)
3533 return -1;
3534 err = PyDict_DelItem(v, kv);
3535 Py_DECREF(kv);
3536 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003537}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003538
Raymond Hettinger019a1482004-03-18 02:41:19 +00003539/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003540
3541typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003542 PyObject_HEAD
3543 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
3544 Py_ssize_t di_used;
3545 Py_ssize_t di_pos;
3546 PyObject* di_result; /* reusable result tuple for iteritems */
3547 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003548} dictiterobject;
3549
3550static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003551dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003552{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003553 dictiterobject *di;
3554 di = PyObject_GC_New(dictiterobject, itertype);
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003555 if (di == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003556 return NULL;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003557 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003558 Py_INCREF(dict);
3559 di->di_dict = dict;
3560 di->di_used = dict->ma_used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003561 di->len = dict->ma_used;
Dong-hee Na24dc2f82019-10-20 05:01:08 +09003562 if (itertype == &PyDictRevIterKey_Type ||
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003563 itertype == &PyDictRevIterItem_Type ||
Dong-hee Na24dc2f82019-10-20 05:01:08 +09003564 itertype == &PyDictRevIterValue_Type) {
3565 if (dict->ma_values) {
3566 di->di_pos = dict->ma_used - 1;
3567 }
3568 else {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003569 di->di_pos = dict->ma_keys->dk_nentries - 1;
Dong-hee Na24dc2f82019-10-20 05:01:08 +09003570 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003571 }
3572 else {
3573 di->di_pos = 0;
3574 }
3575 if (itertype == &PyDictIterItem_Type ||
3576 itertype == &PyDictRevIterItem_Type) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003577 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
3578 if (di->di_result == NULL) {
3579 Py_DECREF(di);
3580 return NULL;
3581 }
3582 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003583 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003584 di->di_result = NULL;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003585 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003586 _PyObject_GC_TRACK(di);
3587 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003588}
3589
3590static void
3591dictiter_dealloc(dictiterobject *di)
3592{
INADA Naokia6296d32017-08-24 14:55:17 +09003593 /* bpo-31095: UnTrack is needed before calling any callbacks */
3594 _PyObject_GC_UNTRACK(di);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003595 Py_XDECREF(di->di_dict);
3596 Py_XDECREF(di->di_result);
3597 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003598}
3599
3600static int
3601dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
3602{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003603 Py_VISIT(di->di_dict);
3604 Py_VISIT(di->di_result);
3605 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003606}
3607
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003608static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303609dictiter_len(dictiterobject *di, PyObject *Py_UNUSED(ignored))
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003610{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003611 Py_ssize_t len = 0;
3612 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
3613 len = di->len;
3614 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003615}
3616
Guido van Rossumb90c8482007-02-10 01:11:45 +00003617PyDoc_STRVAR(length_hint_doc,
3618 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003619
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003620static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303621dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003622
3623PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3624
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003625static PyMethodDef dictiter_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003626 {"__length_hint__", (PyCFunction)(void(*)(void))dictiter_len, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003627 length_hint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003628 {"__reduce__", (PyCFunction)(void(*)(void))dictiter_reduce, METH_NOARGS,
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003629 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003630 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003631};
3632
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003633static PyObject*
3634dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00003635{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003636 PyObject *key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003637 Py_ssize_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003638 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003639 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00003640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003641 if (d == NULL)
3642 return NULL;
3643 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00003644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003645 if (di->di_used != d->ma_used) {
3646 PyErr_SetString(PyExc_RuntimeError,
3647 "dictionary changed size during iteration");
3648 di->di_used = -1; /* Make this state sticky */
3649 return NULL;
3650 }
Guido van Rossum2147df72002-07-16 20:30:22 +00003651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003652 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003653 k = d->ma_keys;
INADA Naokica2d8be2016-11-04 16:59:10 +09003654 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003655 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003656 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003657 goto fail;
3658 key = DK_ENTRIES(k)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003659 assert(d->ma_values[i] != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003660 }
3661 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003662 Py_ssize_t n = k->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003663 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3664 while (i < n && entry_ptr->me_value == NULL) {
3665 entry_ptr++;
3666 i++;
3667 }
3668 if (i >= n)
3669 goto fail;
3670 key = entry_ptr->me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003671 }
Thomas Perl796cc6e2019-03-28 07:03:25 +01003672 // We found an element (key), but did not expect it
3673 if (di->len == 0) {
3674 PyErr_SetString(PyExc_RuntimeError,
3675 "dictionary keys changed during iteration");
3676 goto fail;
3677 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003678 di->di_pos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003679 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003680 Py_INCREF(key);
3681 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003682
3683fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003684 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003685 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003686 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003687}
3688
Raymond Hettinger019a1482004-03-18 02:41:19 +00003689PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003690 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3691 "dict_keyiterator", /* tp_name */
3692 sizeof(dictiterobject), /* tp_basicsize */
3693 0, /* tp_itemsize */
3694 /* methods */
3695 (destructor)dictiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003696 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003697 0, /* tp_getattr */
3698 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003699 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003700 0, /* tp_repr */
3701 0, /* tp_as_number */
3702 0, /* tp_as_sequence */
3703 0, /* tp_as_mapping */
3704 0, /* tp_hash */
3705 0, /* tp_call */
3706 0, /* tp_str */
3707 PyObject_GenericGetAttr, /* tp_getattro */
3708 0, /* tp_setattro */
3709 0, /* tp_as_buffer */
3710 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3711 0, /* tp_doc */
3712 (traverseproc)dictiter_traverse, /* tp_traverse */
3713 0, /* tp_clear */
3714 0, /* tp_richcompare */
3715 0, /* tp_weaklistoffset */
3716 PyObject_SelfIter, /* tp_iter */
3717 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3718 dictiter_methods, /* tp_methods */
3719 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003720};
3721
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003722static PyObject *
3723dictiter_iternextvalue(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003724{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003725 PyObject *value;
INADA Naokica2d8be2016-11-04 16:59:10 +09003726 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003727 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003729 if (d == NULL)
3730 return NULL;
3731 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003733 if (di->di_used != d->ma_used) {
3734 PyErr_SetString(PyExc_RuntimeError,
3735 "dictionary changed size during iteration");
3736 di->di_used = -1; /* Make this state sticky */
3737 return NULL;
3738 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003740 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003741 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003742 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003743 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003744 goto fail;
INADA Naokica2d8be2016-11-04 16:59:10 +09003745 value = d->ma_values[i];
3746 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003747 }
3748 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003749 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003750 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3751 while (i < n && entry_ptr->me_value == NULL) {
3752 entry_ptr++;
3753 i++;
3754 }
3755 if (i >= n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003756 goto fail;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003757 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003758 }
Thomas Perlb8311cf2019-04-02 11:30:10 +02003759 // We found an element, but did not expect it
3760 if (di->len == 0) {
3761 PyErr_SetString(PyExc_RuntimeError,
3762 "dictionary keys changed during iteration");
3763 goto fail;
3764 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003765 di->di_pos = i+1;
3766 di->len--;
3767 Py_INCREF(value);
3768 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003769
3770fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003771 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003772 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003773 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003774}
3775
3776PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003777 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3778 "dict_valueiterator", /* tp_name */
3779 sizeof(dictiterobject), /* tp_basicsize */
3780 0, /* tp_itemsize */
3781 /* methods */
3782 (destructor)dictiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003783 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003784 0, /* tp_getattr */
3785 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003786 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003787 0, /* tp_repr */
3788 0, /* tp_as_number */
3789 0, /* tp_as_sequence */
3790 0, /* tp_as_mapping */
3791 0, /* tp_hash */
3792 0, /* tp_call */
3793 0, /* tp_str */
3794 PyObject_GenericGetAttr, /* tp_getattro */
3795 0, /* tp_setattro */
3796 0, /* tp_as_buffer */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003797 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003798 0, /* tp_doc */
3799 (traverseproc)dictiter_traverse, /* tp_traverse */
3800 0, /* tp_clear */
3801 0, /* tp_richcompare */
3802 0, /* tp_weaklistoffset */
3803 PyObject_SelfIter, /* tp_iter */
3804 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3805 dictiter_methods, /* tp_methods */
3806 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003807};
3808
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003809static PyObject *
3810dictiter_iternextitem(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003811{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003812 PyObject *key, *value, *result;
INADA Naokica2d8be2016-11-04 16:59:10 +09003813 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003814 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003816 if (d == NULL)
3817 return NULL;
3818 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003820 if (di->di_used != d->ma_used) {
3821 PyErr_SetString(PyExc_RuntimeError,
3822 "dictionary changed size during iteration");
3823 di->di_used = -1; /* Make this state sticky */
3824 return NULL;
3825 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003827 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003828 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003829 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003830 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003831 goto fail;
3832 key = DK_ENTRIES(d->ma_keys)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003833 value = d->ma_values[i];
3834 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003835 }
3836 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003837 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003838 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3839 while (i < n && entry_ptr->me_value == NULL) {
3840 entry_ptr++;
3841 i++;
3842 }
3843 if (i >= n)
3844 goto fail;
3845 key = entry_ptr->me_key;
3846 value = entry_ptr->me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003847 }
Thomas Perlb8311cf2019-04-02 11:30:10 +02003848 // We found an element, but did not expect it
3849 if (di->len == 0) {
3850 PyErr_SetString(PyExc_RuntimeError,
3851 "dictionary keys changed during iteration");
3852 goto fail;
3853 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003854 di->di_pos = i+1;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003855 di->len--;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003856 Py_INCREF(key);
3857 Py_INCREF(value);
3858 result = di->di_result;
3859 if (Py_REFCNT(result) == 1) {
3860 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3861 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3862 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3863 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003864 Py_INCREF(result);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003865 Py_DECREF(oldkey);
3866 Py_DECREF(oldvalue);
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003867 }
3868 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003869 result = PyTuple_New(2);
3870 if (result == NULL)
3871 return NULL;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003872 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3873 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003874 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003875 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003876
3877fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003878 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003879 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003880 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003881}
3882
3883PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003884 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3885 "dict_itemiterator", /* tp_name */
3886 sizeof(dictiterobject), /* tp_basicsize */
3887 0, /* tp_itemsize */
3888 /* methods */
3889 (destructor)dictiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003890 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003891 0, /* tp_getattr */
3892 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003893 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003894 0, /* tp_repr */
3895 0, /* tp_as_number */
3896 0, /* tp_as_sequence */
3897 0, /* tp_as_mapping */
3898 0, /* tp_hash */
3899 0, /* tp_call */
3900 0, /* tp_str */
3901 PyObject_GenericGetAttr, /* tp_getattro */
3902 0, /* tp_setattro */
3903 0, /* tp_as_buffer */
3904 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3905 0, /* tp_doc */
3906 (traverseproc)dictiter_traverse, /* tp_traverse */
3907 0, /* tp_clear */
3908 0, /* tp_richcompare */
3909 0, /* tp_weaklistoffset */
3910 PyObject_SelfIter, /* tp_iter */
3911 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3912 dictiter_methods, /* tp_methods */
3913 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003914};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003915
3916
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003917/* dictreviter */
3918
3919static PyObject *
3920dictreviter_iternext(dictiterobject *di)
3921{
3922 PyDictObject *d = di->di_dict;
3923
3924 if (d == NULL) {
3925 return NULL;
3926 }
3927 assert (PyDict_Check(d));
3928
3929 if (di->di_used != d->ma_used) {
3930 PyErr_SetString(PyExc_RuntimeError,
3931 "dictionary changed size during iteration");
3932 di->di_used = -1; /* Make this state sticky */
3933 return NULL;
3934 }
3935
3936 Py_ssize_t i = di->di_pos;
3937 PyDictKeysObject *k = d->ma_keys;
3938 PyObject *key, *value, *result;
3939
Serhiy Storchaka2e3d8732019-10-23 14:48:08 +03003940 if (i < 0) {
3941 goto fail;
3942 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003943 if (d->ma_values) {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003944 key = DK_ENTRIES(k)[i].me_key;
3945 value = d->ma_values[i];
3946 assert (value != NULL);
3947 }
3948 else {
3949 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
Serhiy Storchaka2e3d8732019-10-23 14:48:08 +03003950 while (entry_ptr->me_value == NULL) {
3951 if (--i < 0) {
3952 goto fail;
3953 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003954 entry_ptr--;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003955 }
3956 key = entry_ptr->me_key;
3957 value = entry_ptr->me_value;
3958 }
3959 di->di_pos = i-1;
3960 di->len--;
3961
Dong-hee Na1b55b652020-02-17 19:09:15 +09003962 if (Py_IS_TYPE(di, &PyDictRevIterKey_Type)) {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003963 Py_INCREF(key);
3964 return key;
3965 }
Dong-hee Na1b55b652020-02-17 19:09:15 +09003966 else if (Py_IS_TYPE(di, &PyDictRevIterValue_Type)) {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003967 Py_INCREF(value);
3968 return value;
3969 }
Dong-hee Na1b55b652020-02-17 19:09:15 +09003970 else if (Py_IS_TYPE(di, &PyDictRevIterItem_Type)) {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003971 Py_INCREF(key);
3972 Py_INCREF(value);
3973 result = di->di_result;
3974 if (Py_REFCNT(result) == 1) {
3975 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3976 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3977 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3978 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
3979 Py_INCREF(result);
3980 Py_DECREF(oldkey);
3981 Py_DECREF(oldvalue);
3982 }
3983 else {
3984 result = PyTuple_New(2);
3985 if (result == NULL) {
3986 return NULL;
3987 }
3988 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3989 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
3990 }
3991 return result;
3992 }
3993 else {
3994 Py_UNREACHABLE();
3995 }
3996
3997fail:
3998 di->di_dict = NULL;
3999 Py_DECREF(d);
4000 return NULL;
4001}
4002
4003PyTypeObject PyDictRevIterKey_Type = {
4004 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4005 "dict_reversekeyiterator",
4006 sizeof(dictiterobject),
4007 .tp_dealloc = (destructor)dictiter_dealloc,
4008 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
4009 .tp_traverse = (traverseproc)dictiter_traverse,
4010 .tp_iter = PyObject_SelfIter,
4011 .tp_iternext = (iternextfunc)dictreviter_iternext,
4012 .tp_methods = dictiter_methods
4013};
4014
4015
4016/*[clinic input]
4017dict.__reversed__
4018
4019Return a reverse iterator over the dict keys.
4020[clinic start generated code]*/
4021
4022static PyObject *
4023dict___reversed___impl(PyDictObject *self)
4024/*[clinic end generated code: output=e674483336d1ed51 input=23210ef3477d8c4d]*/
4025{
4026 assert (PyDict_Check(self));
4027 return dictiter_new(self, &PyDictRevIterKey_Type);
4028}
4029
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004030static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304031dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004032{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004033 _Py_IDENTIFIER(iter);
Sergey Fedoseev63958442018-10-20 05:43:33 +05004034 /* copy the iterator state */
4035 dictiterobject tmp = *di;
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004036 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004037
Sergey Fedoseev63958442018-10-20 05:43:33 +05004038 PyObject *list = PySequence_List((PyObject*)&tmp);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004039 Py_XDECREF(tmp.di_dict);
Sergey Fedoseev63958442018-10-20 05:43:33 +05004040 if (list == NULL) {
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004041 return NULL;
4042 }
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004043 return Py_BuildValue("N(N)", _PyEval_GetBuiltinId(&PyId_iter), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004044}
4045
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004046PyTypeObject PyDictRevIterItem_Type = {
4047 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4048 "dict_reverseitemiterator",
4049 sizeof(dictiterobject),
4050 .tp_dealloc = (destructor)dictiter_dealloc,
4051 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
4052 .tp_traverse = (traverseproc)dictiter_traverse,
4053 .tp_iter = PyObject_SelfIter,
4054 .tp_iternext = (iternextfunc)dictreviter_iternext,
4055 .tp_methods = dictiter_methods
4056};
4057
4058PyTypeObject PyDictRevIterValue_Type = {
4059 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4060 "dict_reversevalueiterator",
4061 sizeof(dictiterobject),
4062 .tp_dealloc = (destructor)dictiter_dealloc,
4063 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
4064 .tp_traverse = (traverseproc)dictiter_traverse,
4065 .tp_iter = PyObject_SelfIter,
4066 .tp_iternext = (iternextfunc)dictreviter_iternext,
4067 .tp_methods = dictiter_methods
4068};
4069
Guido van Rossum3ac67412007-02-10 18:55:06 +00004070/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004071/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00004072/***********************************************/
4073
Guido van Rossumb90c8482007-02-10 01:11:45 +00004074/* The instance lay-out is the same for all three; but the type differs. */
4075
Guido van Rossumb90c8482007-02-10 01:11:45 +00004076static void
Eric Snow96c6af92015-05-29 22:21:39 -06004077dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004078{
INADA Naokia6296d32017-08-24 14:55:17 +09004079 /* bpo-31095: UnTrack is needed before calling any callbacks */
4080 _PyObject_GC_UNTRACK(dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004081 Py_XDECREF(dv->dv_dict);
4082 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00004083}
4084
4085static int
Eric Snow96c6af92015-05-29 22:21:39 -06004086dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00004087{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004088 Py_VISIT(dv->dv_dict);
4089 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004090}
4091
Guido van Rossum83825ac2007-02-10 04:54:19 +00004092static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06004093dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004094{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004095 Py_ssize_t len = 0;
4096 if (dv->dv_dict != NULL)
4097 len = dv->dv_dict->ma_used;
4098 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004099}
4100
Eric Snow96c6af92015-05-29 22:21:39 -06004101PyObject *
4102_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004103{
Eric Snow96c6af92015-05-29 22:21:39 -06004104 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004105 if (dict == NULL) {
4106 PyErr_BadInternalCall();
4107 return NULL;
4108 }
4109 if (!PyDict_Check(dict)) {
4110 /* XXX Get rid of this restriction later */
4111 PyErr_Format(PyExc_TypeError,
4112 "%s() requires a dict argument, not '%s'",
Victor Stinner58ac7002020-02-07 03:04:21 +01004113 type->tp_name, Py_TYPE(dict)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004114 return NULL;
4115 }
Eric Snow96c6af92015-05-29 22:21:39 -06004116 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004117 if (dv == NULL)
4118 return NULL;
4119 Py_INCREF(dict);
4120 dv->dv_dict = (PyDictObject *)dict;
4121 _PyObject_GC_TRACK(dv);
4122 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004123}
4124
Dennis Sweeney3ee0e482020-06-12 13:19:25 -04004125static PyObject *
4126dictview_mapping(PyObject *view)
4127{
4128 assert(view != NULL);
4129 assert(PyDictKeys_Check(view)
4130 || PyDictValues_Check(view)
4131 || PyDictItems_Check(view));
4132 PyObject *mapping = (PyObject *)((_PyDictViewObject *)view)->dv_dict;
4133 return PyDictProxy_New(mapping);
4134}
4135
4136static PyGetSetDef dictview_getset[] = {
4137 {"mapping", (getter)dictview_mapping, (setter)NULL,
4138 "dictionary that this view refers to", NULL},
4139 {0}
4140};
4141
Neal Norwitze36f2ba2007-02-26 23:12:28 +00004142/* TODO(guido): The views objects are not complete:
4143
4144 * support more set operations
4145 * support arbitrary mappings?
4146 - either these should be static or exported in dictobject.h
4147 - if public then they should probably be in builtins
4148*/
4149
Guido van Rossumaac530c2007-08-24 22:33:45 +00004150/* Return 1 if self is a subset of other, iterating over self;
4151 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00004152static int
4153all_contained_in(PyObject *self, PyObject *other)
4154{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004155 PyObject *iter = PyObject_GetIter(self);
4156 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004158 if (iter == NULL)
4159 return -1;
4160 for (;;) {
4161 PyObject *next = PyIter_Next(iter);
4162 if (next == NULL) {
4163 if (PyErr_Occurred())
4164 ok = -1;
4165 break;
4166 }
4167 ok = PySequence_Contains(other, next);
4168 Py_DECREF(next);
4169 if (ok <= 0)
4170 break;
4171 }
4172 Py_DECREF(iter);
4173 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004174}
4175
4176static PyObject *
4177dictview_richcompare(PyObject *self, PyObject *other, int op)
4178{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004179 Py_ssize_t len_self, len_other;
4180 int ok;
4181 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004183 assert(self != NULL);
4184 assert(PyDictViewSet_Check(self));
4185 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00004186
Brian Curtindfc80e32011-08-10 20:28:54 -05004187 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
4188 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004190 len_self = PyObject_Size(self);
4191 if (len_self < 0)
4192 return NULL;
4193 len_other = PyObject_Size(other);
4194 if (len_other < 0)
4195 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004197 ok = 0;
4198 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00004199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004200 case Py_NE:
4201 case Py_EQ:
4202 if (len_self == len_other)
4203 ok = all_contained_in(self, other);
4204 if (op == Py_NE && ok >= 0)
4205 ok = !ok;
4206 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004208 case Py_LT:
4209 if (len_self < len_other)
4210 ok = all_contained_in(self, other);
4211 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004213 case Py_LE:
4214 if (len_self <= len_other)
4215 ok = all_contained_in(self, other);
4216 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004218 case Py_GT:
4219 if (len_self > len_other)
4220 ok = all_contained_in(other, self);
4221 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004223 case Py_GE:
4224 if (len_self >= len_other)
4225 ok = all_contained_in(other, self);
4226 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004228 }
4229 if (ok < 0)
4230 return NULL;
4231 result = ok ? Py_True : Py_False;
4232 Py_INCREF(result);
4233 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004234}
4235
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004236static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004237dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004238{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004239 PyObject *seq;
bennorthd7773d92018-01-26 15:46:01 +00004240 PyObject *result = NULL;
4241 Py_ssize_t rc;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004242
bennorthd7773d92018-01-26 15:46:01 +00004243 rc = Py_ReprEnter((PyObject *)dv);
4244 if (rc != 0) {
4245 return rc > 0 ? PyUnicode_FromString("...") : NULL;
4246 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004247 seq = PySequence_List((PyObject *)dv);
bennorthd7773d92018-01-26 15:46:01 +00004248 if (seq == NULL) {
4249 goto Done;
4250 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004251 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
4252 Py_DECREF(seq);
bennorthd7773d92018-01-26 15:46:01 +00004253
4254Done:
4255 Py_ReprLeave((PyObject *)dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004256 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004257}
4258
Guido van Rossum3ac67412007-02-10 18:55:06 +00004259/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004260
4261static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004262dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004264 if (dv->dv_dict == NULL) {
4265 Py_RETURN_NONE;
4266 }
4267 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004268}
4269
4270static int
Eric Snow96c6af92015-05-29 22:21:39 -06004271dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004272{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004273 if (dv->dv_dict == NULL)
4274 return 0;
4275 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004276}
4277
Guido van Rossum83825ac2007-02-10 04:54:19 +00004278static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004279 (lenfunc)dictview_len, /* sq_length */
4280 0, /* sq_concat */
4281 0, /* sq_repeat */
4282 0, /* sq_item */
4283 0, /* sq_slice */
4284 0, /* sq_ass_item */
4285 0, /* sq_ass_slice */
4286 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004287};
4288
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004289// Create an set object from dictviews object.
4290// Returns a new reference.
4291// This utility function is used by set operations.
Guido van Rossum523259b2007-08-24 23:41:22 +00004292static PyObject*
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004293dictviews_to_set(PyObject *self)
Guido van Rossum523259b2007-08-24 23:41:22 +00004294{
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004295 PyObject *left = self;
4296 if (PyDictKeys_Check(self)) {
4297 // PySet_New() has fast path for the dict object.
4298 PyObject *dict = (PyObject *)((_PyDictViewObject *)self)->dv_dict;
4299 if (PyDict_CheckExact(dict)) {
4300 left = dict;
4301 }
4302 }
4303 return PySet_New(left);
4304}
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004305
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004306static PyObject*
4307dictviews_sub(PyObject *self, PyObject *other)
4308{
4309 PyObject *result = dictviews_to_set(self);
4310 if (result == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004311 return NULL;
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004312 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004313
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004314 _Py_IDENTIFIER(difference_update);
4315 PyObject *tmp = _PyObject_CallMethodIdOneArg(
4316 result, &PyId_difference_update, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004317 if (tmp == NULL) {
4318 Py_DECREF(result);
4319 return NULL;
4320 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004322 Py_DECREF(tmp);
4323 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004324}
4325
Forest Gregg998cf1f2019-08-26 02:17:43 -05004326static int
4327dictitems_contains(_PyDictViewObject *dv, PyObject *obj);
4328
4329PyObject *
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004330_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00004331{
Forest Gregg998cf1f2019-08-26 02:17:43 -05004332 PyObject *result;
4333 PyObject *it;
4334 PyObject *key;
4335 Py_ssize_t len_self;
4336 int rv;
4337 int (*dict_contains)(_PyDictViewObject *, PyObject *);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004338
Forest Gregg998cf1f2019-08-26 02:17:43 -05004339 /* Python interpreter swaps parameters when dict view
4340 is on right side of & */
4341 if (!PyDictViewSet_Check(self)) {
4342 PyObject *tmp = other;
4343 other = self;
4344 self = tmp;
4345 }
4346
4347 len_self = dictview_len((_PyDictViewObject *)self);
4348
4349 /* if other is a set and self is smaller than other,
4350 reuse set intersection logic */
Dong-hee Na1b55b652020-02-17 19:09:15 +09004351 if (Py_IS_TYPE(other, &PySet_Type) && len_self <= PyObject_Size(other)) {
Forest Gregg998cf1f2019-08-26 02:17:43 -05004352 _Py_IDENTIFIER(intersection);
4353 return _PyObject_CallMethodIdObjArgs(other, &PyId_intersection, self, NULL);
4354 }
4355
4356 /* if other is another dict view, and it is bigger than self,
4357 swap them */
4358 if (PyDictViewSet_Check(other)) {
4359 Py_ssize_t len_other = dictview_len((_PyDictViewObject *)other);
4360 if (len_other > len_self) {
4361 PyObject *tmp = other;
4362 other = self;
4363 self = tmp;
4364 }
4365 }
4366
4367 /* at this point, two things should be true
4368 1. self is a dictview
4369 2. if other is a dictview then it is smaller than self */
4370 result = PySet_New(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004371 if (result == NULL)
4372 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004373
Forest Gregg998cf1f2019-08-26 02:17:43 -05004374 it = PyObject_GetIter(other);
Zackery Spytzb16e3822019-10-13 05:49:05 -06004375 if (it == NULL) {
4376 Py_DECREF(result);
4377 return NULL;
4378 }
Forest Gregg998cf1f2019-08-26 02:17:43 -05004379
Forest Gregg998cf1f2019-08-26 02:17:43 -05004380 if (PyDictKeys_Check(self)) {
4381 dict_contains = dictkeys_contains;
4382 }
4383 /* else PyDictItems_Check(self) */
4384 else {
4385 dict_contains = dictitems_contains;
4386 }
4387
4388 while ((key = PyIter_Next(it)) != NULL) {
4389 rv = dict_contains((_PyDictViewObject *)self, key);
4390 if (rv < 0) {
4391 goto error;
4392 }
4393 if (rv) {
4394 if (PySet_Add(result, key)) {
4395 goto error;
4396 }
4397 }
4398 Py_DECREF(key);
4399 }
4400 Py_DECREF(it);
4401 if (PyErr_Occurred()) {
4402 Py_DECREF(result);
4403 return NULL;
4404 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004405 return result;
Forest Gregg998cf1f2019-08-26 02:17:43 -05004406
4407error:
4408 Py_DECREF(it);
4409 Py_DECREF(result);
4410 Py_DECREF(key);
4411 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004412}
4413
4414static PyObject*
4415dictviews_or(PyObject* self, PyObject *other)
4416{
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004417 PyObject *result = dictviews_to_set(self);
4418 if (result == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004419 return NULL;
4420 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004421
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004422 if (_PySet_Update(result, other) < 0) {
4423 Py_DECREF(result);
4424 return NULL;
4425 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004426 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004427}
4428
Dennis Sweeney07d81122020-06-10 01:56:56 -04004429static PyObject *
4430dictitems_xor(PyObject *self, PyObject *other)
4431{
4432 assert(PyDictItems_Check(self));
4433 assert(PyDictItems_Check(other));
4434 PyObject *d1 = (PyObject *)((_PyDictViewObject *)self)->dv_dict;
4435 PyObject *d2 = (PyObject *)((_PyDictViewObject *)other)->dv_dict;
4436
4437 PyObject *temp_dict = PyDict_Copy(d1);
4438 if (temp_dict == NULL) {
4439 return NULL;
4440 }
4441 PyObject *result_set = PySet_New(NULL);
4442 if (result_set == NULL) {
4443 Py_CLEAR(temp_dict);
4444 return NULL;
4445 }
4446
4447 PyObject *key = NULL, *val1 = NULL, *val2 = NULL;
4448 Py_ssize_t pos = 0;
4449 Py_hash_t hash;
4450
4451 while (_PyDict_Next(d2, &pos, &key, &val2, &hash)) {
4452 Py_INCREF(key);
4453 Py_INCREF(val2);
4454 val1 = _PyDict_GetItem_KnownHash(temp_dict, key, hash);
4455
4456 int to_delete;
4457 if (val1 == NULL) {
4458 if (PyErr_Occurred()) {
4459 goto error;
4460 }
4461 to_delete = 0;
4462 }
4463 else {
4464 Py_INCREF(val1);
4465 to_delete = PyObject_RichCompareBool(val1, val2, Py_EQ);
4466 if (to_delete < 0) {
4467 goto error;
4468 }
4469 }
4470
4471 if (to_delete) {
4472 if (_PyDict_DelItem_KnownHash(temp_dict, key, hash) < 0) {
4473 goto error;
4474 }
4475 }
4476 else {
4477 PyObject *pair = PyTuple_Pack(2, key, val2);
4478 if (pair == NULL) {
4479 goto error;
4480 }
4481 if (PySet_Add(result_set, pair) < 0) {
4482 Py_DECREF(pair);
4483 goto error;
4484 }
4485 Py_DECREF(pair);
4486 }
4487 Py_DECREF(key);
4488 Py_XDECREF(val1);
4489 Py_DECREF(val2);
4490 }
4491 key = val1 = val2 = NULL;
4492
4493 _Py_IDENTIFIER(items);
4494 PyObject *remaining_pairs = _PyObject_CallMethodIdNoArgs(temp_dict,
4495 &PyId_items);
4496 if (remaining_pairs == NULL) {
4497 goto error;
4498 }
4499 if (_PySet_Update(result_set, remaining_pairs) < 0) {
4500 Py_DECREF(remaining_pairs);
4501 goto error;
4502 }
4503 Py_DECREF(temp_dict);
4504 Py_DECREF(remaining_pairs);
4505 return result_set;
4506
4507error:
4508 Py_XDECREF(temp_dict);
4509 Py_XDECREF(result_set);
4510 Py_XDECREF(key);
4511 Py_XDECREF(val1);
4512 Py_XDECREF(val2);
4513 return NULL;
4514}
4515
Guido van Rossum523259b2007-08-24 23:41:22 +00004516static PyObject*
4517dictviews_xor(PyObject* self, PyObject *other)
4518{
Dennis Sweeney07d81122020-06-10 01:56:56 -04004519 if (PyDictItems_Check(self) && PyDictItems_Check(other)) {
4520 return dictitems_xor(self, other);
4521 }
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004522 PyObject *result = dictviews_to_set(self);
4523 if (result == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004524 return NULL;
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004525 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004526
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004527 _Py_IDENTIFIER(symmetric_difference_update);
4528 PyObject *tmp = _PyObject_CallMethodIdOneArg(
4529 result, &PyId_symmetric_difference_update, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004530 if (tmp == NULL) {
4531 Py_DECREF(result);
4532 return NULL;
4533 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004535 Py_DECREF(tmp);
4536 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004537}
4538
4539static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004540 0, /*nb_add*/
4541 (binaryfunc)dictviews_sub, /*nb_subtract*/
4542 0, /*nb_multiply*/
4543 0, /*nb_remainder*/
4544 0, /*nb_divmod*/
4545 0, /*nb_power*/
4546 0, /*nb_negative*/
4547 0, /*nb_positive*/
4548 0, /*nb_absolute*/
4549 0, /*nb_bool*/
4550 0, /*nb_invert*/
4551 0, /*nb_lshift*/
4552 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004553 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004554 (binaryfunc)dictviews_xor, /*nb_xor*/
4555 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00004556};
4557
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004558static PyObject*
4559dictviews_isdisjoint(PyObject *self, PyObject *other)
4560{
4561 PyObject *it;
4562 PyObject *item = NULL;
4563
4564 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06004565 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004566 Py_RETURN_TRUE;
4567 else
4568 Py_RETURN_FALSE;
4569 }
4570
4571 /* Iterate over the shorter object (only if other is a set,
4572 * because PySequence_Contains may be expensive otherwise): */
4573 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06004574 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004575 Py_ssize_t len_other = PyObject_Size(other);
4576 if (len_other == -1)
4577 return NULL;
4578
4579 if ((len_other > len_self)) {
4580 PyObject *tmp = other;
4581 other = self;
4582 self = tmp;
4583 }
4584 }
4585
4586 it = PyObject_GetIter(other);
4587 if (it == NULL)
4588 return NULL;
4589
4590 while ((item = PyIter_Next(it)) != NULL) {
4591 int contains = PySequence_Contains(self, item);
4592 Py_DECREF(item);
4593 if (contains == -1) {
4594 Py_DECREF(it);
4595 return NULL;
4596 }
4597
4598 if (contains) {
4599 Py_DECREF(it);
4600 Py_RETURN_FALSE;
4601 }
4602 }
4603 Py_DECREF(it);
4604 if (PyErr_Occurred())
4605 return NULL; /* PyIter_Next raised an exception. */
4606 Py_RETURN_TRUE;
4607}
4608
4609PyDoc_STRVAR(isdisjoint_doc,
4610"Return True if the view and the given iterable have a null intersection.");
4611
Serhiy Storchaka81524022018-11-27 13:05:02 +02004612static PyObject* dictkeys_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored));
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004613
4614PyDoc_STRVAR(reversed_keys_doc,
4615"Return a reverse iterator over the dict keys.");
4616
Guido van Rossumb90c8482007-02-10 01:11:45 +00004617static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004618 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4619 isdisjoint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004620 {"__reversed__", (PyCFunction)(void(*)(void))dictkeys_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004621 reversed_keys_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004622 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004623};
4624
4625PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004626 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4627 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004628 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004629 0, /* tp_itemsize */
4630 /* methods */
4631 (destructor)dictview_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004632 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004633 0, /* tp_getattr */
4634 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004635 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004636 (reprfunc)dictview_repr, /* tp_repr */
4637 &dictviews_as_number, /* tp_as_number */
4638 &dictkeys_as_sequence, /* tp_as_sequence */
4639 0, /* tp_as_mapping */
4640 0, /* tp_hash */
4641 0, /* tp_call */
4642 0, /* tp_str */
4643 PyObject_GenericGetAttr, /* tp_getattro */
4644 0, /* tp_setattro */
4645 0, /* tp_as_buffer */
4646 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4647 0, /* tp_doc */
4648 (traverseproc)dictview_traverse, /* tp_traverse */
4649 0, /* tp_clear */
4650 dictview_richcompare, /* tp_richcompare */
4651 0, /* tp_weaklistoffset */
4652 (getiterfunc)dictkeys_iter, /* tp_iter */
4653 0, /* tp_iternext */
4654 dictkeys_methods, /* tp_methods */
Dennis Sweeney3ee0e482020-06-12 13:19:25 -04004655 .tp_getset = dictview_getset,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004656};
4657
4658static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304659dictkeys_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004660{
Eric Snow96c6af92015-05-29 22:21:39 -06004661 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004662}
4663
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004664static PyObject *
Serhiy Storchaka81524022018-11-27 13:05:02 +02004665dictkeys_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored))
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004666{
4667 if (dv->dv_dict == NULL) {
4668 Py_RETURN_NONE;
4669 }
4670 return dictiter_new(dv->dv_dict, &PyDictRevIterKey_Type);
4671}
4672
Guido van Rossum3ac67412007-02-10 18:55:06 +00004673/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004674
4675static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004676dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004677{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004678 if (dv->dv_dict == NULL) {
4679 Py_RETURN_NONE;
4680 }
4681 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004682}
4683
4684static int
Eric Snow96c6af92015-05-29 22:21:39 -06004685dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004686{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004687 int result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004688 PyObject *key, *value, *found;
4689 if (dv->dv_dict == NULL)
4690 return 0;
4691 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
4692 return 0;
4693 key = PyTuple_GET_ITEM(obj, 0);
4694 value = PyTuple_GET_ITEM(obj, 1);
Raymond Hettinger6692f012016-09-18 21:46:08 -07004695 found = PyDict_GetItemWithError((PyObject *)dv->dv_dict, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004696 if (found == NULL) {
4697 if (PyErr_Occurred())
4698 return -1;
4699 return 0;
4700 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004701 Py_INCREF(found);
Serhiy Storchaka18b711c2019-08-04 14:12:48 +03004702 result = PyObject_RichCompareBool(found, value, Py_EQ);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004703 Py_DECREF(found);
4704 return result;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004705}
4706
Guido van Rossum83825ac2007-02-10 04:54:19 +00004707static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004708 (lenfunc)dictview_len, /* sq_length */
4709 0, /* sq_concat */
4710 0, /* sq_repeat */
4711 0, /* sq_item */
4712 0, /* sq_slice */
4713 0, /* sq_ass_item */
4714 0, /* sq_ass_slice */
4715 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004716};
4717
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004718static PyObject* dictitems_reversed(_PyDictViewObject *dv);
4719
4720PyDoc_STRVAR(reversed_items_doc,
4721"Return a reverse iterator over the dict items.");
4722
Guido van Rossumb90c8482007-02-10 01:11:45 +00004723static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004724 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4725 isdisjoint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004726 {"__reversed__", (PyCFunction)(void(*)(void))dictitems_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004727 reversed_items_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004728 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004729};
4730
4731PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004732 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4733 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004734 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004735 0, /* tp_itemsize */
4736 /* methods */
4737 (destructor)dictview_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004738 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004739 0, /* tp_getattr */
4740 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004741 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004742 (reprfunc)dictview_repr, /* tp_repr */
4743 &dictviews_as_number, /* tp_as_number */
4744 &dictitems_as_sequence, /* tp_as_sequence */
4745 0, /* tp_as_mapping */
4746 0, /* tp_hash */
4747 0, /* tp_call */
4748 0, /* tp_str */
4749 PyObject_GenericGetAttr, /* tp_getattro */
4750 0, /* tp_setattro */
4751 0, /* tp_as_buffer */
4752 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4753 0, /* tp_doc */
4754 (traverseproc)dictview_traverse, /* tp_traverse */
4755 0, /* tp_clear */
4756 dictview_richcompare, /* tp_richcompare */
4757 0, /* tp_weaklistoffset */
4758 (getiterfunc)dictitems_iter, /* tp_iter */
4759 0, /* tp_iternext */
4760 dictitems_methods, /* tp_methods */
Dennis Sweeney3ee0e482020-06-12 13:19:25 -04004761 .tp_getset = dictview_getset,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004762};
4763
4764static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304765dictitems_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004766{
Eric Snow96c6af92015-05-29 22:21:39 -06004767 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004768}
4769
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004770static PyObject *
4771dictitems_reversed(_PyDictViewObject *dv)
4772{
4773 if (dv->dv_dict == NULL) {
4774 Py_RETURN_NONE;
4775 }
4776 return dictiter_new(dv->dv_dict, &PyDictRevIterItem_Type);
4777}
4778
Guido van Rossum3ac67412007-02-10 18:55:06 +00004779/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004780
4781static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004782dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004783{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004784 if (dv->dv_dict == NULL) {
4785 Py_RETURN_NONE;
4786 }
4787 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004788}
4789
Guido van Rossum83825ac2007-02-10 04:54:19 +00004790static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004791 (lenfunc)dictview_len, /* sq_length */
4792 0, /* sq_concat */
4793 0, /* sq_repeat */
4794 0, /* sq_item */
4795 0, /* sq_slice */
4796 0, /* sq_ass_item */
4797 0, /* sq_ass_slice */
4798 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004799};
4800
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004801static PyObject* dictvalues_reversed(_PyDictViewObject *dv);
4802
4803PyDoc_STRVAR(reversed_values_doc,
4804"Return a reverse iterator over the dict values.");
4805
Guido van Rossumb90c8482007-02-10 01:11:45 +00004806static PyMethodDef dictvalues_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004807 {"__reversed__", (PyCFunction)(void(*)(void))dictvalues_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004808 reversed_values_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004809 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004810};
4811
4812PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004813 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4814 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004815 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004816 0, /* tp_itemsize */
4817 /* methods */
4818 (destructor)dictview_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004819 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004820 0, /* tp_getattr */
4821 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004822 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004823 (reprfunc)dictview_repr, /* tp_repr */
4824 0, /* tp_as_number */
4825 &dictvalues_as_sequence, /* tp_as_sequence */
4826 0, /* tp_as_mapping */
4827 0, /* tp_hash */
4828 0, /* tp_call */
4829 0, /* tp_str */
4830 PyObject_GenericGetAttr, /* tp_getattro */
4831 0, /* tp_setattro */
4832 0, /* tp_as_buffer */
4833 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4834 0, /* tp_doc */
4835 (traverseproc)dictview_traverse, /* tp_traverse */
4836 0, /* tp_clear */
4837 0, /* tp_richcompare */
4838 0, /* tp_weaklistoffset */
4839 (getiterfunc)dictvalues_iter, /* tp_iter */
4840 0, /* tp_iternext */
4841 dictvalues_methods, /* tp_methods */
Dennis Sweeney3ee0e482020-06-12 13:19:25 -04004842 .tp_getset = dictview_getset,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004843};
4844
4845static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304846dictvalues_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004847{
Eric Snow96c6af92015-05-29 22:21:39 -06004848 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004849}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004850
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004851static PyObject *
4852dictvalues_reversed(_PyDictViewObject *dv)
4853{
4854 if (dv->dv_dict == NULL) {
4855 Py_RETURN_NONE;
4856 }
4857 return dictiter_new(dv->dv_dict, &PyDictRevIterValue_Type);
4858}
4859
4860
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004861/* Returns NULL if cannot allocate a new PyDictKeysObject,
4862 but does not set an error */
4863PyDictKeysObject *
4864_PyDict_NewKeysForClass(void)
4865{
Victor Stinner742da042016-09-07 17:40:12 -07004866 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004867 if (keys == NULL)
4868 PyErr_Clear();
4869 else
4870 keys->dk_lookup = lookdict_split;
4871 return keys;
4872}
4873
4874#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
4875
4876PyObject *
4877PyObject_GenericGetDict(PyObject *obj, void *context)
4878{
4879 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
4880 if (dictptr == NULL) {
4881 PyErr_SetString(PyExc_AttributeError,
4882 "This object has no __dict__");
4883 return NULL;
4884 }
4885 dict = *dictptr;
4886 if (dict == NULL) {
4887 PyTypeObject *tp = Py_TYPE(obj);
4888 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
INADA Naokia7576492018-11-14 18:39:27 +09004889 dictkeys_incref(CACHED_KEYS(tp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004890 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
4891 }
4892 else {
4893 *dictptr = dict = PyDict_New();
4894 }
4895 }
4896 Py_XINCREF(dict);
4897 return dict;
4898}
4899
4900int
4901_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
Victor Stinner742da042016-09-07 17:40:12 -07004902 PyObject *key, PyObject *value)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004903{
4904 PyObject *dict;
4905 int res;
4906 PyDictKeysObject *cached;
4907
4908 assert(dictptr != NULL);
4909 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
4910 assert(dictptr != NULL);
4911 dict = *dictptr;
4912 if (dict == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +09004913 dictkeys_incref(cached);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004914 dict = new_dict_with_shared_keys(cached);
4915 if (dict == NULL)
4916 return -1;
4917 *dictptr = dict;
4918 }
4919 if (value == NULL) {
4920 res = PyDict_DelItem(dict, key);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004921 // Since key sharing dict doesn't allow deletion, PyDict_DelItem()
4922 // always converts dict to combined form.
4923 if ((cached = CACHED_KEYS(tp)) != NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004924 CACHED_KEYS(tp) = NULL;
INADA Naokia7576492018-11-14 18:39:27 +09004925 dictkeys_decref(cached);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004926 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01004927 }
4928 else {
INADA Naoki2294f3a2017-02-12 13:51:30 +09004929 int was_shared = (cached == ((PyDictObject *)dict)->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004930 res = PyDict_SetItem(dict, key, value);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004931 if (was_shared &&
4932 (cached = CACHED_KEYS(tp)) != NULL &&
4933 cached != ((PyDictObject *)dict)->ma_keys) {
Victor Stinner3d3f2642016-12-15 17:21:23 +01004934 /* PyDict_SetItem() may call dictresize and convert split table
4935 * into combined table. In such case, convert it to split
4936 * table again and update type's shared key only when this is
4937 * the only dict sharing key with the type.
4938 *
4939 * This is to allow using shared key in class like this:
4940 *
4941 * class C:
4942 * def __init__(self):
4943 * # one dict resize happens
4944 * self.a, self.b, self.c = 1, 2, 3
4945 * self.d, self.e, self.f = 4, 5, 6
4946 * a = C()
4947 */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004948 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004949 CACHED_KEYS(tp) = make_keys_shared(dict);
Victor Stinner742da042016-09-07 17:40:12 -07004950 }
4951 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004952 CACHED_KEYS(tp) = NULL;
4953 }
INADA Naokia7576492018-11-14 18:39:27 +09004954 dictkeys_decref(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004955 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
4956 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004957 }
4958 }
4959 } else {
4960 dict = *dictptr;
4961 if (dict == NULL) {
4962 dict = PyDict_New();
4963 if (dict == NULL)
4964 return -1;
4965 *dictptr = dict;
4966 }
4967 if (value == NULL) {
4968 res = PyDict_DelItem(dict, key);
4969 } else {
4970 res = PyDict_SetItem(dict, key, value);
4971 }
4972 }
4973 return res;
4974}
4975
4976void
4977_PyDictKeys_DecRef(PyDictKeysObject *keys)
4978{
INADA Naokia7576492018-11-14 18:39:27 +09004979 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004980}