blob: f3b11571776554ece94ec3b4e93557694460799a [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
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300250#include "clinic/dictobject.c.h"
251
Victor Stinnerae00a5a2020-04-29 02:29:20 +0200252void
Victor Stinnerb4e85ca2020-06-23 11:33:18 +0200253_PyDict_ClearFreeList(PyThreadState *tstate)
Christian Heimes77c02eb2008-02-09 02:18:51 +0000254{
Victor Stinnerb4e85ca2020-06-23 11:33:18 +0200255 struct _Py_dict_state *state = &tstate->interp->dict_state;
256 while (state->numfree) {
257 PyDictObject *op = state->free_list[--state->numfree];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 assert(PyDict_CheckExact(op));
259 PyObject_GC_Del(op);
260 }
Victor Stinnerb4e85ca2020-06-23 11:33:18 +0200261 while (state->keys_numfree) {
262 PyObject_FREE(state->keys_free_list[--state->keys_numfree]);
Victor Stinner742da042016-09-07 17:40:12 -0700263 }
Victor Stinnerb4e85ca2020-06-23 11:33:18 +0200264}
265
266
267void
268_PyDict_Fini(PyThreadState *tstate)
269{
270 _PyDict_ClearFreeList(tstate);
271#ifdef Py_DEBUG
272 PyInterpreterState *interp = _PyInterpreterState_GET();
273 struct _Py_dict_state *state = &interp->dict_state;
274 state->numfree = -1;
275 state->keys_numfree = -1;
Victor Stinnerb4b53862020-05-05 19:55:29 +0200276#endif
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100277}
278
Victor Stinnerb4e85ca2020-06-23 11:33:18 +0200279
David Malcolm49526f42012-06-22 14:55:41 -0400280/* Print summary info about the state of the optimized allocator */
281void
282_PyDict_DebugMallocStats(FILE *out)
283{
Victor Stinnerb4e85ca2020-06-23 11:33:18 +0200284 PyInterpreterState *interp = _PyInterpreterState_GET();
285 struct _Py_dict_state *state = &interp->dict_state;
286 _PyDebugAllocatorStats(out, "free PyDictObject",
287 state->numfree, sizeof(PyDictObject));
David Malcolm49526f42012-06-22 14:55:41 -0400288}
289
290
Victor Stinner742da042016-09-07 17:40:12 -0700291#define DK_SIZE(dk) ((dk)->dk_size)
292#if SIZEOF_VOID_P > 4
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700293#define DK_IXSIZE(dk) \
294 (DK_SIZE(dk) <= 0xff ? \
295 1 : DK_SIZE(dk) <= 0xffff ? \
296 2 : DK_SIZE(dk) <= 0xffffffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700297 4 : sizeof(int64_t))
Victor Stinner742da042016-09-07 17:40:12 -0700298#else
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700299#define DK_IXSIZE(dk) \
300 (DK_SIZE(dk) <= 0xff ? \
301 1 : DK_SIZE(dk) <= 0xffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700302 2 : sizeof(int32_t))
Victor Stinner742da042016-09-07 17:40:12 -0700303#endif
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700304#define DK_ENTRIES(dk) \
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700305 ((PyDictKeyEntry*)(&((int8_t*)((dk)->dk_indices))[DK_SIZE(dk) * DK_IXSIZE(dk)]))
Victor Stinner742da042016-09-07 17:40:12 -0700306
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400307#define DK_MASK(dk) (((dk)->dk_size)-1)
308#define IS_POWER_OF_2(x) (((x) & (x-1)) == 0)
309
INADA Naokia7576492018-11-14 18:39:27 +0900310static void free_keys_object(PyDictKeysObject *keys);
311
312static inline void
313dictkeys_incref(PyDictKeysObject *dk)
314{
Victor Stinner49932fe2020-02-03 17:55:05 +0100315#ifdef Py_REF_DEBUG
316 _Py_RefTotal++;
317#endif
INADA Naokia7576492018-11-14 18:39:27 +0900318 dk->dk_refcnt++;
319}
320
321static inline void
322dictkeys_decref(PyDictKeysObject *dk)
323{
324 assert(dk->dk_refcnt > 0);
Victor Stinner49932fe2020-02-03 17:55:05 +0100325#ifdef Py_REF_DEBUG
326 _Py_RefTotal--;
327#endif
INADA Naokia7576492018-11-14 18:39:27 +0900328 if (--dk->dk_refcnt == 0) {
329 free_keys_object(dk);
330 }
331}
332
Victor Stinner742da042016-09-07 17:40:12 -0700333/* lookup indices. returns DKIX_EMPTY, DKIX_DUMMY, or ix >=0 */
Benjamin Peterson73222252016-09-08 09:58:47 -0700334static inline Py_ssize_t
Andy Lester62d21c92020-03-25 23:13:01 -0500335dictkeys_get_index(const PyDictKeysObject *keys, Py_ssize_t i)
Victor Stinner742da042016-09-07 17:40:12 -0700336{
337 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700338 Py_ssize_t ix;
339
Victor Stinner742da042016-09-07 17:40:12 -0700340 if (s <= 0xff) {
Andy Lester62d21c92020-03-25 23:13:01 -0500341 const int8_t *indices = (const int8_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700342 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700343 }
344 else if (s <= 0xffff) {
Andy Lester62d21c92020-03-25 23:13:01 -0500345 const int16_t *indices = (const int16_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700346 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700347 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700348#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300349 else if (s > 0xffffffff) {
Andy Lester62d21c92020-03-25 23:13:01 -0500350 const int64_t *indices = (const int64_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700351 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700352 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700353#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300354 else {
Andy Lester62d21c92020-03-25 23:13:01 -0500355 const int32_t *indices = (const int32_t*)(keys->dk_indices);
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300356 ix = indices[i];
357 }
Victor Stinner71211e32016-09-08 10:52:46 -0700358 assert(ix >= DKIX_DUMMY);
359 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700360}
361
362/* write to indices. */
Benjamin Peterson73222252016-09-08 09:58:47 -0700363static inline void
INADA Naokia7576492018-11-14 18:39:27 +0900364dictkeys_set_index(PyDictKeysObject *keys, Py_ssize_t i, Py_ssize_t ix)
Victor Stinner742da042016-09-07 17:40:12 -0700365{
366 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700367
368 assert(ix >= DKIX_DUMMY);
369
Victor Stinner742da042016-09-07 17:40:12 -0700370 if (s <= 0xff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700371 int8_t *indices = (int8_t*)(keys->dk_indices);
Victor Stinner71211e32016-09-08 10:52:46 -0700372 assert(ix <= 0x7f);
Victor Stinner208857e2016-09-08 11:35:46 -0700373 indices[i] = (char)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700374 }
375 else if (s <= 0xffff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700376 int16_t *indices = (int16_t*)(keys->dk_indices);
Victor Stinner71211e32016-09-08 10:52:46 -0700377 assert(ix <= 0x7fff);
Victor Stinner208857e2016-09-08 11:35:46 -0700378 indices[i] = (int16_t)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700379 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700380#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300381 else if (s > 0xffffffff) {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700382 int64_t *indices = (int64_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700383 indices[i] = ix;
Victor Stinner742da042016-09-07 17:40:12 -0700384 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700385#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300386 else {
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700387 int32_t *indices = (int32_t*)(keys->dk_indices);
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300388 assert(ix <= 0x7fffffff);
389 indices[i] = (int32_t)ix;
390 }
Victor Stinner742da042016-09-07 17:40:12 -0700391}
392
393
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200394/* USABLE_FRACTION is the maximum dictionary load.
Victor Stinner742da042016-09-07 17:40:12 -0700395 * Increasing this ratio makes dictionaries more dense resulting in more
396 * collisions. Decreasing it improves sparseness at the expense of spreading
397 * indices over more cache lines and at the cost of total memory consumed.
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200398 *
399 * USABLE_FRACTION must obey the following:
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400400 * (0 < USABLE_FRACTION(n) < n) for all n >= 2
401 *
Victor Stinner742da042016-09-07 17:40:12 -0700402 * USABLE_FRACTION should be quick to calculate.
403 * Fractions around 1/2 to 2/3 seem to work well in practice.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400404 */
Victor Stinner742da042016-09-07 17:40:12 -0700405#define USABLE_FRACTION(n) (((n) << 1)/3)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400406
Victor Stinner742da042016-09-07 17:40:12 -0700407/* ESTIMATE_SIZE is reverse function of USABLE_FRACTION.
408 * This can be used to reserve enough size to insert n entries without
409 * resizing.
410 */
INADA Naoki92c50ee2016-11-22 00:57:02 +0900411#define ESTIMATE_SIZE(n) (((n)*3+1) >> 1)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400412
Victor Stinner742da042016-09-07 17:40:12 -0700413/* Alternative fraction that is otherwise close enough to 2n/3 to make
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400414 * little difference. 8 * 2/3 == 8 * 5/8 == 5. 16 * 2/3 == 16 * 5/8 == 10.
415 * 32 * 2/3 = 21, 32 * 5/8 = 20.
416 * Its advantage is that it is faster to compute on machines with slow division.
417 * #define USABLE_FRACTION(n) (((n) >> 1) + ((n) >> 2) - ((n) >> 3))
Victor Stinner742da042016-09-07 17:40:12 -0700418 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400419
Victor Stinnera9f61a52013-07-16 22:17:26 +0200420/* GROWTH_RATE. Growth rate upon hitting maximum load.
INADA Naoki5fbc5112018-04-17 15:53:34 +0900421 * Currently set to used*3.
Victor Stinnera9f61a52013-07-16 22:17:26 +0200422 * This means that dicts double in size when growing without deletions,
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700423 * but have more head room when the number of deletions is on a par with the
INADA Naoki5fbc5112018-04-17 15:53:34 +0900424 * number of insertions. See also bpo-17563 and bpo-33205.
425 *
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700426 * GROWTH_RATE was set to used*4 up to version 3.2.
427 * GROWTH_RATE was set to used*2 in version 3.3.0
INADA Naoki5fbc5112018-04-17 15:53:34 +0900428 * GROWTH_RATE was set to used*2 + capacity/2 in 3.4.0-3.6.0.
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200429 */
INADA Naoki5fbc5112018-04-17 15:53:34 +0900430#define GROWTH_RATE(d) ((d)->ma_used*3)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400431
432#define ENSURE_ALLOWS_DELETIONS(d) \
433 if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \
434 (d)->ma_keys->dk_lookup = lookdict_unicode; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400436
437/* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
438 * (which cannot fail and thus can do no allocation).
439 */
440static PyDictKeysObject empty_keys_struct = {
Serhiy Storchaka97932e42016-09-26 23:01:23 +0300441 1, /* dk_refcnt */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400442 1, /* dk_size */
443 lookdict_split, /* dk_lookup */
444 0, /* dk_usable (immutable) */
Victor Stinner742da042016-09-07 17:40:12 -0700445 0, /* dk_nentries */
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700446 {DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY,
447 DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY}, /* dk_indices */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400448};
449
450static PyObject *empty_values[1] = { NULL };
451
452#define Py_EMPTY_KEYS &empty_keys_struct
453
Victor Stinner611b0fa2016-09-14 15:02:01 +0200454/* Uncomment to check the dict content in _PyDict_CheckConsistency() */
455/* #define DEBUG_PYDICT */
456
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200457#ifdef DEBUG_PYDICT
458# define ASSERT_CONSISTENT(op) assert(_PyDict_CheckConsistency((PyObject *)(op), 1))
459#else
460# define ASSERT_CONSISTENT(op) assert(_PyDict_CheckConsistency((PyObject *)(op), 0))
461#endif
Victor Stinner611b0fa2016-09-14 15:02:01 +0200462
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200463
464int
465_PyDict_CheckConsistency(PyObject *op, int check_content)
Victor Stinner611b0fa2016-09-14 15:02:01 +0200466{
Victor Stinner68762572019-10-07 18:42:01 +0200467#define CHECK(expr) \
468 do { if (!(expr)) { _PyObject_ASSERT_FAILED_MSG(op, Py_STRINGIFY(expr)); } } while (0)
469
470 assert(op != NULL);
471 CHECK(PyDict_Check(op));
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200472 PyDictObject *mp = (PyDictObject *)op;
Victor Stinner50fe3f82018-10-26 18:47:15 +0200473
Victor Stinner611b0fa2016-09-14 15:02:01 +0200474 PyDictKeysObject *keys = mp->ma_keys;
475 int splitted = _PyDict_HasSplitTable(mp);
476 Py_ssize_t usable = USABLE_FRACTION(keys->dk_size);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200477
Victor Stinner68762572019-10-07 18:42:01 +0200478 CHECK(0 <= mp->ma_used && mp->ma_used <= usable);
479 CHECK(IS_POWER_OF_2(keys->dk_size));
480 CHECK(0 <= keys->dk_usable && keys->dk_usable <= usable);
481 CHECK(0 <= keys->dk_nentries && keys->dk_nentries <= usable);
482 CHECK(keys->dk_usable + keys->dk_nentries <= usable);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200483
484 if (!splitted) {
485 /* combined table */
Victor Stinner68762572019-10-07 18:42:01 +0200486 CHECK(keys->dk_refcnt == 1);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200487 }
488
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200489 if (check_content) {
490 PyDictKeyEntry *entries = DK_ENTRIES(keys);
491 Py_ssize_t i;
Victor Stinner611b0fa2016-09-14 15:02:01 +0200492
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200493 for (i=0; i < keys->dk_size; i++) {
494 Py_ssize_t ix = dictkeys_get_index(keys, i);
Victor Stinner68762572019-10-07 18:42:01 +0200495 CHECK(DKIX_DUMMY <= ix && ix <= usable);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200496 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200497
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200498 for (i=0; i < usable; i++) {
499 PyDictKeyEntry *entry = &entries[i];
500 PyObject *key = entry->me_key;
501
502 if (key != NULL) {
503 if (PyUnicode_CheckExact(key)) {
504 Py_hash_t hash = ((PyASCIIObject *)key)->hash;
Victor Stinner68762572019-10-07 18:42:01 +0200505 CHECK(hash != -1);
506 CHECK(entry->me_hash == hash);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200507 }
508 else {
509 /* test_dict fails if PyObject_Hash() is called again */
Victor Stinner68762572019-10-07 18:42:01 +0200510 CHECK(entry->me_hash != -1);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200511 }
512 if (!splitted) {
Victor Stinner68762572019-10-07 18:42:01 +0200513 CHECK(entry->me_value != NULL);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200514 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200515 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200516
517 if (splitted) {
Victor Stinner68762572019-10-07 18:42:01 +0200518 CHECK(entry->me_value == NULL);
Victor Stinner611b0fa2016-09-14 15:02:01 +0200519 }
520 }
521
522 if (splitted) {
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200523 /* splitted table */
524 for (i=0; i < mp->ma_used; i++) {
Victor Stinner68762572019-10-07 18:42:01 +0200525 CHECK(mp->ma_values[i] != NULL);
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200526 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200527 }
528 }
Victor Stinner611b0fa2016-09-14 15:02:01 +0200529 return 1;
Victor Stinner68762572019-10-07 18:42:01 +0200530
531#undef CHECK
Victor Stinner611b0fa2016-09-14 15:02:01 +0200532}
Victor Stinner611b0fa2016-09-14 15:02:01 +0200533
534
Victor Stinnerb4e85ca2020-06-23 11:33:18 +0200535static PyDictKeysObject*
536new_keys_object(Py_ssize_t size)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400537{
538 PyDictKeysObject *dk;
Victor Stinner742da042016-09-07 17:40:12 -0700539 Py_ssize_t es, usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400540
Victor Stinner742da042016-09-07 17:40:12 -0700541 assert(size >= PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400542 assert(IS_POWER_OF_2(size));
Victor Stinner742da042016-09-07 17:40:12 -0700543
544 usable = USABLE_FRACTION(size);
545 if (size <= 0xff) {
546 es = 1;
547 }
548 else if (size <= 0xffff) {
549 es = 2;
550 }
551#if SIZEOF_VOID_P > 4
552 else if (size <= 0xffffffff) {
553 es = 4;
554 }
555#endif
556 else {
557 es = sizeof(Py_ssize_t);
558 }
559
Victor Stinnerb4e85ca2020-06-23 11:33:18 +0200560 PyInterpreterState *interp = _PyInterpreterState_GET();
561 struct _Py_dict_state *state = &interp->dict_state;
562#ifdef Py_DEBUG
563 // new_keys_object() must not be called after _PyDict_Fini()
564 assert(state->keys_numfree != -1);
565#endif
566 if (size == PyDict_MINSIZE && state->keys_numfree > 0) {
567 dk = state->keys_free_list[--state->keys_numfree];
Victor Stinner742da042016-09-07 17:40:12 -0700568 }
Victor Stinnerb4b53862020-05-05 19:55:29 +0200569 else
Victor Stinnerb4b53862020-05-05 19:55:29 +0200570 {
Victor Stinner98ee9d52016-09-08 09:33:56 -0700571 dk = PyObject_MALLOC(sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -0700572 + es * size
573 + sizeof(PyDictKeyEntry) * usable);
Victor Stinner742da042016-09-07 17:40:12 -0700574 if (dk == NULL) {
575 PyErr_NoMemory();
576 return NULL;
577 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400578 }
Victor Stinner49932fe2020-02-03 17:55:05 +0100579#ifdef Py_REF_DEBUG
580 _Py_RefTotal++;
581#endif
INADA Naokia7576492018-11-14 18:39:27 +0900582 dk->dk_refcnt = 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400583 dk->dk_size = size;
Victor Stinner742da042016-09-07 17:40:12 -0700584 dk->dk_usable = usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400585 dk->dk_lookup = lookdict_unicode_nodummy;
Victor Stinner742da042016-09-07 17:40:12 -0700586 dk->dk_nentries = 0;
Gregory P. Smith397f1b22018-04-19 22:41:19 -0700587 memset(&dk->dk_indices[0], 0xff, es * size);
Victor Stinner742da042016-09-07 17:40:12 -0700588 memset(DK_ENTRIES(dk), 0, sizeof(PyDictKeyEntry) * usable);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400589 return dk;
590}
591
592static void
593free_keys_object(PyDictKeysObject *keys)
594{
Victor Stinner742da042016-09-07 17:40:12 -0700595 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400596 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -0700597 for (i = 0, n = keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400598 Py_XDECREF(entries[i].me_key);
599 Py_XDECREF(entries[i].me_value);
600 }
Victor Stinnerb4e85ca2020-06-23 11:33:18 +0200601 PyInterpreterState *interp = _PyInterpreterState_GET();
602 struct _Py_dict_state *state = &interp->dict_state;
603#ifdef Py_DEBUG
604 // free_keys_object() must not be called after _PyDict_Fini()
605 assert(state->keys_numfree != -1);
606#endif
607 if (keys->dk_size == PyDict_MINSIZE && state->keys_numfree < PyDict_MAXFREELIST) {
608 state->keys_free_list[state->keys_numfree++] = keys;
Victor Stinner742da042016-09-07 17:40:12 -0700609 return;
610 }
Raymond Hettingerce5179f2016-01-31 08:56:21 -0800611 PyObject_FREE(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400612}
613
614#define new_values(size) PyMem_NEW(PyObject *, size)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400615#define free_values(values) PyMem_FREE(values)
616
617/* Consumes a reference to the keys object */
618static PyObject *
619new_dict(PyDictKeysObject *keys, PyObject **values)
620{
621 PyDictObject *mp;
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200622 assert(keys != NULL);
Victor Stinnerb4e85ca2020-06-23 11:33:18 +0200623 PyInterpreterState *interp = _PyInterpreterState_GET();
624 struct _Py_dict_state *state = &interp->dict_state;
625#ifdef Py_DEBUG
626 // new_dict() must not be called after _PyDict_Fini()
627 assert(state->numfree != -1);
628#endif
629 if (state->numfree) {
630 mp = state->free_list[--state->numfree];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 assert (mp != NULL);
Dong-hee Na1b55b652020-02-17 19:09:15 +0900632 assert (Py_IS_TYPE(mp, &PyDict_Type));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 }
Victor Stinnerb4e85ca2020-06-23 11:33:18 +0200635 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400636 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
637 if (mp == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +0900638 dictkeys_decref(keys);
Zackery Spytz3d07c1e2019-03-23 20:23:29 -0600639 if (values != empty_values) {
640 free_values(values);
641 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400642 return NULL;
643 }
644 }
645 mp->ma_keys = keys;
646 mp->ma_values = values;
647 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700648 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200649 ASSERT_CONSISTENT(mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000651}
652
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400653/* Consumes a reference to the keys object */
654static PyObject *
655new_dict_with_shared_keys(PyDictKeysObject *keys)
656{
657 PyObject **values;
658 Py_ssize_t i, size;
659
Victor Stinner742da042016-09-07 17:40:12 -0700660 size = USABLE_FRACTION(DK_SIZE(keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400661 values = new_values(size);
662 if (values == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +0900663 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400664 return PyErr_NoMemory();
665 }
666 for (i = 0; i < size; i++) {
667 values[i] = NULL;
668 }
669 return new_dict(keys, values);
670}
671
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500672
673static PyObject *
674clone_combined_dict(PyDictObject *orig)
675{
676 assert(PyDict_CheckExact(orig));
677 assert(orig->ma_values == NULL);
678 assert(orig->ma_keys->dk_refcnt == 1);
679
680 Py_ssize_t keys_size = _PyDict_KeysSize(orig->ma_keys);
681 PyDictKeysObject *keys = PyObject_Malloc(keys_size);
682 if (keys == NULL) {
683 PyErr_NoMemory();
684 return NULL;
685 }
686
687 memcpy(keys, orig->ma_keys, keys_size);
688
689 /* After copying key/value pairs, we need to incref all
690 keys and values and they are about to be co-owned by a
691 new dict object. */
692 PyDictKeyEntry *ep0 = DK_ENTRIES(keys);
693 Py_ssize_t n = keys->dk_nentries;
694 for (Py_ssize_t i = 0; i < n; i++) {
695 PyDictKeyEntry *entry = &ep0[i];
696 PyObject *value = entry->me_value;
697 if (value != NULL) {
698 Py_INCREF(value);
699 Py_INCREF(entry->me_key);
700 }
701 }
702
703 PyDictObject *new = (PyDictObject *)new_dict(keys, NULL);
704 if (new == NULL) {
705 /* In case of an error, `new_dict()` takes care of
706 cleaning up `keys`. */
707 return NULL;
708 }
709 new->ma_used = orig->ma_used;
Victor Stinner0fc91ee2019-04-12 21:51:34 +0200710 ASSERT_CONSISTENT(new);
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500711 if (_PyObject_GC_IS_TRACKED(orig)) {
712 /* Maintain tracking. */
713 _PyObject_GC_TRACK(new);
714 }
Yury Selivanov0b752282018-07-06 12:20:07 -0400715
716 /* Since we copied the keys table we now have an extra reference
Victor Stinner49932fe2020-02-03 17:55:05 +0100717 in the system. Manually call increment _Py_RefTotal to signal that
INADA Naokia7576492018-11-14 18:39:27 +0900718 we have it now; calling dictkeys_incref would be an error as
Yury Selivanov0b752282018-07-06 12:20:07 -0400719 keys->dk_refcnt is already set to 1 (after memcpy). */
Victor Stinner49932fe2020-02-03 17:55:05 +0100720#ifdef Py_REF_DEBUG
721 _Py_RefTotal++;
722#endif
Yury Selivanov0b752282018-07-06 12:20:07 -0400723
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500724 return (PyObject *)new;
725}
726
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400727PyObject *
728PyDict_New(void)
729{
Inada Naokif2a18672019-03-12 17:25:44 +0900730 dictkeys_incref(Py_EMPTY_KEYS);
731 return new_dict(Py_EMPTY_KEYS, empty_values);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400732}
733
Victor Stinner742da042016-09-07 17:40:12 -0700734/* Search index of hash table from offset of entry table */
735static Py_ssize_t
736lookdict_index(PyDictKeysObject *k, Py_hash_t hash, Py_ssize_t index)
737{
Victor Stinner742da042016-09-07 17:40:12 -0700738 size_t mask = DK_MASK(k);
INADA Naoki073ae482017-06-23 15:22:50 +0900739 size_t perturb = (size_t)hash;
740 size_t i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700741
INADA Naoki073ae482017-06-23 15:22:50 +0900742 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900743 Py_ssize_t ix = dictkeys_get_index(k, i);
Victor Stinner742da042016-09-07 17:40:12 -0700744 if (ix == index) {
745 return i;
746 }
747 if (ix == DKIX_EMPTY) {
748 return DKIX_EMPTY;
749 }
INADA Naoki073ae482017-06-23 15:22:50 +0900750 perturb >>= PERTURB_SHIFT;
751 i = mask & (i*5 + perturb + 1);
Victor Stinner742da042016-09-07 17:40:12 -0700752 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700753 Py_UNREACHABLE();
Victor Stinner742da042016-09-07 17:40:12 -0700754}
755
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000756/*
757The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000758This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000759Open addressing is preferred over chaining since the link overhead for
760chaining would be substantial (100% with typical malloc overhead).
761
Tim Peterseb28ef22001-06-02 05:27:19 +0000762The initial probe index is computed as hash mod the table size. Subsequent
763probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000764
765All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000766
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000767The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000768contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000769Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000770
Victor Stinner742da042016-09-07 17:40:12 -0700771lookdict() is general-purpose, and may return DKIX_ERROR if (and only if) a
Victor Stinnera4348cc2016-09-08 12:01:25 -0700772comparison raises an exception.
Guido van Rossum89d8c602007-09-18 17:26:56 +0000773lookdict_unicode() below is specialized to string keys, comparison of which can
INADA Naoki1b8df102017-02-20 22:48:10 +0900774never raise an exception; that function can never return DKIX_ERROR when key
775is string. Otherwise, it falls back to lookdict().
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400776lookdict_unicode_nodummy is further specialized for string keys that cannot be
777the <dummy> value.
INADA Naoki778928b2017-08-03 23:45:15 +0900778For both, when the key isn't found a DKIX_EMPTY is returned.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000779*/
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100780static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400781lookdict(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900782 Py_hash_t hash, PyObject **value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000783{
INADA Naoki778928b2017-08-03 23:45:15 +0900784 size_t i, mask, perturb;
Victor Stinner742da042016-09-07 17:40:12 -0700785 PyDictKeysObject *dk;
INADA Naoki778928b2017-08-03 23:45:15 +0900786 PyDictKeyEntry *ep0;
Tim Peterseb28ef22001-06-02 05:27:19 +0000787
Antoine Pitrou9a234902012-05-13 20:48:01 +0200788top:
Victor Stinner742da042016-09-07 17:40:12 -0700789 dk = mp->ma_keys;
Victor Stinner742da042016-09-07 17:40:12 -0700790 ep0 = DK_ENTRIES(dk);
INADA Naoki778928b2017-08-03 23:45:15 +0900791 mask = DK_MASK(dk);
792 perturb = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700794
INADA Naoki778928b2017-08-03 23:45:15 +0900795 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900796 Py_ssize_t ix = dictkeys_get_index(dk, i);
Victor Stinner742da042016-09-07 17:40:12 -0700797 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700798 *value_addr = NULL;
799 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400800 }
INADA Naoki778928b2017-08-03 23:45:15 +0900801 if (ix >= 0) {
802 PyDictKeyEntry *ep = &ep0[ix];
803 assert(ep->me_key != NULL);
804 if (ep->me_key == key) {
805 *value_addr = ep->me_value;
806 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700807 }
INADA Naoki778928b2017-08-03 23:45:15 +0900808 if (ep->me_hash == hash) {
809 PyObject *startkey = ep->me_key;
810 Py_INCREF(startkey);
811 int cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
812 Py_DECREF(startkey);
813 if (cmp < 0) {
814 *value_addr = NULL;
815 return DKIX_ERROR;
816 }
817 if (dk == mp->ma_keys && ep->me_key == startkey) {
818 if (cmp > 0) {
819 *value_addr = ep->me_value;
820 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700821 }
INADA Naoki778928b2017-08-03 23:45:15 +0900822 }
823 else {
824 /* The dict was mutated, restart */
825 goto top;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400826 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 }
INADA Naoki778928b2017-08-03 23:45:15 +0900829 perturb >>= PERTURB_SHIFT;
830 i = (i*5 + perturb + 1) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700832 Py_UNREACHABLE();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000833}
834
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400835/* Specialized version for string-only keys */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100836static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400837lookdict_unicode(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900838 Py_hash_t hash, PyObject **value_addr)
Fred Drake1bff34a2000-08-31 19:31:38 +0000839{
Victor Stinner742da042016-09-07 17:40:12 -0700840 assert(mp->ma_values == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 /* Make sure this function doesn't have to handle non-unicode keys,
842 including subclasses of str; e.g., one reason to subclass
843 unicodes is to override __eq__, and for speed we don't cater to
844 that here. */
845 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400846 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900847 return lookdict(mp, key, hash, value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 }
Tim Peters15d49292001-05-27 07:39:22 +0000849
INADA Naoki778928b2017-08-03 23:45:15 +0900850 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
851 size_t mask = DK_MASK(mp->ma_keys);
852 size_t perturb = (size_t)hash;
853 size_t i = (size_t)hash & mask;
854
855 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900856 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700857 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700858 *value_addr = NULL;
859 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400860 }
INADA Naoki778928b2017-08-03 23:45:15 +0900861 if (ix >= 0) {
862 PyDictKeyEntry *ep = &ep0[ix];
863 assert(ep->me_key != NULL);
864 assert(PyUnicode_CheckExact(ep->me_key));
865 if (ep->me_key == key ||
866 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
867 *value_addr = ep->me_value;
868 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700869 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400870 }
INADA Naoki778928b2017-08-03 23:45:15 +0900871 perturb >>= PERTURB_SHIFT;
872 i = mask & (i*5 + perturb + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700874 Py_UNREACHABLE();
Fred Drake1bff34a2000-08-31 19:31:38 +0000875}
876
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400877/* Faster version of lookdict_unicode when it is known that no <dummy> keys
878 * will be present. */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100879static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400880lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900881 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400882{
Victor Stinner742da042016-09-07 17:40:12 -0700883 assert(mp->ma_values == NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400884 /* Make sure this function doesn't have to handle non-unicode keys,
885 including subclasses of str; e.g., one reason to subclass
886 unicodes is to override __eq__, and for speed we don't cater to
887 that here. */
888 if (!PyUnicode_CheckExact(key)) {
889 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900890 return lookdict(mp, key, hash, value_addr);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400891 }
INADA Naoki778928b2017-08-03 23:45:15 +0900892
893 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
894 size_t mask = DK_MASK(mp->ma_keys);
895 size_t perturb = (size_t)hash;
896 size_t i = (size_t)hash & mask;
897
898 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900899 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700900 assert (ix != DKIX_DUMMY);
901 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700902 *value_addr = NULL;
903 return DKIX_EMPTY;
904 }
INADA Naoki778928b2017-08-03 23:45:15 +0900905 PyDictKeyEntry *ep = &ep0[ix];
906 assert(ep->me_key != NULL);
907 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700908 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400909 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900910 *value_addr = ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700911 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400912 }
INADA Naoki778928b2017-08-03 23:45:15 +0900913 perturb >>= PERTURB_SHIFT;
914 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400915 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700916 Py_UNREACHABLE();
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400917}
918
919/* Version of lookdict for split tables.
920 * All split tables and only split tables use this lookup function.
921 * Split tables only contain unicode keys and no dummy keys,
922 * so algorithm is the same as lookdict_unicode_nodummy.
923 */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100924static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400925lookdict_split(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900926 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400927{
Victor Stinner742da042016-09-07 17:40:12 -0700928 /* mp must split table */
929 assert(mp->ma_values != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400930 if (!PyUnicode_CheckExact(key)) {
INADA Naoki778928b2017-08-03 23:45:15 +0900931 Py_ssize_t ix = lookdict(mp, key, hash, value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700932 if (ix >= 0) {
INADA Naokiba609772016-12-07 20:41:42 +0900933 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700934 }
935 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400936 }
Victor Stinner742da042016-09-07 17:40:12 -0700937
INADA Naoki778928b2017-08-03 23:45:15 +0900938 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
939 size_t mask = DK_MASK(mp->ma_keys);
940 size_t perturb = (size_t)hash;
941 size_t i = (size_t)hash & mask;
942
943 for (;;) {
INADA Naokia7576492018-11-14 18:39:27 +0900944 Py_ssize_t ix = dictkeys_get_index(mp->ma_keys, i);
INADA Naoki778928b2017-08-03 23:45:15 +0900945 assert (ix != DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -0700946 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700947 *value_addr = NULL;
948 return DKIX_EMPTY;
949 }
INADA Naoki778928b2017-08-03 23:45:15 +0900950 PyDictKeyEntry *ep = &ep0[ix];
951 assert(ep->me_key != NULL);
952 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700953 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400954 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900955 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700956 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400957 }
INADA Naoki778928b2017-08-03 23:45:15 +0900958 perturb >>= PERTURB_SHIFT;
959 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400960 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700961 Py_UNREACHABLE();
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400962}
963
Benjamin Petersonfb886362010-04-24 18:21:17 +0000964int
965_PyDict_HasOnlyStringKeys(PyObject *dict)
966{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 Py_ssize_t pos = 0;
968 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000969 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400971 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 return 1;
973 while (PyDict_Next(dict, &pos, &key, &value))
974 if (!PyUnicode_Check(key))
975 return 0;
976 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000977}
978
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000979#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 do { \
981 if (!_PyObject_GC_IS_TRACKED(mp)) { \
982 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
983 _PyObject_GC_MAY_BE_TRACKED(value)) { \
984 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 } \
986 } \
987 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000988
989void
990_PyDict_MaybeUntrack(PyObject *op)
991{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 PyDictObject *mp;
993 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -0700994 Py_ssize_t i, numentries;
995 PyDictKeyEntry *ep0;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
998 return;
999
1000 mp = (PyDictObject *) op;
Victor Stinner742da042016-09-07 17:40:12 -07001001 ep0 = DK_ENTRIES(mp->ma_keys);
1002 numentries = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001003 if (_PyDict_HasSplitTable(mp)) {
Victor Stinner742da042016-09-07 17:40:12 -07001004 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001005 if ((value = mp->ma_values[i]) == NULL)
1006 continue;
1007 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
Victor Stinner742da042016-09-07 17:40:12 -07001008 assert(!_PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001009 return;
1010 }
1011 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001013 else {
Victor Stinner742da042016-09-07 17:40:12 -07001014 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001015 if ((value = ep0[i].me_value) == NULL)
1016 continue;
1017 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
1018 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
1019 return;
1020 }
1021 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001023}
1024
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001025/* Internal function to find slot for an item from its hash
Victor Stinner3c336c52016-09-12 14:17:40 +02001026 when it is known that the key is not present in the dict.
1027
1028 The dict must be combined. */
INADA Naokiba609772016-12-07 20:41:42 +09001029static Py_ssize_t
INADA Naoki778928b2017-08-03 23:45:15 +09001030find_empty_slot(PyDictKeysObject *keys, Py_hash_t hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001031{
INADA Naoki778928b2017-08-03 23:45:15 +09001032 assert(keys != NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001033
INADA Naoki778928b2017-08-03 23:45:15 +09001034 const size_t mask = DK_MASK(keys);
1035 size_t i = hash & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001036 Py_ssize_t ix = dictkeys_get_index(keys, i);
INADA Naoki778928b2017-08-03 23:45:15 +09001037 for (size_t perturb = hash; ix >= 0;) {
INADA Naoki267941c2016-10-06 15:19:07 +09001038 perturb >>= PERTURB_SHIFT;
INADA Naoki778928b2017-08-03 23:45:15 +09001039 i = (i*5 + perturb + 1) & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001040 ix = dictkeys_get_index(keys, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 }
INADA Naoki778928b2017-08-03 23:45:15 +09001042 return i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001043}
1044
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001045static int
1046insertion_resize(PyDictObject *mp)
1047{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -07001048 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001049}
Antoine Pitroue965d972012-02-27 00:45:12 +01001050
1051/*
1052Internal routine to insert a new item into the table.
1053Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +01001054Returns -1 if an error occurred, or 0 on success.
1055*/
1056static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001057insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001058{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001059 PyObject *old_value;
INADA Naokiba609772016-12-07 20:41:42 +09001060 PyDictKeyEntry *ep;
Antoine Pitroue965d972012-02-27 00:45:12 +01001061
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001062 Py_INCREF(key);
1063 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001064 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
1065 if (insertion_resize(mp) < 0)
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001066 goto Fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001067 }
1068
INADA Naoki778928b2017-08-03 23:45:15 +09001069 Py_ssize_t ix = mp->ma_keys->dk_lookup(mp, key, hash, &old_value);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001070 if (ix == DKIX_ERROR)
1071 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001072
Antoine Pitroud6967322014-10-18 00:35:00 +02001073 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001074 MAINTAIN_TRACKING(mp, key, value);
Victor Stinner742da042016-09-07 17:40:12 -07001075
1076 /* When insertion order is different from shared key, we can't share
1077 * the key anymore. Convert this instance to combine table.
1078 */
1079 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09001080 ((ix >= 0 && old_value == NULL && mp->ma_used != ix) ||
Victor Stinner742da042016-09-07 17:40:12 -07001081 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001082 if (insertion_resize(mp) < 0)
1083 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001084 ix = DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001085 }
Victor Stinner742da042016-09-07 17:40:12 -07001086
1087 if (ix == DKIX_EMPTY) {
1088 /* Insert into new slot. */
INADA Naokiba609772016-12-07 20:41:42 +09001089 assert(old_value == NULL);
Victor Stinner742da042016-09-07 17:40:12 -07001090 if (mp->ma_keys->dk_usable <= 0) {
1091 /* Need to resize. */
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001092 if (insertion_resize(mp) < 0)
1093 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001094 }
INADA Naoki778928b2017-08-03 23:45:15 +09001095 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naokiba609772016-12-07 20:41:42 +09001096 ep = &DK_ENTRIES(mp->ma_keys)[mp->ma_keys->dk_nentries];
INADA Naokia7576492018-11-14 18:39:27 +09001097 dictkeys_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Victor Stinner742da042016-09-07 17:40:12 -07001098 ep->me_key = key;
1099 ep->me_hash = hash;
1100 if (mp->ma_values) {
1101 assert (mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
1102 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001103 }
1104 else {
Victor Stinner742da042016-09-07 17:40:12 -07001105 ep->me_value = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001106 }
1107 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001108 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07001109 mp->ma_keys->dk_usable--;
1110 mp->ma_keys->dk_nentries++;
1111 assert(mp->ma_keys->dk_usable >= 0);
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001112 ASSERT_CONSISTENT(mp);
Victor Stinner742da042016-09-07 17:40:12 -07001113 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001114 }
Victor Stinner742da042016-09-07 17:40:12 -07001115
Inada Naoki91234a12019-06-03 21:30:58 +09001116 if (old_value != value) {
1117 if (_PyDict_HasSplitTable(mp)) {
1118 mp->ma_values[ix] = value;
1119 if (old_value == NULL) {
1120 /* pending state */
1121 assert(ix == mp->ma_used);
1122 mp->ma_used++;
1123 }
INADA Naokiba609772016-12-07 20:41:42 +09001124 }
Inada Naoki91234a12019-06-03 21:30:58 +09001125 else {
1126 assert(old_value != NULL);
1127 DK_ENTRIES(mp->ma_keys)[ix].me_value = value;
1128 }
1129 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokiba609772016-12-07 20:41:42 +09001130 }
INADA Naokiba609772016-12-07 20:41:42 +09001131 Py_XDECREF(old_value); /* which **CAN** re-enter (see issue #22653) */
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001132 ASSERT_CONSISTENT(mp);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001133 Py_DECREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001134 return 0;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001135
1136Fail:
1137 Py_DECREF(value);
1138 Py_DECREF(key);
1139 return -1;
Antoine Pitroue965d972012-02-27 00:45:12 +01001140}
1141
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001142// Same to insertdict but specialized for ma_keys = Py_EMPTY_KEYS.
1143static int
1144insert_to_emptydict(PyDictObject *mp, PyObject *key, Py_hash_t hash,
1145 PyObject *value)
1146{
1147 assert(mp->ma_keys == Py_EMPTY_KEYS);
1148
1149 PyDictKeysObject *newkeys = new_keys_object(PyDict_MINSIZE);
1150 if (newkeys == NULL) {
1151 return -1;
1152 }
1153 if (!PyUnicode_CheckExact(key)) {
1154 newkeys->dk_lookup = lookdict;
1155 }
1156 dictkeys_decref(Py_EMPTY_KEYS);
1157 mp->ma_keys = newkeys;
1158 mp->ma_values = NULL;
1159
1160 Py_INCREF(key);
1161 Py_INCREF(value);
1162 MAINTAIN_TRACKING(mp, key, value);
1163
1164 size_t hashpos = (size_t)hash & (PyDict_MINSIZE-1);
Dong-hee Nac39d1dd2019-10-11 17:43:11 +09001165 PyDictKeyEntry *ep = DK_ENTRIES(mp->ma_keys);
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001166 dictkeys_set_index(mp->ma_keys, hashpos, 0);
1167 ep->me_key = key;
1168 ep->me_hash = hash;
1169 ep->me_value = value;
1170 mp->ma_used++;
1171 mp->ma_version_tag = DICT_NEXT_VERSION();
1172 mp->ma_keys->dk_usable--;
1173 mp->ma_keys->dk_nentries++;
1174 return 0;
1175}
1176
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001177/*
luzpaza5293b42017-11-05 07:37:50 -06001178Internal routine used by dictresize() to build a hashtable of entries.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001179*/
1180static void
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001181build_indices(PyDictKeysObject *keys, PyDictKeyEntry *ep, Py_ssize_t n)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001182{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001183 size_t mask = (size_t)DK_SIZE(keys) - 1;
1184 for (Py_ssize_t ix = 0; ix != n; ix++, ep++) {
1185 Py_hash_t hash = ep->me_hash;
1186 size_t i = hash & mask;
INADA Naokia7576492018-11-14 18:39:27 +09001187 for (size_t perturb = hash; dictkeys_get_index(keys, i) != DKIX_EMPTY;) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001188 perturb >>= PERTURB_SHIFT;
INADA Naoki870c2862017-06-24 09:03:19 +09001189 i = mask & (i*5 + perturb + 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001190 }
INADA Naokia7576492018-11-14 18:39:27 +09001191 dictkeys_set_index(keys, i, ix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001193}
1194
1195/*
1196Restructure the table by allocating a new table and reinserting all
1197items again. When entries have been deleted, the new table may
1198actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001199If a table is split (its keys and hashes are shared, its values are not),
1200then the values are temporarily copied into the table, it is resized as
1201a combined table, then the me_value slots in the old table are NULLed out.
1202After resizing a table is always combined,
1203but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001204*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001205static int
Victor Stinner3d3f2642016-12-15 17:21:23 +01001206dictresize(PyDictObject *mp, Py_ssize_t minsize)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001207{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001208 Py_ssize_t newsize, numentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001209 PyDictKeysObject *oldkeys;
1210 PyObject **oldvalues;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001211 PyDictKeyEntry *oldentries, *newentries;
Tim Peters91a364d2001-05-19 07:04:38 +00001212
Victor Stinner742da042016-09-07 17:40:12 -07001213 /* Find the smallest table size > minused. */
1214 for (newsize = PyDict_MINSIZE;
Victor Stinner3d3f2642016-12-15 17:21:23 +01001215 newsize < minsize && newsize > 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 newsize <<= 1)
1217 ;
1218 if (newsize <= 0) {
1219 PyErr_NoMemory();
1220 return -1;
1221 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001222
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001223 oldkeys = mp->ma_keys;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001224
1225 /* NOTE: Current odict checks mp->ma_keys to detect resize happen.
1226 * So we can't reuse oldkeys even if oldkeys->dk_size == newsize.
1227 * TODO: Try reusing oldkeys when reimplement odict.
1228 */
1229
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001230 /* Allocate a new table. */
1231 mp->ma_keys = new_keys_object(newsize);
1232 if (mp->ma_keys == NULL) {
1233 mp->ma_keys = oldkeys;
1234 return -1;
1235 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01001236 // New table must be large enough.
1237 assert(mp->ma_keys->dk_usable >= mp->ma_used);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001238 if (oldkeys->dk_lookup == lookdict)
1239 mp->ma_keys->dk_lookup = lookdict;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001240
1241 numentries = mp->ma_used;
1242 oldentries = DK_ENTRIES(oldkeys);
1243 newentries = DK_ENTRIES(mp->ma_keys);
1244 oldvalues = mp->ma_values;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001245 if (oldvalues != NULL) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001246 /* Convert split table into new combined table.
1247 * We must incref keys; we can transfer values.
1248 * Note that values of split table is always dense.
1249 */
1250 for (Py_ssize_t i = 0; i < numentries; i++) {
1251 assert(oldvalues[i] != NULL);
1252 PyDictKeyEntry *ep = &oldentries[i];
1253 PyObject *key = ep->me_key;
1254 Py_INCREF(key);
1255 newentries[i].me_key = key;
1256 newentries[i].me_hash = ep->me_hash;
1257 newentries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001259
INADA Naokia7576492018-11-14 18:39:27 +09001260 dictkeys_decref(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001261 mp->ma_values = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001262 if (oldvalues != empty_values) {
1263 free_values(oldvalues);
1264 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001265 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001266 else { // combined table.
1267 if (oldkeys->dk_nentries == numentries) {
1268 memcpy(newentries, oldentries, numentries * sizeof(PyDictKeyEntry));
1269 }
1270 else {
1271 PyDictKeyEntry *ep = oldentries;
1272 for (Py_ssize_t i = 0; i < numentries; i++) {
1273 while (ep->me_value == NULL)
1274 ep++;
1275 newentries[i] = *ep++;
1276 }
1277 }
1278
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001279 assert(oldkeys->dk_lookup != lookdict_split);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001280 assert(oldkeys->dk_refcnt == 1);
Victor Stinner49932fe2020-02-03 17:55:05 +01001281#ifdef Py_REF_DEBUG
1282 _Py_RefTotal--;
1283#endif
Victor Stinnerb4e85ca2020-06-23 11:33:18 +02001284 PyInterpreterState *interp = _PyInterpreterState_GET();
1285 struct _Py_dict_state *state = &interp->dict_state;
1286#ifdef Py_DEBUG
1287 // dictresize() must not be called after _PyDict_Fini()
1288 assert(state->keys_numfree != -1);
Victor Stinnerb4b53862020-05-05 19:55:29 +02001289#endif
Victor Stinnerb4e85ca2020-06-23 11:33:18 +02001290 if (oldkeys->dk_size == PyDict_MINSIZE &&
1291 state->keys_numfree < PyDict_MAXFREELIST)
Victor Stinnerb4b53862020-05-05 19:55:29 +02001292 {
Victor Stinnerb4e85ca2020-06-23 11:33:18 +02001293 state->keys_free_list[state->keys_numfree++] = oldkeys;
1294 }
1295 else {
INADA Naokia7576492018-11-14 18:39:27 +09001296 PyObject_FREE(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001297 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001298 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001299
1300 build_indices(mp->ma_keys, newentries, numentries);
1301 mp->ma_keys->dk_usable -= numentries;
1302 mp->ma_keys->dk_nentries = numentries;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001304}
1305
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001306/* Returns NULL if unable to split table.
1307 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001308static PyDictKeysObject *
1309make_keys_shared(PyObject *op)
1310{
1311 Py_ssize_t i;
1312 Py_ssize_t size;
1313 PyDictObject *mp = (PyDictObject *)op;
1314
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001315 if (!PyDict_CheckExact(op))
1316 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001317 if (!_PyDict_HasSplitTable(mp)) {
1318 PyDictKeyEntry *ep0;
1319 PyObject **values;
1320 assert(mp->ma_keys->dk_refcnt == 1);
1321 if (mp->ma_keys->dk_lookup == lookdict) {
1322 return NULL;
1323 }
1324 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1325 /* Remove dummy keys */
1326 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1327 return NULL;
1328 }
1329 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1330 /* Copy values into a new array */
Victor Stinner742da042016-09-07 17:40:12 -07001331 ep0 = DK_ENTRIES(mp->ma_keys);
1332 size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001333 values = new_values(size);
1334 if (values == NULL) {
1335 PyErr_SetString(PyExc_MemoryError,
1336 "Not enough memory to allocate new values array");
1337 return NULL;
1338 }
1339 for (i = 0; i < size; i++) {
1340 values[i] = ep0[i].me_value;
1341 ep0[i].me_value = NULL;
1342 }
1343 mp->ma_keys->dk_lookup = lookdict_split;
1344 mp->ma_values = values;
1345 }
INADA Naokia7576492018-11-14 18:39:27 +09001346 dictkeys_incref(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001347 return mp->ma_keys;
1348}
Christian Heimes99170a52007-12-19 02:07:34 +00001349
1350PyObject *
1351_PyDict_NewPresized(Py_ssize_t minused)
1352{
INADA Naoki92c50ee2016-11-22 00:57:02 +09001353 const Py_ssize_t max_presize = 128 * 1024;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001354 Py_ssize_t newsize;
1355 PyDictKeysObject *new_keys;
INADA Naoki92c50ee2016-11-22 00:57:02 +09001356
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001357 if (minused <= USABLE_FRACTION(PyDict_MINSIZE)) {
Inada Naokif2a18672019-03-12 17:25:44 +09001358 return PyDict_New();
1359 }
INADA Naoki92c50ee2016-11-22 00:57:02 +09001360 /* There are no strict guarantee that returned dict can contain minused
1361 * items without resize. So we create medium size dict instead of very
1362 * large dict or MemoryError.
1363 */
1364 if (minused > USABLE_FRACTION(max_presize)) {
1365 newsize = max_presize;
1366 }
1367 else {
1368 Py_ssize_t minsize = ESTIMATE_SIZE(minused);
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001369 newsize = PyDict_MINSIZE*2;
INADA Naoki92c50ee2016-11-22 00:57:02 +09001370 while (newsize < minsize) {
1371 newsize <<= 1;
1372 }
1373 }
1374 assert(IS_POWER_OF_2(newsize));
1375
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001376 new_keys = new_keys_object(newsize);
1377 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001379 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001380}
1381
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001382/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1383 * that may occur (originally dicts supported only string keys, and exceptions
1384 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001385 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001386 * (suppressed) occurred while computing the key's hash, or that some error
1387 * (suppressed) occurred when comparing keys in the dict's internal probe
1388 * sequence. A nasty example of the latter is when a Python-coded comparison
1389 * function hits a stack-depth error, which can cause this to return NULL
1390 * even if the key is present.
1391 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001392PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001393PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001394{
Victor Stinner59d3dce2020-06-02 14:03:25 +02001395 if (!PyDict_Check(op)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 return NULL;
Victor Stinner59d3dce2020-06-02 14:03:25 +02001397 }
1398 PyDictObject *mp = (PyDictObject *)op;
1399
1400 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001402 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 {
1404 hash = PyObject_Hash(key);
1405 if (hash == -1) {
1406 PyErr_Clear();
1407 return NULL;
1408 }
1409 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001410
Victor Stinner59d3dce2020-06-02 14:03:25 +02001411 PyThreadState *tstate = _PyThreadState_GET();
1412#ifdef Py_DEBUG
1413 // bpo-40839: Before Python 3.10, it was possible to call PyDict_GetItem()
1414 // with the GIL released.
1415 _Py_EnsureTstateNotNULL(tstate);
1416#endif
1417
1418 /* Preserve the existing exception */
1419 PyObject *exc_type, *exc_value, *exc_tb;
1420 PyObject *value;
1421 Py_ssize_t ix;
1422
1423 _PyErr_Fetch(tstate, &exc_type, &exc_value, &exc_tb);
1424 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
1425
1426 /* Ignore any exception raised by the lookup */
1427 _PyErr_Restore(tstate, exc_type, exc_value, exc_tb);
1428
1429 if (ix < 0) {
1430 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431 }
INADA Naokiba609772016-12-07 20:41:42 +09001432 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001433}
1434
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001435/* Same as PyDict_GetItemWithError() but with hash supplied by caller.
1436 This returns NULL *with* an exception set if an exception occurred.
1437 It returns NULL *without* an exception set if the key wasn't present.
1438*/
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001439PyObject *
1440_PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1441{
Victor Stinner742da042016-09-07 17:40:12 -07001442 Py_ssize_t ix;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001443 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001444 PyObject *value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001445
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001446 if (!PyDict_Check(op)) {
1447 PyErr_BadInternalCall();
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001448 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001449 }
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001450
INADA Naoki778928b2017-08-03 23:45:15 +09001451 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001452 if (ix < 0) {
1453 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001454 }
INADA Naokiba609772016-12-07 20:41:42 +09001455 return value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001456}
1457
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001458/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1459 This returns NULL *with* an exception set if an exception occurred.
1460 It returns NULL *without* an exception set if the key wasn't present.
1461*/
1462PyObject *
1463PyDict_GetItemWithError(PyObject *op, PyObject *key)
1464{
Victor Stinner742da042016-09-07 17:40:12 -07001465 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001466 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001467 PyDictObject*mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001468 PyObject *value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 if (!PyDict_Check(op)) {
1471 PyErr_BadInternalCall();
1472 return NULL;
1473 }
1474 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001475 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 {
1477 hash = PyObject_Hash(key);
1478 if (hash == -1) {
1479 return NULL;
1480 }
1481 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001482
INADA Naoki778928b2017-08-03 23:45:15 +09001483 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001484 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001486 return value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001487}
1488
Brett Cannonfd074152012-04-14 14:10:13 -04001489PyObject *
1490_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1491{
1492 PyObject *kv;
1493 kv = _PyUnicode_FromId(key); /* borrowed */
1494 if (kv == NULL)
1495 return NULL;
scoder6067d4b2020-05-11 06:04:31 +02001496 Py_hash_t hash = ((PyASCIIObject *) kv)->hash;
1497 assert (hash != -1); /* interned strings have their hash value initialised */
1498 return _PyDict_GetItem_KnownHash(dp, kv, hash);
Brett Cannonfd074152012-04-14 14:10:13 -04001499}
1500
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001501PyObject *
1502_PyDict_GetItemStringWithError(PyObject *v, const char *key)
1503{
1504 PyObject *kv, *rv;
1505 kv = PyUnicode_FromString(key);
1506 if (kv == NULL) {
1507 return NULL;
1508 }
1509 rv = PyDict_GetItemWithError(v, kv);
1510 Py_DECREF(kv);
1511 return rv;
1512}
1513
Victor Stinnerb4efc962015-11-20 09:24:02 +01001514/* Fast version of global value lookup (LOAD_GLOBAL).
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001515 * Lookup in globals, then builtins.
Victor Stinnerb4efc962015-11-20 09:24:02 +01001516 *
1517 * Raise an exception and return NULL if an error occurred (ex: computing the
1518 * key hash failed, key comparison failed, ...). Return NULL if the key doesn't
1519 * exist. Return the value if the key exists.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001520 */
1521PyObject *
1522_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001523{
Victor Stinner742da042016-09-07 17:40:12 -07001524 Py_ssize_t ix;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001525 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09001526 PyObject *value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001527
1528 if (!PyUnicode_CheckExact(key) ||
1529 (hash = ((PyASCIIObject *) key)->hash) == -1)
1530 {
1531 hash = PyObject_Hash(key);
1532 if (hash == -1)
1533 return NULL;
Antoine Pitroue965d972012-02-27 00:45:12 +01001534 }
Victor Stinnerb4efc962015-11-20 09:24:02 +01001535
1536 /* namespace 1: globals */
INADA Naoki778928b2017-08-03 23:45:15 +09001537 ix = globals->ma_keys->dk_lookup(globals, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001538 if (ix == DKIX_ERROR)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001539 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001540 if (ix != DKIX_EMPTY && value != NULL)
1541 return value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001542
1543 /* namespace 2: builtins */
INADA Naoki778928b2017-08-03 23:45:15 +09001544 ix = builtins->ma_keys->dk_lookup(builtins, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001545 if (ix < 0)
Victor Stinnerb4efc962015-11-20 09:24:02 +01001546 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001547 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001548}
1549
Antoine Pitroue965d972012-02-27 00:45:12 +01001550/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1551 * dictionary if it's merely replacing the value for an existing key.
1552 * This means that it's safe to loop over a dictionary with PyDict_Next()
1553 * and occasionally replace a value -- but you can't insert new keys or
1554 * remove them.
1555 */
1556int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001557PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001558{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001559 PyDictObject *mp;
1560 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001561 if (!PyDict_Check(op)) {
1562 PyErr_BadInternalCall();
1563 return -1;
1564 }
1565 assert(key);
1566 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001567 mp = (PyDictObject *)op;
1568 if (!PyUnicode_CheckExact(key) ||
1569 (hash = ((PyASCIIObject *) key)->hash) == -1)
1570 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001571 hash = PyObject_Hash(key);
1572 if (hash == -1)
1573 return -1;
1574 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001575
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001576 if (mp->ma_keys == Py_EMPTY_KEYS) {
1577 return insert_to_emptydict(mp, key, hash, value);
1578 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001579 /* insertdict() handles any resizing that might be necessary */
1580 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001581}
1582
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001583int
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001584_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1585 Py_hash_t hash)
1586{
1587 PyDictObject *mp;
1588
1589 if (!PyDict_Check(op)) {
1590 PyErr_BadInternalCall();
1591 return -1;
1592 }
1593 assert(key);
1594 assert(value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001595 assert(hash != -1);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001596 mp = (PyDictObject *)op;
1597
Inada Naoki2ddc7f62019-03-18 20:38:33 +09001598 if (mp->ma_keys == Py_EMPTY_KEYS) {
1599 return insert_to_emptydict(mp, key, hash, value);
1600 }
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001601 /* insertdict() handles any resizing that might be necessary */
1602 return insertdict(mp, key, hash, value);
1603}
1604
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001605static int
INADA Naoki778928b2017-08-03 23:45:15 +09001606delitem_common(PyDictObject *mp, Py_hash_t hash, Py_ssize_t ix,
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001607 PyObject *old_value)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001608{
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001609 PyObject *old_key;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001610 PyDictKeyEntry *ep;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001611
INADA Naoki778928b2017-08-03 23:45:15 +09001612 Py_ssize_t hashpos = lookdict_index(mp->ma_keys, hash, ix);
1613 assert(hashpos >= 0);
1614
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001615 mp->ma_used--;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001616 mp->ma_version_tag = DICT_NEXT_VERSION();
1617 ep = &DK_ENTRIES(mp->ma_keys)[ix];
INADA Naokia7576492018-11-14 18:39:27 +09001618 dictkeys_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001619 ENSURE_ALLOWS_DELETIONS(mp);
1620 old_key = ep->me_key;
1621 ep->me_key = NULL;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001622 ep->me_value = NULL;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001623 Py_DECREF(old_key);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001624 Py_DECREF(old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001625
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001626 ASSERT_CONSISTENT(mp);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001627 return 0;
1628}
1629
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001630int
Tim Peters1f5871e2000-07-04 17:44:48 +00001631PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001632{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001633 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 assert(key);
1635 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001636 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637 hash = PyObject_Hash(key);
1638 if (hash == -1)
1639 return -1;
1640 }
Victor Stinner742da042016-09-07 17:40:12 -07001641
1642 return _PyDict_DelItem_KnownHash(op, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001643}
1644
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001645int
1646_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1647{
INADA Naoki778928b2017-08-03 23:45:15 +09001648 Py_ssize_t ix;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001649 PyDictObject *mp;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001650 PyObject *old_value;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001651
1652 if (!PyDict_Check(op)) {
1653 PyErr_BadInternalCall();
1654 return -1;
1655 }
1656 assert(key);
1657 assert(hash != -1);
1658 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001659 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001660 if (ix == DKIX_ERROR)
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001661 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09001662 if (ix == DKIX_EMPTY || old_value == NULL) {
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001663 _PyErr_SetKeyError(key);
1664 return -1;
1665 }
Victor Stinner78601a32016-09-09 19:28:36 -07001666
1667 // Split table doesn't allow deletion. Combine it.
1668 if (_PyDict_HasSplitTable(mp)) {
1669 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1670 return -1;
1671 }
INADA Naoki778928b2017-08-03 23:45:15 +09001672 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001673 assert(ix >= 0);
1674 }
1675
INADA Naoki778928b2017-08-03 23:45:15 +09001676 return delitem_common(mp, hash, ix, old_value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001677}
1678
Antoine Pitroud741ed42016-12-27 14:23:43 +01001679/* This function promises that the predicate -> deletion sequence is atomic
1680 * (i.e. protected by the GIL), assuming the predicate itself doesn't
1681 * release the GIL.
1682 */
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001683int
1684_PyDict_DelItemIf(PyObject *op, PyObject *key,
1685 int (*predicate)(PyObject *value))
1686{
Antoine Pitroud741ed42016-12-27 14:23:43 +01001687 Py_ssize_t hashpos, ix;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001688 PyDictObject *mp;
1689 Py_hash_t hash;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001690 PyObject *old_value;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001691 int res;
1692
1693 if (!PyDict_Check(op)) {
1694 PyErr_BadInternalCall();
1695 return -1;
1696 }
1697 assert(key);
1698 hash = PyObject_Hash(key);
1699 if (hash == -1)
1700 return -1;
1701 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001702 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001703 if (ix == DKIX_ERROR)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001704 return -1;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001705 if (ix == DKIX_EMPTY || old_value == NULL) {
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001706 _PyErr_SetKeyError(key);
1707 return -1;
1708 }
Antoine Pitroud741ed42016-12-27 14:23:43 +01001709
1710 // Split table doesn't allow deletion. Combine it.
1711 if (_PyDict_HasSplitTable(mp)) {
1712 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1713 return -1;
1714 }
INADA Naoki778928b2017-08-03 23:45:15 +09001715 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001716 assert(ix >= 0);
1717 }
1718
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001719 res = predicate(old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001720 if (res == -1)
1721 return -1;
INADA Naoki778928b2017-08-03 23:45:15 +09001722
1723 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1724 assert(hashpos >= 0);
1725
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001726 if (res > 0)
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001727 return delitem_common(mp, hashpos, ix, old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001728 else
1729 return 0;
1730}
1731
1732
Guido van Rossum25831651993-05-19 14:50:45 +00001733void
Tim Peters1f5871e2000-07-04 17:44:48 +00001734PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001735{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001736 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001737 PyDictKeysObject *oldkeys;
1738 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 if (!PyDict_Check(op))
1742 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001743 mp = ((PyDictObject *)op);
1744 oldkeys = mp->ma_keys;
1745 oldvalues = mp->ma_values;
1746 if (oldvalues == empty_values)
1747 return;
1748 /* Empty the dict... */
INADA Naokia7576492018-11-14 18:39:27 +09001749 dictkeys_incref(Py_EMPTY_KEYS);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001750 mp->ma_keys = Py_EMPTY_KEYS;
1751 mp->ma_values = empty_values;
1752 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001753 mp->ma_version_tag = DICT_NEXT_VERSION();
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001754 /* ...then clear the keys and values */
1755 if (oldvalues != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001756 n = oldkeys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001757 for (i = 0; i < n; i++)
1758 Py_CLEAR(oldvalues[i]);
1759 free_values(oldvalues);
INADA Naokia7576492018-11-14 18:39:27 +09001760 dictkeys_decref(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001761 }
1762 else {
1763 assert(oldkeys->dk_refcnt == 1);
INADA Naokia7576492018-11-14 18:39:27 +09001764 dictkeys_decref(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001765 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001766 ASSERT_CONSISTENT(mp);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001767}
1768
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001769/* Internal version of PyDict_Next that returns a hash value in addition
1770 * to the key and value.
1771 * Return 1 on success, return 0 when the reached the end of the dictionary
1772 * (or if op is not a dictionary)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001773 */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001774int
1775_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1776 PyObject **pvalue, Py_hash_t *phash)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001777{
INADA Naokica2d8be2016-11-04 16:59:10 +09001778 Py_ssize_t i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001779 PyDictObject *mp;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001780 PyDictKeyEntry *entry_ptr;
1781 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001782
1783 if (!PyDict_Check(op))
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001784 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 mp = (PyDictObject *)op;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001786 i = *ppos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001787 if (mp->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09001788 if (i < 0 || i >= mp->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001789 return 0;
INADA Naokica2d8be2016-11-04 16:59:10 +09001790 /* values of split table is always dense */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001791 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
INADA Naokica2d8be2016-11-04 16:59:10 +09001792 value = mp->ma_values[i];
1793 assert(value != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001794 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001795 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09001796 Py_ssize_t n = mp->ma_keys->dk_nentries;
1797 if (i < 0 || i >= n)
1798 return 0;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001799 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
1800 while (i < n && entry_ptr->me_value == NULL) {
1801 entry_ptr++;
1802 i++;
Victor Stinner742da042016-09-07 17:40:12 -07001803 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001804 if (i >= n)
1805 return 0;
1806 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001808 *ppos = i+1;
1809 if (pkey)
1810 *pkey = entry_ptr->me_key;
1811 if (phash)
1812 *phash = entry_ptr->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001813 if (pvalue)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001814 *pvalue = value;
1815 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001816}
1817
Tim Peters080c88b2003-02-15 03:01:11 +00001818/*
1819 * Iterate over a dict. Use like so:
1820 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001821 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001822 * PyObject *key, *value;
1823 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001824 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001825 * Refer to borrowed references in key and value.
Tim Peters080c88b2003-02-15 03:01:11 +00001826 * }
1827 *
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001828 * Return 1 on success, return 0 when the reached the end of the dictionary
1829 * (or if op is not a dictionary)
1830 *
Tim Peters080c88b2003-02-15 03:01:11 +00001831 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001832 * mutates the dict. One exception: it is safe if the loop merely changes
1833 * the values associated with the keys (but doesn't insert new keys or
1834 * delete keys), via PyDict_SetItem().
1835 */
Guido van Rossum25831651993-05-19 14:50:45 +00001836int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001837PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001838{
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001839 return _PyDict_Next(op, ppos, pkey, pvalue, NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001840}
1841
Eric Snow96c6af92015-05-29 22:21:39 -06001842/* Internal version of dict.pop(). */
1843PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001844_PyDict_Pop_KnownHash(PyObject *dict, PyObject *key, Py_hash_t hash, PyObject *deflt)
Eric Snow96c6af92015-05-29 22:21:39 -06001845{
Victor Stinner742da042016-09-07 17:40:12 -07001846 Py_ssize_t ix, hashpos;
Eric Snow96c6af92015-05-29 22:21:39 -06001847 PyObject *old_value, *old_key;
1848 PyDictKeyEntry *ep;
Yury Selivanov684ef2c2016-10-28 19:01:21 -04001849 PyDictObject *mp;
1850
1851 assert(PyDict_Check(dict));
1852 mp = (PyDictObject *)dict;
Eric Snow96c6af92015-05-29 22:21:39 -06001853
1854 if (mp->ma_used == 0) {
1855 if (deflt) {
1856 Py_INCREF(deflt);
1857 return deflt;
1858 }
1859 _PyErr_SetKeyError(key);
1860 return NULL;
1861 }
INADA Naoki778928b2017-08-03 23:45:15 +09001862 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001863 if (ix == DKIX_ERROR)
Eric Snow96c6af92015-05-29 22:21:39 -06001864 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001865 if (ix == DKIX_EMPTY || old_value == NULL) {
Eric Snow96c6af92015-05-29 22:21:39 -06001866 if (deflt) {
1867 Py_INCREF(deflt);
1868 return deflt;
1869 }
1870 _PyErr_SetKeyError(key);
1871 return NULL;
1872 }
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001873
Victor Stinner78601a32016-09-09 19:28:36 -07001874 // Split table doesn't allow deletion. Combine it.
1875 if (_PyDict_HasSplitTable(mp)) {
1876 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1877 return NULL;
1878 }
INADA Naoki778928b2017-08-03 23:45:15 +09001879 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001880 assert(ix >= 0);
1881 }
1882
INADA Naoki778928b2017-08-03 23:45:15 +09001883 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1884 assert(hashpos >= 0);
Victor Stinner78601a32016-09-09 19:28:36 -07001885 assert(old_value != NULL);
Eric Snow96c6af92015-05-29 22:21:39 -06001886 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001887 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokia7576492018-11-14 18:39:27 +09001888 dictkeys_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
Victor Stinner78601a32016-09-09 19:28:36 -07001889 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1890 ENSURE_ALLOWS_DELETIONS(mp);
1891 old_key = ep->me_key;
1892 ep->me_key = NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001893 ep->me_value = NULL;
Victor Stinner78601a32016-09-09 19:28:36 -07001894 Py_DECREF(old_key);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001895
Victor Stinner0fc91ee2019-04-12 21:51:34 +02001896 ASSERT_CONSISTENT(mp);
Eric Snow96c6af92015-05-29 22:21:39 -06001897 return old_value;
1898}
1899
Serhiy Storchaka67796522017-01-12 18:34:33 +02001900PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001901_PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt)
Serhiy Storchaka67796522017-01-12 18:34:33 +02001902{
1903 Py_hash_t hash;
1904
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001905 if (((PyDictObject *)dict)->ma_used == 0) {
Serhiy Storchaka67796522017-01-12 18:34:33 +02001906 if (deflt) {
1907 Py_INCREF(deflt);
1908 return deflt;
1909 }
1910 _PyErr_SetKeyError(key);
1911 return NULL;
1912 }
1913 if (!PyUnicode_CheckExact(key) ||
1914 (hash = ((PyASCIIObject *) key)->hash) == -1) {
1915 hash = PyObject_Hash(key);
1916 if (hash == -1)
1917 return NULL;
1918 }
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001919 return _PyDict_Pop_KnownHash(dict, key, hash, deflt);
Serhiy Storchaka67796522017-01-12 18:34:33 +02001920}
1921
Eric Snow96c6af92015-05-29 22:21:39 -06001922/* Internal version of dict.from_keys(). It is subclass-friendly. */
1923PyObject *
1924_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1925{
1926 PyObject *it; /* iter(iterable) */
1927 PyObject *key;
1928 PyObject *d;
1929 int status;
1930
Victor Stinnera5ed5f02016-12-06 18:45:50 +01001931 d = _PyObject_CallNoArg(cls);
Eric Snow96c6af92015-05-29 22:21:39 -06001932 if (d == NULL)
1933 return NULL;
1934
1935 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
1936 if (PyDict_CheckExact(iterable)) {
1937 PyDictObject *mp = (PyDictObject *)d;
1938 PyObject *oldvalue;
1939 Py_ssize_t pos = 0;
1940 PyObject *key;
1941 Py_hash_t hash;
1942
Serhiy Storchakac61ac162017-03-21 08:52:38 +02001943 if (dictresize(mp, ESTIMATE_SIZE(PyDict_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001944 Py_DECREF(d);
1945 return NULL;
1946 }
1947
1948 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
1949 if (insertdict(mp, key, hash, value)) {
1950 Py_DECREF(d);
1951 return NULL;
1952 }
1953 }
1954 return d;
1955 }
1956 if (PyAnySet_CheckExact(iterable)) {
1957 PyDictObject *mp = (PyDictObject *)d;
1958 Py_ssize_t pos = 0;
1959 PyObject *key;
1960 Py_hash_t hash;
1961
Victor Stinner742da042016-09-07 17:40:12 -07001962 if (dictresize(mp, ESTIMATE_SIZE(PySet_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001963 Py_DECREF(d);
1964 return NULL;
1965 }
1966
1967 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
1968 if (insertdict(mp, key, hash, value)) {
1969 Py_DECREF(d);
1970 return NULL;
1971 }
1972 }
1973 return d;
1974 }
1975 }
1976
1977 it = PyObject_GetIter(iterable);
1978 if (it == NULL){
1979 Py_DECREF(d);
1980 return NULL;
1981 }
1982
1983 if (PyDict_CheckExact(d)) {
1984 while ((key = PyIter_Next(it)) != NULL) {
1985 status = PyDict_SetItem(d, key, value);
1986 Py_DECREF(key);
1987 if (status < 0)
1988 goto Fail;
1989 }
1990 } else {
1991 while ((key = PyIter_Next(it)) != NULL) {
1992 status = PyObject_SetItem(d, key, value);
1993 Py_DECREF(key);
1994 if (status < 0)
1995 goto Fail;
1996 }
1997 }
1998
1999 if (PyErr_Occurred())
2000 goto Fail;
2001 Py_DECREF(it);
2002 return d;
2003
2004Fail:
2005 Py_DECREF(it);
2006 Py_DECREF(d);
2007 return NULL;
2008}
2009
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002010/* Methods */
2011
2012static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002013dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002014{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002015 PyObject **values = mp->ma_values;
2016 PyDictKeysObject *keys = mp->ma_keys;
2017 Py_ssize_t i, n;
INADA Naokia6296d32017-08-24 14:55:17 +09002018
2019 /* bpo-31095: UnTrack is needed before calling any callbacks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002020 PyObject_GC_UnTrack(mp);
Jeroen Demeyer351c6742019-05-10 19:21:11 +02002021 Py_TRASHCAN_BEGIN(mp, dict_dealloc)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002022 if (values != NULL) {
2023 if (values != empty_values) {
Victor Stinner742da042016-09-07 17:40:12 -07002024 for (i = 0, n = mp->ma_keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002025 Py_XDECREF(values[i]);
2026 }
2027 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002028 }
INADA Naokia7576492018-11-14 18:39:27 +09002029 dictkeys_decref(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002030 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002031 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02002032 assert(keys->dk_refcnt == 1);
INADA Naokia7576492018-11-14 18:39:27 +09002033 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002034 }
Victor Stinnerb4e85ca2020-06-23 11:33:18 +02002035 PyInterpreterState *interp = _PyInterpreterState_GET();
2036 struct _Py_dict_state *state = &interp->dict_state;
2037#ifdef Py_DEBUG
2038 // new_dict() must not be called after _PyDict_Fini()
2039 assert(state->numfree != -1);
Victor Stinnerb4b53862020-05-05 19:55:29 +02002040#endif
Victor Stinnerb4e85ca2020-06-23 11:33:18 +02002041 if (state->numfree < PyDict_MAXFREELIST && Py_IS_TYPE(mp, &PyDict_Type)) {
2042 state->free_list[state->numfree++] = mp;
2043 }
2044 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045 Py_TYPE(mp)->tp_free((PyObject *)mp);
Victor Stinnerb4b53862020-05-05 19:55:29 +02002046 }
Jeroen Demeyer351c6742019-05-10 19:21:11 +02002047 Py_TRASHCAN_END
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002048}
2049
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002050
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002051static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002052dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002054 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01002055 PyObject *key = NULL, *value = NULL;
2056 _PyUnicodeWriter writer;
2057 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00002058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 i = Py_ReprEnter((PyObject *)mp);
2060 if (i != 0) {
2061 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
2062 }
Guido van Rossum255443b1998-04-10 22:47:14 +00002063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002064 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01002065 Py_ReprLeave((PyObject *)mp);
2066 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 }
Tim Petersa7259592001-06-16 05:11:17 +00002068
Victor Stinnerf91929b2013-11-19 13:07:38 +01002069 _PyUnicodeWriter_Init(&writer);
2070 writer.overallocate = 1;
2071 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
2072 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00002073
Victor Stinnerf91929b2013-11-19 13:07:38 +01002074 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
2075 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00002076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077 /* Do repr() on each key+value pair, and insert ": " between them.
2078 Note that repr may mutate the dict. */
2079 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01002080 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002081 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01002082 PyObject *s;
2083 int res;
2084
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002085 /* Prevent repr from deleting key or value during key format. */
2086 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002087 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02002088
Victor Stinnerf91929b2013-11-19 13:07:38 +01002089 if (!first) {
2090 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
2091 goto error;
2092 }
2093 first = 0;
2094
2095 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01002097 goto error;
2098 res = _PyUnicodeWriter_WriteStr(&writer, s);
2099 Py_DECREF(s);
2100 if (res < 0)
2101 goto error;
2102
2103 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
2104 goto error;
2105
2106 s = PyObject_Repr(value);
2107 if (s == NULL)
2108 goto error;
2109 res = _PyUnicodeWriter_WriteStr(&writer, s);
2110 Py_DECREF(s);
2111 if (res < 0)
2112 goto error;
2113
2114 Py_CLEAR(key);
2115 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002116 }
Tim Petersa7259592001-06-16 05:11:17 +00002117
Victor Stinnerf91929b2013-11-19 13:07:38 +01002118 writer.overallocate = 0;
2119 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
2120 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00002121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01002123
2124 return _PyUnicodeWriter_Finish(&writer);
2125
2126error:
2127 Py_ReprLeave((PyObject *)mp);
2128 _PyUnicodeWriter_Dealloc(&writer);
2129 Py_XDECREF(key);
2130 Py_XDECREF(value);
2131 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002132}
2133
Martin v. Löwis18e16552006-02-15 17:27:45 +00002134static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002135dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002136{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002137 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002138}
2139
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002140static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002141dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002142{
Victor Stinner742da042016-09-07 17:40:12 -07002143 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002144 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09002145 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002147 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002148 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002149 hash = PyObject_Hash(key);
2150 if (hash == -1)
2151 return NULL;
2152 }
INADA Naoki778928b2017-08-03 23:45:15 +09002153 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002154 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002155 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002156 if (ix == DKIX_EMPTY || value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002157 if (!PyDict_CheckExact(mp)) {
2158 /* Look up __missing__ method if we're a subclass. */
2159 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05002160 _Py_IDENTIFIER(__missing__);
2161 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162 if (missing != NULL) {
Petr Viktorinffd97532020-02-11 17:46:57 +01002163 res = PyObject_CallOneArg(missing, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002164 Py_DECREF(missing);
2165 return res;
2166 }
2167 else if (PyErr_Occurred())
2168 return NULL;
2169 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07002170 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002171 return NULL;
2172 }
INADA Naokiba609772016-12-07 20:41:42 +09002173 Py_INCREF(value);
2174 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002175}
2176
2177static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002178dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002179{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002180 if (w == NULL)
2181 return PyDict_DelItem((PyObject *)mp, v);
2182 else
2183 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002184}
2185
Guido van Rossuma9e7a811997-05-13 21:02:11 +00002186static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002187 (lenfunc)dict_length, /*mp_length*/
2188 (binaryfunc)dict_subscript, /*mp_subscript*/
2189 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002190};
2191
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002192static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002193dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002194{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002195 PyObject *v;
2196 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002197 PyDictKeyEntry *ep;
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002198 Py_ssize_t n, offset;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002199 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002200
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002201 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002202 n = mp->ma_used;
2203 v = PyList_New(n);
2204 if (v == NULL)
2205 return NULL;
2206 if (n != mp->ma_used) {
2207 /* Durnit. The allocations caused the dict to resize.
2208 * Just start over, this shouldn't normally happen.
2209 */
2210 Py_DECREF(v);
2211 goto again;
2212 }
Victor Stinner742da042016-09-07 17:40:12 -07002213 ep = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002214 if (mp->ma_values) {
2215 value_ptr = mp->ma_values;
2216 offset = sizeof(PyObject *);
2217 }
2218 else {
2219 value_ptr = &ep[0].me_value;
2220 offset = sizeof(PyDictKeyEntry);
2221 }
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002222 for (i = 0, j = 0; j < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002223 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224 PyObject *key = ep[i].me_key;
2225 Py_INCREF(key);
2226 PyList_SET_ITEM(v, j, key);
2227 j++;
2228 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002229 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 }
2231 assert(j == n);
2232 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002233}
2234
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002235static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002236dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002237{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002238 PyObject *v;
2239 Py_ssize_t i, j;
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002240 PyDictKeyEntry *ep;
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002241 Py_ssize_t n, offset;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002242 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002243
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002244 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 n = mp->ma_used;
2246 v = PyList_New(n);
2247 if (v == NULL)
2248 return NULL;
2249 if (n != mp->ma_used) {
2250 /* Durnit. The allocations caused the dict to resize.
2251 * Just start over, this shouldn't normally happen.
2252 */
2253 Py_DECREF(v);
2254 goto again;
2255 }
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002256 ep = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002257 if (mp->ma_values) {
2258 value_ptr = mp->ma_values;
2259 offset = sizeof(PyObject *);
2260 }
2261 else {
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002262 value_ptr = &ep[0].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002263 offset = sizeof(PyDictKeyEntry);
2264 }
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002265 for (i = 0, j = 0; j < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002266 PyObject *value = *value_ptr;
2267 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2268 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002269 Py_INCREF(value);
2270 PyList_SET_ITEM(v, j, value);
2271 j++;
2272 }
2273 }
2274 assert(j == n);
2275 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002276}
2277
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002278static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002279dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002280{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002281 PyObject *v;
2282 Py_ssize_t i, j, n;
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002283 Py_ssize_t offset;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002284 PyObject *item, *key;
2285 PyDictKeyEntry *ep;
2286 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002288 /* Preallocate the list of tuples, to avoid allocations during
2289 * the loop over the items, which could trigger GC, which
2290 * could resize the dict. :-(
2291 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002292 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002293 n = mp->ma_used;
2294 v = PyList_New(n);
2295 if (v == NULL)
2296 return NULL;
2297 for (i = 0; i < n; i++) {
2298 item = PyTuple_New(2);
2299 if (item == NULL) {
2300 Py_DECREF(v);
2301 return NULL;
2302 }
2303 PyList_SET_ITEM(v, i, item);
2304 }
2305 if (n != mp->ma_used) {
2306 /* Durnit. The allocations caused the dict to resize.
2307 * Just start over, this shouldn't normally happen.
2308 */
2309 Py_DECREF(v);
2310 goto again;
2311 }
2312 /* Nothing we do below makes any function calls. */
Victor Stinner742da042016-09-07 17:40:12 -07002313 ep = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002314 if (mp->ma_values) {
2315 value_ptr = mp->ma_values;
2316 offset = sizeof(PyObject *);
2317 }
2318 else {
2319 value_ptr = &ep[0].me_value;
2320 offset = sizeof(PyDictKeyEntry);
2321 }
Cheryl Sabellaf66e3362019-04-05 06:08:43 -04002322 for (i = 0, j = 0; j < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002323 PyObject *value = *value_ptr;
2324 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2325 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002326 key = ep[i].me_key;
2327 item = PyList_GET_ITEM(v, j);
2328 Py_INCREF(key);
2329 PyTuple_SET_ITEM(item, 0, key);
2330 Py_INCREF(value);
2331 PyTuple_SET_ITEM(item, 1, value);
2332 j++;
2333 }
2334 }
2335 assert(j == n);
2336 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002337}
2338
Larry Hastings5c661892014-01-24 06:17:25 -08002339/*[clinic input]
2340@classmethod
2341dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08002342 iterable: object
2343 value: object=None
2344 /
2345
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002346Create a new dictionary with keys from iterable and values set to value.
Larry Hastings5c661892014-01-24 06:17:25 -08002347[clinic start generated code]*/
2348
Larry Hastings5c661892014-01-24 06:17:25 -08002349static PyObject *
2350dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002351/*[clinic end generated code: output=8fb98e4b10384999 input=382ba4855d0f74c3]*/
Larry Hastings5c661892014-01-24 06:17:25 -08002352{
Eric Snow96c6af92015-05-29 22:21:39 -06002353 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002354}
2355
Brandt Buchereb8ac572020-02-24 19:47:34 -08002356/* Single-arg dict update; used by dict_update_common and operators. */
2357static int
2358dict_update_arg(PyObject *self, PyObject *arg)
2359{
2360 if (PyDict_CheckExact(arg)) {
2361 return PyDict_Merge(self, arg, 1);
2362 }
2363 _Py_IDENTIFIER(keys);
2364 PyObject *func;
2365 if (_PyObject_LookupAttrId(arg, &PyId_keys, &func) < 0) {
2366 return -1;
2367 }
2368 if (func != NULL) {
2369 Py_DECREF(func);
2370 return PyDict_Merge(self, arg, 1);
2371 }
2372 return PyDict_MergeFromSeq2(self, arg, 1);
2373}
2374
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002375static int
Victor Stinner742da042016-09-07 17:40:12 -07002376dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
2377 const char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002378{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379 PyObject *arg = NULL;
2380 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002381
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002382 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 result = -1;
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002384 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002385 else if (arg != NULL) {
Brandt Buchereb8ac572020-02-24 19:47:34 -08002386 result = dict_update_arg(self, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002387 }
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 if (result == 0 && kwds != NULL) {
2390 if (PyArg_ValidateKeywordArguments(kwds))
2391 result = PyDict_Merge(self, kwds, 1);
2392 else
2393 result = -1;
2394 }
2395 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002396}
2397
Victor Stinner91f0d4a2017-01-19 12:45:06 +01002398/* Note: dict.update() uses the METH_VARARGS|METH_KEYWORDS calling convention.
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002399 Using METH_FASTCALL|METH_KEYWORDS would make dict.update(**dict2) calls
2400 slower, see the issue #29312. */
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002401static PyObject *
2402dict_update(PyObject *self, PyObject *args, PyObject *kwds)
2403{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002404 if (dict_update_common(self, args, kwds, "update") != -1)
2405 Py_RETURN_NONE;
2406 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002407}
2408
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002409/* Update unconditionally replaces existing items.
2410 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00002411 otherwise it leaves existing items unchanged.
2412
2413 PyDict_{Update,Merge} update/merge from a mapping object.
2414
Tim Petersf582b822001-12-11 18:51:08 +00002415 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00002416 producing iterable objects of length 2.
2417*/
2418
Tim Petersf582b822001-12-11 18:51:08 +00002419int
Tim Peters1fc240e2001-10-26 05:06:50 +00002420PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
2421{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002422 PyObject *it; /* iter(seq2) */
2423 Py_ssize_t i; /* index into seq2 of current element */
2424 PyObject *item; /* seq2[i] */
2425 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00002426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002427 assert(d != NULL);
2428 assert(PyDict_Check(d));
2429 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00002430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002431 it = PyObject_GetIter(seq2);
2432 if (it == NULL)
2433 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 for (i = 0; ; ++i) {
2436 PyObject *key, *value;
2437 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00002438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 fast = NULL;
2440 item = PyIter_Next(it);
2441 if (item == NULL) {
2442 if (PyErr_Occurred())
2443 goto Fail;
2444 break;
2445 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002447 /* Convert item to sequence, and verify length 2. */
2448 fast = PySequence_Fast(item, "");
2449 if (fast == NULL) {
2450 if (PyErr_ExceptionMatches(PyExc_TypeError))
2451 PyErr_Format(PyExc_TypeError,
2452 "cannot convert dictionary update "
2453 "sequence element #%zd to a sequence",
2454 i);
2455 goto Fail;
2456 }
2457 n = PySequence_Fast_GET_SIZE(fast);
2458 if (n != 2) {
2459 PyErr_Format(PyExc_ValueError,
2460 "dictionary update sequence element #%zd "
2461 "has length %zd; 2 is required",
2462 i, n);
2463 goto Fail;
2464 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002466 /* Update/merge with this (key, value) pair. */
2467 key = PySequence_Fast_GET_ITEM(fast, 0);
2468 value = PySequence_Fast_GET_ITEM(fast, 1);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002469 Py_INCREF(key);
2470 Py_INCREF(value);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002471 if (override) {
2472 if (PyDict_SetItem(d, key, value) < 0) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002473 Py_DECREF(key);
2474 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002475 goto Fail;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002476 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002477 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002478 else if (PyDict_GetItemWithError(d, key) == NULL) {
2479 if (PyErr_Occurred() || PyDict_SetItem(d, key, value) < 0) {
2480 Py_DECREF(key);
2481 Py_DECREF(value);
2482 goto Fail;
2483 }
2484 }
2485
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002486 Py_DECREF(key);
2487 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002488 Py_DECREF(fast);
2489 Py_DECREF(item);
2490 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002492 i = 0;
Victor Stinner0fc91ee2019-04-12 21:51:34 +02002493 ASSERT_CONSISTENT(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002494 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002495Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002496 Py_XDECREF(item);
2497 Py_XDECREF(fast);
2498 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002499Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002500 Py_DECREF(it);
2501 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002502}
2503
doko@ubuntu.comc96df682016-10-11 08:04:02 +02002504static int
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002505dict_merge(PyObject *a, PyObject *b, int override)
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002506{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002507 PyDictObject *mp, *other;
2508 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -07002509 PyDictKeyEntry *entry, *ep0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002510
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002511 assert(0 <= override && override <= 2);
2512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002513 /* We accept for the argument either a concrete dictionary object,
2514 * or an abstract "mapping" object. For the former, we can do
2515 * things quite efficiently. For the latter, we only require that
2516 * PyMapping_Keys() and PyObject_GetItem() be supported.
2517 */
2518 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2519 PyErr_BadInternalCall();
2520 return -1;
2521 }
2522 mp = (PyDictObject*)a;
INADA Naoki2aaf98c2018-09-26 12:59:00 +09002523 if (PyDict_Check(b) && (Py_TYPE(b)->tp_iter == (getiterfunc)dict_iter)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002524 other = (PyDictObject*)b;
2525 if (other == mp || other->ma_used == 0)
2526 /* a.update(a) or a.update({}); nothing to do */
2527 return 0;
2528 if (mp->ma_used == 0)
2529 /* Since the target dict is empty, PyDict_GetItem()
2530 * always returns NULL. Setting override to 1
2531 * skips the unnecessary test.
2532 */
2533 override = 1;
2534 /* Do one big resize at the start, rather than
2535 * incrementally resizing as we insert new items. Expect
2536 * that there will be no (or few) overlapping keys.
2537 */
INADA Naokib1152be2016-10-27 19:26:50 +09002538 if (USABLE_FRACTION(mp->ma_keys->dk_size) < other->ma_used) {
2539 if (dictresize(mp, ESTIMATE_SIZE(mp->ma_used + other->ma_used))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002540 return -1;
INADA Naokib1152be2016-10-27 19:26:50 +09002541 }
2542 }
Victor Stinner742da042016-09-07 17:40:12 -07002543 ep0 = DK_ENTRIES(other->ma_keys);
2544 for (i = 0, n = other->ma_keys->dk_nentries; i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002545 PyObject *key, *value;
2546 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002547 entry = &ep0[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002548 key = entry->me_key;
2549 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002550 if (other->ma_values)
2551 value = other->ma_values[i];
2552 else
2553 value = entry->me_value;
2554
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002555 if (value != NULL) {
2556 int err = 0;
2557 Py_INCREF(key);
2558 Py_INCREF(value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002559 if (override == 1)
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002560 err = insertdict(mp, key, hash, value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002561 else if (_PyDict_GetItem_KnownHash(a, key, hash) == NULL) {
2562 if (PyErr_Occurred()) {
2563 Py_DECREF(value);
2564 Py_DECREF(key);
2565 return -1;
2566 }
2567 err = insertdict(mp, key, hash, value);
2568 }
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002569 else if (override != 0) {
2570 _PyErr_SetKeyError(key);
2571 Py_DECREF(value);
2572 Py_DECREF(key);
2573 return -1;
2574 }
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002575 Py_DECREF(value);
2576 Py_DECREF(key);
2577 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002578 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002579
Victor Stinner742da042016-09-07 17:40:12 -07002580 if (n != other->ma_keys->dk_nentries) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002581 PyErr_SetString(PyExc_RuntimeError,
2582 "dict mutated during update");
2583 return -1;
2584 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002585 }
2586 }
2587 }
2588 else {
2589 /* Do it the generic, slower way */
2590 PyObject *keys = PyMapping_Keys(b);
2591 PyObject *iter;
2592 PyObject *key, *value;
2593 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002595 if (keys == NULL)
2596 /* Docstring says this is equivalent to E.keys() so
2597 * if E doesn't have a .keys() method we want
2598 * AttributeError to percolate up. Might as well
2599 * do the same for any other error.
2600 */
2601 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002603 iter = PyObject_GetIter(keys);
2604 Py_DECREF(keys);
2605 if (iter == NULL)
2606 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002608 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002609 if (override != 1) {
2610 if (PyDict_GetItemWithError(a, key) != NULL) {
2611 if (override != 0) {
2612 _PyErr_SetKeyError(key);
2613 Py_DECREF(key);
2614 Py_DECREF(iter);
2615 return -1;
2616 }
2617 Py_DECREF(key);
2618 continue;
2619 }
2620 else if (PyErr_Occurred()) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002621 Py_DECREF(key);
2622 Py_DECREF(iter);
2623 return -1;
2624 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002625 }
2626 value = PyObject_GetItem(b, key);
2627 if (value == NULL) {
2628 Py_DECREF(iter);
2629 Py_DECREF(key);
2630 return -1;
2631 }
2632 status = PyDict_SetItem(a, key, value);
2633 Py_DECREF(key);
2634 Py_DECREF(value);
2635 if (status < 0) {
2636 Py_DECREF(iter);
2637 return -1;
2638 }
2639 }
2640 Py_DECREF(iter);
2641 if (PyErr_Occurred())
2642 /* Iterator completed, via error */
2643 return -1;
2644 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +02002645 ASSERT_CONSISTENT(a);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002646 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002647}
2648
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002649int
2650PyDict_Update(PyObject *a, PyObject *b)
2651{
2652 return dict_merge(a, b, 1);
2653}
2654
2655int
2656PyDict_Merge(PyObject *a, PyObject *b, int override)
2657{
2658 /* XXX Deprecate override not in (0, 1). */
2659 return dict_merge(a, b, override != 0);
2660}
2661
2662int
2663_PyDict_MergeEx(PyObject *a, PyObject *b, int override)
2664{
2665 return dict_merge(a, b, override);
2666}
2667
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002668static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05302669dict_copy(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002670{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002671 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002672}
2673
2674PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002675PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002676{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002677 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002678 PyDictObject *mp;
2679 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002681 if (o == NULL || !PyDict_Check(o)) {
2682 PyErr_BadInternalCall();
2683 return NULL;
2684 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002685
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002686 mp = (PyDictObject *)o;
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002687 if (mp->ma_used == 0) {
2688 /* The dict is empty; just return a new dict. */
2689 return PyDict_New();
2690 }
2691
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002692 if (_PyDict_HasSplitTable(mp)) {
2693 PyDictObject *split_copy;
Victor Stinner742da042016-09-07 17:40:12 -07002694 Py_ssize_t size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
2695 PyObject **newvalues;
2696 newvalues = new_values(size);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002697 if (newvalues == NULL)
2698 return PyErr_NoMemory();
2699 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2700 if (split_copy == NULL) {
2701 free_values(newvalues);
2702 return NULL;
2703 }
2704 split_copy->ma_values = newvalues;
2705 split_copy->ma_keys = mp->ma_keys;
2706 split_copy->ma_used = mp->ma_used;
INADA Naokid1c82c52018-04-03 11:43:53 +09002707 split_copy->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokia7576492018-11-14 18:39:27 +09002708 dictkeys_incref(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002709 for (i = 0, n = size; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002710 PyObject *value = mp->ma_values[i];
2711 Py_XINCREF(value);
2712 split_copy->ma_values[i] = value;
2713 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002714 if (_PyObject_GC_IS_TRACKED(mp))
2715 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002716 return (PyObject *)split_copy;
2717 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002718
2719 if (PyDict_CheckExact(mp) && mp->ma_values == NULL &&
2720 (mp->ma_used >= (mp->ma_keys->dk_nentries * 2) / 3))
2721 {
2722 /* Use fast-copy if:
2723
2724 (1) 'mp' is an instance of a subclassed dict; and
2725
2726 (2) 'mp' is not a split-dict; and
2727
2728 (3) if 'mp' is non-compact ('del' operation does not resize dicts),
2729 do fast-copy only if it has at most 1/3 non-used keys.
2730
Ville Skyttä61f82e02018-04-20 23:08:45 +03002731 The last condition (3) is important to guard against a pathological
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002732 case when a large dict is almost emptied with multiple del/pop
2733 operations and copied after that. In cases like this, we defer to
2734 PyDict_Merge, which produces a compacted copy.
2735 */
2736 return clone_combined_dict(mp);
2737 }
2738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002739 copy = PyDict_New();
2740 if (copy == NULL)
2741 return NULL;
2742 if (PyDict_Merge(copy, o, 1) == 0)
2743 return copy;
2744 Py_DECREF(copy);
2745 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002746}
2747
Martin v. Löwis18e16552006-02-15 17:27:45 +00002748Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002749PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002750{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 if (mp == NULL || !PyDict_Check(mp)) {
2752 PyErr_BadInternalCall();
2753 return -1;
2754 }
2755 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002756}
2757
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002758PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002759PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002761 if (mp == NULL || !PyDict_Check(mp)) {
2762 PyErr_BadInternalCall();
2763 return NULL;
2764 }
2765 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002766}
2767
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002768PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002769PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002770{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002771 if (mp == NULL || !PyDict_Check(mp)) {
2772 PyErr_BadInternalCall();
2773 return NULL;
2774 }
2775 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002776}
2777
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002778PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002779PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002780{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002781 if (mp == NULL || !PyDict_Check(mp)) {
2782 PyErr_BadInternalCall();
2783 return NULL;
2784 }
2785 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002786}
2787
Tim Peterse63415e2001-05-08 04:38:29 +00002788/* Return 1 if dicts equal, 0 if not, -1 if error.
2789 * Gets out as soon as any difference is detected.
2790 * Uses only Py_EQ comparison.
2791 */
2792static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002793dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002794{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002795 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002797 if (a->ma_used != b->ma_used)
2798 /* can't be equal if # of entries differ */
2799 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002800 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Victor Stinner742da042016-09-07 17:40:12 -07002801 for (i = 0; i < a->ma_keys->dk_nentries; i++) {
2802 PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002803 PyObject *aval;
2804 if (a->ma_values)
2805 aval = a->ma_values[i];
2806 else
2807 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002808 if (aval != NULL) {
2809 int cmp;
2810 PyObject *bval;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002811 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002812 /* temporarily bump aval's refcount to ensure it stays
2813 alive until we're done with it */
2814 Py_INCREF(aval);
2815 /* ditto for key */
2816 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002817 /* reuse the known hash value */
INADA Naoki778928b2017-08-03 23:45:15 +09002818 b->ma_keys->dk_lookup(b, key, ep->me_hash, &bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002819 if (bval == NULL) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002820 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002821 Py_DECREF(aval);
2822 if (PyErr_Occurred())
2823 return -1;
2824 return 0;
2825 }
Dong-hee Na2d5bf562019-12-31 10:04:22 +09002826 Py_INCREF(bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002827 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002828 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002829 Py_DECREF(aval);
Dong-hee Na2d5bf562019-12-31 10:04:22 +09002830 Py_DECREF(bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002831 if (cmp <= 0) /* error or not equal */
2832 return cmp;
2833 }
2834 }
2835 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002836}
Tim Peterse63415e2001-05-08 04:38:29 +00002837
2838static PyObject *
2839dict_richcompare(PyObject *v, PyObject *w, int op)
2840{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002841 int cmp;
2842 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002844 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2845 res = Py_NotImplemented;
2846 }
2847 else if (op == Py_EQ || op == Py_NE) {
2848 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2849 if (cmp < 0)
2850 return NULL;
2851 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2852 }
2853 else
2854 res = Py_NotImplemented;
2855 Py_INCREF(res);
2856 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002857}
Tim Peterse63415e2001-05-08 04:38:29 +00002858
Larry Hastings61272b72014-01-07 12:41:53 -08002859/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002860
2861@coexist
2862dict.__contains__
2863
2864 key: object
2865 /
2866
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002867True if the dictionary has the specified key, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002868[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002869
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002870static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002871dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka19d25972017-02-04 08:05:07 +02002872/*[clinic end generated code: output=a3d03db709ed6e6b input=fe1cb42ad831e820]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002873{
Larry Hastingsc2047262014-01-25 20:43:29 -08002874 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002875 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002876 Py_ssize_t ix;
INADA Naokiba609772016-12-07 20:41:42 +09002877 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002880 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 hash = PyObject_Hash(key);
2882 if (hash == -1)
2883 return NULL;
2884 }
INADA Naoki778928b2017-08-03 23:45:15 +09002885 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002886 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002887 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002888 if (ix == DKIX_EMPTY || value == NULL)
Victor Stinner742da042016-09-07 17:40:12 -07002889 Py_RETURN_FALSE;
2890 Py_RETURN_TRUE;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002891}
2892
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002893/*[clinic input]
2894dict.get
2895
2896 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002897 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002898 /
2899
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002900Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002901[clinic start generated code]*/
2902
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002903static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002904dict_get_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002905/*[clinic end generated code: output=bba707729dee05bf input=279ddb5790b6b107]*/
Barry Warsawc38c5da1997-10-06 17:49:20 +00002906{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002907 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002908 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002909 Py_ssize_t ix;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002911 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002912 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 hash = PyObject_Hash(key);
2914 if (hash == -1)
2915 return NULL;
2916 }
INADA Naoki778928b2017-08-03 23:45:15 +09002917 ix = (self->ma_keys->dk_lookup) (self, key, hash, &val);
Victor Stinner742da042016-09-07 17:40:12 -07002918 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002919 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002920 if (ix == DKIX_EMPTY || val == NULL) {
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002921 val = default_value;
INADA Naokiba609772016-12-07 20:41:42 +09002922 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002923 Py_INCREF(val);
2924 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002925}
2926
Benjamin Peterson00e98862013-03-07 22:16:29 -05002927PyObject *
2928PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002929{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002930 PyDictObject *mp = (PyDictObject *)d;
INADA Naoki93f26f72016-11-02 18:45:16 +09002931 PyObject *value;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002932 Py_hash_t hash;
Guido van Rossum164452c2000-08-08 16:12:54 +00002933
Benjamin Peterson00e98862013-03-07 22:16:29 -05002934 if (!PyDict_Check(d)) {
2935 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002936 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002937 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002939 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002940 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002941 hash = PyObject_Hash(key);
2942 if (hash == -1)
2943 return NULL;
2944 }
Inada Naoki2ddc7f62019-03-18 20:38:33 +09002945 if (mp->ma_keys == Py_EMPTY_KEYS) {
2946 if (insert_to_emptydict(mp, key, hash, defaultobj) < 0) {
2947 return NULL;
2948 }
2949 return defaultobj;
2950 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002951
2952 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
2953 if (insertion_resize(mp) < 0)
2954 return NULL;
2955 }
2956
INADA Naoki778928b2017-08-03 23:45:15 +09002957 Py_ssize_t ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002958 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002959 return NULL;
INADA Naoki93f26f72016-11-02 18:45:16 +09002960
2961 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09002962 ((ix >= 0 && value == NULL && mp->ma_used != ix) ||
INADA Naoki93f26f72016-11-02 18:45:16 +09002963 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
2964 if (insertion_resize(mp) < 0) {
2965 return NULL;
2966 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002967 ix = DKIX_EMPTY;
2968 }
2969
2970 if (ix == DKIX_EMPTY) {
2971 PyDictKeyEntry *ep, *ep0;
2972 value = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002973 if (mp->ma_keys->dk_usable <= 0) {
Victor Stinner3c336c52016-09-12 14:17:40 +02002974 if (insertion_resize(mp) < 0) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002975 return NULL;
Victor Stinner3c336c52016-09-12 14:17:40 +02002976 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002977 }
INADA Naoki778928b2017-08-03 23:45:15 +09002978 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naoki93f26f72016-11-02 18:45:16 +09002979 ep0 = DK_ENTRIES(mp->ma_keys);
2980 ep = &ep0[mp->ma_keys->dk_nentries];
INADA Naokia7576492018-11-14 18:39:27 +09002981 dictkeys_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002982 Py_INCREF(key);
INADA Naoki93f26f72016-11-02 18:45:16 +09002983 Py_INCREF(value);
2984 MAINTAIN_TRACKING(mp, key, value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002985 ep->me_key = key;
2986 ep->me_hash = hash;
INADA Naokiba609772016-12-07 20:41:42 +09002987 if (_PyDict_HasSplitTable(mp)) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002988 assert(mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
2989 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Victor Stinner742da042016-09-07 17:40:12 -07002990 }
2991 else {
INADA Naoki93f26f72016-11-02 18:45:16 +09002992 ep->me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07002993 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002994 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002995 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naoki93f26f72016-11-02 18:45:16 +09002996 mp->ma_keys->dk_usable--;
2997 mp->ma_keys->dk_nentries++;
2998 assert(mp->ma_keys->dk_usable >= 0);
2999 }
INADA Naokiba609772016-12-07 20:41:42 +09003000 else if (value == NULL) {
INADA Naoki93f26f72016-11-02 18:45:16 +09003001 value = defaultobj;
3002 assert(_PyDict_HasSplitTable(mp));
3003 assert(ix == mp->ma_used);
3004 Py_INCREF(value);
3005 MAINTAIN_TRACKING(mp, key, value);
INADA Naokiba609772016-12-07 20:41:42 +09003006 mp->ma_values[ix] = value;
INADA Naoki93f26f72016-11-02 18:45:16 +09003007 mp->ma_used++;
3008 mp->ma_version_tag = DICT_NEXT_VERSION();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003009 }
INADA Naoki93f26f72016-11-02 18:45:16 +09003010
Victor Stinner0fc91ee2019-04-12 21:51:34 +02003011 ASSERT_CONSISTENT(mp);
INADA Naoki93f26f72016-11-02 18:45:16 +09003012 return value;
Guido van Rossum164452c2000-08-08 16:12:54 +00003013}
3014
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003015/*[clinic input]
3016dict.setdefault
3017
3018 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02003019 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003020 /
3021
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02003022Insert key with a value of default if key is not in the dictionary.
3023
3024Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003025[clinic start generated code]*/
3026
Benjamin Peterson00e98862013-03-07 22:16:29 -05003027static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02003028dict_setdefault_impl(PyDictObject *self, PyObject *key,
3029 PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02003030/*[clinic end generated code: output=f8c1101ebf69e220 input=0f063756e815fd9d]*/
Benjamin Peterson00e98862013-03-07 22:16:29 -05003031{
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003032 PyObject *val;
Benjamin Peterson00e98862013-03-07 22:16:29 -05003033
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02003034 val = PyDict_SetDefault((PyObject *)self, key, default_value);
Benjamin Peterson00e98862013-03-07 22:16:29 -05003035 Py_XINCREF(val);
3036 return val;
3037}
Guido van Rossum164452c2000-08-08 16:12:54 +00003038
3039static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303040dict_clear(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00003041{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003042 PyDict_Clear((PyObject *)mp);
3043 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00003044}
3045
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003046/*[clinic input]
3047dict.pop
3048
3049 key: object
3050 default: object = NULL
3051 /
3052
Serhiy Storchaka279f4462019-09-14 12:24:05 +03003053D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003054
3055If key is not found, default is returned if given, otherwise KeyError is raised
3056[clinic start generated code]*/
3057
Guido van Rossumba6ab842000-12-12 22:02:18 +00003058static PyObject *
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003059dict_pop_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
Serhiy Storchaka279f4462019-09-14 12:24:05 +03003060/*[clinic end generated code: output=3abb47b89f24c21c input=eeebec7812190348]*/
Guido van Rossume027d982002-04-12 15:11:59 +00003061{
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003062 return _PyDict_Pop((PyObject*)self, key, default_value);
Guido van Rossume027d982002-04-12 15:11:59 +00003063}
3064
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003065/*[clinic input]
3066dict.popitem
3067
3068Remove and return a (key, value) pair as a 2-tuple.
3069
3070Pairs are returned in LIFO (last-in, first-out) order.
3071Raises KeyError if the dict is empty.
3072[clinic start generated code]*/
3073
Guido van Rossume027d982002-04-12 15:11:59 +00003074static PyObject *
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003075dict_popitem_impl(PyDictObject *self)
3076/*[clinic end generated code: output=e65fcb04420d230d input=1c38a49f21f64941]*/
Guido van Rossumba6ab842000-12-12 22:02:18 +00003077{
Victor Stinner742da042016-09-07 17:40:12 -07003078 Py_ssize_t i, j;
3079 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003080 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00003081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003082 /* Allocate the result tuple before checking the size. Believe it
3083 * or not, this allocation could trigger a garbage collection which
3084 * could empty the dict, so if we checked the size first and that
3085 * happened, the result would be an infinite loop (searching for an
3086 * entry that no longer exists). Note that the usual popitem()
3087 * idiom is "while d: k, v = d.popitem()". so needing to throw the
3088 * tuple away if the dict *is* empty isn't a significant
3089 * inefficiency -- possible, but unlikely in practice.
3090 */
3091 res = PyTuple_New(2);
3092 if (res == NULL)
3093 return NULL;
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003094 if (self->ma_used == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003095 Py_DECREF(res);
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003096 PyErr_SetString(PyExc_KeyError, "popitem(): dictionary is empty");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003097 return NULL;
3098 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003099 /* Convert split table to combined table */
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003100 if (self->ma_keys->dk_lookup == lookdict_split) {
3101 if (dictresize(self, DK_SIZE(self->ma_keys))) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003102 Py_DECREF(res);
3103 return NULL;
3104 }
3105 }
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003106 ENSURE_ALLOWS_DELETIONS(self);
Victor Stinner742da042016-09-07 17:40:12 -07003107
3108 /* Pop last item */
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003109 ep0 = DK_ENTRIES(self->ma_keys);
3110 i = self->ma_keys->dk_nentries - 1;
Victor Stinner742da042016-09-07 17:40:12 -07003111 while (i >= 0 && ep0[i].me_value == NULL) {
3112 i--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003113 }
Victor Stinner742da042016-09-07 17:40:12 -07003114 assert(i >= 0);
3115
3116 ep = &ep0[i];
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003117 j = lookdict_index(self->ma_keys, ep->me_hash, i);
Victor Stinner742da042016-09-07 17:40:12 -07003118 assert(j >= 0);
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003119 assert(dictkeys_get_index(self->ma_keys, j) == i);
3120 dictkeys_set_index(self->ma_keys, j, DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -07003121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003122 PyTuple_SET_ITEM(res, 0, ep->me_key);
3123 PyTuple_SET_ITEM(res, 1, ep->me_value);
Victor Stinner742da042016-09-07 17:40:12 -07003124 ep->me_key = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003125 ep->me_value = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07003126 /* We can't dk_usable++ since there is DKIX_DUMMY in indices */
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003127 self->ma_keys->dk_nentries = i;
3128 self->ma_used--;
3129 self->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner0fc91ee2019-04-12 21:51:34 +02003130 ASSERT_CONSISTENT(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003131 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00003132}
3133
Jeremy Hylton8caad492000-06-23 14:18:11 +00003134static int
3135dict_traverse(PyObject *op, visitproc visit, void *arg)
3136{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003137 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson55f44522016-09-05 12:12:59 -07003138 PyDictKeysObject *keys = mp->ma_keys;
Serhiy Storchaka46825d22016-09-26 21:29:34 +03003139 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Victor Stinner742da042016-09-07 17:40:12 -07003140 Py_ssize_t i, n = keys->dk_nentries;
3141
Benjamin Peterson55f44522016-09-05 12:12:59 -07003142 if (keys->dk_lookup == lookdict) {
3143 for (i = 0; i < n; i++) {
3144 if (entries[i].me_value != NULL) {
3145 Py_VISIT(entries[i].me_value);
3146 Py_VISIT(entries[i].me_key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003147 }
3148 }
Victor Stinner742da042016-09-07 17:40:12 -07003149 }
3150 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003151 if (mp->ma_values != NULL) {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003152 for (i = 0; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003153 Py_VISIT(mp->ma_values[i]);
3154 }
3155 }
3156 else {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003157 for (i = 0; i < n; i++) {
3158 Py_VISIT(entries[i].me_value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003159 }
3160 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003161 }
3162 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003163}
3164
3165static int
3166dict_tp_clear(PyObject *op)
3167{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003168 PyDict_Clear(op);
3169 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003170}
3171
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003172static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003173
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003174Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003175_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003176{
Victor Stinner742da042016-09-07 17:40:12 -07003177 Py_ssize_t size, usable, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003178
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003179 size = DK_SIZE(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07003180 usable = USABLE_FRACTION(size);
3181
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02003182 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003183 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -07003184 res += usable * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003185 /* If the dictionary is split, the keys portion is accounted-for
3186 in the type object. */
3187 if (mp->ma_keys->dk_refcnt == 1)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003188 res += (sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003189 + DK_IXSIZE(mp->ma_keys) * size
3190 + sizeof(PyDictKeyEntry) * usable);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003191 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003192}
3193
3194Py_ssize_t
3195_PyDict_KeysSize(PyDictKeysObject *keys)
3196{
Victor Stinner98ee9d52016-09-08 09:33:56 -07003197 return (sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003198 + DK_IXSIZE(keys) * DK_SIZE(keys)
3199 + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003200}
3201
doko@ubuntu.com17210f52016-01-14 14:04:59 +01003202static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303203dict_sizeof(PyDictObject *mp, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003204{
3205 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
3206}
3207
Brandt Buchereb8ac572020-02-24 19:47:34 -08003208static PyObject *
3209dict_or(PyObject *self, PyObject *other)
3210{
3211 if (!PyDict_Check(self) || !PyDict_Check(other)) {
3212 Py_RETURN_NOTIMPLEMENTED;
3213 }
3214 PyObject *new = PyDict_Copy(self);
3215 if (new == NULL) {
3216 return NULL;
3217 }
3218 if (dict_update_arg(new, other)) {
3219 Py_DECREF(new);
3220 return NULL;
3221 }
3222 return new;
3223}
3224
3225static PyObject *
3226dict_ior(PyObject *self, PyObject *other)
3227{
3228 if (dict_update_arg(self, other)) {
3229 return NULL;
3230 }
3231 Py_INCREF(self);
3232 return self;
3233}
3234
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00003235PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
3236
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003237PyDoc_STRVAR(sizeof__doc__,
3238"D.__sizeof__() -> size of D in memory, in bytes");
3239
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003240PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04003241"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
3242If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
3243If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
3244In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00003245
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003246PyDoc_STRVAR(clear__doc__,
3247"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003248
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003249PyDoc_STRVAR(copy__doc__,
3250"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00003251
Guido van Rossumb90c8482007-02-10 01:11:45 +00003252/* Forward */
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303253static PyObject *dictkeys_new(PyObject *, PyObject *);
3254static PyObject *dictitems_new(PyObject *, PyObject *);
3255static PyObject *dictvalues_new(PyObject *, PyObject *);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003256
Guido van Rossum45c85d12007-07-27 16:31:40 +00003257PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003258 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003259PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003260 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003261PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003262 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00003263
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003264static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07003265 DICT___CONTAINS___METHODDEF
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003266 {"__getitem__", (PyCFunction)(void(*)(void))dict_subscript, METH_O | METH_COEXIST,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003267 getitem__doc__},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003268 {"__sizeof__", (PyCFunction)(void(*)(void))dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003269 sizeof__doc__},
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003270 DICT_GET_METHODDEF
3271 DICT_SETDEFAULT_METHODDEF
Inada Naoki9e4f2f32019-04-12 16:11:28 +09003272 DICT_POP_METHODDEF
3273 DICT_POPITEM_METHODDEF
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303274 {"keys", dictkeys_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003275 keys__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303276 {"items", dictitems_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003277 items__doc__},
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303278 {"values", dictvalues_new, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003279 values__doc__},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003280 {"update", (PyCFunction)(void(*)(void))dict_update, METH_VARARGS | METH_KEYWORDS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003281 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08003282 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003283 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
3284 clear__doc__},
3285 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
3286 copy__doc__},
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003287 DICT___REVERSED___METHODDEF
Guido van Rossum48b069a2020-04-07 09:50:06 -07003288 {"__class_getitem__", (PyCFunction)Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003289 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003290};
3291
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003292/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00003293int
3294PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003295{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003296 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07003297 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003298 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003299 PyObject *value;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003301 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003302 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003303 hash = PyObject_Hash(key);
3304 if (hash == -1)
3305 return -1;
3306 }
INADA Naoki778928b2017-08-03 23:45:15 +09003307 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003308 if (ix == DKIX_ERROR)
3309 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003310 return (ix != DKIX_EMPTY && value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003311}
3312
Thomas Wouterscf297e42007-02-23 15:07:44 +00003313/* Internal version of PyDict_Contains used when the hash value is already known */
3314int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003315_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00003316{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003317 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003318 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07003319 Py_ssize_t ix;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003320
INADA Naoki778928b2017-08-03 23:45:15 +09003321 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003322 if (ix == DKIX_ERROR)
3323 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003324 return (ix != DKIX_EMPTY && value != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00003325}
3326
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003327/* Hack to implement "key in dict" */
3328static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003329 0, /* sq_length */
3330 0, /* sq_concat */
3331 0, /* sq_repeat */
3332 0, /* sq_item */
3333 0, /* sq_slice */
3334 0, /* sq_ass_item */
3335 0, /* sq_ass_slice */
3336 PyDict_Contains, /* sq_contains */
3337 0, /* sq_inplace_concat */
3338 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003339};
3340
Brandt Buchereb8ac572020-02-24 19:47:34 -08003341static PyNumberMethods dict_as_number = {
3342 .nb_or = dict_or,
3343 .nb_inplace_or = dict_ior,
3344};
3345
Guido van Rossum09e563a2001-05-01 12:10:21 +00003346static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003347dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3348{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003349 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003350 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003352 assert(type != NULL && type->tp_alloc != NULL);
3353 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02003354 if (self == NULL)
3355 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003356 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003357
Victor Stinnera9f61a52013-07-16 22:17:26 +02003358 /* The object has been implicitly tracked by tp_alloc */
3359 if (type == &PyDict_Type)
3360 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003361
3362 d->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003363 d->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07003364 d->ma_keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003365 if (d->ma_keys == NULL) {
3366 Py_DECREF(self);
3367 return NULL;
3368 }
Victor Stinner0fc91ee2019-04-12 21:51:34 +02003369 ASSERT_CONSISTENT(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003370 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003371}
3372
Tim Peters25786c02001-09-02 08:22:48 +00003373static int
3374dict_init(PyObject *self, PyObject *args, PyObject *kwds)
3375{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003376 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00003377}
3378
Tim Peters6d6c1a32001-08-02 04:15:00 +00003379static PyObject *
Dong-hee Nae27916b2020-04-02 09:55:43 +09003380dict_vectorcall(PyObject *type, PyObject * const*args,
3381 size_t nargsf, PyObject *kwnames)
3382{
3383 assert(PyType_Check(type));
3384 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
3385 if (!_PyArg_CheckPositional("dict", nargs, 0, 1)) {
3386 return NULL;
3387 }
3388
3389 PyObject *self = dict_new((PyTypeObject *)type, NULL, NULL);
3390 if (self == NULL) {
3391 return NULL;
3392 }
3393 if (nargs == 1) {
3394 if (dict_update_arg(self, args[0]) < 0) {
3395 Py_DECREF(self);
3396 return NULL;
3397 }
3398 args++;
3399 }
3400 if (kwnames != NULL) {
3401 for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(kwnames); i++) {
3402 if (PyDict_SetItem(self, PyTuple_GET_ITEM(kwnames, i), args[i]) < 0) {
3403 Py_DECREF(self);
3404 return NULL;
3405 }
3406 }
3407 }
3408 return self;
3409}
3410
3411static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003412dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00003413{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003414 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003415}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003416
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003417PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00003418"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00003419"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003420" (key, value) pairs\n"
3421"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00003422" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003423" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00003424" d[k] = v\n"
3425"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
3426" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00003427
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003428PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003429 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3430 "dict",
3431 sizeof(PyDictObject),
3432 0,
3433 (destructor)dict_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003434 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003435 0, /* tp_getattr */
3436 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003437 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003438 (reprfunc)dict_repr, /* tp_repr */
Brandt Buchereb8ac572020-02-24 19:47:34 -08003439 &dict_as_number, /* tp_as_number */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003440 &dict_as_sequence, /* tp_as_sequence */
3441 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003442 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003443 0, /* tp_call */
3444 0, /* tp_str */
3445 PyObject_GenericGetAttr, /* tp_getattro */
3446 0, /* tp_setattro */
3447 0, /* tp_as_buffer */
3448 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3449 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
3450 dictionary_doc, /* tp_doc */
3451 dict_traverse, /* tp_traverse */
3452 dict_tp_clear, /* tp_clear */
3453 dict_richcompare, /* tp_richcompare */
3454 0, /* tp_weaklistoffset */
3455 (getiterfunc)dict_iter, /* tp_iter */
3456 0, /* tp_iternext */
3457 mapp_methods, /* tp_methods */
3458 0, /* tp_members */
3459 0, /* tp_getset */
3460 0, /* tp_base */
3461 0, /* tp_dict */
3462 0, /* tp_descr_get */
3463 0, /* tp_descr_set */
3464 0, /* tp_dictoffset */
3465 dict_init, /* tp_init */
3466 PyType_GenericAlloc, /* tp_alloc */
3467 dict_new, /* tp_new */
3468 PyObject_GC_Del, /* tp_free */
Dong-hee Nae27916b2020-04-02 09:55:43 +09003469 .tp_vectorcall = dict_vectorcall,
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003470};
3471
Victor Stinner3c1e4812012-03-26 22:10:51 +02003472PyObject *
3473_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
3474{
3475 PyObject *kv;
3476 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02003477 if (kv == NULL) {
3478 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003479 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02003480 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02003481 return PyDict_GetItem(dp, kv);
3482}
3483
Guido van Rossum3cca2451997-05-16 14:23:33 +00003484/* For backward compatibility with old dictionary interface */
3485
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003486PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003487PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003488{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003489 PyObject *kv, *rv;
3490 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003491 if (kv == NULL) {
3492 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003493 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003494 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003495 rv = PyDict_GetItem(v, kv);
3496 Py_DECREF(kv);
3497 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003498}
3499
3500int
Victor Stinner3c1e4812012-03-26 22:10:51 +02003501_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
3502{
3503 PyObject *kv;
3504 kv = _PyUnicode_FromId(key); /* borrowed */
3505 if (kv == NULL)
3506 return -1;
3507 return PyDict_SetItem(v, kv, item);
3508}
3509
3510int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003511PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003512{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003513 PyObject *kv;
3514 int err;
3515 kv = PyUnicode_FromString(key);
3516 if (kv == NULL)
3517 return -1;
3518 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
3519 err = PyDict_SetItem(v, kv, item);
3520 Py_DECREF(kv);
3521 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003522}
3523
3524int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01003525_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
3526{
3527 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3528 if (kv == NULL)
3529 return -1;
3530 return PyDict_DelItem(v, kv);
3531}
3532
3533int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003534PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003535{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003536 PyObject *kv;
3537 int err;
3538 kv = PyUnicode_FromString(key);
3539 if (kv == NULL)
3540 return -1;
3541 err = PyDict_DelItem(v, kv);
3542 Py_DECREF(kv);
3543 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003544}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003545
Raymond Hettinger019a1482004-03-18 02:41:19 +00003546/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003547
3548typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003549 PyObject_HEAD
3550 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
3551 Py_ssize_t di_used;
3552 Py_ssize_t di_pos;
3553 PyObject* di_result; /* reusable result tuple for iteritems */
3554 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003555} dictiterobject;
3556
3557static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003558dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003560 dictiterobject *di;
3561 di = PyObject_GC_New(dictiterobject, itertype);
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003562 if (di == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003563 return NULL;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003564 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003565 Py_INCREF(dict);
3566 di->di_dict = dict;
3567 di->di_used = dict->ma_used;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003568 di->len = dict->ma_used;
Dong-hee Na24dc2f82019-10-20 05:01:08 +09003569 if (itertype == &PyDictRevIterKey_Type ||
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003570 itertype == &PyDictRevIterItem_Type ||
Dong-hee Na24dc2f82019-10-20 05:01:08 +09003571 itertype == &PyDictRevIterValue_Type) {
3572 if (dict->ma_values) {
3573 di->di_pos = dict->ma_used - 1;
3574 }
3575 else {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003576 di->di_pos = dict->ma_keys->dk_nentries - 1;
Dong-hee Na24dc2f82019-10-20 05:01:08 +09003577 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003578 }
3579 else {
3580 di->di_pos = 0;
3581 }
3582 if (itertype == &PyDictIterItem_Type ||
3583 itertype == &PyDictRevIterItem_Type) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003584 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
3585 if (di->di_result == NULL) {
3586 Py_DECREF(di);
3587 return NULL;
3588 }
3589 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003590 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003591 di->di_result = NULL;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003592 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003593 _PyObject_GC_TRACK(di);
3594 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003595}
3596
3597static void
3598dictiter_dealloc(dictiterobject *di)
3599{
INADA Naokia6296d32017-08-24 14:55:17 +09003600 /* bpo-31095: UnTrack is needed before calling any callbacks */
3601 _PyObject_GC_UNTRACK(di);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003602 Py_XDECREF(di->di_dict);
3603 Py_XDECREF(di->di_result);
3604 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003605}
3606
3607static int
3608dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
3609{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003610 Py_VISIT(di->di_dict);
3611 Py_VISIT(di->di_result);
3612 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003613}
3614
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003615static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303616dictiter_len(dictiterobject *di, PyObject *Py_UNUSED(ignored))
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003617{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003618 Py_ssize_t len = 0;
3619 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
3620 len = di->len;
3621 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003622}
3623
Guido van Rossumb90c8482007-02-10 01:11:45 +00003624PyDoc_STRVAR(length_hint_doc,
3625 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003626
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003627static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05303628dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored));
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003629
3630PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3631
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003632static PyMethodDef dictiter_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003633 {"__length_hint__", (PyCFunction)(void(*)(void))dictiter_len, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003634 length_hint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02003635 {"__reduce__", (PyCFunction)(void(*)(void))dictiter_reduce, METH_NOARGS,
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003636 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003637 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003638};
3639
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003640static PyObject*
3641dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00003642{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003643 PyObject *key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003644 Py_ssize_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003645 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003646 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00003647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003648 if (d == NULL)
3649 return NULL;
3650 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00003651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003652 if (di->di_used != d->ma_used) {
3653 PyErr_SetString(PyExc_RuntimeError,
3654 "dictionary changed size during iteration");
3655 di->di_used = -1; /* Make this state sticky */
3656 return NULL;
3657 }
Guido van Rossum2147df72002-07-16 20:30:22 +00003658
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003659 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003660 k = d->ma_keys;
INADA Naokica2d8be2016-11-04 16:59:10 +09003661 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003662 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003663 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003664 goto fail;
3665 key = DK_ENTRIES(k)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003666 assert(d->ma_values[i] != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003667 }
3668 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003669 Py_ssize_t n = k->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003670 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3671 while (i < n && entry_ptr->me_value == NULL) {
3672 entry_ptr++;
3673 i++;
3674 }
3675 if (i >= n)
3676 goto fail;
3677 key = entry_ptr->me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003678 }
Thomas Perl796cc6e2019-03-28 07:03:25 +01003679 // We found an element (key), but did not expect it
3680 if (di->len == 0) {
3681 PyErr_SetString(PyExc_RuntimeError,
3682 "dictionary keys changed during iteration");
3683 goto fail;
3684 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003685 di->di_pos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003686 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003687 Py_INCREF(key);
3688 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003689
3690fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003691 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003692 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003693 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003694}
3695
Raymond Hettinger019a1482004-03-18 02:41:19 +00003696PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003697 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3698 "dict_keyiterator", /* tp_name */
3699 sizeof(dictiterobject), /* tp_basicsize */
3700 0, /* tp_itemsize */
3701 /* methods */
3702 (destructor)dictiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003703 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003704 0, /* tp_getattr */
3705 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003706 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003707 0, /* tp_repr */
3708 0, /* tp_as_number */
3709 0, /* tp_as_sequence */
3710 0, /* tp_as_mapping */
3711 0, /* tp_hash */
3712 0, /* tp_call */
3713 0, /* tp_str */
3714 PyObject_GenericGetAttr, /* tp_getattro */
3715 0, /* tp_setattro */
3716 0, /* tp_as_buffer */
3717 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3718 0, /* tp_doc */
3719 (traverseproc)dictiter_traverse, /* tp_traverse */
3720 0, /* tp_clear */
3721 0, /* tp_richcompare */
3722 0, /* tp_weaklistoffset */
3723 PyObject_SelfIter, /* tp_iter */
3724 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3725 dictiter_methods, /* tp_methods */
3726 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003727};
3728
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003729static PyObject *
3730dictiter_iternextvalue(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003731{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003732 PyObject *value;
INADA Naokica2d8be2016-11-04 16:59:10 +09003733 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003734 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003736 if (d == NULL)
3737 return NULL;
3738 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003740 if (di->di_used != d->ma_used) {
3741 PyErr_SetString(PyExc_RuntimeError,
3742 "dictionary changed size during iteration");
3743 di->di_used = -1; /* Make this state sticky */
3744 return NULL;
3745 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003747 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003748 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003749 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003750 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003751 goto fail;
INADA Naokica2d8be2016-11-04 16:59:10 +09003752 value = d->ma_values[i];
3753 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003754 }
3755 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003756 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003757 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3758 while (i < n && entry_ptr->me_value == NULL) {
3759 entry_ptr++;
3760 i++;
3761 }
3762 if (i >= n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003763 goto fail;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003764 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003765 }
Thomas Perlb8311cf2019-04-02 11:30:10 +02003766 // We found an element, but did not expect it
3767 if (di->len == 0) {
3768 PyErr_SetString(PyExc_RuntimeError,
3769 "dictionary keys changed during iteration");
3770 goto fail;
3771 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003772 di->di_pos = i+1;
3773 di->len--;
3774 Py_INCREF(value);
3775 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003776
3777fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003778 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003779 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003780 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003781}
3782
3783PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003784 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3785 "dict_valueiterator", /* tp_name */
3786 sizeof(dictiterobject), /* tp_basicsize */
3787 0, /* tp_itemsize */
3788 /* methods */
3789 (destructor)dictiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003790 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003791 0, /* tp_getattr */
3792 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003793 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003794 0, /* tp_repr */
3795 0, /* tp_as_number */
3796 0, /* tp_as_sequence */
3797 0, /* tp_as_mapping */
3798 0, /* tp_hash */
3799 0, /* tp_call */
3800 0, /* tp_str */
3801 PyObject_GenericGetAttr, /* tp_getattro */
3802 0, /* tp_setattro */
3803 0, /* tp_as_buffer */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003804 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003805 0, /* tp_doc */
3806 (traverseproc)dictiter_traverse, /* tp_traverse */
3807 0, /* tp_clear */
3808 0, /* tp_richcompare */
3809 0, /* tp_weaklistoffset */
3810 PyObject_SelfIter, /* tp_iter */
3811 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3812 dictiter_methods, /* tp_methods */
3813 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003814};
3815
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003816static PyObject *
3817dictiter_iternextitem(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003818{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003819 PyObject *key, *value, *result;
INADA Naokica2d8be2016-11-04 16:59:10 +09003820 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003821 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003823 if (d == NULL)
3824 return NULL;
3825 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003827 if (di->di_used != d->ma_used) {
3828 PyErr_SetString(PyExc_RuntimeError,
3829 "dictionary changed size during iteration");
3830 di->di_used = -1; /* Make this state sticky */
3831 return NULL;
3832 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003834 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003835 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003836 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003837 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003838 goto fail;
3839 key = DK_ENTRIES(d->ma_keys)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003840 value = d->ma_values[i];
3841 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003842 }
3843 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003844 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003845 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3846 while (i < n && entry_ptr->me_value == NULL) {
3847 entry_ptr++;
3848 i++;
3849 }
3850 if (i >= n)
3851 goto fail;
3852 key = entry_ptr->me_key;
3853 value = entry_ptr->me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003854 }
Thomas Perlb8311cf2019-04-02 11:30:10 +02003855 // We found an element, but did not expect it
3856 if (di->len == 0) {
3857 PyErr_SetString(PyExc_RuntimeError,
3858 "dictionary keys changed during iteration");
3859 goto fail;
3860 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003861 di->di_pos = i+1;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003862 di->len--;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003863 Py_INCREF(key);
3864 Py_INCREF(value);
3865 result = di->di_result;
3866 if (Py_REFCNT(result) == 1) {
3867 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3868 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3869 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3870 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003871 Py_INCREF(result);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003872 Py_DECREF(oldkey);
3873 Py_DECREF(oldvalue);
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003874 }
3875 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003876 result = PyTuple_New(2);
3877 if (result == NULL)
3878 return NULL;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003879 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3880 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003881 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003882 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003883
3884fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003885 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003886 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003887 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003888}
3889
3890PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003891 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3892 "dict_itemiterator", /* tp_name */
3893 sizeof(dictiterobject), /* tp_basicsize */
3894 0, /* tp_itemsize */
3895 /* methods */
3896 (destructor)dictiter_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003897 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003898 0, /* tp_getattr */
3899 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02003900 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003901 0, /* tp_repr */
3902 0, /* tp_as_number */
3903 0, /* tp_as_sequence */
3904 0, /* tp_as_mapping */
3905 0, /* tp_hash */
3906 0, /* tp_call */
3907 0, /* tp_str */
3908 PyObject_GenericGetAttr, /* tp_getattro */
3909 0, /* tp_setattro */
3910 0, /* tp_as_buffer */
3911 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3912 0, /* tp_doc */
3913 (traverseproc)dictiter_traverse, /* tp_traverse */
3914 0, /* tp_clear */
3915 0, /* tp_richcompare */
3916 0, /* tp_weaklistoffset */
3917 PyObject_SelfIter, /* tp_iter */
3918 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3919 dictiter_methods, /* tp_methods */
3920 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003921};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003922
3923
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003924/* dictreviter */
3925
3926static PyObject *
3927dictreviter_iternext(dictiterobject *di)
3928{
3929 PyDictObject *d = di->di_dict;
3930
3931 if (d == NULL) {
3932 return NULL;
3933 }
3934 assert (PyDict_Check(d));
3935
3936 if (di->di_used != d->ma_used) {
3937 PyErr_SetString(PyExc_RuntimeError,
3938 "dictionary changed size during iteration");
3939 di->di_used = -1; /* Make this state sticky */
3940 return NULL;
3941 }
3942
3943 Py_ssize_t i = di->di_pos;
3944 PyDictKeysObject *k = d->ma_keys;
3945 PyObject *key, *value, *result;
3946
Serhiy Storchaka2e3d8732019-10-23 14:48:08 +03003947 if (i < 0) {
3948 goto fail;
3949 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003950 if (d->ma_values) {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003951 key = DK_ENTRIES(k)[i].me_key;
3952 value = d->ma_values[i];
3953 assert (value != NULL);
3954 }
3955 else {
3956 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
Serhiy Storchaka2e3d8732019-10-23 14:48:08 +03003957 while (entry_ptr->me_value == NULL) {
3958 if (--i < 0) {
3959 goto fail;
3960 }
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003961 entry_ptr--;
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003962 }
3963 key = entry_ptr->me_key;
3964 value = entry_ptr->me_value;
3965 }
3966 di->di_pos = i-1;
3967 di->len--;
3968
Dong-hee Na1b55b652020-02-17 19:09:15 +09003969 if (Py_IS_TYPE(di, &PyDictRevIterKey_Type)) {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003970 Py_INCREF(key);
3971 return key;
3972 }
Dong-hee Na1b55b652020-02-17 19:09:15 +09003973 else if (Py_IS_TYPE(di, &PyDictRevIterValue_Type)) {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003974 Py_INCREF(value);
3975 return value;
3976 }
Dong-hee Na1b55b652020-02-17 19:09:15 +09003977 else if (Py_IS_TYPE(di, &PyDictRevIterItem_Type)) {
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01003978 Py_INCREF(key);
3979 Py_INCREF(value);
3980 result = di->di_result;
3981 if (Py_REFCNT(result) == 1) {
3982 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3983 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3984 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3985 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
3986 Py_INCREF(result);
3987 Py_DECREF(oldkey);
3988 Py_DECREF(oldvalue);
3989 }
3990 else {
3991 result = PyTuple_New(2);
3992 if (result == NULL) {
3993 return NULL;
3994 }
3995 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3996 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
3997 }
3998 return result;
3999 }
4000 else {
4001 Py_UNREACHABLE();
4002 }
4003
4004fail:
4005 di->di_dict = NULL;
4006 Py_DECREF(d);
4007 return NULL;
4008}
4009
4010PyTypeObject PyDictRevIterKey_Type = {
4011 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4012 "dict_reversekeyiterator",
4013 sizeof(dictiterobject),
4014 .tp_dealloc = (destructor)dictiter_dealloc,
4015 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
4016 .tp_traverse = (traverseproc)dictiter_traverse,
4017 .tp_iter = PyObject_SelfIter,
4018 .tp_iternext = (iternextfunc)dictreviter_iternext,
4019 .tp_methods = dictiter_methods
4020};
4021
4022
4023/*[clinic input]
4024dict.__reversed__
4025
4026Return a reverse iterator over the dict keys.
4027[clinic start generated code]*/
4028
4029static PyObject *
4030dict___reversed___impl(PyDictObject *self)
4031/*[clinic end generated code: output=e674483336d1ed51 input=23210ef3477d8c4d]*/
4032{
4033 assert (PyDict_Check(self));
4034 return dictiter_new(self, &PyDictRevIterKey_Type);
4035}
4036
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004037static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304038dictiter_reduce(dictiterobject *di, PyObject *Py_UNUSED(ignored))
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004039{
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004040 _Py_IDENTIFIER(iter);
Sergey Fedoseev63958442018-10-20 05:43:33 +05004041 /* copy the iterator state */
4042 dictiterobject tmp = *di;
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004043 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004044
Sergey Fedoseev63958442018-10-20 05:43:33 +05004045 PyObject *list = PySequence_List((PyObject*)&tmp);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004046 Py_XDECREF(tmp.di_dict);
Sergey Fedoseev63958442018-10-20 05:43:33 +05004047 if (list == NULL) {
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004048 return NULL;
4049 }
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004050 return Py_BuildValue("N(N)", _PyEval_GetBuiltinId(&PyId_iter), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00004051}
4052
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004053PyTypeObject PyDictRevIterItem_Type = {
4054 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4055 "dict_reverseitemiterator",
4056 sizeof(dictiterobject),
4057 .tp_dealloc = (destructor)dictiter_dealloc,
4058 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
4059 .tp_traverse = (traverseproc)dictiter_traverse,
4060 .tp_iter = PyObject_SelfIter,
4061 .tp_iternext = (iternextfunc)dictreviter_iternext,
4062 .tp_methods = dictiter_methods
4063};
4064
4065PyTypeObject PyDictRevIterValue_Type = {
4066 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4067 "dict_reversevalueiterator",
4068 sizeof(dictiterobject),
4069 .tp_dealloc = (destructor)dictiter_dealloc,
4070 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
4071 .tp_traverse = (traverseproc)dictiter_traverse,
4072 .tp_iter = PyObject_SelfIter,
4073 .tp_iternext = (iternextfunc)dictreviter_iternext,
4074 .tp_methods = dictiter_methods
4075};
4076
Guido van Rossum3ac67412007-02-10 18:55:06 +00004077/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004078/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00004079/***********************************************/
4080
Guido van Rossumb90c8482007-02-10 01:11:45 +00004081/* The instance lay-out is the same for all three; but the type differs. */
4082
Guido van Rossumb90c8482007-02-10 01:11:45 +00004083static void
Eric Snow96c6af92015-05-29 22:21:39 -06004084dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004085{
INADA Naokia6296d32017-08-24 14:55:17 +09004086 /* bpo-31095: UnTrack is needed before calling any callbacks */
4087 _PyObject_GC_UNTRACK(dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004088 Py_XDECREF(dv->dv_dict);
4089 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00004090}
4091
4092static int
Eric Snow96c6af92015-05-29 22:21:39 -06004093dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00004094{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004095 Py_VISIT(dv->dv_dict);
4096 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004097}
4098
Guido van Rossum83825ac2007-02-10 04:54:19 +00004099static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06004100dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004101{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004102 Py_ssize_t len = 0;
4103 if (dv->dv_dict != NULL)
4104 len = dv->dv_dict->ma_used;
4105 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004106}
4107
Eric Snow96c6af92015-05-29 22:21:39 -06004108PyObject *
4109_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004110{
Eric Snow96c6af92015-05-29 22:21:39 -06004111 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004112 if (dict == NULL) {
4113 PyErr_BadInternalCall();
4114 return NULL;
4115 }
4116 if (!PyDict_Check(dict)) {
4117 /* XXX Get rid of this restriction later */
4118 PyErr_Format(PyExc_TypeError,
4119 "%s() requires a dict argument, not '%s'",
Victor Stinner58ac7002020-02-07 03:04:21 +01004120 type->tp_name, Py_TYPE(dict)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004121 return NULL;
4122 }
Eric Snow96c6af92015-05-29 22:21:39 -06004123 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004124 if (dv == NULL)
4125 return NULL;
4126 Py_INCREF(dict);
4127 dv->dv_dict = (PyDictObject *)dict;
4128 _PyObject_GC_TRACK(dv);
4129 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004130}
4131
Dennis Sweeney3ee0e482020-06-12 13:19:25 -04004132static PyObject *
Pablo Galindo10c3b212020-06-15 02:05:20 +01004133dictview_mapping(PyObject *view, void *Py_UNUSED(ignored)) {
Dennis Sweeney3ee0e482020-06-12 13:19:25 -04004134 assert(view != NULL);
4135 assert(PyDictKeys_Check(view)
4136 || PyDictValues_Check(view)
4137 || PyDictItems_Check(view));
4138 PyObject *mapping = (PyObject *)((_PyDictViewObject *)view)->dv_dict;
4139 return PyDictProxy_New(mapping);
4140}
4141
4142static PyGetSetDef dictview_getset[] = {
Pablo Galindo10c3b212020-06-15 02:05:20 +01004143 {"mapping", dictview_mapping, (setter)NULL,
Dennis Sweeney3ee0e482020-06-12 13:19:25 -04004144 "dictionary that this view refers to", NULL},
4145 {0}
4146};
4147
Neal Norwitze36f2ba2007-02-26 23:12:28 +00004148/* TODO(guido): The views objects are not complete:
4149
4150 * support more set operations
4151 * support arbitrary mappings?
4152 - either these should be static or exported in dictobject.h
4153 - if public then they should probably be in builtins
4154*/
4155
Guido van Rossumaac530c2007-08-24 22:33:45 +00004156/* Return 1 if self is a subset of other, iterating over self;
4157 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00004158static int
4159all_contained_in(PyObject *self, PyObject *other)
4160{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004161 PyObject *iter = PyObject_GetIter(self);
4162 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004164 if (iter == NULL)
4165 return -1;
4166 for (;;) {
4167 PyObject *next = PyIter_Next(iter);
4168 if (next == NULL) {
4169 if (PyErr_Occurred())
4170 ok = -1;
4171 break;
4172 }
4173 ok = PySequence_Contains(other, next);
4174 Py_DECREF(next);
4175 if (ok <= 0)
4176 break;
4177 }
4178 Py_DECREF(iter);
4179 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004180}
4181
4182static PyObject *
4183dictview_richcompare(PyObject *self, PyObject *other, int op)
4184{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004185 Py_ssize_t len_self, len_other;
4186 int ok;
4187 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004189 assert(self != NULL);
4190 assert(PyDictViewSet_Check(self));
4191 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00004192
Brian Curtindfc80e32011-08-10 20:28:54 -05004193 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
4194 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004196 len_self = PyObject_Size(self);
4197 if (len_self < 0)
4198 return NULL;
4199 len_other = PyObject_Size(other);
4200 if (len_other < 0)
4201 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004202
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004203 ok = 0;
4204 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00004205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004206 case Py_NE:
4207 case Py_EQ:
4208 if (len_self == len_other)
4209 ok = all_contained_in(self, other);
4210 if (op == Py_NE && ok >= 0)
4211 ok = !ok;
4212 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004214 case Py_LT:
4215 if (len_self < len_other)
4216 ok = all_contained_in(self, other);
4217 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004219 case Py_LE:
4220 if (len_self <= len_other)
4221 ok = all_contained_in(self, other);
4222 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004224 case Py_GT:
4225 if (len_self > len_other)
4226 ok = all_contained_in(other, self);
4227 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004229 case Py_GE:
4230 if (len_self >= len_other)
4231 ok = all_contained_in(other, self);
4232 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00004233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004234 }
4235 if (ok < 0)
4236 return NULL;
4237 result = ok ? Py_True : Py_False;
4238 Py_INCREF(result);
4239 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00004240}
4241
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004242static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004243dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004244{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004245 PyObject *seq;
bennorthd7773d92018-01-26 15:46:01 +00004246 PyObject *result = NULL;
4247 Py_ssize_t rc;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004248
bennorthd7773d92018-01-26 15:46:01 +00004249 rc = Py_ReprEnter((PyObject *)dv);
4250 if (rc != 0) {
4251 return rc > 0 ? PyUnicode_FromString("...") : NULL;
4252 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004253 seq = PySequence_List((PyObject *)dv);
bennorthd7773d92018-01-26 15:46:01 +00004254 if (seq == NULL) {
4255 goto Done;
4256 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004257 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
4258 Py_DECREF(seq);
bennorthd7773d92018-01-26 15:46:01 +00004259
4260Done:
4261 Py_ReprLeave((PyObject *)dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004262 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00004263}
4264
Guido van Rossum3ac67412007-02-10 18:55:06 +00004265/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004266
4267static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004268dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004270 if (dv->dv_dict == NULL) {
4271 Py_RETURN_NONE;
4272 }
4273 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004274}
4275
4276static int
Eric Snow96c6af92015-05-29 22:21:39 -06004277dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004278{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004279 if (dv->dv_dict == NULL)
4280 return 0;
4281 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004282}
4283
Guido van Rossum83825ac2007-02-10 04:54:19 +00004284static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004285 (lenfunc)dictview_len, /* sq_length */
4286 0, /* sq_concat */
4287 0, /* sq_repeat */
4288 0, /* sq_item */
4289 0, /* sq_slice */
4290 0, /* sq_ass_item */
4291 0, /* sq_ass_slice */
4292 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004293};
4294
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004295// Create an set object from dictviews object.
4296// Returns a new reference.
4297// This utility function is used by set operations.
Guido van Rossum523259b2007-08-24 23:41:22 +00004298static PyObject*
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004299dictviews_to_set(PyObject *self)
Guido van Rossum523259b2007-08-24 23:41:22 +00004300{
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004301 PyObject *left = self;
4302 if (PyDictKeys_Check(self)) {
4303 // PySet_New() has fast path for the dict object.
4304 PyObject *dict = (PyObject *)((_PyDictViewObject *)self)->dv_dict;
4305 if (PyDict_CheckExact(dict)) {
4306 left = dict;
4307 }
4308 }
4309 return PySet_New(left);
4310}
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004311
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004312static PyObject*
4313dictviews_sub(PyObject *self, PyObject *other)
4314{
4315 PyObject *result = dictviews_to_set(self);
4316 if (result == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004317 return NULL;
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004318 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004319
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004320 _Py_IDENTIFIER(difference_update);
4321 PyObject *tmp = _PyObject_CallMethodIdOneArg(
4322 result, &PyId_difference_update, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004323 if (tmp == NULL) {
4324 Py_DECREF(result);
4325 return NULL;
4326 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004328 Py_DECREF(tmp);
4329 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004330}
4331
Forest Gregg998cf1f2019-08-26 02:17:43 -05004332static int
4333dictitems_contains(_PyDictViewObject *dv, PyObject *obj);
4334
4335PyObject *
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004336_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00004337{
Forest Gregg998cf1f2019-08-26 02:17:43 -05004338 PyObject *result;
4339 PyObject *it;
4340 PyObject *key;
4341 Py_ssize_t len_self;
4342 int rv;
4343 int (*dict_contains)(_PyDictViewObject *, PyObject *);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004344
Forest Gregg998cf1f2019-08-26 02:17:43 -05004345 /* Python interpreter swaps parameters when dict view
4346 is on right side of & */
4347 if (!PyDictViewSet_Check(self)) {
4348 PyObject *tmp = other;
4349 other = self;
4350 self = tmp;
4351 }
4352
4353 len_self = dictview_len((_PyDictViewObject *)self);
4354
4355 /* if other is a set and self is smaller than other,
4356 reuse set intersection logic */
Dong-hee Na1b55b652020-02-17 19:09:15 +09004357 if (Py_IS_TYPE(other, &PySet_Type) && len_self <= PyObject_Size(other)) {
Forest Gregg998cf1f2019-08-26 02:17:43 -05004358 _Py_IDENTIFIER(intersection);
4359 return _PyObject_CallMethodIdObjArgs(other, &PyId_intersection, self, NULL);
4360 }
4361
4362 /* if other is another dict view, and it is bigger than self,
4363 swap them */
4364 if (PyDictViewSet_Check(other)) {
4365 Py_ssize_t len_other = dictview_len((_PyDictViewObject *)other);
4366 if (len_other > len_self) {
4367 PyObject *tmp = other;
4368 other = self;
4369 self = tmp;
4370 }
4371 }
4372
4373 /* at this point, two things should be true
4374 1. self is a dictview
4375 2. if other is a dictview then it is smaller than self */
4376 result = PySet_New(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004377 if (result == NULL)
4378 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004379
Forest Gregg998cf1f2019-08-26 02:17:43 -05004380 it = PyObject_GetIter(other);
Zackery Spytzb16e3822019-10-13 05:49:05 -06004381 if (it == NULL) {
4382 Py_DECREF(result);
4383 return NULL;
4384 }
Forest Gregg998cf1f2019-08-26 02:17:43 -05004385
Forest Gregg998cf1f2019-08-26 02:17:43 -05004386 if (PyDictKeys_Check(self)) {
4387 dict_contains = dictkeys_contains;
4388 }
4389 /* else PyDictItems_Check(self) */
4390 else {
4391 dict_contains = dictitems_contains;
4392 }
4393
4394 while ((key = PyIter_Next(it)) != NULL) {
4395 rv = dict_contains((_PyDictViewObject *)self, key);
4396 if (rv < 0) {
4397 goto error;
4398 }
4399 if (rv) {
4400 if (PySet_Add(result, key)) {
4401 goto error;
4402 }
4403 }
4404 Py_DECREF(key);
4405 }
4406 Py_DECREF(it);
4407 if (PyErr_Occurred()) {
4408 Py_DECREF(result);
4409 return NULL;
4410 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004411 return result;
Forest Gregg998cf1f2019-08-26 02:17:43 -05004412
4413error:
4414 Py_DECREF(it);
4415 Py_DECREF(result);
4416 Py_DECREF(key);
4417 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004418}
4419
4420static PyObject*
4421dictviews_or(PyObject* self, PyObject *other)
4422{
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004423 PyObject *result = dictviews_to_set(self);
4424 if (result == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004425 return NULL;
4426 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004427
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004428 if (_PySet_Update(result, other) < 0) {
4429 Py_DECREF(result);
4430 return NULL;
4431 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004432 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004433}
4434
Dennis Sweeney07d81122020-06-10 01:56:56 -04004435static PyObject *
4436dictitems_xor(PyObject *self, PyObject *other)
4437{
4438 assert(PyDictItems_Check(self));
4439 assert(PyDictItems_Check(other));
4440 PyObject *d1 = (PyObject *)((_PyDictViewObject *)self)->dv_dict;
4441 PyObject *d2 = (PyObject *)((_PyDictViewObject *)other)->dv_dict;
4442
4443 PyObject *temp_dict = PyDict_Copy(d1);
4444 if (temp_dict == NULL) {
4445 return NULL;
4446 }
4447 PyObject *result_set = PySet_New(NULL);
4448 if (result_set == NULL) {
4449 Py_CLEAR(temp_dict);
4450 return NULL;
4451 }
4452
4453 PyObject *key = NULL, *val1 = NULL, *val2 = NULL;
4454 Py_ssize_t pos = 0;
4455 Py_hash_t hash;
4456
4457 while (_PyDict_Next(d2, &pos, &key, &val2, &hash)) {
4458 Py_INCREF(key);
4459 Py_INCREF(val2);
4460 val1 = _PyDict_GetItem_KnownHash(temp_dict, key, hash);
4461
4462 int to_delete;
4463 if (val1 == NULL) {
4464 if (PyErr_Occurred()) {
4465 goto error;
4466 }
4467 to_delete = 0;
4468 }
4469 else {
4470 Py_INCREF(val1);
4471 to_delete = PyObject_RichCompareBool(val1, val2, Py_EQ);
4472 if (to_delete < 0) {
4473 goto error;
4474 }
4475 }
4476
4477 if (to_delete) {
4478 if (_PyDict_DelItem_KnownHash(temp_dict, key, hash) < 0) {
4479 goto error;
4480 }
4481 }
4482 else {
4483 PyObject *pair = PyTuple_Pack(2, key, val2);
4484 if (pair == NULL) {
4485 goto error;
4486 }
4487 if (PySet_Add(result_set, pair) < 0) {
4488 Py_DECREF(pair);
4489 goto error;
4490 }
4491 Py_DECREF(pair);
4492 }
4493 Py_DECREF(key);
4494 Py_XDECREF(val1);
4495 Py_DECREF(val2);
4496 }
4497 key = val1 = val2 = NULL;
4498
4499 _Py_IDENTIFIER(items);
4500 PyObject *remaining_pairs = _PyObject_CallMethodIdNoArgs(temp_dict,
4501 &PyId_items);
4502 if (remaining_pairs == NULL) {
4503 goto error;
4504 }
4505 if (_PySet_Update(result_set, remaining_pairs) < 0) {
4506 Py_DECREF(remaining_pairs);
4507 goto error;
4508 }
4509 Py_DECREF(temp_dict);
4510 Py_DECREF(remaining_pairs);
4511 return result_set;
4512
4513error:
4514 Py_XDECREF(temp_dict);
4515 Py_XDECREF(result_set);
4516 Py_XDECREF(key);
4517 Py_XDECREF(val1);
4518 Py_XDECREF(val2);
4519 return NULL;
4520}
4521
Guido van Rossum523259b2007-08-24 23:41:22 +00004522static PyObject*
4523dictviews_xor(PyObject* self, PyObject *other)
4524{
Dennis Sweeney07d81122020-06-10 01:56:56 -04004525 if (PyDictItems_Check(self) && PyDictItems_Check(other)) {
4526 return dictitems_xor(self, other);
4527 }
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004528 PyObject *result = dictviews_to_set(self);
4529 if (result == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004530 return NULL;
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004531 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004532
Inada Naoki6cbc84f2019-11-08 00:59:04 +09004533 _Py_IDENTIFIER(symmetric_difference_update);
4534 PyObject *tmp = _PyObject_CallMethodIdOneArg(
4535 result, &PyId_symmetric_difference_update, other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004536 if (tmp == NULL) {
4537 Py_DECREF(result);
4538 return NULL;
4539 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004541 Py_DECREF(tmp);
4542 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004543}
4544
4545static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004546 0, /*nb_add*/
4547 (binaryfunc)dictviews_sub, /*nb_subtract*/
4548 0, /*nb_multiply*/
4549 0, /*nb_remainder*/
4550 0, /*nb_divmod*/
4551 0, /*nb_power*/
4552 0, /*nb_negative*/
4553 0, /*nb_positive*/
4554 0, /*nb_absolute*/
4555 0, /*nb_bool*/
4556 0, /*nb_invert*/
4557 0, /*nb_lshift*/
4558 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004559 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004560 (binaryfunc)dictviews_xor, /*nb_xor*/
4561 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00004562};
4563
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004564static PyObject*
4565dictviews_isdisjoint(PyObject *self, PyObject *other)
4566{
4567 PyObject *it;
4568 PyObject *item = NULL;
4569
4570 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06004571 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004572 Py_RETURN_TRUE;
4573 else
4574 Py_RETURN_FALSE;
4575 }
4576
4577 /* Iterate over the shorter object (only if other is a set,
4578 * because PySequence_Contains may be expensive otherwise): */
4579 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06004580 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004581 Py_ssize_t len_other = PyObject_Size(other);
4582 if (len_other == -1)
4583 return NULL;
4584
4585 if ((len_other > len_self)) {
4586 PyObject *tmp = other;
4587 other = self;
4588 self = tmp;
4589 }
4590 }
4591
4592 it = PyObject_GetIter(other);
4593 if (it == NULL)
4594 return NULL;
4595
4596 while ((item = PyIter_Next(it)) != NULL) {
4597 int contains = PySequence_Contains(self, item);
4598 Py_DECREF(item);
4599 if (contains == -1) {
4600 Py_DECREF(it);
4601 return NULL;
4602 }
4603
4604 if (contains) {
4605 Py_DECREF(it);
4606 Py_RETURN_FALSE;
4607 }
4608 }
4609 Py_DECREF(it);
4610 if (PyErr_Occurred())
4611 return NULL; /* PyIter_Next raised an exception. */
4612 Py_RETURN_TRUE;
4613}
4614
4615PyDoc_STRVAR(isdisjoint_doc,
4616"Return True if the view and the given iterable have a null intersection.");
4617
Serhiy Storchaka81524022018-11-27 13:05:02 +02004618static PyObject* dictkeys_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored));
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004619
4620PyDoc_STRVAR(reversed_keys_doc,
4621"Return a reverse iterator over the dict keys.");
4622
Guido van Rossumb90c8482007-02-10 01:11:45 +00004623static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004624 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4625 isdisjoint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004626 {"__reversed__", (PyCFunction)(void(*)(void))dictkeys_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004627 reversed_keys_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004628 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004629};
4630
4631PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004632 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4633 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004634 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004635 0, /* tp_itemsize */
4636 /* methods */
4637 (destructor)dictview_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004638 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004639 0, /* tp_getattr */
4640 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004641 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004642 (reprfunc)dictview_repr, /* tp_repr */
4643 &dictviews_as_number, /* tp_as_number */
4644 &dictkeys_as_sequence, /* tp_as_sequence */
4645 0, /* tp_as_mapping */
4646 0, /* tp_hash */
4647 0, /* tp_call */
4648 0, /* tp_str */
4649 PyObject_GenericGetAttr, /* tp_getattro */
4650 0, /* tp_setattro */
4651 0, /* tp_as_buffer */
4652 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4653 0, /* tp_doc */
4654 (traverseproc)dictview_traverse, /* tp_traverse */
4655 0, /* tp_clear */
4656 dictview_richcompare, /* tp_richcompare */
4657 0, /* tp_weaklistoffset */
4658 (getiterfunc)dictkeys_iter, /* tp_iter */
4659 0, /* tp_iternext */
4660 dictkeys_methods, /* tp_methods */
Dennis Sweeney3ee0e482020-06-12 13:19:25 -04004661 .tp_getset = dictview_getset,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004662};
4663
4664static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304665dictkeys_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004666{
Eric Snow96c6af92015-05-29 22:21:39 -06004667 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004668}
4669
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004670static PyObject *
Serhiy Storchaka81524022018-11-27 13:05:02 +02004671dictkeys_reversed(_PyDictViewObject *dv, PyObject *Py_UNUSED(ignored))
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004672{
4673 if (dv->dv_dict == NULL) {
4674 Py_RETURN_NONE;
4675 }
4676 return dictiter_new(dv->dv_dict, &PyDictRevIterKey_Type);
4677}
4678
Guido van Rossum3ac67412007-02-10 18:55:06 +00004679/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004680
4681static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004682dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004683{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004684 if (dv->dv_dict == NULL) {
4685 Py_RETURN_NONE;
4686 }
4687 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004688}
4689
4690static int
Eric Snow96c6af92015-05-29 22:21:39 -06004691dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004692{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004693 int result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004694 PyObject *key, *value, *found;
4695 if (dv->dv_dict == NULL)
4696 return 0;
4697 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
4698 return 0;
4699 key = PyTuple_GET_ITEM(obj, 0);
4700 value = PyTuple_GET_ITEM(obj, 1);
Raymond Hettinger6692f012016-09-18 21:46:08 -07004701 found = PyDict_GetItemWithError((PyObject *)dv->dv_dict, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004702 if (found == NULL) {
4703 if (PyErr_Occurred())
4704 return -1;
4705 return 0;
4706 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004707 Py_INCREF(found);
Serhiy Storchaka18b711c2019-08-04 14:12:48 +03004708 result = PyObject_RichCompareBool(found, value, Py_EQ);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004709 Py_DECREF(found);
4710 return result;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004711}
4712
Guido van Rossum83825ac2007-02-10 04:54:19 +00004713static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004714 (lenfunc)dictview_len, /* sq_length */
4715 0, /* sq_concat */
4716 0, /* sq_repeat */
4717 0, /* sq_item */
4718 0, /* sq_slice */
4719 0, /* sq_ass_item */
4720 0, /* sq_ass_slice */
4721 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004722};
4723
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004724static PyObject* dictitems_reversed(_PyDictViewObject *dv);
4725
4726PyDoc_STRVAR(reversed_items_doc,
4727"Return a reverse iterator over the dict items.");
4728
Guido van Rossumb90c8482007-02-10 01:11:45 +00004729static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004730 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4731 isdisjoint_doc},
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004732 {"__reversed__", (PyCFunction)(void(*)(void))dictitems_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004733 reversed_items_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004734 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004735};
4736
4737PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004738 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4739 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004740 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004741 0, /* tp_itemsize */
4742 /* methods */
4743 (destructor)dictview_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004744 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004745 0, /* tp_getattr */
4746 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004747 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004748 (reprfunc)dictview_repr, /* tp_repr */
4749 &dictviews_as_number, /* tp_as_number */
4750 &dictitems_as_sequence, /* tp_as_sequence */
4751 0, /* tp_as_mapping */
4752 0, /* tp_hash */
4753 0, /* tp_call */
4754 0, /* tp_str */
4755 PyObject_GenericGetAttr, /* tp_getattro */
4756 0, /* tp_setattro */
4757 0, /* tp_as_buffer */
4758 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4759 0, /* tp_doc */
4760 (traverseproc)dictview_traverse, /* tp_traverse */
4761 0, /* tp_clear */
4762 dictview_richcompare, /* tp_richcompare */
4763 0, /* tp_weaklistoffset */
4764 (getiterfunc)dictitems_iter, /* tp_iter */
4765 0, /* tp_iternext */
4766 dictitems_methods, /* tp_methods */
Dennis Sweeney3ee0e482020-06-12 13:19:25 -04004767 .tp_getset = dictview_getset,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004768};
4769
4770static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304771dictitems_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004772{
Eric Snow96c6af92015-05-29 22:21:39 -06004773 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004774}
4775
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004776static PyObject *
4777dictitems_reversed(_PyDictViewObject *dv)
4778{
4779 if (dv->dv_dict == NULL) {
4780 Py_RETURN_NONE;
4781 }
4782 return dictiter_new(dv->dv_dict, &PyDictRevIterItem_Type);
4783}
4784
Guido van Rossum3ac67412007-02-10 18:55:06 +00004785/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004786
4787static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004788dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004789{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004790 if (dv->dv_dict == NULL) {
4791 Py_RETURN_NONE;
4792 }
4793 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004794}
4795
Guido van Rossum83825ac2007-02-10 04:54:19 +00004796static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004797 (lenfunc)dictview_len, /* sq_length */
4798 0, /* sq_concat */
4799 0, /* sq_repeat */
4800 0, /* sq_item */
4801 0, /* sq_slice */
4802 0, /* sq_ass_item */
4803 0, /* sq_ass_slice */
4804 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004805};
4806
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004807static PyObject* dictvalues_reversed(_PyDictViewObject *dv);
4808
4809PyDoc_STRVAR(reversed_values_doc,
4810"Return a reverse iterator over the dict values.");
4811
Guido van Rossumb90c8482007-02-10 01:11:45 +00004812static PyMethodDef dictvalues_methods[] = {
Serhiy Storchaka62be7422018-11-27 13:27:31 +02004813 {"__reversed__", (PyCFunction)(void(*)(void))dictvalues_reversed, METH_NOARGS,
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004814 reversed_values_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004815 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004816};
4817
4818PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004819 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4820 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004821 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004822 0, /* tp_itemsize */
4823 /* methods */
4824 (destructor)dictview_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004825 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004826 0, /* tp_getattr */
4827 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02004828 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004829 (reprfunc)dictview_repr, /* tp_repr */
4830 0, /* tp_as_number */
4831 &dictvalues_as_sequence, /* tp_as_sequence */
4832 0, /* tp_as_mapping */
4833 0, /* tp_hash */
4834 0, /* tp_call */
4835 0, /* tp_str */
4836 PyObject_GenericGetAttr, /* tp_getattro */
4837 0, /* tp_setattro */
4838 0, /* tp_as_buffer */
4839 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4840 0, /* tp_doc */
4841 (traverseproc)dictview_traverse, /* tp_traverse */
4842 0, /* tp_clear */
4843 0, /* tp_richcompare */
4844 0, /* tp_weaklistoffset */
4845 (getiterfunc)dictvalues_iter, /* tp_iter */
4846 0, /* tp_iternext */
4847 dictvalues_methods, /* tp_methods */
Dennis Sweeney3ee0e482020-06-12 13:19:25 -04004848 .tp_getset = dictview_getset,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004849};
4850
4851static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05304852dictvalues_new(PyObject *dict, PyObject *Py_UNUSED(ignored))
Guido van Rossumb90c8482007-02-10 01:11:45 +00004853{
Eric Snow96c6af92015-05-29 22:21:39 -06004854 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004855}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004856
Rémi Lapeyre6531bf62018-11-06 01:38:54 +01004857static PyObject *
4858dictvalues_reversed(_PyDictViewObject *dv)
4859{
4860 if (dv->dv_dict == NULL) {
4861 Py_RETURN_NONE;
4862 }
4863 return dictiter_new(dv->dv_dict, &PyDictRevIterValue_Type);
4864}
4865
4866
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004867/* Returns NULL if cannot allocate a new PyDictKeysObject,
4868 but does not set an error */
4869PyDictKeysObject *
4870_PyDict_NewKeysForClass(void)
4871{
Victor Stinner742da042016-09-07 17:40:12 -07004872 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004873 if (keys == NULL)
4874 PyErr_Clear();
4875 else
4876 keys->dk_lookup = lookdict_split;
4877 return keys;
4878}
4879
4880#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
4881
4882PyObject *
4883PyObject_GenericGetDict(PyObject *obj, void *context)
4884{
4885 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
4886 if (dictptr == NULL) {
4887 PyErr_SetString(PyExc_AttributeError,
4888 "This object has no __dict__");
4889 return NULL;
4890 }
4891 dict = *dictptr;
4892 if (dict == NULL) {
4893 PyTypeObject *tp = Py_TYPE(obj);
4894 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
INADA Naokia7576492018-11-14 18:39:27 +09004895 dictkeys_incref(CACHED_KEYS(tp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004896 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
4897 }
4898 else {
4899 *dictptr = dict = PyDict_New();
4900 }
4901 }
4902 Py_XINCREF(dict);
4903 return dict;
4904}
4905
4906int
4907_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
Victor Stinner742da042016-09-07 17:40:12 -07004908 PyObject *key, PyObject *value)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004909{
4910 PyObject *dict;
4911 int res;
4912 PyDictKeysObject *cached;
4913
4914 assert(dictptr != NULL);
4915 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
4916 assert(dictptr != NULL);
4917 dict = *dictptr;
4918 if (dict == NULL) {
INADA Naokia7576492018-11-14 18:39:27 +09004919 dictkeys_incref(cached);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004920 dict = new_dict_with_shared_keys(cached);
4921 if (dict == NULL)
4922 return -1;
4923 *dictptr = dict;
4924 }
4925 if (value == NULL) {
4926 res = PyDict_DelItem(dict, key);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004927 // Since key sharing dict doesn't allow deletion, PyDict_DelItem()
4928 // always converts dict to combined form.
4929 if ((cached = CACHED_KEYS(tp)) != NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004930 CACHED_KEYS(tp) = NULL;
INADA Naokia7576492018-11-14 18:39:27 +09004931 dictkeys_decref(cached);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004932 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01004933 }
4934 else {
INADA Naoki2294f3a2017-02-12 13:51:30 +09004935 int was_shared = (cached == ((PyDictObject *)dict)->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004936 res = PyDict_SetItem(dict, key, value);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004937 if (was_shared &&
4938 (cached = CACHED_KEYS(tp)) != NULL &&
4939 cached != ((PyDictObject *)dict)->ma_keys) {
Victor Stinner3d3f2642016-12-15 17:21:23 +01004940 /* PyDict_SetItem() may call dictresize and convert split table
4941 * into combined table. In such case, convert it to split
4942 * table again and update type's shared key only when this is
4943 * the only dict sharing key with the type.
4944 *
4945 * This is to allow using shared key in class like this:
4946 *
4947 * class C:
4948 * def __init__(self):
4949 * # one dict resize happens
4950 * self.a, self.b, self.c = 1, 2, 3
4951 * self.d, self.e, self.f = 4, 5, 6
4952 * a = C()
4953 */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004954 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004955 CACHED_KEYS(tp) = make_keys_shared(dict);
Victor Stinner742da042016-09-07 17:40:12 -07004956 }
4957 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004958 CACHED_KEYS(tp) = NULL;
4959 }
INADA Naokia7576492018-11-14 18:39:27 +09004960 dictkeys_decref(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004961 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
4962 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004963 }
4964 }
4965 } else {
4966 dict = *dictptr;
4967 if (dict == NULL) {
4968 dict = PyDict_New();
4969 if (dict == NULL)
4970 return -1;
4971 *dictptr = dict;
4972 }
4973 if (value == NULL) {
4974 res = PyDict_DelItem(dict, key);
4975 } else {
4976 res = PyDict_SetItem(dict, key, value);
4977 }
4978 }
4979 return res;
4980}
4981
4982void
4983_PyDictKeys_DecRef(PyDictKeysObject *keys)
4984{
INADA Naokia7576492018-11-14 18:39:27 +09004985 dictkeys_decref(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004986}