blob: 6ba2cc975fcb7eae1b2b09bcf21b83f1b9f96d86 [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) \
Benjamin Peterson186122e2016-09-08 12:20:12 -0700301 ((PyDictKeyEntry*)(&(dk)->dk_indices.as_1[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) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700319 int8_t *indices = keys->dk_indices.as_1;
Victor Stinner208857e2016-09-08 11:35:46 -0700320 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700321 }
322 else if (s <= 0xffff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700323 int16_t *indices = keys->dk_indices.as_2;
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) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700328 int64_t *indices = keys->dk_indices.as_8;
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 {
333 int32_t *indices = keys->dk_indices.as_4;
334 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) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700349 int8_t *indices = keys->dk_indices.as_1;
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) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700354 int16_t *indices = keys->dk_indices.as_2;
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) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700360 int64_t *indices = keys->dk_indices.as_8;
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 {
365 int32_t *indices = keys->dk_indices.as_4;
366 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.
399 * Currently set to used*2 + capacity/2.
400 * 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
402 * number of insertions.
403 * Raising this to used*4 doubles memory consumption depending on the size of
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200404 * the dictionary, but results in half the number of resizes, less effort to
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700405 * resize.
406 * GROWTH_RATE was set to used*4 up to version 3.2.
407 * GROWTH_RATE was set to used*2 in version 3.3.0
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200408 */
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700409#define GROWTH_RATE(d) (((d)->ma_used*2)+((d)->ma_keys->dk_size>>1))
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400410
411#define ENSURE_ALLOWS_DELETIONS(d) \
412 if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \
413 (d)->ma_keys->dk_lookup = lookdict_unicode; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400415
416/* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
417 * (which cannot fail and thus can do no allocation).
418 */
419static PyDictKeysObject empty_keys_struct = {
Serhiy Storchaka97932e42016-09-26 23:01:23 +0300420 1, /* dk_refcnt */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400421 1, /* dk_size */
422 lookdict_split, /* dk_lookup */
423 0, /* dk_usable (immutable) */
Victor Stinner742da042016-09-07 17:40:12 -0700424 0, /* dk_nentries */
Benjamin Peterson186122e2016-09-08 12:20:12 -0700425 .dk_indices = { .as_1 = {DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY,
426 DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY}},
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400427};
428
429static PyObject *empty_values[1] = { NULL };
430
431#define Py_EMPTY_KEYS &empty_keys_struct
432
Victor Stinner611b0fa2016-09-14 15:02:01 +0200433/* Uncomment to check the dict content in _PyDict_CheckConsistency() */
434/* #define DEBUG_PYDICT */
435
436
T. Woutersa00c3fd2017-03-31 09:14:41 -0700437#ifndef NDEBUG
Victor Stinner611b0fa2016-09-14 15:02:01 +0200438static int
439_PyDict_CheckConsistency(PyDictObject *mp)
440{
441 PyDictKeysObject *keys = mp->ma_keys;
442 int splitted = _PyDict_HasSplitTable(mp);
443 Py_ssize_t usable = USABLE_FRACTION(keys->dk_size);
444#ifdef DEBUG_PYDICT
445 PyDictKeyEntry *entries = DK_ENTRIES(keys);
446 Py_ssize_t i;
447#endif
448
449 assert(0 <= mp->ma_used && mp->ma_used <= usable);
450 assert(IS_POWER_OF_2(keys->dk_size));
451 assert(0 <= keys->dk_usable
452 && keys->dk_usable <= usable);
453 assert(0 <= keys->dk_nentries
454 && keys->dk_nentries <= usable);
455 assert(keys->dk_usable + keys->dk_nentries <= usable);
456
457 if (!splitted) {
458 /* combined table */
459 assert(keys->dk_refcnt == 1);
460 }
461
462#ifdef DEBUG_PYDICT
463 for (i=0; i < keys->dk_size; i++) {
464 Py_ssize_t ix = dk_get_index(keys, i);
465 assert(DKIX_DUMMY <= ix && ix <= usable);
466 }
467
468 for (i=0; i < usable; i++) {
469 PyDictKeyEntry *entry = &entries[i];
470 PyObject *key = entry->me_key;
471
472 if (key != NULL) {
473 if (PyUnicode_CheckExact(key)) {
474 Py_hash_t hash = ((PyASCIIObject *)key)->hash;
475 assert(hash != -1);
476 assert(entry->me_hash == hash);
477 }
478 else {
479 /* test_dict fails if PyObject_Hash() is called again */
480 assert(entry->me_hash != -1);
481 }
482 if (!splitted) {
483 assert(entry->me_value != NULL);
484 }
485 }
486
487 if (splitted) {
488 assert(entry->me_value == NULL);
489 }
490 }
491
492 if (splitted) {
493 /* splitted table */
494 for (i=0; i < mp->ma_used; i++) {
495 assert(mp->ma_values[i] != NULL);
496 }
497 }
498#endif
499
500 return 1;
501}
502#endif
503
504
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400505static PyDictKeysObject *new_keys_object(Py_ssize_t size)
506{
507 PyDictKeysObject *dk;
Victor Stinner742da042016-09-07 17:40:12 -0700508 Py_ssize_t es, usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400509
Victor Stinner742da042016-09-07 17:40:12 -0700510 assert(size >= PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400511 assert(IS_POWER_OF_2(size));
Victor Stinner742da042016-09-07 17:40:12 -0700512
513 usable = USABLE_FRACTION(size);
514 if (size <= 0xff) {
515 es = 1;
516 }
517 else if (size <= 0xffff) {
518 es = 2;
519 }
520#if SIZEOF_VOID_P > 4
521 else if (size <= 0xffffffff) {
522 es = 4;
523 }
524#endif
525 else {
526 es = sizeof(Py_ssize_t);
527 }
528
529 if (size == PyDict_MINSIZE && numfreekeys > 0) {
530 dk = keys_free_list[--numfreekeys];
531 }
532 else {
Victor Stinner98ee9d52016-09-08 09:33:56 -0700533 dk = PyObject_MALLOC(sizeof(PyDictKeysObject)
534 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
535 + es * size
536 + sizeof(PyDictKeyEntry) * usable);
Victor Stinner742da042016-09-07 17:40:12 -0700537 if (dk == NULL) {
538 PyErr_NoMemory();
539 return NULL;
540 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400541 }
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200542 DK_DEBUG_INCREF dk->dk_refcnt = 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400543 dk->dk_size = size;
Victor Stinner742da042016-09-07 17:40:12 -0700544 dk->dk_usable = usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400545 dk->dk_lookup = lookdict_unicode_nodummy;
Victor Stinner742da042016-09-07 17:40:12 -0700546 dk->dk_nentries = 0;
Benjamin Peterson186122e2016-09-08 12:20:12 -0700547 memset(&dk->dk_indices.as_1[0], 0xff, es * size);
Victor Stinner742da042016-09-07 17:40:12 -0700548 memset(DK_ENTRIES(dk), 0, sizeof(PyDictKeyEntry) * usable);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400549 return dk;
550}
551
552static void
553free_keys_object(PyDictKeysObject *keys)
554{
Victor Stinner742da042016-09-07 17:40:12 -0700555 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400556 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -0700557 for (i = 0, n = keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400558 Py_XDECREF(entries[i].me_key);
559 Py_XDECREF(entries[i].me_value);
560 }
Victor Stinner742da042016-09-07 17:40:12 -0700561 if (keys->dk_size == PyDict_MINSIZE && numfreekeys < PyDict_MAXFREELIST) {
562 keys_free_list[numfreekeys++] = keys;
563 return;
564 }
Raymond Hettingerce5179f2016-01-31 08:56:21 -0800565 PyObject_FREE(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400566}
567
568#define new_values(size) PyMem_NEW(PyObject *, size)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400569#define free_values(values) PyMem_FREE(values)
570
571/* Consumes a reference to the keys object */
572static PyObject *
573new_dict(PyDictKeysObject *keys, PyObject **values)
574{
575 PyDictObject *mp;
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200576 assert(keys != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 if (numfree) {
578 mp = free_list[--numfree];
579 assert (mp != NULL);
580 assert (Py_TYPE(mp) == &PyDict_Type);
581 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400583 else {
584 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
585 if (mp == NULL) {
586 DK_DECREF(keys);
587 free_values(values);
588 return NULL;
589 }
590 }
591 mp->ma_keys = keys;
592 mp->ma_values = values;
593 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700594 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +0200595 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000597}
598
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400599/* Consumes a reference to the keys object */
600static PyObject *
601new_dict_with_shared_keys(PyDictKeysObject *keys)
602{
603 PyObject **values;
604 Py_ssize_t i, size;
605
Victor Stinner742da042016-09-07 17:40:12 -0700606 size = USABLE_FRACTION(DK_SIZE(keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400607 values = new_values(size);
608 if (values == NULL) {
609 DK_DECREF(keys);
610 return PyErr_NoMemory();
611 }
612 for (i = 0; i < size; i++) {
613 values[i] = NULL;
614 }
615 return new_dict(keys, values);
616}
617
618PyObject *
619PyDict_New(void)
620{
Victor Stinner742da042016-09-07 17:40:12 -0700621 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200622 if (keys == NULL)
623 return NULL;
624 return new_dict(keys, NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400625}
626
Victor Stinner742da042016-09-07 17:40:12 -0700627/* Search index of hash table from offset of entry table */
628static Py_ssize_t
629lookdict_index(PyDictKeysObject *k, Py_hash_t hash, Py_ssize_t index)
630{
Victor Stinner742da042016-09-07 17:40:12 -0700631 size_t mask = DK_MASK(k);
INADA Naoki073ae482017-06-23 15:22:50 +0900632 size_t perturb = (size_t)hash;
633 size_t i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700634
INADA Naoki073ae482017-06-23 15:22:50 +0900635 for (;;) {
636 Py_ssize_t ix = dk_get_index(k, i);
Victor Stinner742da042016-09-07 17:40:12 -0700637 if (ix == index) {
638 return i;
639 }
640 if (ix == DKIX_EMPTY) {
641 return DKIX_EMPTY;
642 }
INADA Naoki073ae482017-06-23 15:22:50 +0900643 perturb >>= PERTURB_SHIFT;
644 i = mask & (i*5 + perturb + 1);
Victor Stinner742da042016-09-07 17:40:12 -0700645 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700646 Py_UNREACHABLE();
Victor Stinner742da042016-09-07 17:40:12 -0700647}
648
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000649/*
650The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000651This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000652Open addressing is preferred over chaining since the link overhead for
653chaining would be substantial (100% with typical malloc overhead).
654
Tim Peterseb28ef22001-06-02 05:27:19 +0000655The initial probe index is computed as hash mod the table size. Subsequent
656probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000657
658All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000659
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000660The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000661contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000662Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000663
Victor Stinner742da042016-09-07 17:40:12 -0700664lookdict() is general-purpose, and may return DKIX_ERROR if (and only if) a
Victor Stinnera4348cc2016-09-08 12:01:25 -0700665comparison raises an exception.
Guido van Rossum89d8c602007-09-18 17:26:56 +0000666lookdict_unicode() below is specialized to string keys, comparison of which can
INADA Naoki1b8df102017-02-20 22:48:10 +0900667never raise an exception; that function can never return DKIX_ERROR when key
668is string. Otherwise, it falls back to lookdict().
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400669lookdict_unicode_nodummy is further specialized for string keys that cannot be
670the <dummy> value.
INADA Naoki778928b2017-08-03 23:45:15 +0900671For both, when the key isn't found a DKIX_EMPTY is returned.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000672*/
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100673static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400674lookdict(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900675 Py_hash_t hash, PyObject **value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000676{
INADA Naoki778928b2017-08-03 23:45:15 +0900677 size_t i, mask, perturb;
Victor Stinner742da042016-09-07 17:40:12 -0700678 PyDictKeysObject *dk;
INADA Naoki778928b2017-08-03 23:45:15 +0900679 PyDictKeyEntry *ep0;
Tim Peterseb28ef22001-06-02 05:27:19 +0000680
Antoine Pitrou9a234902012-05-13 20:48:01 +0200681top:
Victor Stinner742da042016-09-07 17:40:12 -0700682 dk = mp->ma_keys;
Victor Stinner742da042016-09-07 17:40:12 -0700683 ep0 = DK_ENTRIES(dk);
INADA Naoki778928b2017-08-03 23:45:15 +0900684 mask = DK_MASK(dk);
685 perturb = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700687
INADA Naoki778928b2017-08-03 23:45:15 +0900688 for (;;) {
689 Py_ssize_t ix = dk_get_index(dk, i);
Victor Stinner742da042016-09-07 17:40:12 -0700690 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700691 *value_addr = NULL;
692 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400693 }
INADA Naoki778928b2017-08-03 23:45:15 +0900694 if (ix >= 0) {
695 PyDictKeyEntry *ep = &ep0[ix];
696 assert(ep->me_key != NULL);
697 if (ep->me_key == key) {
698 *value_addr = ep->me_value;
699 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700700 }
INADA Naoki778928b2017-08-03 23:45:15 +0900701 if (ep->me_hash == hash) {
702 PyObject *startkey = ep->me_key;
703 Py_INCREF(startkey);
704 int cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
705 Py_DECREF(startkey);
706 if (cmp < 0) {
707 *value_addr = NULL;
708 return DKIX_ERROR;
709 }
710 if (dk == mp->ma_keys && ep->me_key == startkey) {
711 if (cmp > 0) {
712 *value_addr = ep->me_value;
713 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700714 }
INADA Naoki778928b2017-08-03 23:45:15 +0900715 }
716 else {
717 /* The dict was mutated, restart */
718 goto top;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400719 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 }
INADA Naoki778928b2017-08-03 23:45:15 +0900722 perturb >>= PERTURB_SHIFT;
723 i = (i*5 + perturb + 1) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700725 Py_UNREACHABLE();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000726}
727
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400728/* Specialized version for string-only keys */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100729static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400730lookdict_unicode(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900731 Py_hash_t hash, PyObject **value_addr)
Fred Drake1bff34a2000-08-31 19:31:38 +0000732{
Victor Stinner742da042016-09-07 17:40:12 -0700733 assert(mp->ma_values == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 /* Make sure this function doesn't have to handle non-unicode keys,
735 including subclasses of str; e.g., one reason to subclass
736 unicodes is to override __eq__, and for speed we don't cater to
737 that here. */
738 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400739 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900740 return lookdict(mp, key, hash, value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 }
Tim Peters15d49292001-05-27 07:39:22 +0000742
INADA Naoki778928b2017-08-03 23:45:15 +0900743 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
744 size_t mask = DK_MASK(mp->ma_keys);
745 size_t perturb = (size_t)hash;
746 size_t i = (size_t)hash & mask;
747
748 for (;;) {
749 Py_ssize_t ix = dk_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700750 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700751 *value_addr = NULL;
752 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400753 }
INADA Naoki778928b2017-08-03 23:45:15 +0900754 if (ix >= 0) {
755 PyDictKeyEntry *ep = &ep0[ix];
756 assert(ep->me_key != NULL);
757 assert(PyUnicode_CheckExact(ep->me_key));
758 if (ep->me_key == key ||
759 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
760 *value_addr = ep->me_value;
761 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700762 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400763 }
INADA Naoki778928b2017-08-03 23:45:15 +0900764 perturb >>= PERTURB_SHIFT;
765 i = mask & (i*5 + perturb + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700767 Py_UNREACHABLE();
Fred Drake1bff34a2000-08-31 19:31:38 +0000768}
769
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400770/* Faster version of lookdict_unicode when it is known that no <dummy> keys
771 * will be present. */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100772static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400773lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900774 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400775{
Victor Stinner742da042016-09-07 17:40:12 -0700776 assert(mp->ma_values == NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400777 /* Make sure this function doesn't have to handle non-unicode keys,
778 including subclasses of str; e.g., one reason to subclass
779 unicodes is to override __eq__, and for speed we don't cater to
780 that here. */
781 if (!PyUnicode_CheckExact(key)) {
782 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900783 return lookdict(mp, key, hash, value_addr);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400784 }
INADA Naoki778928b2017-08-03 23:45:15 +0900785
786 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
787 size_t mask = DK_MASK(mp->ma_keys);
788 size_t perturb = (size_t)hash;
789 size_t i = (size_t)hash & mask;
790
791 for (;;) {
792 Py_ssize_t ix = dk_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700793 assert (ix != DKIX_DUMMY);
794 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700795 *value_addr = NULL;
796 return DKIX_EMPTY;
797 }
INADA Naoki778928b2017-08-03 23:45:15 +0900798 PyDictKeyEntry *ep = &ep0[ix];
799 assert(ep->me_key != NULL);
800 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700801 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400802 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900803 *value_addr = ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700804 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400805 }
INADA Naoki778928b2017-08-03 23:45:15 +0900806 perturb >>= PERTURB_SHIFT;
807 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400808 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700809 Py_UNREACHABLE();
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400810}
811
812/* Version of lookdict for split tables.
813 * All split tables and only split tables use this lookup function.
814 * Split tables only contain unicode keys and no dummy keys,
815 * so algorithm is the same as lookdict_unicode_nodummy.
816 */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100817static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400818lookdict_split(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900819 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400820{
Victor Stinner742da042016-09-07 17:40:12 -0700821 /* mp must split table */
822 assert(mp->ma_values != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400823 if (!PyUnicode_CheckExact(key)) {
INADA Naoki778928b2017-08-03 23:45:15 +0900824 Py_ssize_t ix = lookdict(mp, key, hash, value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700825 if (ix >= 0) {
INADA Naokiba609772016-12-07 20:41:42 +0900826 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700827 }
828 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400829 }
Victor Stinner742da042016-09-07 17:40:12 -0700830
INADA Naoki778928b2017-08-03 23:45:15 +0900831 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
832 size_t mask = DK_MASK(mp->ma_keys);
833 size_t perturb = (size_t)hash;
834 size_t i = (size_t)hash & mask;
835
836 for (;;) {
837 Py_ssize_t ix = dk_get_index(mp->ma_keys, i);
838 assert (ix != DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -0700839 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700840 *value_addr = NULL;
841 return DKIX_EMPTY;
842 }
INADA Naoki778928b2017-08-03 23:45:15 +0900843 PyDictKeyEntry *ep = &ep0[ix];
844 assert(ep->me_key != NULL);
845 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700846 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400847 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900848 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700849 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400850 }
INADA Naoki778928b2017-08-03 23:45:15 +0900851 perturb >>= PERTURB_SHIFT;
852 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400853 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700854 Py_UNREACHABLE();
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400855}
856
Benjamin Petersonfb886362010-04-24 18:21:17 +0000857int
858_PyDict_HasOnlyStringKeys(PyObject *dict)
859{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 Py_ssize_t pos = 0;
861 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000862 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400864 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 return 1;
866 while (PyDict_Next(dict, &pos, &key, &value))
867 if (!PyUnicode_Check(key))
868 return 0;
869 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000870}
871
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000872#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 do { \
874 if (!_PyObject_GC_IS_TRACKED(mp)) { \
875 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
876 _PyObject_GC_MAY_BE_TRACKED(value)) { \
877 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 } \
879 } \
880 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000881
882void
883_PyDict_MaybeUntrack(PyObject *op)
884{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 PyDictObject *mp;
886 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -0700887 Py_ssize_t i, numentries;
888 PyDictKeyEntry *ep0;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
891 return;
892
893 mp = (PyDictObject *) op;
Victor Stinner742da042016-09-07 17:40:12 -0700894 ep0 = DK_ENTRIES(mp->ma_keys);
895 numentries = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400896 if (_PyDict_HasSplitTable(mp)) {
Victor Stinner742da042016-09-07 17:40:12 -0700897 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400898 if ((value = mp->ma_values[i]) == NULL)
899 continue;
900 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
Victor Stinner742da042016-09-07 17:40:12 -0700901 assert(!_PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400902 return;
903 }
904 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400906 else {
Victor Stinner742da042016-09-07 17:40:12 -0700907 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400908 if ((value = ep0[i].me_value) == NULL)
909 continue;
910 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
911 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
912 return;
913 }
914 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000916}
917
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400918/* Internal function to find slot for an item from its hash
Victor Stinner3c336c52016-09-12 14:17:40 +0200919 when it is known that the key is not present in the dict.
920
921 The dict must be combined. */
INADA Naokiba609772016-12-07 20:41:42 +0900922static Py_ssize_t
INADA Naoki778928b2017-08-03 23:45:15 +0900923find_empty_slot(PyDictKeysObject *keys, Py_hash_t hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000924{
INADA Naoki778928b2017-08-03 23:45:15 +0900925 assert(keys != NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000926
INADA Naoki778928b2017-08-03 23:45:15 +0900927 const size_t mask = DK_MASK(keys);
928 size_t i = hash & mask;
929 Py_ssize_t ix = dk_get_index(keys, i);
930 for (size_t perturb = hash; ix >= 0;) {
INADA Naoki267941c2016-10-06 15:19:07 +0900931 perturb >>= PERTURB_SHIFT;
INADA Naoki778928b2017-08-03 23:45:15 +0900932 i = (i*5 + perturb + 1) & mask;
933 ix = dk_get_index(keys, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 }
INADA Naoki778928b2017-08-03 23:45:15 +0900935 return i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000936}
937
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400938static int
939insertion_resize(PyDictObject *mp)
940{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700941 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400942}
Antoine Pitroue965d972012-02-27 00:45:12 +0100943
944/*
945Internal routine to insert a new item into the table.
946Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +0100947Returns -1 if an error occurred, or 0 on success.
948*/
949static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400950insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +0100951{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400952 PyObject *old_value;
INADA Naokiba609772016-12-07 20:41:42 +0900953 PyDictKeyEntry *ep;
Antoine Pitroue965d972012-02-27 00:45:12 +0100954
Serhiy Storchaka753bca32017-05-20 12:30:02 +0300955 Py_INCREF(key);
956 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400957 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
958 if (insertion_resize(mp) < 0)
Serhiy Storchaka753bca32017-05-20 12:30:02 +0300959 goto Fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400960 }
961
INADA Naoki778928b2017-08-03 23:45:15 +0900962 Py_ssize_t ix = mp->ma_keys->dk_lookup(mp, key, hash, &old_value);
Serhiy Storchaka753bca32017-05-20 12:30:02 +0300963 if (ix == DKIX_ERROR)
964 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -0700965
Antoine Pitroud6967322014-10-18 00:35:00 +0200966 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400967 MAINTAIN_TRACKING(mp, key, value);
Victor Stinner742da042016-09-07 17:40:12 -0700968
969 /* When insertion order is different from shared key, we can't share
970 * the key anymore. Convert this instance to combine table.
971 */
972 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +0900973 ((ix >= 0 && old_value == NULL && mp->ma_used != ix) ||
Victor Stinner742da042016-09-07 17:40:12 -0700974 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +0300975 if (insertion_resize(mp) < 0)
976 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -0700977 ix = DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400978 }
Victor Stinner742da042016-09-07 17:40:12 -0700979
980 if (ix == DKIX_EMPTY) {
981 /* Insert into new slot. */
INADA Naokiba609772016-12-07 20:41:42 +0900982 assert(old_value == NULL);
Victor Stinner742da042016-09-07 17:40:12 -0700983 if (mp->ma_keys->dk_usable <= 0) {
984 /* Need to resize. */
Serhiy Storchaka753bca32017-05-20 12:30:02 +0300985 if (insertion_resize(mp) < 0)
986 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -0700987 }
INADA Naoki778928b2017-08-03 23:45:15 +0900988 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naokiba609772016-12-07 20:41:42 +0900989 ep = &DK_ENTRIES(mp->ma_keys)[mp->ma_keys->dk_nentries];
Victor Stinner742da042016-09-07 17:40:12 -0700990 dk_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Victor Stinner742da042016-09-07 17:40:12 -0700991 ep->me_key = key;
992 ep->me_hash = hash;
993 if (mp->ma_values) {
994 assert (mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
995 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400996 }
997 else {
Victor Stinner742da042016-09-07 17:40:12 -0700998 ep->me_value = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400999 }
1000 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001001 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07001002 mp->ma_keys->dk_usable--;
1003 mp->ma_keys->dk_nentries++;
1004 assert(mp->ma_keys->dk_usable >= 0);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001005 assert(_PyDict_CheckConsistency(mp));
Victor Stinner742da042016-09-07 17:40:12 -07001006 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001007 }
Victor Stinner742da042016-09-07 17:40:12 -07001008
INADA Naokiba609772016-12-07 20:41:42 +09001009 if (_PyDict_HasSplitTable(mp)) {
1010 mp->ma_values[ix] = value;
1011 if (old_value == NULL) {
1012 /* pending state */
1013 assert(ix == mp->ma_used);
1014 mp->ma_used++;
1015 }
1016 }
1017 else {
1018 assert(old_value != NULL);
1019 DK_ENTRIES(mp->ma_keys)[ix].me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07001020 }
1021
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001022 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokiba609772016-12-07 20:41:42 +09001023 Py_XDECREF(old_value); /* which **CAN** re-enter (see issue #22653) */
Victor Stinner611b0fa2016-09-14 15:02:01 +02001024 assert(_PyDict_CheckConsistency(mp));
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001025 Py_DECREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001026 return 0;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001027
1028Fail:
1029 Py_DECREF(value);
1030 Py_DECREF(key);
1031 return -1;
Antoine Pitroue965d972012-02-27 00:45:12 +01001032}
1033
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001034/*
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001035Internal routine used by dictresize() to buid a hashtable of entries.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001036*/
1037static void
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001038build_indices(PyDictKeysObject *keys, PyDictKeyEntry *ep, Py_ssize_t n)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001039{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001040 size_t mask = (size_t)DK_SIZE(keys) - 1;
1041 for (Py_ssize_t ix = 0; ix != n; ix++, ep++) {
1042 Py_hash_t hash = ep->me_hash;
1043 size_t i = hash & mask;
1044 for (size_t perturb = hash; dk_get_index(keys, i) != DKIX_EMPTY;) {
1045 perturb >>= PERTURB_SHIFT;
INADA Naoki870c2862017-06-24 09:03:19 +09001046 i = mask & (i*5 + perturb + 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001047 }
1048 dk_set_index(keys, i, ix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001050}
1051
1052/*
1053Restructure the table by allocating a new table and reinserting all
1054items again. When entries have been deleted, the new table may
1055actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001056If a table is split (its keys and hashes are shared, its values are not),
1057then the values are temporarily copied into the table, it is resized as
1058a combined table, then the me_value slots in the old table are NULLed out.
1059After resizing a table is always combined,
1060but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001061*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001062static int
Victor Stinner3d3f2642016-12-15 17:21:23 +01001063dictresize(PyDictObject *mp, Py_ssize_t minsize)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001064{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001065 Py_ssize_t newsize, numentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001066 PyDictKeysObject *oldkeys;
1067 PyObject **oldvalues;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001068 PyDictKeyEntry *oldentries, *newentries;
Tim Peters91a364d2001-05-19 07:04:38 +00001069
Victor Stinner742da042016-09-07 17:40:12 -07001070 /* Find the smallest table size > minused. */
1071 for (newsize = PyDict_MINSIZE;
Victor Stinner3d3f2642016-12-15 17:21:23 +01001072 newsize < minsize && newsize > 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 newsize <<= 1)
1074 ;
1075 if (newsize <= 0) {
1076 PyErr_NoMemory();
1077 return -1;
1078 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001079
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001080 oldkeys = mp->ma_keys;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001081
1082 /* NOTE: Current odict checks mp->ma_keys to detect resize happen.
1083 * So we can't reuse oldkeys even if oldkeys->dk_size == newsize.
1084 * TODO: Try reusing oldkeys when reimplement odict.
1085 */
1086
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001087 /* Allocate a new table. */
1088 mp->ma_keys = new_keys_object(newsize);
1089 if (mp->ma_keys == NULL) {
1090 mp->ma_keys = oldkeys;
1091 return -1;
1092 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01001093 // New table must be large enough.
1094 assert(mp->ma_keys->dk_usable >= mp->ma_used);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001095 if (oldkeys->dk_lookup == lookdict)
1096 mp->ma_keys->dk_lookup = lookdict;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001097
1098 numentries = mp->ma_used;
1099 oldentries = DK_ENTRIES(oldkeys);
1100 newentries = DK_ENTRIES(mp->ma_keys);
1101 oldvalues = mp->ma_values;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001102 if (oldvalues != NULL) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001103 /* Convert split table into new combined table.
1104 * We must incref keys; we can transfer values.
1105 * Note that values of split table is always dense.
1106 */
1107 for (Py_ssize_t i = 0; i < numentries; i++) {
1108 assert(oldvalues[i] != NULL);
1109 PyDictKeyEntry *ep = &oldentries[i];
1110 PyObject *key = ep->me_key;
1111 Py_INCREF(key);
1112 newentries[i].me_key = key;
1113 newentries[i].me_hash = ep->me_hash;
1114 newentries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001116
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001117 DK_DECREF(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001118 mp->ma_values = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001119 if (oldvalues != empty_values) {
1120 free_values(oldvalues);
1121 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001122 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001123 else { // combined table.
1124 if (oldkeys->dk_nentries == numentries) {
1125 memcpy(newentries, oldentries, numentries * sizeof(PyDictKeyEntry));
1126 }
1127 else {
1128 PyDictKeyEntry *ep = oldentries;
1129 for (Py_ssize_t i = 0; i < numentries; i++) {
1130 while (ep->me_value == NULL)
1131 ep++;
1132 newentries[i] = *ep++;
1133 }
1134 }
1135
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001136 assert(oldkeys->dk_lookup != lookdict_split);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001137 assert(oldkeys->dk_refcnt == 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001138 if (oldkeys->dk_size == PyDict_MINSIZE &&
1139 numfreekeys < PyDict_MAXFREELIST) {
1140 DK_DEBUG_DECREF keys_free_list[numfreekeys++] = oldkeys;
1141 }
1142 else {
1143 DK_DEBUG_DECREF PyObject_FREE(oldkeys);
1144 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001145 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001146
1147 build_indices(mp->ma_keys, newentries, numentries);
1148 mp->ma_keys->dk_usable -= numentries;
1149 mp->ma_keys->dk_nentries = numentries;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001151}
1152
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001153/* Returns NULL if unable to split table.
1154 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001155static PyDictKeysObject *
1156make_keys_shared(PyObject *op)
1157{
1158 Py_ssize_t i;
1159 Py_ssize_t size;
1160 PyDictObject *mp = (PyDictObject *)op;
1161
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001162 if (!PyDict_CheckExact(op))
1163 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001164 if (!_PyDict_HasSplitTable(mp)) {
1165 PyDictKeyEntry *ep0;
1166 PyObject **values;
1167 assert(mp->ma_keys->dk_refcnt == 1);
1168 if (mp->ma_keys->dk_lookup == lookdict) {
1169 return NULL;
1170 }
1171 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1172 /* Remove dummy keys */
1173 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1174 return NULL;
1175 }
1176 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1177 /* Copy values into a new array */
Victor Stinner742da042016-09-07 17:40:12 -07001178 ep0 = DK_ENTRIES(mp->ma_keys);
1179 size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001180 values = new_values(size);
1181 if (values == NULL) {
1182 PyErr_SetString(PyExc_MemoryError,
1183 "Not enough memory to allocate new values array");
1184 return NULL;
1185 }
1186 for (i = 0; i < size; i++) {
1187 values[i] = ep0[i].me_value;
1188 ep0[i].me_value = NULL;
1189 }
1190 mp->ma_keys->dk_lookup = lookdict_split;
1191 mp->ma_values = values;
1192 }
1193 DK_INCREF(mp->ma_keys);
1194 return mp->ma_keys;
1195}
Christian Heimes99170a52007-12-19 02:07:34 +00001196
1197PyObject *
1198_PyDict_NewPresized(Py_ssize_t minused)
1199{
INADA Naoki92c50ee2016-11-22 00:57:02 +09001200 const Py_ssize_t max_presize = 128 * 1024;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001201 Py_ssize_t newsize;
1202 PyDictKeysObject *new_keys;
INADA Naoki92c50ee2016-11-22 00:57:02 +09001203
1204 /* There are no strict guarantee that returned dict can contain minused
1205 * items without resize. So we create medium size dict instead of very
1206 * large dict or MemoryError.
1207 */
1208 if (minused > USABLE_FRACTION(max_presize)) {
1209 newsize = max_presize;
1210 }
1211 else {
1212 Py_ssize_t minsize = ESTIMATE_SIZE(minused);
1213 newsize = PyDict_MINSIZE;
1214 while (newsize < minsize) {
1215 newsize <<= 1;
1216 }
1217 }
1218 assert(IS_POWER_OF_2(newsize));
1219
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001220 new_keys = new_keys_object(newsize);
1221 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001223 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001224}
1225
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001226/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1227 * that may occur (originally dicts supported only string keys, and exceptions
1228 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001229 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001230 * (suppressed) occurred while computing the key's hash, or that some error
1231 * (suppressed) occurred when comparing keys in the dict's internal probe
1232 * sequence. A nasty example of the latter is when a Python-coded comparison
1233 * function hits a stack-depth error, which can cause this to return NULL
1234 * even if the key is present.
1235 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001236PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001237PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001238{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001239 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07001240 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 PyDictObject *mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 PyThreadState *tstate;
INADA Naokiba609772016-12-07 20:41:42 +09001243 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 if (!PyDict_Check(op))
1246 return NULL;
1247 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001248 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 {
1250 hash = PyObject_Hash(key);
1251 if (hash == -1) {
1252 PyErr_Clear();
1253 return NULL;
1254 }
1255 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 /* We can arrive here with a NULL tstate during initialization: try
1258 running "python -Wi" for an example related to string interning.
1259 Let's just hope that no exception occurs then... This must be
1260 _PyThreadState_Current and not PyThreadState_GET() because in debug
1261 mode, the latter complains if tstate is NULL. */
Victor Stinner0cae6092016-11-11 01:43:56 +01001262 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 if (tstate != NULL && tstate->curexc_type != NULL) {
1264 /* preserve the existing exception */
1265 PyObject *err_type, *err_value, *err_tb;
1266 PyErr_Fetch(&err_type, &err_value, &err_tb);
INADA Naoki778928b2017-08-03 23:45:15 +09001267 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 /* ignore errors */
1269 PyErr_Restore(err_type, err_value, err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001270 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 return NULL;
1272 }
1273 else {
INADA Naoki778928b2017-08-03 23:45:15 +09001274 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001275 if (ix < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 PyErr_Clear();
1277 return NULL;
1278 }
1279 }
INADA Naokiba609772016-12-07 20:41:42 +09001280 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001281}
1282
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001283/* Same as PyDict_GetItemWithError() but with hash supplied by caller.
1284 This returns NULL *with* an exception set if an exception occurred.
1285 It returns NULL *without* an exception set if the key wasn't present.
1286*/
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001287PyObject *
1288_PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1289{
Victor Stinner742da042016-09-07 17:40:12 -07001290 Py_ssize_t ix;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001291 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001292 PyObject *value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001293
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001294 if (!PyDict_Check(op)) {
1295 PyErr_BadInternalCall();
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001296 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001297 }
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001298
INADA Naoki778928b2017-08-03 23:45:15 +09001299 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001300 if (ix < 0) {
1301 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001302 }
INADA Naokiba609772016-12-07 20:41:42 +09001303 return value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001304}
1305
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001306/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1307 This returns NULL *with* an exception set if an exception occurred.
1308 It returns NULL *without* an exception set if the key wasn't present.
1309*/
1310PyObject *
1311PyDict_GetItemWithError(PyObject *op, PyObject *key)
1312{
Victor Stinner742da042016-09-07 17:40:12 -07001313 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001314 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 PyDictObject*mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001316 PyObject *value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 if (!PyDict_Check(op)) {
1319 PyErr_BadInternalCall();
1320 return NULL;
1321 }
1322 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001323 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 {
1325 hash = PyObject_Hash(key);
1326 if (hash == -1) {
1327 return NULL;
1328 }
1329 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001330
INADA Naoki778928b2017-08-03 23:45:15 +09001331 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001332 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001334 return value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001335}
1336
Brett Cannonfd074152012-04-14 14:10:13 -04001337PyObject *
1338_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1339{
1340 PyObject *kv;
1341 kv = _PyUnicode_FromId(key); /* borrowed */
1342 if (kv == NULL)
1343 return NULL;
1344 return PyDict_GetItemWithError(dp, kv);
1345}
1346
Victor Stinnerb4efc962015-11-20 09:24:02 +01001347/* Fast version of global value lookup (LOAD_GLOBAL).
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001348 * Lookup in globals, then builtins.
Victor Stinnerb4efc962015-11-20 09:24:02 +01001349 *
1350 * Raise an exception and return NULL if an error occurred (ex: computing the
1351 * key hash failed, key comparison failed, ...). Return NULL if the key doesn't
1352 * exist. Return the value if the key exists.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001353 */
1354PyObject *
1355_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001356{
Victor Stinner742da042016-09-07 17:40:12 -07001357 Py_ssize_t ix;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001358 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09001359 PyObject *value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001360
1361 if (!PyUnicode_CheckExact(key) ||
1362 (hash = ((PyASCIIObject *) key)->hash) == -1)
1363 {
1364 hash = PyObject_Hash(key);
1365 if (hash == -1)
1366 return NULL;
Antoine Pitroue965d972012-02-27 00:45:12 +01001367 }
Victor Stinnerb4efc962015-11-20 09:24:02 +01001368
1369 /* namespace 1: globals */
INADA Naoki778928b2017-08-03 23:45:15 +09001370 ix = globals->ma_keys->dk_lookup(globals, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001371 if (ix == DKIX_ERROR)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001372 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001373 if (ix != DKIX_EMPTY && value != NULL)
1374 return value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001375
1376 /* namespace 2: builtins */
INADA Naoki778928b2017-08-03 23:45:15 +09001377 ix = builtins->ma_keys->dk_lookup(builtins, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001378 if (ix < 0)
Victor Stinnerb4efc962015-11-20 09:24:02 +01001379 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001380 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001381}
1382
Antoine Pitroue965d972012-02-27 00:45:12 +01001383/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1384 * dictionary if it's merely replacing the value for an existing key.
1385 * This means that it's safe to loop over a dictionary with PyDict_Next()
1386 * and occasionally replace a value -- but you can't insert new keys or
1387 * remove them.
1388 */
1389int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001390PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001391{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001392 PyDictObject *mp;
1393 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001394 if (!PyDict_Check(op)) {
1395 PyErr_BadInternalCall();
1396 return -1;
1397 }
1398 assert(key);
1399 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001400 mp = (PyDictObject *)op;
1401 if (!PyUnicode_CheckExact(key) ||
1402 (hash = ((PyASCIIObject *) key)->hash) == -1)
1403 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001404 hash = PyObject_Hash(key);
1405 if (hash == -1)
1406 return -1;
1407 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001408
1409 /* insertdict() handles any resizing that might be necessary */
1410 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001411}
1412
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001413int
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001414_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1415 Py_hash_t hash)
1416{
1417 PyDictObject *mp;
1418
1419 if (!PyDict_Check(op)) {
1420 PyErr_BadInternalCall();
1421 return -1;
1422 }
1423 assert(key);
1424 assert(value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001425 assert(hash != -1);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001426 mp = (PyDictObject *)op;
1427
1428 /* insertdict() handles any resizing that might be necessary */
1429 return insertdict(mp, key, hash, value);
1430}
1431
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001432static int
INADA Naoki778928b2017-08-03 23:45:15 +09001433delitem_common(PyDictObject *mp, Py_hash_t hash, Py_ssize_t ix,
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001434 PyObject *old_value)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001435{
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001436 PyObject *old_key;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001437 PyDictKeyEntry *ep;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001438
INADA Naoki778928b2017-08-03 23:45:15 +09001439 Py_ssize_t hashpos = lookdict_index(mp->ma_keys, hash, ix);
1440 assert(hashpos >= 0);
1441
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001442 mp->ma_used--;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001443 mp->ma_version_tag = DICT_NEXT_VERSION();
1444 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1445 dk_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
1446 ENSURE_ALLOWS_DELETIONS(mp);
1447 old_key = ep->me_key;
1448 ep->me_key = NULL;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001449 ep->me_value = NULL;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001450 Py_DECREF(old_key);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001451 Py_DECREF(old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001452
1453 assert(_PyDict_CheckConsistency(mp));
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001454 return 0;
1455}
1456
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001457int
Tim Peters1f5871e2000-07-04 17:44:48 +00001458PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001459{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001460 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 assert(key);
1462 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001463 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 hash = PyObject_Hash(key);
1465 if (hash == -1)
1466 return -1;
1467 }
Victor Stinner742da042016-09-07 17:40:12 -07001468
1469 return _PyDict_DelItem_KnownHash(op, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001470}
1471
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001472int
1473_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1474{
INADA Naoki778928b2017-08-03 23:45:15 +09001475 Py_ssize_t ix;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001476 PyDictObject *mp;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001477 PyObject *old_value;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001478
1479 if (!PyDict_Check(op)) {
1480 PyErr_BadInternalCall();
1481 return -1;
1482 }
1483 assert(key);
1484 assert(hash != -1);
1485 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001486 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001487 if (ix == DKIX_ERROR)
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001488 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09001489 if (ix == DKIX_EMPTY || old_value == NULL) {
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001490 _PyErr_SetKeyError(key);
1491 return -1;
1492 }
Victor Stinner78601a32016-09-09 19:28:36 -07001493
1494 // Split table doesn't allow deletion. Combine it.
1495 if (_PyDict_HasSplitTable(mp)) {
1496 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1497 return -1;
1498 }
INADA Naoki778928b2017-08-03 23:45:15 +09001499 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001500 assert(ix >= 0);
1501 }
1502
INADA Naoki778928b2017-08-03 23:45:15 +09001503 return delitem_common(mp, hash, ix, old_value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001504}
1505
Antoine Pitroud741ed42016-12-27 14:23:43 +01001506/* This function promises that the predicate -> deletion sequence is atomic
1507 * (i.e. protected by the GIL), assuming the predicate itself doesn't
1508 * release the GIL.
1509 */
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001510int
1511_PyDict_DelItemIf(PyObject *op, PyObject *key,
1512 int (*predicate)(PyObject *value))
1513{
Antoine Pitroud741ed42016-12-27 14:23:43 +01001514 Py_ssize_t hashpos, ix;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001515 PyDictObject *mp;
1516 Py_hash_t hash;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001517 PyObject *old_value;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001518 int res;
1519
1520 if (!PyDict_Check(op)) {
1521 PyErr_BadInternalCall();
1522 return -1;
1523 }
1524 assert(key);
1525 hash = PyObject_Hash(key);
1526 if (hash == -1)
1527 return -1;
1528 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001529 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001530 if (ix == DKIX_ERROR)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001531 return -1;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001532 if (ix == DKIX_EMPTY || old_value == NULL) {
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001533 _PyErr_SetKeyError(key);
1534 return -1;
1535 }
Antoine Pitroud741ed42016-12-27 14:23:43 +01001536
1537 // Split table doesn't allow deletion. Combine it.
1538 if (_PyDict_HasSplitTable(mp)) {
1539 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1540 return -1;
1541 }
INADA Naoki778928b2017-08-03 23:45:15 +09001542 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001543 assert(ix >= 0);
1544 }
1545
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001546 res = predicate(old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001547 if (res == -1)
1548 return -1;
INADA Naoki778928b2017-08-03 23:45:15 +09001549
1550 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1551 assert(hashpos >= 0);
1552
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001553 if (res > 0)
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001554 return delitem_common(mp, hashpos, ix, old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001555 else
1556 return 0;
1557}
1558
1559
Guido van Rossum25831651993-05-19 14:50:45 +00001560void
Tim Peters1f5871e2000-07-04 17:44:48 +00001561PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001562{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001564 PyDictKeysObject *oldkeys;
1565 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 if (!PyDict_Check(op))
1569 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001570 mp = ((PyDictObject *)op);
1571 oldkeys = mp->ma_keys;
1572 oldvalues = mp->ma_values;
1573 if (oldvalues == empty_values)
1574 return;
1575 /* Empty the dict... */
1576 DK_INCREF(Py_EMPTY_KEYS);
1577 mp->ma_keys = Py_EMPTY_KEYS;
1578 mp->ma_values = empty_values;
1579 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001580 mp->ma_version_tag = DICT_NEXT_VERSION();
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001581 /* ...then clear the keys and values */
1582 if (oldvalues != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001583 n = oldkeys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001584 for (i = 0; i < n; i++)
1585 Py_CLEAR(oldvalues[i]);
1586 free_values(oldvalues);
1587 DK_DECREF(oldkeys);
1588 }
1589 else {
1590 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001591 DK_DECREF(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001592 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02001593 assert(_PyDict_CheckConsistency(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001594}
1595
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001596/* Internal version of PyDict_Next that returns a hash value in addition
1597 * to the key and value.
1598 * Return 1 on success, return 0 when the reached the end of the dictionary
1599 * (or if op is not a dictionary)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001600 */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001601int
1602_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1603 PyObject **pvalue, Py_hash_t *phash)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001604{
INADA Naokica2d8be2016-11-04 16:59:10 +09001605 Py_ssize_t i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001606 PyDictObject *mp;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001607 PyDictKeyEntry *entry_ptr;
1608 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001609
1610 if (!PyDict_Check(op))
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001611 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 mp = (PyDictObject *)op;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001613 i = *ppos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001614 if (mp->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09001615 if (i < 0 || i >= mp->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001616 return 0;
INADA Naokica2d8be2016-11-04 16:59:10 +09001617 /* values of split table is always dense */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001618 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
INADA Naokica2d8be2016-11-04 16:59:10 +09001619 value = mp->ma_values[i];
1620 assert(value != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001622 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09001623 Py_ssize_t n = mp->ma_keys->dk_nentries;
1624 if (i < 0 || i >= n)
1625 return 0;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001626 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
1627 while (i < n && entry_ptr->me_value == NULL) {
1628 entry_ptr++;
1629 i++;
Victor Stinner742da042016-09-07 17:40:12 -07001630 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001631 if (i >= n)
1632 return 0;
1633 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001635 *ppos = i+1;
1636 if (pkey)
1637 *pkey = entry_ptr->me_key;
1638 if (phash)
1639 *phash = entry_ptr->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001640 if (pvalue)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001641 *pvalue = value;
1642 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001643}
1644
Tim Peters080c88b2003-02-15 03:01:11 +00001645/*
1646 * Iterate over a dict. Use like so:
1647 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001648 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001649 * PyObject *key, *value;
1650 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001651 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001652 * Refer to borrowed references in key and value.
Tim Peters080c88b2003-02-15 03:01:11 +00001653 * }
1654 *
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001655 * Return 1 on success, return 0 when the reached the end of the dictionary
1656 * (or if op is not a dictionary)
1657 *
Tim Peters080c88b2003-02-15 03:01:11 +00001658 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001659 * mutates the dict. One exception: it is safe if the loop merely changes
1660 * the values associated with the keys (but doesn't insert new keys or
1661 * delete keys), via PyDict_SetItem().
1662 */
Guido van Rossum25831651993-05-19 14:50:45 +00001663int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001664PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001665{
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001666 return _PyDict_Next(op, ppos, pkey, pvalue, NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001667}
1668
Eric Snow96c6af92015-05-29 22:21:39 -06001669/* Internal version of dict.pop(). */
1670PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001671_PyDict_Pop_KnownHash(PyObject *dict, PyObject *key, Py_hash_t hash, PyObject *deflt)
Eric Snow96c6af92015-05-29 22:21:39 -06001672{
Victor Stinner742da042016-09-07 17:40:12 -07001673 Py_ssize_t ix, hashpos;
Eric Snow96c6af92015-05-29 22:21:39 -06001674 PyObject *old_value, *old_key;
1675 PyDictKeyEntry *ep;
Yury Selivanov684ef2c2016-10-28 19:01:21 -04001676 PyDictObject *mp;
1677
1678 assert(PyDict_Check(dict));
1679 mp = (PyDictObject *)dict;
Eric Snow96c6af92015-05-29 22:21:39 -06001680
1681 if (mp->ma_used == 0) {
1682 if (deflt) {
1683 Py_INCREF(deflt);
1684 return deflt;
1685 }
1686 _PyErr_SetKeyError(key);
1687 return NULL;
1688 }
INADA Naoki778928b2017-08-03 23:45:15 +09001689 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001690 if (ix == DKIX_ERROR)
Eric Snow96c6af92015-05-29 22:21:39 -06001691 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001692 if (ix == DKIX_EMPTY || old_value == NULL) {
Eric Snow96c6af92015-05-29 22:21:39 -06001693 if (deflt) {
1694 Py_INCREF(deflt);
1695 return deflt;
1696 }
1697 _PyErr_SetKeyError(key);
1698 return NULL;
1699 }
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001700
Victor Stinner78601a32016-09-09 19:28:36 -07001701 // Split table doesn't allow deletion. Combine it.
1702 if (_PyDict_HasSplitTable(mp)) {
1703 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1704 return NULL;
1705 }
INADA Naoki778928b2017-08-03 23:45:15 +09001706 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001707 assert(ix >= 0);
1708 }
1709
INADA Naoki778928b2017-08-03 23:45:15 +09001710 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1711 assert(hashpos >= 0);
Victor Stinner78601a32016-09-09 19:28:36 -07001712 assert(old_value != NULL);
Eric Snow96c6af92015-05-29 22:21:39 -06001713 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001714 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner78601a32016-09-09 19:28:36 -07001715 dk_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
1716 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1717 ENSURE_ALLOWS_DELETIONS(mp);
1718 old_key = ep->me_key;
1719 ep->me_key = NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001720 ep->me_value = NULL;
Victor Stinner78601a32016-09-09 19:28:36 -07001721 Py_DECREF(old_key);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001722
1723 assert(_PyDict_CheckConsistency(mp));
Eric Snow96c6af92015-05-29 22:21:39 -06001724 return old_value;
1725}
1726
Serhiy Storchaka67796522017-01-12 18:34:33 +02001727PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001728_PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt)
Serhiy Storchaka67796522017-01-12 18:34:33 +02001729{
1730 Py_hash_t hash;
1731
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001732 if (((PyDictObject *)dict)->ma_used == 0) {
Serhiy Storchaka67796522017-01-12 18:34:33 +02001733 if (deflt) {
1734 Py_INCREF(deflt);
1735 return deflt;
1736 }
1737 _PyErr_SetKeyError(key);
1738 return NULL;
1739 }
1740 if (!PyUnicode_CheckExact(key) ||
1741 (hash = ((PyASCIIObject *) key)->hash) == -1) {
1742 hash = PyObject_Hash(key);
1743 if (hash == -1)
1744 return NULL;
1745 }
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001746 return _PyDict_Pop_KnownHash(dict, key, hash, deflt);
Serhiy Storchaka67796522017-01-12 18:34:33 +02001747}
1748
Eric Snow96c6af92015-05-29 22:21:39 -06001749/* Internal version of dict.from_keys(). It is subclass-friendly. */
1750PyObject *
1751_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1752{
1753 PyObject *it; /* iter(iterable) */
1754 PyObject *key;
1755 PyObject *d;
1756 int status;
1757
Victor Stinnera5ed5f02016-12-06 18:45:50 +01001758 d = _PyObject_CallNoArg(cls);
Eric Snow96c6af92015-05-29 22:21:39 -06001759 if (d == NULL)
1760 return NULL;
1761
1762 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
1763 if (PyDict_CheckExact(iterable)) {
1764 PyDictObject *mp = (PyDictObject *)d;
1765 PyObject *oldvalue;
1766 Py_ssize_t pos = 0;
1767 PyObject *key;
1768 Py_hash_t hash;
1769
Serhiy Storchakac61ac162017-03-21 08:52:38 +02001770 if (dictresize(mp, ESTIMATE_SIZE(PyDict_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001771 Py_DECREF(d);
1772 return NULL;
1773 }
1774
1775 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
1776 if (insertdict(mp, key, hash, value)) {
1777 Py_DECREF(d);
1778 return NULL;
1779 }
1780 }
1781 return d;
1782 }
1783 if (PyAnySet_CheckExact(iterable)) {
1784 PyDictObject *mp = (PyDictObject *)d;
1785 Py_ssize_t pos = 0;
1786 PyObject *key;
1787 Py_hash_t hash;
1788
Victor Stinner742da042016-09-07 17:40:12 -07001789 if (dictresize(mp, ESTIMATE_SIZE(PySet_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001790 Py_DECREF(d);
1791 return NULL;
1792 }
1793
1794 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
1795 if (insertdict(mp, key, hash, value)) {
1796 Py_DECREF(d);
1797 return NULL;
1798 }
1799 }
1800 return d;
1801 }
1802 }
1803
1804 it = PyObject_GetIter(iterable);
1805 if (it == NULL){
1806 Py_DECREF(d);
1807 return NULL;
1808 }
1809
1810 if (PyDict_CheckExact(d)) {
1811 while ((key = PyIter_Next(it)) != NULL) {
1812 status = PyDict_SetItem(d, key, value);
1813 Py_DECREF(key);
1814 if (status < 0)
1815 goto Fail;
1816 }
1817 } else {
1818 while ((key = PyIter_Next(it)) != NULL) {
1819 status = PyObject_SetItem(d, key, value);
1820 Py_DECREF(key);
1821 if (status < 0)
1822 goto Fail;
1823 }
1824 }
1825
1826 if (PyErr_Occurred())
1827 goto Fail;
1828 Py_DECREF(it);
1829 return d;
1830
1831Fail:
1832 Py_DECREF(it);
1833 Py_DECREF(d);
1834 return NULL;
1835}
1836
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001837/* Methods */
1838
1839static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001840dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001841{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001842 PyObject **values = mp->ma_values;
1843 PyDictKeysObject *keys = mp->ma_keys;
1844 Py_ssize_t i, n;
INADA Naokia6296d32017-08-24 14:55:17 +09001845
1846 /* bpo-31095: UnTrack is needed before calling any callbacks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 PyObject_GC_UnTrack(mp);
1848 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001849 if (values != NULL) {
1850 if (values != empty_values) {
Victor Stinner742da042016-09-07 17:40:12 -07001851 for (i = 0, n = mp->ma_keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001852 Py_XDECREF(values[i]);
1853 }
1854 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001856 DK_DECREF(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02001858 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001859 assert(keys->dk_refcnt == 1);
1860 DK_DECREF(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001861 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1863 free_list[numfree++] = mp;
1864 else
1865 Py_TYPE(mp)->tp_free((PyObject *)mp);
1866 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001867}
1868
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001869
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001870static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001871dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001872{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001873 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001874 PyObject *key = NULL, *value = NULL;
1875 _PyUnicodeWriter writer;
1876 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00001877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878 i = Py_ReprEnter((PyObject *)mp);
1879 if (i != 0) {
1880 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1881 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001883 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001884 Py_ReprLeave((PyObject *)mp);
1885 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 }
Tim Petersa7259592001-06-16 05:11:17 +00001887
Victor Stinnerf91929b2013-11-19 13:07:38 +01001888 _PyUnicodeWriter_Init(&writer);
1889 writer.overallocate = 1;
1890 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
1891 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00001892
Victor Stinnerf91929b2013-11-19 13:07:38 +01001893 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
1894 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 /* Do repr() on each key+value pair, and insert ": " between them.
1897 Note that repr may mutate the dict. */
1898 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001899 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001901 PyObject *s;
1902 int res;
1903
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001904 /* Prevent repr from deleting key or value during key format. */
1905 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02001907
Victor Stinnerf91929b2013-11-19 13:07:38 +01001908 if (!first) {
1909 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
1910 goto error;
1911 }
1912 first = 0;
1913
1914 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01001916 goto error;
1917 res = _PyUnicodeWriter_WriteStr(&writer, s);
1918 Py_DECREF(s);
1919 if (res < 0)
1920 goto error;
1921
1922 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
1923 goto error;
1924
1925 s = PyObject_Repr(value);
1926 if (s == NULL)
1927 goto error;
1928 res = _PyUnicodeWriter_WriteStr(&writer, s);
1929 Py_DECREF(s);
1930 if (res < 0)
1931 goto error;
1932
1933 Py_CLEAR(key);
1934 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 }
Tim Petersa7259592001-06-16 05:11:17 +00001936
Victor Stinnerf91929b2013-11-19 13:07:38 +01001937 writer.overallocate = 0;
1938 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
1939 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001940
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01001942
1943 return _PyUnicodeWriter_Finish(&writer);
1944
1945error:
1946 Py_ReprLeave((PyObject *)mp);
1947 _PyUnicodeWriter_Dealloc(&writer);
1948 Py_XDECREF(key);
1949 Py_XDECREF(value);
1950 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001951}
1952
Martin v. Löwis18e16552006-02-15 17:27:45 +00001953static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001954dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001955{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001957}
1958
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001959static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001960dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001961{
Victor Stinner742da042016-09-07 17:40:12 -07001962 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001963 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09001964 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001966 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001967 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968 hash = PyObject_Hash(key);
1969 if (hash == -1)
1970 return NULL;
1971 }
INADA Naoki778928b2017-08-03 23:45:15 +09001972 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001973 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001975 if (ix == DKIX_EMPTY || value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001976 if (!PyDict_CheckExact(mp)) {
1977 /* Look up __missing__ method if we're a subclass. */
1978 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05001979 _Py_IDENTIFIER(__missing__);
1980 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 if (missing != NULL) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001982 res = PyObject_CallFunctionObjArgs(missing,
1983 key, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 Py_DECREF(missing);
1985 return res;
1986 }
1987 else if (PyErr_Occurred())
1988 return NULL;
1989 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07001990 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001991 return NULL;
1992 }
INADA Naokiba609772016-12-07 20:41:42 +09001993 Py_INCREF(value);
1994 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001995}
1996
1997static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001998dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001999{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000 if (w == NULL)
2001 return PyDict_DelItem((PyObject *)mp, v);
2002 else
2003 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002004}
2005
Guido van Rossuma9e7a811997-05-13 21:02:11 +00002006static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002007 (lenfunc)dict_length, /*mp_length*/
2008 (binaryfunc)dict_subscript, /*mp_subscript*/
2009 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002010};
2011
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002012static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002013dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002014{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002015 PyObject *v;
2016 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002017 PyDictKeyEntry *ep;
2018 Py_ssize_t size, n, offset;
2019 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002020
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002021 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002022 n = mp->ma_used;
2023 v = PyList_New(n);
2024 if (v == NULL)
2025 return NULL;
2026 if (n != mp->ma_used) {
2027 /* Durnit. The allocations caused the dict to resize.
2028 * Just start over, this shouldn't normally happen.
2029 */
2030 Py_DECREF(v);
2031 goto again;
2032 }
Victor Stinner742da042016-09-07 17:40:12 -07002033 ep = DK_ENTRIES(mp->ma_keys);
2034 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002035 if (mp->ma_values) {
2036 value_ptr = mp->ma_values;
2037 offset = sizeof(PyObject *);
2038 }
2039 else {
2040 value_ptr = &ep[0].me_value;
2041 offset = sizeof(PyDictKeyEntry);
2042 }
2043 for (i = 0, j = 0; i < size; i++) {
2044 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045 PyObject *key = ep[i].me_key;
2046 Py_INCREF(key);
2047 PyList_SET_ITEM(v, j, key);
2048 j++;
2049 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002050 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002051 }
2052 assert(j == n);
2053 return v;
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_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002058{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002059 PyObject *v;
2060 Py_ssize_t i, j;
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002061 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002062 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 }
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002077 ep = DK_ENTRIES(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002078 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 {
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002084 value_ptr = &ep[0].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002085 offset = sizeof(PyDictKeyEntry);
2086 }
2087 for (i = 0, j = 0; i < size; i++) {
2088 PyObject *value = *value_ptr;
2089 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2090 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002091 Py_INCREF(value);
2092 PyList_SET_ITEM(v, j, value);
2093 j++;
2094 }
2095 }
2096 assert(j == n);
2097 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002098}
2099
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002100static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002101dict_items(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, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002105 Py_ssize_t size, offset;
2106 PyObject *item, *key;
2107 PyDictKeyEntry *ep;
2108 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002110 /* Preallocate the list of tuples, to avoid allocations during
2111 * the loop over the items, which could trigger GC, which
2112 * could resize the dict. :-(
2113 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002114 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002115 n = mp->ma_used;
2116 v = PyList_New(n);
2117 if (v == NULL)
2118 return NULL;
2119 for (i = 0; i < n; i++) {
2120 item = PyTuple_New(2);
2121 if (item == NULL) {
2122 Py_DECREF(v);
2123 return NULL;
2124 }
2125 PyList_SET_ITEM(v, i, item);
2126 }
2127 if (n != mp->ma_used) {
2128 /* Durnit. The allocations caused the dict to resize.
2129 * Just start over, this shouldn't normally happen.
2130 */
2131 Py_DECREF(v);
2132 goto again;
2133 }
2134 /* Nothing we do below makes any function calls. */
Victor Stinner742da042016-09-07 17:40:12 -07002135 ep = DK_ENTRIES(mp->ma_keys);
2136 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002137 if (mp->ma_values) {
2138 value_ptr = mp->ma_values;
2139 offset = sizeof(PyObject *);
2140 }
2141 else {
2142 value_ptr = &ep[0].me_value;
2143 offset = sizeof(PyDictKeyEntry);
2144 }
2145 for (i = 0, j = 0; i < size; i++) {
2146 PyObject *value = *value_ptr;
2147 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2148 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002149 key = ep[i].me_key;
2150 item = PyList_GET_ITEM(v, j);
2151 Py_INCREF(key);
2152 PyTuple_SET_ITEM(item, 0, key);
2153 Py_INCREF(value);
2154 PyTuple_SET_ITEM(item, 1, value);
2155 j++;
2156 }
2157 }
2158 assert(j == n);
2159 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002160}
2161
Larry Hastings5c661892014-01-24 06:17:25 -08002162/*[clinic input]
2163@classmethod
2164dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08002165 iterable: object
2166 value: object=None
2167 /
2168
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002169Create a new dictionary with keys from iterable and values set to value.
Larry Hastings5c661892014-01-24 06:17:25 -08002170[clinic start generated code]*/
2171
Larry Hastings5c661892014-01-24 06:17:25 -08002172static PyObject *
2173dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002174/*[clinic end generated code: output=8fb98e4b10384999 input=382ba4855d0f74c3]*/
Larry Hastings5c661892014-01-24 06:17:25 -08002175{
Eric Snow96c6af92015-05-29 22:21:39 -06002176 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002177}
2178
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002179static int
Victor Stinner742da042016-09-07 17:40:12 -07002180dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
2181 const char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002182{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002183 PyObject *arg = NULL;
2184 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002186 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
2187 result = -1;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002189 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002190 _Py_IDENTIFIER(keys);
2191 if (_PyObject_HasAttrId(arg, &PyId_keys))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 result = PyDict_Merge(self, arg, 1);
2193 else
2194 result = PyDict_MergeFromSeq2(self, arg, 1);
2195 }
2196 if (result == 0 && kwds != NULL) {
2197 if (PyArg_ValidateKeywordArguments(kwds))
2198 result = PyDict_Merge(self, kwds, 1);
2199 else
2200 result = -1;
2201 }
2202 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002203}
2204
Victor Stinner91f0d4a2017-01-19 12:45:06 +01002205/* Note: dict.update() uses the METH_VARARGS|METH_KEYWORDS calling convention.
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002206 Using METH_FASTCALL|METH_KEYWORDS would make dict.update(**dict2) calls
2207 slower, see the issue #29312. */
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002208static PyObject *
2209dict_update(PyObject *self, PyObject *args, PyObject *kwds)
2210{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 if (dict_update_common(self, args, kwds, "update") != -1)
2212 Py_RETURN_NONE;
2213 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002214}
2215
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002216/* Update unconditionally replaces existing items.
2217 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00002218 otherwise it leaves existing items unchanged.
2219
2220 PyDict_{Update,Merge} update/merge from a mapping object.
2221
Tim Petersf582b822001-12-11 18:51:08 +00002222 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00002223 producing iterable objects of length 2.
2224*/
2225
Tim Petersf582b822001-12-11 18:51:08 +00002226int
Tim Peters1fc240e2001-10-26 05:06:50 +00002227PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
2228{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002229 PyObject *it; /* iter(seq2) */
2230 Py_ssize_t i; /* index into seq2 of current element */
2231 PyObject *item; /* seq2[i] */
2232 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00002233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002234 assert(d != NULL);
2235 assert(PyDict_Check(d));
2236 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00002237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002238 it = PyObject_GetIter(seq2);
2239 if (it == NULL)
2240 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002242 for (i = 0; ; ++i) {
2243 PyObject *key, *value;
2244 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00002245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002246 fast = NULL;
2247 item = PyIter_Next(it);
2248 if (item == NULL) {
2249 if (PyErr_Occurred())
2250 goto Fail;
2251 break;
2252 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002254 /* Convert item to sequence, and verify length 2. */
2255 fast = PySequence_Fast(item, "");
2256 if (fast == NULL) {
2257 if (PyErr_ExceptionMatches(PyExc_TypeError))
2258 PyErr_Format(PyExc_TypeError,
2259 "cannot convert dictionary update "
2260 "sequence element #%zd to a sequence",
2261 i);
2262 goto Fail;
2263 }
2264 n = PySequence_Fast_GET_SIZE(fast);
2265 if (n != 2) {
2266 PyErr_Format(PyExc_ValueError,
2267 "dictionary update sequence element #%zd "
2268 "has length %zd; 2 is required",
2269 i, n);
2270 goto Fail;
2271 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002273 /* Update/merge with this (key, value) pair. */
2274 key = PySequence_Fast_GET_ITEM(fast, 0);
2275 value = PySequence_Fast_GET_ITEM(fast, 1);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002276 Py_INCREF(key);
2277 Py_INCREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 if (override || PyDict_GetItem(d, key) == NULL) {
2279 int status = PyDict_SetItem(d, key, value);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002280 if (status < 0) {
2281 Py_DECREF(key);
2282 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002283 goto Fail;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002284 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002285 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002286 Py_DECREF(key);
2287 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002288 Py_DECREF(fast);
2289 Py_DECREF(item);
2290 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292 i = 0;
Victor Stinner611b0fa2016-09-14 15:02:01 +02002293 assert(_PyDict_CheckConsistency((PyDictObject *)d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002295Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002296 Py_XDECREF(item);
2297 Py_XDECREF(fast);
2298 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002299Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002300 Py_DECREF(it);
2301 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002302}
2303
doko@ubuntu.comc96df682016-10-11 08:04:02 +02002304static int
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002305dict_merge(PyObject *a, PyObject *b, int override)
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002306{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002307 PyDictObject *mp, *other;
2308 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -07002309 PyDictKeyEntry *entry, *ep0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002310
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002311 assert(0 <= override && override <= 2);
2312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002313 /* We accept for the argument either a concrete dictionary object,
2314 * or an abstract "mapping" object. For the former, we can do
2315 * things quite efficiently. For the latter, we only require that
2316 * PyMapping_Keys() and PyObject_GetItem() be supported.
2317 */
2318 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2319 PyErr_BadInternalCall();
2320 return -1;
2321 }
2322 mp = (PyDictObject*)a;
2323 if (PyDict_Check(b)) {
2324 other = (PyDictObject*)b;
2325 if (other == mp || other->ma_used == 0)
2326 /* a.update(a) or a.update({}); nothing to do */
2327 return 0;
2328 if (mp->ma_used == 0)
2329 /* Since the target dict is empty, PyDict_GetItem()
2330 * always returns NULL. Setting override to 1
2331 * skips the unnecessary test.
2332 */
2333 override = 1;
2334 /* Do one big resize at the start, rather than
2335 * incrementally resizing as we insert new items. Expect
2336 * that there will be no (or few) overlapping keys.
2337 */
INADA Naokib1152be2016-10-27 19:26:50 +09002338 if (USABLE_FRACTION(mp->ma_keys->dk_size) < other->ma_used) {
2339 if (dictresize(mp, ESTIMATE_SIZE(mp->ma_used + other->ma_used))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002340 return -1;
INADA Naokib1152be2016-10-27 19:26:50 +09002341 }
2342 }
Victor Stinner742da042016-09-07 17:40:12 -07002343 ep0 = DK_ENTRIES(other->ma_keys);
2344 for (i = 0, n = other->ma_keys->dk_nentries; i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002345 PyObject *key, *value;
2346 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002347 entry = &ep0[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002348 key = entry->me_key;
2349 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002350 if (other->ma_values)
2351 value = other->ma_values[i];
2352 else
2353 value = entry->me_value;
2354
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002355 if (value != NULL) {
2356 int err = 0;
2357 Py_INCREF(key);
2358 Py_INCREF(value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002359 if (override == 1)
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002360 err = insertdict(mp, key, hash, value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002361 else if (_PyDict_GetItem_KnownHash(a, key, hash) == NULL) {
2362 if (PyErr_Occurred()) {
2363 Py_DECREF(value);
2364 Py_DECREF(key);
2365 return -1;
2366 }
2367 err = insertdict(mp, key, hash, value);
2368 }
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002369 else if (override != 0) {
2370 _PyErr_SetKeyError(key);
2371 Py_DECREF(value);
2372 Py_DECREF(key);
2373 return -1;
2374 }
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002375 Py_DECREF(value);
2376 Py_DECREF(key);
2377 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002378 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002379
Victor Stinner742da042016-09-07 17:40:12 -07002380 if (n != other->ma_keys->dk_nentries) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002381 PyErr_SetString(PyExc_RuntimeError,
2382 "dict mutated during update");
2383 return -1;
2384 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002385 }
2386 }
2387 }
2388 else {
2389 /* Do it the generic, slower way */
2390 PyObject *keys = PyMapping_Keys(b);
2391 PyObject *iter;
2392 PyObject *key, *value;
2393 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 if (keys == NULL)
2396 /* Docstring says this is equivalent to E.keys() so
2397 * if E doesn't have a .keys() method we want
2398 * AttributeError to percolate up. Might as well
2399 * do the same for any other error.
2400 */
2401 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002403 iter = PyObject_GetIter(keys);
2404 Py_DECREF(keys);
2405 if (iter == NULL)
2406 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002408 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002409 if (override != 1 && PyDict_GetItem(a, key) != NULL) {
2410 if (override != 0) {
2411 _PyErr_SetKeyError(key);
2412 Py_DECREF(key);
2413 Py_DECREF(iter);
2414 return -1;
2415 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 Py_DECREF(key);
2417 continue;
2418 }
2419 value = PyObject_GetItem(b, key);
2420 if (value == NULL) {
2421 Py_DECREF(iter);
2422 Py_DECREF(key);
2423 return -1;
2424 }
2425 status = PyDict_SetItem(a, key, value);
2426 Py_DECREF(key);
2427 Py_DECREF(value);
2428 if (status < 0) {
2429 Py_DECREF(iter);
2430 return -1;
2431 }
2432 }
2433 Py_DECREF(iter);
2434 if (PyErr_Occurred())
2435 /* Iterator completed, via error */
2436 return -1;
2437 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02002438 assert(_PyDict_CheckConsistency((PyDictObject *)a));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002440}
2441
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002442int
2443PyDict_Update(PyObject *a, PyObject *b)
2444{
2445 return dict_merge(a, b, 1);
2446}
2447
2448int
2449PyDict_Merge(PyObject *a, PyObject *b, int override)
2450{
2451 /* XXX Deprecate override not in (0, 1). */
2452 return dict_merge(a, b, override != 0);
2453}
2454
2455int
2456_PyDict_MergeEx(PyObject *a, PyObject *b, int override)
2457{
2458 return dict_merge(a, b, override);
2459}
2460
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002461static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002462dict_copy(PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002463{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002464 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002465}
2466
2467PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002468PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002469{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002470 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002471 PyDictObject *mp;
2472 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002474 if (o == NULL || !PyDict_Check(o)) {
2475 PyErr_BadInternalCall();
2476 return NULL;
2477 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002478 mp = (PyDictObject *)o;
2479 if (_PyDict_HasSplitTable(mp)) {
2480 PyDictObject *split_copy;
Victor Stinner742da042016-09-07 17:40:12 -07002481 Py_ssize_t size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
2482 PyObject **newvalues;
2483 newvalues = new_values(size);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002484 if (newvalues == NULL)
2485 return PyErr_NoMemory();
2486 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2487 if (split_copy == NULL) {
2488 free_values(newvalues);
2489 return NULL;
2490 }
2491 split_copy->ma_values = newvalues;
2492 split_copy->ma_keys = mp->ma_keys;
2493 split_copy->ma_used = mp->ma_used;
2494 DK_INCREF(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002495 for (i = 0, n = size; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002496 PyObject *value = mp->ma_values[i];
2497 Py_XINCREF(value);
2498 split_copy->ma_values[i] = value;
2499 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002500 if (_PyObject_GC_IS_TRACKED(mp))
2501 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002502 return (PyObject *)split_copy;
2503 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002504 copy = PyDict_New();
2505 if (copy == NULL)
2506 return NULL;
2507 if (PyDict_Merge(copy, o, 1) == 0)
2508 return copy;
2509 Py_DECREF(copy);
2510 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002511}
2512
Martin v. Löwis18e16552006-02-15 17:27:45 +00002513Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002514PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002515{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002516 if (mp == NULL || !PyDict_Check(mp)) {
2517 PyErr_BadInternalCall();
2518 return -1;
2519 }
2520 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002521}
2522
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002523PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002524PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002525{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002526 if (mp == NULL || !PyDict_Check(mp)) {
2527 PyErr_BadInternalCall();
2528 return NULL;
2529 }
2530 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002531}
2532
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002533PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002534PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002535{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002536 if (mp == NULL || !PyDict_Check(mp)) {
2537 PyErr_BadInternalCall();
2538 return NULL;
2539 }
2540 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002541}
2542
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002543PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002544PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002545{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002546 if (mp == NULL || !PyDict_Check(mp)) {
2547 PyErr_BadInternalCall();
2548 return NULL;
2549 }
2550 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002551}
2552
Tim Peterse63415e2001-05-08 04:38:29 +00002553/* Return 1 if dicts equal, 0 if not, -1 if error.
2554 * Gets out as soon as any difference is detected.
2555 * Uses only Py_EQ comparison.
2556 */
2557static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002558dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002560 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002562 if (a->ma_used != b->ma_used)
2563 /* can't be equal if # of entries differ */
2564 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002565 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Victor Stinner742da042016-09-07 17:40:12 -07002566 for (i = 0; i < a->ma_keys->dk_nentries; i++) {
2567 PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002568 PyObject *aval;
2569 if (a->ma_values)
2570 aval = a->ma_values[i];
2571 else
2572 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002573 if (aval != NULL) {
2574 int cmp;
2575 PyObject *bval;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002576 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002577 /* temporarily bump aval's refcount to ensure it stays
2578 alive until we're done with it */
2579 Py_INCREF(aval);
2580 /* ditto for key */
2581 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002582 /* reuse the known hash value */
INADA Naoki778928b2017-08-03 23:45:15 +09002583 b->ma_keys->dk_lookup(b, key, ep->me_hash, &bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002584 if (bval == NULL) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002585 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002586 Py_DECREF(aval);
2587 if (PyErr_Occurred())
2588 return -1;
2589 return 0;
2590 }
2591 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002592 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002593 Py_DECREF(aval);
2594 if (cmp <= 0) /* error or not equal */
2595 return cmp;
2596 }
2597 }
2598 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002599}
Tim Peterse63415e2001-05-08 04:38:29 +00002600
2601static PyObject *
2602dict_richcompare(PyObject *v, PyObject *w, int op)
2603{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002604 int cmp;
2605 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002607 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2608 res = Py_NotImplemented;
2609 }
2610 else if (op == Py_EQ || op == Py_NE) {
2611 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2612 if (cmp < 0)
2613 return NULL;
2614 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2615 }
2616 else
2617 res = Py_NotImplemented;
2618 Py_INCREF(res);
2619 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002620}
Tim Peterse63415e2001-05-08 04:38:29 +00002621
Larry Hastings61272b72014-01-07 12:41:53 -08002622/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002623
2624@coexist
2625dict.__contains__
2626
2627 key: object
2628 /
2629
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002630True if the dictionary has the specified key, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002631[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002632
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002633static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002634dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka19d25972017-02-04 08:05:07 +02002635/*[clinic end generated code: output=a3d03db709ed6e6b input=fe1cb42ad831e820]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002636{
Larry Hastingsc2047262014-01-25 20:43:29 -08002637 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002638 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002639 Py_ssize_t ix;
INADA Naokiba609772016-12-07 20:41:42 +09002640 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002642 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002643 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002644 hash = PyObject_Hash(key);
2645 if (hash == -1)
2646 return NULL;
2647 }
INADA Naoki778928b2017-08-03 23:45:15 +09002648 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002649 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002650 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002651 if (ix == DKIX_EMPTY || value == NULL)
Victor Stinner742da042016-09-07 17:40:12 -07002652 Py_RETURN_FALSE;
2653 Py_RETURN_TRUE;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002654}
2655
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002656/*[clinic input]
2657dict.get
2658
2659 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002660 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002661 /
2662
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002663Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002664[clinic start generated code]*/
2665
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002666static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002667dict_get_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002668/*[clinic end generated code: output=bba707729dee05bf input=279ddb5790b6b107]*/
Barry Warsawc38c5da1997-10-06 17:49:20 +00002669{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002670 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002671 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002672 Py_ssize_t ix;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002674 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002675 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002676 hash = PyObject_Hash(key);
2677 if (hash == -1)
2678 return NULL;
2679 }
INADA Naoki778928b2017-08-03 23:45:15 +09002680 ix = (self->ma_keys->dk_lookup) (self, key, hash, &val);
Victor Stinner742da042016-09-07 17:40:12 -07002681 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002682 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002683 if (ix == DKIX_EMPTY || val == NULL) {
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002684 val = default_value;
INADA Naokiba609772016-12-07 20:41:42 +09002685 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002686 Py_INCREF(val);
2687 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002688}
2689
Benjamin Peterson00e98862013-03-07 22:16:29 -05002690PyObject *
2691PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002692{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002693 PyDictObject *mp = (PyDictObject *)d;
INADA Naoki93f26f72016-11-02 18:45:16 +09002694 PyObject *value;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002695 Py_hash_t hash;
Guido van Rossum164452c2000-08-08 16:12:54 +00002696
Benjamin Peterson00e98862013-03-07 22:16:29 -05002697 if (!PyDict_Check(d)) {
2698 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002699 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002700 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002702 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002703 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002704 hash = PyObject_Hash(key);
2705 if (hash == -1)
2706 return NULL;
2707 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002708
2709 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
2710 if (insertion_resize(mp) < 0)
2711 return NULL;
2712 }
2713
INADA Naoki778928b2017-08-03 23:45:15 +09002714 Py_ssize_t ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002715 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002716 return NULL;
INADA Naoki93f26f72016-11-02 18:45:16 +09002717
2718 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09002719 ((ix >= 0 && value == NULL && mp->ma_used != ix) ||
INADA Naoki93f26f72016-11-02 18:45:16 +09002720 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
2721 if (insertion_resize(mp) < 0) {
2722 return NULL;
2723 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002724 ix = DKIX_EMPTY;
2725 }
2726
2727 if (ix == DKIX_EMPTY) {
2728 PyDictKeyEntry *ep, *ep0;
2729 value = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002730 if (mp->ma_keys->dk_usable <= 0) {
Victor Stinner3c336c52016-09-12 14:17:40 +02002731 if (insertion_resize(mp) < 0) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002732 return NULL;
Victor Stinner3c336c52016-09-12 14:17:40 +02002733 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002734 }
INADA Naoki778928b2017-08-03 23:45:15 +09002735 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naoki93f26f72016-11-02 18:45:16 +09002736 ep0 = DK_ENTRIES(mp->ma_keys);
2737 ep = &ep0[mp->ma_keys->dk_nentries];
2738 dk_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002739 Py_INCREF(key);
INADA Naoki93f26f72016-11-02 18:45:16 +09002740 Py_INCREF(value);
2741 MAINTAIN_TRACKING(mp, key, value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002742 ep->me_key = key;
2743 ep->me_hash = hash;
INADA Naokiba609772016-12-07 20:41:42 +09002744 if (_PyDict_HasSplitTable(mp)) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002745 assert(mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
2746 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Victor Stinner742da042016-09-07 17:40:12 -07002747 }
2748 else {
INADA Naoki93f26f72016-11-02 18:45:16 +09002749 ep->me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07002750 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002751 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002752 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naoki93f26f72016-11-02 18:45:16 +09002753 mp->ma_keys->dk_usable--;
2754 mp->ma_keys->dk_nentries++;
2755 assert(mp->ma_keys->dk_usable >= 0);
2756 }
INADA Naokiba609772016-12-07 20:41:42 +09002757 else if (value == NULL) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002758 value = defaultobj;
2759 assert(_PyDict_HasSplitTable(mp));
2760 assert(ix == mp->ma_used);
2761 Py_INCREF(value);
2762 MAINTAIN_TRACKING(mp, key, value);
INADA Naokiba609772016-12-07 20:41:42 +09002763 mp->ma_values[ix] = value;
INADA Naoki93f26f72016-11-02 18:45:16 +09002764 mp->ma_used++;
2765 mp->ma_version_tag = DICT_NEXT_VERSION();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002766 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002767
2768 assert(_PyDict_CheckConsistency(mp));
2769 return value;
Guido van Rossum164452c2000-08-08 16:12:54 +00002770}
2771
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002772/*[clinic input]
2773dict.setdefault
2774
2775 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002776 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002777 /
2778
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002779Insert key with a value of default if key is not in the dictionary.
2780
2781Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002782[clinic start generated code]*/
2783
Benjamin Peterson00e98862013-03-07 22:16:29 -05002784static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002785dict_setdefault_impl(PyDictObject *self, PyObject *key,
2786 PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002787/*[clinic end generated code: output=f8c1101ebf69e220 input=0f063756e815fd9d]*/
Benjamin Peterson00e98862013-03-07 22:16:29 -05002788{
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002789 PyObject *val;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002790
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002791 val = PyDict_SetDefault((PyObject *)self, key, default_value);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002792 Py_XINCREF(val);
2793 return val;
2794}
Guido van Rossum164452c2000-08-08 16:12:54 +00002795
2796static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002797dict_clear(PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002798{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002799 PyDict_Clear((PyObject *)mp);
2800 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002801}
2802
Guido van Rossumba6ab842000-12-12 22:02:18 +00002803static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002804dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002805{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002806 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00002807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002808 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2809 return NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06002810
Yury Selivanov684ef2c2016-10-28 19:01:21 -04002811 return _PyDict_Pop((PyObject*)mp, key, deflt);
Guido van Rossume027d982002-04-12 15:11:59 +00002812}
2813
2814static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002815dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002816{
Victor Stinner742da042016-09-07 17:40:12 -07002817 Py_ssize_t i, j;
2818 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002819 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002821 /* Allocate the result tuple before checking the size. Believe it
2822 * or not, this allocation could trigger a garbage collection which
2823 * could empty the dict, so if we checked the size first and that
2824 * happened, the result would be an infinite loop (searching for an
2825 * entry that no longer exists). Note that the usual popitem()
2826 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2827 * tuple away if the dict *is* empty isn't a significant
2828 * inefficiency -- possible, but unlikely in practice.
2829 */
2830 res = PyTuple_New(2);
2831 if (res == NULL)
2832 return NULL;
2833 if (mp->ma_used == 0) {
2834 Py_DECREF(res);
2835 PyErr_SetString(PyExc_KeyError,
2836 "popitem(): dictionary is empty");
2837 return NULL;
2838 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002839 /* Convert split table to combined table */
2840 if (mp->ma_keys->dk_lookup == lookdict_split) {
2841 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2842 Py_DECREF(res);
2843 return NULL;
2844 }
2845 }
2846 ENSURE_ALLOWS_DELETIONS(mp);
Victor Stinner742da042016-09-07 17:40:12 -07002847
2848 /* Pop last item */
2849 ep0 = DK_ENTRIES(mp->ma_keys);
2850 i = mp->ma_keys->dk_nentries - 1;
2851 while (i >= 0 && ep0[i].me_value == NULL) {
2852 i--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002853 }
Victor Stinner742da042016-09-07 17:40:12 -07002854 assert(i >= 0);
2855
2856 ep = &ep0[i];
2857 j = lookdict_index(mp->ma_keys, ep->me_hash, i);
2858 assert(j >= 0);
2859 assert(dk_get_index(mp->ma_keys, j) == i);
2860 dk_set_index(mp->ma_keys, j, DKIX_DUMMY);
2861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002862 PyTuple_SET_ITEM(res, 0, ep->me_key);
2863 PyTuple_SET_ITEM(res, 1, ep->me_value);
Victor Stinner742da042016-09-07 17:40:12 -07002864 ep->me_key = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002865 ep->me_value = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002866 /* We can't dk_usable++ since there is DKIX_DUMMY in indices */
2867 mp->ma_keys->dk_nentries = i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002869 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02002870 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002871 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002872}
2873
Jeremy Hylton8caad492000-06-23 14:18:11 +00002874static int
2875dict_traverse(PyObject *op, visitproc visit, void *arg)
2876{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002877 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson55f44522016-09-05 12:12:59 -07002878 PyDictKeysObject *keys = mp->ma_keys;
Serhiy Storchaka46825d22016-09-26 21:29:34 +03002879 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Victor Stinner742da042016-09-07 17:40:12 -07002880 Py_ssize_t i, n = keys->dk_nentries;
2881
Benjamin Peterson55f44522016-09-05 12:12:59 -07002882 if (keys->dk_lookup == lookdict) {
2883 for (i = 0; i < n; i++) {
2884 if (entries[i].me_value != NULL) {
2885 Py_VISIT(entries[i].me_value);
2886 Py_VISIT(entries[i].me_key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002887 }
2888 }
Victor Stinner742da042016-09-07 17:40:12 -07002889 }
2890 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002891 if (mp->ma_values != NULL) {
Benjamin Peterson55f44522016-09-05 12:12:59 -07002892 for (i = 0; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002893 Py_VISIT(mp->ma_values[i]);
2894 }
2895 }
2896 else {
Benjamin Peterson55f44522016-09-05 12:12:59 -07002897 for (i = 0; i < n; i++) {
2898 Py_VISIT(entries[i].me_value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002899 }
2900 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 }
2902 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002903}
2904
2905static int
2906dict_tp_clear(PyObject *op)
2907{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 PyDict_Clear(op);
2909 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002910}
2911
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002912static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002913
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002914Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06002915_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002916{
Victor Stinner742da042016-09-07 17:40:12 -07002917 Py_ssize_t size, usable, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002918
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002919 size = DK_SIZE(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002920 usable = USABLE_FRACTION(size);
2921
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002922 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002923 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -07002924 res += usable * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002925 /* If the dictionary is split, the keys portion is accounted-for
2926 in the type object. */
2927 if (mp->ma_keys->dk_refcnt == 1)
Victor Stinner98ee9d52016-09-08 09:33:56 -07002928 res += (sizeof(PyDictKeysObject)
2929 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
2930 + DK_IXSIZE(mp->ma_keys) * size
2931 + sizeof(PyDictKeyEntry) * usable);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002932 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002933}
2934
2935Py_ssize_t
2936_PyDict_KeysSize(PyDictKeysObject *keys)
2937{
Victor Stinner98ee9d52016-09-08 09:33:56 -07002938 return (sizeof(PyDictKeysObject)
2939 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
2940 + DK_IXSIZE(keys) * DK_SIZE(keys)
2941 + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002942}
2943
doko@ubuntu.com17210f52016-01-14 14:04:59 +01002944static PyObject *
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002945dict_sizeof(PyDictObject *mp)
2946{
2947 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
2948}
2949
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002950PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
2951
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002952PyDoc_STRVAR(sizeof__doc__,
2953"D.__sizeof__() -> size of D in memory, in bytes");
2954
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002955PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002956"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002957If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00002958
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002959PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00002960"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000029612-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002962
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002963PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04002964"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
2965If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
2966If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
2967In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00002968
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002969PyDoc_STRVAR(clear__doc__,
2970"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002971
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002972PyDoc_STRVAR(copy__doc__,
2973"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00002974
Guido van Rossumb90c8482007-02-10 01:11:45 +00002975/* Forward */
2976static PyObject *dictkeys_new(PyObject *);
2977static PyObject *dictitems_new(PyObject *);
2978static PyObject *dictvalues_new(PyObject *);
2979
Guido van Rossum45c85d12007-07-27 16:31:40 +00002980PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002981 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002982PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002983 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002984PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002985 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00002986
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002987static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07002988 DICT___CONTAINS___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002989 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
2990 getitem__doc__},
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002991 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002992 sizeof__doc__},
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002993 DICT_GET_METHODDEF
2994 DICT_SETDEFAULT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002995 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
2996 pop__doc__},
2997 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
2998 popitem__doc__},
2999 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
3000 keys__doc__},
3001 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
3002 items__doc__},
3003 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
3004 values__doc__},
3005 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
3006 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08003007 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003008 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
3009 clear__doc__},
3010 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
3011 copy__doc__},
3012 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003013};
3014
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003015/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00003016int
3017PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003018{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003019 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07003020 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003021 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003022 PyObject *value;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003024 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003025 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003026 hash = PyObject_Hash(key);
3027 if (hash == -1)
3028 return -1;
3029 }
INADA Naoki778928b2017-08-03 23:45:15 +09003030 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003031 if (ix == DKIX_ERROR)
3032 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003033 return (ix != DKIX_EMPTY && value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003034}
3035
Thomas Wouterscf297e42007-02-23 15:07:44 +00003036/* Internal version of PyDict_Contains used when the hash value is already known */
3037int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003038_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00003039{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003040 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003041 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07003042 Py_ssize_t ix;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003043
INADA Naoki778928b2017-08-03 23:45:15 +09003044 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003045 if (ix == DKIX_ERROR)
3046 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003047 return (ix != DKIX_EMPTY && value != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00003048}
3049
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003050/* Hack to implement "key in dict" */
3051static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003052 0, /* sq_length */
3053 0, /* sq_concat */
3054 0, /* sq_repeat */
3055 0, /* sq_item */
3056 0, /* sq_slice */
3057 0, /* sq_ass_item */
3058 0, /* sq_ass_slice */
3059 PyDict_Contains, /* sq_contains */
3060 0, /* sq_inplace_concat */
3061 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003062};
3063
Guido van Rossum09e563a2001-05-01 12:10:21 +00003064static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003065dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3066{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003067 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003068 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003070 assert(type != NULL && type->tp_alloc != NULL);
3071 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02003072 if (self == NULL)
3073 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003074 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003075
Victor Stinnera9f61a52013-07-16 22:17:26 +02003076 /* The object has been implicitly tracked by tp_alloc */
3077 if (type == &PyDict_Type)
3078 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003079
3080 d->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003081 d->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07003082 d->ma_keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003083 if (d->ma_keys == NULL) {
3084 Py_DECREF(self);
3085 return NULL;
3086 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02003087 assert(_PyDict_CheckConsistency(d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003088 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003089}
3090
Tim Peters25786c02001-09-02 08:22:48 +00003091static int
3092dict_init(PyObject *self, PyObject *args, PyObject *kwds)
3093{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003094 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00003095}
3096
Tim Peters6d6c1a32001-08-02 04:15:00 +00003097static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003098dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00003099{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003100 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003101}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003102
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003103PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00003104"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00003105"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003106" (key, value) pairs\n"
3107"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00003108" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003109" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00003110" d[k] = v\n"
3111"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
3112" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00003113
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003114PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003115 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3116 "dict",
3117 sizeof(PyDictObject),
3118 0,
3119 (destructor)dict_dealloc, /* tp_dealloc */
3120 0, /* tp_print */
3121 0, /* tp_getattr */
3122 0, /* tp_setattr */
3123 0, /* tp_reserved */
3124 (reprfunc)dict_repr, /* tp_repr */
3125 0, /* tp_as_number */
3126 &dict_as_sequence, /* tp_as_sequence */
3127 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003128 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003129 0, /* tp_call */
3130 0, /* tp_str */
3131 PyObject_GenericGetAttr, /* tp_getattro */
3132 0, /* tp_setattro */
3133 0, /* tp_as_buffer */
3134 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3135 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
3136 dictionary_doc, /* tp_doc */
3137 dict_traverse, /* tp_traverse */
3138 dict_tp_clear, /* tp_clear */
3139 dict_richcompare, /* tp_richcompare */
3140 0, /* tp_weaklistoffset */
3141 (getiterfunc)dict_iter, /* tp_iter */
3142 0, /* tp_iternext */
3143 mapp_methods, /* tp_methods */
3144 0, /* tp_members */
3145 0, /* tp_getset */
3146 0, /* tp_base */
3147 0, /* tp_dict */
3148 0, /* tp_descr_get */
3149 0, /* tp_descr_set */
3150 0, /* tp_dictoffset */
3151 dict_init, /* tp_init */
3152 PyType_GenericAlloc, /* tp_alloc */
3153 dict_new, /* tp_new */
3154 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003155};
3156
Victor Stinner3c1e4812012-03-26 22:10:51 +02003157PyObject *
3158_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
3159{
3160 PyObject *kv;
3161 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02003162 if (kv == NULL) {
3163 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003164 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02003165 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02003166 return PyDict_GetItem(dp, kv);
3167}
3168
Guido van Rossum3cca2451997-05-16 14:23:33 +00003169/* For backward compatibility with old dictionary interface */
3170
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003171PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003172PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003173{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003174 PyObject *kv, *rv;
3175 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003176 if (kv == NULL) {
3177 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003178 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003179 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003180 rv = PyDict_GetItem(v, kv);
3181 Py_DECREF(kv);
3182 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003183}
3184
3185int
Victor Stinner3c1e4812012-03-26 22:10:51 +02003186_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
3187{
3188 PyObject *kv;
3189 kv = _PyUnicode_FromId(key); /* borrowed */
3190 if (kv == NULL)
3191 return -1;
3192 return PyDict_SetItem(v, kv, item);
3193}
3194
3195int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003196PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003198 PyObject *kv;
3199 int err;
3200 kv = PyUnicode_FromString(key);
3201 if (kv == NULL)
3202 return -1;
3203 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
3204 err = PyDict_SetItem(v, kv, item);
3205 Py_DECREF(kv);
3206 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003207}
3208
3209int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01003210_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
3211{
3212 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3213 if (kv == NULL)
3214 return -1;
3215 return PyDict_DelItem(v, kv);
3216}
3217
3218int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003219PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003220{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003221 PyObject *kv;
3222 int err;
3223 kv = PyUnicode_FromString(key);
3224 if (kv == NULL)
3225 return -1;
3226 err = PyDict_DelItem(v, kv);
3227 Py_DECREF(kv);
3228 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003229}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003230
Raymond Hettinger019a1482004-03-18 02:41:19 +00003231/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003232
3233typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003234 PyObject_HEAD
3235 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
3236 Py_ssize_t di_used;
3237 Py_ssize_t di_pos;
3238 PyObject* di_result; /* reusable result tuple for iteritems */
3239 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003240} dictiterobject;
3241
3242static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003243dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003244{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003245 dictiterobject *di;
3246 di = PyObject_GC_New(dictiterobject, itertype);
3247 if (di == NULL)
3248 return NULL;
3249 Py_INCREF(dict);
3250 di->di_dict = dict;
3251 di->di_used = dict->ma_used;
3252 di->di_pos = 0;
3253 di->len = dict->ma_used;
3254 if (itertype == &PyDictIterItem_Type) {
3255 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
3256 if (di->di_result == NULL) {
3257 Py_DECREF(di);
3258 return NULL;
3259 }
3260 }
3261 else
3262 di->di_result = NULL;
3263 _PyObject_GC_TRACK(di);
3264 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003265}
3266
3267static void
3268dictiter_dealloc(dictiterobject *di)
3269{
INADA Naokia6296d32017-08-24 14:55:17 +09003270 /* bpo-31095: UnTrack is needed before calling any callbacks */
3271 _PyObject_GC_UNTRACK(di);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003272 Py_XDECREF(di->di_dict);
3273 Py_XDECREF(di->di_result);
3274 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003275}
3276
3277static int
3278dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
3279{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003280 Py_VISIT(di->di_dict);
3281 Py_VISIT(di->di_result);
3282 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003283}
3284
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003285static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003286dictiter_len(dictiterobject *di)
3287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003288 Py_ssize_t len = 0;
3289 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
3290 len = di->len;
3291 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003292}
3293
Guido van Rossumb90c8482007-02-10 01:11:45 +00003294PyDoc_STRVAR(length_hint_doc,
3295 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003296
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003297static PyObject *
3298dictiter_reduce(dictiterobject *di);
3299
3300PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3301
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003302static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003303 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
3304 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003305 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
3306 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003307 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003308};
3309
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003310static PyObject*
3311dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00003312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003313 PyObject *key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003314 Py_ssize_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003315 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003316 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00003317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003318 if (d == NULL)
3319 return NULL;
3320 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00003321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003322 if (di->di_used != d->ma_used) {
3323 PyErr_SetString(PyExc_RuntimeError,
3324 "dictionary changed size during iteration");
3325 di->di_used = -1; /* Make this state sticky */
3326 return NULL;
3327 }
Guido van Rossum2147df72002-07-16 20:30:22 +00003328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003329 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003330 k = d->ma_keys;
INADA Naokica2d8be2016-11-04 16:59:10 +09003331 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003332 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003333 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003334 goto fail;
3335 key = DK_ENTRIES(k)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003336 assert(d->ma_values[i] != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003337 }
3338 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003339 Py_ssize_t n = k->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003340 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3341 while (i < n && entry_ptr->me_value == NULL) {
3342 entry_ptr++;
3343 i++;
3344 }
3345 if (i >= n)
3346 goto fail;
3347 key = entry_ptr->me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003348 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003349 di->di_pos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003350 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003351 Py_INCREF(key);
3352 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003353
3354fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003355 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003356 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003357 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003358}
3359
Raymond Hettinger019a1482004-03-18 02:41:19 +00003360PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003361 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3362 "dict_keyiterator", /* tp_name */
3363 sizeof(dictiterobject), /* tp_basicsize */
3364 0, /* tp_itemsize */
3365 /* methods */
3366 (destructor)dictiter_dealloc, /* tp_dealloc */
3367 0, /* tp_print */
3368 0, /* tp_getattr */
3369 0, /* tp_setattr */
3370 0, /* tp_reserved */
3371 0, /* tp_repr */
3372 0, /* tp_as_number */
3373 0, /* tp_as_sequence */
3374 0, /* tp_as_mapping */
3375 0, /* tp_hash */
3376 0, /* tp_call */
3377 0, /* tp_str */
3378 PyObject_GenericGetAttr, /* tp_getattro */
3379 0, /* tp_setattro */
3380 0, /* tp_as_buffer */
3381 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3382 0, /* tp_doc */
3383 (traverseproc)dictiter_traverse, /* tp_traverse */
3384 0, /* tp_clear */
3385 0, /* tp_richcompare */
3386 0, /* tp_weaklistoffset */
3387 PyObject_SelfIter, /* tp_iter */
3388 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3389 dictiter_methods, /* tp_methods */
3390 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003391};
3392
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003393static PyObject *
3394dictiter_iternextvalue(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003395{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003396 PyObject *value;
INADA Naokica2d8be2016-11-04 16:59:10 +09003397 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003398 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003400 if (d == NULL)
3401 return NULL;
3402 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003404 if (di->di_used != d->ma_used) {
3405 PyErr_SetString(PyExc_RuntimeError,
3406 "dictionary changed size during iteration");
3407 di->di_used = -1; /* Make this state sticky */
3408 return NULL;
3409 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003411 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003412 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003413 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003414 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003415 goto fail;
INADA Naokica2d8be2016-11-04 16:59:10 +09003416 value = d->ma_values[i];
3417 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003418 }
3419 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003420 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003421 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3422 while (i < n && entry_ptr->me_value == NULL) {
3423 entry_ptr++;
3424 i++;
3425 }
3426 if (i >= n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003427 goto fail;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003428 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003429 }
3430 di->di_pos = i+1;
3431 di->len--;
3432 Py_INCREF(value);
3433 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003434
3435fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003436 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003437 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003438 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003439}
3440
3441PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003442 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3443 "dict_valueiterator", /* tp_name */
3444 sizeof(dictiterobject), /* tp_basicsize */
3445 0, /* tp_itemsize */
3446 /* methods */
3447 (destructor)dictiter_dealloc, /* tp_dealloc */
3448 0, /* tp_print */
3449 0, /* tp_getattr */
3450 0, /* tp_setattr */
3451 0, /* tp_reserved */
3452 0, /* tp_repr */
3453 0, /* tp_as_number */
3454 0, /* tp_as_sequence */
3455 0, /* tp_as_mapping */
3456 0, /* tp_hash */
3457 0, /* tp_call */
3458 0, /* tp_str */
3459 PyObject_GenericGetAttr, /* tp_getattro */
3460 0, /* tp_setattro */
3461 0, /* tp_as_buffer */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003462 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003463 0, /* tp_doc */
3464 (traverseproc)dictiter_traverse, /* tp_traverse */
3465 0, /* tp_clear */
3466 0, /* tp_richcompare */
3467 0, /* tp_weaklistoffset */
3468 PyObject_SelfIter, /* tp_iter */
3469 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3470 dictiter_methods, /* tp_methods */
3471 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003472};
3473
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003474static PyObject *
3475dictiter_iternextitem(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003476{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003477 PyObject *key, *value, *result;
INADA Naokica2d8be2016-11-04 16:59:10 +09003478 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003479 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003481 if (d == NULL)
3482 return NULL;
3483 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003485 if (di->di_used != d->ma_used) {
3486 PyErr_SetString(PyExc_RuntimeError,
3487 "dictionary changed size during iteration");
3488 di->di_used = -1; /* Make this state sticky */
3489 return NULL;
3490 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003492 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003493 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003494 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003495 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003496 goto fail;
3497 key = DK_ENTRIES(d->ma_keys)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003498 value = d->ma_values[i];
3499 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003500 }
3501 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003502 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003503 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3504 while (i < n && entry_ptr->me_value == NULL) {
3505 entry_ptr++;
3506 i++;
3507 }
3508 if (i >= n)
3509 goto fail;
3510 key = entry_ptr->me_key;
3511 value = entry_ptr->me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003512 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003513 di->di_pos = i+1;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003514 di->len--;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003515 Py_INCREF(key);
3516 Py_INCREF(value);
3517 result = di->di_result;
3518 if (Py_REFCNT(result) == 1) {
3519 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3520 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3521 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3522 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003523 Py_INCREF(result);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003524 Py_DECREF(oldkey);
3525 Py_DECREF(oldvalue);
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003526 }
3527 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003528 result = PyTuple_New(2);
3529 if (result == NULL)
3530 return NULL;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003531 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3532 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003533 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003534 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003535
3536fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003537 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003538 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003539 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003540}
3541
3542PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003543 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3544 "dict_itemiterator", /* tp_name */
3545 sizeof(dictiterobject), /* tp_basicsize */
3546 0, /* tp_itemsize */
3547 /* methods */
3548 (destructor)dictiter_dealloc, /* tp_dealloc */
3549 0, /* tp_print */
3550 0, /* tp_getattr */
3551 0, /* tp_setattr */
3552 0, /* tp_reserved */
3553 0, /* tp_repr */
3554 0, /* tp_as_number */
3555 0, /* tp_as_sequence */
3556 0, /* tp_as_mapping */
3557 0, /* tp_hash */
3558 0, /* tp_call */
3559 0, /* tp_str */
3560 PyObject_GenericGetAttr, /* tp_getattro */
3561 0, /* tp_setattro */
3562 0, /* tp_as_buffer */
3563 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3564 0, /* tp_doc */
3565 (traverseproc)dictiter_traverse, /* tp_traverse */
3566 0, /* tp_clear */
3567 0, /* tp_richcompare */
3568 0, /* tp_weaklistoffset */
3569 PyObject_SelfIter, /* tp_iter */
3570 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3571 dictiter_methods, /* tp_methods */
3572 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003573};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003574
3575
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003576static PyObject *
3577dictiter_reduce(dictiterobject *di)
3578{
3579 PyObject *list;
3580 dictiterobject tmp;
3581
3582 list = PyList_New(0);
3583 if (!list)
3584 return NULL;
3585
3586 /* copy the itertor state */
3587 tmp = *di;
3588 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003589
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003590 /* iterate the temporary into a list */
3591 for(;;) {
3592 PyObject *element = 0;
3593 if (Py_TYPE(di) == &PyDictIterItem_Type)
3594 element = dictiter_iternextitem(&tmp);
3595 else if (Py_TYPE(di) == &PyDictIterKey_Type)
3596 element = dictiter_iternextkey(&tmp);
3597 else if (Py_TYPE(di) == &PyDictIterValue_Type)
3598 element = dictiter_iternextvalue(&tmp);
3599 else
Barry Warsawb2e57942017-09-14 18:13:16 -07003600 Py_UNREACHABLE();
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003601 if (element) {
3602 if (PyList_Append(list, element)) {
3603 Py_DECREF(element);
3604 Py_DECREF(list);
3605 Py_XDECREF(tmp.di_dict);
3606 return NULL;
3607 }
3608 Py_DECREF(element);
3609 } else
3610 break;
3611 }
3612 Py_XDECREF(tmp.di_dict);
3613 /* check for error */
3614 if (tmp.di_dict != NULL) {
3615 /* we have an error */
3616 Py_DECREF(list);
3617 return NULL;
3618 }
Antoine Pitroua7013882012-04-05 00:04:20 +02003619 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003620}
3621
Guido van Rossum3ac67412007-02-10 18:55:06 +00003622/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003623/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003624/***********************************************/
3625
Guido van Rossumb90c8482007-02-10 01:11:45 +00003626/* The instance lay-out is the same for all three; but the type differs. */
3627
Guido van Rossumb90c8482007-02-10 01:11:45 +00003628static void
Eric Snow96c6af92015-05-29 22:21:39 -06003629dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003630{
INADA Naokia6296d32017-08-24 14:55:17 +09003631 /* bpo-31095: UnTrack is needed before calling any callbacks */
3632 _PyObject_GC_UNTRACK(dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003633 Py_XDECREF(dv->dv_dict);
3634 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003635}
3636
3637static int
Eric Snow96c6af92015-05-29 22:21:39 -06003638dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003639{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003640 Py_VISIT(dv->dv_dict);
3641 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003642}
3643
Guido van Rossum83825ac2007-02-10 04:54:19 +00003644static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003645dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003646{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003647 Py_ssize_t len = 0;
3648 if (dv->dv_dict != NULL)
3649 len = dv->dv_dict->ma_used;
3650 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003651}
3652
Eric Snow96c6af92015-05-29 22:21:39 -06003653PyObject *
3654_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003655{
Eric Snow96c6af92015-05-29 22:21:39 -06003656 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003657 if (dict == NULL) {
3658 PyErr_BadInternalCall();
3659 return NULL;
3660 }
3661 if (!PyDict_Check(dict)) {
3662 /* XXX Get rid of this restriction later */
3663 PyErr_Format(PyExc_TypeError,
3664 "%s() requires a dict argument, not '%s'",
3665 type->tp_name, dict->ob_type->tp_name);
3666 return NULL;
3667 }
Eric Snow96c6af92015-05-29 22:21:39 -06003668 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003669 if (dv == NULL)
3670 return NULL;
3671 Py_INCREF(dict);
3672 dv->dv_dict = (PyDictObject *)dict;
3673 _PyObject_GC_TRACK(dv);
3674 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003675}
3676
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003677/* TODO(guido): The views objects are not complete:
3678
3679 * support more set operations
3680 * support arbitrary mappings?
3681 - either these should be static or exported in dictobject.h
3682 - if public then they should probably be in builtins
3683*/
3684
Guido van Rossumaac530c2007-08-24 22:33:45 +00003685/* Return 1 if self is a subset of other, iterating over self;
3686 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003687static int
3688all_contained_in(PyObject *self, PyObject *other)
3689{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003690 PyObject *iter = PyObject_GetIter(self);
3691 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003693 if (iter == NULL)
3694 return -1;
3695 for (;;) {
3696 PyObject *next = PyIter_Next(iter);
3697 if (next == NULL) {
3698 if (PyErr_Occurred())
3699 ok = -1;
3700 break;
3701 }
3702 ok = PySequence_Contains(other, next);
3703 Py_DECREF(next);
3704 if (ok <= 0)
3705 break;
3706 }
3707 Py_DECREF(iter);
3708 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003709}
3710
3711static PyObject *
3712dictview_richcompare(PyObject *self, PyObject *other, int op)
3713{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003714 Py_ssize_t len_self, len_other;
3715 int ok;
3716 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003718 assert(self != NULL);
3719 assert(PyDictViewSet_Check(self));
3720 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003721
Brian Curtindfc80e32011-08-10 20:28:54 -05003722 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3723 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003724
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003725 len_self = PyObject_Size(self);
3726 if (len_self < 0)
3727 return NULL;
3728 len_other = PyObject_Size(other);
3729 if (len_other < 0)
3730 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003732 ok = 0;
3733 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003734
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003735 case Py_NE:
3736 case Py_EQ:
3737 if (len_self == len_other)
3738 ok = all_contained_in(self, other);
3739 if (op == Py_NE && ok >= 0)
3740 ok = !ok;
3741 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003743 case Py_LT:
3744 if (len_self < len_other)
3745 ok = all_contained_in(self, other);
3746 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003747
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003748 case Py_LE:
3749 if (len_self <= len_other)
3750 ok = all_contained_in(self, other);
3751 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003753 case Py_GT:
3754 if (len_self > len_other)
3755 ok = all_contained_in(other, self);
3756 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003757
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003758 case Py_GE:
3759 if (len_self >= len_other)
3760 ok = all_contained_in(other, self);
3761 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003763 }
3764 if (ok < 0)
3765 return NULL;
3766 result = ok ? Py_True : Py_False;
3767 Py_INCREF(result);
3768 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003769}
3770
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003771static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003772dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003773{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003774 PyObject *seq;
3775 PyObject *result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003777 seq = PySequence_List((PyObject *)dv);
3778 if (seq == NULL)
3779 return NULL;
3780
3781 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
3782 Py_DECREF(seq);
3783 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003784}
3785
Guido van Rossum3ac67412007-02-10 18:55:06 +00003786/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003787
3788static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003789dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003790{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003791 if (dv->dv_dict == NULL) {
3792 Py_RETURN_NONE;
3793 }
3794 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003795}
3796
3797static int
Eric Snow96c6af92015-05-29 22:21:39 -06003798dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00003799{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003800 if (dv->dv_dict == NULL)
3801 return 0;
3802 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003803}
3804
Guido van Rossum83825ac2007-02-10 04:54:19 +00003805static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003806 (lenfunc)dictview_len, /* sq_length */
3807 0, /* sq_concat */
3808 0, /* sq_repeat */
3809 0, /* sq_item */
3810 0, /* sq_slice */
3811 0, /* sq_ass_item */
3812 0, /* sq_ass_slice */
3813 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003814};
3815
Guido van Rossum523259b2007-08-24 23:41:22 +00003816static PyObject*
3817dictviews_sub(PyObject* self, PyObject *other)
3818{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003819 PyObject *result = PySet_New(self);
3820 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003821 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003823 if (result == NULL)
3824 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003825
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003826 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003827 if (tmp == NULL) {
3828 Py_DECREF(result);
3829 return NULL;
3830 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003832 Py_DECREF(tmp);
3833 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003834}
3835
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003836PyObject*
3837_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00003838{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003839 PyObject *result = PySet_New(self);
3840 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003841 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003842
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003843 if (result == NULL)
3844 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003845
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003846 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_intersection_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003847 if (tmp == NULL) {
3848 Py_DECREF(result);
3849 return NULL;
3850 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003852 Py_DECREF(tmp);
3853 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003854}
3855
3856static PyObject*
3857dictviews_or(PyObject* self, PyObject *other)
3858{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003859 PyObject *result = PySet_New(self);
3860 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003861 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003863 if (result == NULL)
3864 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003865
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003866 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003867 if (tmp == NULL) {
3868 Py_DECREF(result);
3869 return NULL;
3870 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003872 Py_DECREF(tmp);
3873 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003874}
3875
3876static PyObject*
3877dictviews_xor(PyObject* self, PyObject *other)
3878{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003879 PyObject *result = PySet_New(self);
3880 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003881 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003883 if (result == NULL)
3884 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003885
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003886 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_symmetric_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003887 if (tmp == NULL) {
3888 Py_DECREF(result);
3889 return NULL;
3890 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003892 Py_DECREF(tmp);
3893 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003894}
3895
3896static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003897 0, /*nb_add*/
3898 (binaryfunc)dictviews_sub, /*nb_subtract*/
3899 0, /*nb_multiply*/
3900 0, /*nb_remainder*/
3901 0, /*nb_divmod*/
3902 0, /*nb_power*/
3903 0, /*nb_negative*/
3904 0, /*nb_positive*/
3905 0, /*nb_absolute*/
3906 0, /*nb_bool*/
3907 0, /*nb_invert*/
3908 0, /*nb_lshift*/
3909 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003910 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003911 (binaryfunc)dictviews_xor, /*nb_xor*/
3912 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00003913};
3914
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003915static PyObject*
3916dictviews_isdisjoint(PyObject *self, PyObject *other)
3917{
3918 PyObject *it;
3919 PyObject *item = NULL;
3920
3921 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06003922 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003923 Py_RETURN_TRUE;
3924 else
3925 Py_RETURN_FALSE;
3926 }
3927
3928 /* Iterate over the shorter object (only if other is a set,
3929 * because PySequence_Contains may be expensive otherwise): */
3930 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06003931 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003932 Py_ssize_t len_other = PyObject_Size(other);
3933 if (len_other == -1)
3934 return NULL;
3935
3936 if ((len_other > len_self)) {
3937 PyObject *tmp = other;
3938 other = self;
3939 self = tmp;
3940 }
3941 }
3942
3943 it = PyObject_GetIter(other);
3944 if (it == NULL)
3945 return NULL;
3946
3947 while ((item = PyIter_Next(it)) != NULL) {
3948 int contains = PySequence_Contains(self, item);
3949 Py_DECREF(item);
3950 if (contains == -1) {
3951 Py_DECREF(it);
3952 return NULL;
3953 }
3954
3955 if (contains) {
3956 Py_DECREF(it);
3957 Py_RETURN_FALSE;
3958 }
3959 }
3960 Py_DECREF(it);
3961 if (PyErr_Occurred())
3962 return NULL; /* PyIter_Next raised an exception. */
3963 Py_RETURN_TRUE;
3964}
3965
3966PyDoc_STRVAR(isdisjoint_doc,
3967"Return True if the view and the given iterable have a null intersection.");
3968
Guido van Rossumb90c8482007-02-10 01:11:45 +00003969static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003970 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3971 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003972 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003973};
3974
3975PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003976 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3977 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06003978 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003979 0, /* tp_itemsize */
3980 /* methods */
3981 (destructor)dictview_dealloc, /* tp_dealloc */
3982 0, /* tp_print */
3983 0, /* tp_getattr */
3984 0, /* tp_setattr */
3985 0, /* tp_reserved */
3986 (reprfunc)dictview_repr, /* tp_repr */
3987 &dictviews_as_number, /* tp_as_number */
3988 &dictkeys_as_sequence, /* tp_as_sequence */
3989 0, /* tp_as_mapping */
3990 0, /* tp_hash */
3991 0, /* tp_call */
3992 0, /* tp_str */
3993 PyObject_GenericGetAttr, /* tp_getattro */
3994 0, /* tp_setattro */
3995 0, /* tp_as_buffer */
3996 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3997 0, /* tp_doc */
3998 (traverseproc)dictview_traverse, /* tp_traverse */
3999 0, /* tp_clear */
4000 dictview_richcompare, /* tp_richcompare */
4001 0, /* tp_weaklistoffset */
4002 (getiterfunc)dictkeys_iter, /* tp_iter */
4003 0, /* tp_iternext */
4004 dictkeys_methods, /* tp_methods */
4005 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004006};
4007
4008static PyObject *
4009dictkeys_new(PyObject *dict)
4010{
Eric Snow96c6af92015-05-29 22:21:39 -06004011 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004012}
4013
Guido van Rossum3ac67412007-02-10 18:55:06 +00004014/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004015
4016static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004017dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004018{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004019 if (dv->dv_dict == NULL) {
4020 Py_RETURN_NONE;
4021 }
4022 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004023}
4024
4025static int
Eric Snow96c6af92015-05-29 22:21:39 -06004026dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004027{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004028 int result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004029 PyObject *key, *value, *found;
4030 if (dv->dv_dict == NULL)
4031 return 0;
4032 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
4033 return 0;
4034 key = PyTuple_GET_ITEM(obj, 0);
4035 value = PyTuple_GET_ITEM(obj, 1);
Raymond Hettinger6692f012016-09-18 21:46:08 -07004036 found = PyDict_GetItemWithError((PyObject *)dv->dv_dict, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004037 if (found == NULL) {
4038 if (PyErr_Occurred())
4039 return -1;
4040 return 0;
4041 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004042 Py_INCREF(found);
4043 result = PyObject_RichCompareBool(value, found, Py_EQ);
4044 Py_DECREF(found);
4045 return result;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004046}
4047
Guido van Rossum83825ac2007-02-10 04:54:19 +00004048static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004049 (lenfunc)dictview_len, /* sq_length */
4050 0, /* sq_concat */
4051 0, /* sq_repeat */
4052 0, /* sq_item */
4053 0, /* sq_slice */
4054 0, /* sq_ass_item */
4055 0, /* sq_ass_slice */
4056 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004057};
4058
Guido van Rossumb90c8482007-02-10 01:11:45 +00004059static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004060 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4061 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004062 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004063};
4064
4065PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004066 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4067 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004068 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004069 0, /* tp_itemsize */
4070 /* methods */
4071 (destructor)dictview_dealloc, /* tp_dealloc */
4072 0, /* tp_print */
4073 0, /* tp_getattr */
4074 0, /* tp_setattr */
4075 0, /* tp_reserved */
4076 (reprfunc)dictview_repr, /* tp_repr */
4077 &dictviews_as_number, /* tp_as_number */
4078 &dictitems_as_sequence, /* tp_as_sequence */
4079 0, /* tp_as_mapping */
4080 0, /* tp_hash */
4081 0, /* tp_call */
4082 0, /* tp_str */
4083 PyObject_GenericGetAttr, /* tp_getattro */
4084 0, /* tp_setattro */
4085 0, /* tp_as_buffer */
4086 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4087 0, /* tp_doc */
4088 (traverseproc)dictview_traverse, /* tp_traverse */
4089 0, /* tp_clear */
4090 dictview_richcompare, /* tp_richcompare */
4091 0, /* tp_weaklistoffset */
4092 (getiterfunc)dictitems_iter, /* tp_iter */
4093 0, /* tp_iternext */
4094 dictitems_methods, /* tp_methods */
4095 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004096};
4097
4098static PyObject *
4099dictitems_new(PyObject *dict)
4100{
Eric Snow96c6af92015-05-29 22:21:39 -06004101 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004102}
4103
Guido van Rossum3ac67412007-02-10 18:55:06 +00004104/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004105
4106static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004107dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004108{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004109 if (dv->dv_dict == NULL) {
4110 Py_RETURN_NONE;
4111 }
4112 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004113}
4114
Guido van Rossum83825ac2007-02-10 04:54:19 +00004115static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004116 (lenfunc)dictview_len, /* sq_length */
4117 0, /* sq_concat */
4118 0, /* sq_repeat */
4119 0, /* sq_item */
4120 0, /* sq_slice */
4121 0, /* sq_ass_item */
4122 0, /* sq_ass_slice */
4123 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004124};
4125
Guido van Rossumb90c8482007-02-10 01:11:45 +00004126static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004127 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004128};
4129
4130PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004131 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4132 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004133 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004134 0, /* tp_itemsize */
4135 /* methods */
4136 (destructor)dictview_dealloc, /* tp_dealloc */
4137 0, /* tp_print */
4138 0, /* tp_getattr */
4139 0, /* tp_setattr */
4140 0, /* tp_reserved */
4141 (reprfunc)dictview_repr, /* tp_repr */
4142 0, /* tp_as_number */
4143 &dictvalues_as_sequence, /* tp_as_sequence */
4144 0, /* tp_as_mapping */
4145 0, /* tp_hash */
4146 0, /* tp_call */
4147 0, /* tp_str */
4148 PyObject_GenericGetAttr, /* tp_getattro */
4149 0, /* tp_setattro */
4150 0, /* tp_as_buffer */
4151 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4152 0, /* tp_doc */
4153 (traverseproc)dictview_traverse, /* tp_traverse */
4154 0, /* tp_clear */
4155 0, /* tp_richcompare */
4156 0, /* tp_weaklistoffset */
4157 (getiterfunc)dictvalues_iter, /* tp_iter */
4158 0, /* tp_iternext */
4159 dictvalues_methods, /* tp_methods */
4160 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004161};
4162
4163static PyObject *
4164dictvalues_new(PyObject *dict)
4165{
Eric Snow96c6af92015-05-29 22:21:39 -06004166 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004167}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004168
4169/* Returns NULL if cannot allocate a new PyDictKeysObject,
4170 but does not set an error */
4171PyDictKeysObject *
4172_PyDict_NewKeysForClass(void)
4173{
Victor Stinner742da042016-09-07 17:40:12 -07004174 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004175 if (keys == NULL)
4176 PyErr_Clear();
4177 else
4178 keys->dk_lookup = lookdict_split;
4179 return keys;
4180}
4181
4182#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
4183
4184PyObject *
4185PyObject_GenericGetDict(PyObject *obj, void *context)
4186{
4187 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
4188 if (dictptr == NULL) {
4189 PyErr_SetString(PyExc_AttributeError,
4190 "This object has no __dict__");
4191 return NULL;
4192 }
4193 dict = *dictptr;
4194 if (dict == NULL) {
4195 PyTypeObject *tp = Py_TYPE(obj);
4196 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
4197 DK_INCREF(CACHED_KEYS(tp));
4198 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
4199 }
4200 else {
4201 *dictptr = dict = PyDict_New();
4202 }
4203 }
4204 Py_XINCREF(dict);
4205 return dict;
4206}
4207
4208int
4209_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
Victor Stinner742da042016-09-07 17:40:12 -07004210 PyObject *key, PyObject *value)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004211{
4212 PyObject *dict;
4213 int res;
4214 PyDictKeysObject *cached;
4215
4216 assert(dictptr != NULL);
4217 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
4218 assert(dictptr != NULL);
4219 dict = *dictptr;
4220 if (dict == NULL) {
4221 DK_INCREF(cached);
4222 dict = new_dict_with_shared_keys(cached);
4223 if (dict == NULL)
4224 return -1;
4225 *dictptr = dict;
4226 }
4227 if (value == NULL) {
4228 res = PyDict_DelItem(dict, key);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004229 // Since key sharing dict doesn't allow deletion, PyDict_DelItem()
4230 // always converts dict to combined form.
4231 if ((cached = CACHED_KEYS(tp)) != NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004232 CACHED_KEYS(tp) = NULL;
4233 DK_DECREF(cached);
4234 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01004235 }
4236 else {
INADA Naoki2294f3a2017-02-12 13:51:30 +09004237 int was_shared = (cached == ((PyDictObject *)dict)->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004238 res = PyDict_SetItem(dict, key, value);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004239 if (was_shared &&
4240 (cached = CACHED_KEYS(tp)) != NULL &&
4241 cached != ((PyDictObject *)dict)->ma_keys) {
Victor Stinner3d3f2642016-12-15 17:21:23 +01004242 /* PyDict_SetItem() may call dictresize and convert split table
4243 * into combined table. In such case, convert it to split
4244 * table again and update type's shared key only when this is
4245 * the only dict sharing key with the type.
4246 *
4247 * This is to allow using shared key in class like this:
4248 *
4249 * class C:
4250 * def __init__(self):
4251 * # one dict resize happens
4252 * self.a, self.b, self.c = 1, 2, 3
4253 * self.d, self.e, self.f = 4, 5, 6
4254 * a = C()
4255 */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004256 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004257 CACHED_KEYS(tp) = make_keys_shared(dict);
Victor Stinner742da042016-09-07 17:40:12 -07004258 }
4259 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004260 CACHED_KEYS(tp) = NULL;
4261 }
4262 DK_DECREF(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004263 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
4264 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004265 }
4266 }
4267 } else {
4268 dict = *dictptr;
4269 if (dict == NULL) {
4270 dict = PyDict_New();
4271 if (dict == NULL)
4272 return -1;
4273 *dictptr = dict;
4274 }
4275 if (value == NULL) {
4276 res = PyDict_DelItem(dict, key);
4277 } else {
4278 res = PyDict_SetItem(dict, key, value);
4279 }
4280 }
4281 return res;
4282}
4283
4284void
4285_PyDictKeys_DecRef(PyDictKeysObject *keys)
4286{
4287 DK_DECREF(keys);
4288}