blob: 01d913bfce4f847f0d22b78a0a3feab6a20d7cf7 [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.
Miss Islington (bot)902bb622018-04-17 10:17:19 -0700399 * Currently set to used*3.
Victor Stinnera9f61a52013-07-16 22:17:26 +0200400 * This means that dicts double in size when growing without deletions,
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700401 * but have more head room when the number of deletions is on a par with the
Miss Islington (bot)902bb622018-04-17 10:17:19 -0700402 * number of insertions. See also bpo-17563 and bpo-33205.
403 *
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700404 * GROWTH_RATE was set to used*4 up to version 3.2.
405 * GROWTH_RATE was set to used*2 in version 3.3.0
Miss Islington (bot)902bb622018-04-17 10:17:19 -0700406 * GROWTH_RATE was set to used*2 + capacity/2 in 3.4.0-3.6.0.
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200407 */
Miss Islington (bot)902bb622018-04-17 10:17:19 -0700408#define GROWTH_RATE(d) ((d)->ma_used*3)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400409
410#define ENSURE_ALLOWS_DELETIONS(d) \
411 if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \
412 (d)->ma_keys->dk_lookup = lookdict_unicode; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400414
415/* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
416 * (which cannot fail and thus can do no allocation).
417 */
418static PyDictKeysObject empty_keys_struct = {
Serhiy Storchaka97932e42016-09-26 23:01:23 +0300419 1, /* dk_refcnt */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400420 1, /* dk_size */
421 lookdict_split, /* dk_lookup */
422 0, /* dk_usable (immutable) */
Victor Stinner742da042016-09-07 17:40:12 -0700423 0, /* dk_nentries */
Benjamin Peterson186122e2016-09-08 12:20:12 -0700424 .dk_indices = { .as_1 = {DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY,
425 DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY}},
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400426};
427
428static PyObject *empty_values[1] = { NULL };
429
430#define Py_EMPTY_KEYS &empty_keys_struct
431
Victor Stinner611b0fa2016-09-14 15:02:01 +0200432/* Uncomment to check the dict content in _PyDict_CheckConsistency() */
433/* #define DEBUG_PYDICT */
434
435
T. Woutersa00c3fd2017-03-31 09:14:41 -0700436#ifndef NDEBUG
Victor Stinner611b0fa2016-09-14 15:02:01 +0200437static int
438_PyDict_CheckConsistency(PyDictObject *mp)
439{
440 PyDictKeysObject *keys = mp->ma_keys;
441 int splitted = _PyDict_HasSplitTable(mp);
442 Py_ssize_t usable = USABLE_FRACTION(keys->dk_size);
443#ifdef DEBUG_PYDICT
444 PyDictKeyEntry *entries = DK_ENTRIES(keys);
445 Py_ssize_t i;
446#endif
447
448 assert(0 <= mp->ma_used && mp->ma_used <= usable);
449 assert(IS_POWER_OF_2(keys->dk_size));
450 assert(0 <= keys->dk_usable
451 && keys->dk_usable <= usable);
452 assert(0 <= keys->dk_nentries
453 && keys->dk_nentries <= usable);
454 assert(keys->dk_usable + keys->dk_nentries <= usable);
455
456 if (!splitted) {
457 /* combined table */
458 assert(keys->dk_refcnt == 1);
459 }
460
461#ifdef DEBUG_PYDICT
462 for (i=0; i < keys->dk_size; i++) {
463 Py_ssize_t ix = dk_get_index(keys, i);
464 assert(DKIX_DUMMY <= ix && ix <= usable);
465 }
466
467 for (i=0; i < usable; i++) {
468 PyDictKeyEntry *entry = &entries[i];
469 PyObject *key = entry->me_key;
470
471 if (key != NULL) {
472 if (PyUnicode_CheckExact(key)) {
473 Py_hash_t hash = ((PyASCIIObject *)key)->hash;
474 assert(hash != -1);
475 assert(entry->me_hash == hash);
476 }
477 else {
478 /* test_dict fails if PyObject_Hash() is called again */
479 assert(entry->me_hash != -1);
480 }
481 if (!splitted) {
482 assert(entry->me_value != NULL);
483 }
484 }
485
486 if (splitted) {
487 assert(entry->me_value == NULL);
488 }
489 }
490
491 if (splitted) {
492 /* splitted table */
493 for (i=0; i < mp->ma_used; i++) {
494 assert(mp->ma_values[i] != NULL);
495 }
496 }
497#endif
498
499 return 1;
500}
501#endif
502
503
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400504static PyDictKeysObject *new_keys_object(Py_ssize_t size)
505{
506 PyDictKeysObject *dk;
Victor Stinner742da042016-09-07 17:40:12 -0700507 Py_ssize_t es, usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400508
Victor Stinner742da042016-09-07 17:40:12 -0700509 assert(size >= PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400510 assert(IS_POWER_OF_2(size));
Victor Stinner742da042016-09-07 17:40:12 -0700511
512 usable = USABLE_FRACTION(size);
513 if (size <= 0xff) {
514 es = 1;
515 }
516 else if (size <= 0xffff) {
517 es = 2;
518 }
519#if SIZEOF_VOID_P > 4
520 else if (size <= 0xffffffff) {
521 es = 4;
522 }
523#endif
524 else {
525 es = sizeof(Py_ssize_t);
526 }
527
528 if (size == PyDict_MINSIZE && numfreekeys > 0) {
529 dk = keys_free_list[--numfreekeys];
530 }
531 else {
Victor Stinner98ee9d52016-09-08 09:33:56 -0700532 dk = PyObject_MALLOC(sizeof(PyDictKeysObject)
533 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
534 + es * size
535 + sizeof(PyDictKeyEntry) * usable);
Victor Stinner742da042016-09-07 17:40:12 -0700536 if (dk == NULL) {
537 PyErr_NoMemory();
538 return NULL;
539 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400540 }
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200541 DK_DEBUG_INCREF dk->dk_refcnt = 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400542 dk->dk_size = size;
Victor Stinner742da042016-09-07 17:40:12 -0700543 dk->dk_usable = usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400544 dk->dk_lookup = lookdict_unicode_nodummy;
Victor Stinner742da042016-09-07 17:40:12 -0700545 dk->dk_nentries = 0;
Benjamin Peterson186122e2016-09-08 12:20:12 -0700546 memset(&dk->dk_indices.as_1[0], 0xff, es * size);
Victor Stinner742da042016-09-07 17:40:12 -0700547 memset(DK_ENTRIES(dk), 0, sizeof(PyDictKeyEntry) * usable);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400548 return dk;
549}
550
551static void
552free_keys_object(PyDictKeysObject *keys)
553{
Victor Stinner742da042016-09-07 17:40:12 -0700554 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400555 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -0700556 for (i = 0, n = keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400557 Py_XDECREF(entries[i].me_key);
558 Py_XDECREF(entries[i].me_value);
559 }
Victor Stinner742da042016-09-07 17:40:12 -0700560 if (keys->dk_size == PyDict_MINSIZE && numfreekeys < PyDict_MAXFREELIST) {
561 keys_free_list[numfreekeys++] = keys;
562 return;
563 }
Raymond Hettingerce5179f2016-01-31 08:56:21 -0800564 PyObject_FREE(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400565}
566
567#define new_values(size) PyMem_NEW(PyObject *, size)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400568#define free_values(values) PyMem_FREE(values)
569
570/* Consumes a reference to the keys object */
571static PyObject *
572new_dict(PyDictKeysObject *keys, PyObject **values)
573{
574 PyDictObject *mp;
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200575 assert(keys != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 if (numfree) {
577 mp = free_list[--numfree];
578 assert (mp != NULL);
579 assert (Py_TYPE(mp) == &PyDict_Type);
580 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400582 else {
583 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
584 if (mp == NULL) {
585 DK_DECREF(keys);
586 free_values(values);
587 return NULL;
588 }
589 }
590 mp->ma_keys = keys;
591 mp->ma_values = values;
592 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700593 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +0200594 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000596}
597
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400598/* Consumes a reference to the keys object */
599static PyObject *
600new_dict_with_shared_keys(PyDictKeysObject *keys)
601{
602 PyObject **values;
603 Py_ssize_t i, size;
604
Victor Stinner742da042016-09-07 17:40:12 -0700605 size = USABLE_FRACTION(DK_SIZE(keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400606 values = new_values(size);
607 if (values == NULL) {
608 DK_DECREF(keys);
609 return PyErr_NoMemory();
610 }
611 for (i = 0; i < size; i++) {
612 values[i] = NULL;
613 }
614 return new_dict(keys, values);
615}
616
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500617
618static PyObject *
619clone_combined_dict(PyDictObject *orig)
620{
621 assert(PyDict_CheckExact(orig));
622 assert(orig->ma_values == NULL);
623 assert(orig->ma_keys->dk_refcnt == 1);
624
625 Py_ssize_t keys_size = _PyDict_KeysSize(orig->ma_keys);
626 PyDictKeysObject *keys = PyObject_Malloc(keys_size);
627 if (keys == NULL) {
628 PyErr_NoMemory();
629 return NULL;
630 }
631
632 memcpy(keys, orig->ma_keys, keys_size);
633
634 /* After copying key/value pairs, we need to incref all
635 keys and values and they are about to be co-owned by a
636 new dict object. */
637 PyDictKeyEntry *ep0 = DK_ENTRIES(keys);
638 Py_ssize_t n = keys->dk_nentries;
639 for (Py_ssize_t i = 0; i < n; i++) {
640 PyDictKeyEntry *entry = &ep0[i];
641 PyObject *value = entry->me_value;
642 if (value != NULL) {
643 Py_INCREF(value);
644 Py_INCREF(entry->me_key);
645 }
646 }
647
648 PyDictObject *new = (PyDictObject *)new_dict(keys, NULL);
649 if (new == NULL) {
650 /* In case of an error, `new_dict()` takes care of
651 cleaning up `keys`. */
652 return NULL;
653 }
654 new->ma_used = orig->ma_used;
655 assert(_PyDict_CheckConsistency(new));
656 if (_PyObject_GC_IS_TRACKED(orig)) {
657 /* Maintain tracking. */
658 _PyObject_GC_TRACK(new);
659 }
660 return (PyObject *)new;
661}
662
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400663PyObject *
664PyDict_New(void)
665{
Victor Stinner742da042016-09-07 17:40:12 -0700666 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200667 if (keys == NULL)
668 return NULL;
669 return new_dict(keys, NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400670}
671
Victor Stinner742da042016-09-07 17:40:12 -0700672/* Search index of hash table from offset of entry table */
673static Py_ssize_t
674lookdict_index(PyDictKeysObject *k, Py_hash_t hash, Py_ssize_t index)
675{
Victor Stinner742da042016-09-07 17:40:12 -0700676 size_t mask = DK_MASK(k);
INADA Naoki073ae482017-06-23 15:22:50 +0900677 size_t perturb = (size_t)hash;
678 size_t i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700679
INADA Naoki073ae482017-06-23 15:22:50 +0900680 for (;;) {
681 Py_ssize_t ix = dk_get_index(k, i);
Victor Stinner742da042016-09-07 17:40:12 -0700682 if (ix == index) {
683 return i;
684 }
685 if (ix == DKIX_EMPTY) {
686 return DKIX_EMPTY;
687 }
INADA Naoki073ae482017-06-23 15:22:50 +0900688 perturb >>= PERTURB_SHIFT;
689 i = mask & (i*5 + perturb + 1);
Victor Stinner742da042016-09-07 17:40:12 -0700690 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700691 Py_UNREACHABLE();
Victor Stinner742da042016-09-07 17:40:12 -0700692}
693
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000694/*
695The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000696This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000697Open addressing is preferred over chaining since the link overhead for
698chaining would be substantial (100% with typical malloc overhead).
699
Tim Peterseb28ef22001-06-02 05:27:19 +0000700The initial probe index is computed as hash mod the table size. Subsequent
701probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000702
703All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000704
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000705The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000706contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000707Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000708
Victor Stinner742da042016-09-07 17:40:12 -0700709lookdict() is general-purpose, and may return DKIX_ERROR if (and only if) a
Victor Stinnera4348cc2016-09-08 12:01:25 -0700710comparison raises an exception.
Guido van Rossum89d8c602007-09-18 17:26:56 +0000711lookdict_unicode() below is specialized to string keys, comparison of which can
INADA Naoki1b8df102017-02-20 22:48:10 +0900712never raise an exception; that function can never return DKIX_ERROR when key
713is string. Otherwise, it falls back to lookdict().
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400714lookdict_unicode_nodummy is further specialized for string keys that cannot be
715the <dummy> value.
INADA Naoki778928b2017-08-03 23:45:15 +0900716For both, when the key isn't found a DKIX_EMPTY is returned.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000717*/
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100718static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400719lookdict(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900720 Py_hash_t hash, PyObject **value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000721{
INADA Naoki778928b2017-08-03 23:45:15 +0900722 size_t i, mask, perturb;
Victor Stinner742da042016-09-07 17:40:12 -0700723 PyDictKeysObject *dk;
INADA Naoki778928b2017-08-03 23:45:15 +0900724 PyDictKeyEntry *ep0;
Tim Peterseb28ef22001-06-02 05:27:19 +0000725
Antoine Pitrou9a234902012-05-13 20:48:01 +0200726top:
Victor Stinner742da042016-09-07 17:40:12 -0700727 dk = mp->ma_keys;
Victor Stinner742da042016-09-07 17:40:12 -0700728 ep0 = DK_ENTRIES(dk);
INADA Naoki778928b2017-08-03 23:45:15 +0900729 mask = DK_MASK(dk);
730 perturb = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700732
INADA Naoki778928b2017-08-03 23:45:15 +0900733 for (;;) {
734 Py_ssize_t ix = dk_get_index(dk, i);
Victor Stinner742da042016-09-07 17:40:12 -0700735 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700736 *value_addr = NULL;
737 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400738 }
INADA Naoki778928b2017-08-03 23:45:15 +0900739 if (ix >= 0) {
740 PyDictKeyEntry *ep = &ep0[ix];
741 assert(ep->me_key != NULL);
742 if (ep->me_key == key) {
743 *value_addr = ep->me_value;
744 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700745 }
INADA Naoki778928b2017-08-03 23:45:15 +0900746 if (ep->me_hash == hash) {
747 PyObject *startkey = ep->me_key;
748 Py_INCREF(startkey);
749 int cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
750 Py_DECREF(startkey);
751 if (cmp < 0) {
752 *value_addr = NULL;
753 return DKIX_ERROR;
754 }
755 if (dk == mp->ma_keys && ep->me_key == startkey) {
756 if (cmp > 0) {
757 *value_addr = ep->me_value;
758 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700759 }
INADA Naoki778928b2017-08-03 23:45:15 +0900760 }
761 else {
762 /* The dict was mutated, restart */
763 goto top;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400764 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 }
INADA Naoki778928b2017-08-03 23:45:15 +0900767 perturb >>= PERTURB_SHIFT;
768 i = (i*5 + perturb + 1) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700770 Py_UNREACHABLE();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000771}
772
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400773/* Specialized version for string-only keys */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100774static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400775lookdict_unicode(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900776 Py_hash_t hash, PyObject **value_addr)
Fred Drake1bff34a2000-08-31 19:31:38 +0000777{
Victor Stinner742da042016-09-07 17:40:12 -0700778 assert(mp->ma_values == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 /* Make sure this function doesn't have to handle non-unicode keys,
780 including subclasses of str; e.g., one reason to subclass
781 unicodes is to override __eq__, and for speed we don't cater to
782 that here. */
783 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400784 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900785 return lookdict(mp, key, hash, value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 }
Tim Peters15d49292001-05-27 07:39:22 +0000787
INADA Naoki778928b2017-08-03 23:45:15 +0900788 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
789 size_t mask = DK_MASK(mp->ma_keys);
790 size_t perturb = (size_t)hash;
791 size_t i = (size_t)hash & mask;
792
793 for (;;) {
794 Py_ssize_t ix = dk_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700795 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700796 *value_addr = NULL;
797 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400798 }
INADA Naoki778928b2017-08-03 23:45:15 +0900799 if (ix >= 0) {
800 PyDictKeyEntry *ep = &ep0[ix];
801 assert(ep->me_key != NULL);
802 assert(PyUnicode_CheckExact(ep->me_key));
803 if (ep->me_key == key ||
804 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
805 *value_addr = ep->me_value;
806 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700807 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400808 }
INADA Naoki778928b2017-08-03 23:45:15 +0900809 perturb >>= PERTURB_SHIFT;
810 i = mask & (i*5 + perturb + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700812 Py_UNREACHABLE();
Fred Drake1bff34a2000-08-31 19:31:38 +0000813}
814
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400815/* Faster version of lookdict_unicode when it is known that no <dummy> keys
816 * will be present. */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100817static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400818lookdict_unicode_nodummy(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 assert(mp->ma_values == NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400822 /* Make sure this function doesn't have to handle non-unicode keys,
823 including subclasses of str; e.g., one reason to subclass
824 unicodes is to override __eq__, and for speed we don't cater to
825 that here. */
826 if (!PyUnicode_CheckExact(key)) {
827 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900828 return lookdict(mp, key, hash, value_addr);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400829 }
INADA Naoki778928b2017-08-03 23:45:15 +0900830
831 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);
Victor Stinner742da042016-09-07 17:40:12 -0700838 assert (ix != DKIX_DUMMY);
839 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 = ep->me_value;
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
857/* Version of lookdict for split tables.
858 * All split tables and only split tables use this lookup function.
859 * Split tables only contain unicode keys and no dummy keys,
860 * so algorithm is the same as lookdict_unicode_nodummy.
861 */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100862static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400863lookdict_split(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900864 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400865{
Victor Stinner742da042016-09-07 17:40:12 -0700866 /* mp must split table */
867 assert(mp->ma_values != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400868 if (!PyUnicode_CheckExact(key)) {
INADA Naoki778928b2017-08-03 23:45:15 +0900869 Py_ssize_t ix = lookdict(mp, key, hash, value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700870 if (ix >= 0) {
INADA Naokiba609772016-12-07 20:41:42 +0900871 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700872 }
873 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400874 }
Victor Stinner742da042016-09-07 17:40:12 -0700875
INADA Naoki778928b2017-08-03 23:45:15 +0900876 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
877 size_t mask = DK_MASK(mp->ma_keys);
878 size_t perturb = (size_t)hash;
879 size_t i = (size_t)hash & mask;
880
881 for (;;) {
882 Py_ssize_t ix = dk_get_index(mp->ma_keys, i);
883 assert (ix != DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -0700884 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700885 *value_addr = NULL;
886 return DKIX_EMPTY;
887 }
INADA Naoki778928b2017-08-03 23:45:15 +0900888 PyDictKeyEntry *ep = &ep0[ix];
889 assert(ep->me_key != NULL);
890 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700891 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400892 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900893 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700894 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400895 }
INADA Naoki778928b2017-08-03 23:45:15 +0900896 perturb >>= PERTURB_SHIFT;
897 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400898 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700899 Py_UNREACHABLE();
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400900}
901
Benjamin Petersonfb886362010-04-24 18:21:17 +0000902int
903_PyDict_HasOnlyStringKeys(PyObject *dict)
904{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 Py_ssize_t pos = 0;
906 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000907 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400909 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 return 1;
911 while (PyDict_Next(dict, &pos, &key, &value))
912 if (!PyUnicode_Check(key))
913 return 0;
914 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000915}
916
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000917#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 do { \
919 if (!_PyObject_GC_IS_TRACKED(mp)) { \
920 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
921 _PyObject_GC_MAY_BE_TRACKED(value)) { \
922 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 } \
924 } \
925 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000926
927void
928_PyDict_MaybeUntrack(PyObject *op)
929{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 PyDictObject *mp;
931 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -0700932 Py_ssize_t i, numentries;
933 PyDictKeyEntry *ep0;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000934
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
936 return;
937
938 mp = (PyDictObject *) op;
Victor Stinner742da042016-09-07 17:40:12 -0700939 ep0 = DK_ENTRIES(mp->ma_keys);
940 numentries = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400941 if (_PyDict_HasSplitTable(mp)) {
Victor Stinner742da042016-09-07 17:40:12 -0700942 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400943 if ((value = mp->ma_values[i]) == NULL)
944 continue;
945 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
Victor Stinner742da042016-09-07 17:40:12 -0700946 assert(!_PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400947 return;
948 }
949 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400951 else {
Victor Stinner742da042016-09-07 17:40:12 -0700952 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400953 if ((value = ep0[i].me_value) == NULL)
954 continue;
955 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
956 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
957 return;
958 }
959 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000961}
962
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400963/* Internal function to find slot for an item from its hash
Victor Stinner3c336c52016-09-12 14:17:40 +0200964 when it is known that the key is not present in the dict.
965
966 The dict must be combined. */
INADA Naokiba609772016-12-07 20:41:42 +0900967static Py_ssize_t
INADA Naoki778928b2017-08-03 23:45:15 +0900968find_empty_slot(PyDictKeysObject *keys, Py_hash_t hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000969{
INADA Naoki778928b2017-08-03 23:45:15 +0900970 assert(keys != NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000971
INADA Naoki778928b2017-08-03 23:45:15 +0900972 const size_t mask = DK_MASK(keys);
973 size_t i = hash & mask;
974 Py_ssize_t ix = dk_get_index(keys, i);
975 for (size_t perturb = hash; ix >= 0;) {
INADA Naoki267941c2016-10-06 15:19:07 +0900976 perturb >>= PERTURB_SHIFT;
INADA Naoki778928b2017-08-03 23:45:15 +0900977 i = (i*5 + perturb + 1) & mask;
978 ix = dk_get_index(keys, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 }
INADA Naoki778928b2017-08-03 23:45:15 +0900980 return i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000981}
982
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400983static int
984insertion_resize(PyDictObject *mp)
985{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700986 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400987}
Antoine Pitroue965d972012-02-27 00:45:12 +0100988
989/*
990Internal routine to insert a new item into the table.
991Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +0100992Returns -1 if an error occurred, or 0 on success.
993*/
994static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400995insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +0100996{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400997 PyObject *old_value;
INADA Naokiba609772016-12-07 20:41:42 +0900998 PyDictKeyEntry *ep;
Antoine Pitroue965d972012-02-27 00:45:12 +0100999
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001000 Py_INCREF(key);
1001 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001002 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
1003 if (insertion_resize(mp) < 0)
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001004 goto Fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001005 }
1006
INADA Naoki778928b2017-08-03 23:45:15 +09001007 Py_ssize_t ix = mp->ma_keys->dk_lookup(mp, key, hash, &old_value);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001008 if (ix == DKIX_ERROR)
1009 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001010
Antoine Pitroud6967322014-10-18 00:35:00 +02001011 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001012 MAINTAIN_TRACKING(mp, key, value);
Victor Stinner742da042016-09-07 17:40:12 -07001013
1014 /* When insertion order is different from shared key, we can't share
1015 * the key anymore. Convert this instance to combine table.
1016 */
1017 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09001018 ((ix >= 0 && old_value == NULL && mp->ma_used != ix) ||
Victor Stinner742da042016-09-07 17:40:12 -07001019 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001020 if (insertion_resize(mp) < 0)
1021 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001022 ix = DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001023 }
Victor Stinner742da042016-09-07 17:40:12 -07001024
1025 if (ix == DKIX_EMPTY) {
1026 /* Insert into new slot. */
INADA Naokiba609772016-12-07 20:41:42 +09001027 assert(old_value == NULL);
Victor Stinner742da042016-09-07 17:40:12 -07001028 if (mp->ma_keys->dk_usable <= 0) {
1029 /* Need to resize. */
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001030 if (insertion_resize(mp) < 0)
1031 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001032 }
INADA Naoki778928b2017-08-03 23:45:15 +09001033 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naokiba609772016-12-07 20:41:42 +09001034 ep = &DK_ENTRIES(mp->ma_keys)[mp->ma_keys->dk_nentries];
Victor Stinner742da042016-09-07 17:40:12 -07001035 dk_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Victor Stinner742da042016-09-07 17:40:12 -07001036 ep->me_key = key;
1037 ep->me_hash = hash;
1038 if (mp->ma_values) {
1039 assert (mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
1040 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001041 }
1042 else {
Victor Stinner742da042016-09-07 17:40:12 -07001043 ep->me_value = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001044 }
1045 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001046 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07001047 mp->ma_keys->dk_usable--;
1048 mp->ma_keys->dk_nentries++;
1049 assert(mp->ma_keys->dk_usable >= 0);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001050 assert(_PyDict_CheckConsistency(mp));
Victor Stinner742da042016-09-07 17:40:12 -07001051 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001052 }
Victor Stinner742da042016-09-07 17:40:12 -07001053
INADA Naokiba609772016-12-07 20:41:42 +09001054 if (_PyDict_HasSplitTable(mp)) {
1055 mp->ma_values[ix] = value;
1056 if (old_value == NULL) {
1057 /* pending state */
1058 assert(ix == mp->ma_used);
1059 mp->ma_used++;
1060 }
1061 }
1062 else {
1063 assert(old_value != NULL);
1064 DK_ENTRIES(mp->ma_keys)[ix].me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07001065 }
1066
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001067 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokiba609772016-12-07 20:41:42 +09001068 Py_XDECREF(old_value); /* which **CAN** re-enter (see issue #22653) */
Victor Stinner611b0fa2016-09-14 15:02:01 +02001069 assert(_PyDict_CheckConsistency(mp));
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001070 Py_DECREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001071 return 0;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001072
1073Fail:
1074 Py_DECREF(value);
1075 Py_DECREF(key);
1076 return -1;
Antoine Pitroue965d972012-02-27 00:45:12 +01001077}
1078
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001079/*
luzpaza5293b42017-11-05 07:37:50 -06001080Internal routine used by dictresize() to build a hashtable of entries.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001081*/
1082static void
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001083build_indices(PyDictKeysObject *keys, PyDictKeyEntry *ep, Py_ssize_t n)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001084{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001085 size_t mask = (size_t)DK_SIZE(keys) - 1;
1086 for (Py_ssize_t ix = 0; ix != n; ix++, ep++) {
1087 Py_hash_t hash = ep->me_hash;
1088 size_t i = hash & mask;
1089 for (size_t perturb = hash; dk_get_index(keys, i) != DKIX_EMPTY;) {
1090 perturb >>= PERTURB_SHIFT;
INADA Naoki870c2862017-06-24 09:03:19 +09001091 i = mask & (i*5 + perturb + 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001092 }
1093 dk_set_index(keys, i, ix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001095}
1096
1097/*
1098Restructure the table by allocating a new table and reinserting all
1099items again. When entries have been deleted, the new table may
1100actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001101If a table is split (its keys and hashes are shared, its values are not),
1102then the values are temporarily copied into the table, it is resized as
1103a combined table, then the me_value slots in the old table are NULLed out.
1104After resizing a table is always combined,
1105but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001106*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001107static int
Victor Stinner3d3f2642016-12-15 17:21:23 +01001108dictresize(PyDictObject *mp, Py_ssize_t minsize)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001109{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001110 Py_ssize_t newsize, numentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001111 PyDictKeysObject *oldkeys;
1112 PyObject **oldvalues;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001113 PyDictKeyEntry *oldentries, *newentries;
Tim Peters91a364d2001-05-19 07:04:38 +00001114
Victor Stinner742da042016-09-07 17:40:12 -07001115 /* Find the smallest table size > minused. */
1116 for (newsize = PyDict_MINSIZE;
Victor Stinner3d3f2642016-12-15 17:21:23 +01001117 newsize < minsize && newsize > 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 newsize <<= 1)
1119 ;
1120 if (newsize <= 0) {
1121 PyErr_NoMemory();
1122 return -1;
1123 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001124
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001125 oldkeys = mp->ma_keys;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001126
1127 /* NOTE: Current odict checks mp->ma_keys to detect resize happen.
1128 * So we can't reuse oldkeys even if oldkeys->dk_size == newsize.
1129 * TODO: Try reusing oldkeys when reimplement odict.
1130 */
1131
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001132 /* Allocate a new table. */
1133 mp->ma_keys = new_keys_object(newsize);
1134 if (mp->ma_keys == NULL) {
1135 mp->ma_keys = oldkeys;
1136 return -1;
1137 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01001138 // New table must be large enough.
1139 assert(mp->ma_keys->dk_usable >= mp->ma_used);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001140 if (oldkeys->dk_lookup == lookdict)
1141 mp->ma_keys->dk_lookup = lookdict;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001142
1143 numentries = mp->ma_used;
1144 oldentries = DK_ENTRIES(oldkeys);
1145 newentries = DK_ENTRIES(mp->ma_keys);
1146 oldvalues = mp->ma_values;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001147 if (oldvalues != NULL) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001148 /* Convert split table into new combined table.
1149 * We must incref keys; we can transfer values.
1150 * Note that values of split table is always dense.
1151 */
1152 for (Py_ssize_t i = 0; i < numentries; i++) {
1153 assert(oldvalues[i] != NULL);
1154 PyDictKeyEntry *ep = &oldentries[i];
1155 PyObject *key = ep->me_key;
1156 Py_INCREF(key);
1157 newentries[i].me_key = key;
1158 newentries[i].me_hash = ep->me_hash;
1159 newentries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001161
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001162 DK_DECREF(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001163 mp->ma_values = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001164 if (oldvalues != empty_values) {
1165 free_values(oldvalues);
1166 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001167 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001168 else { // combined table.
1169 if (oldkeys->dk_nentries == numentries) {
1170 memcpy(newentries, oldentries, numentries * sizeof(PyDictKeyEntry));
1171 }
1172 else {
1173 PyDictKeyEntry *ep = oldentries;
1174 for (Py_ssize_t i = 0; i < numentries; i++) {
1175 while (ep->me_value == NULL)
1176 ep++;
1177 newentries[i] = *ep++;
1178 }
1179 }
1180
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001181 assert(oldkeys->dk_lookup != lookdict_split);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001182 assert(oldkeys->dk_refcnt == 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001183 if (oldkeys->dk_size == PyDict_MINSIZE &&
1184 numfreekeys < PyDict_MAXFREELIST) {
1185 DK_DEBUG_DECREF keys_free_list[numfreekeys++] = oldkeys;
1186 }
1187 else {
1188 DK_DEBUG_DECREF PyObject_FREE(oldkeys);
1189 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001190 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001191
1192 build_indices(mp->ma_keys, newentries, numentries);
1193 mp->ma_keys->dk_usable -= numentries;
1194 mp->ma_keys->dk_nentries = numentries;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001196}
1197
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001198/* Returns NULL if unable to split table.
1199 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001200static PyDictKeysObject *
1201make_keys_shared(PyObject *op)
1202{
1203 Py_ssize_t i;
1204 Py_ssize_t size;
1205 PyDictObject *mp = (PyDictObject *)op;
1206
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001207 if (!PyDict_CheckExact(op))
1208 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001209 if (!_PyDict_HasSplitTable(mp)) {
1210 PyDictKeyEntry *ep0;
1211 PyObject **values;
1212 assert(mp->ma_keys->dk_refcnt == 1);
1213 if (mp->ma_keys->dk_lookup == lookdict) {
1214 return NULL;
1215 }
1216 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1217 /* Remove dummy keys */
1218 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1219 return NULL;
1220 }
1221 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1222 /* Copy values into a new array */
Victor Stinner742da042016-09-07 17:40:12 -07001223 ep0 = DK_ENTRIES(mp->ma_keys);
1224 size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001225 values = new_values(size);
1226 if (values == NULL) {
1227 PyErr_SetString(PyExc_MemoryError,
1228 "Not enough memory to allocate new values array");
1229 return NULL;
1230 }
1231 for (i = 0; i < size; i++) {
1232 values[i] = ep0[i].me_value;
1233 ep0[i].me_value = NULL;
1234 }
1235 mp->ma_keys->dk_lookup = lookdict_split;
1236 mp->ma_values = values;
1237 }
1238 DK_INCREF(mp->ma_keys);
1239 return mp->ma_keys;
1240}
Christian Heimes99170a52007-12-19 02:07:34 +00001241
1242PyObject *
1243_PyDict_NewPresized(Py_ssize_t minused)
1244{
INADA Naoki92c50ee2016-11-22 00:57:02 +09001245 const Py_ssize_t max_presize = 128 * 1024;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001246 Py_ssize_t newsize;
1247 PyDictKeysObject *new_keys;
INADA Naoki92c50ee2016-11-22 00:57:02 +09001248
1249 /* There are no strict guarantee that returned dict can contain minused
1250 * items without resize. So we create medium size dict instead of very
1251 * large dict or MemoryError.
1252 */
1253 if (minused > USABLE_FRACTION(max_presize)) {
1254 newsize = max_presize;
1255 }
1256 else {
1257 Py_ssize_t minsize = ESTIMATE_SIZE(minused);
1258 newsize = PyDict_MINSIZE;
1259 while (newsize < minsize) {
1260 newsize <<= 1;
1261 }
1262 }
1263 assert(IS_POWER_OF_2(newsize));
1264
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001265 new_keys = new_keys_object(newsize);
1266 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001268 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001269}
1270
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001271/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1272 * that may occur (originally dicts supported only string keys, and exceptions
1273 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001274 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001275 * (suppressed) occurred while computing the key's hash, or that some error
1276 * (suppressed) occurred when comparing keys in the dict's internal probe
1277 * sequence. A nasty example of the latter is when a Python-coded comparison
1278 * function hits a stack-depth error, which can cause this to return NULL
1279 * even if the key is present.
1280 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001281PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001282PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001283{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001284 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07001285 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 PyDictObject *mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 PyThreadState *tstate;
INADA Naokiba609772016-12-07 20:41:42 +09001288 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 if (!PyDict_Check(op))
1291 return NULL;
1292 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001293 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 {
1295 hash = PyObject_Hash(key);
1296 if (hash == -1) {
1297 PyErr_Clear();
1298 return NULL;
1299 }
1300 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 /* We can arrive here with a NULL tstate during initialization: try
1303 running "python -Wi" for an example related to string interning.
1304 Let's just hope that no exception occurs then... This must be
1305 _PyThreadState_Current and not PyThreadState_GET() because in debug
1306 mode, the latter complains if tstate is NULL. */
Victor Stinner0cae6092016-11-11 01:43:56 +01001307 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 if (tstate != NULL && tstate->curexc_type != NULL) {
1309 /* preserve the existing exception */
1310 PyObject *err_type, *err_value, *err_tb;
1311 PyErr_Fetch(&err_type, &err_value, &err_tb);
INADA Naoki778928b2017-08-03 23:45:15 +09001312 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 /* ignore errors */
1314 PyErr_Restore(err_type, err_value, err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001315 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 return NULL;
1317 }
1318 else {
INADA Naoki778928b2017-08-03 23:45:15 +09001319 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001320 if (ix < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 PyErr_Clear();
1322 return NULL;
1323 }
1324 }
INADA Naokiba609772016-12-07 20:41:42 +09001325 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001326}
1327
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001328/* Same as PyDict_GetItemWithError() but with hash supplied by caller.
1329 This returns NULL *with* an exception set if an exception occurred.
1330 It returns NULL *without* an exception set if the key wasn't present.
1331*/
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001332PyObject *
1333_PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1334{
Victor Stinner742da042016-09-07 17:40:12 -07001335 Py_ssize_t ix;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001336 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001337 PyObject *value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001338
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001339 if (!PyDict_Check(op)) {
1340 PyErr_BadInternalCall();
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001341 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001342 }
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001343
INADA Naoki778928b2017-08-03 23:45:15 +09001344 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001345 if (ix < 0) {
1346 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001347 }
INADA Naokiba609772016-12-07 20:41:42 +09001348 return value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001349}
1350
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001351/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1352 This returns NULL *with* an exception set if an exception occurred.
1353 It returns NULL *without* an exception set if the key wasn't present.
1354*/
1355PyObject *
1356PyDict_GetItemWithError(PyObject *op, PyObject *key)
1357{
Victor Stinner742da042016-09-07 17:40:12 -07001358 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001359 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 PyDictObject*mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001361 PyObject *value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 if (!PyDict_Check(op)) {
1364 PyErr_BadInternalCall();
1365 return NULL;
1366 }
1367 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001368 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 {
1370 hash = PyObject_Hash(key);
1371 if (hash == -1) {
1372 return NULL;
1373 }
1374 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001375
INADA Naoki778928b2017-08-03 23:45:15 +09001376 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001377 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001379 return value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001380}
1381
Brett Cannonfd074152012-04-14 14:10:13 -04001382PyObject *
1383_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1384{
1385 PyObject *kv;
1386 kv = _PyUnicode_FromId(key); /* borrowed */
1387 if (kv == NULL)
1388 return NULL;
1389 return PyDict_GetItemWithError(dp, kv);
1390}
1391
Victor Stinnerb4efc962015-11-20 09:24:02 +01001392/* Fast version of global value lookup (LOAD_GLOBAL).
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001393 * Lookup in globals, then builtins.
Victor Stinnerb4efc962015-11-20 09:24:02 +01001394 *
1395 * Raise an exception and return NULL if an error occurred (ex: computing the
1396 * key hash failed, key comparison failed, ...). Return NULL if the key doesn't
1397 * exist. Return the value if the key exists.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001398 */
1399PyObject *
1400_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001401{
Victor Stinner742da042016-09-07 17:40:12 -07001402 Py_ssize_t ix;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001403 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09001404 PyObject *value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001405
1406 if (!PyUnicode_CheckExact(key) ||
1407 (hash = ((PyASCIIObject *) key)->hash) == -1)
1408 {
1409 hash = PyObject_Hash(key);
1410 if (hash == -1)
1411 return NULL;
Antoine Pitroue965d972012-02-27 00:45:12 +01001412 }
Victor Stinnerb4efc962015-11-20 09:24:02 +01001413
1414 /* namespace 1: globals */
INADA Naoki778928b2017-08-03 23:45:15 +09001415 ix = globals->ma_keys->dk_lookup(globals, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001416 if (ix == DKIX_ERROR)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001417 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001418 if (ix != DKIX_EMPTY && value != NULL)
1419 return value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001420
1421 /* namespace 2: builtins */
INADA Naoki778928b2017-08-03 23:45:15 +09001422 ix = builtins->ma_keys->dk_lookup(builtins, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001423 if (ix < 0)
Victor Stinnerb4efc962015-11-20 09:24:02 +01001424 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001425 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001426}
1427
Antoine Pitroue965d972012-02-27 00:45:12 +01001428/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1429 * dictionary if it's merely replacing the value for an existing key.
1430 * This means that it's safe to loop over a dictionary with PyDict_Next()
1431 * and occasionally replace a value -- but you can't insert new keys or
1432 * remove them.
1433 */
1434int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001435PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001436{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001437 PyDictObject *mp;
1438 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001439 if (!PyDict_Check(op)) {
1440 PyErr_BadInternalCall();
1441 return -1;
1442 }
1443 assert(key);
1444 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001445 mp = (PyDictObject *)op;
1446 if (!PyUnicode_CheckExact(key) ||
1447 (hash = ((PyASCIIObject *) key)->hash) == -1)
1448 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001449 hash = PyObject_Hash(key);
1450 if (hash == -1)
1451 return -1;
1452 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001453
1454 /* insertdict() handles any resizing that might be necessary */
1455 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001456}
1457
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001458int
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001459_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1460 Py_hash_t hash)
1461{
1462 PyDictObject *mp;
1463
1464 if (!PyDict_Check(op)) {
1465 PyErr_BadInternalCall();
1466 return -1;
1467 }
1468 assert(key);
1469 assert(value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001470 assert(hash != -1);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001471 mp = (PyDictObject *)op;
1472
1473 /* insertdict() handles any resizing that might be necessary */
1474 return insertdict(mp, key, hash, value);
1475}
1476
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001477static int
INADA Naoki778928b2017-08-03 23:45:15 +09001478delitem_common(PyDictObject *mp, Py_hash_t hash, Py_ssize_t ix,
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001479 PyObject *old_value)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001480{
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001481 PyObject *old_key;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001482 PyDictKeyEntry *ep;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001483
INADA Naoki778928b2017-08-03 23:45:15 +09001484 Py_ssize_t hashpos = lookdict_index(mp->ma_keys, hash, ix);
1485 assert(hashpos >= 0);
1486
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001487 mp->ma_used--;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001488 mp->ma_version_tag = DICT_NEXT_VERSION();
1489 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1490 dk_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
1491 ENSURE_ALLOWS_DELETIONS(mp);
1492 old_key = ep->me_key;
1493 ep->me_key = NULL;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001494 ep->me_value = NULL;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001495 Py_DECREF(old_key);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001496 Py_DECREF(old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001497
1498 assert(_PyDict_CheckConsistency(mp));
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001499 return 0;
1500}
1501
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001502int
Tim Peters1f5871e2000-07-04 17:44:48 +00001503PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001504{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001505 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 assert(key);
1507 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001508 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 hash = PyObject_Hash(key);
1510 if (hash == -1)
1511 return -1;
1512 }
Victor Stinner742da042016-09-07 17:40:12 -07001513
1514 return _PyDict_DelItem_KnownHash(op, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001515}
1516
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001517int
1518_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1519{
INADA Naoki778928b2017-08-03 23:45:15 +09001520 Py_ssize_t ix;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001521 PyDictObject *mp;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001522 PyObject *old_value;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001523
1524 if (!PyDict_Check(op)) {
1525 PyErr_BadInternalCall();
1526 return -1;
1527 }
1528 assert(key);
1529 assert(hash != -1);
1530 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001531 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001532 if (ix == DKIX_ERROR)
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001533 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09001534 if (ix == DKIX_EMPTY || old_value == NULL) {
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001535 _PyErr_SetKeyError(key);
1536 return -1;
1537 }
Victor Stinner78601a32016-09-09 19:28:36 -07001538
1539 // Split table doesn't allow deletion. Combine it.
1540 if (_PyDict_HasSplitTable(mp)) {
1541 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1542 return -1;
1543 }
INADA Naoki778928b2017-08-03 23:45:15 +09001544 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001545 assert(ix >= 0);
1546 }
1547
INADA Naoki778928b2017-08-03 23:45:15 +09001548 return delitem_common(mp, hash, ix, old_value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001549}
1550
Antoine Pitroud741ed42016-12-27 14:23:43 +01001551/* This function promises that the predicate -> deletion sequence is atomic
1552 * (i.e. protected by the GIL), assuming the predicate itself doesn't
1553 * release the GIL.
1554 */
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001555int
1556_PyDict_DelItemIf(PyObject *op, PyObject *key,
1557 int (*predicate)(PyObject *value))
1558{
Antoine Pitroud741ed42016-12-27 14:23:43 +01001559 Py_ssize_t hashpos, ix;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001560 PyDictObject *mp;
1561 Py_hash_t hash;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001562 PyObject *old_value;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001563 int res;
1564
1565 if (!PyDict_Check(op)) {
1566 PyErr_BadInternalCall();
1567 return -1;
1568 }
1569 assert(key);
1570 hash = PyObject_Hash(key);
1571 if (hash == -1)
1572 return -1;
1573 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001574 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001575 if (ix == DKIX_ERROR)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001576 return -1;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001577 if (ix == DKIX_EMPTY || old_value == NULL) {
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001578 _PyErr_SetKeyError(key);
1579 return -1;
1580 }
Antoine Pitroud741ed42016-12-27 14:23:43 +01001581
1582 // Split table doesn't allow deletion. Combine it.
1583 if (_PyDict_HasSplitTable(mp)) {
1584 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1585 return -1;
1586 }
INADA Naoki778928b2017-08-03 23:45:15 +09001587 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001588 assert(ix >= 0);
1589 }
1590
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001591 res = predicate(old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001592 if (res == -1)
1593 return -1;
INADA Naoki778928b2017-08-03 23:45:15 +09001594
1595 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1596 assert(hashpos >= 0);
1597
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001598 if (res > 0)
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001599 return delitem_common(mp, hashpos, ix, old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001600 else
1601 return 0;
1602}
1603
1604
Guido van Rossum25831651993-05-19 14:50:45 +00001605void
Tim Peters1f5871e2000-07-04 17:44:48 +00001606PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001607{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001609 PyDictKeysObject *oldkeys;
1610 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 if (!PyDict_Check(op))
1614 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001615 mp = ((PyDictObject *)op);
1616 oldkeys = mp->ma_keys;
1617 oldvalues = mp->ma_values;
1618 if (oldvalues == empty_values)
1619 return;
1620 /* Empty the dict... */
1621 DK_INCREF(Py_EMPTY_KEYS);
1622 mp->ma_keys = Py_EMPTY_KEYS;
1623 mp->ma_values = empty_values;
1624 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001625 mp->ma_version_tag = DICT_NEXT_VERSION();
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001626 /* ...then clear the keys and values */
1627 if (oldvalues != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001628 n = oldkeys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001629 for (i = 0; i < n; i++)
1630 Py_CLEAR(oldvalues[i]);
1631 free_values(oldvalues);
1632 DK_DECREF(oldkeys);
1633 }
1634 else {
1635 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001636 DK_DECREF(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001637 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02001638 assert(_PyDict_CheckConsistency(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001639}
1640
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001641/* Internal version of PyDict_Next that returns a hash value in addition
1642 * to the key and value.
1643 * Return 1 on success, return 0 when the reached the end of the dictionary
1644 * (or if op is not a dictionary)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001645 */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001646int
1647_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1648 PyObject **pvalue, Py_hash_t *phash)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001649{
INADA Naokica2d8be2016-11-04 16:59:10 +09001650 Py_ssize_t i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001651 PyDictObject *mp;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001652 PyDictKeyEntry *entry_ptr;
1653 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001654
1655 if (!PyDict_Check(op))
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001656 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 mp = (PyDictObject *)op;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001658 i = *ppos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001659 if (mp->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09001660 if (i < 0 || i >= mp->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001661 return 0;
INADA Naokica2d8be2016-11-04 16:59:10 +09001662 /* values of split table is always dense */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001663 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
INADA Naokica2d8be2016-11-04 16:59:10 +09001664 value = mp->ma_values[i];
1665 assert(value != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001667 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09001668 Py_ssize_t n = mp->ma_keys->dk_nentries;
1669 if (i < 0 || i >= n)
1670 return 0;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001671 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
1672 while (i < n && entry_ptr->me_value == NULL) {
1673 entry_ptr++;
1674 i++;
Victor Stinner742da042016-09-07 17:40:12 -07001675 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001676 if (i >= n)
1677 return 0;
1678 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001680 *ppos = i+1;
1681 if (pkey)
1682 *pkey = entry_ptr->me_key;
1683 if (phash)
1684 *phash = entry_ptr->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001685 if (pvalue)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001686 *pvalue = value;
1687 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001688}
1689
Tim Peters080c88b2003-02-15 03:01:11 +00001690/*
1691 * Iterate over a dict. Use like so:
1692 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001693 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001694 * PyObject *key, *value;
1695 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001696 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001697 * Refer to borrowed references in key and value.
Tim Peters080c88b2003-02-15 03:01:11 +00001698 * }
1699 *
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001700 * Return 1 on success, return 0 when the reached the end of the dictionary
1701 * (or if op is not a dictionary)
1702 *
Tim Peters080c88b2003-02-15 03:01:11 +00001703 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001704 * mutates the dict. One exception: it is safe if the loop merely changes
1705 * the values associated with the keys (but doesn't insert new keys or
1706 * delete keys), via PyDict_SetItem().
1707 */
Guido van Rossum25831651993-05-19 14:50:45 +00001708int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001709PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001710{
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001711 return _PyDict_Next(op, ppos, pkey, pvalue, NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001712}
1713
Eric Snow96c6af92015-05-29 22:21:39 -06001714/* Internal version of dict.pop(). */
1715PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001716_PyDict_Pop_KnownHash(PyObject *dict, PyObject *key, Py_hash_t hash, PyObject *deflt)
Eric Snow96c6af92015-05-29 22:21:39 -06001717{
Victor Stinner742da042016-09-07 17:40:12 -07001718 Py_ssize_t ix, hashpos;
Eric Snow96c6af92015-05-29 22:21:39 -06001719 PyObject *old_value, *old_key;
1720 PyDictKeyEntry *ep;
Yury Selivanov684ef2c2016-10-28 19:01:21 -04001721 PyDictObject *mp;
1722
1723 assert(PyDict_Check(dict));
1724 mp = (PyDictObject *)dict;
Eric Snow96c6af92015-05-29 22:21:39 -06001725
1726 if (mp->ma_used == 0) {
1727 if (deflt) {
1728 Py_INCREF(deflt);
1729 return deflt;
1730 }
1731 _PyErr_SetKeyError(key);
1732 return NULL;
1733 }
INADA Naoki778928b2017-08-03 23:45:15 +09001734 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001735 if (ix == DKIX_ERROR)
Eric Snow96c6af92015-05-29 22:21:39 -06001736 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001737 if (ix == DKIX_EMPTY || old_value == NULL) {
Eric Snow96c6af92015-05-29 22:21:39 -06001738 if (deflt) {
1739 Py_INCREF(deflt);
1740 return deflt;
1741 }
1742 _PyErr_SetKeyError(key);
1743 return NULL;
1744 }
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001745
Victor Stinner78601a32016-09-09 19:28:36 -07001746 // Split table doesn't allow deletion. Combine it.
1747 if (_PyDict_HasSplitTable(mp)) {
1748 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1749 return NULL;
1750 }
INADA Naoki778928b2017-08-03 23:45:15 +09001751 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001752 assert(ix >= 0);
1753 }
1754
INADA Naoki778928b2017-08-03 23:45:15 +09001755 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1756 assert(hashpos >= 0);
Victor Stinner78601a32016-09-09 19:28:36 -07001757 assert(old_value != NULL);
Eric Snow96c6af92015-05-29 22:21:39 -06001758 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001759 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner78601a32016-09-09 19:28:36 -07001760 dk_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
1761 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1762 ENSURE_ALLOWS_DELETIONS(mp);
1763 old_key = ep->me_key;
1764 ep->me_key = NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001765 ep->me_value = NULL;
Victor Stinner78601a32016-09-09 19:28:36 -07001766 Py_DECREF(old_key);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001767
1768 assert(_PyDict_CheckConsistency(mp));
Eric Snow96c6af92015-05-29 22:21:39 -06001769 return old_value;
1770}
1771
Serhiy Storchaka67796522017-01-12 18:34:33 +02001772PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001773_PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt)
Serhiy Storchaka67796522017-01-12 18:34:33 +02001774{
1775 Py_hash_t hash;
1776
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001777 if (((PyDictObject *)dict)->ma_used == 0) {
Serhiy Storchaka67796522017-01-12 18:34:33 +02001778 if (deflt) {
1779 Py_INCREF(deflt);
1780 return deflt;
1781 }
1782 _PyErr_SetKeyError(key);
1783 return NULL;
1784 }
1785 if (!PyUnicode_CheckExact(key) ||
1786 (hash = ((PyASCIIObject *) key)->hash) == -1) {
1787 hash = PyObject_Hash(key);
1788 if (hash == -1)
1789 return NULL;
1790 }
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001791 return _PyDict_Pop_KnownHash(dict, key, hash, deflt);
Serhiy Storchaka67796522017-01-12 18:34:33 +02001792}
1793
Eric Snow96c6af92015-05-29 22:21:39 -06001794/* Internal version of dict.from_keys(). It is subclass-friendly. */
1795PyObject *
1796_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1797{
1798 PyObject *it; /* iter(iterable) */
1799 PyObject *key;
1800 PyObject *d;
1801 int status;
1802
Victor Stinnera5ed5f02016-12-06 18:45:50 +01001803 d = _PyObject_CallNoArg(cls);
Eric Snow96c6af92015-05-29 22:21:39 -06001804 if (d == NULL)
1805 return NULL;
1806
1807 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
1808 if (PyDict_CheckExact(iterable)) {
1809 PyDictObject *mp = (PyDictObject *)d;
1810 PyObject *oldvalue;
1811 Py_ssize_t pos = 0;
1812 PyObject *key;
1813 Py_hash_t hash;
1814
Serhiy Storchakac61ac162017-03-21 08:52:38 +02001815 if (dictresize(mp, ESTIMATE_SIZE(PyDict_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001816 Py_DECREF(d);
1817 return NULL;
1818 }
1819
1820 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
1821 if (insertdict(mp, key, hash, value)) {
1822 Py_DECREF(d);
1823 return NULL;
1824 }
1825 }
1826 return d;
1827 }
1828 if (PyAnySet_CheckExact(iterable)) {
1829 PyDictObject *mp = (PyDictObject *)d;
1830 Py_ssize_t pos = 0;
1831 PyObject *key;
1832 Py_hash_t hash;
1833
Victor Stinner742da042016-09-07 17:40:12 -07001834 if (dictresize(mp, ESTIMATE_SIZE(PySet_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001835 Py_DECREF(d);
1836 return NULL;
1837 }
1838
1839 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
1840 if (insertdict(mp, key, hash, value)) {
1841 Py_DECREF(d);
1842 return NULL;
1843 }
1844 }
1845 return d;
1846 }
1847 }
1848
1849 it = PyObject_GetIter(iterable);
1850 if (it == NULL){
1851 Py_DECREF(d);
1852 return NULL;
1853 }
1854
1855 if (PyDict_CheckExact(d)) {
1856 while ((key = PyIter_Next(it)) != NULL) {
1857 status = PyDict_SetItem(d, key, value);
1858 Py_DECREF(key);
1859 if (status < 0)
1860 goto Fail;
1861 }
1862 } else {
1863 while ((key = PyIter_Next(it)) != NULL) {
1864 status = PyObject_SetItem(d, key, value);
1865 Py_DECREF(key);
1866 if (status < 0)
1867 goto Fail;
1868 }
1869 }
1870
1871 if (PyErr_Occurred())
1872 goto Fail;
1873 Py_DECREF(it);
1874 return d;
1875
1876Fail:
1877 Py_DECREF(it);
1878 Py_DECREF(d);
1879 return NULL;
1880}
1881
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001882/* Methods */
1883
1884static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001885dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001886{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001887 PyObject **values = mp->ma_values;
1888 PyDictKeysObject *keys = mp->ma_keys;
1889 Py_ssize_t i, n;
INADA Naokia6296d32017-08-24 14:55:17 +09001890
1891 /* bpo-31095: UnTrack is needed before calling any callbacks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 PyObject_GC_UnTrack(mp);
1893 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001894 if (values != NULL) {
1895 if (values != empty_values) {
Victor Stinner742da042016-09-07 17:40:12 -07001896 for (i = 0, n = mp->ma_keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001897 Py_XDECREF(values[i]);
1898 }
1899 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001901 DK_DECREF(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02001903 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001904 assert(keys->dk_refcnt == 1);
1905 DK_DECREF(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001906 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001907 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1908 free_list[numfree++] = mp;
1909 else
1910 Py_TYPE(mp)->tp_free((PyObject *)mp);
1911 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001912}
1913
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001914
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001915static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001916dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001917{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001919 PyObject *key = NULL, *value = NULL;
1920 _PyUnicodeWriter writer;
1921 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00001922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001923 i = Py_ReprEnter((PyObject *)mp);
1924 if (i != 0) {
1925 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1926 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001929 Py_ReprLeave((PyObject *)mp);
1930 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 }
Tim Petersa7259592001-06-16 05:11:17 +00001932
Victor Stinnerf91929b2013-11-19 13:07:38 +01001933 _PyUnicodeWriter_Init(&writer);
1934 writer.overallocate = 1;
1935 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
1936 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00001937
Victor Stinnerf91929b2013-11-19 13:07:38 +01001938 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
1939 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001940
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 /* Do repr() on each key+value pair, and insert ": " between them.
1942 Note that repr may mutate the dict. */
1943 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001944 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001946 PyObject *s;
1947 int res;
1948
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001949 /* Prevent repr from deleting key or value during key format. */
1950 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02001952
Victor Stinnerf91929b2013-11-19 13:07:38 +01001953 if (!first) {
1954 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
1955 goto error;
1956 }
1957 first = 0;
1958
1959 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01001961 goto error;
1962 res = _PyUnicodeWriter_WriteStr(&writer, s);
1963 Py_DECREF(s);
1964 if (res < 0)
1965 goto error;
1966
1967 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
1968 goto error;
1969
1970 s = PyObject_Repr(value);
1971 if (s == NULL)
1972 goto error;
1973 res = _PyUnicodeWriter_WriteStr(&writer, s);
1974 Py_DECREF(s);
1975 if (res < 0)
1976 goto error;
1977
1978 Py_CLEAR(key);
1979 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 }
Tim Petersa7259592001-06-16 05:11:17 +00001981
Victor Stinnerf91929b2013-11-19 13:07:38 +01001982 writer.overallocate = 0;
1983 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
1984 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001986 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01001987
1988 return _PyUnicodeWriter_Finish(&writer);
1989
1990error:
1991 Py_ReprLeave((PyObject *)mp);
1992 _PyUnicodeWriter_Dealloc(&writer);
1993 Py_XDECREF(key);
1994 Py_XDECREF(value);
1995 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001996}
1997
Martin v. Löwis18e16552006-02-15 17:27:45 +00001998static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001999dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002000{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002001 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002002}
2003
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002004static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002005dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002006{
Victor Stinner742da042016-09-07 17:40:12 -07002007 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002008 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09002009 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002011 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002012 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002013 hash = PyObject_Hash(key);
2014 if (hash == -1)
2015 return NULL;
2016 }
INADA Naoki778928b2017-08-03 23:45:15 +09002017 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002018 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002019 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002020 if (ix == DKIX_EMPTY || value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002021 if (!PyDict_CheckExact(mp)) {
2022 /* Look up __missing__ method if we're a subclass. */
2023 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05002024 _Py_IDENTIFIER(__missing__);
2025 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002026 if (missing != NULL) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002027 res = PyObject_CallFunctionObjArgs(missing,
2028 key, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002029 Py_DECREF(missing);
2030 return res;
2031 }
2032 else if (PyErr_Occurred())
2033 return NULL;
2034 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07002035 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002036 return NULL;
2037 }
INADA Naokiba609772016-12-07 20:41:42 +09002038 Py_INCREF(value);
2039 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002040}
2041
2042static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002043dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002044{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045 if (w == NULL)
2046 return PyDict_DelItem((PyObject *)mp, v);
2047 else
2048 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002049}
2050
Guido van Rossuma9e7a811997-05-13 21:02:11 +00002051static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002052 (lenfunc)dict_length, /*mp_length*/
2053 (binaryfunc)dict_subscript, /*mp_subscript*/
2054 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002055};
2056
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002057static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002058dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002059{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002060 PyObject *v;
2061 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002062 PyDictKeyEntry *ep;
2063 Py_ssize_t size, n, offset;
2064 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002065
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002066 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 n = mp->ma_used;
2068 v = PyList_New(n);
2069 if (v == NULL)
2070 return NULL;
2071 if (n != mp->ma_used) {
2072 /* Durnit. The allocations caused the dict to resize.
2073 * Just start over, this shouldn't normally happen.
2074 */
2075 Py_DECREF(v);
2076 goto again;
2077 }
Victor Stinner742da042016-09-07 17:40:12 -07002078 ep = DK_ENTRIES(mp->ma_keys);
2079 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002080 if (mp->ma_values) {
2081 value_ptr = mp->ma_values;
2082 offset = sizeof(PyObject *);
2083 }
2084 else {
2085 value_ptr = &ep[0].me_value;
2086 offset = sizeof(PyDictKeyEntry);
2087 }
2088 for (i = 0, j = 0; i < size; i++) {
2089 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 PyObject *key = ep[i].me_key;
2091 Py_INCREF(key);
2092 PyList_SET_ITEM(v, j, key);
2093 j++;
2094 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002095 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096 }
2097 assert(j == n);
2098 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002099}
2100
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002101static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002102dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002103{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002104 PyObject *v;
2105 Py_ssize_t i, j;
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002106 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002107 Py_ssize_t size, n, offset;
2108 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002109
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002110 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002111 n = mp->ma_used;
2112 v = PyList_New(n);
2113 if (v == NULL)
2114 return NULL;
2115 if (n != mp->ma_used) {
2116 /* Durnit. The allocations caused the dict to resize.
2117 * Just start over, this shouldn't normally happen.
2118 */
2119 Py_DECREF(v);
2120 goto again;
2121 }
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002122 ep = DK_ENTRIES(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002123 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002124 if (mp->ma_values) {
2125 value_ptr = mp->ma_values;
2126 offset = sizeof(PyObject *);
2127 }
2128 else {
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002129 value_ptr = &ep[0].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002130 offset = sizeof(PyDictKeyEntry);
2131 }
2132 for (i = 0, j = 0; i < size; i++) {
2133 PyObject *value = *value_ptr;
2134 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2135 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002136 Py_INCREF(value);
2137 PyList_SET_ITEM(v, j, value);
2138 j++;
2139 }
2140 }
2141 assert(j == n);
2142 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002143}
2144
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002145static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002146dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002147{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002148 PyObject *v;
2149 Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002150 Py_ssize_t size, offset;
2151 PyObject *item, *key;
2152 PyDictKeyEntry *ep;
2153 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002155 /* Preallocate the list of tuples, to avoid allocations during
2156 * the loop over the items, which could trigger GC, which
2157 * could resize the dict. :-(
2158 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002159 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002160 n = mp->ma_used;
2161 v = PyList_New(n);
2162 if (v == NULL)
2163 return NULL;
2164 for (i = 0; i < n; i++) {
2165 item = PyTuple_New(2);
2166 if (item == NULL) {
2167 Py_DECREF(v);
2168 return NULL;
2169 }
2170 PyList_SET_ITEM(v, i, item);
2171 }
2172 if (n != mp->ma_used) {
2173 /* Durnit. The allocations caused the dict to resize.
2174 * Just start over, this shouldn't normally happen.
2175 */
2176 Py_DECREF(v);
2177 goto again;
2178 }
2179 /* Nothing we do below makes any function calls. */
Victor Stinner742da042016-09-07 17:40:12 -07002180 ep = DK_ENTRIES(mp->ma_keys);
2181 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002182 if (mp->ma_values) {
2183 value_ptr = mp->ma_values;
2184 offset = sizeof(PyObject *);
2185 }
2186 else {
2187 value_ptr = &ep[0].me_value;
2188 offset = sizeof(PyDictKeyEntry);
2189 }
2190 for (i = 0, j = 0; i < size; i++) {
2191 PyObject *value = *value_ptr;
2192 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2193 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 key = ep[i].me_key;
2195 item = PyList_GET_ITEM(v, j);
2196 Py_INCREF(key);
2197 PyTuple_SET_ITEM(item, 0, key);
2198 Py_INCREF(value);
2199 PyTuple_SET_ITEM(item, 1, value);
2200 j++;
2201 }
2202 }
2203 assert(j == n);
2204 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002205}
2206
Larry Hastings5c661892014-01-24 06:17:25 -08002207/*[clinic input]
2208@classmethod
2209dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08002210 iterable: object
2211 value: object=None
2212 /
2213
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002214Create a new dictionary with keys from iterable and values set to value.
Larry Hastings5c661892014-01-24 06:17:25 -08002215[clinic start generated code]*/
2216
Larry Hastings5c661892014-01-24 06:17:25 -08002217static PyObject *
2218dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002219/*[clinic end generated code: output=8fb98e4b10384999 input=382ba4855d0f74c3]*/
Larry Hastings5c661892014-01-24 06:17:25 -08002220{
Eric Snow96c6af92015-05-29 22:21:39 -06002221 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002222}
2223
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002224static int
Victor Stinner742da042016-09-07 17:40:12 -07002225dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
2226 const char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002227{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 PyObject *arg = NULL;
2229 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002230
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002231 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002232 result = -1;
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002233 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002234 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002235 _Py_IDENTIFIER(keys);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002236 PyObject *func;
2237 if (_PyObject_LookupAttrId(arg, &PyId_keys, &func) < 0) {
2238 result = -1;
2239 }
2240 else if (func != NULL) {
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002241 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002242 result = PyDict_Merge(self, arg, 1);
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002243 }
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002244 else {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002245 result = PyDict_MergeFromSeq2(self, arg, 1);
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002246 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002247 }
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002249 if (result == 0 && kwds != NULL) {
2250 if (PyArg_ValidateKeywordArguments(kwds))
2251 result = PyDict_Merge(self, kwds, 1);
2252 else
2253 result = -1;
2254 }
2255 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002256}
2257
Victor Stinner91f0d4a2017-01-19 12:45:06 +01002258/* Note: dict.update() uses the METH_VARARGS|METH_KEYWORDS calling convention.
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002259 Using METH_FASTCALL|METH_KEYWORDS would make dict.update(**dict2) calls
2260 slower, see the issue #29312. */
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002261static PyObject *
2262dict_update(PyObject *self, PyObject *args, PyObject *kwds)
2263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002264 if (dict_update_common(self, args, kwds, "update") != -1)
2265 Py_RETURN_NONE;
2266 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002267}
2268
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002269/* Update unconditionally replaces existing items.
2270 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00002271 otherwise it leaves existing items unchanged.
2272
2273 PyDict_{Update,Merge} update/merge from a mapping object.
2274
Tim Petersf582b822001-12-11 18:51:08 +00002275 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00002276 producing iterable objects of length 2.
2277*/
2278
Tim Petersf582b822001-12-11 18:51:08 +00002279int
Tim Peters1fc240e2001-10-26 05:06:50 +00002280PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
2281{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002282 PyObject *it; /* iter(seq2) */
2283 Py_ssize_t i; /* index into seq2 of current element */
2284 PyObject *item; /* seq2[i] */
2285 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00002286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287 assert(d != NULL);
2288 assert(PyDict_Check(d));
2289 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00002290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 it = PyObject_GetIter(seq2);
2292 if (it == NULL)
2293 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002295 for (i = 0; ; ++i) {
2296 PyObject *key, *value;
2297 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00002298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002299 fast = NULL;
2300 item = PyIter_Next(it);
2301 if (item == NULL) {
2302 if (PyErr_Occurred())
2303 goto Fail;
2304 break;
2305 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 /* Convert item to sequence, and verify length 2. */
2308 fast = PySequence_Fast(item, "");
2309 if (fast == NULL) {
2310 if (PyErr_ExceptionMatches(PyExc_TypeError))
2311 PyErr_Format(PyExc_TypeError,
2312 "cannot convert dictionary update "
2313 "sequence element #%zd to a sequence",
2314 i);
2315 goto Fail;
2316 }
2317 n = PySequence_Fast_GET_SIZE(fast);
2318 if (n != 2) {
2319 PyErr_Format(PyExc_ValueError,
2320 "dictionary update sequence element #%zd "
2321 "has length %zd; 2 is required",
2322 i, n);
2323 goto Fail;
2324 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002326 /* Update/merge with this (key, value) pair. */
2327 key = PySequence_Fast_GET_ITEM(fast, 0);
2328 value = PySequence_Fast_GET_ITEM(fast, 1);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002329 Py_INCREF(key);
2330 Py_INCREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002331 if (override || PyDict_GetItem(d, key) == NULL) {
2332 int status = PyDict_SetItem(d, key, value);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002333 if (status < 0) {
2334 Py_DECREF(key);
2335 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 goto Fail;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002337 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002338 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002339 Py_DECREF(key);
2340 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002341 Py_DECREF(fast);
2342 Py_DECREF(item);
2343 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002345 i = 0;
Victor Stinner611b0fa2016-09-14 15:02:01 +02002346 assert(_PyDict_CheckConsistency((PyDictObject *)d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002347 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002348Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002349 Py_XDECREF(item);
2350 Py_XDECREF(fast);
2351 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002352Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353 Py_DECREF(it);
2354 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002355}
2356
doko@ubuntu.comc96df682016-10-11 08:04:02 +02002357static int
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002358dict_merge(PyObject *a, PyObject *b, int override)
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002359{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002360 PyDictObject *mp, *other;
2361 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -07002362 PyDictKeyEntry *entry, *ep0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002363
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002364 assert(0 <= override && override <= 2);
2365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 /* We accept for the argument either a concrete dictionary object,
2367 * or an abstract "mapping" object. For the former, we can do
2368 * things quite efficiently. For the latter, we only require that
2369 * PyMapping_Keys() and PyObject_GetItem() be supported.
2370 */
2371 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2372 PyErr_BadInternalCall();
2373 return -1;
2374 }
2375 mp = (PyDictObject*)a;
2376 if (PyDict_Check(b)) {
2377 other = (PyDictObject*)b;
2378 if (other == mp || other->ma_used == 0)
2379 /* a.update(a) or a.update({}); nothing to do */
2380 return 0;
2381 if (mp->ma_used == 0)
2382 /* Since the target dict is empty, PyDict_GetItem()
2383 * always returns NULL. Setting override to 1
2384 * skips the unnecessary test.
2385 */
2386 override = 1;
2387 /* Do one big resize at the start, rather than
2388 * incrementally resizing as we insert new items. Expect
2389 * that there will be no (or few) overlapping keys.
2390 */
INADA Naokib1152be2016-10-27 19:26:50 +09002391 if (USABLE_FRACTION(mp->ma_keys->dk_size) < other->ma_used) {
2392 if (dictresize(mp, ESTIMATE_SIZE(mp->ma_used + other->ma_used))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 return -1;
INADA Naokib1152be2016-10-27 19:26:50 +09002394 }
2395 }
Victor Stinner742da042016-09-07 17:40:12 -07002396 ep0 = DK_ENTRIES(other->ma_keys);
2397 for (i = 0, n = other->ma_keys->dk_nentries; i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002398 PyObject *key, *value;
2399 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002400 entry = &ep0[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002401 key = entry->me_key;
2402 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002403 if (other->ma_values)
2404 value = other->ma_values[i];
2405 else
2406 value = entry->me_value;
2407
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002408 if (value != NULL) {
2409 int err = 0;
2410 Py_INCREF(key);
2411 Py_INCREF(value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002412 if (override == 1)
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002413 err = insertdict(mp, key, hash, value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002414 else if (_PyDict_GetItem_KnownHash(a, key, hash) == NULL) {
2415 if (PyErr_Occurred()) {
2416 Py_DECREF(value);
2417 Py_DECREF(key);
2418 return -1;
2419 }
2420 err = insertdict(mp, key, hash, value);
2421 }
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002422 else if (override != 0) {
2423 _PyErr_SetKeyError(key);
2424 Py_DECREF(value);
2425 Py_DECREF(key);
2426 return -1;
2427 }
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002428 Py_DECREF(value);
2429 Py_DECREF(key);
2430 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002431 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002432
Victor Stinner742da042016-09-07 17:40:12 -07002433 if (n != other->ma_keys->dk_nentries) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002434 PyErr_SetString(PyExc_RuntimeError,
2435 "dict mutated during update");
2436 return -1;
2437 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002438 }
2439 }
2440 }
2441 else {
2442 /* Do it the generic, slower way */
2443 PyObject *keys = PyMapping_Keys(b);
2444 PyObject *iter;
2445 PyObject *key, *value;
2446 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002448 if (keys == NULL)
2449 /* Docstring says this is equivalent to E.keys() so
2450 * if E doesn't have a .keys() method we want
2451 * AttributeError to percolate up. Might as well
2452 * do the same for any other error.
2453 */
2454 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002456 iter = PyObject_GetIter(keys);
2457 Py_DECREF(keys);
2458 if (iter == NULL)
2459 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002461 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002462 if (override != 1 && PyDict_GetItem(a, key) != NULL) {
2463 if (override != 0) {
2464 _PyErr_SetKeyError(key);
2465 Py_DECREF(key);
2466 Py_DECREF(iter);
2467 return -1;
2468 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002469 Py_DECREF(key);
2470 continue;
2471 }
2472 value = PyObject_GetItem(b, key);
2473 if (value == NULL) {
2474 Py_DECREF(iter);
2475 Py_DECREF(key);
2476 return -1;
2477 }
2478 status = PyDict_SetItem(a, key, value);
2479 Py_DECREF(key);
2480 Py_DECREF(value);
2481 if (status < 0) {
2482 Py_DECREF(iter);
2483 return -1;
2484 }
2485 }
2486 Py_DECREF(iter);
2487 if (PyErr_Occurred())
2488 /* Iterator completed, via error */
2489 return -1;
2490 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02002491 assert(_PyDict_CheckConsistency((PyDictObject *)a));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002492 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002493}
2494
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002495int
2496PyDict_Update(PyObject *a, PyObject *b)
2497{
2498 return dict_merge(a, b, 1);
2499}
2500
2501int
2502PyDict_Merge(PyObject *a, PyObject *b, int override)
2503{
2504 /* XXX Deprecate override not in (0, 1). */
2505 return dict_merge(a, b, override != 0);
2506}
2507
2508int
2509_PyDict_MergeEx(PyObject *a, PyObject *b, int override)
2510{
2511 return dict_merge(a, b, override);
2512}
2513
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002514static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002515dict_copy(PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002516{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002517 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002518}
2519
2520PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002521PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002522{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002523 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002524 PyDictObject *mp;
2525 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002527 if (o == NULL || !PyDict_Check(o)) {
2528 PyErr_BadInternalCall();
2529 return NULL;
2530 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002531
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002532 mp = (PyDictObject *)o;
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002533 if (mp->ma_used == 0) {
2534 /* The dict is empty; just return a new dict. */
2535 return PyDict_New();
2536 }
2537
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002538 if (_PyDict_HasSplitTable(mp)) {
2539 PyDictObject *split_copy;
Victor Stinner742da042016-09-07 17:40:12 -07002540 Py_ssize_t size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
2541 PyObject **newvalues;
2542 newvalues = new_values(size);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002543 if (newvalues == NULL)
2544 return PyErr_NoMemory();
2545 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2546 if (split_copy == NULL) {
2547 free_values(newvalues);
2548 return NULL;
2549 }
2550 split_copy->ma_values = newvalues;
2551 split_copy->ma_keys = mp->ma_keys;
2552 split_copy->ma_used = mp->ma_used;
Miss Islington (bot)de755122018-04-02 20:00:26 -07002553 split_copy->ma_version_tag = DICT_NEXT_VERSION();
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002554 DK_INCREF(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002555 for (i = 0, n = size; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002556 PyObject *value = mp->ma_values[i];
2557 Py_XINCREF(value);
2558 split_copy->ma_values[i] = value;
2559 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002560 if (_PyObject_GC_IS_TRACKED(mp))
2561 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002562 return (PyObject *)split_copy;
2563 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002564
2565 if (PyDict_CheckExact(mp) && mp->ma_values == NULL &&
2566 (mp->ma_used >= (mp->ma_keys->dk_nentries * 2) / 3))
2567 {
2568 /* Use fast-copy if:
2569
2570 (1) 'mp' is an instance of a subclassed dict; and
2571
2572 (2) 'mp' is not a split-dict; and
2573
2574 (3) if 'mp' is non-compact ('del' operation does not resize dicts),
2575 do fast-copy only if it has at most 1/3 non-used keys.
2576
2577 The last condition (3) is important to guard against a pathalogical
2578 case when a large dict is almost emptied with multiple del/pop
2579 operations and copied after that. In cases like this, we defer to
2580 PyDict_Merge, which produces a compacted copy.
2581 */
2582 return clone_combined_dict(mp);
2583 }
2584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002585 copy = PyDict_New();
2586 if (copy == NULL)
2587 return NULL;
2588 if (PyDict_Merge(copy, o, 1) == 0)
2589 return copy;
2590 Py_DECREF(copy);
2591 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002592}
2593
Martin v. Löwis18e16552006-02-15 17:27:45 +00002594Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002595PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002596{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002597 if (mp == NULL || !PyDict_Check(mp)) {
2598 PyErr_BadInternalCall();
2599 return -1;
2600 }
2601 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002602}
2603
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002604PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002605PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002606{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002607 if (mp == NULL || !PyDict_Check(mp)) {
2608 PyErr_BadInternalCall();
2609 return NULL;
2610 }
2611 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002612}
2613
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002614PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002615PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002616{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002617 if (mp == NULL || !PyDict_Check(mp)) {
2618 PyErr_BadInternalCall();
2619 return NULL;
2620 }
2621 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002622}
2623
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002624PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002625PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002626{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002627 if (mp == NULL || !PyDict_Check(mp)) {
2628 PyErr_BadInternalCall();
2629 return NULL;
2630 }
2631 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002632}
2633
Tim Peterse63415e2001-05-08 04:38:29 +00002634/* Return 1 if dicts equal, 0 if not, -1 if error.
2635 * Gets out as soon as any difference is detected.
2636 * Uses only Py_EQ comparison.
2637 */
2638static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002639dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002640{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002641 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002643 if (a->ma_used != b->ma_used)
2644 /* can't be equal if # of entries differ */
2645 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002646 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Victor Stinner742da042016-09-07 17:40:12 -07002647 for (i = 0; i < a->ma_keys->dk_nentries; i++) {
2648 PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002649 PyObject *aval;
2650 if (a->ma_values)
2651 aval = a->ma_values[i];
2652 else
2653 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002654 if (aval != NULL) {
2655 int cmp;
2656 PyObject *bval;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002657 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002658 /* temporarily bump aval's refcount to ensure it stays
2659 alive until we're done with it */
2660 Py_INCREF(aval);
2661 /* ditto for key */
2662 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002663 /* reuse the known hash value */
INADA Naoki778928b2017-08-03 23:45:15 +09002664 b->ma_keys->dk_lookup(b, key, ep->me_hash, &bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002665 if (bval == NULL) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002666 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002667 Py_DECREF(aval);
2668 if (PyErr_Occurred())
2669 return -1;
2670 return 0;
2671 }
2672 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002673 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002674 Py_DECREF(aval);
2675 if (cmp <= 0) /* error or not equal */
2676 return cmp;
2677 }
2678 }
2679 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002680}
Tim Peterse63415e2001-05-08 04:38:29 +00002681
2682static PyObject *
2683dict_richcompare(PyObject *v, PyObject *w, int op)
2684{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002685 int cmp;
2686 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002688 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2689 res = Py_NotImplemented;
2690 }
2691 else if (op == Py_EQ || op == Py_NE) {
2692 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2693 if (cmp < 0)
2694 return NULL;
2695 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2696 }
2697 else
2698 res = Py_NotImplemented;
2699 Py_INCREF(res);
2700 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002701}
Tim Peterse63415e2001-05-08 04:38:29 +00002702
Larry Hastings61272b72014-01-07 12:41:53 -08002703/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002704
2705@coexist
2706dict.__contains__
2707
2708 key: object
2709 /
2710
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002711True if the dictionary has the specified key, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002712[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002713
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002714static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002715dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka19d25972017-02-04 08:05:07 +02002716/*[clinic end generated code: output=a3d03db709ed6e6b input=fe1cb42ad831e820]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002717{
Larry Hastingsc2047262014-01-25 20:43:29 -08002718 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002719 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002720 Py_ssize_t ix;
INADA Naokiba609772016-12-07 20:41:42 +09002721 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002723 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002724 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002725 hash = PyObject_Hash(key);
2726 if (hash == -1)
2727 return NULL;
2728 }
INADA Naoki778928b2017-08-03 23:45:15 +09002729 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002730 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002731 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002732 if (ix == DKIX_EMPTY || value == NULL)
Victor Stinner742da042016-09-07 17:40:12 -07002733 Py_RETURN_FALSE;
2734 Py_RETURN_TRUE;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002735}
2736
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002737/*[clinic input]
2738dict.get
2739
2740 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002741 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002742 /
2743
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002744Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002745[clinic start generated code]*/
2746
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002747static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002748dict_get_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002749/*[clinic end generated code: output=bba707729dee05bf input=279ddb5790b6b107]*/
Barry Warsawc38c5da1997-10-06 17:49:20 +00002750{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002752 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002753 Py_ssize_t ix;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002755 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002756 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 hash = PyObject_Hash(key);
2758 if (hash == -1)
2759 return NULL;
2760 }
INADA Naoki778928b2017-08-03 23:45:15 +09002761 ix = (self->ma_keys->dk_lookup) (self, key, hash, &val);
Victor Stinner742da042016-09-07 17:40:12 -07002762 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002763 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002764 if (ix == DKIX_EMPTY || val == NULL) {
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002765 val = default_value;
INADA Naokiba609772016-12-07 20:41:42 +09002766 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002767 Py_INCREF(val);
2768 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002769}
2770
Benjamin Peterson00e98862013-03-07 22:16:29 -05002771PyObject *
2772PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002773{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002774 PyDictObject *mp = (PyDictObject *)d;
INADA Naoki93f26f72016-11-02 18:45:16 +09002775 PyObject *value;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002776 Py_hash_t hash;
Guido van Rossum164452c2000-08-08 16:12:54 +00002777
Benjamin Peterson00e98862013-03-07 22:16:29 -05002778 if (!PyDict_Check(d)) {
2779 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002780 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002781 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002782
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002783 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002784 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002785 hash = PyObject_Hash(key);
2786 if (hash == -1)
2787 return NULL;
2788 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002789
2790 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
2791 if (insertion_resize(mp) < 0)
2792 return NULL;
2793 }
2794
INADA Naoki778928b2017-08-03 23:45:15 +09002795 Py_ssize_t ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002796 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002797 return NULL;
INADA Naoki93f26f72016-11-02 18:45:16 +09002798
2799 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09002800 ((ix >= 0 && value == NULL && mp->ma_used != ix) ||
INADA Naoki93f26f72016-11-02 18:45:16 +09002801 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
2802 if (insertion_resize(mp) < 0) {
2803 return NULL;
2804 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002805 ix = DKIX_EMPTY;
2806 }
2807
2808 if (ix == DKIX_EMPTY) {
2809 PyDictKeyEntry *ep, *ep0;
2810 value = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002811 if (mp->ma_keys->dk_usable <= 0) {
Victor Stinner3c336c52016-09-12 14:17:40 +02002812 if (insertion_resize(mp) < 0) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002813 return NULL;
Victor Stinner3c336c52016-09-12 14:17:40 +02002814 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002815 }
INADA Naoki778928b2017-08-03 23:45:15 +09002816 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naoki93f26f72016-11-02 18:45:16 +09002817 ep0 = DK_ENTRIES(mp->ma_keys);
2818 ep = &ep0[mp->ma_keys->dk_nentries];
2819 dk_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002820 Py_INCREF(key);
INADA Naoki93f26f72016-11-02 18:45:16 +09002821 Py_INCREF(value);
2822 MAINTAIN_TRACKING(mp, key, value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002823 ep->me_key = key;
2824 ep->me_hash = hash;
INADA Naokiba609772016-12-07 20:41:42 +09002825 if (_PyDict_HasSplitTable(mp)) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002826 assert(mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
2827 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Victor Stinner742da042016-09-07 17:40:12 -07002828 }
2829 else {
INADA Naoki93f26f72016-11-02 18:45:16 +09002830 ep->me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07002831 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002832 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002833 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naoki93f26f72016-11-02 18:45:16 +09002834 mp->ma_keys->dk_usable--;
2835 mp->ma_keys->dk_nentries++;
2836 assert(mp->ma_keys->dk_usable >= 0);
2837 }
INADA Naokiba609772016-12-07 20:41:42 +09002838 else if (value == NULL) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002839 value = defaultobj;
2840 assert(_PyDict_HasSplitTable(mp));
2841 assert(ix == mp->ma_used);
2842 Py_INCREF(value);
2843 MAINTAIN_TRACKING(mp, key, value);
INADA Naokiba609772016-12-07 20:41:42 +09002844 mp->ma_values[ix] = value;
INADA Naoki93f26f72016-11-02 18:45:16 +09002845 mp->ma_used++;
2846 mp->ma_version_tag = DICT_NEXT_VERSION();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002847 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002848
2849 assert(_PyDict_CheckConsistency(mp));
2850 return value;
Guido van Rossum164452c2000-08-08 16:12:54 +00002851}
2852
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002853/*[clinic input]
2854dict.setdefault
2855
2856 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002857 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002858 /
2859
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002860Insert key with a value of default if key is not in the dictionary.
2861
2862Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002863[clinic start generated code]*/
2864
Benjamin Peterson00e98862013-03-07 22:16:29 -05002865static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002866dict_setdefault_impl(PyDictObject *self, PyObject *key,
2867 PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002868/*[clinic end generated code: output=f8c1101ebf69e220 input=0f063756e815fd9d]*/
Benjamin Peterson00e98862013-03-07 22:16:29 -05002869{
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002870 PyObject *val;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002871
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002872 val = PyDict_SetDefault((PyObject *)self, key, default_value);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002873 Py_XINCREF(val);
2874 return val;
2875}
Guido van Rossum164452c2000-08-08 16:12:54 +00002876
2877static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002878dict_clear(PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002879{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 PyDict_Clear((PyObject *)mp);
2881 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002882}
2883
Guido van Rossumba6ab842000-12-12 22:02:18 +00002884static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002885dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002886{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002887 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00002888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002889 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2890 return NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06002891
Yury Selivanov684ef2c2016-10-28 19:01:21 -04002892 return _PyDict_Pop((PyObject*)mp, key, deflt);
Guido van Rossume027d982002-04-12 15:11:59 +00002893}
2894
2895static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002896dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002897{
Victor Stinner742da042016-09-07 17:40:12 -07002898 Py_ssize_t i, j;
2899 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002900 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002902 /* Allocate the result tuple before checking the size. Believe it
2903 * or not, this allocation could trigger a garbage collection which
2904 * could empty the dict, so if we checked the size first and that
2905 * happened, the result would be an infinite loop (searching for an
2906 * entry that no longer exists). Note that the usual popitem()
2907 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2908 * tuple away if the dict *is* empty isn't a significant
2909 * inefficiency -- possible, but unlikely in practice.
2910 */
2911 res = PyTuple_New(2);
2912 if (res == NULL)
2913 return NULL;
2914 if (mp->ma_used == 0) {
2915 Py_DECREF(res);
2916 PyErr_SetString(PyExc_KeyError,
2917 "popitem(): dictionary is empty");
2918 return NULL;
2919 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002920 /* Convert split table to combined table */
2921 if (mp->ma_keys->dk_lookup == lookdict_split) {
2922 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2923 Py_DECREF(res);
2924 return NULL;
2925 }
2926 }
2927 ENSURE_ALLOWS_DELETIONS(mp);
Victor Stinner742da042016-09-07 17:40:12 -07002928
2929 /* Pop last item */
2930 ep0 = DK_ENTRIES(mp->ma_keys);
2931 i = mp->ma_keys->dk_nentries - 1;
2932 while (i >= 0 && ep0[i].me_value == NULL) {
2933 i--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002934 }
Victor Stinner742da042016-09-07 17:40:12 -07002935 assert(i >= 0);
2936
2937 ep = &ep0[i];
2938 j = lookdict_index(mp->ma_keys, ep->me_hash, i);
2939 assert(j >= 0);
2940 assert(dk_get_index(mp->ma_keys, j) == i);
2941 dk_set_index(mp->ma_keys, j, DKIX_DUMMY);
2942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002943 PyTuple_SET_ITEM(res, 0, ep->me_key);
2944 PyTuple_SET_ITEM(res, 1, ep->me_value);
Victor Stinner742da042016-09-07 17:40:12 -07002945 ep->me_key = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 ep->me_value = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002947 /* We can't dk_usable++ since there is DKIX_DUMMY in indices */
2948 mp->ma_keys->dk_nentries = i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002949 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002950 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02002951 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002952 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002953}
2954
Jeremy Hylton8caad492000-06-23 14:18:11 +00002955static int
2956dict_traverse(PyObject *op, visitproc visit, void *arg)
2957{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002958 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson55f44522016-09-05 12:12:59 -07002959 PyDictKeysObject *keys = mp->ma_keys;
Serhiy Storchaka46825d22016-09-26 21:29:34 +03002960 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Victor Stinner742da042016-09-07 17:40:12 -07002961 Py_ssize_t i, n = keys->dk_nentries;
2962
Benjamin Peterson55f44522016-09-05 12:12:59 -07002963 if (keys->dk_lookup == lookdict) {
2964 for (i = 0; i < n; i++) {
2965 if (entries[i].me_value != NULL) {
2966 Py_VISIT(entries[i].me_value);
2967 Py_VISIT(entries[i].me_key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002968 }
2969 }
Victor Stinner742da042016-09-07 17:40:12 -07002970 }
2971 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002972 if (mp->ma_values != NULL) {
Benjamin Peterson55f44522016-09-05 12:12:59 -07002973 for (i = 0; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002974 Py_VISIT(mp->ma_values[i]);
2975 }
2976 }
2977 else {
Benjamin Peterson55f44522016-09-05 12:12:59 -07002978 for (i = 0; i < n; i++) {
2979 Py_VISIT(entries[i].me_value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002980 }
2981 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002982 }
2983 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002984}
2985
2986static int
2987dict_tp_clear(PyObject *op)
2988{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002989 PyDict_Clear(op);
2990 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002991}
2992
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002993static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002994
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002995Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06002996_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002997{
Victor Stinner742da042016-09-07 17:40:12 -07002998 Py_ssize_t size, usable, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002999
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003000 size = DK_SIZE(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07003001 usable = USABLE_FRACTION(size);
3002
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02003003 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003004 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -07003005 res += usable * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003006 /* If the dictionary is split, the keys portion is accounted-for
3007 in the type object. */
3008 if (mp->ma_keys->dk_refcnt == 1)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003009 res += (sizeof(PyDictKeysObject)
3010 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
3011 + DK_IXSIZE(mp->ma_keys) * size
3012 + sizeof(PyDictKeyEntry) * usable);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003013 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003014}
3015
3016Py_ssize_t
3017_PyDict_KeysSize(PyDictKeysObject *keys)
3018{
Victor Stinner98ee9d52016-09-08 09:33:56 -07003019 return (sizeof(PyDictKeysObject)
3020 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
3021 + DK_IXSIZE(keys) * DK_SIZE(keys)
3022 + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003023}
3024
doko@ubuntu.com17210f52016-01-14 14:04:59 +01003025static PyObject *
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003026dict_sizeof(PyDictObject *mp)
3027{
3028 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
3029}
3030
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00003031PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
3032
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003033PyDoc_STRVAR(sizeof__doc__,
3034"D.__sizeof__() -> size of D in memory, in bytes");
3035
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003036PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00003037"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00003038If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00003039
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003040PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00003041"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000030422-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003043
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003044PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04003045"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
3046If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
3047If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
3048In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00003049
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003050PyDoc_STRVAR(clear__doc__,
3051"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003052
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003053PyDoc_STRVAR(copy__doc__,
3054"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00003055
Guido van Rossumb90c8482007-02-10 01:11:45 +00003056/* Forward */
3057static PyObject *dictkeys_new(PyObject *);
3058static PyObject *dictitems_new(PyObject *);
3059static PyObject *dictvalues_new(PyObject *);
3060
Guido van Rossum45c85d12007-07-27 16:31:40 +00003061PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003062 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003063PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003064 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003065PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003066 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00003067
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003068static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07003069 DICT___CONTAINS___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003070 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
3071 getitem__doc__},
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003072 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003073 sizeof__doc__},
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003074 DICT_GET_METHODDEF
3075 DICT_SETDEFAULT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003076 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
3077 pop__doc__},
3078 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
3079 popitem__doc__},
3080 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
3081 keys__doc__},
3082 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
3083 items__doc__},
3084 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
3085 values__doc__},
3086 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
3087 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08003088 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003089 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
3090 clear__doc__},
3091 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
3092 copy__doc__},
3093 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003094};
3095
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003096/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00003097int
3098PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003099{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003100 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07003101 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003102 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003103 PyObject *value;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003104
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003105 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003106 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003107 hash = PyObject_Hash(key);
3108 if (hash == -1)
3109 return -1;
3110 }
INADA Naoki778928b2017-08-03 23:45:15 +09003111 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003112 if (ix == DKIX_ERROR)
3113 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003114 return (ix != DKIX_EMPTY && value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003115}
3116
Thomas Wouterscf297e42007-02-23 15:07:44 +00003117/* Internal version of PyDict_Contains used when the hash value is already known */
3118int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003119_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00003120{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003121 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003122 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07003123 Py_ssize_t ix;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003124
INADA Naoki778928b2017-08-03 23:45:15 +09003125 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003126 if (ix == DKIX_ERROR)
3127 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003128 return (ix != DKIX_EMPTY && value != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00003129}
3130
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003131/* Hack to implement "key in dict" */
3132static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003133 0, /* sq_length */
3134 0, /* sq_concat */
3135 0, /* sq_repeat */
3136 0, /* sq_item */
3137 0, /* sq_slice */
3138 0, /* sq_ass_item */
3139 0, /* sq_ass_slice */
3140 PyDict_Contains, /* sq_contains */
3141 0, /* sq_inplace_concat */
3142 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003143};
3144
Guido van Rossum09e563a2001-05-01 12:10:21 +00003145static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003146dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3147{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003148 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003149 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003151 assert(type != NULL && type->tp_alloc != NULL);
3152 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02003153 if (self == NULL)
3154 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003155 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003156
Victor Stinnera9f61a52013-07-16 22:17:26 +02003157 /* The object has been implicitly tracked by tp_alloc */
3158 if (type == &PyDict_Type)
3159 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003160
3161 d->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003162 d->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07003163 d->ma_keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003164 if (d->ma_keys == NULL) {
3165 Py_DECREF(self);
3166 return NULL;
3167 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02003168 assert(_PyDict_CheckConsistency(d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003169 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003170}
3171
Tim Peters25786c02001-09-02 08:22:48 +00003172static int
3173dict_init(PyObject *self, PyObject *args, PyObject *kwds)
3174{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003175 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00003176}
3177
Tim Peters6d6c1a32001-08-02 04:15:00 +00003178static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003179dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00003180{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003181 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003182}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003183
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003184PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00003185"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00003186"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003187" (key, value) pairs\n"
3188"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00003189" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003190" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00003191" d[k] = v\n"
3192"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
3193" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00003194
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003195PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003196 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3197 "dict",
3198 sizeof(PyDictObject),
3199 0,
3200 (destructor)dict_dealloc, /* tp_dealloc */
3201 0, /* tp_print */
3202 0, /* tp_getattr */
3203 0, /* tp_setattr */
3204 0, /* tp_reserved */
3205 (reprfunc)dict_repr, /* tp_repr */
3206 0, /* tp_as_number */
3207 &dict_as_sequence, /* tp_as_sequence */
3208 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003209 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003210 0, /* tp_call */
3211 0, /* tp_str */
3212 PyObject_GenericGetAttr, /* tp_getattro */
3213 0, /* tp_setattro */
3214 0, /* tp_as_buffer */
3215 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3216 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
3217 dictionary_doc, /* tp_doc */
3218 dict_traverse, /* tp_traverse */
3219 dict_tp_clear, /* tp_clear */
3220 dict_richcompare, /* tp_richcompare */
3221 0, /* tp_weaklistoffset */
3222 (getiterfunc)dict_iter, /* tp_iter */
3223 0, /* tp_iternext */
3224 mapp_methods, /* tp_methods */
3225 0, /* tp_members */
3226 0, /* tp_getset */
3227 0, /* tp_base */
3228 0, /* tp_dict */
3229 0, /* tp_descr_get */
3230 0, /* tp_descr_set */
3231 0, /* tp_dictoffset */
3232 dict_init, /* tp_init */
3233 PyType_GenericAlloc, /* tp_alloc */
3234 dict_new, /* tp_new */
3235 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003236};
3237
Victor Stinner3c1e4812012-03-26 22:10:51 +02003238PyObject *
3239_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
3240{
3241 PyObject *kv;
3242 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02003243 if (kv == NULL) {
3244 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003245 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02003246 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02003247 return PyDict_GetItem(dp, kv);
3248}
3249
Guido van Rossum3cca2451997-05-16 14:23:33 +00003250/* For backward compatibility with old dictionary interface */
3251
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003252PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003253PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003254{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003255 PyObject *kv, *rv;
3256 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003257 if (kv == NULL) {
3258 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003259 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003260 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003261 rv = PyDict_GetItem(v, kv);
3262 Py_DECREF(kv);
3263 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003264}
3265
3266int
Victor Stinner3c1e4812012-03-26 22:10:51 +02003267_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
3268{
3269 PyObject *kv;
3270 kv = _PyUnicode_FromId(key); /* borrowed */
3271 if (kv == NULL)
3272 return -1;
3273 return PyDict_SetItem(v, kv, item);
3274}
3275
3276int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003277PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003278{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003279 PyObject *kv;
3280 int err;
3281 kv = PyUnicode_FromString(key);
3282 if (kv == NULL)
3283 return -1;
3284 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
3285 err = PyDict_SetItem(v, kv, item);
3286 Py_DECREF(kv);
3287 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003288}
3289
3290int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01003291_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
3292{
3293 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3294 if (kv == NULL)
3295 return -1;
3296 return PyDict_DelItem(v, kv);
3297}
3298
3299int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003300PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003301{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003302 PyObject *kv;
3303 int err;
3304 kv = PyUnicode_FromString(key);
3305 if (kv == NULL)
3306 return -1;
3307 err = PyDict_DelItem(v, kv);
3308 Py_DECREF(kv);
3309 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003310}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003311
Raymond Hettinger019a1482004-03-18 02:41:19 +00003312/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003313
3314typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003315 PyObject_HEAD
3316 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
3317 Py_ssize_t di_used;
3318 Py_ssize_t di_pos;
3319 PyObject* di_result; /* reusable result tuple for iteritems */
3320 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003321} dictiterobject;
3322
3323static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003324dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003325{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003326 dictiterobject *di;
3327 di = PyObject_GC_New(dictiterobject, itertype);
3328 if (di == NULL)
3329 return NULL;
3330 Py_INCREF(dict);
3331 di->di_dict = dict;
3332 di->di_used = dict->ma_used;
3333 di->di_pos = 0;
3334 di->len = dict->ma_used;
3335 if (itertype == &PyDictIterItem_Type) {
3336 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
3337 if (di->di_result == NULL) {
3338 Py_DECREF(di);
3339 return NULL;
3340 }
3341 }
3342 else
3343 di->di_result = NULL;
3344 _PyObject_GC_TRACK(di);
3345 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003346}
3347
3348static void
3349dictiter_dealloc(dictiterobject *di)
3350{
INADA Naokia6296d32017-08-24 14:55:17 +09003351 /* bpo-31095: UnTrack is needed before calling any callbacks */
3352 _PyObject_GC_UNTRACK(di);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003353 Py_XDECREF(di->di_dict);
3354 Py_XDECREF(di->di_result);
3355 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003356}
3357
3358static int
3359dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
3360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003361 Py_VISIT(di->di_dict);
3362 Py_VISIT(di->di_result);
3363 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003364}
3365
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003366static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003367dictiter_len(dictiterobject *di)
3368{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003369 Py_ssize_t len = 0;
3370 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
3371 len = di->len;
3372 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003373}
3374
Guido van Rossumb90c8482007-02-10 01:11:45 +00003375PyDoc_STRVAR(length_hint_doc,
3376 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003377
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003378static PyObject *
3379dictiter_reduce(dictiterobject *di);
3380
3381PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3382
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003383static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003384 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
3385 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003386 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
3387 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003388 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003389};
3390
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003391static PyObject*
3392dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00003393{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003394 PyObject *key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003395 Py_ssize_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003396 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003397 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00003398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003399 if (d == NULL)
3400 return NULL;
3401 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00003402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003403 if (di->di_used != d->ma_used) {
3404 PyErr_SetString(PyExc_RuntimeError,
3405 "dictionary changed size during iteration");
3406 di->di_used = -1; /* Make this state sticky */
3407 return NULL;
3408 }
Guido van Rossum2147df72002-07-16 20:30:22 +00003409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003410 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003411 k = d->ma_keys;
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;
3416 key = DK_ENTRIES(k)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003417 assert(d->ma_values[i] != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003418 }
3419 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003420 Py_ssize_t n = k->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003421 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3422 while (i < n && entry_ptr->me_value == NULL) {
3423 entry_ptr++;
3424 i++;
3425 }
3426 if (i >= n)
3427 goto fail;
3428 key = entry_ptr->me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003429 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003430 di->di_pos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003431 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003432 Py_INCREF(key);
3433 return key;
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;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003439}
3440
Raymond Hettinger019a1482004-03-18 02:41:19 +00003441PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003442 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3443 "dict_keyiterator", /* 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 */
3462 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3463 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_iternextkey, /* 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_iternextvalue(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003476{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003477 PyObject *value;
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;
INADA Naokica2d8be2016-11-04 16:59:10 +09003497 value = d->ma_values[i];
3498 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003499 }
3500 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003501 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003502 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3503 while (i < n && entry_ptr->me_value == NULL) {
3504 entry_ptr++;
3505 i++;
3506 }
3507 if (i >= n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003508 goto fail;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003509 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003510 }
3511 di->di_pos = i+1;
3512 di->len--;
3513 Py_INCREF(value);
3514 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003515
3516fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003517 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003518 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003519 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003520}
3521
3522PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003523 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3524 "dict_valueiterator", /* tp_name */
3525 sizeof(dictiterobject), /* tp_basicsize */
3526 0, /* tp_itemsize */
3527 /* methods */
3528 (destructor)dictiter_dealloc, /* tp_dealloc */
3529 0, /* tp_print */
3530 0, /* tp_getattr */
3531 0, /* tp_setattr */
3532 0, /* tp_reserved */
3533 0, /* tp_repr */
3534 0, /* tp_as_number */
3535 0, /* tp_as_sequence */
3536 0, /* tp_as_mapping */
3537 0, /* tp_hash */
3538 0, /* tp_call */
3539 0, /* tp_str */
3540 PyObject_GenericGetAttr, /* tp_getattro */
3541 0, /* tp_setattro */
3542 0, /* tp_as_buffer */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003543 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003544 0, /* tp_doc */
3545 (traverseproc)dictiter_traverse, /* tp_traverse */
3546 0, /* tp_clear */
3547 0, /* tp_richcompare */
3548 0, /* tp_weaklistoffset */
3549 PyObject_SelfIter, /* tp_iter */
3550 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3551 dictiter_methods, /* tp_methods */
3552 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003553};
3554
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003555static PyObject *
3556dictiter_iternextitem(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003557{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003558 PyObject *key, *value, *result;
INADA Naokica2d8be2016-11-04 16:59:10 +09003559 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003560 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003562 if (d == NULL)
3563 return NULL;
3564 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003566 if (di->di_used != d->ma_used) {
3567 PyErr_SetString(PyExc_RuntimeError,
3568 "dictionary changed size during iteration");
3569 di->di_used = -1; /* Make this state sticky */
3570 return NULL;
3571 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003573 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003574 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003575 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003576 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003577 goto fail;
3578 key = DK_ENTRIES(d->ma_keys)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003579 value = d->ma_values[i];
3580 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003581 }
3582 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003583 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003584 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3585 while (i < n && entry_ptr->me_value == NULL) {
3586 entry_ptr++;
3587 i++;
3588 }
3589 if (i >= n)
3590 goto fail;
3591 key = entry_ptr->me_key;
3592 value = entry_ptr->me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003593 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003594 di->di_pos = i+1;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003595 di->len--;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003596 Py_INCREF(key);
3597 Py_INCREF(value);
3598 result = di->di_result;
3599 if (Py_REFCNT(result) == 1) {
3600 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3601 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3602 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3603 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003604 Py_INCREF(result);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003605 Py_DECREF(oldkey);
3606 Py_DECREF(oldvalue);
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003607 }
3608 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003609 result = PyTuple_New(2);
3610 if (result == NULL)
3611 return NULL;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003612 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3613 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003614 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003615 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003616
3617fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003618 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003619 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003620 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003621}
3622
3623PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003624 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3625 "dict_itemiterator", /* tp_name */
3626 sizeof(dictiterobject), /* tp_basicsize */
3627 0, /* tp_itemsize */
3628 /* methods */
3629 (destructor)dictiter_dealloc, /* tp_dealloc */
3630 0, /* tp_print */
3631 0, /* tp_getattr */
3632 0, /* tp_setattr */
3633 0, /* tp_reserved */
3634 0, /* tp_repr */
3635 0, /* tp_as_number */
3636 0, /* tp_as_sequence */
3637 0, /* tp_as_mapping */
3638 0, /* tp_hash */
3639 0, /* tp_call */
3640 0, /* tp_str */
3641 PyObject_GenericGetAttr, /* tp_getattro */
3642 0, /* tp_setattro */
3643 0, /* tp_as_buffer */
3644 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3645 0, /* tp_doc */
3646 (traverseproc)dictiter_traverse, /* tp_traverse */
3647 0, /* tp_clear */
3648 0, /* tp_richcompare */
3649 0, /* tp_weaklistoffset */
3650 PyObject_SelfIter, /* tp_iter */
3651 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3652 dictiter_methods, /* tp_methods */
3653 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003654};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003655
3656
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003657static PyObject *
3658dictiter_reduce(dictiterobject *di)
3659{
3660 PyObject *list;
3661 dictiterobject tmp;
3662
3663 list = PyList_New(0);
3664 if (!list)
3665 return NULL;
3666
3667 /* copy the itertor state */
3668 tmp = *di;
3669 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003670
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003671 /* iterate the temporary into a list */
3672 for(;;) {
3673 PyObject *element = 0;
3674 if (Py_TYPE(di) == &PyDictIterItem_Type)
3675 element = dictiter_iternextitem(&tmp);
3676 else if (Py_TYPE(di) == &PyDictIterKey_Type)
3677 element = dictiter_iternextkey(&tmp);
3678 else if (Py_TYPE(di) == &PyDictIterValue_Type)
3679 element = dictiter_iternextvalue(&tmp);
3680 else
Barry Warsawb2e57942017-09-14 18:13:16 -07003681 Py_UNREACHABLE();
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003682 if (element) {
3683 if (PyList_Append(list, element)) {
3684 Py_DECREF(element);
3685 Py_DECREF(list);
3686 Py_XDECREF(tmp.di_dict);
3687 return NULL;
3688 }
3689 Py_DECREF(element);
3690 } else
3691 break;
3692 }
3693 Py_XDECREF(tmp.di_dict);
3694 /* check for error */
3695 if (tmp.di_dict != NULL) {
3696 /* we have an error */
3697 Py_DECREF(list);
3698 return NULL;
3699 }
Antoine Pitroua7013882012-04-05 00:04:20 +02003700 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003701}
3702
Guido van Rossum3ac67412007-02-10 18:55:06 +00003703/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003704/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003705/***********************************************/
3706
Guido van Rossumb90c8482007-02-10 01:11:45 +00003707/* The instance lay-out is the same for all three; but the type differs. */
3708
Guido van Rossumb90c8482007-02-10 01:11:45 +00003709static void
Eric Snow96c6af92015-05-29 22:21:39 -06003710dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003711{
INADA Naokia6296d32017-08-24 14:55:17 +09003712 /* bpo-31095: UnTrack is needed before calling any callbacks */
3713 _PyObject_GC_UNTRACK(dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003714 Py_XDECREF(dv->dv_dict);
3715 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003716}
3717
3718static int
Eric Snow96c6af92015-05-29 22:21:39 -06003719dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003720{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003721 Py_VISIT(dv->dv_dict);
3722 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003723}
3724
Guido van Rossum83825ac2007-02-10 04:54:19 +00003725static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003726dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003727{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003728 Py_ssize_t len = 0;
3729 if (dv->dv_dict != NULL)
3730 len = dv->dv_dict->ma_used;
3731 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003732}
3733
Eric Snow96c6af92015-05-29 22:21:39 -06003734PyObject *
3735_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003736{
Eric Snow96c6af92015-05-29 22:21:39 -06003737 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003738 if (dict == NULL) {
3739 PyErr_BadInternalCall();
3740 return NULL;
3741 }
3742 if (!PyDict_Check(dict)) {
3743 /* XXX Get rid of this restriction later */
3744 PyErr_Format(PyExc_TypeError,
3745 "%s() requires a dict argument, not '%s'",
3746 type->tp_name, dict->ob_type->tp_name);
3747 return NULL;
3748 }
Eric Snow96c6af92015-05-29 22:21:39 -06003749 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003750 if (dv == NULL)
3751 return NULL;
3752 Py_INCREF(dict);
3753 dv->dv_dict = (PyDictObject *)dict;
3754 _PyObject_GC_TRACK(dv);
3755 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003756}
3757
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003758/* TODO(guido): The views objects are not complete:
3759
3760 * support more set operations
3761 * support arbitrary mappings?
3762 - either these should be static or exported in dictobject.h
3763 - if public then they should probably be in builtins
3764*/
3765
Guido van Rossumaac530c2007-08-24 22:33:45 +00003766/* Return 1 if self is a subset of other, iterating over self;
3767 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003768static int
3769all_contained_in(PyObject *self, PyObject *other)
3770{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003771 PyObject *iter = PyObject_GetIter(self);
3772 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003774 if (iter == NULL)
3775 return -1;
3776 for (;;) {
3777 PyObject *next = PyIter_Next(iter);
3778 if (next == NULL) {
3779 if (PyErr_Occurred())
3780 ok = -1;
3781 break;
3782 }
3783 ok = PySequence_Contains(other, next);
3784 Py_DECREF(next);
3785 if (ok <= 0)
3786 break;
3787 }
3788 Py_DECREF(iter);
3789 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003790}
3791
3792static PyObject *
3793dictview_richcompare(PyObject *self, PyObject *other, int op)
3794{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003795 Py_ssize_t len_self, len_other;
3796 int ok;
3797 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003799 assert(self != NULL);
3800 assert(PyDictViewSet_Check(self));
3801 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003802
Brian Curtindfc80e32011-08-10 20:28:54 -05003803 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3804 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003806 len_self = PyObject_Size(self);
3807 if (len_self < 0)
3808 return NULL;
3809 len_other = PyObject_Size(other);
3810 if (len_other < 0)
3811 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003813 ok = 0;
3814 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003816 case Py_NE:
3817 case Py_EQ:
3818 if (len_self == len_other)
3819 ok = all_contained_in(self, other);
3820 if (op == Py_NE && ok >= 0)
3821 ok = !ok;
3822 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003824 case Py_LT:
3825 if (len_self < len_other)
3826 ok = all_contained_in(self, other);
3827 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003829 case Py_LE:
3830 if (len_self <= len_other)
3831 ok = all_contained_in(self, other);
3832 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003834 case Py_GT:
3835 if (len_self > len_other)
3836 ok = all_contained_in(other, self);
3837 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003839 case Py_GE:
3840 if (len_self >= len_other)
3841 ok = all_contained_in(other, self);
3842 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003844 }
3845 if (ok < 0)
3846 return NULL;
3847 result = ok ? Py_True : Py_False;
3848 Py_INCREF(result);
3849 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003850}
3851
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003852static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003853dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003854{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003855 PyObject *seq;
bennorthd7773d92018-01-26 15:46:01 +00003856 PyObject *result = NULL;
3857 Py_ssize_t rc;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003858
bennorthd7773d92018-01-26 15:46:01 +00003859 rc = Py_ReprEnter((PyObject *)dv);
3860 if (rc != 0) {
3861 return rc > 0 ? PyUnicode_FromString("...") : NULL;
3862 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003863 seq = PySequence_List((PyObject *)dv);
bennorthd7773d92018-01-26 15:46:01 +00003864 if (seq == NULL) {
3865 goto Done;
3866 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003867 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
3868 Py_DECREF(seq);
bennorthd7773d92018-01-26 15:46:01 +00003869
3870Done:
3871 Py_ReprLeave((PyObject *)dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003872 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003873}
3874
Guido van Rossum3ac67412007-02-10 18:55:06 +00003875/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003876
3877static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003878dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003879{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003880 if (dv->dv_dict == NULL) {
3881 Py_RETURN_NONE;
3882 }
3883 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003884}
3885
3886static int
Eric Snow96c6af92015-05-29 22:21:39 -06003887dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00003888{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003889 if (dv->dv_dict == NULL)
3890 return 0;
3891 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003892}
3893
Guido van Rossum83825ac2007-02-10 04:54:19 +00003894static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003895 (lenfunc)dictview_len, /* sq_length */
3896 0, /* sq_concat */
3897 0, /* sq_repeat */
3898 0, /* sq_item */
3899 0, /* sq_slice */
3900 0, /* sq_ass_item */
3901 0, /* sq_ass_slice */
3902 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003903};
3904
Guido van Rossum523259b2007-08-24 23:41:22 +00003905static PyObject*
3906dictviews_sub(PyObject* self, PyObject *other)
3907{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003908 PyObject *result = PySet_New(self);
3909 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003910 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003912 if (result == NULL)
3913 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003914
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003915 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003916 if (tmp == NULL) {
3917 Py_DECREF(result);
3918 return NULL;
3919 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003921 Py_DECREF(tmp);
3922 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003923}
3924
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003925PyObject*
3926_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00003927{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003928 PyObject *result = PySet_New(self);
3929 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003930 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003932 if (result == NULL)
3933 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003934
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003935 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_intersection_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003936 if (tmp == NULL) {
3937 Py_DECREF(result);
3938 return NULL;
3939 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003940
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003941 Py_DECREF(tmp);
3942 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003943}
3944
3945static PyObject*
3946dictviews_or(PyObject* self, PyObject *other)
3947{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003948 PyObject *result = PySet_New(self);
3949 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003950 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003952 if (result == NULL)
3953 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003954
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003955 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003956 if (tmp == NULL) {
3957 Py_DECREF(result);
3958 return NULL;
3959 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003961 Py_DECREF(tmp);
3962 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003963}
3964
3965static PyObject*
3966dictviews_xor(PyObject* self, PyObject *other)
3967{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003968 PyObject *result = PySet_New(self);
3969 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003970 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003972 if (result == NULL)
3973 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003974
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003975 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_symmetric_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003976 if (tmp == NULL) {
3977 Py_DECREF(result);
3978 return NULL;
3979 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003981 Py_DECREF(tmp);
3982 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003983}
3984
3985static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003986 0, /*nb_add*/
3987 (binaryfunc)dictviews_sub, /*nb_subtract*/
3988 0, /*nb_multiply*/
3989 0, /*nb_remainder*/
3990 0, /*nb_divmod*/
3991 0, /*nb_power*/
3992 0, /*nb_negative*/
3993 0, /*nb_positive*/
3994 0, /*nb_absolute*/
3995 0, /*nb_bool*/
3996 0, /*nb_invert*/
3997 0, /*nb_lshift*/
3998 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003999 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004000 (binaryfunc)dictviews_xor, /*nb_xor*/
4001 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00004002};
4003
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004004static PyObject*
4005dictviews_isdisjoint(PyObject *self, PyObject *other)
4006{
4007 PyObject *it;
4008 PyObject *item = NULL;
4009
4010 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06004011 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004012 Py_RETURN_TRUE;
4013 else
4014 Py_RETURN_FALSE;
4015 }
4016
4017 /* Iterate over the shorter object (only if other is a set,
4018 * because PySequence_Contains may be expensive otherwise): */
4019 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06004020 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004021 Py_ssize_t len_other = PyObject_Size(other);
4022 if (len_other == -1)
4023 return NULL;
4024
4025 if ((len_other > len_self)) {
4026 PyObject *tmp = other;
4027 other = self;
4028 self = tmp;
4029 }
4030 }
4031
4032 it = PyObject_GetIter(other);
4033 if (it == NULL)
4034 return NULL;
4035
4036 while ((item = PyIter_Next(it)) != NULL) {
4037 int contains = PySequence_Contains(self, item);
4038 Py_DECREF(item);
4039 if (contains == -1) {
4040 Py_DECREF(it);
4041 return NULL;
4042 }
4043
4044 if (contains) {
4045 Py_DECREF(it);
4046 Py_RETURN_FALSE;
4047 }
4048 }
4049 Py_DECREF(it);
4050 if (PyErr_Occurred())
4051 return NULL; /* PyIter_Next raised an exception. */
4052 Py_RETURN_TRUE;
4053}
4054
4055PyDoc_STRVAR(isdisjoint_doc,
4056"Return True if the view and the given iterable have a null intersection.");
4057
Guido van Rossumb90c8482007-02-10 01:11:45 +00004058static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004059 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4060 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004061 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004062};
4063
4064PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004065 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4066 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004067 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004068 0, /* tp_itemsize */
4069 /* methods */
4070 (destructor)dictview_dealloc, /* tp_dealloc */
4071 0, /* tp_print */
4072 0, /* tp_getattr */
4073 0, /* tp_setattr */
4074 0, /* tp_reserved */
4075 (reprfunc)dictview_repr, /* tp_repr */
4076 &dictviews_as_number, /* tp_as_number */
4077 &dictkeys_as_sequence, /* tp_as_sequence */
4078 0, /* tp_as_mapping */
4079 0, /* tp_hash */
4080 0, /* tp_call */
4081 0, /* tp_str */
4082 PyObject_GenericGetAttr, /* tp_getattro */
4083 0, /* tp_setattro */
4084 0, /* tp_as_buffer */
4085 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4086 0, /* tp_doc */
4087 (traverseproc)dictview_traverse, /* tp_traverse */
4088 0, /* tp_clear */
4089 dictview_richcompare, /* tp_richcompare */
4090 0, /* tp_weaklistoffset */
4091 (getiterfunc)dictkeys_iter, /* tp_iter */
4092 0, /* tp_iternext */
4093 dictkeys_methods, /* tp_methods */
4094 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004095};
4096
4097static PyObject *
4098dictkeys_new(PyObject *dict)
4099{
Eric Snow96c6af92015-05-29 22:21:39 -06004100 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004101}
4102
Guido van Rossum3ac67412007-02-10 18:55:06 +00004103/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004104
4105static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004106dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004107{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004108 if (dv->dv_dict == NULL) {
4109 Py_RETURN_NONE;
4110 }
4111 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004112}
4113
4114static int
Eric Snow96c6af92015-05-29 22:21:39 -06004115dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004116{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004117 int result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004118 PyObject *key, *value, *found;
4119 if (dv->dv_dict == NULL)
4120 return 0;
4121 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
4122 return 0;
4123 key = PyTuple_GET_ITEM(obj, 0);
4124 value = PyTuple_GET_ITEM(obj, 1);
Raymond Hettinger6692f012016-09-18 21:46:08 -07004125 found = PyDict_GetItemWithError((PyObject *)dv->dv_dict, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004126 if (found == NULL) {
4127 if (PyErr_Occurred())
4128 return -1;
4129 return 0;
4130 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004131 Py_INCREF(found);
4132 result = PyObject_RichCompareBool(value, found, Py_EQ);
4133 Py_DECREF(found);
4134 return result;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004135}
4136
Guido van Rossum83825ac2007-02-10 04:54:19 +00004137static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004138 (lenfunc)dictview_len, /* sq_length */
4139 0, /* sq_concat */
4140 0, /* sq_repeat */
4141 0, /* sq_item */
4142 0, /* sq_slice */
4143 0, /* sq_ass_item */
4144 0, /* sq_ass_slice */
4145 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004146};
4147
Guido van Rossumb90c8482007-02-10 01:11:45 +00004148static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004149 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4150 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004151 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004152};
4153
4154PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004155 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4156 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004157 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004158 0, /* tp_itemsize */
4159 /* methods */
4160 (destructor)dictview_dealloc, /* tp_dealloc */
4161 0, /* tp_print */
4162 0, /* tp_getattr */
4163 0, /* tp_setattr */
4164 0, /* tp_reserved */
4165 (reprfunc)dictview_repr, /* tp_repr */
4166 &dictviews_as_number, /* tp_as_number */
4167 &dictitems_as_sequence, /* tp_as_sequence */
4168 0, /* tp_as_mapping */
4169 0, /* tp_hash */
4170 0, /* tp_call */
4171 0, /* tp_str */
4172 PyObject_GenericGetAttr, /* tp_getattro */
4173 0, /* tp_setattro */
4174 0, /* tp_as_buffer */
4175 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4176 0, /* tp_doc */
4177 (traverseproc)dictview_traverse, /* tp_traverse */
4178 0, /* tp_clear */
4179 dictview_richcompare, /* tp_richcompare */
4180 0, /* tp_weaklistoffset */
4181 (getiterfunc)dictitems_iter, /* tp_iter */
4182 0, /* tp_iternext */
4183 dictitems_methods, /* tp_methods */
4184 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004185};
4186
4187static PyObject *
4188dictitems_new(PyObject *dict)
4189{
Eric Snow96c6af92015-05-29 22:21:39 -06004190 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004191}
4192
Guido van Rossum3ac67412007-02-10 18:55:06 +00004193/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004194
4195static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004196dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004198 if (dv->dv_dict == NULL) {
4199 Py_RETURN_NONE;
4200 }
4201 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004202}
4203
Guido van Rossum83825ac2007-02-10 04:54:19 +00004204static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004205 (lenfunc)dictview_len, /* sq_length */
4206 0, /* sq_concat */
4207 0, /* sq_repeat */
4208 0, /* sq_item */
4209 0, /* sq_slice */
4210 0, /* sq_ass_item */
4211 0, /* sq_ass_slice */
4212 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004213};
4214
Guido van Rossumb90c8482007-02-10 01:11:45 +00004215static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004216 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004217};
4218
4219PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004220 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4221 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004222 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004223 0, /* tp_itemsize */
4224 /* methods */
4225 (destructor)dictview_dealloc, /* tp_dealloc */
4226 0, /* tp_print */
4227 0, /* tp_getattr */
4228 0, /* tp_setattr */
4229 0, /* tp_reserved */
4230 (reprfunc)dictview_repr, /* tp_repr */
4231 0, /* tp_as_number */
4232 &dictvalues_as_sequence, /* tp_as_sequence */
4233 0, /* tp_as_mapping */
4234 0, /* tp_hash */
4235 0, /* tp_call */
4236 0, /* tp_str */
4237 PyObject_GenericGetAttr, /* tp_getattro */
4238 0, /* tp_setattro */
4239 0, /* tp_as_buffer */
4240 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4241 0, /* tp_doc */
4242 (traverseproc)dictview_traverse, /* tp_traverse */
4243 0, /* tp_clear */
4244 0, /* tp_richcompare */
4245 0, /* tp_weaklistoffset */
4246 (getiterfunc)dictvalues_iter, /* tp_iter */
4247 0, /* tp_iternext */
4248 dictvalues_methods, /* tp_methods */
4249 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004250};
4251
4252static PyObject *
4253dictvalues_new(PyObject *dict)
4254{
Eric Snow96c6af92015-05-29 22:21:39 -06004255 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004256}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004257
4258/* Returns NULL if cannot allocate a new PyDictKeysObject,
4259 but does not set an error */
4260PyDictKeysObject *
4261_PyDict_NewKeysForClass(void)
4262{
Victor Stinner742da042016-09-07 17:40:12 -07004263 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004264 if (keys == NULL)
4265 PyErr_Clear();
4266 else
4267 keys->dk_lookup = lookdict_split;
4268 return keys;
4269}
4270
4271#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
4272
4273PyObject *
4274PyObject_GenericGetDict(PyObject *obj, void *context)
4275{
4276 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
4277 if (dictptr == NULL) {
4278 PyErr_SetString(PyExc_AttributeError,
4279 "This object has no __dict__");
4280 return NULL;
4281 }
4282 dict = *dictptr;
4283 if (dict == NULL) {
4284 PyTypeObject *tp = Py_TYPE(obj);
4285 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
4286 DK_INCREF(CACHED_KEYS(tp));
4287 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
4288 }
4289 else {
4290 *dictptr = dict = PyDict_New();
4291 }
4292 }
4293 Py_XINCREF(dict);
4294 return dict;
4295}
4296
4297int
4298_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
Victor Stinner742da042016-09-07 17:40:12 -07004299 PyObject *key, PyObject *value)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004300{
4301 PyObject *dict;
4302 int res;
4303 PyDictKeysObject *cached;
4304
4305 assert(dictptr != NULL);
4306 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
4307 assert(dictptr != NULL);
4308 dict = *dictptr;
4309 if (dict == NULL) {
4310 DK_INCREF(cached);
4311 dict = new_dict_with_shared_keys(cached);
4312 if (dict == NULL)
4313 return -1;
4314 *dictptr = dict;
4315 }
4316 if (value == NULL) {
4317 res = PyDict_DelItem(dict, key);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004318 // Since key sharing dict doesn't allow deletion, PyDict_DelItem()
4319 // always converts dict to combined form.
4320 if ((cached = CACHED_KEYS(tp)) != NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004321 CACHED_KEYS(tp) = NULL;
4322 DK_DECREF(cached);
4323 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01004324 }
4325 else {
INADA Naoki2294f3a2017-02-12 13:51:30 +09004326 int was_shared = (cached == ((PyDictObject *)dict)->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004327 res = PyDict_SetItem(dict, key, value);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004328 if (was_shared &&
4329 (cached = CACHED_KEYS(tp)) != NULL &&
4330 cached != ((PyDictObject *)dict)->ma_keys) {
Victor Stinner3d3f2642016-12-15 17:21:23 +01004331 /* PyDict_SetItem() may call dictresize and convert split table
4332 * into combined table. In such case, convert it to split
4333 * table again and update type's shared key only when this is
4334 * the only dict sharing key with the type.
4335 *
4336 * This is to allow using shared key in class like this:
4337 *
4338 * class C:
4339 * def __init__(self):
4340 * # one dict resize happens
4341 * self.a, self.b, self.c = 1, 2, 3
4342 * self.d, self.e, self.f = 4, 5, 6
4343 * a = C()
4344 */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004345 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004346 CACHED_KEYS(tp) = make_keys_shared(dict);
Victor Stinner742da042016-09-07 17:40:12 -07004347 }
4348 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004349 CACHED_KEYS(tp) = NULL;
4350 }
4351 DK_DECREF(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004352 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
4353 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004354 }
4355 }
4356 } else {
4357 dict = *dictptr;
4358 if (dict == NULL) {
4359 dict = PyDict_New();
4360 if (dict == NULL)
4361 return -1;
4362 *dictptr = dict;
4363 }
4364 if (value == NULL) {
4365 res = PyDict_DelItem(dict, key);
4366 } else {
4367 res = PyDict_SetItem(dict, key, value);
4368 }
4369 }
4370 return res;
4371}
4372
4373void
4374_PyDictKeys_DecRef(PyDictKeysObject *keys)
4375{
4376 DK_DECREF(keys);
4377}