blob: 7a1bcebec6fdfd30b4ffe108f6540e65313c64b9 [file] [log] [blame]
Guido van Rossum2bc13791999-03-24 19:06:42 +00001/* Dictionary object implementation using a hash table */
Guido van Rossum9bfef441993-03-29 10:43:31 +00002
Raymond Hettinger930427b2003-05-03 06:51:59 +00003/* The distribution includes a separate file, Objects/dictnotes.txt,
Tim Peters60b29962006-01-01 01:19:23 +00004 describing explorations into dictionary design and optimization.
Raymond Hettinger930427b2003-05-03 06:51:59 +00005 It covers typical dictionary use patterns, the parameters for
6 tuning dictionaries, and several ideas for possible optimizations.
7*/
8
Victor Stinner742da042016-09-07 17:40:12 -07009/* PyDictKeysObject
10
11This implements the dictionary's hashtable.
12
Raymond Hettingerb12785d2016-10-22 09:58:14 -070013As of Python 3.6, this is compact and ordered. Basic idea is described here:
14* https://mail.python.org/pipermail/python-dev/2012-December/123028.html
15* https://morepypy.blogspot.com/2015/01/faster-more-memory-efficient-and-more.html
Victor Stinner742da042016-09-07 17:40:12 -070016
17layout:
18
19+---------------+
20| dk_refcnt |
21| dk_size |
22| dk_lookup |
23| dk_usable |
24| dk_nentries |
25+---------------+
26| dk_indices |
27| |
28+---------------+
29| dk_entries |
30| |
31+---------------+
32
33dk_indices is actual hashtable. It holds index in entries, or DKIX_EMPTY(-1)
34or DKIX_DUMMY(-2).
35Size of indices is dk_size. Type of each index in indices is vary on dk_size:
36
37* int8 for dk_size <= 128
38* int16 for 256 <= dk_size <= 2**15
39* int32 for 2**16 <= dk_size <= 2**31
40* int64 for 2**32 <= dk_size
41
42dk_entries is array of PyDictKeyEntry. It's size is USABLE_FRACTION(dk_size).
43DK_ENTRIES(dk) can be used to get pointer to entries.
44
45NOTE: Since negative value is used for DKIX_EMPTY and DKIX_DUMMY, type of
46dk_indices entry is signed integer and int16 is used for table which
47dk_size == 256.
48*/
49
Benjamin Peterson7d95e402012-04-23 11:24:50 -040050
51/*
Benjamin Peterson7d95e402012-04-23 11:24:50 -040052The DictObject can be in one of two forms.
Victor Stinner742da042016-09-07 17:40:12 -070053
Benjamin Peterson7d95e402012-04-23 11:24:50 -040054Either:
55 A combined table:
56 ma_values == NULL, dk_refcnt == 1.
57 Values are stored in the me_value field of the PyDictKeysObject.
Benjamin Peterson7d95e402012-04-23 11:24:50 -040058Or:
59 A split table:
60 ma_values != NULL, dk_refcnt >= 1
61 Values are stored in the ma_values array.
Victor Stinner742da042016-09-07 17:40:12 -070062 Only string (unicode) keys are allowed.
63 All dicts sharing same key must have same insertion order.
Benjamin Peterson7d95e402012-04-23 11:24:50 -040064
Victor Stinner742da042016-09-07 17:40:12 -070065There are four kinds of slots in the table (slot is index, and
66DK_ENTRIES(keys)[index] if index >= 0):
67
681. Unused. index == DKIX_EMPTY
69 Does not hold an active (key, value) pair now and never did. Unused can
70 transition to Active upon key insertion. This is each slot's initial state.
71
722. Active. index >= 0, me_key != NULL and me_value != NULL
73 Holds an active (key, value) pair. Active can transition to Dummy or
74 Pending upon key deletion (for combined and split tables respectively).
75 This is the only case in which me_value != NULL.
76
773. Dummy. index == DKIX_DUMMY (combined only)
78 Previously held an active (key, value) pair, but that was deleted and an
79 active pair has not yet overwritten the slot. Dummy can transition to
80 Active upon key insertion. Dummy slots cannot be made Unused again
81 else the probe sequence in case of collision would have no way to know
82 they were once active.
83
844. Pending. index >= 0, key != NULL, and value == NULL (split only)
85 Not yet inserted in split-table.
Benjamin Peterson7d95e402012-04-23 11:24:50 -040086*/
87
Victor Stinner742da042016-09-07 17:40:12 -070088/*
89Preserving insertion order
Benjamin Peterson7d95e402012-04-23 11:24:50 -040090
Victor Stinner742da042016-09-07 17:40:12 -070091It's simple for combined table. Since dk_entries is mostly append only, we can
92get insertion order by just iterating dk_entries.
93
94One exception is .popitem(). It removes last item in dk_entries and decrement
95dk_nentries to achieve amortized O(1). Since there are DKIX_DUMMY remains in
96dk_indices, we can't increment dk_usable even though dk_nentries is
97decremented.
98
99In split table, inserting into pending entry is allowed only for dk_entries[ix]
100where ix == mp->ma_used. Inserting into other index and deleting item cause
101converting the dict to the combined table.
102*/
103
104/* PyDict_MINSIZE is the starting size for any new dict.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400105 * 8 allows dicts with no more than 5 active entries; experiments suggested
106 * this suffices for the majority of dicts (consisting mostly of usually-small
107 * dicts created to pass keyword arguments).
108 * Making this 8, rather than 4 reduces the number of resizes for most
109 * dictionaries, without any significant extra memory use.
110 */
Victor Stinner742da042016-09-07 17:40:12 -0700111#define PyDict_MINSIZE 8
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400112
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000113#include "Python.h"
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600114#include "internal/pystate.h"
Eric Snow96c6af92015-05-29 22:21:39 -0600115#include "dict-common.h"
Victor Stinner990397e2016-09-09 20:22:59 -0700116#include "stringlib/eq.h" /* to get unicode_eq() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000117
Larry Hastings61272b72014-01-07 12:41:53 -0800118/*[clinic input]
Larry Hastingsc2047262014-01-25 20:43:29 -0800119class dict "PyDictObject *" "&PyDict_Type"
Larry Hastings61272b72014-01-07 12:41:53 -0800120[clinic start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -0800121/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f157a5a0ce9589d6]*/
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800122
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400123
124/*
125To ensure the lookup algorithm terminates, there must be at least one Unused
126slot (NULL key) in the table.
127To avoid slowing down lookups on a near-full table, we resize the table when
128it's USABLE_FRACTION (currently two-thirds) full.
129*/
Guido van Rossum16e93a81997-01-28 00:00:11 +0000130
Tim Peterseb28ef22001-06-02 05:27:19 +0000131#define PERTURB_SHIFT 5
132
Guido van Rossum16e93a81997-01-28 00:00:11 +0000133/*
Tim Peterseb28ef22001-06-02 05:27:19 +0000134Major subtleties ahead: Most hash schemes depend on having a "good" hash
135function, in the sense of simulating randomness. Python doesn't: its most
R David Murray537ad7a2016-07-10 12:33:18 -0400136important hash functions (for ints) are very regular in common
Tim Peterseb28ef22001-06-02 05:27:19 +0000137cases:
Tim Peters15d49292001-05-27 07:39:22 +0000138
R David Murray537ad7a2016-07-10 12:33:18 -0400139 >>>[hash(i) for i in range(4)]
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000140 [0, 1, 2, 3]
Tim Peters15d49292001-05-27 07:39:22 +0000141
Tim Peterseb28ef22001-06-02 05:27:19 +0000142This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
143the low-order i bits as the initial table index is extremely fast, and there
R David Murray537ad7a2016-07-10 12:33:18 -0400144are no collisions at all for dicts indexed by a contiguous range of ints. So
145this gives better-than-random behavior in common cases, and that's very
146desirable.
Tim Peters15d49292001-05-27 07:39:22 +0000147
Tim Peterseb28ef22001-06-02 05:27:19 +0000148OTOH, when collisions occur, the tendency to fill contiguous slices of the
149hash table makes a good collision resolution strategy crucial. Taking only
150the last i bits of the hash code is also vulnerable: for example, consider
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000152their own hash codes, and this fits in a dict of size 2**15, the last 15 bits
153 of every hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +0000154
Tim Peterseb28ef22001-06-02 05:27:19 +0000155But catering to unusual cases should not slow the usual ones, so we just take
156the last i bits anyway. It's up to collision resolution to do the rest. If
157we *usually* find the key we're looking for on the first try (and, it turns
158out, we usually do -- the table load factor is kept under 2/3, so the odds
159are solidly in our favor), then it makes best sense to keep the initial index
160computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +0000161
Tim Peterseb28ef22001-06-02 05:27:19 +0000162The first half of collision resolution is to visit table indices via this
163recurrence:
Tim Peters15d49292001-05-27 07:39:22 +0000164
Tim Peterseb28ef22001-06-02 05:27:19 +0000165 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +0000166
Tim Peterseb28ef22001-06-02 05:27:19 +0000167For any initial j in range(2**i), repeating that 2**i times generates each
168int in range(2**i) exactly once (see any text on random-number generation for
169proof). By itself, this doesn't help much: like linear probing (setting
170j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
171order. This would be bad, except that's not the only thing we do, and it's
172actually *good* in the common cases where hash keys are consecutive. In an
173example that's really too small to make this entirely clear, for a table of
174size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +0000175
Tim Peterseb28ef22001-06-02 05:27:19 +0000176 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
177
178If two things come in at index 5, the first place we look after is index 2,
179not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
180Linear probing is deadly in this case because there the fixed probe order
181is the *same* as the order consecutive keys are likely to arrive. But it's
182extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
183and certain that consecutive hash codes do not.
184
185The other half of the strategy is to get the other bits of the hash code
186into play. This is done by initializing a (unsigned) vrbl "perturb" to the
187full hash code, and changing the recurrence to:
188
Tim Peterseb28ef22001-06-02 05:27:19 +0000189 perturb >>= PERTURB_SHIFT;
INADA Naoki267941c2016-10-06 15:19:07 +0900190 j = (5*j) + 1 + perturb;
Tim Peterseb28ef22001-06-02 05:27:19 +0000191 use j % 2**i as the next table index;
192
193Now the probe sequence depends (eventually) on every bit in the hash code,
194and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
195because it quickly magnifies small differences in the bits that didn't affect
196the initial index. Note that because perturb is unsigned, if the recurrence
197is executed often enough perturb eventually becomes and remains 0. At that
198point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
199that's certain to find an empty slot eventually (since it generates every int
200in range(2**i), and we make sure there's always at least one empty slot).
201
202Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
203small so that the high bits of the hash code continue to affect the probe
204sequence across iterations; but you want it large so that in really bad cases
205the high-order hash bits have an effect on early iterations. 5 was "the
206best" in minimizing total collisions across experiments Tim Peters ran (on
207both normal and pathological cases), but 4 and 6 weren't significantly worse.
208
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000209Historical: Reimer Behrends contributed the idea of using a polynomial-based
Tim Peterseb28ef22001-06-02 05:27:19 +0000210approach, using repeated multiplication by x in GF(2**n) where an irreducible
211polynomial for each table size was chosen such that x was a primitive root.
212Christian Tismer later extended that to use division by x instead, as an
213efficient way to get the high bits of the hash code into play. This scheme
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000214also gave excellent collision statistics, but was more expensive: two
215if-tests were required inside the loop; computing "the next" index took about
216the same number of operations but without as much potential parallelism
217(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
218above, and then shifting perturb can be done while the table index is being
219masked); and the PyDictObject struct required a member to hold the table's
220polynomial. In Tim's experiments the current scheme ran faster, produced
221equally good collision statistics, needed less code & used less memory.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000222
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000223*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000224
Fred Drake1bff34a2000-08-31 19:31:38 +0000225/* forward declarations */
Victor Stinner742da042016-09-07 17:40:12 -0700226static Py_ssize_t lookdict(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900227 Py_hash_t hash, PyObject **value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700228static Py_ssize_t lookdict_unicode(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900229 Py_hash_t hash, PyObject **value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700230static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400231lookdict_unicode_nodummy(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 lookdict_split(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900234 Py_hash_t hash, PyObject **value_addr);
Fred Drake1bff34a2000-08-31 19:31:38 +0000235
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400236static int dictresize(PyDictObject *mp, Py_ssize_t minused);
Tim Petersdea48ec2001-05-22 20:40:22 +0000237
Benjamin Peterson3c569292016-09-08 13:16:41 -0700238/*Global counter used to set ma_version_tag field of dictionary.
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700239 * It is incremented each time that a dictionary is created and each
240 * time that a dictionary is modified. */
241static uint64_t pydict_global_version = 0;
242
243#define DICT_NEXT_VERSION() (++pydict_global_version)
244
Victor Stinner742da042016-09-07 17:40:12 -0700245/* Dictionary reuse scheme to save calls to malloc and free */
Christian Heimes2202f872008-02-06 14:31:34 +0000246#ifndef PyDict_MAXFREELIST
247#define PyDict_MAXFREELIST 80
248#endif
249static PyDictObject *free_list[PyDict_MAXFREELIST];
250static int numfree = 0;
Victor Stinner742da042016-09-07 17:40:12 -0700251static PyDictKeysObject *keys_free_list[PyDict_MAXFREELIST];
252static int numfreekeys = 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000253
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300254#include "clinic/dictobject.c.h"
255
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100256int
257PyDict_ClearFreeList(void)
Christian Heimes77c02eb2008-02-09 02:18:51 +0000258{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 PyDictObject *op;
Victor Stinner742da042016-09-07 17:40:12 -0700260 int ret = numfree + numfreekeys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 while (numfree) {
262 op = free_list[--numfree];
263 assert(PyDict_CheckExact(op));
264 PyObject_GC_Del(op);
265 }
Victor Stinner742da042016-09-07 17:40:12 -0700266 while (numfreekeys) {
267 PyObject_FREE(keys_free_list[--numfreekeys]);
268 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100269 return ret;
270}
271
David Malcolm49526f42012-06-22 14:55:41 -0400272/* Print summary info about the state of the optimized allocator */
273void
274_PyDict_DebugMallocStats(FILE *out)
275{
276 _PyDebugAllocatorStats(out,
277 "free PyDictObject", numfree, sizeof(PyDictObject));
278}
279
280
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100281void
282PyDict_Fini(void)
283{
284 PyDict_ClearFreeList();
Christian Heimes77c02eb2008-02-09 02:18:51 +0000285}
286
Victor Stinner742da042016-09-07 17:40:12 -0700287#define DK_SIZE(dk) ((dk)->dk_size)
288#if SIZEOF_VOID_P > 4
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700289#define DK_IXSIZE(dk) \
290 (DK_SIZE(dk) <= 0xff ? \
291 1 : DK_SIZE(dk) <= 0xffff ? \
292 2 : DK_SIZE(dk) <= 0xffffffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700293 4 : sizeof(int64_t))
Victor Stinner742da042016-09-07 17:40:12 -0700294#else
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700295#define DK_IXSIZE(dk) \
296 (DK_SIZE(dk) <= 0xff ? \
297 1 : DK_SIZE(dk) <= 0xffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700298 2 : sizeof(int32_t))
Victor Stinner742da042016-09-07 17:40:12 -0700299#endif
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700300#define DK_ENTRIES(dk) \
Miss Islington (bot)392520b2018-04-20 10:06:21 -0700301 ((PyDictKeyEntry*)(&((int8_t*)((dk)->dk_indices))[DK_SIZE(dk) * DK_IXSIZE(dk)]))
Victor Stinner742da042016-09-07 17:40:12 -0700302
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200303#define DK_DEBUG_INCREF _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA
304#define DK_DEBUG_DECREF _Py_DEC_REFTOTAL _Py_REF_DEBUG_COMMA
305
306#define DK_INCREF(dk) (DK_DEBUG_INCREF ++(dk)->dk_refcnt)
307#define DK_DECREF(dk) if (DK_DEBUG_DECREF (--(dk)->dk_refcnt) == 0) free_keys_object(dk)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400308#define DK_MASK(dk) (((dk)->dk_size)-1)
309#define IS_POWER_OF_2(x) (((x) & (x-1)) == 0)
310
Victor Stinner742da042016-09-07 17:40:12 -0700311/* lookup indices. returns DKIX_EMPTY, DKIX_DUMMY, or ix >=0 */
Benjamin Peterson73222252016-09-08 09:58:47 -0700312static inline Py_ssize_t
Victor Stinner742da042016-09-07 17:40:12 -0700313dk_get_index(PyDictKeysObject *keys, Py_ssize_t i)
314{
315 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700316 Py_ssize_t ix;
317
Victor Stinner742da042016-09-07 17:40:12 -0700318 if (s <= 0xff) {
Miss Islington (bot)392520b2018-04-20 10:06:21 -0700319 int8_t *indices = (int8_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700320 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700321 }
322 else if (s <= 0xffff) {
Miss Islington (bot)392520b2018-04-20 10:06:21 -0700323 int16_t *indices = (int16_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700324 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700325 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700326#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300327 else if (s > 0xffffffff) {
Miss Islington (bot)392520b2018-04-20 10:06:21 -0700328 int64_t *indices = (int64_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700329 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700330 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700331#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300332 else {
Miss Islington (bot)392520b2018-04-20 10:06:21 -0700333 int32_t *indices = (int32_t*)(keys->dk_indices);
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300334 ix = indices[i];
335 }
Victor Stinner71211e32016-09-08 10:52:46 -0700336 assert(ix >= DKIX_DUMMY);
337 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700338}
339
340/* write to indices. */
Benjamin Peterson73222252016-09-08 09:58:47 -0700341static inline void
Victor Stinner742da042016-09-07 17:40:12 -0700342dk_set_index(PyDictKeysObject *keys, Py_ssize_t i, Py_ssize_t ix)
343{
344 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700345
346 assert(ix >= DKIX_DUMMY);
347
Victor Stinner742da042016-09-07 17:40:12 -0700348 if (s <= 0xff) {
Miss Islington (bot)392520b2018-04-20 10:06:21 -0700349 int8_t *indices = (int8_t*)(keys->dk_indices);
Victor Stinner71211e32016-09-08 10:52:46 -0700350 assert(ix <= 0x7f);
Victor Stinner208857e2016-09-08 11:35:46 -0700351 indices[i] = (char)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700352 }
353 else if (s <= 0xffff) {
Miss Islington (bot)392520b2018-04-20 10:06:21 -0700354 int16_t *indices = (int16_t*)(keys->dk_indices);
Victor Stinner71211e32016-09-08 10:52:46 -0700355 assert(ix <= 0x7fff);
Victor Stinner208857e2016-09-08 11:35:46 -0700356 indices[i] = (int16_t)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700357 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700358#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300359 else if (s > 0xffffffff) {
Miss Islington (bot)392520b2018-04-20 10:06:21 -0700360 int64_t *indices = (int64_t*)(keys->dk_indices);
Victor Stinner208857e2016-09-08 11:35:46 -0700361 indices[i] = ix;
Victor Stinner742da042016-09-07 17:40:12 -0700362 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700363#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300364 else {
Miss Islington (bot)392520b2018-04-20 10:06:21 -0700365 int32_t *indices = (int32_t*)(keys->dk_indices);
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300366 assert(ix <= 0x7fffffff);
367 indices[i] = (int32_t)ix;
368 }
Victor Stinner742da042016-09-07 17:40:12 -0700369}
370
371
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200372/* USABLE_FRACTION is the maximum dictionary load.
Victor Stinner742da042016-09-07 17:40:12 -0700373 * Increasing this ratio makes dictionaries more dense resulting in more
374 * collisions. Decreasing it improves sparseness at the expense of spreading
375 * indices over more cache lines and at the cost of total memory consumed.
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200376 *
377 * USABLE_FRACTION must obey the following:
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400378 * (0 < USABLE_FRACTION(n) < n) for all n >= 2
379 *
Victor Stinner742da042016-09-07 17:40:12 -0700380 * USABLE_FRACTION should be quick to calculate.
381 * Fractions around 1/2 to 2/3 seem to work well in practice.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400382 */
Victor Stinner742da042016-09-07 17:40:12 -0700383#define USABLE_FRACTION(n) (((n) << 1)/3)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400384
Victor Stinner742da042016-09-07 17:40:12 -0700385/* ESTIMATE_SIZE is reverse function of USABLE_FRACTION.
386 * This can be used to reserve enough size to insert n entries without
387 * resizing.
388 */
INADA Naoki92c50ee2016-11-22 00:57:02 +0900389#define ESTIMATE_SIZE(n) (((n)*3+1) >> 1)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400390
Victor Stinner742da042016-09-07 17:40:12 -0700391/* Alternative fraction that is otherwise close enough to 2n/3 to make
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400392 * little difference. 8 * 2/3 == 8 * 5/8 == 5. 16 * 2/3 == 16 * 5/8 == 10.
393 * 32 * 2/3 = 21, 32 * 5/8 = 20.
394 * Its advantage is that it is faster to compute on machines with slow division.
395 * #define USABLE_FRACTION(n) (((n) >> 1) + ((n) >> 2) - ((n) >> 3))
Victor Stinner742da042016-09-07 17:40:12 -0700396 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400397
Victor Stinnera9f61a52013-07-16 22:17:26 +0200398/* GROWTH_RATE. Growth rate upon hitting maximum load.
Miss Islington (bot)902bb622018-04-17 10:17:19 -0700399 * Currently set to used*3.
Victor Stinnera9f61a52013-07-16 22:17:26 +0200400 * This means that dicts double in size when growing without deletions,
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700401 * but have more head room when the number of deletions is on a par with the
Miss Islington (bot)902bb622018-04-17 10:17:19 -0700402 * number of insertions. See also bpo-17563 and bpo-33205.
403 *
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700404 * GROWTH_RATE was set to used*4 up to version 3.2.
405 * GROWTH_RATE was set to used*2 in version 3.3.0
Miss Islington (bot)902bb622018-04-17 10:17:19 -0700406 * GROWTH_RATE was set to used*2 + capacity/2 in 3.4.0-3.6.0.
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200407 */
Miss Islington (bot)902bb622018-04-17 10:17:19 -0700408#define GROWTH_RATE(d) ((d)->ma_used*3)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400409
410#define ENSURE_ALLOWS_DELETIONS(d) \
411 if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \
412 (d)->ma_keys->dk_lookup = lookdict_unicode; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400414
415/* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
416 * (which cannot fail and thus can do no allocation).
417 */
418static PyDictKeysObject empty_keys_struct = {
Serhiy Storchaka97932e42016-09-26 23:01:23 +0300419 1, /* dk_refcnt */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400420 1, /* dk_size */
421 lookdict_split, /* dk_lookup */
422 0, /* dk_usable (immutable) */
Victor Stinner742da042016-09-07 17:40:12 -0700423 0, /* dk_nentries */
Miss Islington (bot)392520b2018-04-20 10:06:21 -0700424 {DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY,
425 DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY}, /* dk_indices */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400426};
427
428static PyObject *empty_values[1] = { NULL };
429
430#define Py_EMPTY_KEYS &empty_keys_struct
431
Victor Stinner611b0fa2016-09-14 15:02:01 +0200432/* Uncomment to check the dict content in _PyDict_CheckConsistency() */
433/* #define DEBUG_PYDICT */
434
435
T. Woutersa00c3fd2017-03-31 09:14:41 -0700436#ifndef NDEBUG
Victor Stinner611b0fa2016-09-14 15:02:01 +0200437static int
438_PyDict_CheckConsistency(PyDictObject *mp)
439{
440 PyDictKeysObject *keys = mp->ma_keys;
441 int splitted = _PyDict_HasSplitTable(mp);
442 Py_ssize_t usable = USABLE_FRACTION(keys->dk_size);
443#ifdef DEBUG_PYDICT
444 PyDictKeyEntry *entries = DK_ENTRIES(keys);
445 Py_ssize_t i;
446#endif
447
448 assert(0 <= mp->ma_used && mp->ma_used <= usable);
449 assert(IS_POWER_OF_2(keys->dk_size));
450 assert(0 <= keys->dk_usable
451 && keys->dk_usable <= usable);
452 assert(0 <= keys->dk_nentries
453 && keys->dk_nentries <= usable);
454 assert(keys->dk_usable + keys->dk_nentries <= usable);
455
456 if (!splitted) {
457 /* combined table */
458 assert(keys->dk_refcnt == 1);
459 }
460
461#ifdef DEBUG_PYDICT
462 for (i=0; i < keys->dk_size; i++) {
463 Py_ssize_t ix = dk_get_index(keys, i);
464 assert(DKIX_DUMMY <= ix && ix <= usable);
465 }
466
467 for (i=0; i < usable; i++) {
468 PyDictKeyEntry *entry = &entries[i];
469 PyObject *key = entry->me_key;
470
471 if (key != NULL) {
472 if (PyUnicode_CheckExact(key)) {
473 Py_hash_t hash = ((PyASCIIObject *)key)->hash;
474 assert(hash != -1);
475 assert(entry->me_hash == hash);
476 }
477 else {
478 /* test_dict fails if PyObject_Hash() is called again */
479 assert(entry->me_hash != -1);
480 }
481 if (!splitted) {
482 assert(entry->me_value != NULL);
483 }
484 }
485
486 if (splitted) {
487 assert(entry->me_value == NULL);
488 }
489 }
490
491 if (splitted) {
492 /* splitted table */
493 for (i=0; i < mp->ma_used; i++) {
494 assert(mp->ma_values[i] != NULL);
495 }
496 }
497#endif
498
499 return 1;
500}
501#endif
502
503
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400504static PyDictKeysObject *new_keys_object(Py_ssize_t size)
505{
506 PyDictKeysObject *dk;
Victor Stinner742da042016-09-07 17:40:12 -0700507 Py_ssize_t es, usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400508
Victor Stinner742da042016-09-07 17:40:12 -0700509 assert(size >= PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400510 assert(IS_POWER_OF_2(size));
Victor Stinner742da042016-09-07 17:40:12 -0700511
512 usable = USABLE_FRACTION(size);
513 if (size <= 0xff) {
514 es = 1;
515 }
516 else if (size <= 0xffff) {
517 es = 2;
518 }
519#if SIZEOF_VOID_P > 4
520 else if (size <= 0xffffffff) {
521 es = 4;
522 }
523#endif
524 else {
525 es = sizeof(Py_ssize_t);
526 }
527
528 if (size == PyDict_MINSIZE && numfreekeys > 0) {
529 dk = keys_free_list[--numfreekeys];
530 }
531 else {
Victor Stinner98ee9d52016-09-08 09:33:56 -0700532 dk = PyObject_MALLOC(sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -0700533 + es * size
534 + sizeof(PyDictKeyEntry) * usable);
Victor Stinner742da042016-09-07 17:40:12 -0700535 if (dk == NULL) {
536 PyErr_NoMemory();
537 return NULL;
538 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400539 }
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200540 DK_DEBUG_INCREF dk->dk_refcnt = 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400541 dk->dk_size = size;
Victor Stinner742da042016-09-07 17:40:12 -0700542 dk->dk_usable = usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400543 dk->dk_lookup = lookdict_unicode_nodummy;
Victor Stinner742da042016-09-07 17:40:12 -0700544 dk->dk_nentries = 0;
Miss Islington (bot)392520b2018-04-20 10:06:21 -0700545 memset(&dk->dk_indices[0], 0xff, es * size);
Victor Stinner742da042016-09-07 17:40:12 -0700546 memset(DK_ENTRIES(dk), 0, sizeof(PyDictKeyEntry) * usable);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400547 return dk;
548}
549
550static void
551free_keys_object(PyDictKeysObject *keys)
552{
Victor Stinner742da042016-09-07 17:40:12 -0700553 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400554 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -0700555 for (i = 0, n = keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400556 Py_XDECREF(entries[i].me_key);
557 Py_XDECREF(entries[i].me_value);
558 }
Victor Stinner742da042016-09-07 17:40:12 -0700559 if (keys->dk_size == PyDict_MINSIZE && numfreekeys < PyDict_MAXFREELIST) {
560 keys_free_list[numfreekeys++] = keys;
561 return;
562 }
Raymond Hettingerce5179f2016-01-31 08:56:21 -0800563 PyObject_FREE(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400564}
565
566#define new_values(size) PyMem_NEW(PyObject *, size)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400567#define free_values(values) PyMem_FREE(values)
568
569/* Consumes a reference to the keys object */
570static PyObject *
571new_dict(PyDictKeysObject *keys, PyObject **values)
572{
573 PyDictObject *mp;
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200574 assert(keys != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 if (numfree) {
576 mp = free_list[--numfree];
577 assert (mp != NULL);
578 assert (Py_TYPE(mp) == &PyDict_Type);
579 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400581 else {
582 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
583 if (mp == NULL) {
584 DK_DECREF(keys);
585 free_values(values);
586 return NULL;
587 }
588 }
589 mp->ma_keys = keys;
590 mp->ma_values = values;
591 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700592 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +0200593 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000595}
596
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400597/* Consumes a reference to the keys object */
598static PyObject *
599new_dict_with_shared_keys(PyDictKeysObject *keys)
600{
601 PyObject **values;
602 Py_ssize_t i, size;
603
Victor Stinner742da042016-09-07 17:40:12 -0700604 size = USABLE_FRACTION(DK_SIZE(keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400605 values = new_values(size);
606 if (values == NULL) {
607 DK_DECREF(keys);
608 return PyErr_NoMemory();
609 }
610 for (i = 0; i < size; i++) {
611 values[i] = NULL;
612 }
613 return new_dict(keys, values);
614}
615
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500616
617static PyObject *
618clone_combined_dict(PyDictObject *orig)
619{
620 assert(PyDict_CheckExact(orig));
621 assert(orig->ma_values == NULL);
622 assert(orig->ma_keys->dk_refcnt == 1);
623
624 Py_ssize_t keys_size = _PyDict_KeysSize(orig->ma_keys);
625 PyDictKeysObject *keys = PyObject_Malloc(keys_size);
626 if (keys == NULL) {
627 PyErr_NoMemory();
628 return NULL;
629 }
630
631 memcpy(keys, orig->ma_keys, keys_size);
632
633 /* After copying key/value pairs, we need to incref all
634 keys and values and they are about to be co-owned by a
635 new dict object. */
636 PyDictKeyEntry *ep0 = DK_ENTRIES(keys);
637 Py_ssize_t n = keys->dk_nentries;
638 for (Py_ssize_t i = 0; i < n; i++) {
639 PyDictKeyEntry *entry = &ep0[i];
640 PyObject *value = entry->me_value;
641 if (value != NULL) {
642 Py_INCREF(value);
643 Py_INCREF(entry->me_key);
644 }
645 }
646
647 PyDictObject *new = (PyDictObject *)new_dict(keys, NULL);
648 if (new == NULL) {
649 /* In case of an error, `new_dict()` takes care of
650 cleaning up `keys`. */
651 return NULL;
652 }
653 new->ma_used = orig->ma_used;
654 assert(_PyDict_CheckConsistency(new));
655 if (_PyObject_GC_IS_TRACKED(orig)) {
656 /* Maintain tracking. */
657 _PyObject_GC_TRACK(new);
658 }
659 return (PyObject *)new;
660}
661
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400662PyObject *
663PyDict_New(void)
664{
Victor Stinner742da042016-09-07 17:40:12 -0700665 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200666 if (keys == NULL)
667 return NULL;
668 return new_dict(keys, NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400669}
670
Victor Stinner742da042016-09-07 17:40:12 -0700671/* Search index of hash table from offset of entry table */
672static Py_ssize_t
673lookdict_index(PyDictKeysObject *k, Py_hash_t hash, Py_ssize_t index)
674{
Victor Stinner742da042016-09-07 17:40:12 -0700675 size_t mask = DK_MASK(k);
INADA Naoki073ae482017-06-23 15:22:50 +0900676 size_t perturb = (size_t)hash;
677 size_t i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700678
INADA Naoki073ae482017-06-23 15:22:50 +0900679 for (;;) {
680 Py_ssize_t ix = dk_get_index(k, i);
Victor Stinner742da042016-09-07 17:40:12 -0700681 if (ix == index) {
682 return i;
683 }
684 if (ix == DKIX_EMPTY) {
685 return DKIX_EMPTY;
686 }
INADA Naoki073ae482017-06-23 15:22:50 +0900687 perturb >>= PERTURB_SHIFT;
688 i = mask & (i*5 + perturb + 1);
Victor Stinner742da042016-09-07 17:40:12 -0700689 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700690 Py_UNREACHABLE();
Victor Stinner742da042016-09-07 17:40:12 -0700691}
692
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000693/*
694The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000695This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000696Open addressing is preferred over chaining since the link overhead for
697chaining would be substantial (100% with typical malloc overhead).
698
Tim Peterseb28ef22001-06-02 05:27:19 +0000699The initial probe index is computed as hash mod the table size. Subsequent
700probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000701
702All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000703
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000704The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000705contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000706Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000707
Victor Stinner742da042016-09-07 17:40:12 -0700708lookdict() is general-purpose, and may return DKIX_ERROR if (and only if) a
Victor Stinnera4348cc2016-09-08 12:01:25 -0700709comparison raises an exception.
Guido van Rossum89d8c602007-09-18 17:26:56 +0000710lookdict_unicode() below is specialized to string keys, comparison of which can
INADA Naoki1b8df102017-02-20 22:48:10 +0900711never raise an exception; that function can never return DKIX_ERROR when key
712is string. Otherwise, it falls back to lookdict().
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400713lookdict_unicode_nodummy is further specialized for string keys that cannot be
714the <dummy> value.
INADA Naoki778928b2017-08-03 23:45:15 +0900715For both, when the key isn't found a DKIX_EMPTY is returned.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000716*/
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100717static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400718lookdict(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900719 Py_hash_t hash, PyObject **value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000720{
INADA Naoki778928b2017-08-03 23:45:15 +0900721 size_t i, mask, perturb;
Victor Stinner742da042016-09-07 17:40:12 -0700722 PyDictKeysObject *dk;
INADA Naoki778928b2017-08-03 23:45:15 +0900723 PyDictKeyEntry *ep0;
Tim Peterseb28ef22001-06-02 05:27:19 +0000724
Antoine Pitrou9a234902012-05-13 20:48:01 +0200725top:
Victor Stinner742da042016-09-07 17:40:12 -0700726 dk = mp->ma_keys;
Victor Stinner742da042016-09-07 17:40:12 -0700727 ep0 = DK_ENTRIES(dk);
INADA Naoki778928b2017-08-03 23:45:15 +0900728 mask = DK_MASK(dk);
729 perturb = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700731
INADA Naoki778928b2017-08-03 23:45:15 +0900732 for (;;) {
733 Py_ssize_t ix = dk_get_index(dk, i);
Victor Stinner742da042016-09-07 17:40:12 -0700734 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700735 *value_addr = NULL;
736 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400737 }
INADA Naoki778928b2017-08-03 23:45:15 +0900738 if (ix >= 0) {
739 PyDictKeyEntry *ep = &ep0[ix];
740 assert(ep->me_key != NULL);
741 if (ep->me_key == key) {
742 *value_addr = ep->me_value;
743 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700744 }
INADA Naoki778928b2017-08-03 23:45:15 +0900745 if (ep->me_hash == hash) {
746 PyObject *startkey = ep->me_key;
747 Py_INCREF(startkey);
748 int cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
749 Py_DECREF(startkey);
750 if (cmp < 0) {
751 *value_addr = NULL;
752 return DKIX_ERROR;
753 }
754 if (dk == mp->ma_keys && ep->me_key == startkey) {
755 if (cmp > 0) {
756 *value_addr = ep->me_value;
757 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700758 }
INADA Naoki778928b2017-08-03 23:45:15 +0900759 }
760 else {
761 /* The dict was mutated, restart */
762 goto top;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400763 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 }
INADA Naoki778928b2017-08-03 23:45:15 +0900766 perturb >>= PERTURB_SHIFT;
767 i = (i*5 + perturb + 1) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700769 Py_UNREACHABLE();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000770}
771
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400772/* Specialized version for string-only keys */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100773static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400774lookdict_unicode(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900775 Py_hash_t hash, PyObject **value_addr)
Fred Drake1bff34a2000-08-31 19:31:38 +0000776{
Victor Stinner742da042016-09-07 17:40:12 -0700777 assert(mp->ma_values == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 /* Make sure this function doesn't have to handle non-unicode keys,
779 including subclasses of str; e.g., one reason to subclass
780 unicodes is to override __eq__, and for speed we don't cater to
781 that here. */
782 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400783 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900784 return lookdict(mp, key, hash, value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 }
Tim Peters15d49292001-05-27 07:39:22 +0000786
INADA Naoki778928b2017-08-03 23:45:15 +0900787 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
788 size_t mask = DK_MASK(mp->ma_keys);
789 size_t perturb = (size_t)hash;
790 size_t i = (size_t)hash & mask;
791
792 for (;;) {
793 Py_ssize_t ix = dk_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700794 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700795 *value_addr = NULL;
796 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400797 }
INADA Naoki778928b2017-08-03 23:45:15 +0900798 if (ix >= 0) {
799 PyDictKeyEntry *ep = &ep0[ix];
800 assert(ep->me_key != NULL);
801 assert(PyUnicode_CheckExact(ep->me_key));
802 if (ep->me_key == key ||
803 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
804 *value_addr = ep->me_value;
805 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700806 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400807 }
INADA Naoki778928b2017-08-03 23:45:15 +0900808 perturb >>= PERTURB_SHIFT;
809 i = mask & (i*5 + perturb + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700811 Py_UNREACHABLE();
Fred Drake1bff34a2000-08-31 19:31:38 +0000812}
813
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400814/* Faster version of lookdict_unicode when it is known that no <dummy> keys
815 * will be present. */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100816static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400817lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900818 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400819{
Victor Stinner742da042016-09-07 17:40:12 -0700820 assert(mp->ma_values == NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400821 /* Make sure this function doesn't have to handle non-unicode keys,
822 including subclasses of str; e.g., one reason to subclass
823 unicodes is to override __eq__, and for speed we don't cater to
824 that here. */
825 if (!PyUnicode_CheckExact(key)) {
826 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900827 return lookdict(mp, key, hash, value_addr);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400828 }
INADA Naoki778928b2017-08-03 23:45:15 +0900829
830 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
831 size_t mask = DK_MASK(mp->ma_keys);
832 size_t perturb = (size_t)hash;
833 size_t i = (size_t)hash & mask;
834
835 for (;;) {
836 Py_ssize_t ix = dk_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700837 assert (ix != DKIX_DUMMY);
838 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700839 *value_addr = NULL;
840 return DKIX_EMPTY;
841 }
INADA Naoki778928b2017-08-03 23:45:15 +0900842 PyDictKeyEntry *ep = &ep0[ix];
843 assert(ep->me_key != NULL);
844 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700845 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400846 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900847 *value_addr = ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700848 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400849 }
INADA Naoki778928b2017-08-03 23:45:15 +0900850 perturb >>= PERTURB_SHIFT;
851 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400852 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700853 Py_UNREACHABLE();
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400854}
855
856/* Version of lookdict for split tables.
857 * All split tables and only split tables use this lookup function.
858 * Split tables only contain unicode keys and no dummy keys,
859 * so algorithm is the same as lookdict_unicode_nodummy.
860 */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100861static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400862lookdict_split(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900863 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400864{
Victor Stinner742da042016-09-07 17:40:12 -0700865 /* mp must split table */
866 assert(mp->ma_values != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400867 if (!PyUnicode_CheckExact(key)) {
INADA Naoki778928b2017-08-03 23:45:15 +0900868 Py_ssize_t ix = lookdict(mp, key, hash, value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700869 if (ix >= 0) {
INADA Naokiba609772016-12-07 20:41:42 +0900870 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700871 }
872 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400873 }
Victor Stinner742da042016-09-07 17:40:12 -0700874
INADA Naoki778928b2017-08-03 23:45:15 +0900875 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
876 size_t mask = DK_MASK(mp->ma_keys);
877 size_t perturb = (size_t)hash;
878 size_t i = (size_t)hash & mask;
879
880 for (;;) {
881 Py_ssize_t ix = dk_get_index(mp->ma_keys, i);
882 assert (ix != DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -0700883 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700884 *value_addr = NULL;
885 return DKIX_EMPTY;
886 }
INADA Naoki778928b2017-08-03 23:45:15 +0900887 PyDictKeyEntry *ep = &ep0[ix];
888 assert(ep->me_key != NULL);
889 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700890 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400891 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900892 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700893 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400894 }
INADA Naoki778928b2017-08-03 23:45:15 +0900895 perturb >>= PERTURB_SHIFT;
896 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400897 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700898 Py_UNREACHABLE();
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400899}
900
Benjamin Petersonfb886362010-04-24 18:21:17 +0000901int
902_PyDict_HasOnlyStringKeys(PyObject *dict)
903{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 Py_ssize_t pos = 0;
905 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000906 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400908 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 return 1;
910 while (PyDict_Next(dict, &pos, &key, &value))
911 if (!PyUnicode_Check(key))
912 return 0;
913 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000914}
915
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000916#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 do { \
918 if (!_PyObject_GC_IS_TRACKED(mp)) { \
919 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
920 _PyObject_GC_MAY_BE_TRACKED(value)) { \
921 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 } \
923 } \
924 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000925
926void
927_PyDict_MaybeUntrack(PyObject *op)
928{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 PyDictObject *mp;
930 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -0700931 Py_ssize_t i, numentries;
932 PyDictKeyEntry *ep0;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
935 return;
936
937 mp = (PyDictObject *) op;
Victor Stinner742da042016-09-07 17:40:12 -0700938 ep0 = DK_ENTRIES(mp->ma_keys);
939 numentries = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400940 if (_PyDict_HasSplitTable(mp)) {
Victor Stinner742da042016-09-07 17:40:12 -0700941 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400942 if ((value = mp->ma_values[i]) == NULL)
943 continue;
944 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
Victor Stinner742da042016-09-07 17:40:12 -0700945 assert(!_PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400946 return;
947 }
948 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400950 else {
Victor Stinner742da042016-09-07 17:40:12 -0700951 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400952 if ((value = ep0[i].me_value) == NULL)
953 continue;
954 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
955 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
956 return;
957 }
958 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000960}
961
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400962/* Internal function to find slot for an item from its hash
Victor Stinner3c336c52016-09-12 14:17:40 +0200963 when it is known that the key is not present in the dict.
964
965 The dict must be combined. */
INADA Naokiba609772016-12-07 20:41:42 +0900966static Py_ssize_t
INADA Naoki778928b2017-08-03 23:45:15 +0900967find_empty_slot(PyDictKeysObject *keys, Py_hash_t hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000968{
INADA Naoki778928b2017-08-03 23:45:15 +0900969 assert(keys != NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000970
INADA Naoki778928b2017-08-03 23:45:15 +0900971 const size_t mask = DK_MASK(keys);
972 size_t i = hash & mask;
973 Py_ssize_t ix = dk_get_index(keys, i);
974 for (size_t perturb = hash; ix >= 0;) {
INADA Naoki267941c2016-10-06 15:19:07 +0900975 perturb >>= PERTURB_SHIFT;
INADA Naoki778928b2017-08-03 23:45:15 +0900976 i = (i*5 + perturb + 1) & mask;
977 ix = dk_get_index(keys, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 }
INADA Naoki778928b2017-08-03 23:45:15 +0900979 return i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000980}
981
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400982static int
983insertion_resize(PyDictObject *mp)
984{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700985 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400986}
Antoine Pitroue965d972012-02-27 00:45:12 +0100987
988/*
989Internal routine to insert a new item into the table.
990Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +0100991Returns -1 if an error occurred, or 0 on success.
992*/
993static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400994insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +0100995{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400996 PyObject *old_value;
INADA Naokiba609772016-12-07 20:41:42 +0900997 PyDictKeyEntry *ep;
Antoine Pitroue965d972012-02-27 00:45:12 +0100998
Serhiy Storchaka753bca32017-05-20 12:30:02 +0300999 Py_INCREF(key);
1000 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001001 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
1002 if (insertion_resize(mp) < 0)
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001003 goto Fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001004 }
1005
INADA Naoki778928b2017-08-03 23:45:15 +09001006 Py_ssize_t ix = mp->ma_keys->dk_lookup(mp, key, hash, &old_value);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001007 if (ix == DKIX_ERROR)
1008 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001009
Antoine Pitroud6967322014-10-18 00:35:00 +02001010 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001011 MAINTAIN_TRACKING(mp, key, value);
Victor Stinner742da042016-09-07 17:40:12 -07001012
1013 /* When insertion order is different from shared key, we can't share
1014 * the key anymore. Convert this instance to combine table.
1015 */
1016 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09001017 ((ix >= 0 && old_value == NULL && mp->ma_used != ix) ||
Victor Stinner742da042016-09-07 17:40:12 -07001018 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001019 if (insertion_resize(mp) < 0)
1020 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001021 ix = DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001022 }
Victor Stinner742da042016-09-07 17:40:12 -07001023
1024 if (ix == DKIX_EMPTY) {
1025 /* Insert into new slot. */
INADA Naokiba609772016-12-07 20:41:42 +09001026 assert(old_value == NULL);
Victor Stinner742da042016-09-07 17:40:12 -07001027 if (mp->ma_keys->dk_usable <= 0) {
1028 /* Need to resize. */
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001029 if (insertion_resize(mp) < 0)
1030 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001031 }
INADA Naoki778928b2017-08-03 23:45:15 +09001032 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naokiba609772016-12-07 20:41:42 +09001033 ep = &DK_ENTRIES(mp->ma_keys)[mp->ma_keys->dk_nentries];
Victor Stinner742da042016-09-07 17:40:12 -07001034 dk_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Victor Stinner742da042016-09-07 17:40:12 -07001035 ep->me_key = key;
1036 ep->me_hash = hash;
1037 if (mp->ma_values) {
1038 assert (mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
1039 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001040 }
1041 else {
Victor Stinner742da042016-09-07 17:40:12 -07001042 ep->me_value = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001043 }
1044 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001045 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07001046 mp->ma_keys->dk_usable--;
1047 mp->ma_keys->dk_nentries++;
1048 assert(mp->ma_keys->dk_usable >= 0);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001049 assert(_PyDict_CheckConsistency(mp));
Victor Stinner742da042016-09-07 17:40:12 -07001050 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001051 }
Victor Stinner742da042016-09-07 17:40:12 -07001052
INADA Naokiba609772016-12-07 20:41:42 +09001053 if (_PyDict_HasSplitTable(mp)) {
1054 mp->ma_values[ix] = value;
1055 if (old_value == NULL) {
1056 /* pending state */
1057 assert(ix == mp->ma_used);
1058 mp->ma_used++;
1059 }
1060 }
1061 else {
1062 assert(old_value != NULL);
1063 DK_ENTRIES(mp->ma_keys)[ix].me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07001064 }
1065
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001066 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokiba609772016-12-07 20:41:42 +09001067 Py_XDECREF(old_value); /* which **CAN** re-enter (see issue #22653) */
Victor Stinner611b0fa2016-09-14 15:02:01 +02001068 assert(_PyDict_CheckConsistency(mp));
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001069 Py_DECREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001070 return 0;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001071
1072Fail:
1073 Py_DECREF(value);
1074 Py_DECREF(key);
1075 return -1;
Antoine Pitroue965d972012-02-27 00:45:12 +01001076}
1077
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001078/*
luzpaza5293b42017-11-05 07:37:50 -06001079Internal routine used by dictresize() to build a hashtable of entries.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001080*/
1081static void
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001082build_indices(PyDictKeysObject *keys, PyDictKeyEntry *ep, Py_ssize_t n)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001083{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001084 size_t mask = (size_t)DK_SIZE(keys) - 1;
1085 for (Py_ssize_t ix = 0; ix != n; ix++, ep++) {
1086 Py_hash_t hash = ep->me_hash;
1087 size_t i = hash & mask;
1088 for (size_t perturb = hash; dk_get_index(keys, i) != DKIX_EMPTY;) {
1089 perturb >>= PERTURB_SHIFT;
INADA Naoki870c2862017-06-24 09:03:19 +09001090 i = mask & (i*5 + perturb + 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001091 }
1092 dk_set_index(keys, i, ix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001094}
1095
1096/*
1097Restructure the table by allocating a new table and reinserting all
1098items again. When entries have been deleted, the new table may
1099actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001100If a table is split (its keys and hashes are shared, its values are not),
1101then the values are temporarily copied into the table, it is resized as
1102a combined table, then the me_value slots in the old table are NULLed out.
1103After resizing a table is always combined,
1104but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001105*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001106static int
Victor Stinner3d3f2642016-12-15 17:21:23 +01001107dictresize(PyDictObject *mp, Py_ssize_t minsize)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001108{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001109 Py_ssize_t newsize, numentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001110 PyDictKeysObject *oldkeys;
1111 PyObject **oldvalues;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001112 PyDictKeyEntry *oldentries, *newentries;
Tim Peters91a364d2001-05-19 07:04:38 +00001113
Victor Stinner742da042016-09-07 17:40:12 -07001114 /* Find the smallest table size > minused. */
1115 for (newsize = PyDict_MINSIZE;
Victor Stinner3d3f2642016-12-15 17:21:23 +01001116 newsize < minsize && newsize > 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 newsize <<= 1)
1118 ;
1119 if (newsize <= 0) {
1120 PyErr_NoMemory();
1121 return -1;
1122 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001123
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001124 oldkeys = mp->ma_keys;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001125
1126 /* NOTE: Current odict checks mp->ma_keys to detect resize happen.
1127 * So we can't reuse oldkeys even if oldkeys->dk_size == newsize.
1128 * TODO: Try reusing oldkeys when reimplement odict.
1129 */
1130
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001131 /* Allocate a new table. */
1132 mp->ma_keys = new_keys_object(newsize);
1133 if (mp->ma_keys == NULL) {
1134 mp->ma_keys = oldkeys;
1135 return -1;
1136 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01001137 // New table must be large enough.
1138 assert(mp->ma_keys->dk_usable >= mp->ma_used);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001139 if (oldkeys->dk_lookup == lookdict)
1140 mp->ma_keys->dk_lookup = lookdict;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001141
1142 numentries = mp->ma_used;
1143 oldentries = DK_ENTRIES(oldkeys);
1144 newentries = DK_ENTRIES(mp->ma_keys);
1145 oldvalues = mp->ma_values;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001146 if (oldvalues != NULL) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001147 /* Convert split table into new combined table.
1148 * We must incref keys; we can transfer values.
1149 * Note that values of split table is always dense.
1150 */
1151 for (Py_ssize_t i = 0; i < numentries; i++) {
1152 assert(oldvalues[i] != NULL);
1153 PyDictKeyEntry *ep = &oldentries[i];
1154 PyObject *key = ep->me_key;
1155 Py_INCREF(key);
1156 newentries[i].me_key = key;
1157 newentries[i].me_hash = ep->me_hash;
1158 newentries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001160
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001161 DK_DECREF(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001162 mp->ma_values = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001163 if (oldvalues != empty_values) {
1164 free_values(oldvalues);
1165 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001166 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001167 else { // combined table.
1168 if (oldkeys->dk_nentries == numentries) {
1169 memcpy(newentries, oldentries, numentries * sizeof(PyDictKeyEntry));
1170 }
1171 else {
1172 PyDictKeyEntry *ep = oldentries;
1173 for (Py_ssize_t i = 0; i < numentries; i++) {
1174 while (ep->me_value == NULL)
1175 ep++;
1176 newentries[i] = *ep++;
1177 }
1178 }
1179
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001180 assert(oldkeys->dk_lookup != lookdict_split);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001181 assert(oldkeys->dk_refcnt == 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001182 if (oldkeys->dk_size == PyDict_MINSIZE &&
1183 numfreekeys < PyDict_MAXFREELIST) {
1184 DK_DEBUG_DECREF keys_free_list[numfreekeys++] = oldkeys;
1185 }
1186 else {
1187 DK_DEBUG_DECREF PyObject_FREE(oldkeys);
1188 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001189 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001190
1191 build_indices(mp->ma_keys, newentries, numentries);
1192 mp->ma_keys->dk_usable -= numentries;
1193 mp->ma_keys->dk_nentries = numentries;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001195}
1196
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001197/* Returns NULL if unable to split table.
1198 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001199static PyDictKeysObject *
1200make_keys_shared(PyObject *op)
1201{
1202 Py_ssize_t i;
1203 Py_ssize_t size;
1204 PyDictObject *mp = (PyDictObject *)op;
1205
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001206 if (!PyDict_CheckExact(op))
1207 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001208 if (!_PyDict_HasSplitTable(mp)) {
1209 PyDictKeyEntry *ep0;
1210 PyObject **values;
1211 assert(mp->ma_keys->dk_refcnt == 1);
1212 if (mp->ma_keys->dk_lookup == lookdict) {
1213 return NULL;
1214 }
1215 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1216 /* Remove dummy keys */
1217 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1218 return NULL;
1219 }
1220 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1221 /* Copy values into a new array */
Victor Stinner742da042016-09-07 17:40:12 -07001222 ep0 = DK_ENTRIES(mp->ma_keys);
1223 size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001224 values = new_values(size);
1225 if (values == NULL) {
1226 PyErr_SetString(PyExc_MemoryError,
1227 "Not enough memory to allocate new values array");
1228 return NULL;
1229 }
1230 for (i = 0; i < size; i++) {
1231 values[i] = ep0[i].me_value;
1232 ep0[i].me_value = NULL;
1233 }
1234 mp->ma_keys->dk_lookup = lookdict_split;
1235 mp->ma_values = values;
1236 }
1237 DK_INCREF(mp->ma_keys);
1238 return mp->ma_keys;
1239}
Christian Heimes99170a52007-12-19 02:07:34 +00001240
1241PyObject *
1242_PyDict_NewPresized(Py_ssize_t minused)
1243{
INADA Naoki92c50ee2016-11-22 00:57:02 +09001244 const Py_ssize_t max_presize = 128 * 1024;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001245 Py_ssize_t newsize;
1246 PyDictKeysObject *new_keys;
INADA Naoki92c50ee2016-11-22 00:57:02 +09001247
1248 /* There are no strict guarantee that returned dict can contain minused
1249 * items without resize. So we create medium size dict instead of very
1250 * large dict or MemoryError.
1251 */
1252 if (minused > USABLE_FRACTION(max_presize)) {
1253 newsize = max_presize;
1254 }
1255 else {
1256 Py_ssize_t minsize = ESTIMATE_SIZE(minused);
1257 newsize = PyDict_MINSIZE;
1258 while (newsize < minsize) {
1259 newsize <<= 1;
1260 }
1261 }
1262 assert(IS_POWER_OF_2(newsize));
1263
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001264 new_keys = new_keys_object(newsize);
1265 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001267 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001268}
1269
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001270/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1271 * that may occur (originally dicts supported only string keys, and exceptions
1272 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001273 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001274 * (suppressed) occurred while computing the key's hash, or that some error
1275 * (suppressed) occurred when comparing keys in the dict's internal probe
1276 * sequence. A nasty example of the latter is when a Python-coded comparison
1277 * function hits a stack-depth error, which can cause this to return NULL
1278 * even if the key is present.
1279 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001280PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001281PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001282{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001283 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07001284 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 PyDictObject *mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 PyThreadState *tstate;
INADA Naokiba609772016-12-07 20:41:42 +09001287 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 if (!PyDict_Check(op))
1290 return NULL;
1291 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001292 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 {
1294 hash = PyObject_Hash(key);
1295 if (hash == -1) {
1296 PyErr_Clear();
1297 return NULL;
1298 }
1299 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 /* We can arrive here with a NULL tstate during initialization: try
1302 running "python -Wi" for an example related to string interning.
1303 Let's just hope that no exception occurs then... This must be
1304 _PyThreadState_Current and not PyThreadState_GET() because in debug
1305 mode, the latter complains if tstate is NULL. */
Victor Stinner0cae6092016-11-11 01:43:56 +01001306 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 if (tstate != NULL && tstate->curexc_type != NULL) {
1308 /* preserve the existing exception */
1309 PyObject *err_type, *err_value, *err_tb;
1310 PyErr_Fetch(&err_type, &err_value, &err_tb);
INADA Naoki778928b2017-08-03 23:45:15 +09001311 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 /* ignore errors */
1313 PyErr_Restore(err_type, err_value, err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001314 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 return NULL;
1316 }
1317 else {
INADA Naoki778928b2017-08-03 23:45:15 +09001318 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001319 if (ix < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 PyErr_Clear();
1321 return NULL;
1322 }
1323 }
INADA Naokiba609772016-12-07 20:41:42 +09001324 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001325}
1326
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001327/* Same as PyDict_GetItemWithError() but with hash supplied by caller.
1328 This returns NULL *with* an exception set if an exception occurred.
1329 It returns NULL *without* an exception set if the key wasn't present.
1330*/
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001331PyObject *
1332_PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1333{
Victor Stinner742da042016-09-07 17:40:12 -07001334 Py_ssize_t ix;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001335 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001336 PyObject *value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001337
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001338 if (!PyDict_Check(op)) {
1339 PyErr_BadInternalCall();
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001340 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001341 }
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001342
INADA Naoki778928b2017-08-03 23:45:15 +09001343 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001344 if (ix < 0) {
1345 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001346 }
INADA Naokiba609772016-12-07 20:41:42 +09001347 return value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001348}
1349
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001350/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1351 This returns NULL *with* an exception set if an exception occurred.
1352 It returns NULL *without* an exception set if the key wasn't present.
1353*/
1354PyObject *
1355PyDict_GetItemWithError(PyObject *op, PyObject *key)
1356{
Victor Stinner742da042016-09-07 17:40:12 -07001357 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001358 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001359 PyDictObject*mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001360 PyObject *value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 if (!PyDict_Check(op)) {
1363 PyErr_BadInternalCall();
1364 return NULL;
1365 }
1366 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001367 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 {
1369 hash = PyObject_Hash(key);
1370 if (hash == -1) {
1371 return NULL;
1372 }
1373 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001374
INADA Naoki778928b2017-08-03 23:45:15 +09001375 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001376 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001378 return value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001379}
1380
Brett Cannonfd074152012-04-14 14:10:13 -04001381PyObject *
1382_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1383{
1384 PyObject *kv;
1385 kv = _PyUnicode_FromId(key); /* borrowed */
1386 if (kv == NULL)
1387 return NULL;
1388 return PyDict_GetItemWithError(dp, kv);
1389}
1390
Victor Stinnerb4efc962015-11-20 09:24:02 +01001391/* Fast version of global value lookup (LOAD_GLOBAL).
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001392 * Lookup in globals, then builtins.
Victor Stinnerb4efc962015-11-20 09:24:02 +01001393 *
1394 * Raise an exception and return NULL if an error occurred (ex: computing the
1395 * key hash failed, key comparison failed, ...). Return NULL if the key doesn't
1396 * exist. Return the value if the key exists.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001397 */
1398PyObject *
1399_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001400{
Victor Stinner742da042016-09-07 17:40:12 -07001401 Py_ssize_t ix;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001402 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09001403 PyObject *value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001404
1405 if (!PyUnicode_CheckExact(key) ||
1406 (hash = ((PyASCIIObject *) key)->hash) == -1)
1407 {
1408 hash = PyObject_Hash(key);
1409 if (hash == -1)
1410 return NULL;
Antoine Pitroue965d972012-02-27 00:45:12 +01001411 }
Victor Stinnerb4efc962015-11-20 09:24:02 +01001412
1413 /* namespace 1: globals */
INADA Naoki778928b2017-08-03 23:45:15 +09001414 ix = globals->ma_keys->dk_lookup(globals, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001415 if (ix == DKIX_ERROR)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001416 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001417 if (ix != DKIX_EMPTY && value != NULL)
1418 return value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001419
1420 /* namespace 2: builtins */
INADA Naoki778928b2017-08-03 23:45:15 +09001421 ix = builtins->ma_keys->dk_lookup(builtins, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001422 if (ix < 0)
Victor Stinnerb4efc962015-11-20 09:24:02 +01001423 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001424 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001425}
1426
Antoine Pitroue965d972012-02-27 00:45:12 +01001427/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1428 * dictionary if it's merely replacing the value for an existing key.
1429 * This means that it's safe to loop over a dictionary with PyDict_Next()
1430 * and occasionally replace a value -- but you can't insert new keys or
1431 * remove them.
1432 */
1433int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001434PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001435{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001436 PyDictObject *mp;
1437 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001438 if (!PyDict_Check(op)) {
1439 PyErr_BadInternalCall();
1440 return -1;
1441 }
1442 assert(key);
1443 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001444 mp = (PyDictObject *)op;
1445 if (!PyUnicode_CheckExact(key) ||
1446 (hash = ((PyASCIIObject *) key)->hash) == -1)
1447 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001448 hash = PyObject_Hash(key);
1449 if (hash == -1)
1450 return -1;
1451 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001452
1453 /* insertdict() handles any resizing that might be necessary */
1454 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001455}
1456
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001457int
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001458_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1459 Py_hash_t hash)
1460{
1461 PyDictObject *mp;
1462
1463 if (!PyDict_Check(op)) {
1464 PyErr_BadInternalCall();
1465 return -1;
1466 }
1467 assert(key);
1468 assert(value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001469 assert(hash != -1);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001470 mp = (PyDictObject *)op;
1471
1472 /* insertdict() handles any resizing that might be necessary */
1473 return insertdict(mp, key, hash, value);
1474}
1475
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001476static int
INADA Naoki778928b2017-08-03 23:45:15 +09001477delitem_common(PyDictObject *mp, Py_hash_t hash, Py_ssize_t ix,
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001478 PyObject *old_value)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001479{
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001480 PyObject *old_key;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001481 PyDictKeyEntry *ep;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001482
INADA Naoki778928b2017-08-03 23:45:15 +09001483 Py_ssize_t hashpos = lookdict_index(mp->ma_keys, hash, ix);
1484 assert(hashpos >= 0);
1485
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001486 mp->ma_used--;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001487 mp->ma_version_tag = DICT_NEXT_VERSION();
1488 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1489 dk_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
1490 ENSURE_ALLOWS_DELETIONS(mp);
1491 old_key = ep->me_key;
1492 ep->me_key = NULL;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001493 ep->me_value = NULL;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001494 Py_DECREF(old_key);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001495 Py_DECREF(old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001496
1497 assert(_PyDict_CheckConsistency(mp));
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001498 return 0;
1499}
1500
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001501int
Tim Peters1f5871e2000-07-04 17:44:48 +00001502PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001503{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001504 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 assert(key);
1506 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001507 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001508 hash = PyObject_Hash(key);
1509 if (hash == -1)
1510 return -1;
1511 }
Victor Stinner742da042016-09-07 17:40:12 -07001512
1513 return _PyDict_DelItem_KnownHash(op, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001514}
1515
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001516int
1517_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1518{
INADA Naoki778928b2017-08-03 23:45:15 +09001519 Py_ssize_t ix;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001520 PyDictObject *mp;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001521 PyObject *old_value;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001522
1523 if (!PyDict_Check(op)) {
1524 PyErr_BadInternalCall();
1525 return -1;
1526 }
1527 assert(key);
1528 assert(hash != -1);
1529 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001530 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001531 if (ix == DKIX_ERROR)
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001532 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09001533 if (ix == DKIX_EMPTY || old_value == NULL) {
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001534 _PyErr_SetKeyError(key);
1535 return -1;
1536 }
Victor Stinner78601a32016-09-09 19:28:36 -07001537
1538 // Split table doesn't allow deletion. Combine it.
1539 if (_PyDict_HasSplitTable(mp)) {
1540 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1541 return -1;
1542 }
INADA Naoki778928b2017-08-03 23:45:15 +09001543 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001544 assert(ix >= 0);
1545 }
1546
INADA Naoki778928b2017-08-03 23:45:15 +09001547 return delitem_common(mp, hash, ix, old_value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001548}
1549
Antoine Pitroud741ed42016-12-27 14:23:43 +01001550/* This function promises that the predicate -> deletion sequence is atomic
1551 * (i.e. protected by the GIL), assuming the predicate itself doesn't
1552 * release the GIL.
1553 */
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001554int
1555_PyDict_DelItemIf(PyObject *op, PyObject *key,
1556 int (*predicate)(PyObject *value))
1557{
Antoine Pitroud741ed42016-12-27 14:23:43 +01001558 Py_ssize_t hashpos, ix;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001559 PyDictObject *mp;
1560 Py_hash_t hash;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001561 PyObject *old_value;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001562 int res;
1563
1564 if (!PyDict_Check(op)) {
1565 PyErr_BadInternalCall();
1566 return -1;
1567 }
1568 assert(key);
1569 hash = PyObject_Hash(key);
1570 if (hash == -1)
1571 return -1;
1572 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001573 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001574 if (ix == DKIX_ERROR)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001575 return -1;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001576 if (ix == DKIX_EMPTY || old_value == NULL) {
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001577 _PyErr_SetKeyError(key);
1578 return -1;
1579 }
Antoine Pitroud741ed42016-12-27 14:23:43 +01001580
1581 // Split table doesn't allow deletion. Combine it.
1582 if (_PyDict_HasSplitTable(mp)) {
1583 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1584 return -1;
1585 }
INADA Naoki778928b2017-08-03 23:45:15 +09001586 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001587 assert(ix >= 0);
1588 }
1589
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001590 res = predicate(old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001591 if (res == -1)
1592 return -1;
INADA Naoki778928b2017-08-03 23:45:15 +09001593
1594 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1595 assert(hashpos >= 0);
1596
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001597 if (res > 0)
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001598 return delitem_common(mp, hashpos, ix, old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001599 else
1600 return 0;
1601}
1602
1603
Guido van Rossum25831651993-05-19 14:50:45 +00001604void
Tim Peters1f5871e2000-07-04 17:44:48 +00001605PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001606{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001608 PyDictKeysObject *oldkeys;
1609 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 if (!PyDict_Check(op))
1613 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001614 mp = ((PyDictObject *)op);
1615 oldkeys = mp->ma_keys;
1616 oldvalues = mp->ma_values;
1617 if (oldvalues == empty_values)
1618 return;
1619 /* Empty the dict... */
1620 DK_INCREF(Py_EMPTY_KEYS);
1621 mp->ma_keys = Py_EMPTY_KEYS;
1622 mp->ma_values = empty_values;
1623 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001624 mp->ma_version_tag = DICT_NEXT_VERSION();
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001625 /* ...then clear the keys and values */
1626 if (oldvalues != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001627 n = oldkeys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001628 for (i = 0; i < n; i++)
1629 Py_CLEAR(oldvalues[i]);
1630 free_values(oldvalues);
1631 DK_DECREF(oldkeys);
1632 }
1633 else {
1634 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001635 DK_DECREF(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001636 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02001637 assert(_PyDict_CheckConsistency(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001638}
1639
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001640/* Internal version of PyDict_Next that returns a hash value in addition
1641 * to the key and value.
1642 * Return 1 on success, return 0 when the reached the end of the dictionary
1643 * (or if op is not a dictionary)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001644 */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001645int
1646_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1647 PyObject **pvalue, Py_hash_t *phash)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001648{
INADA Naokica2d8be2016-11-04 16:59:10 +09001649 Py_ssize_t i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001650 PyDictObject *mp;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001651 PyDictKeyEntry *entry_ptr;
1652 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001653
1654 if (!PyDict_Check(op))
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001655 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 mp = (PyDictObject *)op;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001657 i = *ppos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001658 if (mp->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09001659 if (i < 0 || i >= mp->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001660 return 0;
INADA Naokica2d8be2016-11-04 16:59:10 +09001661 /* values of split table is always dense */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001662 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
INADA Naokica2d8be2016-11-04 16:59:10 +09001663 value = mp->ma_values[i];
1664 assert(value != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001666 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09001667 Py_ssize_t n = mp->ma_keys->dk_nentries;
1668 if (i < 0 || i >= n)
1669 return 0;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001670 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
1671 while (i < n && entry_ptr->me_value == NULL) {
1672 entry_ptr++;
1673 i++;
Victor Stinner742da042016-09-07 17:40:12 -07001674 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001675 if (i >= n)
1676 return 0;
1677 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001678 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001679 *ppos = i+1;
1680 if (pkey)
1681 *pkey = entry_ptr->me_key;
1682 if (phash)
1683 *phash = entry_ptr->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001684 if (pvalue)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001685 *pvalue = value;
1686 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001687}
1688
Tim Peters080c88b2003-02-15 03:01:11 +00001689/*
1690 * Iterate over a dict. Use like so:
1691 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001692 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001693 * PyObject *key, *value;
1694 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001695 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001696 * Refer to borrowed references in key and value.
Tim Peters080c88b2003-02-15 03:01:11 +00001697 * }
1698 *
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001699 * Return 1 on success, return 0 when the reached the end of the dictionary
1700 * (or if op is not a dictionary)
1701 *
Tim Peters080c88b2003-02-15 03:01:11 +00001702 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001703 * mutates the dict. One exception: it is safe if the loop merely changes
1704 * the values associated with the keys (but doesn't insert new keys or
1705 * delete keys), via PyDict_SetItem().
1706 */
Guido van Rossum25831651993-05-19 14:50:45 +00001707int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001708PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001709{
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001710 return _PyDict_Next(op, ppos, pkey, pvalue, NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001711}
1712
Eric Snow96c6af92015-05-29 22:21:39 -06001713/* Internal version of dict.pop(). */
1714PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001715_PyDict_Pop_KnownHash(PyObject *dict, PyObject *key, Py_hash_t hash, PyObject *deflt)
Eric Snow96c6af92015-05-29 22:21:39 -06001716{
Victor Stinner742da042016-09-07 17:40:12 -07001717 Py_ssize_t ix, hashpos;
Eric Snow96c6af92015-05-29 22:21:39 -06001718 PyObject *old_value, *old_key;
1719 PyDictKeyEntry *ep;
Yury Selivanov684ef2c2016-10-28 19:01:21 -04001720 PyDictObject *mp;
1721
1722 assert(PyDict_Check(dict));
1723 mp = (PyDictObject *)dict;
Eric Snow96c6af92015-05-29 22:21:39 -06001724
1725 if (mp->ma_used == 0) {
1726 if (deflt) {
1727 Py_INCREF(deflt);
1728 return deflt;
1729 }
1730 _PyErr_SetKeyError(key);
1731 return NULL;
1732 }
INADA Naoki778928b2017-08-03 23:45:15 +09001733 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001734 if (ix == DKIX_ERROR)
Eric Snow96c6af92015-05-29 22:21:39 -06001735 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001736 if (ix == DKIX_EMPTY || old_value == NULL) {
Eric Snow96c6af92015-05-29 22:21:39 -06001737 if (deflt) {
1738 Py_INCREF(deflt);
1739 return deflt;
1740 }
1741 _PyErr_SetKeyError(key);
1742 return NULL;
1743 }
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001744
Victor Stinner78601a32016-09-09 19:28:36 -07001745 // Split table doesn't allow deletion. Combine it.
1746 if (_PyDict_HasSplitTable(mp)) {
1747 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1748 return NULL;
1749 }
INADA Naoki778928b2017-08-03 23:45:15 +09001750 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001751 assert(ix >= 0);
1752 }
1753
INADA Naoki778928b2017-08-03 23:45:15 +09001754 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1755 assert(hashpos >= 0);
Victor Stinner78601a32016-09-09 19:28:36 -07001756 assert(old_value != NULL);
Eric Snow96c6af92015-05-29 22:21:39 -06001757 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001758 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner78601a32016-09-09 19:28:36 -07001759 dk_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
1760 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1761 ENSURE_ALLOWS_DELETIONS(mp);
1762 old_key = ep->me_key;
1763 ep->me_key = NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001764 ep->me_value = NULL;
Victor Stinner78601a32016-09-09 19:28:36 -07001765 Py_DECREF(old_key);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001766
1767 assert(_PyDict_CheckConsistency(mp));
Eric Snow96c6af92015-05-29 22:21:39 -06001768 return old_value;
1769}
1770
Serhiy Storchaka67796522017-01-12 18:34:33 +02001771PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001772_PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt)
Serhiy Storchaka67796522017-01-12 18:34:33 +02001773{
1774 Py_hash_t hash;
1775
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001776 if (((PyDictObject *)dict)->ma_used == 0) {
Serhiy Storchaka67796522017-01-12 18:34:33 +02001777 if (deflt) {
1778 Py_INCREF(deflt);
1779 return deflt;
1780 }
1781 _PyErr_SetKeyError(key);
1782 return NULL;
1783 }
1784 if (!PyUnicode_CheckExact(key) ||
1785 (hash = ((PyASCIIObject *) key)->hash) == -1) {
1786 hash = PyObject_Hash(key);
1787 if (hash == -1)
1788 return NULL;
1789 }
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001790 return _PyDict_Pop_KnownHash(dict, key, hash, deflt);
Serhiy Storchaka67796522017-01-12 18:34:33 +02001791}
1792
Eric Snow96c6af92015-05-29 22:21:39 -06001793/* Internal version of dict.from_keys(). It is subclass-friendly. */
1794PyObject *
1795_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1796{
1797 PyObject *it; /* iter(iterable) */
1798 PyObject *key;
1799 PyObject *d;
1800 int status;
1801
Victor Stinnera5ed5f02016-12-06 18:45:50 +01001802 d = _PyObject_CallNoArg(cls);
Eric Snow96c6af92015-05-29 22:21:39 -06001803 if (d == NULL)
1804 return NULL;
1805
1806 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
1807 if (PyDict_CheckExact(iterable)) {
1808 PyDictObject *mp = (PyDictObject *)d;
1809 PyObject *oldvalue;
1810 Py_ssize_t pos = 0;
1811 PyObject *key;
1812 Py_hash_t hash;
1813
Serhiy Storchakac61ac162017-03-21 08:52:38 +02001814 if (dictresize(mp, ESTIMATE_SIZE(PyDict_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001815 Py_DECREF(d);
1816 return NULL;
1817 }
1818
1819 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
1820 if (insertdict(mp, key, hash, value)) {
1821 Py_DECREF(d);
1822 return NULL;
1823 }
1824 }
1825 return d;
1826 }
1827 if (PyAnySet_CheckExact(iterable)) {
1828 PyDictObject *mp = (PyDictObject *)d;
1829 Py_ssize_t pos = 0;
1830 PyObject *key;
1831 Py_hash_t hash;
1832
Victor Stinner742da042016-09-07 17:40:12 -07001833 if (dictresize(mp, ESTIMATE_SIZE(PySet_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001834 Py_DECREF(d);
1835 return NULL;
1836 }
1837
1838 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
1839 if (insertdict(mp, key, hash, value)) {
1840 Py_DECREF(d);
1841 return NULL;
1842 }
1843 }
1844 return d;
1845 }
1846 }
1847
1848 it = PyObject_GetIter(iterable);
1849 if (it == NULL){
1850 Py_DECREF(d);
1851 return NULL;
1852 }
1853
1854 if (PyDict_CheckExact(d)) {
1855 while ((key = PyIter_Next(it)) != NULL) {
1856 status = PyDict_SetItem(d, key, value);
1857 Py_DECREF(key);
1858 if (status < 0)
1859 goto Fail;
1860 }
1861 } else {
1862 while ((key = PyIter_Next(it)) != NULL) {
1863 status = PyObject_SetItem(d, key, value);
1864 Py_DECREF(key);
1865 if (status < 0)
1866 goto Fail;
1867 }
1868 }
1869
1870 if (PyErr_Occurred())
1871 goto Fail;
1872 Py_DECREF(it);
1873 return d;
1874
1875Fail:
1876 Py_DECREF(it);
1877 Py_DECREF(d);
1878 return NULL;
1879}
1880
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001881/* Methods */
1882
1883static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001884dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001885{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001886 PyObject **values = mp->ma_values;
1887 PyDictKeysObject *keys = mp->ma_keys;
1888 Py_ssize_t i, n;
INADA Naokia6296d32017-08-24 14:55:17 +09001889
1890 /* bpo-31095: UnTrack is needed before calling any callbacks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 PyObject_GC_UnTrack(mp);
1892 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001893 if (values != NULL) {
1894 if (values != empty_values) {
Victor Stinner742da042016-09-07 17:40:12 -07001895 for (i = 0, n = mp->ma_keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001896 Py_XDECREF(values[i]);
1897 }
1898 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001900 DK_DECREF(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001901 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02001902 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001903 assert(keys->dk_refcnt == 1);
1904 DK_DECREF(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001905 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1907 free_list[numfree++] = mp;
1908 else
1909 Py_TYPE(mp)->tp_free((PyObject *)mp);
1910 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001911}
1912
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001913
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001914static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001915dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001916{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001917 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001918 PyObject *key = NULL, *value = NULL;
1919 _PyUnicodeWriter writer;
1920 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00001921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 i = Py_ReprEnter((PyObject *)mp);
1923 if (i != 0) {
1924 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1925 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001927 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001928 Py_ReprLeave((PyObject *)mp);
1929 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 }
Tim Petersa7259592001-06-16 05:11:17 +00001931
Victor Stinnerf91929b2013-11-19 13:07:38 +01001932 _PyUnicodeWriter_Init(&writer);
1933 writer.overallocate = 1;
1934 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
1935 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00001936
Victor Stinnerf91929b2013-11-19 13:07:38 +01001937 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
1938 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 /* Do repr() on each key+value pair, and insert ": " between them.
1941 Note that repr may mutate the dict. */
1942 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001943 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001944 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001945 PyObject *s;
1946 int res;
1947
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001948 /* Prevent repr from deleting key or value during key format. */
1949 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02001951
Victor Stinnerf91929b2013-11-19 13:07:38 +01001952 if (!first) {
1953 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
1954 goto error;
1955 }
1956 first = 0;
1957
1958 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01001960 goto error;
1961 res = _PyUnicodeWriter_WriteStr(&writer, s);
1962 Py_DECREF(s);
1963 if (res < 0)
1964 goto error;
1965
1966 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
1967 goto error;
1968
1969 s = PyObject_Repr(value);
1970 if (s == NULL)
1971 goto error;
1972 res = _PyUnicodeWriter_WriteStr(&writer, s);
1973 Py_DECREF(s);
1974 if (res < 0)
1975 goto error;
1976
1977 Py_CLEAR(key);
1978 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 }
Tim Petersa7259592001-06-16 05:11:17 +00001980
Victor Stinnerf91929b2013-11-19 13:07:38 +01001981 writer.overallocate = 0;
1982 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
1983 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001985 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01001986
1987 return _PyUnicodeWriter_Finish(&writer);
1988
1989error:
1990 Py_ReprLeave((PyObject *)mp);
1991 _PyUnicodeWriter_Dealloc(&writer);
1992 Py_XDECREF(key);
1993 Py_XDECREF(value);
1994 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001995}
1996
Martin v. Löwis18e16552006-02-15 17:27:45 +00001997static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001998dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001999{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002001}
2002
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002003static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002004dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002005{
Victor Stinner742da042016-09-07 17:40:12 -07002006 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002007 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09002008 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002010 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002011 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002012 hash = PyObject_Hash(key);
2013 if (hash == -1)
2014 return NULL;
2015 }
INADA Naoki778928b2017-08-03 23:45:15 +09002016 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002017 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002018 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002019 if (ix == DKIX_EMPTY || value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002020 if (!PyDict_CheckExact(mp)) {
2021 /* Look up __missing__ method if we're a subclass. */
2022 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05002023 _Py_IDENTIFIER(__missing__);
2024 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 if (missing != NULL) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002026 res = PyObject_CallFunctionObjArgs(missing,
2027 key, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002028 Py_DECREF(missing);
2029 return res;
2030 }
2031 else if (PyErr_Occurred())
2032 return NULL;
2033 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07002034 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002035 return NULL;
2036 }
INADA Naokiba609772016-12-07 20:41:42 +09002037 Py_INCREF(value);
2038 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002039}
2040
2041static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002042dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002043{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002044 if (w == NULL)
2045 return PyDict_DelItem((PyObject *)mp, v);
2046 else
2047 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002048}
2049
Guido van Rossuma9e7a811997-05-13 21:02:11 +00002050static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002051 (lenfunc)dict_length, /*mp_length*/
2052 (binaryfunc)dict_subscript, /*mp_subscript*/
2053 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002054};
2055
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002056static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002057dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002058{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002059 PyObject *v;
2060 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002061 PyDictKeyEntry *ep;
2062 Py_ssize_t size, n, offset;
2063 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002064
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002065 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002066 n = mp->ma_used;
2067 v = PyList_New(n);
2068 if (v == NULL)
2069 return NULL;
2070 if (n != mp->ma_used) {
2071 /* Durnit. The allocations caused the dict to resize.
2072 * Just start over, this shouldn't normally happen.
2073 */
2074 Py_DECREF(v);
2075 goto again;
2076 }
Victor Stinner742da042016-09-07 17:40:12 -07002077 ep = DK_ENTRIES(mp->ma_keys);
2078 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002079 if (mp->ma_values) {
2080 value_ptr = mp->ma_values;
2081 offset = sizeof(PyObject *);
2082 }
2083 else {
2084 value_ptr = &ep[0].me_value;
2085 offset = sizeof(PyDictKeyEntry);
2086 }
2087 for (i = 0, j = 0; i < size; i++) {
2088 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002089 PyObject *key = ep[i].me_key;
2090 Py_INCREF(key);
2091 PyList_SET_ITEM(v, j, key);
2092 j++;
2093 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002094 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002095 }
2096 assert(j == n);
2097 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002098}
2099
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002100static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002101dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002102{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002103 PyObject *v;
2104 Py_ssize_t i, j;
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002105 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002106 Py_ssize_t size, n, offset;
2107 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002108
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002109 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002110 n = mp->ma_used;
2111 v = PyList_New(n);
2112 if (v == NULL)
2113 return NULL;
2114 if (n != mp->ma_used) {
2115 /* Durnit. The allocations caused the dict to resize.
2116 * Just start over, this shouldn't normally happen.
2117 */
2118 Py_DECREF(v);
2119 goto again;
2120 }
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002121 ep = DK_ENTRIES(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002122 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002123 if (mp->ma_values) {
2124 value_ptr = mp->ma_values;
2125 offset = sizeof(PyObject *);
2126 }
2127 else {
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002128 value_ptr = &ep[0].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002129 offset = sizeof(PyDictKeyEntry);
2130 }
2131 for (i = 0, j = 0; i < size; i++) {
2132 PyObject *value = *value_ptr;
2133 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2134 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002135 Py_INCREF(value);
2136 PyList_SET_ITEM(v, j, value);
2137 j++;
2138 }
2139 }
2140 assert(j == n);
2141 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002142}
2143
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002144static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002145dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002146{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002147 PyObject *v;
2148 Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002149 Py_ssize_t size, offset;
2150 PyObject *item, *key;
2151 PyDictKeyEntry *ep;
2152 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154 /* Preallocate the list of tuples, to avoid allocations during
2155 * the loop over the items, which could trigger GC, which
2156 * could resize the dict. :-(
2157 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002158 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002159 n = mp->ma_used;
2160 v = PyList_New(n);
2161 if (v == NULL)
2162 return NULL;
2163 for (i = 0; i < n; i++) {
2164 item = PyTuple_New(2);
2165 if (item == NULL) {
2166 Py_DECREF(v);
2167 return NULL;
2168 }
2169 PyList_SET_ITEM(v, i, item);
2170 }
2171 if (n != mp->ma_used) {
2172 /* Durnit. The allocations caused the dict to resize.
2173 * Just start over, this shouldn't normally happen.
2174 */
2175 Py_DECREF(v);
2176 goto again;
2177 }
2178 /* Nothing we do below makes any function calls. */
Victor Stinner742da042016-09-07 17:40:12 -07002179 ep = DK_ENTRIES(mp->ma_keys);
2180 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002181 if (mp->ma_values) {
2182 value_ptr = mp->ma_values;
2183 offset = sizeof(PyObject *);
2184 }
2185 else {
2186 value_ptr = &ep[0].me_value;
2187 offset = sizeof(PyDictKeyEntry);
2188 }
2189 for (i = 0, j = 0; i < size; i++) {
2190 PyObject *value = *value_ptr;
2191 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2192 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002193 key = ep[i].me_key;
2194 item = PyList_GET_ITEM(v, j);
2195 Py_INCREF(key);
2196 PyTuple_SET_ITEM(item, 0, key);
2197 Py_INCREF(value);
2198 PyTuple_SET_ITEM(item, 1, value);
2199 j++;
2200 }
2201 }
2202 assert(j == n);
2203 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002204}
2205
Larry Hastings5c661892014-01-24 06:17:25 -08002206/*[clinic input]
2207@classmethod
2208dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08002209 iterable: object
2210 value: object=None
2211 /
2212
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002213Create a new dictionary with keys from iterable and values set to value.
Larry Hastings5c661892014-01-24 06:17:25 -08002214[clinic start generated code]*/
2215
Larry Hastings5c661892014-01-24 06:17:25 -08002216static PyObject *
2217dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002218/*[clinic end generated code: output=8fb98e4b10384999 input=382ba4855d0f74c3]*/
Larry Hastings5c661892014-01-24 06:17:25 -08002219{
Eric Snow96c6af92015-05-29 22:21:39 -06002220 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002221}
2222
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002223static int
Victor Stinner742da042016-09-07 17:40:12 -07002224dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
2225 const char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002226{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002227 PyObject *arg = NULL;
2228 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002229
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002230 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002231 result = -1;
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002232 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002233 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002234 _Py_IDENTIFIER(keys);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002235 PyObject *func;
2236 if (_PyObject_LookupAttrId(arg, &PyId_keys, &func) < 0) {
2237 result = -1;
2238 }
2239 else if (func != NULL) {
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002240 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002241 result = PyDict_Merge(self, arg, 1);
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002242 }
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002243 else {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002244 result = PyDict_MergeFromSeq2(self, arg, 1);
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002245 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002246 }
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002248 if (result == 0 && kwds != NULL) {
2249 if (PyArg_ValidateKeywordArguments(kwds))
2250 result = PyDict_Merge(self, kwds, 1);
2251 else
2252 result = -1;
2253 }
2254 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002255}
2256
Victor Stinner91f0d4a2017-01-19 12:45:06 +01002257/* Note: dict.update() uses the METH_VARARGS|METH_KEYWORDS calling convention.
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002258 Using METH_FASTCALL|METH_KEYWORDS would make dict.update(**dict2) calls
2259 slower, see the issue #29312. */
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002260static PyObject *
2261dict_update(PyObject *self, PyObject *args, PyObject *kwds)
2262{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002263 if (dict_update_common(self, args, kwds, "update") != -1)
2264 Py_RETURN_NONE;
2265 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002266}
2267
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002268/* Update unconditionally replaces existing items.
2269 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00002270 otherwise it leaves existing items unchanged.
2271
2272 PyDict_{Update,Merge} update/merge from a mapping object.
2273
Tim Petersf582b822001-12-11 18:51:08 +00002274 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00002275 producing iterable objects of length 2.
2276*/
2277
Tim Petersf582b822001-12-11 18:51:08 +00002278int
Tim Peters1fc240e2001-10-26 05:06:50 +00002279PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
2280{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 PyObject *it; /* iter(seq2) */
2282 Py_ssize_t i; /* index into seq2 of current element */
2283 PyObject *item; /* seq2[i] */
2284 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00002285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002286 assert(d != NULL);
2287 assert(PyDict_Check(d));
2288 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00002289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002290 it = PyObject_GetIter(seq2);
2291 if (it == NULL)
2292 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 for (i = 0; ; ++i) {
2295 PyObject *key, *value;
2296 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00002297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 fast = NULL;
2299 item = PyIter_Next(it);
2300 if (item == NULL) {
2301 if (PyErr_Occurred())
2302 goto Fail;
2303 break;
2304 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002306 /* Convert item to sequence, and verify length 2. */
2307 fast = PySequence_Fast(item, "");
2308 if (fast == NULL) {
2309 if (PyErr_ExceptionMatches(PyExc_TypeError))
2310 PyErr_Format(PyExc_TypeError,
2311 "cannot convert dictionary update "
2312 "sequence element #%zd to a sequence",
2313 i);
2314 goto Fail;
2315 }
2316 n = PySequence_Fast_GET_SIZE(fast);
2317 if (n != 2) {
2318 PyErr_Format(PyExc_ValueError,
2319 "dictionary update sequence element #%zd "
2320 "has length %zd; 2 is required",
2321 i, n);
2322 goto Fail;
2323 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002325 /* Update/merge with this (key, value) pair. */
2326 key = PySequence_Fast_GET_ITEM(fast, 0);
2327 value = PySequence_Fast_GET_ITEM(fast, 1);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002328 Py_INCREF(key);
2329 Py_INCREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002330 if (override || PyDict_GetItem(d, key) == NULL) {
2331 int status = PyDict_SetItem(d, key, value);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002332 if (status < 0) {
2333 Py_DECREF(key);
2334 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002335 goto Fail;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002336 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002337 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002338 Py_DECREF(key);
2339 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002340 Py_DECREF(fast);
2341 Py_DECREF(item);
2342 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344 i = 0;
Victor Stinner611b0fa2016-09-14 15:02:01 +02002345 assert(_PyDict_CheckConsistency((PyDictObject *)d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002347Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002348 Py_XDECREF(item);
2349 Py_XDECREF(fast);
2350 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002351Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 Py_DECREF(it);
2353 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002354}
2355
doko@ubuntu.comc96df682016-10-11 08:04:02 +02002356static int
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002357dict_merge(PyObject *a, PyObject *b, int override)
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002358{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002359 PyDictObject *mp, *other;
2360 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -07002361 PyDictKeyEntry *entry, *ep0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002362
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002363 assert(0 <= override && override <= 2);
2364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365 /* We accept for the argument either a concrete dictionary object,
2366 * or an abstract "mapping" object. For the former, we can do
2367 * things quite efficiently. For the latter, we only require that
2368 * PyMapping_Keys() and PyObject_GetItem() be supported.
2369 */
2370 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2371 PyErr_BadInternalCall();
2372 return -1;
2373 }
2374 mp = (PyDictObject*)a;
2375 if (PyDict_Check(b)) {
2376 other = (PyDictObject*)b;
2377 if (other == mp || other->ma_used == 0)
2378 /* a.update(a) or a.update({}); nothing to do */
2379 return 0;
2380 if (mp->ma_used == 0)
2381 /* Since the target dict is empty, PyDict_GetItem()
2382 * always returns NULL. Setting override to 1
2383 * skips the unnecessary test.
2384 */
2385 override = 1;
2386 /* Do one big resize at the start, rather than
2387 * incrementally resizing as we insert new items. Expect
2388 * that there will be no (or few) overlapping keys.
2389 */
INADA Naokib1152be2016-10-27 19:26:50 +09002390 if (USABLE_FRACTION(mp->ma_keys->dk_size) < other->ma_used) {
2391 if (dictresize(mp, ESTIMATE_SIZE(mp->ma_used + other->ma_used))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392 return -1;
INADA Naokib1152be2016-10-27 19:26:50 +09002393 }
2394 }
Victor Stinner742da042016-09-07 17:40:12 -07002395 ep0 = DK_ENTRIES(other->ma_keys);
2396 for (i = 0, n = other->ma_keys->dk_nentries; i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002397 PyObject *key, *value;
2398 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002399 entry = &ep0[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002400 key = entry->me_key;
2401 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002402 if (other->ma_values)
2403 value = other->ma_values[i];
2404 else
2405 value = entry->me_value;
2406
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002407 if (value != NULL) {
2408 int err = 0;
2409 Py_INCREF(key);
2410 Py_INCREF(value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002411 if (override == 1)
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002412 err = insertdict(mp, key, hash, value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002413 else if (_PyDict_GetItem_KnownHash(a, key, hash) == NULL) {
2414 if (PyErr_Occurred()) {
2415 Py_DECREF(value);
2416 Py_DECREF(key);
2417 return -1;
2418 }
2419 err = insertdict(mp, key, hash, value);
2420 }
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002421 else if (override != 0) {
2422 _PyErr_SetKeyError(key);
2423 Py_DECREF(value);
2424 Py_DECREF(key);
2425 return -1;
2426 }
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002427 Py_DECREF(value);
2428 Py_DECREF(key);
2429 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002430 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002431
Victor Stinner742da042016-09-07 17:40:12 -07002432 if (n != other->ma_keys->dk_nentries) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002433 PyErr_SetString(PyExc_RuntimeError,
2434 "dict mutated during update");
2435 return -1;
2436 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002437 }
2438 }
2439 }
2440 else {
2441 /* Do it the generic, slower way */
2442 PyObject *keys = PyMapping_Keys(b);
2443 PyObject *iter;
2444 PyObject *key, *value;
2445 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002447 if (keys == NULL)
2448 /* Docstring says this is equivalent to E.keys() so
2449 * if E doesn't have a .keys() method we want
2450 * AttributeError to percolate up. Might as well
2451 * do the same for any other error.
2452 */
2453 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002455 iter = PyObject_GetIter(keys);
2456 Py_DECREF(keys);
2457 if (iter == NULL)
2458 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002460 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002461 if (override != 1 && PyDict_GetItem(a, key) != NULL) {
2462 if (override != 0) {
2463 _PyErr_SetKeyError(key);
2464 Py_DECREF(key);
2465 Py_DECREF(iter);
2466 return -1;
2467 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002468 Py_DECREF(key);
2469 continue;
2470 }
2471 value = PyObject_GetItem(b, key);
2472 if (value == NULL) {
2473 Py_DECREF(iter);
2474 Py_DECREF(key);
2475 return -1;
2476 }
2477 status = PyDict_SetItem(a, key, value);
2478 Py_DECREF(key);
2479 Py_DECREF(value);
2480 if (status < 0) {
2481 Py_DECREF(iter);
2482 return -1;
2483 }
2484 }
2485 Py_DECREF(iter);
2486 if (PyErr_Occurred())
2487 /* Iterator completed, via error */
2488 return -1;
2489 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02002490 assert(_PyDict_CheckConsistency((PyDictObject *)a));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002491 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002492}
2493
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002494int
2495PyDict_Update(PyObject *a, PyObject *b)
2496{
2497 return dict_merge(a, b, 1);
2498}
2499
2500int
2501PyDict_Merge(PyObject *a, PyObject *b, int override)
2502{
2503 /* XXX Deprecate override not in (0, 1). */
2504 return dict_merge(a, b, override != 0);
2505}
2506
2507int
2508_PyDict_MergeEx(PyObject *a, PyObject *b, int override)
2509{
2510 return dict_merge(a, b, override);
2511}
2512
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002513static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002514dict_copy(PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002515{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002516 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002517}
2518
2519PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002520PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002521{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002522 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002523 PyDictObject *mp;
2524 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002526 if (o == NULL || !PyDict_Check(o)) {
2527 PyErr_BadInternalCall();
2528 return NULL;
2529 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002530
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002531 mp = (PyDictObject *)o;
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002532 if (mp->ma_used == 0) {
2533 /* The dict is empty; just return a new dict. */
2534 return PyDict_New();
2535 }
2536
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002537 if (_PyDict_HasSplitTable(mp)) {
2538 PyDictObject *split_copy;
Victor Stinner742da042016-09-07 17:40:12 -07002539 Py_ssize_t size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
2540 PyObject **newvalues;
2541 newvalues = new_values(size);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002542 if (newvalues == NULL)
2543 return PyErr_NoMemory();
2544 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2545 if (split_copy == NULL) {
2546 free_values(newvalues);
2547 return NULL;
2548 }
2549 split_copy->ma_values = newvalues;
2550 split_copy->ma_keys = mp->ma_keys;
2551 split_copy->ma_used = mp->ma_used;
Miss Islington (bot)de755122018-04-02 20:00:26 -07002552 split_copy->ma_version_tag = DICT_NEXT_VERSION();
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002553 DK_INCREF(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002554 for (i = 0, n = size; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002555 PyObject *value = mp->ma_values[i];
2556 Py_XINCREF(value);
2557 split_copy->ma_values[i] = value;
2558 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002559 if (_PyObject_GC_IS_TRACKED(mp))
2560 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002561 return (PyObject *)split_copy;
2562 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002563
2564 if (PyDict_CheckExact(mp) && mp->ma_values == NULL &&
2565 (mp->ma_used >= (mp->ma_keys->dk_nentries * 2) / 3))
2566 {
2567 /* Use fast-copy if:
2568
2569 (1) 'mp' is an instance of a subclassed dict; and
2570
2571 (2) 'mp' is not a split-dict; and
2572
2573 (3) if 'mp' is non-compact ('del' operation does not resize dicts),
2574 do fast-copy only if it has at most 1/3 non-used keys.
2575
Miss Islington (bot)32955292018-04-20 14:00:41 -07002576 The last condition (3) is important to guard against a pathological
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002577 case when a large dict is almost emptied with multiple del/pop
2578 operations and copied after that. In cases like this, we defer to
2579 PyDict_Merge, which produces a compacted copy.
2580 */
2581 return clone_combined_dict(mp);
2582 }
2583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002584 copy = PyDict_New();
2585 if (copy == NULL)
2586 return NULL;
2587 if (PyDict_Merge(copy, o, 1) == 0)
2588 return copy;
2589 Py_DECREF(copy);
2590 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002591}
2592
Martin v. Löwis18e16552006-02-15 17:27:45 +00002593Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002594PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002595{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002596 if (mp == NULL || !PyDict_Check(mp)) {
2597 PyErr_BadInternalCall();
2598 return -1;
2599 }
2600 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002601}
2602
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002603PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002604PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002605{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002606 if (mp == NULL || !PyDict_Check(mp)) {
2607 PyErr_BadInternalCall();
2608 return NULL;
2609 }
2610 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002611}
2612
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002613PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002614PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002615{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002616 if (mp == NULL || !PyDict_Check(mp)) {
2617 PyErr_BadInternalCall();
2618 return NULL;
2619 }
2620 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002621}
2622
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002623PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002624PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002625{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002626 if (mp == NULL || !PyDict_Check(mp)) {
2627 PyErr_BadInternalCall();
2628 return NULL;
2629 }
2630 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002631}
2632
Tim Peterse63415e2001-05-08 04:38:29 +00002633/* Return 1 if dicts equal, 0 if not, -1 if error.
2634 * Gets out as soon as any difference is detected.
2635 * Uses only Py_EQ comparison.
2636 */
2637static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002638dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002639{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002640 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002642 if (a->ma_used != b->ma_used)
2643 /* can't be equal if # of entries differ */
2644 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Victor Stinner742da042016-09-07 17:40:12 -07002646 for (i = 0; i < a->ma_keys->dk_nentries; i++) {
2647 PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002648 PyObject *aval;
2649 if (a->ma_values)
2650 aval = a->ma_values[i];
2651 else
2652 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002653 if (aval != NULL) {
2654 int cmp;
2655 PyObject *bval;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002656 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002657 /* temporarily bump aval's refcount to ensure it stays
2658 alive until we're done with it */
2659 Py_INCREF(aval);
2660 /* ditto for key */
2661 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002662 /* reuse the known hash value */
INADA Naoki778928b2017-08-03 23:45:15 +09002663 b->ma_keys->dk_lookup(b, key, ep->me_hash, &bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002664 if (bval == NULL) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002665 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002666 Py_DECREF(aval);
2667 if (PyErr_Occurred())
2668 return -1;
2669 return 0;
2670 }
2671 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002672 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002673 Py_DECREF(aval);
2674 if (cmp <= 0) /* error or not equal */
2675 return cmp;
2676 }
2677 }
2678 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002679}
Tim Peterse63415e2001-05-08 04:38:29 +00002680
2681static PyObject *
2682dict_richcompare(PyObject *v, PyObject *w, int op)
2683{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002684 int cmp;
2685 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002686
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002687 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2688 res = Py_NotImplemented;
2689 }
2690 else if (op == Py_EQ || op == Py_NE) {
2691 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2692 if (cmp < 0)
2693 return NULL;
2694 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2695 }
2696 else
2697 res = Py_NotImplemented;
2698 Py_INCREF(res);
2699 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002700}
Tim Peterse63415e2001-05-08 04:38:29 +00002701
Larry Hastings61272b72014-01-07 12:41:53 -08002702/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002703
2704@coexist
2705dict.__contains__
2706
2707 key: object
2708 /
2709
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002710True if the dictionary has the specified key, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002711[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002712
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002713static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002714dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka19d25972017-02-04 08:05:07 +02002715/*[clinic end generated code: output=a3d03db709ed6e6b input=fe1cb42ad831e820]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002716{
Larry Hastingsc2047262014-01-25 20:43:29 -08002717 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002718 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002719 Py_ssize_t ix;
INADA Naokiba609772016-12-07 20:41:42 +09002720 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002722 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002723 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002724 hash = PyObject_Hash(key);
2725 if (hash == -1)
2726 return NULL;
2727 }
INADA Naoki778928b2017-08-03 23:45:15 +09002728 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002729 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002730 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002731 if (ix == DKIX_EMPTY || value == NULL)
Victor Stinner742da042016-09-07 17:40:12 -07002732 Py_RETURN_FALSE;
2733 Py_RETURN_TRUE;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002734}
2735
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002736/*[clinic input]
2737dict.get
2738
2739 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002740 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002741 /
2742
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002743Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002744[clinic start generated code]*/
2745
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002746static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002747dict_get_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002748/*[clinic end generated code: output=bba707729dee05bf input=279ddb5790b6b107]*/
Barry Warsawc38c5da1997-10-06 17:49:20 +00002749{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002750 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002751 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002752 Py_ssize_t ix;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002754 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002755 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002756 hash = PyObject_Hash(key);
2757 if (hash == -1)
2758 return NULL;
2759 }
INADA Naoki778928b2017-08-03 23:45:15 +09002760 ix = (self->ma_keys->dk_lookup) (self, key, hash, &val);
Victor Stinner742da042016-09-07 17:40:12 -07002761 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002762 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002763 if (ix == DKIX_EMPTY || val == NULL) {
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002764 val = default_value;
INADA Naokiba609772016-12-07 20:41:42 +09002765 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002766 Py_INCREF(val);
2767 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002768}
2769
Benjamin Peterson00e98862013-03-07 22:16:29 -05002770PyObject *
2771PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002772{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002773 PyDictObject *mp = (PyDictObject *)d;
INADA Naoki93f26f72016-11-02 18:45:16 +09002774 PyObject *value;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002775 Py_hash_t hash;
Guido van Rossum164452c2000-08-08 16:12:54 +00002776
Benjamin Peterson00e98862013-03-07 22:16:29 -05002777 if (!PyDict_Check(d)) {
2778 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002779 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002780 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002781
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002782 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002783 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002784 hash = PyObject_Hash(key);
2785 if (hash == -1)
2786 return NULL;
2787 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002788
2789 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
2790 if (insertion_resize(mp) < 0)
2791 return NULL;
2792 }
2793
INADA Naoki778928b2017-08-03 23:45:15 +09002794 Py_ssize_t ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002795 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002796 return NULL;
INADA Naoki93f26f72016-11-02 18:45:16 +09002797
2798 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09002799 ((ix >= 0 && value == NULL && mp->ma_used != ix) ||
INADA Naoki93f26f72016-11-02 18:45:16 +09002800 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
2801 if (insertion_resize(mp) < 0) {
2802 return NULL;
2803 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002804 ix = DKIX_EMPTY;
2805 }
2806
2807 if (ix == DKIX_EMPTY) {
2808 PyDictKeyEntry *ep, *ep0;
2809 value = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002810 if (mp->ma_keys->dk_usable <= 0) {
Victor Stinner3c336c52016-09-12 14:17:40 +02002811 if (insertion_resize(mp) < 0) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002812 return NULL;
Victor Stinner3c336c52016-09-12 14:17:40 +02002813 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002814 }
INADA Naoki778928b2017-08-03 23:45:15 +09002815 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naoki93f26f72016-11-02 18:45:16 +09002816 ep0 = DK_ENTRIES(mp->ma_keys);
2817 ep = &ep0[mp->ma_keys->dk_nentries];
2818 dk_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002819 Py_INCREF(key);
INADA Naoki93f26f72016-11-02 18:45:16 +09002820 Py_INCREF(value);
2821 MAINTAIN_TRACKING(mp, key, value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002822 ep->me_key = key;
2823 ep->me_hash = hash;
INADA Naokiba609772016-12-07 20:41:42 +09002824 if (_PyDict_HasSplitTable(mp)) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002825 assert(mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
2826 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Victor Stinner742da042016-09-07 17:40:12 -07002827 }
2828 else {
INADA Naoki93f26f72016-11-02 18:45:16 +09002829 ep->me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07002830 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002831 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002832 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naoki93f26f72016-11-02 18:45:16 +09002833 mp->ma_keys->dk_usable--;
2834 mp->ma_keys->dk_nentries++;
2835 assert(mp->ma_keys->dk_usable >= 0);
2836 }
INADA Naokiba609772016-12-07 20:41:42 +09002837 else if (value == NULL) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002838 value = defaultobj;
2839 assert(_PyDict_HasSplitTable(mp));
2840 assert(ix == mp->ma_used);
2841 Py_INCREF(value);
2842 MAINTAIN_TRACKING(mp, key, value);
INADA Naokiba609772016-12-07 20:41:42 +09002843 mp->ma_values[ix] = value;
INADA Naoki93f26f72016-11-02 18:45:16 +09002844 mp->ma_used++;
2845 mp->ma_version_tag = DICT_NEXT_VERSION();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002846 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002847
2848 assert(_PyDict_CheckConsistency(mp));
2849 return value;
Guido van Rossum164452c2000-08-08 16:12:54 +00002850}
2851
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002852/*[clinic input]
2853dict.setdefault
2854
2855 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002856 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002857 /
2858
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002859Insert key with a value of default if key is not in the dictionary.
2860
2861Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002862[clinic start generated code]*/
2863
Benjamin Peterson00e98862013-03-07 22:16:29 -05002864static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002865dict_setdefault_impl(PyDictObject *self, PyObject *key,
2866 PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002867/*[clinic end generated code: output=f8c1101ebf69e220 input=0f063756e815fd9d]*/
Benjamin Peterson00e98862013-03-07 22:16:29 -05002868{
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002869 PyObject *val;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002870
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002871 val = PyDict_SetDefault((PyObject *)self, key, default_value);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002872 Py_XINCREF(val);
2873 return val;
2874}
Guido van Rossum164452c2000-08-08 16:12:54 +00002875
2876static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002877dict_clear(PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002878{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 PyDict_Clear((PyObject *)mp);
2880 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002881}
2882
Guido van Rossumba6ab842000-12-12 22:02:18 +00002883static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002884dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002885{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002886 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00002887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002888 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2889 return NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06002890
Yury Selivanov684ef2c2016-10-28 19:01:21 -04002891 return _PyDict_Pop((PyObject*)mp, key, deflt);
Guido van Rossume027d982002-04-12 15:11:59 +00002892}
2893
2894static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002895dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002896{
Victor Stinner742da042016-09-07 17:40:12 -07002897 Py_ssize_t i, j;
2898 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002899 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 /* Allocate the result tuple before checking the size. Believe it
2902 * or not, this allocation could trigger a garbage collection which
2903 * could empty the dict, so if we checked the size first and that
2904 * happened, the result would be an infinite loop (searching for an
2905 * entry that no longer exists). Note that the usual popitem()
2906 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2907 * tuple away if the dict *is* empty isn't a significant
2908 * inefficiency -- possible, but unlikely in practice.
2909 */
2910 res = PyTuple_New(2);
2911 if (res == NULL)
2912 return NULL;
2913 if (mp->ma_used == 0) {
2914 Py_DECREF(res);
2915 PyErr_SetString(PyExc_KeyError,
2916 "popitem(): dictionary is empty");
2917 return NULL;
2918 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002919 /* Convert split table to combined table */
2920 if (mp->ma_keys->dk_lookup == lookdict_split) {
2921 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2922 Py_DECREF(res);
2923 return NULL;
2924 }
2925 }
2926 ENSURE_ALLOWS_DELETIONS(mp);
Victor Stinner742da042016-09-07 17:40:12 -07002927
2928 /* Pop last item */
2929 ep0 = DK_ENTRIES(mp->ma_keys);
2930 i = mp->ma_keys->dk_nentries - 1;
2931 while (i >= 0 && ep0[i].me_value == NULL) {
2932 i--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002933 }
Victor Stinner742da042016-09-07 17:40:12 -07002934 assert(i >= 0);
2935
2936 ep = &ep0[i];
2937 j = lookdict_index(mp->ma_keys, ep->me_hash, i);
2938 assert(j >= 0);
2939 assert(dk_get_index(mp->ma_keys, j) == i);
2940 dk_set_index(mp->ma_keys, j, DKIX_DUMMY);
2941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002942 PyTuple_SET_ITEM(res, 0, ep->me_key);
2943 PyTuple_SET_ITEM(res, 1, ep->me_value);
Victor Stinner742da042016-09-07 17:40:12 -07002944 ep->me_key = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002945 ep->me_value = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002946 /* We can't dk_usable++ since there is DKIX_DUMMY in indices */
2947 mp->ma_keys->dk_nentries = i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002948 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002949 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02002950 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002951 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002952}
2953
Jeremy Hylton8caad492000-06-23 14:18:11 +00002954static int
2955dict_traverse(PyObject *op, visitproc visit, void *arg)
2956{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002957 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson55f44522016-09-05 12:12:59 -07002958 PyDictKeysObject *keys = mp->ma_keys;
Serhiy Storchaka46825d22016-09-26 21:29:34 +03002959 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Victor Stinner742da042016-09-07 17:40:12 -07002960 Py_ssize_t i, n = keys->dk_nentries;
2961
Benjamin Peterson55f44522016-09-05 12:12:59 -07002962 if (keys->dk_lookup == lookdict) {
2963 for (i = 0; i < n; i++) {
2964 if (entries[i].me_value != NULL) {
2965 Py_VISIT(entries[i].me_value);
2966 Py_VISIT(entries[i].me_key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002967 }
2968 }
Victor Stinner742da042016-09-07 17:40:12 -07002969 }
2970 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002971 if (mp->ma_values != NULL) {
Benjamin Peterson55f44522016-09-05 12:12:59 -07002972 for (i = 0; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002973 Py_VISIT(mp->ma_values[i]);
2974 }
2975 }
2976 else {
Benjamin Peterson55f44522016-09-05 12:12:59 -07002977 for (i = 0; i < n; i++) {
2978 Py_VISIT(entries[i].me_value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002979 }
2980 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002981 }
2982 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002983}
2984
2985static int
2986dict_tp_clear(PyObject *op)
2987{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002988 PyDict_Clear(op);
2989 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002990}
2991
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002992static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002993
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002994Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06002995_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002996{
Victor Stinner742da042016-09-07 17:40:12 -07002997 Py_ssize_t size, usable, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002998
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002999 size = DK_SIZE(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07003000 usable = USABLE_FRACTION(size);
3001
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02003002 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003003 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -07003004 res += usable * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003005 /* If the dictionary is split, the keys portion is accounted-for
3006 in the type object. */
3007 if (mp->ma_keys->dk_refcnt == 1)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003008 res += (sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003009 + DK_IXSIZE(mp->ma_keys) * size
3010 + sizeof(PyDictKeyEntry) * usable);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003011 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003012}
3013
3014Py_ssize_t
3015_PyDict_KeysSize(PyDictKeysObject *keys)
3016{
Victor Stinner98ee9d52016-09-08 09:33:56 -07003017 return (sizeof(PyDictKeysObject)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003018 + DK_IXSIZE(keys) * DK_SIZE(keys)
3019 + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003020}
3021
doko@ubuntu.com17210f52016-01-14 14:04:59 +01003022static PyObject *
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003023dict_sizeof(PyDictObject *mp)
3024{
3025 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
3026}
3027
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00003028PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
3029
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003030PyDoc_STRVAR(sizeof__doc__,
3031"D.__sizeof__() -> size of D in memory, in bytes");
3032
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003033PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00003034"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00003035If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00003036
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003037PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00003038"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000030392-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003040
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003041PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04003042"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
3043If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
3044If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
3045In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00003046
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003047PyDoc_STRVAR(clear__doc__,
3048"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003049
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003050PyDoc_STRVAR(copy__doc__,
3051"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00003052
Guido van Rossumb90c8482007-02-10 01:11:45 +00003053/* Forward */
3054static PyObject *dictkeys_new(PyObject *);
3055static PyObject *dictitems_new(PyObject *);
3056static PyObject *dictvalues_new(PyObject *);
3057
Guido van Rossum45c85d12007-07-27 16:31:40 +00003058PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003059 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003060PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003061 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003062PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003063 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00003064
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003065static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07003066 DICT___CONTAINS___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003067 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
3068 getitem__doc__},
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003069 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003070 sizeof__doc__},
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003071 DICT_GET_METHODDEF
3072 DICT_SETDEFAULT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003073 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
3074 pop__doc__},
3075 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
3076 popitem__doc__},
3077 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
3078 keys__doc__},
3079 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
3080 items__doc__},
3081 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
3082 values__doc__},
3083 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
3084 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08003085 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003086 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
3087 clear__doc__},
3088 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
3089 copy__doc__},
3090 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003091};
3092
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003093/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00003094int
3095PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003096{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003097 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07003098 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003099 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003100 PyObject *value;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003102 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003103 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003104 hash = PyObject_Hash(key);
3105 if (hash == -1)
3106 return -1;
3107 }
INADA Naoki778928b2017-08-03 23:45:15 +09003108 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003109 if (ix == DKIX_ERROR)
3110 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003111 return (ix != DKIX_EMPTY && value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003112}
3113
Thomas Wouterscf297e42007-02-23 15:07:44 +00003114/* Internal version of PyDict_Contains used when the hash value is already known */
3115int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003116_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00003117{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003118 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003119 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07003120 Py_ssize_t ix;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003121
INADA Naoki778928b2017-08-03 23:45:15 +09003122 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003123 if (ix == DKIX_ERROR)
3124 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003125 return (ix != DKIX_EMPTY && value != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00003126}
3127
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003128/* Hack to implement "key in dict" */
3129static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003130 0, /* sq_length */
3131 0, /* sq_concat */
3132 0, /* sq_repeat */
3133 0, /* sq_item */
3134 0, /* sq_slice */
3135 0, /* sq_ass_item */
3136 0, /* sq_ass_slice */
3137 PyDict_Contains, /* sq_contains */
3138 0, /* sq_inplace_concat */
3139 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003140};
3141
Guido van Rossum09e563a2001-05-01 12:10:21 +00003142static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003143dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3144{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003145 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003146 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003148 assert(type != NULL && type->tp_alloc != NULL);
3149 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02003150 if (self == NULL)
3151 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003152 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003153
Victor Stinnera9f61a52013-07-16 22:17:26 +02003154 /* The object has been implicitly tracked by tp_alloc */
3155 if (type == &PyDict_Type)
3156 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003157
3158 d->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003159 d->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07003160 d->ma_keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003161 if (d->ma_keys == NULL) {
3162 Py_DECREF(self);
3163 return NULL;
3164 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02003165 assert(_PyDict_CheckConsistency(d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003166 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003167}
3168
Tim Peters25786c02001-09-02 08:22:48 +00003169static int
3170dict_init(PyObject *self, PyObject *args, PyObject *kwds)
3171{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003172 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00003173}
3174
Tim Peters6d6c1a32001-08-02 04:15:00 +00003175static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003176dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00003177{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003178 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003179}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003180
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003181PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00003182"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00003183"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003184" (key, value) pairs\n"
3185"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00003186" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003187" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00003188" d[k] = v\n"
3189"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
3190" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00003191
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003192PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003193 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3194 "dict",
3195 sizeof(PyDictObject),
3196 0,
3197 (destructor)dict_dealloc, /* tp_dealloc */
3198 0, /* tp_print */
3199 0, /* tp_getattr */
3200 0, /* tp_setattr */
3201 0, /* tp_reserved */
3202 (reprfunc)dict_repr, /* tp_repr */
3203 0, /* tp_as_number */
3204 &dict_as_sequence, /* tp_as_sequence */
3205 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003206 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003207 0, /* tp_call */
3208 0, /* tp_str */
3209 PyObject_GenericGetAttr, /* tp_getattro */
3210 0, /* tp_setattro */
3211 0, /* tp_as_buffer */
3212 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3213 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
3214 dictionary_doc, /* tp_doc */
3215 dict_traverse, /* tp_traverse */
3216 dict_tp_clear, /* tp_clear */
3217 dict_richcompare, /* tp_richcompare */
3218 0, /* tp_weaklistoffset */
3219 (getiterfunc)dict_iter, /* tp_iter */
3220 0, /* tp_iternext */
3221 mapp_methods, /* tp_methods */
3222 0, /* tp_members */
3223 0, /* tp_getset */
3224 0, /* tp_base */
3225 0, /* tp_dict */
3226 0, /* tp_descr_get */
3227 0, /* tp_descr_set */
3228 0, /* tp_dictoffset */
3229 dict_init, /* tp_init */
3230 PyType_GenericAlloc, /* tp_alloc */
3231 dict_new, /* tp_new */
3232 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003233};
3234
Victor Stinner3c1e4812012-03-26 22:10:51 +02003235PyObject *
3236_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
3237{
3238 PyObject *kv;
3239 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02003240 if (kv == NULL) {
3241 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003242 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02003243 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02003244 return PyDict_GetItem(dp, kv);
3245}
3246
Guido van Rossum3cca2451997-05-16 14:23:33 +00003247/* For backward compatibility with old dictionary interface */
3248
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003249PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003250PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003251{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003252 PyObject *kv, *rv;
3253 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003254 if (kv == NULL) {
3255 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003256 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003257 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003258 rv = PyDict_GetItem(v, kv);
3259 Py_DECREF(kv);
3260 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003261}
3262
3263int
Victor Stinner3c1e4812012-03-26 22:10:51 +02003264_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
3265{
3266 PyObject *kv;
3267 kv = _PyUnicode_FromId(key); /* borrowed */
3268 if (kv == NULL)
3269 return -1;
3270 return PyDict_SetItem(v, kv, item);
3271}
3272
3273int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003274PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003275{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003276 PyObject *kv;
3277 int err;
3278 kv = PyUnicode_FromString(key);
3279 if (kv == NULL)
3280 return -1;
3281 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
3282 err = PyDict_SetItem(v, kv, item);
3283 Py_DECREF(kv);
3284 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003285}
3286
3287int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01003288_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
3289{
3290 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3291 if (kv == NULL)
3292 return -1;
3293 return PyDict_DelItem(v, kv);
3294}
3295
3296int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003297PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003298{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003299 PyObject *kv;
3300 int err;
3301 kv = PyUnicode_FromString(key);
3302 if (kv == NULL)
3303 return -1;
3304 err = PyDict_DelItem(v, kv);
3305 Py_DECREF(kv);
3306 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003307}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003308
Raymond Hettinger019a1482004-03-18 02:41:19 +00003309/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003310
3311typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003312 PyObject_HEAD
3313 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
3314 Py_ssize_t di_used;
3315 Py_ssize_t di_pos;
3316 PyObject* di_result; /* reusable result tuple for iteritems */
3317 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003318} dictiterobject;
3319
3320static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003321dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003322{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003323 dictiterobject *di;
3324 di = PyObject_GC_New(dictiterobject, itertype);
3325 if (di == NULL)
3326 return NULL;
3327 Py_INCREF(dict);
3328 di->di_dict = dict;
3329 di->di_used = dict->ma_used;
3330 di->di_pos = 0;
3331 di->len = dict->ma_used;
3332 if (itertype == &PyDictIterItem_Type) {
3333 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
3334 if (di->di_result == NULL) {
3335 Py_DECREF(di);
3336 return NULL;
3337 }
3338 }
3339 else
3340 di->di_result = NULL;
3341 _PyObject_GC_TRACK(di);
3342 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003343}
3344
3345static void
3346dictiter_dealloc(dictiterobject *di)
3347{
INADA Naokia6296d32017-08-24 14:55:17 +09003348 /* bpo-31095: UnTrack is needed before calling any callbacks */
3349 _PyObject_GC_UNTRACK(di);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003350 Py_XDECREF(di->di_dict);
3351 Py_XDECREF(di->di_result);
3352 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003353}
3354
3355static int
3356dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
3357{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003358 Py_VISIT(di->di_dict);
3359 Py_VISIT(di->di_result);
3360 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003361}
3362
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003363static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003364dictiter_len(dictiterobject *di)
3365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003366 Py_ssize_t len = 0;
3367 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
3368 len = di->len;
3369 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003370}
3371
Guido van Rossumb90c8482007-02-10 01:11:45 +00003372PyDoc_STRVAR(length_hint_doc,
3373 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003374
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003375static PyObject *
3376dictiter_reduce(dictiterobject *di);
3377
3378PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3379
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003380static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003381 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
3382 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003383 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
3384 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003385 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003386};
3387
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003388static PyObject*
3389dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00003390{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003391 PyObject *key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003392 Py_ssize_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003393 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003394 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00003395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003396 if (d == NULL)
3397 return NULL;
3398 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00003399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003400 if (di->di_used != d->ma_used) {
3401 PyErr_SetString(PyExc_RuntimeError,
3402 "dictionary changed size during iteration");
3403 di->di_used = -1; /* Make this state sticky */
3404 return NULL;
3405 }
Guido van Rossum2147df72002-07-16 20:30:22 +00003406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003407 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003408 k = d->ma_keys;
INADA Naokica2d8be2016-11-04 16:59:10 +09003409 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003410 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003411 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003412 goto fail;
3413 key = DK_ENTRIES(k)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003414 assert(d->ma_values[i] != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003415 }
3416 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003417 Py_ssize_t n = k->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003418 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3419 while (i < n && entry_ptr->me_value == NULL) {
3420 entry_ptr++;
3421 i++;
3422 }
3423 if (i >= n)
3424 goto fail;
3425 key = entry_ptr->me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003426 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003427 di->di_pos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003428 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003429 Py_INCREF(key);
3430 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003431
3432fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003433 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003434 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003435 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003436}
3437
Raymond Hettinger019a1482004-03-18 02:41:19 +00003438PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003439 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3440 "dict_keyiterator", /* tp_name */
3441 sizeof(dictiterobject), /* tp_basicsize */
3442 0, /* tp_itemsize */
3443 /* methods */
3444 (destructor)dictiter_dealloc, /* tp_dealloc */
3445 0, /* tp_print */
3446 0, /* tp_getattr */
3447 0, /* tp_setattr */
3448 0, /* tp_reserved */
3449 0, /* tp_repr */
3450 0, /* tp_as_number */
3451 0, /* tp_as_sequence */
3452 0, /* tp_as_mapping */
3453 0, /* tp_hash */
3454 0, /* tp_call */
3455 0, /* tp_str */
3456 PyObject_GenericGetAttr, /* tp_getattro */
3457 0, /* tp_setattro */
3458 0, /* tp_as_buffer */
3459 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3460 0, /* tp_doc */
3461 (traverseproc)dictiter_traverse, /* tp_traverse */
3462 0, /* tp_clear */
3463 0, /* tp_richcompare */
3464 0, /* tp_weaklistoffset */
3465 PyObject_SelfIter, /* tp_iter */
3466 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3467 dictiter_methods, /* tp_methods */
3468 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003469};
3470
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003471static PyObject *
3472dictiter_iternextvalue(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003473{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003474 PyObject *value;
INADA Naokica2d8be2016-11-04 16:59:10 +09003475 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003476 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003478 if (d == NULL)
3479 return NULL;
3480 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003482 if (di->di_used != d->ma_used) {
3483 PyErr_SetString(PyExc_RuntimeError,
3484 "dictionary changed size during iteration");
3485 di->di_used = -1; /* Make this state sticky */
3486 return NULL;
3487 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003489 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003490 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003491 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003492 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003493 goto fail;
INADA Naokica2d8be2016-11-04 16:59:10 +09003494 value = d->ma_values[i];
3495 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003496 }
3497 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003498 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003499 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3500 while (i < n && entry_ptr->me_value == NULL) {
3501 entry_ptr++;
3502 i++;
3503 }
3504 if (i >= n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003505 goto fail;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003506 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003507 }
3508 di->di_pos = i+1;
3509 di->len--;
3510 Py_INCREF(value);
3511 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003512
3513fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003514 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003515 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003516 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003517}
3518
3519PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003520 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3521 "dict_valueiterator", /* tp_name */
3522 sizeof(dictiterobject), /* tp_basicsize */
3523 0, /* tp_itemsize */
3524 /* methods */
3525 (destructor)dictiter_dealloc, /* tp_dealloc */
3526 0, /* tp_print */
3527 0, /* tp_getattr */
3528 0, /* tp_setattr */
3529 0, /* tp_reserved */
3530 0, /* tp_repr */
3531 0, /* tp_as_number */
3532 0, /* tp_as_sequence */
3533 0, /* tp_as_mapping */
3534 0, /* tp_hash */
3535 0, /* tp_call */
3536 0, /* tp_str */
3537 PyObject_GenericGetAttr, /* tp_getattro */
3538 0, /* tp_setattro */
3539 0, /* tp_as_buffer */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003540 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003541 0, /* tp_doc */
3542 (traverseproc)dictiter_traverse, /* tp_traverse */
3543 0, /* tp_clear */
3544 0, /* tp_richcompare */
3545 0, /* tp_weaklistoffset */
3546 PyObject_SelfIter, /* tp_iter */
3547 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3548 dictiter_methods, /* tp_methods */
3549 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003550};
3551
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003552static PyObject *
3553dictiter_iternextitem(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003554{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003555 PyObject *key, *value, *result;
INADA Naokica2d8be2016-11-04 16:59:10 +09003556 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003557 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003559 if (d == NULL)
3560 return NULL;
3561 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003563 if (di->di_used != d->ma_used) {
3564 PyErr_SetString(PyExc_RuntimeError,
3565 "dictionary changed size during iteration");
3566 di->di_used = -1; /* Make this state sticky */
3567 return NULL;
3568 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003570 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003571 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003572 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003573 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003574 goto fail;
3575 key = DK_ENTRIES(d->ma_keys)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003576 value = d->ma_values[i];
3577 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003578 }
3579 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003580 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003581 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3582 while (i < n && entry_ptr->me_value == NULL) {
3583 entry_ptr++;
3584 i++;
3585 }
3586 if (i >= n)
3587 goto fail;
3588 key = entry_ptr->me_key;
3589 value = entry_ptr->me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003590 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003591 di->di_pos = i+1;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003592 di->len--;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003593 Py_INCREF(key);
3594 Py_INCREF(value);
3595 result = di->di_result;
3596 if (Py_REFCNT(result) == 1) {
3597 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3598 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3599 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3600 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003601 Py_INCREF(result);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003602 Py_DECREF(oldkey);
3603 Py_DECREF(oldvalue);
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003604 }
3605 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003606 result = PyTuple_New(2);
3607 if (result == NULL)
3608 return NULL;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003609 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3610 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003611 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003612 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003613
3614fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003615 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003616 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003617 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003618}
3619
3620PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003621 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3622 "dict_itemiterator", /* tp_name */
3623 sizeof(dictiterobject), /* tp_basicsize */
3624 0, /* tp_itemsize */
3625 /* methods */
3626 (destructor)dictiter_dealloc, /* tp_dealloc */
3627 0, /* tp_print */
3628 0, /* tp_getattr */
3629 0, /* tp_setattr */
3630 0, /* tp_reserved */
3631 0, /* tp_repr */
3632 0, /* tp_as_number */
3633 0, /* tp_as_sequence */
3634 0, /* tp_as_mapping */
3635 0, /* tp_hash */
3636 0, /* tp_call */
3637 0, /* tp_str */
3638 PyObject_GenericGetAttr, /* tp_getattro */
3639 0, /* tp_setattro */
3640 0, /* tp_as_buffer */
3641 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3642 0, /* tp_doc */
3643 (traverseproc)dictiter_traverse, /* tp_traverse */
3644 0, /* tp_clear */
3645 0, /* tp_richcompare */
3646 0, /* tp_weaklistoffset */
3647 PyObject_SelfIter, /* tp_iter */
3648 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3649 dictiter_methods, /* tp_methods */
3650 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003651};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003652
3653
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003654static PyObject *
3655dictiter_reduce(dictiterobject *di)
3656{
3657 PyObject *list;
3658 dictiterobject tmp;
3659
3660 list = PyList_New(0);
3661 if (!list)
3662 return NULL;
3663
3664 /* copy the itertor state */
3665 tmp = *di;
3666 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003667
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003668 /* iterate the temporary into a list */
3669 for(;;) {
3670 PyObject *element = 0;
3671 if (Py_TYPE(di) == &PyDictIterItem_Type)
3672 element = dictiter_iternextitem(&tmp);
3673 else if (Py_TYPE(di) == &PyDictIterKey_Type)
3674 element = dictiter_iternextkey(&tmp);
3675 else if (Py_TYPE(di) == &PyDictIterValue_Type)
3676 element = dictiter_iternextvalue(&tmp);
3677 else
Barry Warsawb2e57942017-09-14 18:13:16 -07003678 Py_UNREACHABLE();
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003679 if (element) {
3680 if (PyList_Append(list, element)) {
3681 Py_DECREF(element);
3682 Py_DECREF(list);
3683 Py_XDECREF(tmp.di_dict);
3684 return NULL;
3685 }
3686 Py_DECREF(element);
3687 } else
3688 break;
3689 }
3690 Py_XDECREF(tmp.di_dict);
3691 /* check for error */
3692 if (tmp.di_dict != NULL) {
3693 /* we have an error */
3694 Py_DECREF(list);
3695 return NULL;
3696 }
Antoine Pitroua7013882012-04-05 00:04:20 +02003697 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003698}
3699
Guido van Rossum3ac67412007-02-10 18:55:06 +00003700/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003701/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003702/***********************************************/
3703
Guido van Rossumb90c8482007-02-10 01:11:45 +00003704/* The instance lay-out is the same for all three; but the type differs. */
3705
Guido van Rossumb90c8482007-02-10 01:11:45 +00003706static void
Eric Snow96c6af92015-05-29 22:21:39 -06003707dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003708{
INADA Naokia6296d32017-08-24 14:55:17 +09003709 /* bpo-31095: UnTrack is needed before calling any callbacks */
3710 _PyObject_GC_UNTRACK(dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003711 Py_XDECREF(dv->dv_dict);
3712 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003713}
3714
3715static int
Eric Snow96c6af92015-05-29 22:21:39 -06003716dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003718 Py_VISIT(dv->dv_dict);
3719 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003720}
3721
Guido van Rossum83825ac2007-02-10 04:54:19 +00003722static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003723dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003724{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003725 Py_ssize_t len = 0;
3726 if (dv->dv_dict != NULL)
3727 len = dv->dv_dict->ma_used;
3728 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003729}
3730
Eric Snow96c6af92015-05-29 22:21:39 -06003731PyObject *
3732_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003733{
Eric Snow96c6af92015-05-29 22:21:39 -06003734 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003735 if (dict == NULL) {
3736 PyErr_BadInternalCall();
3737 return NULL;
3738 }
3739 if (!PyDict_Check(dict)) {
3740 /* XXX Get rid of this restriction later */
3741 PyErr_Format(PyExc_TypeError,
3742 "%s() requires a dict argument, not '%s'",
3743 type->tp_name, dict->ob_type->tp_name);
3744 return NULL;
3745 }
Eric Snow96c6af92015-05-29 22:21:39 -06003746 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003747 if (dv == NULL)
3748 return NULL;
3749 Py_INCREF(dict);
3750 dv->dv_dict = (PyDictObject *)dict;
3751 _PyObject_GC_TRACK(dv);
3752 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003753}
3754
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003755/* TODO(guido): The views objects are not complete:
3756
3757 * support more set operations
3758 * support arbitrary mappings?
3759 - either these should be static or exported in dictobject.h
3760 - if public then they should probably be in builtins
3761*/
3762
Guido van Rossumaac530c2007-08-24 22:33:45 +00003763/* Return 1 if self is a subset of other, iterating over self;
3764 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003765static int
3766all_contained_in(PyObject *self, PyObject *other)
3767{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003768 PyObject *iter = PyObject_GetIter(self);
3769 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003771 if (iter == NULL)
3772 return -1;
3773 for (;;) {
3774 PyObject *next = PyIter_Next(iter);
3775 if (next == NULL) {
3776 if (PyErr_Occurred())
3777 ok = -1;
3778 break;
3779 }
3780 ok = PySequence_Contains(other, next);
3781 Py_DECREF(next);
3782 if (ok <= 0)
3783 break;
3784 }
3785 Py_DECREF(iter);
3786 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003787}
3788
3789static PyObject *
3790dictview_richcompare(PyObject *self, PyObject *other, int op)
3791{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003792 Py_ssize_t len_self, len_other;
3793 int ok;
3794 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003796 assert(self != NULL);
3797 assert(PyDictViewSet_Check(self));
3798 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003799
Brian Curtindfc80e32011-08-10 20:28:54 -05003800 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3801 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003803 len_self = PyObject_Size(self);
3804 if (len_self < 0)
3805 return NULL;
3806 len_other = PyObject_Size(other);
3807 if (len_other < 0)
3808 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003810 ok = 0;
3811 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003813 case Py_NE:
3814 case Py_EQ:
3815 if (len_self == len_other)
3816 ok = all_contained_in(self, other);
3817 if (op == Py_NE && ok >= 0)
3818 ok = !ok;
3819 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003821 case Py_LT:
3822 if (len_self < len_other)
3823 ok = all_contained_in(self, other);
3824 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003826 case Py_LE:
3827 if (len_self <= len_other)
3828 ok = all_contained_in(self, other);
3829 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003830
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003831 case Py_GT:
3832 if (len_self > len_other)
3833 ok = all_contained_in(other, self);
3834 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003836 case Py_GE:
3837 if (len_self >= len_other)
3838 ok = all_contained_in(other, self);
3839 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003840
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003841 }
3842 if (ok < 0)
3843 return NULL;
3844 result = ok ? Py_True : Py_False;
3845 Py_INCREF(result);
3846 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003847}
3848
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003849static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003850dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003851{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003852 PyObject *seq;
bennorthd7773d92018-01-26 15:46:01 +00003853 PyObject *result = NULL;
3854 Py_ssize_t rc;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003855
bennorthd7773d92018-01-26 15:46:01 +00003856 rc = Py_ReprEnter((PyObject *)dv);
3857 if (rc != 0) {
3858 return rc > 0 ? PyUnicode_FromString("...") : NULL;
3859 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003860 seq = PySequence_List((PyObject *)dv);
bennorthd7773d92018-01-26 15:46:01 +00003861 if (seq == NULL) {
3862 goto Done;
3863 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003864 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
3865 Py_DECREF(seq);
bennorthd7773d92018-01-26 15:46:01 +00003866
3867Done:
3868 Py_ReprLeave((PyObject *)dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003869 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003870}
3871
Guido van Rossum3ac67412007-02-10 18:55:06 +00003872/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003873
3874static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003875dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003876{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003877 if (dv->dv_dict == NULL) {
3878 Py_RETURN_NONE;
3879 }
3880 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003881}
3882
3883static int
Eric Snow96c6af92015-05-29 22:21:39 -06003884dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00003885{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003886 if (dv->dv_dict == NULL)
3887 return 0;
3888 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003889}
3890
Guido van Rossum83825ac2007-02-10 04:54:19 +00003891static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003892 (lenfunc)dictview_len, /* sq_length */
3893 0, /* sq_concat */
3894 0, /* sq_repeat */
3895 0, /* sq_item */
3896 0, /* sq_slice */
3897 0, /* sq_ass_item */
3898 0, /* sq_ass_slice */
3899 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003900};
3901
Guido van Rossum523259b2007-08-24 23:41:22 +00003902static PyObject*
3903dictviews_sub(PyObject* self, PyObject *other)
3904{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003905 PyObject *result = PySet_New(self);
3906 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003907 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003909 if (result == NULL)
3910 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003911
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003912 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003913 if (tmp == NULL) {
3914 Py_DECREF(result);
3915 return NULL;
3916 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003918 Py_DECREF(tmp);
3919 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003920}
3921
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003922PyObject*
3923_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00003924{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003925 PyObject *result = PySet_New(self);
3926 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003927 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003929 if (result == NULL)
3930 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003931
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003932 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_intersection_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003933 if (tmp == NULL) {
3934 Py_DECREF(result);
3935 return NULL;
3936 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003938 Py_DECREF(tmp);
3939 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003940}
3941
3942static PyObject*
3943dictviews_or(PyObject* self, PyObject *other)
3944{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003945 PyObject *result = PySet_New(self);
3946 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003947 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003949 if (result == NULL)
3950 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003951
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003952 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003953 if (tmp == NULL) {
3954 Py_DECREF(result);
3955 return NULL;
3956 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003958 Py_DECREF(tmp);
3959 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003960}
3961
3962static PyObject*
3963dictviews_xor(PyObject* self, PyObject *other)
3964{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003965 PyObject *result = PySet_New(self);
3966 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003967 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003968
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003969 if (result == NULL)
3970 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003971
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003972 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_symmetric_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003973 if (tmp == NULL) {
3974 Py_DECREF(result);
3975 return NULL;
3976 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003978 Py_DECREF(tmp);
3979 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003980}
3981
3982static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003983 0, /*nb_add*/
3984 (binaryfunc)dictviews_sub, /*nb_subtract*/
3985 0, /*nb_multiply*/
3986 0, /*nb_remainder*/
3987 0, /*nb_divmod*/
3988 0, /*nb_power*/
3989 0, /*nb_negative*/
3990 0, /*nb_positive*/
3991 0, /*nb_absolute*/
3992 0, /*nb_bool*/
3993 0, /*nb_invert*/
3994 0, /*nb_lshift*/
3995 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003996 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003997 (binaryfunc)dictviews_xor, /*nb_xor*/
3998 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00003999};
4000
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004001static PyObject*
4002dictviews_isdisjoint(PyObject *self, PyObject *other)
4003{
4004 PyObject *it;
4005 PyObject *item = NULL;
4006
4007 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06004008 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004009 Py_RETURN_TRUE;
4010 else
4011 Py_RETURN_FALSE;
4012 }
4013
4014 /* Iterate over the shorter object (only if other is a set,
4015 * because PySequence_Contains may be expensive otherwise): */
4016 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06004017 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004018 Py_ssize_t len_other = PyObject_Size(other);
4019 if (len_other == -1)
4020 return NULL;
4021
4022 if ((len_other > len_self)) {
4023 PyObject *tmp = other;
4024 other = self;
4025 self = tmp;
4026 }
4027 }
4028
4029 it = PyObject_GetIter(other);
4030 if (it == NULL)
4031 return NULL;
4032
4033 while ((item = PyIter_Next(it)) != NULL) {
4034 int contains = PySequence_Contains(self, item);
4035 Py_DECREF(item);
4036 if (contains == -1) {
4037 Py_DECREF(it);
4038 return NULL;
4039 }
4040
4041 if (contains) {
4042 Py_DECREF(it);
4043 Py_RETURN_FALSE;
4044 }
4045 }
4046 Py_DECREF(it);
4047 if (PyErr_Occurred())
4048 return NULL; /* PyIter_Next raised an exception. */
4049 Py_RETURN_TRUE;
4050}
4051
4052PyDoc_STRVAR(isdisjoint_doc,
4053"Return True if the view and the given iterable have a null intersection.");
4054
Guido van Rossumb90c8482007-02-10 01:11:45 +00004055static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004056 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4057 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004058 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004059};
4060
4061PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004062 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4063 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004064 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004065 0, /* tp_itemsize */
4066 /* methods */
4067 (destructor)dictview_dealloc, /* tp_dealloc */
4068 0, /* tp_print */
4069 0, /* tp_getattr */
4070 0, /* tp_setattr */
4071 0, /* tp_reserved */
4072 (reprfunc)dictview_repr, /* tp_repr */
4073 &dictviews_as_number, /* tp_as_number */
4074 &dictkeys_as_sequence, /* tp_as_sequence */
4075 0, /* tp_as_mapping */
4076 0, /* tp_hash */
4077 0, /* tp_call */
4078 0, /* tp_str */
4079 PyObject_GenericGetAttr, /* tp_getattro */
4080 0, /* tp_setattro */
4081 0, /* tp_as_buffer */
4082 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4083 0, /* tp_doc */
4084 (traverseproc)dictview_traverse, /* tp_traverse */
4085 0, /* tp_clear */
4086 dictview_richcompare, /* tp_richcompare */
4087 0, /* tp_weaklistoffset */
4088 (getiterfunc)dictkeys_iter, /* tp_iter */
4089 0, /* tp_iternext */
4090 dictkeys_methods, /* tp_methods */
4091 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004092};
4093
4094static PyObject *
4095dictkeys_new(PyObject *dict)
4096{
Eric Snow96c6af92015-05-29 22:21:39 -06004097 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004098}
4099
Guido van Rossum3ac67412007-02-10 18:55:06 +00004100/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004101
4102static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004103dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004104{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004105 if (dv->dv_dict == NULL) {
4106 Py_RETURN_NONE;
4107 }
4108 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004109}
4110
4111static int
Eric Snow96c6af92015-05-29 22:21:39 -06004112dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004113{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004114 int result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004115 PyObject *key, *value, *found;
4116 if (dv->dv_dict == NULL)
4117 return 0;
4118 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
4119 return 0;
4120 key = PyTuple_GET_ITEM(obj, 0);
4121 value = PyTuple_GET_ITEM(obj, 1);
Raymond Hettinger6692f012016-09-18 21:46:08 -07004122 found = PyDict_GetItemWithError((PyObject *)dv->dv_dict, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004123 if (found == NULL) {
4124 if (PyErr_Occurred())
4125 return -1;
4126 return 0;
4127 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004128 Py_INCREF(found);
4129 result = PyObject_RichCompareBool(value, found, Py_EQ);
4130 Py_DECREF(found);
4131 return result;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004132}
4133
Guido van Rossum83825ac2007-02-10 04:54:19 +00004134static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004135 (lenfunc)dictview_len, /* sq_length */
4136 0, /* sq_concat */
4137 0, /* sq_repeat */
4138 0, /* sq_item */
4139 0, /* sq_slice */
4140 0, /* sq_ass_item */
4141 0, /* sq_ass_slice */
4142 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004143};
4144
Guido van Rossumb90c8482007-02-10 01:11:45 +00004145static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004146 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4147 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004148 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004149};
4150
4151PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004152 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4153 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004154 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004155 0, /* tp_itemsize */
4156 /* methods */
4157 (destructor)dictview_dealloc, /* tp_dealloc */
4158 0, /* tp_print */
4159 0, /* tp_getattr */
4160 0, /* tp_setattr */
4161 0, /* tp_reserved */
4162 (reprfunc)dictview_repr, /* tp_repr */
4163 &dictviews_as_number, /* tp_as_number */
4164 &dictitems_as_sequence, /* tp_as_sequence */
4165 0, /* tp_as_mapping */
4166 0, /* tp_hash */
4167 0, /* tp_call */
4168 0, /* tp_str */
4169 PyObject_GenericGetAttr, /* tp_getattro */
4170 0, /* tp_setattro */
4171 0, /* tp_as_buffer */
4172 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4173 0, /* tp_doc */
4174 (traverseproc)dictview_traverse, /* tp_traverse */
4175 0, /* tp_clear */
4176 dictview_richcompare, /* tp_richcompare */
4177 0, /* tp_weaklistoffset */
4178 (getiterfunc)dictitems_iter, /* tp_iter */
4179 0, /* tp_iternext */
4180 dictitems_methods, /* tp_methods */
4181 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004182};
4183
4184static PyObject *
4185dictitems_new(PyObject *dict)
4186{
Eric Snow96c6af92015-05-29 22:21:39 -06004187 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004188}
4189
Guido van Rossum3ac67412007-02-10 18:55:06 +00004190/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004191
4192static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004193dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004194{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004195 if (dv->dv_dict == NULL) {
4196 Py_RETURN_NONE;
4197 }
4198 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004199}
4200
Guido van Rossum83825ac2007-02-10 04:54:19 +00004201static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004202 (lenfunc)dictview_len, /* sq_length */
4203 0, /* sq_concat */
4204 0, /* sq_repeat */
4205 0, /* sq_item */
4206 0, /* sq_slice */
4207 0, /* sq_ass_item */
4208 0, /* sq_ass_slice */
4209 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004210};
4211
Guido van Rossumb90c8482007-02-10 01:11:45 +00004212static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004213 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004214};
4215
4216PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004217 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4218 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004219 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004220 0, /* tp_itemsize */
4221 /* methods */
4222 (destructor)dictview_dealloc, /* tp_dealloc */
4223 0, /* tp_print */
4224 0, /* tp_getattr */
4225 0, /* tp_setattr */
4226 0, /* tp_reserved */
4227 (reprfunc)dictview_repr, /* tp_repr */
4228 0, /* tp_as_number */
4229 &dictvalues_as_sequence, /* tp_as_sequence */
4230 0, /* tp_as_mapping */
4231 0, /* tp_hash */
4232 0, /* tp_call */
4233 0, /* tp_str */
4234 PyObject_GenericGetAttr, /* tp_getattro */
4235 0, /* tp_setattro */
4236 0, /* tp_as_buffer */
4237 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4238 0, /* tp_doc */
4239 (traverseproc)dictview_traverse, /* tp_traverse */
4240 0, /* tp_clear */
4241 0, /* tp_richcompare */
4242 0, /* tp_weaklistoffset */
4243 (getiterfunc)dictvalues_iter, /* tp_iter */
4244 0, /* tp_iternext */
4245 dictvalues_methods, /* tp_methods */
4246 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004247};
4248
4249static PyObject *
4250dictvalues_new(PyObject *dict)
4251{
Eric Snow96c6af92015-05-29 22:21:39 -06004252 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004253}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004254
4255/* Returns NULL if cannot allocate a new PyDictKeysObject,
4256 but does not set an error */
4257PyDictKeysObject *
4258_PyDict_NewKeysForClass(void)
4259{
Victor Stinner742da042016-09-07 17:40:12 -07004260 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004261 if (keys == NULL)
4262 PyErr_Clear();
4263 else
4264 keys->dk_lookup = lookdict_split;
4265 return keys;
4266}
4267
4268#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
4269
4270PyObject *
4271PyObject_GenericGetDict(PyObject *obj, void *context)
4272{
4273 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
4274 if (dictptr == NULL) {
4275 PyErr_SetString(PyExc_AttributeError,
4276 "This object has no __dict__");
4277 return NULL;
4278 }
4279 dict = *dictptr;
4280 if (dict == NULL) {
4281 PyTypeObject *tp = Py_TYPE(obj);
4282 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
4283 DK_INCREF(CACHED_KEYS(tp));
4284 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
4285 }
4286 else {
4287 *dictptr = dict = PyDict_New();
4288 }
4289 }
4290 Py_XINCREF(dict);
4291 return dict;
4292}
4293
4294int
4295_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
Victor Stinner742da042016-09-07 17:40:12 -07004296 PyObject *key, PyObject *value)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004297{
4298 PyObject *dict;
4299 int res;
4300 PyDictKeysObject *cached;
4301
4302 assert(dictptr != NULL);
4303 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
4304 assert(dictptr != NULL);
4305 dict = *dictptr;
4306 if (dict == NULL) {
4307 DK_INCREF(cached);
4308 dict = new_dict_with_shared_keys(cached);
4309 if (dict == NULL)
4310 return -1;
4311 *dictptr = dict;
4312 }
4313 if (value == NULL) {
4314 res = PyDict_DelItem(dict, key);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004315 // Since key sharing dict doesn't allow deletion, PyDict_DelItem()
4316 // always converts dict to combined form.
4317 if ((cached = CACHED_KEYS(tp)) != NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004318 CACHED_KEYS(tp) = NULL;
4319 DK_DECREF(cached);
4320 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01004321 }
4322 else {
INADA Naoki2294f3a2017-02-12 13:51:30 +09004323 int was_shared = (cached == ((PyDictObject *)dict)->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004324 res = PyDict_SetItem(dict, key, value);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004325 if (was_shared &&
4326 (cached = CACHED_KEYS(tp)) != NULL &&
4327 cached != ((PyDictObject *)dict)->ma_keys) {
Victor Stinner3d3f2642016-12-15 17:21:23 +01004328 /* PyDict_SetItem() may call dictresize and convert split table
4329 * into combined table. In such case, convert it to split
4330 * table again and update type's shared key only when this is
4331 * the only dict sharing key with the type.
4332 *
4333 * This is to allow using shared key in class like this:
4334 *
4335 * class C:
4336 * def __init__(self):
4337 * # one dict resize happens
4338 * self.a, self.b, self.c = 1, 2, 3
4339 * self.d, self.e, self.f = 4, 5, 6
4340 * a = C()
4341 */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004342 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004343 CACHED_KEYS(tp) = make_keys_shared(dict);
Victor Stinner742da042016-09-07 17:40:12 -07004344 }
4345 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004346 CACHED_KEYS(tp) = NULL;
4347 }
4348 DK_DECREF(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004349 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
4350 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004351 }
4352 }
4353 } else {
4354 dict = *dictptr;
4355 if (dict == NULL) {
4356 dict = PyDict_New();
4357 if (dict == NULL)
4358 return -1;
4359 *dictptr = dict;
4360 }
4361 if (value == NULL) {
4362 res = PyDict_DelItem(dict, key);
4363 } else {
4364 res = PyDict_SetItem(dict, key, value);
4365 }
4366 }
4367 return res;
4368}
4369
4370void
4371_PyDictKeys_DecRef(PyDictKeysObject *keys)
4372{
4373 DK_DECREF(keys);
4374}