blob: 81c7f7f2436c41bd7afb9c1e10f17b396663f58d [file] [log] [blame]
Guido van Rossum2bc13791999-03-24 19:06:42 +00001/* Dictionary object implementation using a hash table */
Guido van Rossum9bfef441993-03-29 10:43:31 +00002
Raymond Hettinger930427b2003-05-03 06:51:59 +00003/* The distribution includes a separate file, Objects/dictnotes.txt,
Tim Peters60b29962006-01-01 01:19:23 +00004 describing explorations into dictionary design and optimization.
Raymond Hettinger930427b2003-05-03 06:51:59 +00005 It covers typical dictionary use patterns, the parameters for
6 tuning dictionaries, and several ideas for possible optimizations.
7*/
8
Victor Stinner742da042016-09-07 17:40:12 -07009/* PyDictKeysObject
10
11This implements the dictionary's hashtable.
12
Raymond Hettingerb12785d2016-10-22 09:58:14 -070013As of Python 3.6, this is compact and ordered. Basic idea is described here:
14* https://mail.python.org/pipermail/python-dev/2012-December/123028.html
15* https://morepypy.blogspot.com/2015/01/faster-more-memory-efficient-and-more.html
Victor Stinner742da042016-09-07 17:40:12 -070016
17layout:
18
19+---------------+
20| dk_refcnt |
21| dk_size |
22| dk_lookup |
23| dk_usable |
24| dk_nentries |
25+---------------+
26| dk_indices |
27| |
28+---------------+
29| dk_entries |
30| |
31+---------------+
32
33dk_indices is actual hashtable. It holds index in entries, or DKIX_EMPTY(-1)
34or DKIX_DUMMY(-2).
35Size of indices is dk_size. Type of each index in indices is vary on dk_size:
36
37* int8 for dk_size <= 128
38* int16 for 256 <= dk_size <= 2**15
39* int32 for 2**16 <= dk_size <= 2**31
40* int64 for 2**32 <= dk_size
41
42dk_entries is array of PyDictKeyEntry. It's size is USABLE_FRACTION(dk_size).
43DK_ENTRIES(dk) can be used to get pointer to entries.
44
45NOTE: Since negative value is used for DKIX_EMPTY and DKIX_DUMMY, type of
46dk_indices entry is signed integer and int16 is used for table which
47dk_size == 256.
48*/
49
Benjamin Peterson7d95e402012-04-23 11:24:50 -040050
51/*
Benjamin Peterson7d95e402012-04-23 11:24:50 -040052The DictObject can be in one of two forms.
Victor Stinner742da042016-09-07 17:40:12 -070053
Benjamin Peterson7d95e402012-04-23 11:24:50 -040054Either:
55 A combined table:
56 ma_values == NULL, dk_refcnt == 1.
57 Values are stored in the me_value field of the PyDictKeysObject.
Benjamin Peterson7d95e402012-04-23 11:24:50 -040058Or:
59 A split table:
60 ma_values != NULL, dk_refcnt >= 1
61 Values are stored in the ma_values array.
Victor Stinner742da042016-09-07 17:40:12 -070062 Only string (unicode) keys are allowed.
63 All dicts sharing same key must have same insertion order.
Benjamin Peterson7d95e402012-04-23 11:24:50 -040064
Victor Stinner742da042016-09-07 17:40:12 -070065There are four kinds of slots in the table (slot is index, and
66DK_ENTRIES(keys)[index] if index >= 0):
67
681. Unused. index == DKIX_EMPTY
69 Does not hold an active (key, value) pair now and never did. Unused can
70 transition to Active upon key insertion. This is each slot's initial state.
71
722. Active. index >= 0, me_key != NULL and me_value != NULL
73 Holds an active (key, value) pair. Active can transition to Dummy or
74 Pending upon key deletion (for combined and split tables respectively).
75 This is the only case in which me_value != NULL.
76
773. Dummy. index == DKIX_DUMMY (combined only)
78 Previously held an active (key, value) pair, but that was deleted and an
79 active pair has not yet overwritten the slot. Dummy can transition to
80 Active upon key insertion. Dummy slots cannot be made Unused again
81 else the probe sequence in case of collision would have no way to know
82 they were once active.
83
844. Pending. index >= 0, key != NULL, and value == NULL (split only)
85 Not yet inserted in split-table.
Benjamin Peterson7d95e402012-04-23 11:24:50 -040086*/
87
Victor Stinner742da042016-09-07 17:40:12 -070088/*
89Preserving insertion order
Benjamin Peterson7d95e402012-04-23 11:24:50 -040090
Victor Stinner742da042016-09-07 17:40:12 -070091It's simple for combined table. Since dk_entries is mostly append only, we can
92get insertion order by just iterating dk_entries.
93
94One exception is .popitem(). It removes last item in dk_entries and decrement
95dk_nentries to achieve amortized O(1). Since there are DKIX_DUMMY remains in
96dk_indices, we can't increment dk_usable even though dk_nentries is
97decremented.
98
99In split table, inserting into pending entry is allowed only for dk_entries[ix]
100where ix == mp->ma_used. Inserting into other index and deleting item cause
101converting the dict to the combined table.
102*/
103
104/* PyDict_MINSIZE is the starting size for any new dict.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400105 * 8 allows dicts with no more than 5 active entries; experiments suggested
106 * this suffices for the majority of dicts (consisting mostly of usually-small
107 * dicts created to pass keyword arguments).
108 * Making this 8, rather than 4 reduces the number of resizes for most
109 * dictionaries, without any significant extra memory use.
110 */
Victor Stinner742da042016-09-07 17:40:12 -0700111#define PyDict_MINSIZE 8
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400112
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000113#include "Python.h"
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600114#include "internal/pystate.h"
Eric Snow96c6af92015-05-29 22:21:39 -0600115#include "dict-common.h"
Victor Stinner990397e2016-09-09 20:22:59 -0700116#include "stringlib/eq.h" /* to get unicode_eq() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000117
Larry Hastings61272b72014-01-07 12:41:53 -0800118/*[clinic input]
Larry Hastingsc2047262014-01-25 20:43:29 -0800119class dict "PyDictObject *" "&PyDict_Type"
Larry Hastings61272b72014-01-07 12:41:53 -0800120[clinic start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -0800121/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f157a5a0ce9589d6]*/
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800122
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400123
124/*
125To ensure the lookup algorithm terminates, there must be at least one Unused
126slot (NULL key) in the table.
127To avoid slowing down lookups on a near-full table, we resize the table when
128it's USABLE_FRACTION (currently two-thirds) full.
129*/
Guido van Rossum16e93a81997-01-28 00:00:11 +0000130
Tim Peterseb28ef22001-06-02 05:27:19 +0000131#define PERTURB_SHIFT 5
132
Guido van Rossum16e93a81997-01-28 00:00:11 +0000133/*
Tim Peterseb28ef22001-06-02 05:27:19 +0000134Major subtleties ahead: Most hash schemes depend on having a "good" hash
135function, in the sense of simulating randomness. Python doesn't: its most
R David Murray537ad7a2016-07-10 12:33:18 -0400136important hash functions (for ints) are very regular in common
Tim Peterseb28ef22001-06-02 05:27:19 +0000137cases:
Tim Peters15d49292001-05-27 07:39:22 +0000138
R David Murray537ad7a2016-07-10 12:33:18 -0400139 >>>[hash(i) for i in range(4)]
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000140 [0, 1, 2, 3]
Tim Peters15d49292001-05-27 07:39:22 +0000141
Tim Peterseb28ef22001-06-02 05:27:19 +0000142This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
143the low-order i bits as the initial table index is extremely fast, and there
R David Murray537ad7a2016-07-10 12:33:18 -0400144are no collisions at all for dicts indexed by a contiguous range of ints. So
145this gives better-than-random behavior in common cases, and that's very
146desirable.
Tim Peters15d49292001-05-27 07:39:22 +0000147
Tim Peterseb28ef22001-06-02 05:27:19 +0000148OTOH, when collisions occur, the tendency to fill contiguous slices of the
149hash table makes a good collision resolution strategy crucial. Taking only
150the last i bits of the hash code is also vulnerable: for example, consider
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000152their own hash codes, and this fits in a dict of size 2**15, the last 15 bits
153 of every hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +0000154
Tim Peterseb28ef22001-06-02 05:27:19 +0000155But catering to unusual cases should not slow the usual ones, so we just take
156the last i bits anyway. It's up to collision resolution to do the rest. If
157we *usually* find the key we're looking for on the first try (and, it turns
158out, we usually do -- the table load factor is kept under 2/3, so the odds
159are solidly in our favor), then it makes best sense to keep the initial index
160computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +0000161
Tim Peterseb28ef22001-06-02 05:27:19 +0000162The first half of collision resolution is to visit table indices via this
163recurrence:
Tim Peters15d49292001-05-27 07:39:22 +0000164
Tim Peterseb28ef22001-06-02 05:27:19 +0000165 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +0000166
Tim Peterseb28ef22001-06-02 05:27:19 +0000167For any initial j in range(2**i), repeating that 2**i times generates each
168int in range(2**i) exactly once (see any text on random-number generation for
169proof). By itself, this doesn't help much: like linear probing (setting
170j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
171order. This would be bad, except that's not the only thing we do, and it's
172actually *good* in the common cases where hash keys are consecutive. In an
173example that's really too small to make this entirely clear, for a table of
174size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +0000175
Tim Peterseb28ef22001-06-02 05:27:19 +0000176 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
177
178If two things come in at index 5, the first place we look after is index 2,
179not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
180Linear probing is deadly in this case because there the fixed probe order
181is the *same* as the order consecutive keys are likely to arrive. But it's
182extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
183and certain that consecutive hash codes do not.
184
185The other half of the strategy is to get the other bits of the hash code
186into play. This is done by initializing a (unsigned) vrbl "perturb" to the
187full hash code, and changing the recurrence to:
188
Tim Peterseb28ef22001-06-02 05:27:19 +0000189 perturb >>= PERTURB_SHIFT;
INADA Naoki267941c2016-10-06 15:19:07 +0900190 j = (5*j) + 1 + perturb;
Tim Peterseb28ef22001-06-02 05:27:19 +0000191 use j % 2**i as the next table index;
192
193Now the probe sequence depends (eventually) on every bit in the hash code,
194and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
195because it quickly magnifies small differences in the bits that didn't affect
196the initial index. Note that because perturb is unsigned, if the recurrence
197is executed often enough perturb eventually becomes and remains 0. At that
198point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
199that's certain to find an empty slot eventually (since it generates every int
200in range(2**i), and we make sure there's always at least one empty slot).
201
202Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
203small so that the high bits of the hash code continue to affect the probe
204sequence across iterations; but you want it large so that in really bad cases
205the high-order hash bits have an effect on early iterations. 5 was "the
206best" in minimizing total collisions across experiments Tim Peters ran (on
207both normal and pathological cases), but 4 and 6 weren't significantly worse.
208
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000209Historical: Reimer Behrends contributed the idea of using a polynomial-based
Tim Peterseb28ef22001-06-02 05:27:19 +0000210approach, using repeated multiplication by x in GF(2**n) where an irreducible
211polynomial for each table size was chosen such that x was a primitive root.
212Christian Tismer later extended that to use division by x instead, as an
213efficient way to get the high bits of the hash code into play. This scheme
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000214also gave excellent collision statistics, but was more expensive: two
215if-tests were required inside the loop; computing "the next" index took about
216the same number of operations but without as much potential parallelism
217(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
218above, and then shifting perturb can be done while the table index is being
219masked); and the PyDictObject struct required a member to hold the table's
220polynomial. In Tim's experiments the current scheme ran faster, produced
221equally good collision statistics, needed less code & used less memory.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000222
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000223*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000224
Fred Drake1bff34a2000-08-31 19:31:38 +0000225/* forward declarations */
Victor Stinner742da042016-09-07 17:40:12 -0700226static Py_ssize_t lookdict(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900227 Py_hash_t hash, PyObject **value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700228static Py_ssize_t lookdict_unicode(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900229 Py_hash_t hash, PyObject **value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700230static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400231lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900232 Py_hash_t hash, PyObject **value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700233static Py_ssize_t lookdict_split(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900234 Py_hash_t hash, PyObject **value_addr);
Fred Drake1bff34a2000-08-31 19:31:38 +0000235
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400236static int dictresize(PyDictObject *mp, Py_ssize_t minused);
Tim Petersdea48ec2001-05-22 20:40:22 +0000237
Benjamin Peterson3c569292016-09-08 13:16:41 -0700238/*Global counter used to set ma_version_tag field of dictionary.
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700239 * It is incremented each time that a dictionary is created and each
240 * time that a dictionary is modified. */
241static uint64_t pydict_global_version = 0;
242
243#define DICT_NEXT_VERSION() (++pydict_global_version)
244
Victor Stinner742da042016-09-07 17:40:12 -0700245/* Dictionary reuse scheme to save calls to malloc and free */
Christian Heimes2202f872008-02-06 14:31:34 +0000246#ifndef PyDict_MAXFREELIST
247#define PyDict_MAXFREELIST 80
248#endif
249static PyDictObject *free_list[PyDict_MAXFREELIST];
250static int numfree = 0;
Victor Stinner742da042016-09-07 17:40:12 -0700251static PyDictKeysObject *keys_free_list[PyDict_MAXFREELIST];
252static int numfreekeys = 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000253
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300254#include "clinic/dictobject.c.h"
255
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100256int
257PyDict_ClearFreeList(void)
Christian Heimes77c02eb2008-02-09 02:18:51 +0000258{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 PyDictObject *op;
Victor Stinner742da042016-09-07 17:40:12 -0700260 int ret = numfree + numfreekeys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 while (numfree) {
262 op = free_list[--numfree];
263 assert(PyDict_CheckExact(op));
264 PyObject_GC_Del(op);
265 }
Victor Stinner742da042016-09-07 17:40:12 -0700266 while (numfreekeys) {
267 PyObject_FREE(keys_free_list[--numfreekeys]);
268 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100269 return ret;
270}
271
David Malcolm49526f42012-06-22 14:55:41 -0400272/* Print summary info about the state of the optimized allocator */
273void
274_PyDict_DebugMallocStats(FILE *out)
275{
276 _PyDebugAllocatorStats(out,
277 "free PyDictObject", numfree, sizeof(PyDictObject));
278}
279
280
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100281void
282PyDict_Fini(void)
283{
284 PyDict_ClearFreeList();
Christian Heimes77c02eb2008-02-09 02:18:51 +0000285}
286
Victor Stinner742da042016-09-07 17:40:12 -0700287#define DK_SIZE(dk) ((dk)->dk_size)
288#if SIZEOF_VOID_P > 4
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700289#define DK_IXSIZE(dk) \
290 (DK_SIZE(dk) <= 0xff ? \
291 1 : DK_SIZE(dk) <= 0xffff ? \
292 2 : DK_SIZE(dk) <= 0xffffffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700293 4 : sizeof(int64_t))
Victor Stinner742da042016-09-07 17:40:12 -0700294#else
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700295#define DK_IXSIZE(dk) \
296 (DK_SIZE(dk) <= 0xff ? \
297 1 : DK_SIZE(dk) <= 0xffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700298 2 : sizeof(int32_t))
Victor Stinner742da042016-09-07 17:40:12 -0700299#endif
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700300#define DK_ENTRIES(dk) \
Benjamin Peterson186122e2016-09-08 12:20:12 -0700301 ((PyDictKeyEntry*)(&(dk)->dk_indices.as_1[DK_SIZE(dk) * DK_IXSIZE(dk)]))
Victor Stinner742da042016-09-07 17:40:12 -0700302
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200303#define DK_DEBUG_INCREF _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA
304#define DK_DEBUG_DECREF _Py_DEC_REFTOTAL _Py_REF_DEBUG_COMMA
305
306#define DK_INCREF(dk) (DK_DEBUG_INCREF ++(dk)->dk_refcnt)
307#define DK_DECREF(dk) if (DK_DEBUG_DECREF (--(dk)->dk_refcnt) == 0) free_keys_object(dk)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400308#define DK_MASK(dk) (((dk)->dk_size)-1)
309#define IS_POWER_OF_2(x) (((x) & (x-1)) == 0)
310
Victor Stinner742da042016-09-07 17:40:12 -0700311/* lookup indices. returns DKIX_EMPTY, DKIX_DUMMY, or ix >=0 */
Benjamin Peterson73222252016-09-08 09:58:47 -0700312static inline Py_ssize_t
Victor Stinner742da042016-09-07 17:40:12 -0700313dk_get_index(PyDictKeysObject *keys, Py_ssize_t i)
314{
315 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700316 Py_ssize_t ix;
317
Victor Stinner742da042016-09-07 17:40:12 -0700318 if (s <= 0xff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700319 int8_t *indices = keys->dk_indices.as_1;
Victor Stinner208857e2016-09-08 11:35:46 -0700320 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700321 }
322 else if (s <= 0xffff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700323 int16_t *indices = keys->dk_indices.as_2;
Victor Stinner208857e2016-09-08 11:35:46 -0700324 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700325 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700326#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300327 else if (s > 0xffffffff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700328 int64_t *indices = keys->dk_indices.as_8;
Victor Stinner208857e2016-09-08 11:35:46 -0700329 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700330 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700331#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300332 else {
333 int32_t *indices = keys->dk_indices.as_4;
334 ix = indices[i];
335 }
Victor Stinner71211e32016-09-08 10:52:46 -0700336 assert(ix >= DKIX_DUMMY);
337 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700338}
339
340/* write to indices. */
Benjamin Peterson73222252016-09-08 09:58:47 -0700341static inline void
Victor Stinner742da042016-09-07 17:40:12 -0700342dk_set_index(PyDictKeysObject *keys, Py_ssize_t i, Py_ssize_t ix)
343{
344 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700345
346 assert(ix >= DKIX_DUMMY);
347
Victor Stinner742da042016-09-07 17:40:12 -0700348 if (s <= 0xff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700349 int8_t *indices = keys->dk_indices.as_1;
Victor Stinner71211e32016-09-08 10:52:46 -0700350 assert(ix <= 0x7f);
Victor Stinner208857e2016-09-08 11:35:46 -0700351 indices[i] = (char)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700352 }
353 else if (s <= 0xffff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700354 int16_t *indices = keys->dk_indices.as_2;
Victor Stinner71211e32016-09-08 10:52:46 -0700355 assert(ix <= 0x7fff);
Victor Stinner208857e2016-09-08 11:35:46 -0700356 indices[i] = (int16_t)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700357 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700358#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300359 else if (s > 0xffffffff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700360 int64_t *indices = keys->dk_indices.as_8;
Victor Stinner208857e2016-09-08 11:35:46 -0700361 indices[i] = ix;
Victor Stinner742da042016-09-07 17:40:12 -0700362 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700363#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300364 else {
365 int32_t *indices = keys->dk_indices.as_4;
366 assert(ix <= 0x7fffffff);
367 indices[i] = (int32_t)ix;
368 }
Victor Stinner742da042016-09-07 17:40:12 -0700369}
370
371
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200372/* USABLE_FRACTION is the maximum dictionary load.
Victor Stinner742da042016-09-07 17:40:12 -0700373 * Increasing this ratio makes dictionaries more dense resulting in more
374 * collisions. Decreasing it improves sparseness at the expense of spreading
375 * indices over more cache lines and at the cost of total memory consumed.
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200376 *
377 * USABLE_FRACTION must obey the following:
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400378 * (0 < USABLE_FRACTION(n) < n) for all n >= 2
379 *
Victor Stinner742da042016-09-07 17:40:12 -0700380 * USABLE_FRACTION should be quick to calculate.
381 * Fractions around 1/2 to 2/3 seem to work well in practice.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400382 */
Victor Stinner742da042016-09-07 17:40:12 -0700383#define USABLE_FRACTION(n) (((n) << 1)/3)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400384
Victor Stinner742da042016-09-07 17:40:12 -0700385/* ESTIMATE_SIZE is reverse function of USABLE_FRACTION.
386 * This can be used to reserve enough size to insert n entries without
387 * resizing.
388 */
INADA Naoki92c50ee2016-11-22 00:57:02 +0900389#define ESTIMATE_SIZE(n) (((n)*3+1) >> 1)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400390
Victor Stinner742da042016-09-07 17:40:12 -0700391/* Alternative fraction that is otherwise close enough to 2n/3 to make
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400392 * little difference. 8 * 2/3 == 8 * 5/8 == 5. 16 * 2/3 == 16 * 5/8 == 10.
393 * 32 * 2/3 = 21, 32 * 5/8 = 20.
394 * Its advantage is that it is faster to compute on machines with slow division.
395 * #define USABLE_FRACTION(n) (((n) >> 1) + ((n) >> 2) - ((n) >> 3))
Victor Stinner742da042016-09-07 17:40:12 -0700396 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400397
Victor Stinnera9f61a52013-07-16 22:17:26 +0200398/* GROWTH_RATE. Growth rate upon hitting maximum load.
399 * Currently set to used*2 + capacity/2.
400 * This means that dicts double in size when growing without deletions,
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700401 * but have more head room when the number of deletions is on a par with the
402 * number of insertions.
403 * Raising this to used*4 doubles memory consumption depending on the size of
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200404 * the dictionary, but results in half the number of resizes, less effort to
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700405 * resize.
406 * GROWTH_RATE was set to used*4 up to version 3.2.
407 * GROWTH_RATE was set to used*2 in version 3.3.0
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200408 */
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700409#define GROWTH_RATE(d) (((d)->ma_used*2)+((d)->ma_keys->dk_size>>1))
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400410
411#define ENSURE_ALLOWS_DELETIONS(d) \
412 if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \
413 (d)->ma_keys->dk_lookup = lookdict_unicode; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400415
416/* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
417 * (which cannot fail and thus can do no allocation).
418 */
419static PyDictKeysObject empty_keys_struct = {
Serhiy Storchaka97932e42016-09-26 23:01:23 +0300420 1, /* dk_refcnt */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400421 1, /* dk_size */
422 lookdict_split, /* dk_lookup */
423 0, /* dk_usable (immutable) */
Victor Stinner742da042016-09-07 17:40:12 -0700424 0, /* dk_nentries */
Benjamin Peterson186122e2016-09-08 12:20:12 -0700425 .dk_indices = { .as_1 = {DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY,
426 DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY}},
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400427};
428
429static PyObject *empty_values[1] = { NULL };
430
431#define Py_EMPTY_KEYS &empty_keys_struct
432
Victor Stinner611b0fa2016-09-14 15:02:01 +0200433/* Uncomment to check the dict content in _PyDict_CheckConsistency() */
434/* #define DEBUG_PYDICT */
435
436
T. Woutersa00c3fd2017-03-31 09:14:41 -0700437#ifndef NDEBUG
Victor Stinner611b0fa2016-09-14 15:02:01 +0200438static int
439_PyDict_CheckConsistency(PyDictObject *mp)
440{
441 PyDictKeysObject *keys = mp->ma_keys;
442 int splitted = _PyDict_HasSplitTable(mp);
443 Py_ssize_t usable = USABLE_FRACTION(keys->dk_size);
444#ifdef DEBUG_PYDICT
445 PyDictKeyEntry *entries = DK_ENTRIES(keys);
446 Py_ssize_t i;
447#endif
448
449 assert(0 <= mp->ma_used && mp->ma_used <= usable);
450 assert(IS_POWER_OF_2(keys->dk_size));
451 assert(0 <= keys->dk_usable
452 && keys->dk_usable <= usable);
453 assert(0 <= keys->dk_nentries
454 && keys->dk_nentries <= usable);
455 assert(keys->dk_usable + keys->dk_nentries <= usable);
456
457 if (!splitted) {
458 /* combined table */
459 assert(keys->dk_refcnt == 1);
460 }
461
462#ifdef DEBUG_PYDICT
463 for (i=0; i < keys->dk_size; i++) {
464 Py_ssize_t ix = dk_get_index(keys, i);
465 assert(DKIX_DUMMY <= ix && ix <= usable);
466 }
467
468 for (i=0; i < usable; i++) {
469 PyDictKeyEntry *entry = &entries[i];
470 PyObject *key = entry->me_key;
471
472 if (key != NULL) {
473 if (PyUnicode_CheckExact(key)) {
474 Py_hash_t hash = ((PyASCIIObject *)key)->hash;
475 assert(hash != -1);
476 assert(entry->me_hash == hash);
477 }
478 else {
479 /* test_dict fails if PyObject_Hash() is called again */
480 assert(entry->me_hash != -1);
481 }
482 if (!splitted) {
483 assert(entry->me_value != NULL);
484 }
485 }
486
487 if (splitted) {
488 assert(entry->me_value == NULL);
489 }
490 }
491
492 if (splitted) {
493 /* splitted table */
494 for (i=0; i < mp->ma_used; i++) {
495 assert(mp->ma_values[i] != NULL);
496 }
497 }
498#endif
499
500 return 1;
501}
502#endif
503
504
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400505static PyDictKeysObject *new_keys_object(Py_ssize_t size)
506{
507 PyDictKeysObject *dk;
Victor Stinner742da042016-09-07 17:40:12 -0700508 Py_ssize_t es, usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400509
Victor Stinner742da042016-09-07 17:40:12 -0700510 assert(size >= PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400511 assert(IS_POWER_OF_2(size));
Victor Stinner742da042016-09-07 17:40:12 -0700512
513 usable = USABLE_FRACTION(size);
514 if (size <= 0xff) {
515 es = 1;
516 }
517 else if (size <= 0xffff) {
518 es = 2;
519 }
520#if SIZEOF_VOID_P > 4
521 else if (size <= 0xffffffff) {
522 es = 4;
523 }
524#endif
525 else {
526 es = sizeof(Py_ssize_t);
527 }
528
529 if (size == PyDict_MINSIZE && numfreekeys > 0) {
530 dk = keys_free_list[--numfreekeys];
531 }
532 else {
Victor Stinner98ee9d52016-09-08 09:33:56 -0700533 dk = PyObject_MALLOC(sizeof(PyDictKeysObject)
534 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
535 + es * size
536 + sizeof(PyDictKeyEntry) * usable);
Victor Stinner742da042016-09-07 17:40:12 -0700537 if (dk == NULL) {
538 PyErr_NoMemory();
539 return NULL;
540 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400541 }
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200542 DK_DEBUG_INCREF dk->dk_refcnt = 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400543 dk->dk_size = size;
Victor Stinner742da042016-09-07 17:40:12 -0700544 dk->dk_usable = usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400545 dk->dk_lookup = lookdict_unicode_nodummy;
Victor Stinner742da042016-09-07 17:40:12 -0700546 dk->dk_nentries = 0;
Benjamin Peterson186122e2016-09-08 12:20:12 -0700547 memset(&dk->dk_indices.as_1[0], 0xff, es * size);
Victor Stinner742da042016-09-07 17:40:12 -0700548 memset(DK_ENTRIES(dk), 0, sizeof(PyDictKeyEntry) * usable);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400549 return dk;
550}
551
552static void
553free_keys_object(PyDictKeysObject *keys)
554{
Victor Stinner742da042016-09-07 17:40:12 -0700555 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400556 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -0700557 for (i = 0, n = keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400558 Py_XDECREF(entries[i].me_key);
559 Py_XDECREF(entries[i].me_value);
560 }
Victor Stinner742da042016-09-07 17:40:12 -0700561 if (keys->dk_size == PyDict_MINSIZE && numfreekeys < PyDict_MAXFREELIST) {
562 keys_free_list[numfreekeys++] = keys;
563 return;
564 }
Raymond Hettingerce5179f2016-01-31 08:56:21 -0800565 PyObject_FREE(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400566}
567
568#define new_values(size) PyMem_NEW(PyObject *, size)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400569#define free_values(values) PyMem_FREE(values)
570
571/* Consumes a reference to the keys object */
572static PyObject *
573new_dict(PyDictKeysObject *keys, PyObject **values)
574{
575 PyDictObject *mp;
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200576 assert(keys != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 if (numfree) {
578 mp = free_list[--numfree];
579 assert (mp != NULL);
580 assert (Py_TYPE(mp) == &PyDict_Type);
581 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400583 else {
584 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
585 if (mp == NULL) {
586 DK_DECREF(keys);
587 free_values(values);
588 return NULL;
589 }
590 }
591 mp->ma_keys = keys;
592 mp->ma_values = values;
593 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700594 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +0200595 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000597}
598
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400599/* Consumes a reference to the keys object */
600static PyObject *
601new_dict_with_shared_keys(PyDictKeysObject *keys)
602{
603 PyObject **values;
604 Py_ssize_t i, size;
605
Victor Stinner742da042016-09-07 17:40:12 -0700606 size = USABLE_FRACTION(DK_SIZE(keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400607 values = new_values(size);
608 if (values == NULL) {
609 DK_DECREF(keys);
610 return PyErr_NoMemory();
611 }
612 for (i = 0; i < size; i++) {
613 values[i] = NULL;
614 }
615 return new_dict(keys, values);
616}
617
618PyObject *
619PyDict_New(void)
620{
Victor Stinner742da042016-09-07 17:40:12 -0700621 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200622 if (keys == NULL)
623 return NULL;
624 return new_dict(keys, NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400625}
626
Victor Stinner742da042016-09-07 17:40:12 -0700627/* Search index of hash table from offset of entry table */
628static Py_ssize_t
629lookdict_index(PyDictKeysObject *k, Py_hash_t hash, Py_ssize_t index)
630{
Victor Stinner742da042016-09-07 17:40:12 -0700631 size_t mask = DK_MASK(k);
INADA Naoki073ae482017-06-23 15:22:50 +0900632 size_t perturb = (size_t)hash;
633 size_t i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700634
INADA Naoki073ae482017-06-23 15:22:50 +0900635 for (;;) {
636 Py_ssize_t ix = dk_get_index(k, i);
Victor Stinner742da042016-09-07 17:40:12 -0700637 if (ix == index) {
638 return i;
639 }
640 if (ix == DKIX_EMPTY) {
641 return DKIX_EMPTY;
642 }
INADA Naoki073ae482017-06-23 15:22:50 +0900643 perturb >>= PERTURB_SHIFT;
644 i = mask & (i*5 + perturb + 1);
Victor Stinner742da042016-09-07 17:40:12 -0700645 }
646 assert(0); /* NOT REACHED */
647 return DKIX_ERROR;
648}
649
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000650/*
651The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000652This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000653Open addressing is preferred over chaining since the link overhead for
654chaining would be substantial (100% with typical malloc overhead).
655
Tim Peterseb28ef22001-06-02 05:27:19 +0000656The initial probe index is computed as hash mod the table size. Subsequent
657probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000658
659All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000660
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000661The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000662contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000663Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000664
Victor Stinner742da042016-09-07 17:40:12 -0700665lookdict() is general-purpose, and may return DKIX_ERROR if (and only if) a
Victor Stinnera4348cc2016-09-08 12:01:25 -0700666comparison raises an exception.
Guido van Rossum89d8c602007-09-18 17:26:56 +0000667lookdict_unicode() below is specialized to string keys, comparison of which can
INADA Naoki1b8df102017-02-20 22:48:10 +0900668never raise an exception; that function can never return DKIX_ERROR when key
669is string. Otherwise, it falls back to lookdict().
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400670lookdict_unicode_nodummy is further specialized for string keys that cannot be
671the <dummy> value.
INADA Naoki778928b2017-08-03 23:45:15 +0900672For both, when the key isn't found a DKIX_EMPTY is returned.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000673*/
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100674static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400675lookdict(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900676 Py_hash_t hash, PyObject **value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000677{
INADA Naoki778928b2017-08-03 23:45:15 +0900678 size_t i, mask, perturb;
Victor Stinner742da042016-09-07 17:40:12 -0700679 PyDictKeysObject *dk;
INADA Naoki778928b2017-08-03 23:45:15 +0900680 PyDictKeyEntry *ep0;
Tim Peterseb28ef22001-06-02 05:27:19 +0000681
Antoine Pitrou9a234902012-05-13 20:48:01 +0200682top:
Victor Stinner742da042016-09-07 17:40:12 -0700683 dk = mp->ma_keys;
Victor Stinner742da042016-09-07 17:40:12 -0700684 ep0 = DK_ENTRIES(dk);
INADA Naoki778928b2017-08-03 23:45:15 +0900685 mask = DK_MASK(dk);
686 perturb = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700688
INADA Naoki778928b2017-08-03 23:45:15 +0900689 for (;;) {
690 Py_ssize_t ix = dk_get_index(dk, i);
Victor Stinner742da042016-09-07 17:40:12 -0700691 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700692 *value_addr = NULL;
693 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400694 }
INADA Naoki778928b2017-08-03 23:45:15 +0900695 if (ix >= 0) {
696 PyDictKeyEntry *ep = &ep0[ix];
697 assert(ep->me_key != NULL);
698 if (ep->me_key == key) {
699 *value_addr = ep->me_value;
700 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700701 }
INADA Naoki778928b2017-08-03 23:45:15 +0900702 if (ep->me_hash == hash) {
703 PyObject *startkey = ep->me_key;
704 Py_INCREF(startkey);
705 int cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
706 Py_DECREF(startkey);
707 if (cmp < 0) {
708 *value_addr = NULL;
709 return DKIX_ERROR;
710 }
711 if (dk == mp->ma_keys && ep->me_key == startkey) {
712 if (cmp > 0) {
713 *value_addr = ep->me_value;
714 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700715 }
INADA Naoki778928b2017-08-03 23:45:15 +0900716 }
717 else {
718 /* The dict was mutated, restart */
719 goto top;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400720 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 }
INADA Naoki778928b2017-08-03 23:45:15 +0900723 perturb >>= PERTURB_SHIFT;
724 i = (i*5 + perturb + 1) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 }
726 assert(0); /* NOT REACHED */
727 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000728}
729
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400730/* Specialized version for string-only keys */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100731static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400732lookdict_unicode(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900733 Py_hash_t hash, PyObject **value_addr)
Fred Drake1bff34a2000-08-31 19:31:38 +0000734{
Victor Stinner742da042016-09-07 17:40:12 -0700735 assert(mp->ma_values == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 /* Make sure this function doesn't have to handle non-unicode keys,
737 including subclasses of str; e.g., one reason to subclass
738 unicodes is to override __eq__, and for speed we don't cater to
739 that here. */
740 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400741 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900742 return lookdict(mp, key, hash, value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 }
Tim Peters15d49292001-05-27 07:39:22 +0000744
INADA Naoki778928b2017-08-03 23:45:15 +0900745 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
746 size_t mask = DK_MASK(mp->ma_keys);
747 size_t perturb = (size_t)hash;
748 size_t i = (size_t)hash & mask;
749
750 for (;;) {
751 Py_ssize_t ix = dk_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700752 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700753 *value_addr = NULL;
754 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400755 }
INADA Naoki778928b2017-08-03 23:45:15 +0900756 if (ix >= 0) {
757 PyDictKeyEntry *ep = &ep0[ix];
758 assert(ep->me_key != NULL);
759 assert(PyUnicode_CheckExact(ep->me_key));
760 if (ep->me_key == key ||
761 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
762 *value_addr = ep->me_value;
763 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700764 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400765 }
INADA Naoki778928b2017-08-03 23:45:15 +0900766 perturb >>= PERTURB_SHIFT;
767 i = mask & (i*5 + perturb + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 }
INADA Naoki778928b2017-08-03 23:45:15 +0900769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 assert(0); /* NOT REACHED */
771 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000772}
773
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400774/* Faster version of lookdict_unicode when it is known that no <dummy> keys
775 * will be present. */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100776static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400777lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900778 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400779{
Victor Stinner742da042016-09-07 17:40:12 -0700780 assert(mp->ma_values == NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400781 /* Make sure this function doesn't have to handle non-unicode keys,
782 including subclasses of str; e.g., one reason to subclass
783 unicodes is to override __eq__, and for speed we don't cater to
784 that here. */
785 if (!PyUnicode_CheckExact(key)) {
786 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900787 return lookdict(mp, key, hash, value_addr);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400788 }
INADA Naoki778928b2017-08-03 23:45:15 +0900789
790 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
791 size_t mask = DK_MASK(mp->ma_keys);
792 size_t perturb = (size_t)hash;
793 size_t i = (size_t)hash & mask;
794
795 for (;;) {
796 Py_ssize_t ix = dk_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700797 assert (ix != DKIX_DUMMY);
798 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700799 *value_addr = NULL;
800 return DKIX_EMPTY;
801 }
INADA Naoki778928b2017-08-03 23:45:15 +0900802 PyDictKeyEntry *ep = &ep0[ix];
803 assert(ep->me_key != NULL);
804 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700805 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400806 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900807 *value_addr = ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700808 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400809 }
INADA Naoki778928b2017-08-03 23:45:15 +0900810 perturb >>= PERTURB_SHIFT;
811 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400812 }
813 assert(0); /* NOT REACHED */
814 return 0;
815}
816
817/* Version of lookdict for split tables.
818 * All split tables and only split tables use this lookup function.
819 * Split tables only contain unicode keys and no dummy keys,
820 * so algorithm is the same as lookdict_unicode_nodummy.
821 */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100822static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400823lookdict_split(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900824 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400825{
Victor Stinner742da042016-09-07 17:40:12 -0700826 /* mp must split table */
827 assert(mp->ma_values != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400828 if (!PyUnicode_CheckExact(key)) {
INADA Naoki778928b2017-08-03 23:45:15 +0900829 Py_ssize_t ix = lookdict(mp, key, hash, value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700830 if (ix >= 0) {
INADA Naokiba609772016-12-07 20:41:42 +0900831 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700832 }
833 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400834 }
Victor Stinner742da042016-09-07 17:40:12 -0700835
INADA Naoki778928b2017-08-03 23:45:15 +0900836 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
837 size_t mask = DK_MASK(mp->ma_keys);
838 size_t perturb = (size_t)hash;
839 size_t i = (size_t)hash & mask;
840
841 for (;;) {
842 Py_ssize_t ix = dk_get_index(mp->ma_keys, i);
843 assert (ix != DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -0700844 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700845 *value_addr = NULL;
846 return DKIX_EMPTY;
847 }
INADA Naoki778928b2017-08-03 23:45:15 +0900848 PyDictKeyEntry *ep = &ep0[ix];
849 assert(ep->me_key != NULL);
850 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700851 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400852 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900853 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700854 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400855 }
INADA Naoki778928b2017-08-03 23:45:15 +0900856 perturb >>= PERTURB_SHIFT;
857 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400858 }
859 assert(0); /* NOT REACHED */
860 return 0;
861}
862
Benjamin Petersonfb886362010-04-24 18:21:17 +0000863int
864_PyDict_HasOnlyStringKeys(PyObject *dict)
865{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 Py_ssize_t pos = 0;
867 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000868 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400870 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 return 1;
872 while (PyDict_Next(dict, &pos, &key, &value))
873 if (!PyUnicode_Check(key))
874 return 0;
875 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000876}
877
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000878#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 do { \
880 if (!_PyObject_GC_IS_TRACKED(mp)) { \
881 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
882 _PyObject_GC_MAY_BE_TRACKED(value)) { \
883 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 } \
885 } \
886 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000887
888void
889_PyDict_MaybeUntrack(PyObject *op)
890{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 PyDictObject *mp;
892 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -0700893 Py_ssize_t i, numentries;
894 PyDictKeyEntry *ep0;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
897 return;
898
899 mp = (PyDictObject *) op;
Victor Stinner742da042016-09-07 17:40:12 -0700900 ep0 = DK_ENTRIES(mp->ma_keys);
901 numentries = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400902 if (_PyDict_HasSplitTable(mp)) {
Victor Stinner742da042016-09-07 17:40:12 -0700903 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400904 if ((value = mp->ma_values[i]) == NULL)
905 continue;
906 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
Victor Stinner742da042016-09-07 17:40:12 -0700907 assert(!_PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400908 return;
909 }
910 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400912 else {
Victor Stinner742da042016-09-07 17:40:12 -0700913 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400914 if ((value = ep0[i].me_value) == NULL)
915 continue;
916 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
917 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
918 return;
919 }
920 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000922}
923
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400924/* Internal function to find slot for an item from its hash
Victor Stinner3c336c52016-09-12 14:17:40 +0200925 when it is known that the key is not present in the dict.
926
927 The dict must be combined. */
INADA Naokiba609772016-12-07 20:41:42 +0900928static Py_ssize_t
INADA Naoki778928b2017-08-03 23:45:15 +0900929find_empty_slot(PyDictKeysObject *keys, Py_hash_t hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000930{
INADA Naoki778928b2017-08-03 23:45:15 +0900931 assert(keys != NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000932
INADA Naoki778928b2017-08-03 23:45:15 +0900933 const size_t mask = DK_MASK(keys);
934 size_t i = hash & mask;
935 Py_ssize_t ix = dk_get_index(keys, i);
936 for (size_t perturb = hash; ix >= 0;) {
INADA Naoki267941c2016-10-06 15:19:07 +0900937 perturb >>= PERTURB_SHIFT;
INADA Naoki778928b2017-08-03 23:45:15 +0900938 i = (i*5 + perturb + 1) & mask;
939 ix = dk_get_index(keys, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 }
INADA Naoki778928b2017-08-03 23:45:15 +0900941 return i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000942}
943
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400944static int
945insertion_resize(PyDictObject *mp)
946{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700947 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400948}
Antoine Pitroue965d972012-02-27 00:45:12 +0100949
950/*
951Internal routine to insert a new item into the table.
952Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +0100953Returns -1 if an error occurred, or 0 on success.
954*/
955static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400956insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +0100957{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400958 PyObject *old_value;
INADA Naokiba609772016-12-07 20:41:42 +0900959 PyDictKeyEntry *ep;
Antoine Pitroue965d972012-02-27 00:45:12 +0100960
Serhiy Storchaka753bca32017-05-20 12:30:02 +0300961 Py_INCREF(key);
962 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400963 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
964 if (insertion_resize(mp) < 0)
Serhiy Storchaka753bca32017-05-20 12:30:02 +0300965 goto Fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400966 }
967
INADA Naoki778928b2017-08-03 23:45:15 +0900968 Py_ssize_t ix = mp->ma_keys->dk_lookup(mp, key, hash, &old_value);
Serhiy Storchaka753bca32017-05-20 12:30:02 +0300969 if (ix == DKIX_ERROR)
970 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -0700971
Antoine Pitroud6967322014-10-18 00:35:00 +0200972 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400973 MAINTAIN_TRACKING(mp, key, value);
Victor Stinner742da042016-09-07 17:40:12 -0700974
975 /* When insertion order is different from shared key, we can't share
976 * the key anymore. Convert this instance to combine table.
977 */
978 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +0900979 ((ix >= 0 && old_value == NULL && mp->ma_used != ix) ||
Victor Stinner742da042016-09-07 17:40:12 -0700980 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +0300981 if (insertion_resize(mp) < 0)
982 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -0700983 ix = DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400984 }
Victor Stinner742da042016-09-07 17:40:12 -0700985
986 if (ix == DKIX_EMPTY) {
987 /* Insert into new slot. */
INADA Naokiba609772016-12-07 20:41:42 +0900988 assert(old_value == NULL);
Victor Stinner742da042016-09-07 17:40:12 -0700989 if (mp->ma_keys->dk_usable <= 0) {
990 /* Need to resize. */
Serhiy Storchaka753bca32017-05-20 12:30:02 +0300991 if (insertion_resize(mp) < 0)
992 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -0700993 }
INADA Naoki778928b2017-08-03 23:45:15 +0900994 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naokiba609772016-12-07 20:41:42 +0900995 ep = &DK_ENTRIES(mp->ma_keys)[mp->ma_keys->dk_nentries];
Victor Stinner742da042016-09-07 17:40:12 -0700996 dk_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Victor Stinner742da042016-09-07 17:40:12 -0700997 ep->me_key = key;
998 ep->me_hash = hash;
999 if (mp->ma_values) {
1000 assert (mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
1001 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001002 }
1003 else {
Victor Stinner742da042016-09-07 17:40:12 -07001004 ep->me_value = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001005 }
1006 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001007 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07001008 mp->ma_keys->dk_usable--;
1009 mp->ma_keys->dk_nentries++;
1010 assert(mp->ma_keys->dk_usable >= 0);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001011 assert(_PyDict_CheckConsistency(mp));
Victor Stinner742da042016-09-07 17:40:12 -07001012 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001013 }
Victor Stinner742da042016-09-07 17:40:12 -07001014
INADA Naokiba609772016-12-07 20:41:42 +09001015 if (_PyDict_HasSplitTable(mp)) {
1016 mp->ma_values[ix] = value;
1017 if (old_value == NULL) {
1018 /* pending state */
1019 assert(ix == mp->ma_used);
1020 mp->ma_used++;
1021 }
1022 }
1023 else {
1024 assert(old_value != NULL);
1025 DK_ENTRIES(mp->ma_keys)[ix].me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07001026 }
1027
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001028 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokiba609772016-12-07 20:41:42 +09001029 Py_XDECREF(old_value); /* which **CAN** re-enter (see issue #22653) */
Victor Stinner611b0fa2016-09-14 15:02:01 +02001030 assert(_PyDict_CheckConsistency(mp));
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001031 Py_DECREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001032 return 0;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001033
1034Fail:
1035 Py_DECREF(value);
1036 Py_DECREF(key);
1037 return -1;
Antoine Pitroue965d972012-02-27 00:45:12 +01001038}
1039
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001040/*
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001041Internal routine used by dictresize() to buid a hashtable of entries.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001042*/
1043static void
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001044build_indices(PyDictKeysObject *keys, PyDictKeyEntry *ep, Py_ssize_t n)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001045{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001046 size_t mask = (size_t)DK_SIZE(keys) - 1;
1047 for (Py_ssize_t ix = 0; ix != n; ix++, ep++) {
1048 Py_hash_t hash = ep->me_hash;
1049 size_t i = hash & mask;
1050 for (size_t perturb = hash; dk_get_index(keys, i) != DKIX_EMPTY;) {
1051 perturb >>= PERTURB_SHIFT;
INADA Naoki870c2862017-06-24 09:03:19 +09001052 i = mask & (i*5 + perturb + 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001053 }
1054 dk_set_index(keys, i, ix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001056}
1057
1058/*
1059Restructure the table by allocating a new table and reinserting all
1060items again. When entries have been deleted, the new table may
1061actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001062If a table is split (its keys and hashes are shared, its values are not),
1063then the values are temporarily copied into the table, it is resized as
1064a combined table, then the me_value slots in the old table are NULLed out.
1065After resizing a table is always combined,
1066but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001067*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001068static int
Victor Stinner3d3f2642016-12-15 17:21:23 +01001069dictresize(PyDictObject *mp, Py_ssize_t minsize)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001070{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001071 Py_ssize_t newsize, numentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001072 PyDictKeysObject *oldkeys;
1073 PyObject **oldvalues;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001074 PyDictKeyEntry *oldentries, *newentries;
Tim Peters91a364d2001-05-19 07:04:38 +00001075
Victor Stinner742da042016-09-07 17:40:12 -07001076 /* Find the smallest table size > minused. */
1077 for (newsize = PyDict_MINSIZE;
Victor Stinner3d3f2642016-12-15 17:21:23 +01001078 newsize < minsize && newsize > 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 newsize <<= 1)
1080 ;
1081 if (newsize <= 0) {
1082 PyErr_NoMemory();
1083 return -1;
1084 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001085
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001086 oldkeys = mp->ma_keys;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001087
1088 /* NOTE: Current odict checks mp->ma_keys to detect resize happen.
1089 * So we can't reuse oldkeys even if oldkeys->dk_size == newsize.
1090 * TODO: Try reusing oldkeys when reimplement odict.
1091 */
1092
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001093 /* Allocate a new table. */
1094 mp->ma_keys = new_keys_object(newsize);
1095 if (mp->ma_keys == NULL) {
1096 mp->ma_keys = oldkeys;
1097 return -1;
1098 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01001099 // New table must be large enough.
1100 assert(mp->ma_keys->dk_usable >= mp->ma_used);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001101 if (oldkeys->dk_lookup == lookdict)
1102 mp->ma_keys->dk_lookup = lookdict;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001103
1104 numentries = mp->ma_used;
1105 oldentries = DK_ENTRIES(oldkeys);
1106 newentries = DK_ENTRIES(mp->ma_keys);
1107 oldvalues = mp->ma_values;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001108 if (oldvalues != NULL) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001109 /* Convert split table into new combined table.
1110 * We must incref keys; we can transfer values.
1111 * Note that values of split table is always dense.
1112 */
1113 for (Py_ssize_t i = 0; i < numentries; i++) {
1114 assert(oldvalues[i] != NULL);
1115 PyDictKeyEntry *ep = &oldentries[i];
1116 PyObject *key = ep->me_key;
1117 Py_INCREF(key);
1118 newentries[i].me_key = key;
1119 newentries[i].me_hash = ep->me_hash;
1120 newentries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001122
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001123 DK_DECREF(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001124 mp->ma_values = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001125 if (oldvalues != empty_values) {
1126 free_values(oldvalues);
1127 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001128 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001129 else { // combined table.
1130 if (oldkeys->dk_nentries == numentries) {
1131 memcpy(newentries, oldentries, numentries * sizeof(PyDictKeyEntry));
1132 }
1133 else {
1134 PyDictKeyEntry *ep = oldentries;
1135 for (Py_ssize_t i = 0; i < numentries; i++) {
1136 while (ep->me_value == NULL)
1137 ep++;
1138 newentries[i] = *ep++;
1139 }
1140 }
1141
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001142 assert(oldkeys->dk_lookup != lookdict_split);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001143 assert(oldkeys->dk_refcnt == 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001144 if (oldkeys->dk_size == PyDict_MINSIZE &&
1145 numfreekeys < PyDict_MAXFREELIST) {
1146 DK_DEBUG_DECREF keys_free_list[numfreekeys++] = oldkeys;
1147 }
1148 else {
1149 DK_DEBUG_DECREF PyObject_FREE(oldkeys);
1150 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001151 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001152
1153 build_indices(mp->ma_keys, newentries, numentries);
1154 mp->ma_keys->dk_usable -= numentries;
1155 mp->ma_keys->dk_nentries = numentries;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001157}
1158
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001159/* Returns NULL if unable to split table.
1160 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001161static PyDictKeysObject *
1162make_keys_shared(PyObject *op)
1163{
1164 Py_ssize_t i;
1165 Py_ssize_t size;
1166 PyDictObject *mp = (PyDictObject *)op;
1167
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001168 if (!PyDict_CheckExact(op))
1169 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001170 if (!_PyDict_HasSplitTable(mp)) {
1171 PyDictKeyEntry *ep0;
1172 PyObject **values;
1173 assert(mp->ma_keys->dk_refcnt == 1);
1174 if (mp->ma_keys->dk_lookup == lookdict) {
1175 return NULL;
1176 }
1177 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1178 /* Remove dummy keys */
1179 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1180 return NULL;
1181 }
1182 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1183 /* Copy values into a new array */
Victor Stinner742da042016-09-07 17:40:12 -07001184 ep0 = DK_ENTRIES(mp->ma_keys);
1185 size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001186 values = new_values(size);
1187 if (values == NULL) {
1188 PyErr_SetString(PyExc_MemoryError,
1189 "Not enough memory to allocate new values array");
1190 return NULL;
1191 }
1192 for (i = 0; i < size; i++) {
1193 values[i] = ep0[i].me_value;
1194 ep0[i].me_value = NULL;
1195 }
1196 mp->ma_keys->dk_lookup = lookdict_split;
1197 mp->ma_values = values;
1198 }
1199 DK_INCREF(mp->ma_keys);
1200 return mp->ma_keys;
1201}
Christian Heimes99170a52007-12-19 02:07:34 +00001202
1203PyObject *
1204_PyDict_NewPresized(Py_ssize_t minused)
1205{
INADA Naoki92c50ee2016-11-22 00:57:02 +09001206 const Py_ssize_t max_presize = 128 * 1024;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001207 Py_ssize_t newsize;
1208 PyDictKeysObject *new_keys;
INADA Naoki92c50ee2016-11-22 00:57:02 +09001209
1210 /* There are no strict guarantee that returned dict can contain minused
1211 * items without resize. So we create medium size dict instead of very
1212 * large dict or MemoryError.
1213 */
1214 if (minused > USABLE_FRACTION(max_presize)) {
1215 newsize = max_presize;
1216 }
1217 else {
1218 Py_ssize_t minsize = ESTIMATE_SIZE(minused);
1219 newsize = PyDict_MINSIZE;
1220 while (newsize < minsize) {
1221 newsize <<= 1;
1222 }
1223 }
1224 assert(IS_POWER_OF_2(newsize));
1225
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001226 new_keys = new_keys_object(newsize);
1227 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001229 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001230}
1231
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001232/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1233 * that may occur (originally dicts supported only string keys, and exceptions
1234 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001235 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001236 * (suppressed) occurred while computing the key's hash, or that some error
1237 * (suppressed) occurred when comparing keys in the dict's internal probe
1238 * sequence. A nasty example of the latter is when a Python-coded comparison
1239 * function hits a stack-depth error, which can cause this to return NULL
1240 * even if the key is present.
1241 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001242PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001243PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001244{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001245 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07001246 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 PyDictObject *mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 PyThreadState *tstate;
INADA Naokiba609772016-12-07 20:41:42 +09001249 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 if (!PyDict_Check(op))
1252 return NULL;
1253 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001254 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 {
1256 hash = PyObject_Hash(key);
1257 if (hash == -1) {
1258 PyErr_Clear();
1259 return NULL;
1260 }
1261 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 /* We can arrive here with a NULL tstate during initialization: try
1264 running "python -Wi" for an example related to string interning.
1265 Let's just hope that no exception occurs then... This must be
1266 _PyThreadState_Current and not PyThreadState_GET() because in debug
1267 mode, the latter complains if tstate is NULL. */
Victor Stinner0cae6092016-11-11 01:43:56 +01001268 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 if (tstate != NULL && tstate->curexc_type != NULL) {
1270 /* preserve the existing exception */
1271 PyObject *err_type, *err_value, *err_tb;
1272 PyErr_Fetch(&err_type, &err_value, &err_tb);
INADA Naoki778928b2017-08-03 23:45:15 +09001273 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 /* ignore errors */
1275 PyErr_Restore(err_type, err_value, err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001276 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 return NULL;
1278 }
1279 else {
INADA Naoki778928b2017-08-03 23:45:15 +09001280 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001281 if (ix < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 PyErr_Clear();
1283 return NULL;
1284 }
1285 }
INADA Naokiba609772016-12-07 20:41:42 +09001286 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001287}
1288
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001289/* Same as PyDict_GetItemWithError() but with hash supplied by caller.
1290 This returns NULL *with* an exception set if an exception occurred.
1291 It returns NULL *without* an exception set if the key wasn't present.
1292*/
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001293PyObject *
1294_PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1295{
Victor Stinner742da042016-09-07 17:40:12 -07001296 Py_ssize_t ix;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001297 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001298 PyObject *value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001299
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001300 if (!PyDict_Check(op)) {
1301 PyErr_BadInternalCall();
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001302 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001303 }
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001304
INADA Naoki778928b2017-08-03 23:45:15 +09001305 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001306 if (ix < 0) {
1307 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001308 }
INADA Naokiba609772016-12-07 20:41:42 +09001309 return value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001310}
1311
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001312/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1313 This returns NULL *with* an exception set if an exception occurred.
1314 It returns NULL *without* an exception set if the key wasn't present.
1315*/
1316PyObject *
1317PyDict_GetItemWithError(PyObject *op, PyObject *key)
1318{
Victor Stinner742da042016-09-07 17:40:12 -07001319 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001320 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 PyDictObject*mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001322 PyObject *value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 if (!PyDict_Check(op)) {
1325 PyErr_BadInternalCall();
1326 return NULL;
1327 }
1328 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001329 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 {
1331 hash = PyObject_Hash(key);
1332 if (hash == -1) {
1333 return NULL;
1334 }
1335 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001336
INADA Naoki778928b2017-08-03 23:45:15 +09001337 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001338 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001340 return value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001341}
1342
Brett Cannonfd074152012-04-14 14:10:13 -04001343PyObject *
1344_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1345{
1346 PyObject *kv;
1347 kv = _PyUnicode_FromId(key); /* borrowed */
1348 if (kv == NULL)
1349 return NULL;
1350 return PyDict_GetItemWithError(dp, kv);
1351}
1352
Victor Stinnerb4efc962015-11-20 09:24:02 +01001353/* Fast version of global value lookup (LOAD_GLOBAL).
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001354 * Lookup in globals, then builtins.
Victor Stinnerb4efc962015-11-20 09:24:02 +01001355 *
1356 * Raise an exception and return NULL if an error occurred (ex: computing the
1357 * key hash failed, key comparison failed, ...). Return NULL if the key doesn't
1358 * exist. Return the value if the key exists.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001359 */
1360PyObject *
1361_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001362{
Victor Stinner742da042016-09-07 17:40:12 -07001363 Py_ssize_t ix;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001364 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09001365 PyObject *value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001366
1367 if (!PyUnicode_CheckExact(key) ||
1368 (hash = ((PyASCIIObject *) key)->hash) == -1)
1369 {
1370 hash = PyObject_Hash(key);
1371 if (hash == -1)
1372 return NULL;
Antoine Pitroue965d972012-02-27 00:45:12 +01001373 }
Victor Stinnerb4efc962015-11-20 09:24:02 +01001374
1375 /* namespace 1: globals */
INADA Naoki778928b2017-08-03 23:45:15 +09001376 ix = globals->ma_keys->dk_lookup(globals, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001377 if (ix == DKIX_ERROR)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001378 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001379 if (ix != DKIX_EMPTY && value != NULL)
1380 return value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001381
1382 /* namespace 2: builtins */
INADA Naoki778928b2017-08-03 23:45:15 +09001383 ix = builtins->ma_keys->dk_lookup(builtins, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001384 if (ix < 0)
Victor Stinnerb4efc962015-11-20 09:24:02 +01001385 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001386 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001387}
1388
Antoine Pitroue965d972012-02-27 00:45:12 +01001389/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1390 * dictionary if it's merely replacing the value for an existing key.
1391 * This means that it's safe to loop over a dictionary with PyDict_Next()
1392 * and occasionally replace a value -- but you can't insert new keys or
1393 * remove them.
1394 */
1395int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001396PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001397{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001398 PyDictObject *mp;
1399 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001400 if (!PyDict_Check(op)) {
1401 PyErr_BadInternalCall();
1402 return -1;
1403 }
1404 assert(key);
1405 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001406 mp = (PyDictObject *)op;
1407 if (!PyUnicode_CheckExact(key) ||
1408 (hash = ((PyASCIIObject *) key)->hash) == -1)
1409 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001410 hash = PyObject_Hash(key);
1411 if (hash == -1)
1412 return -1;
1413 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001414
1415 /* insertdict() handles any resizing that might be necessary */
1416 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001417}
1418
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001419int
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001420_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1421 Py_hash_t hash)
1422{
1423 PyDictObject *mp;
1424
1425 if (!PyDict_Check(op)) {
1426 PyErr_BadInternalCall();
1427 return -1;
1428 }
1429 assert(key);
1430 assert(value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001431 assert(hash != -1);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001432 mp = (PyDictObject *)op;
1433
1434 /* insertdict() handles any resizing that might be necessary */
1435 return insertdict(mp, key, hash, value);
1436}
1437
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001438static int
INADA Naoki778928b2017-08-03 23:45:15 +09001439delitem_common(PyDictObject *mp, Py_hash_t hash, Py_ssize_t ix,
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001440 PyObject *old_value)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001441{
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001442 PyObject *old_key;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001443 PyDictKeyEntry *ep;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001444
INADA Naoki778928b2017-08-03 23:45:15 +09001445 Py_ssize_t hashpos = lookdict_index(mp->ma_keys, hash, ix);
1446 assert(hashpos >= 0);
1447
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001448 mp->ma_used--;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001449 mp->ma_version_tag = DICT_NEXT_VERSION();
1450 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1451 dk_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
1452 ENSURE_ALLOWS_DELETIONS(mp);
1453 old_key = ep->me_key;
1454 ep->me_key = NULL;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001455 ep->me_value = NULL;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001456 Py_DECREF(old_key);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001457 Py_DECREF(old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001458
1459 assert(_PyDict_CheckConsistency(mp));
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001460 return 0;
1461}
1462
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001463int
Tim Peters1f5871e2000-07-04 17:44:48 +00001464PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001465{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001466 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001467 assert(key);
1468 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001469 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 hash = PyObject_Hash(key);
1471 if (hash == -1)
1472 return -1;
1473 }
Victor Stinner742da042016-09-07 17:40:12 -07001474
1475 return _PyDict_DelItem_KnownHash(op, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001476}
1477
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001478int
1479_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1480{
INADA Naoki778928b2017-08-03 23:45:15 +09001481 Py_ssize_t ix;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001482 PyDictObject *mp;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001483 PyObject *old_value;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001484
1485 if (!PyDict_Check(op)) {
1486 PyErr_BadInternalCall();
1487 return -1;
1488 }
1489 assert(key);
1490 assert(hash != -1);
1491 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001492 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001493 if (ix == DKIX_ERROR)
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001494 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09001495 if (ix == DKIX_EMPTY || old_value == NULL) {
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001496 _PyErr_SetKeyError(key);
1497 return -1;
1498 }
Victor Stinner78601a32016-09-09 19:28:36 -07001499
1500 // Split table doesn't allow deletion. Combine it.
1501 if (_PyDict_HasSplitTable(mp)) {
1502 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1503 return -1;
1504 }
INADA Naoki778928b2017-08-03 23:45:15 +09001505 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001506 assert(ix >= 0);
1507 }
1508
INADA Naoki778928b2017-08-03 23:45:15 +09001509 return delitem_common(mp, hash, ix, old_value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001510}
1511
Antoine Pitroud741ed42016-12-27 14:23:43 +01001512/* This function promises that the predicate -> deletion sequence is atomic
1513 * (i.e. protected by the GIL), assuming the predicate itself doesn't
1514 * release the GIL.
1515 */
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001516int
1517_PyDict_DelItemIf(PyObject *op, PyObject *key,
1518 int (*predicate)(PyObject *value))
1519{
Antoine Pitroud741ed42016-12-27 14:23:43 +01001520 Py_ssize_t hashpos, ix;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001521 PyDictObject *mp;
1522 Py_hash_t hash;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001523 PyObject *old_value;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001524 int res;
1525
1526 if (!PyDict_Check(op)) {
1527 PyErr_BadInternalCall();
1528 return -1;
1529 }
1530 assert(key);
1531 hash = PyObject_Hash(key);
1532 if (hash == -1)
1533 return -1;
1534 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001535 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001536 if (ix == DKIX_ERROR)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001537 return -1;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001538 if (ix == DKIX_EMPTY || old_value == NULL) {
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001539 _PyErr_SetKeyError(key);
1540 return -1;
1541 }
Antoine Pitroud741ed42016-12-27 14:23:43 +01001542
1543 // Split table doesn't allow deletion. Combine it.
1544 if (_PyDict_HasSplitTable(mp)) {
1545 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1546 return -1;
1547 }
INADA Naoki778928b2017-08-03 23:45:15 +09001548 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001549 assert(ix >= 0);
1550 }
1551
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001552 res = predicate(old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001553 if (res == -1)
1554 return -1;
INADA Naoki778928b2017-08-03 23:45:15 +09001555
1556 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1557 assert(hashpos >= 0);
1558
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001559 if (res > 0)
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001560 return delitem_common(mp, hashpos, ix, old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001561 else
1562 return 0;
1563}
1564
1565
Guido van Rossum25831651993-05-19 14:50:45 +00001566void
Tim Peters1f5871e2000-07-04 17:44:48 +00001567PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001568{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001570 PyDictKeysObject *oldkeys;
1571 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 if (!PyDict_Check(op))
1575 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001576 mp = ((PyDictObject *)op);
1577 oldkeys = mp->ma_keys;
1578 oldvalues = mp->ma_values;
1579 if (oldvalues == empty_values)
1580 return;
1581 /* Empty the dict... */
1582 DK_INCREF(Py_EMPTY_KEYS);
1583 mp->ma_keys = Py_EMPTY_KEYS;
1584 mp->ma_values = empty_values;
1585 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001586 mp->ma_version_tag = DICT_NEXT_VERSION();
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001587 /* ...then clear the keys and values */
1588 if (oldvalues != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001589 n = oldkeys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001590 for (i = 0; i < n; i++)
1591 Py_CLEAR(oldvalues[i]);
1592 free_values(oldvalues);
1593 DK_DECREF(oldkeys);
1594 }
1595 else {
1596 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001597 DK_DECREF(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001598 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02001599 assert(_PyDict_CheckConsistency(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001600}
1601
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001602/* Internal version of PyDict_Next that returns a hash value in addition
1603 * to the key and value.
1604 * Return 1 on success, return 0 when the reached the end of the dictionary
1605 * (or if op is not a dictionary)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001606 */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001607int
1608_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1609 PyObject **pvalue, Py_hash_t *phash)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001610{
INADA Naokica2d8be2016-11-04 16:59:10 +09001611 Py_ssize_t i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001612 PyDictObject *mp;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001613 PyDictKeyEntry *entry_ptr;
1614 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001615
1616 if (!PyDict_Check(op))
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001617 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 mp = (PyDictObject *)op;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001619 i = *ppos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001620 if (mp->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09001621 if (i < 0 || i >= mp->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001622 return 0;
INADA Naokica2d8be2016-11-04 16:59:10 +09001623 /* values of split table is always dense */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001624 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
INADA Naokica2d8be2016-11-04 16:59:10 +09001625 value = mp->ma_values[i];
1626 assert(value != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001628 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09001629 Py_ssize_t n = mp->ma_keys->dk_nentries;
1630 if (i < 0 || i >= n)
1631 return 0;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001632 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
1633 while (i < n && entry_ptr->me_value == NULL) {
1634 entry_ptr++;
1635 i++;
Victor Stinner742da042016-09-07 17:40:12 -07001636 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001637 if (i >= n)
1638 return 0;
1639 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001641 *ppos = i+1;
1642 if (pkey)
1643 *pkey = entry_ptr->me_key;
1644 if (phash)
1645 *phash = entry_ptr->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001646 if (pvalue)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001647 *pvalue = value;
1648 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001649}
1650
Tim Peters080c88b2003-02-15 03:01:11 +00001651/*
1652 * Iterate over a dict. Use like so:
1653 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001654 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001655 * PyObject *key, *value;
1656 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001657 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001658 * Refer to borrowed references in key and value.
Tim Peters080c88b2003-02-15 03:01:11 +00001659 * }
1660 *
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001661 * Return 1 on success, return 0 when the reached the end of the dictionary
1662 * (or if op is not a dictionary)
1663 *
Tim Peters080c88b2003-02-15 03:01:11 +00001664 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001665 * mutates the dict. One exception: it is safe if the loop merely changes
1666 * the values associated with the keys (but doesn't insert new keys or
1667 * delete keys), via PyDict_SetItem().
1668 */
Guido van Rossum25831651993-05-19 14:50:45 +00001669int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001670PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001671{
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001672 return _PyDict_Next(op, ppos, pkey, pvalue, NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001673}
1674
Eric Snow96c6af92015-05-29 22:21:39 -06001675/* Internal version of dict.pop(). */
1676PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001677_PyDict_Pop_KnownHash(PyObject *dict, PyObject *key, Py_hash_t hash, PyObject *deflt)
Eric Snow96c6af92015-05-29 22:21:39 -06001678{
Victor Stinner742da042016-09-07 17:40:12 -07001679 Py_ssize_t ix, hashpos;
Eric Snow96c6af92015-05-29 22:21:39 -06001680 PyObject *old_value, *old_key;
1681 PyDictKeyEntry *ep;
Yury Selivanov684ef2c2016-10-28 19:01:21 -04001682 PyDictObject *mp;
1683
1684 assert(PyDict_Check(dict));
1685 mp = (PyDictObject *)dict;
Eric Snow96c6af92015-05-29 22:21:39 -06001686
1687 if (mp->ma_used == 0) {
1688 if (deflt) {
1689 Py_INCREF(deflt);
1690 return deflt;
1691 }
1692 _PyErr_SetKeyError(key);
1693 return NULL;
1694 }
INADA Naoki778928b2017-08-03 23:45:15 +09001695 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001696 if (ix == DKIX_ERROR)
Eric Snow96c6af92015-05-29 22:21:39 -06001697 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001698 if (ix == DKIX_EMPTY || old_value == NULL) {
Eric Snow96c6af92015-05-29 22:21:39 -06001699 if (deflt) {
1700 Py_INCREF(deflt);
1701 return deflt;
1702 }
1703 _PyErr_SetKeyError(key);
1704 return NULL;
1705 }
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001706
Victor Stinner78601a32016-09-09 19:28:36 -07001707 // Split table doesn't allow deletion. Combine it.
1708 if (_PyDict_HasSplitTable(mp)) {
1709 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1710 return NULL;
1711 }
INADA Naoki778928b2017-08-03 23:45:15 +09001712 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001713 assert(ix >= 0);
1714 }
1715
INADA Naoki778928b2017-08-03 23:45:15 +09001716 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1717 assert(hashpos >= 0);
Victor Stinner78601a32016-09-09 19:28:36 -07001718 assert(old_value != NULL);
Eric Snow96c6af92015-05-29 22:21:39 -06001719 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001720 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner78601a32016-09-09 19:28:36 -07001721 dk_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
1722 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1723 ENSURE_ALLOWS_DELETIONS(mp);
1724 old_key = ep->me_key;
1725 ep->me_key = NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001726 ep->me_value = NULL;
Victor Stinner78601a32016-09-09 19:28:36 -07001727 Py_DECREF(old_key);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001728
1729 assert(_PyDict_CheckConsistency(mp));
Eric Snow96c6af92015-05-29 22:21:39 -06001730 return old_value;
1731}
1732
Serhiy Storchaka67796522017-01-12 18:34:33 +02001733PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001734_PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt)
Serhiy Storchaka67796522017-01-12 18:34:33 +02001735{
1736 Py_hash_t hash;
1737
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001738 if (((PyDictObject *)dict)->ma_used == 0) {
Serhiy Storchaka67796522017-01-12 18:34:33 +02001739 if (deflt) {
1740 Py_INCREF(deflt);
1741 return deflt;
1742 }
1743 _PyErr_SetKeyError(key);
1744 return NULL;
1745 }
1746 if (!PyUnicode_CheckExact(key) ||
1747 (hash = ((PyASCIIObject *) key)->hash) == -1) {
1748 hash = PyObject_Hash(key);
1749 if (hash == -1)
1750 return NULL;
1751 }
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001752 return _PyDict_Pop_KnownHash(dict, key, hash, deflt);
Serhiy Storchaka67796522017-01-12 18:34:33 +02001753}
1754
Eric Snow96c6af92015-05-29 22:21:39 -06001755/* Internal version of dict.from_keys(). It is subclass-friendly. */
1756PyObject *
1757_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1758{
1759 PyObject *it; /* iter(iterable) */
1760 PyObject *key;
1761 PyObject *d;
1762 int status;
1763
Victor Stinnera5ed5f02016-12-06 18:45:50 +01001764 d = _PyObject_CallNoArg(cls);
Eric Snow96c6af92015-05-29 22:21:39 -06001765 if (d == NULL)
1766 return NULL;
1767
1768 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
1769 if (PyDict_CheckExact(iterable)) {
1770 PyDictObject *mp = (PyDictObject *)d;
1771 PyObject *oldvalue;
1772 Py_ssize_t pos = 0;
1773 PyObject *key;
1774 Py_hash_t hash;
1775
Serhiy Storchakac61ac162017-03-21 08:52:38 +02001776 if (dictresize(mp, ESTIMATE_SIZE(PyDict_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001777 Py_DECREF(d);
1778 return NULL;
1779 }
1780
1781 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
1782 if (insertdict(mp, key, hash, value)) {
1783 Py_DECREF(d);
1784 return NULL;
1785 }
1786 }
1787 return d;
1788 }
1789 if (PyAnySet_CheckExact(iterable)) {
1790 PyDictObject *mp = (PyDictObject *)d;
1791 Py_ssize_t pos = 0;
1792 PyObject *key;
1793 Py_hash_t hash;
1794
Victor Stinner742da042016-09-07 17:40:12 -07001795 if (dictresize(mp, ESTIMATE_SIZE(PySet_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001796 Py_DECREF(d);
1797 return NULL;
1798 }
1799
1800 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
1801 if (insertdict(mp, key, hash, value)) {
1802 Py_DECREF(d);
1803 return NULL;
1804 }
1805 }
1806 return d;
1807 }
1808 }
1809
1810 it = PyObject_GetIter(iterable);
1811 if (it == NULL){
1812 Py_DECREF(d);
1813 return NULL;
1814 }
1815
1816 if (PyDict_CheckExact(d)) {
1817 while ((key = PyIter_Next(it)) != NULL) {
1818 status = PyDict_SetItem(d, key, value);
1819 Py_DECREF(key);
1820 if (status < 0)
1821 goto Fail;
1822 }
1823 } else {
1824 while ((key = PyIter_Next(it)) != NULL) {
1825 status = PyObject_SetItem(d, key, value);
1826 Py_DECREF(key);
1827 if (status < 0)
1828 goto Fail;
1829 }
1830 }
1831
1832 if (PyErr_Occurred())
1833 goto Fail;
1834 Py_DECREF(it);
1835 return d;
1836
1837Fail:
1838 Py_DECREF(it);
1839 Py_DECREF(d);
1840 return NULL;
1841}
1842
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001843/* Methods */
1844
1845static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001846dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001847{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001848 PyObject **values = mp->ma_values;
1849 PyDictKeysObject *keys = mp->ma_keys;
1850 Py_ssize_t i, n;
INADA Naokia6296d32017-08-24 14:55:17 +09001851
1852 /* bpo-31095: UnTrack is needed before calling any callbacks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 PyObject_GC_UnTrack(mp);
1854 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001855 if (values != NULL) {
1856 if (values != empty_values) {
Victor Stinner742da042016-09-07 17:40:12 -07001857 for (i = 0, n = mp->ma_keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001858 Py_XDECREF(values[i]);
1859 }
1860 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001861 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001862 DK_DECREF(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02001864 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001865 assert(keys->dk_refcnt == 1);
1866 DK_DECREF(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001867 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001868 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1869 free_list[numfree++] = mp;
1870 else
1871 Py_TYPE(mp)->tp_free((PyObject *)mp);
1872 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001873}
1874
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001875
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001876static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001877dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001878{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001880 PyObject *key = NULL, *value = NULL;
1881 _PyUnicodeWriter writer;
1882 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00001883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884 i = Py_ReprEnter((PyObject *)mp);
1885 if (i != 0) {
1886 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1887 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001890 Py_ReprLeave((PyObject *)mp);
1891 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 }
Tim Petersa7259592001-06-16 05:11:17 +00001893
Victor Stinnerf91929b2013-11-19 13:07:38 +01001894 _PyUnicodeWriter_Init(&writer);
1895 writer.overallocate = 1;
1896 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
1897 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00001898
Victor Stinnerf91929b2013-11-19 13:07:38 +01001899 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
1900 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 /* Do repr() on each key+value pair, and insert ": " between them.
1903 Note that repr may mutate the dict. */
1904 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001905 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001907 PyObject *s;
1908 int res;
1909
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001910 /* Prevent repr from deleting key or value during key format. */
1911 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02001913
Victor Stinnerf91929b2013-11-19 13:07:38 +01001914 if (!first) {
1915 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
1916 goto error;
1917 }
1918 first = 0;
1919
1920 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01001922 goto error;
1923 res = _PyUnicodeWriter_WriteStr(&writer, s);
1924 Py_DECREF(s);
1925 if (res < 0)
1926 goto error;
1927
1928 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
1929 goto error;
1930
1931 s = PyObject_Repr(value);
1932 if (s == NULL)
1933 goto error;
1934 res = _PyUnicodeWriter_WriteStr(&writer, s);
1935 Py_DECREF(s);
1936 if (res < 0)
1937 goto error;
1938
1939 Py_CLEAR(key);
1940 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 }
Tim Petersa7259592001-06-16 05:11:17 +00001942
Victor Stinnerf91929b2013-11-19 13:07:38 +01001943 writer.overallocate = 0;
1944 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
1945 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01001948
1949 return _PyUnicodeWriter_Finish(&writer);
1950
1951error:
1952 Py_ReprLeave((PyObject *)mp);
1953 _PyUnicodeWriter_Dealloc(&writer);
1954 Py_XDECREF(key);
1955 Py_XDECREF(value);
1956 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001957}
1958
Martin v. Löwis18e16552006-02-15 17:27:45 +00001959static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001960dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001961{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001962 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001963}
1964
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001965static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001966dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001967{
Victor Stinner742da042016-09-07 17:40:12 -07001968 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001969 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09001970 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001973 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 hash = PyObject_Hash(key);
1975 if (hash == -1)
1976 return NULL;
1977 }
INADA Naoki778928b2017-08-03 23:45:15 +09001978 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001979 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001981 if (ix == DKIX_EMPTY || value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001982 if (!PyDict_CheckExact(mp)) {
1983 /* Look up __missing__ method if we're a subclass. */
1984 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05001985 _Py_IDENTIFIER(__missing__);
1986 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987 if (missing != NULL) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001988 res = PyObject_CallFunctionObjArgs(missing,
1989 key, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001990 Py_DECREF(missing);
1991 return res;
1992 }
1993 else if (PyErr_Occurred())
1994 return NULL;
1995 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07001996 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997 return NULL;
1998 }
INADA Naokiba609772016-12-07 20:41:42 +09001999 Py_INCREF(value);
2000 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002001}
2002
2003static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002004dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002005{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 if (w == NULL)
2007 return PyDict_DelItem((PyObject *)mp, v);
2008 else
2009 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002010}
2011
Guido van Rossuma9e7a811997-05-13 21:02:11 +00002012static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002013 (lenfunc)dict_length, /*mp_length*/
2014 (binaryfunc)dict_subscript, /*mp_subscript*/
2015 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002016};
2017
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002018static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002019dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002020{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002021 PyObject *v;
2022 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002023 PyDictKeyEntry *ep;
2024 Py_ssize_t size, n, offset;
2025 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002026
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002027 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002028 n = mp->ma_used;
2029 v = PyList_New(n);
2030 if (v == NULL)
2031 return NULL;
2032 if (n != mp->ma_used) {
2033 /* Durnit. The allocations caused the dict to resize.
2034 * Just start over, this shouldn't normally happen.
2035 */
2036 Py_DECREF(v);
2037 goto again;
2038 }
Victor Stinner742da042016-09-07 17:40:12 -07002039 ep = DK_ENTRIES(mp->ma_keys);
2040 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002041 if (mp->ma_values) {
2042 value_ptr = mp->ma_values;
2043 offset = sizeof(PyObject *);
2044 }
2045 else {
2046 value_ptr = &ep[0].me_value;
2047 offset = sizeof(PyDictKeyEntry);
2048 }
2049 for (i = 0, j = 0; i < size; i++) {
2050 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002051 PyObject *key = ep[i].me_key;
2052 Py_INCREF(key);
2053 PyList_SET_ITEM(v, j, key);
2054 j++;
2055 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002056 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002057 }
2058 assert(j == n);
2059 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002060}
2061
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002062static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002063dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002064{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002065 PyObject *v;
2066 Py_ssize_t i, j;
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002067 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002068 Py_ssize_t size, n, offset;
2069 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002070
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002071 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002072 n = mp->ma_used;
2073 v = PyList_New(n);
2074 if (v == NULL)
2075 return NULL;
2076 if (n != mp->ma_used) {
2077 /* Durnit. The allocations caused the dict to resize.
2078 * Just start over, this shouldn't normally happen.
2079 */
2080 Py_DECREF(v);
2081 goto again;
2082 }
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002083 ep = DK_ENTRIES(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002084 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002085 if (mp->ma_values) {
2086 value_ptr = mp->ma_values;
2087 offset = sizeof(PyObject *);
2088 }
2089 else {
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002090 value_ptr = &ep[0].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002091 offset = sizeof(PyDictKeyEntry);
2092 }
2093 for (i = 0, j = 0; i < size; i++) {
2094 PyObject *value = *value_ptr;
2095 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2096 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002097 Py_INCREF(value);
2098 PyList_SET_ITEM(v, j, value);
2099 j++;
2100 }
2101 }
2102 assert(j == n);
2103 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002104}
2105
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002106static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002107dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002108{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002109 PyObject *v;
2110 Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002111 Py_ssize_t size, offset;
2112 PyObject *item, *key;
2113 PyDictKeyEntry *ep;
2114 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002116 /* Preallocate the list of tuples, to avoid allocations during
2117 * the loop over the items, which could trigger GC, which
2118 * could resize the dict. :-(
2119 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002120 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 n = mp->ma_used;
2122 v = PyList_New(n);
2123 if (v == NULL)
2124 return NULL;
2125 for (i = 0; i < n; i++) {
2126 item = PyTuple_New(2);
2127 if (item == NULL) {
2128 Py_DECREF(v);
2129 return NULL;
2130 }
2131 PyList_SET_ITEM(v, i, item);
2132 }
2133 if (n != mp->ma_used) {
2134 /* Durnit. The allocations caused the dict to resize.
2135 * Just start over, this shouldn't normally happen.
2136 */
2137 Py_DECREF(v);
2138 goto again;
2139 }
2140 /* Nothing we do below makes any function calls. */
Victor Stinner742da042016-09-07 17:40:12 -07002141 ep = DK_ENTRIES(mp->ma_keys);
2142 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002143 if (mp->ma_values) {
2144 value_ptr = mp->ma_values;
2145 offset = sizeof(PyObject *);
2146 }
2147 else {
2148 value_ptr = &ep[0].me_value;
2149 offset = sizeof(PyDictKeyEntry);
2150 }
2151 for (i = 0, j = 0; i < size; i++) {
2152 PyObject *value = *value_ptr;
2153 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2154 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002155 key = ep[i].me_key;
2156 item = PyList_GET_ITEM(v, j);
2157 Py_INCREF(key);
2158 PyTuple_SET_ITEM(item, 0, key);
2159 Py_INCREF(value);
2160 PyTuple_SET_ITEM(item, 1, value);
2161 j++;
2162 }
2163 }
2164 assert(j == n);
2165 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002166}
2167
Larry Hastings5c661892014-01-24 06:17:25 -08002168/*[clinic input]
2169@classmethod
2170dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08002171 iterable: object
2172 value: object=None
2173 /
2174
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002175Create a new dictionary with keys from iterable and values set to value.
Larry Hastings5c661892014-01-24 06:17:25 -08002176[clinic start generated code]*/
2177
Larry Hastings5c661892014-01-24 06:17:25 -08002178static PyObject *
2179dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002180/*[clinic end generated code: output=8fb98e4b10384999 input=382ba4855d0f74c3]*/
Larry Hastings5c661892014-01-24 06:17:25 -08002181{
Eric Snow96c6af92015-05-29 22:21:39 -06002182 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002183}
2184
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002185static int
Victor Stinner742da042016-09-07 17:40:12 -07002186dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
2187 const char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002188{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002189 PyObject *arg = NULL;
2190 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
2193 result = -1;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002196 _Py_IDENTIFIER(keys);
2197 if (_PyObject_HasAttrId(arg, &PyId_keys))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198 result = PyDict_Merge(self, arg, 1);
2199 else
2200 result = PyDict_MergeFromSeq2(self, arg, 1);
2201 }
2202 if (result == 0 && kwds != NULL) {
2203 if (PyArg_ValidateKeywordArguments(kwds))
2204 result = PyDict_Merge(self, kwds, 1);
2205 else
2206 result = -1;
2207 }
2208 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002209}
2210
Victor Stinner91f0d4a2017-01-19 12:45:06 +01002211/* Note: dict.update() uses the METH_VARARGS|METH_KEYWORDS calling convention.
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002212 Using METH_FASTCALL|METH_KEYWORDS would make dict.update(**dict2) calls
2213 slower, see the issue #29312. */
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002214static PyObject *
2215dict_update(PyObject *self, PyObject *args, PyObject *kwds)
2216{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002217 if (dict_update_common(self, args, kwds, "update") != -1)
2218 Py_RETURN_NONE;
2219 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002220}
2221
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002222/* Update unconditionally replaces existing items.
2223 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00002224 otherwise it leaves existing items unchanged.
2225
2226 PyDict_{Update,Merge} update/merge from a mapping object.
2227
Tim Petersf582b822001-12-11 18:51:08 +00002228 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00002229 producing iterable objects of length 2.
2230*/
2231
Tim Petersf582b822001-12-11 18:51:08 +00002232int
Tim Peters1fc240e2001-10-26 05:06:50 +00002233PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
2234{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002235 PyObject *it; /* iter(seq2) */
2236 Py_ssize_t i; /* index into seq2 of current element */
2237 PyObject *item; /* seq2[i] */
2238 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00002239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002240 assert(d != NULL);
2241 assert(PyDict_Check(d));
2242 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00002243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002244 it = PyObject_GetIter(seq2);
2245 if (it == NULL)
2246 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002248 for (i = 0; ; ++i) {
2249 PyObject *key, *value;
2250 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00002251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002252 fast = NULL;
2253 item = PyIter_Next(it);
2254 if (item == NULL) {
2255 if (PyErr_Occurred())
2256 goto Fail;
2257 break;
2258 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002260 /* Convert item to sequence, and verify length 2. */
2261 fast = PySequence_Fast(item, "");
2262 if (fast == NULL) {
2263 if (PyErr_ExceptionMatches(PyExc_TypeError))
2264 PyErr_Format(PyExc_TypeError,
2265 "cannot convert dictionary update "
2266 "sequence element #%zd to a sequence",
2267 i);
2268 goto Fail;
2269 }
2270 n = PySequence_Fast_GET_SIZE(fast);
2271 if (n != 2) {
2272 PyErr_Format(PyExc_ValueError,
2273 "dictionary update sequence element #%zd "
2274 "has length %zd; 2 is required",
2275 i, n);
2276 goto Fail;
2277 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002279 /* Update/merge with this (key, value) pair. */
2280 key = PySequence_Fast_GET_ITEM(fast, 0);
2281 value = PySequence_Fast_GET_ITEM(fast, 1);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002282 Py_INCREF(key);
2283 Py_INCREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002284 if (override || PyDict_GetItem(d, key) == NULL) {
2285 int status = PyDict_SetItem(d, key, value);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002286 if (status < 0) {
2287 Py_DECREF(key);
2288 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002289 goto Fail;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002290 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002292 Py_DECREF(key);
2293 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 Py_DECREF(fast);
2295 Py_DECREF(item);
2296 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 i = 0;
Victor Stinner611b0fa2016-09-14 15:02:01 +02002299 assert(_PyDict_CheckConsistency((PyDictObject *)d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002300 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002301Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002302 Py_XDECREF(item);
2303 Py_XDECREF(fast);
2304 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002305Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002306 Py_DECREF(it);
2307 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002308}
2309
doko@ubuntu.comc96df682016-10-11 08:04:02 +02002310static int
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002311dict_merge(PyObject *a, PyObject *b, int override)
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002312{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002313 PyDictObject *mp, *other;
2314 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -07002315 PyDictKeyEntry *entry, *ep0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002316
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002317 assert(0 <= override && override <= 2);
2318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002319 /* We accept for the argument either a concrete dictionary object,
2320 * or an abstract "mapping" object. For the former, we can do
2321 * things quite efficiently. For the latter, we only require that
2322 * PyMapping_Keys() and PyObject_GetItem() be supported.
2323 */
2324 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2325 PyErr_BadInternalCall();
2326 return -1;
2327 }
2328 mp = (PyDictObject*)a;
2329 if (PyDict_Check(b)) {
2330 other = (PyDictObject*)b;
2331 if (other == mp || other->ma_used == 0)
2332 /* a.update(a) or a.update({}); nothing to do */
2333 return 0;
2334 if (mp->ma_used == 0)
2335 /* Since the target dict is empty, PyDict_GetItem()
2336 * always returns NULL. Setting override to 1
2337 * skips the unnecessary test.
2338 */
2339 override = 1;
2340 /* Do one big resize at the start, rather than
2341 * incrementally resizing as we insert new items. Expect
2342 * that there will be no (or few) overlapping keys.
2343 */
INADA Naokib1152be2016-10-27 19:26:50 +09002344 if (USABLE_FRACTION(mp->ma_keys->dk_size) < other->ma_used) {
2345 if (dictresize(mp, ESTIMATE_SIZE(mp->ma_used + other->ma_used))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 return -1;
INADA Naokib1152be2016-10-27 19:26:50 +09002347 }
2348 }
Victor Stinner742da042016-09-07 17:40:12 -07002349 ep0 = DK_ENTRIES(other->ma_keys);
2350 for (i = 0, n = other->ma_keys->dk_nentries; i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002351 PyObject *key, *value;
2352 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002353 entry = &ep0[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002354 key = entry->me_key;
2355 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002356 if (other->ma_values)
2357 value = other->ma_values[i];
2358 else
2359 value = entry->me_value;
2360
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002361 if (value != NULL) {
2362 int err = 0;
2363 Py_INCREF(key);
2364 Py_INCREF(value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002365 if (override == 1)
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002366 err = insertdict(mp, key, hash, value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002367 else if (_PyDict_GetItem_KnownHash(a, key, hash) == NULL) {
2368 if (PyErr_Occurred()) {
2369 Py_DECREF(value);
2370 Py_DECREF(key);
2371 return -1;
2372 }
2373 err = insertdict(mp, key, hash, value);
2374 }
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002375 else if (override != 0) {
2376 _PyErr_SetKeyError(key);
2377 Py_DECREF(value);
2378 Py_DECREF(key);
2379 return -1;
2380 }
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002381 Py_DECREF(value);
2382 Py_DECREF(key);
2383 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002384 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002385
Victor Stinner742da042016-09-07 17:40:12 -07002386 if (n != other->ma_keys->dk_nentries) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002387 PyErr_SetString(PyExc_RuntimeError,
2388 "dict mutated during update");
2389 return -1;
2390 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 }
2392 }
2393 }
2394 else {
2395 /* Do it the generic, slower way */
2396 PyObject *keys = PyMapping_Keys(b);
2397 PyObject *iter;
2398 PyObject *key, *value;
2399 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 if (keys == NULL)
2402 /* Docstring says this is equivalent to E.keys() so
2403 * if E doesn't have a .keys() method we want
2404 * AttributeError to percolate up. Might as well
2405 * do the same for any other error.
2406 */
2407 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002409 iter = PyObject_GetIter(keys);
2410 Py_DECREF(keys);
2411 if (iter == NULL)
2412 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002414 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002415 if (override != 1 && PyDict_GetItem(a, key) != NULL) {
2416 if (override != 0) {
2417 _PyErr_SetKeyError(key);
2418 Py_DECREF(key);
2419 Py_DECREF(iter);
2420 return -1;
2421 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002422 Py_DECREF(key);
2423 continue;
2424 }
2425 value = PyObject_GetItem(b, key);
2426 if (value == NULL) {
2427 Py_DECREF(iter);
2428 Py_DECREF(key);
2429 return -1;
2430 }
2431 status = PyDict_SetItem(a, key, value);
2432 Py_DECREF(key);
2433 Py_DECREF(value);
2434 if (status < 0) {
2435 Py_DECREF(iter);
2436 return -1;
2437 }
2438 }
2439 Py_DECREF(iter);
2440 if (PyErr_Occurred())
2441 /* Iterator completed, via error */
2442 return -1;
2443 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02002444 assert(_PyDict_CheckConsistency((PyDictObject *)a));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002445 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002446}
2447
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002448int
2449PyDict_Update(PyObject *a, PyObject *b)
2450{
2451 return dict_merge(a, b, 1);
2452}
2453
2454int
2455PyDict_Merge(PyObject *a, PyObject *b, int override)
2456{
2457 /* XXX Deprecate override not in (0, 1). */
2458 return dict_merge(a, b, override != 0);
2459}
2460
2461int
2462_PyDict_MergeEx(PyObject *a, PyObject *b, int override)
2463{
2464 return dict_merge(a, b, override);
2465}
2466
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002467static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002468dict_copy(PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002469{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002470 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002471}
2472
2473PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002474PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002475{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002476 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002477 PyDictObject *mp;
2478 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002480 if (o == NULL || !PyDict_Check(o)) {
2481 PyErr_BadInternalCall();
2482 return NULL;
2483 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002484 mp = (PyDictObject *)o;
2485 if (_PyDict_HasSplitTable(mp)) {
2486 PyDictObject *split_copy;
Victor Stinner742da042016-09-07 17:40:12 -07002487 Py_ssize_t size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
2488 PyObject **newvalues;
2489 newvalues = new_values(size);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002490 if (newvalues == NULL)
2491 return PyErr_NoMemory();
2492 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2493 if (split_copy == NULL) {
2494 free_values(newvalues);
2495 return NULL;
2496 }
2497 split_copy->ma_values = newvalues;
2498 split_copy->ma_keys = mp->ma_keys;
2499 split_copy->ma_used = mp->ma_used;
2500 DK_INCREF(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002501 for (i = 0, n = size; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002502 PyObject *value = mp->ma_values[i];
2503 Py_XINCREF(value);
2504 split_copy->ma_values[i] = value;
2505 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002506 if (_PyObject_GC_IS_TRACKED(mp))
2507 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002508 return (PyObject *)split_copy;
2509 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002510 copy = PyDict_New();
2511 if (copy == NULL)
2512 return NULL;
2513 if (PyDict_Merge(copy, o, 1) == 0)
2514 return copy;
2515 Py_DECREF(copy);
2516 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002517}
2518
Martin v. Löwis18e16552006-02-15 17:27:45 +00002519Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002520PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002521{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002522 if (mp == NULL || !PyDict_Check(mp)) {
2523 PyErr_BadInternalCall();
2524 return -1;
2525 }
2526 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002527}
2528
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002529PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002530PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002531{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002532 if (mp == NULL || !PyDict_Check(mp)) {
2533 PyErr_BadInternalCall();
2534 return NULL;
2535 }
2536 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002537}
2538
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002539PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002540PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002542 if (mp == NULL || !PyDict_Check(mp)) {
2543 PyErr_BadInternalCall();
2544 return NULL;
2545 }
2546 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002547}
2548
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002549PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002550PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002551{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002552 if (mp == NULL || !PyDict_Check(mp)) {
2553 PyErr_BadInternalCall();
2554 return NULL;
2555 }
2556 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002557}
2558
Tim Peterse63415e2001-05-08 04:38:29 +00002559/* Return 1 if dicts equal, 0 if not, -1 if error.
2560 * Gets out as soon as any difference is detected.
2561 * Uses only Py_EQ comparison.
2562 */
2563static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002564dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002565{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002566 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002568 if (a->ma_used != b->ma_used)
2569 /* can't be equal if # of entries differ */
2570 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002571 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Victor Stinner742da042016-09-07 17:40:12 -07002572 for (i = 0; i < a->ma_keys->dk_nentries; i++) {
2573 PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002574 PyObject *aval;
2575 if (a->ma_values)
2576 aval = a->ma_values[i];
2577 else
2578 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002579 if (aval != NULL) {
2580 int cmp;
2581 PyObject *bval;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002582 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002583 /* temporarily bump aval's refcount to ensure it stays
2584 alive until we're done with it */
2585 Py_INCREF(aval);
2586 /* ditto for key */
2587 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002588 /* reuse the known hash value */
INADA Naoki778928b2017-08-03 23:45:15 +09002589 b->ma_keys->dk_lookup(b, key, ep->me_hash, &bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002590 if (bval == NULL) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002591 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002592 Py_DECREF(aval);
2593 if (PyErr_Occurred())
2594 return -1;
2595 return 0;
2596 }
2597 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002598 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002599 Py_DECREF(aval);
2600 if (cmp <= 0) /* error or not equal */
2601 return cmp;
2602 }
2603 }
2604 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002605}
Tim Peterse63415e2001-05-08 04:38:29 +00002606
2607static PyObject *
2608dict_richcompare(PyObject *v, PyObject *w, int op)
2609{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002610 int cmp;
2611 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002613 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2614 res = Py_NotImplemented;
2615 }
2616 else if (op == Py_EQ || op == Py_NE) {
2617 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2618 if (cmp < 0)
2619 return NULL;
2620 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2621 }
2622 else
2623 res = Py_NotImplemented;
2624 Py_INCREF(res);
2625 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002626}
Tim Peterse63415e2001-05-08 04:38:29 +00002627
Larry Hastings61272b72014-01-07 12:41:53 -08002628/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002629
2630@coexist
2631dict.__contains__
2632
2633 key: object
2634 /
2635
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002636True if the dictionary has the specified key, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002637[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002638
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002639static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002640dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka19d25972017-02-04 08:05:07 +02002641/*[clinic end generated code: output=a3d03db709ed6e6b input=fe1cb42ad831e820]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002642{
Larry Hastingsc2047262014-01-25 20:43:29 -08002643 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002644 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002645 Py_ssize_t ix;
INADA Naokiba609772016-12-07 20:41:42 +09002646 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002648 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002649 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002650 hash = PyObject_Hash(key);
2651 if (hash == -1)
2652 return NULL;
2653 }
INADA Naoki778928b2017-08-03 23:45:15 +09002654 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002655 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002656 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002657 if (ix == DKIX_EMPTY || value == NULL)
Victor Stinner742da042016-09-07 17:40:12 -07002658 Py_RETURN_FALSE;
2659 Py_RETURN_TRUE;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002660}
2661
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002662/*[clinic input]
2663dict.get
2664
2665 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002666 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002667 /
2668
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002669Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002670[clinic start generated code]*/
2671
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002672static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002673dict_get_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002674/*[clinic end generated code: output=bba707729dee05bf input=279ddb5790b6b107]*/
Barry Warsawc38c5da1997-10-06 17:49:20 +00002675{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002676 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002677 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002678 Py_ssize_t ix;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002680 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002681 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002682 hash = PyObject_Hash(key);
2683 if (hash == -1)
2684 return NULL;
2685 }
INADA Naoki778928b2017-08-03 23:45:15 +09002686 ix = (self->ma_keys->dk_lookup) (self, key, hash, &val);
Victor Stinner742da042016-09-07 17:40:12 -07002687 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002688 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002689 if (ix == DKIX_EMPTY || val == NULL) {
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002690 val = default_value;
INADA Naokiba609772016-12-07 20:41:42 +09002691 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002692 Py_INCREF(val);
2693 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002694}
2695
Benjamin Peterson00e98862013-03-07 22:16:29 -05002696PyObject *
2697PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002698{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002699 PyDictObject *mp = (PyDictObject *)d;
INADA Naoki93f26f72016-11-02 18:45:16 +09002700 PyObject *value;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002701 Py_hash_t hash;
Guido van Rossum164452c2000-08-08 16:12:54 +00002702
Benjamin Peterson00e98862013-03-07 22:16:29 -05002703 if (!PyDict_Check(d)) {
2704 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002705 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002706 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002708 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002709 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002710 hash = PyObject_Hash(key);
2711 if (hash == -1)
2712 return NULL;
2713 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002714
2715 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
2716 if (insertion_resize(mp) < 0)
2717 return NULL;
2718 }
2719
INADA Naoki778928b2017-08-03 23:45:15 +09002720 Py_ssize_t ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002721 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002722 return NULL;
INADA Naoki93f26f72016-11-02 18:45:16 +09002723
2724 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09002725 ((ix >= 0 && value == NULL && mp->ma_used != ix) ||
INADA Naoki93f26f72016-11-02 18:45:16 +09002726 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
2727 if (insertion_resize(mp) < 0) {
2728 return NULL;
2729 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002730 ix = DKIX_EMPTY;
2731 }
2732
2733 if (ix == DKIX_EMPTY) {
2734 PyDictKeyEntry *ep, *ep0;
2735 value = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002736 if (mp->ma_keys->dk_usable <= 0) {
Victor Stinner3c336c52016-09-12 14:17:40 +02002737 if (insertion_resize(mp) < 0) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002738 return NULL;
Victor Stinner3c336c52016-09-12 14:17:40 +02002739 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002740 }
INADA Naoki778928b2017-08-03 23:45:15 +09002741 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naoki93f26f72016-11-02 18:45:16 +09002742 ep0 = DK_ENTRIES(mp->ma_keys);
2743 ep = &ep0[mp->ma_keys->dk_nentries];
2744 dk_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002745 Py_INCREF(key);
INADA Naoki93f26f72016-11-02 18:45:16 +09002746 Py_INCREF(value);
2747 MAINTAIN_TRACKING(mp, key, value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002748 ep->me_key = key;
2749 ep->me_hash = hash;
INADA Naokiba609772016-12-07 20:41:42 +09002750 if (_PyDict_HasSplitTable(mp)) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002751 assert(mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
2752 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Victor Stinner742da042016-09-07 17:40:12 -07002753 }
2754 else {
INADA Naoki93f26f72016-11-02 18:45:16 +09002755 ep->me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07002756 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002757 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002758 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naoki93f26f72016-11-02 18:45:16 +09002759 mp->ma_keys->dk_usable--;
2760 mp->ma_keys->dk_nentries++;
2761 assert(mp->ma_keys->dk_usable >= 0);
2762 }
INADA Naokiba609772016-12-07 20:41:42 +09002763 else if (value == NULL) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002764 value = defaultobj;
2765 assert(_PyDict_HasSplitTable(mp));
2766 assert(ix == mp->ma_used);
2767 Py_INCREF(value);
2768 MAINTAIN_TRACKING(mp, key, value);
INADA Naokiba609772016-12-07 20:41:42 +09002769 mp->ma_values[ix] = value;
INADA Naoki93f26f72016-11-02 18:45:16 +09002770 mp->ma_used++;
2771 mp->ma_version_tag = DICT_NEXT_VERSION();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002772 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002773
2774 assert(_PyDict_CheckConsistency(mp));
2775 return value;
Guido van Rossum164452c2000-08-08 16:12:54 +00002776}
2777
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002778/*[clinic input]
2779dict.setdefault
2780
2781 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002782 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002783 /
2784
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002785Insert key with a value of default if key is not in the dictionary.
2786
2787Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002788[clinic start generated code]*/
2789
Benjamin Peterson00e98862013-03-07 22:16:29 -05002790static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002791dict_setdefault_impl(PyDictObject *self, PyObject *key,
2792 PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002793/*[clinic end generated code: output=f8c1101ebf69e220 input=0f063756e815fd9d]*/
Benjamin Peterson00e98862013-03-07 22:16:29 -05002794{
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002795 PyObject *val;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002796
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002797 val = PyDict_SetDefault((PyObject *)self, key, default_value);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002798 Py_XINCREF(val);
2799 return val;
2800}
Guido van Rossum164452c2000-08-08 16:12:54 +00002801
2802static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002803dict_clear(PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002804{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002805 PyDict_Clear((PyObject *)mp);
2806 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002807}
2808
Guido van Rossumba6ab842000-12-12 22:02:18 +00002809static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002810dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002811{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002812 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00002813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002814 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2815 return NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06002816
Yury Selivanov684ef2c2016-10-28 19:01:21 -04002817 return _PyDict_Pop((PyObject*)mp, key, deflt);
Guido van Rossume027d982002-04-12 15:11:59 +00002818}
2819
2820static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002821dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002822{
Victor Stinner742da042016-09-07 17:40:12 -07002823 Py_ssize_t i, j;
2824 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002825 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002827 /* Allocate the result tuple before checking the size. Believe it
2828 * or not, this allocation could trigger a garbage collection which
2829 * could empty the dict, so if we checked the size first and that
2830 * happened, the result would be an infinite loop (searching for an
2831 * entry that no longer exists). Note that the usual popitem()
2832 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2833 * tuple away if the dict *is* empty isn't a significant
2834 * inefficiency -- possible, but unlikely in practice.
2835 */
2836 res = PyTuple_New(2);
2837 if (res == NULL)
2838 return NULL;
2839 if (mp->ma_used == 0) {
2840 Py_DECREF(res);
2841 PyErr_SetString(PyExc_KeyError,
2842 "popitem(): dictionary is empty");
2843 return NULL;
2844 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002845 /* Convert split table to combined table */
2846 if (mp->ma_keys->dk_lookup == lookdict_split) {
2847 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2848 Py_DECREF(res);
2849 return NULL;
2850 }
2851 }
2852 ENSURE_ALLOWS_DELETIONS(mp);
Victor Stinner742da042016-09-07 17:40:12 -07002853
2854 /* Pop last item */
2855 ep0 = DK_ENTRIES(mp->ma_keys);
2856 i = mp->ma_keys->dk_nentries - 1;
2857 while (i >= 0 && ep0[i].me_value == NULL) {
2858 i--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002859 }
Victor Stinner742da042016-09-07 17:40:12 -07002860 assert(i >= 0);
2861
2862 ep = &ep0[i];
2863 j = lookdict_index(mp->ma_keys, ep->me_hash, i);
2864 assert(j >= 0);
2865 assert(dk_get_index(mp->ma_keys, j) == i);
2866 dk_set_index(mp->ma_keys, j, DKIX_DUMMY);
2867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 PyTuple_SET_ITEM(res, 0, ep->me_key);
2869 PyTuple_SET_ITEM(res, 1, ep->me_value);
Victor Stinner742da042016-09-07 17:40:12 -07002870 ep->me_key = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002871 ep->me_value = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002872 /* We can't dk_usable++ since there is DKIX_DUMMY in indices */
2873 mp->ma_keys->dk_nentries = i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002875 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02002876 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002877 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002878}
2879
Jeremy Hylton8caad492000-06-23 14:18:11 +00002880static int
2881dict_traverse(PyObject *op, visitproc visit, void *arg)
2882{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002883 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson55f44522016-09-05 12:12:59 -07002884 PyDictKeysObject *keys = mp->ma_keys;
Serhiy Storchaka46825d22016-09-26 21:29:34 +03002885 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Victor Stinner742da042016-09-07 17:40:12 -07002886 Py_ssize_t i, n = keys->dk_nentries;
2887
Benjamin Peterson55f44522016-09-05 12:12:59 -07002888 if (keys->dk_lookup == lookdict) {
2889 for (i = 0; i < n; i++) {
2890 if (entries[i].me_value != NULL) {
2891 Py_VISIT(entries[i].me_value);
2892 Py_VISIT(entries[i].me_key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002893 }
2894 }
Victor Stinner742da042016-09-07 17:40:12 -07002895 }
2896 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002897 if (mp->ma_values != NULL) {
Benjamin Peterson55f44522016-09-05 12:12:59 -07002898 for (i = 0; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002899 Py_VISIT(mp->ma_values[i]);
2900 }
2901 }
2902 else {
Benjamin Peterson55f44522016-09-05 12:12:59 -07002903 for (i = 0; i < n; i++) {
2904 Py_VISIT(entries[i].me_value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002905 }
2906 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002907 }
2908 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002909}
2910
2911static int
2912dict_tp_clear(PyObject *op)
2913{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002914 PyDict_Clear(op);
2915 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002916}
2917
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002918static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002919
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002920Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06002921_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002922{
Victor Stinner742da042016-09-07 17:40:12 -07002923 Py_ssize_t size, usable, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002924
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002925 size = DK_SIZE(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002926 usable = USABLE_FRACTION(size);
2927
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002928 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002929 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -07002930 res += usable * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002931 /* If the dictionary is split, the keys portion is accounted-for
2932 in the type object. */
2933 if (mp->ma_keys->dk_refcnt == 1)
Victor Stinner98ee9d52016-09-08 09:33:56 -07002934 res += (sizeof(PyDictKeysObject)
2935 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
2936 + DK_IXSIZE(mp->ma_keys) * size
2937 + sizeof(PyDictKeyEntry) * usable);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002938 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002939}
2940
2941Py_ssize_t
2942_PyDict_KeysSize(PyDictKeysObject *keys)
2943{
Victor Stinner98ee9d52016-09-08 09:33:56 -07002944 return (sizeof(PyDictKeysObject)
2945 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
2946 + DK_IXSIZE(keys) * DK_SIZE(keys)
2947 + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002948}
2949
doko@ubuntu.com17210f52016-01-14 14:04:59 +01002950static PyObject *
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002951dict_sizeof(PyDictObject *mp)
2952{
2953 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
2954}
2955
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002956PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
2957
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002958PyDoc_STRVAR(sizeof__doc__,
2959"D.__sizeof__() -> size of D in memory, in bytes");
2960
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002961PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002962"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002963If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00002964
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002965PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00002966"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000029672-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002968
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002969PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04002970"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
2971If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
2972If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
2973In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00002974
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002975PyDoc_STRVAR(clear__doc__,
2976"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002977
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002978PyDoc_STRVAR(copy__doc__,
2979"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00002980
Guido van Rossumb90c8482007-02-10 01:11:45 +00002981/* Forward */
2982static PyObject *dictkeys_new(PyObject *);
2983static PyObject *dictitems_new(PyObject *);
2984static PyObject *dictvalues_new(PyObject *);
2985
Guido van Rossum45c85d12007-07-27 16:31:40 +00002986PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002987 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002988PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002989 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002990PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002991 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00002992
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002993static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07002994 DICT___CONTAINS___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002995 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
2996 getitem__doc__},
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002997 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002998 sizeof__doc__},
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002999 DICT_GET_METHODDEF
3000 DICT_SETDEFAULT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003001 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
3002 pop__doc__},
3003 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
3004 popitem__doc__},
3005 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
3006 keys__doc__},
3007 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
3008 items__doc__},
3009 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
3010 values__doc__},
3011 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
3012 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08003013 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003014 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
3015 clear__doc__},
3016 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
3017 copy__doc__},
3018 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003019};
3020
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003021/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00003022int
3023PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003024{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003025 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07003026 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003027 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003028 PyObject *value;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003030 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003031 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003032 hash = PyObject_Hash(key);
3033 if (hash == -1)
3034 return -1;
3035 }
INADA Naoki778928b2017-08-03 23:45:15 +09003036 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003037 if (ix == DKIX_ERROR)
3038 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003039 return (ix != DKIX_EMPTY && value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003040}
3041
Thomas Wouterscf297e42007-02-23 15:07:44 +00003042/* Internal version of PyDict_Contains used when the hash value is already known */
3043int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003044_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00003045{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003046 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003047 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07003048 Py_ssize_t ix;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003049
INADA Naoki778928b2017-08-03 23:45:15 +09003050 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003051 if (ix == DKIX_ERROR)
3052 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003053 return (ix != DKIX_EMPTY && value != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00003054}
3055
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003056/* Hack to implement "key in dict" */
3057static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003058 0, /* sq_length */
3059 0, /* sq_concat */
3060 0, /* sq_repeat */
3061 0, /* sq_item */
3062 0, /* sq_slice */
3063 0, /* sq_ass_item */
3064 0, /* sq_ass_slice */
3065 PyDict_Contains, /* sq_contains */
3066 0, /* sq_inplace_concat */
3067 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003068};
3069
Guido van Rossum09e563a2001-05-01 12:10:21 +00003070static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003071dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3072{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003073 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003074 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003076 assert(type != NULL && type->tp_alloc != NULL);
3077 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02003078 if (self == NULL)
3079 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003080 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003081
Victor Stinnera9f61a52013-07-16 22:17:26 +02003082 /* The object has been implicitly tracked by tp_alloc */
3083 if (type == &PyDict_Type)
3084 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003085
3086 d->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003087 d->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07003088 d->ma_keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003089 if (d->ma_keys == NULL) {
3090 Py_DECREF(self);
3091 return NULL;
3092 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02003093 assert(_PyDict_CheckConsistency(d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003094 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003095}
3096
Tim Peters25786c02001-09-02 08:22:48 +00003097static int
3098dict_init(PyObject *self, PyObject *args, PyObject *kwds)
3099{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003100 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00003101}
3102
Tim Peters6d6c1a32001-08-02 04:15:00 +00003103static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003104dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00003105{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003106 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003107}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003108
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003109PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00003110"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00003111"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003112" (key, value) pairs\n"
3113"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00003114" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003115" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00003116" d[k] = v\n"
3117"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
3118" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00003119
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003120PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003121 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3122 "dict",
3123 sizeof(PyDictObject),
3124 0,
3125 (destructor)dict_dealloc, /* tp_dealloc */
3126 0, /* tp_print */
3127 0, /* tp_getattr */
3128 0, /* tp_setattr */
3129 0, /* tp_reserved */
3130 (reprfunc)dict_repr, /* tp_repr */
3131 0, /* tp_as_number */
3132 &dict_as_sequence, /* tp_as_sequence */
3133 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003134 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003135 0, /* tp_call */
3136 0, /* tp_str */
3137 PyObject_GenericGetAttr, /* tp_getattro */
3138 0, /* tp_setattro */
3139 0, /* tp_as_buffer */
3140 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3141 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
3142 dictionary_doc, /* tp_doc */
3143 dict_traverse, /* tp_traverse */
3144 dict_tp_clear, /* tp_clear */
3145 dict_richcompare, /* tp_richcompare */
3146 0, /* tp_weaklistoffset */
3147 (getiterfunc)dict_iter, /* tp_iter */
3148 0, /* tp_iternext */
3149 mapp_methods, /* tp_methods */
3150 0, /* tp_members */
3151 0, /* tp_getset */
3152 0, /* tp_base */
3153 0, /* tp_dict */
3154 0, /* tp_descr_get */
3155 0, /* tp_descr_set */
3156 0, /* tp_dictoffset */
3157 dict_init, /* tp_init */
3158 PyType_GenericAlloc, /* tp_alloc */
3159 dict_new, /* tp_new */
3160 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003161};
3162
Victor Stinner3c1e4812012-03-26 22:10:51 +02003163PyObject *
3164_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
3165{
3166 PyObject *kv;
3167 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02003168 if (kv == NULL) {
3169 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003170 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02003171 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02003172 return PyDict_GetItem(dp, kv);
3173}
3174
Guido van Rossum3cca2451997-05-16 14:23:33 +00003175/* For backward compatibility with old dictionary interface */
3176
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003177PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003178PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003179{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003180 PyObject *kv, *rv;
3181 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003182 if (kv == NULL) {
3183 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003184 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003185 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003186 rv = PyDict_GetItem(v, kv);
3187 Py_DECREF(kv);
3188 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003189}
3190
3191int
Victor Stinner3c1e4812012-03-26 22:10:51 +02003192_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
3193{
3194 PyObject *kv;
3195 kv = _PyUnicode_FromId(key); /* borrowed */
3196 if (kv == NULL)
3197 return -1;
3198 return PyDict_SetItem(v, kv, item);
3199}
3200
3201int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003202PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003204 PyObject *kv;
3205 int err;
3206 kv = PyUnicode_FromString(key);
3207 if (kv == NULL)
3208 return -1;
3209 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
3210 err = PyDict_SetItem(v, kv, item);
3211 Py_DECREF(kv);
3212 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003213}
3214
3215int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01003216_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
3217{
3218 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3219 if (kv == NULL)
3220 return -1;
3221 return PyDict_DelItem(v, kv);
3222}
3223
3224int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003225PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003226{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003227 PyObject *kv;
3228 int err;
3229 kv = PyUnicode_FromString(key);
3230 if (kv == NULL)
3231 return -1;
3232 err = PyDict_DelItem(v, kv);
3233 Py_DECREF(kv);
3234 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003235}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003236
Raymond Hettinger019a1482004-03-18 02:41:19 +00003237/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003238
3239typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003240 PyObject_HEAD
3241 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
3242 Py_ssize_t di_used;
3243 Py_ssize_t di_pos;
3244 PyObject* di_result; /* reusable result tuple for iteritems */
3245 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003246} dictiterobject;
3247
3248static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003249dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003250{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003251 dictiterobject *di;
3252 di = PyObject_GC_New(dictiterobject, itertype);
3253 if (di == NULL)
3254 return NULL;
3255 Py_INCREF(dict);
3256 di->di_dict = dict;
3257 di->di_used = dict->ma_used;
3258 di->di_pos = 0;
3259 di->len = dict->ma_used;
3260 if (itertype == &PyDictIterItem_Type) {
3261 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
3262 if (di->di_result == NULL) {
3263 Py_DECREF(di);
3264 return NULL;
3265 }
3266 }
3267 else
3268 di->di_result = NULL;
3269 _PyObject_GC_TRACK(di);
3270 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003271}
3272
3273static void
3274dictiter_dealloc(dictiterobject *di)
3275{
INADA Naokia6296d32017-08-24 14:55:17 +09003276 /* bpo-31095: UnTrack is needed before calling any callbacks */
3277 _PyObject_GC_UNTRACK(di);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003278 Py_XDECREF(di->di_dict);
3279 Py_XDECREF(di->di_result);
3280 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003281}
3282
3283static int
3284dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
3285{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003286 Py_VISIT(di->di_dict);
3287 Py_VISIT(di->di_result);
3288 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003289}
3290
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003291static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003292dictiter_len(dictiterobject *di)
3293{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003294 Py_ssize_t len = 0;
3295 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
3296 len = di->len;
3297 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003298}
3299
Guido van Rossumb90c8482007-02-10 01:11:45 +00003300PyDoc_STRVAR(length_hint_doc,
3301 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003302
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003303static PyObject *
3304dictiter_reduce(dictiterobject *di);
3305
3306PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3307
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003308static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003309 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
3310 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003311 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
3312 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003313 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003314};
3315
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003316static PyObject*
3317dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00003318{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003319 PyObject *key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003320 Py_ssize_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003321 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003322 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00003323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003324 if (d == NULL)
3325 return NULL;
3326 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00003327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003328 if (di->di_used != d->ma_used) {
3329 PyErr_SetString(PyExc_RuntimeError,
3330 "dictionary changed size during iteration");
3331 di->di_used = -1; /* Make this state sticky */
3332 return NULL;
3333 }
Guido van Rossum2147df72002-07-16 20:30:22 +00003334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003335 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003336 k = d->ma_keys;
INADA Naokica2d8be2016-11-04 16:59:10 +09003337 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003338 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003339 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003340 goto fail;
3341 key = DK_ENTRIES(k)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003342 assert(d->ma_values[i] != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003343 }
3344 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003345 Py_ssize_t n = k->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003346 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3347 while (i < n && entry_ptr->me_value == NULL) {
3348 entry_ptr++;
3349 i++;
3350 }
3351 if (i >= n)
3352 goto fail;
3353 key = entry_ptr->me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003354 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003355 di->di_pos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003356 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003357 Py_INCREF(key);
3358 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003359
3360fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003361 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003362 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003363 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003364}
3365
Raymond Hettinger019a1482004-03-18 02:41:19 +00003366PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003367 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3368 "dict_keyiterator", /* tp_name */
3369 sizeof(dictiterobject), /* tp_basicsize */
3370 0, /* tp_itemsize */
3371 /* methods */
3372 (destructor)dictiter_dealloc, /* tp_dealloc */
3373 0, /* tp_print */
3374 0, /* tp_getattr */
3375 0, /* tp_setattr */
3376 0, /* tp_reserved */
3377 0, /* tp_repr */
3378 0, /* tp_as_number */
3379 0, /* tp_as_sequence */
3380 0, /* tp_as_mapping */
3381 0, /* tp_hash */
3382 0, /* tp_call */
3383 0, /* tp_str */
3384 PyObject_GenericGetAttr, /* tp_getattro */
3385 0, /* tp_setattro */
3386 0, /* tp_as_buffer */
3387 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3388 0, /* tp_doc */
3389 (traverseproc)dictiter_traverse, /* tp_traverse */
3390 0, /* tp_clear */
3391 0, /* tp_richcompare */
3392 0, /* tp_weaklistoffset */
3393 PyObject_SelfIter, /* tp_iter */
3394 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3395 dictiter_methods, /* tp_methods */
3396 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003397};
3398
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003399static PyObject *
3400dictiter_iternextvalue(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003401{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003402 PyObject *value;
INADA Naokica2d8be2016-11-04 16:59:10 +09003403 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003404 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003406 if (d == NULL)
3407 return NULL;
3408 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003410 if (di->di_used != d->ma_used) {
3411 PyErr_SetString(PyExc_RuntimeError,
3412 "dictionary changed size during iteration");
3413 di->di_used = -1; /* Make this state sticky */
3414 return NULL;
3415 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003417 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003418 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003419 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003420 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003421 goto fail;
INADA Naokica2d8be2016-11-04 16:59:10 +09003422 value = d->ma_values[i];
3423 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003424 }
3425 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003426 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003427 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3428 while (i < n && entry_ptr->me_value == NULL) {
3429 entry_ptr++;
3430 i++;
3431 }
3432 if (i >= n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003433 goto fail;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003434 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003435 }
3436 di->di_pos = i+1;
3437 di->len--;
3438 Py_INCREF(value);
3439 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003440
3441fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003442 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003443 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003444 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003445}
3446
3447PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003448 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3449 "dict_valueiterator", /* tp_name */
3450 sizeof(dictiterobject), /* tp_basicsize */
3451 0, /* tp_itemsize */
3452 /* methods */
3453 (destructor)dictiter_dealloc, /* tp_dealloc */
3454 0, /* tp_print */
3455 0, /* tp_getattr */
3456 0, /* tp_setattr */
3457 0, /* tp_reserved */
3458 0, /* tp_repr */
3459 0, /* tp_as_number */
3460 0, /* tp_as_sequence */
3461 0, /* tp_as_mapping */
3462 0, /* tp_hash */
3463 0, /* tp_call */
3464 0, /* tp_str */
3465 PyObject_GenericGetAttr, /* tp_getattro */
3466 0, /* tp_setattro */
3467 0, /* tp_as_buffer */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003468 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003469 0, /* tp_doc */
3470 (traverseproc)dictiter_traverse, /* tp_traverse */
3471 0, /* tp_clear */
3472 0, /* tp_richcompare */
3473 0, /* tp_weaklistoffset */
3474 PyObject_SelfIter, /* tp_iter */
3475 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3476 dictiter_methods, /* tp_methods */
3477 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003478};
3479
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003480static PyObject *
3481dictiter_iternextitem(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003482{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003483 PyObject *key, *value, *result;
INADA Naokica2d8be2016-11-04 16:59:10 +09003484 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003485 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003487 if (d == NULL)
3488 return NULL;
3489 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003491 if (di->di_used != d->ma_used) {
3492 PyErr_SetString(PyExc_RuntimeError,
3493 "dictionary changed size during iteration");
3494 di->di_used = -1; /* Make this state sticky */
3495 return NULL;
3496 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003498 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003499 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003500 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003501 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003502 goto fail;
3503 key = DK_ENTRIES(d->ma_keys)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003504 value = d->ma_values[i];
3505 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003506 }
3507 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003508 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003509 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3510 while (i < n && entry_ptr->me_value == NULL) {
3511 entry_ptr++;
3512 i++;
3513 }
3514 if (i >= n)
3515 goto fail;
3516 key = entry_ptr->me_key;
3517 value = entry_ptr->me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003518 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003519 di->di_pos = i+1;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003520 di->len--;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003521 Py_INCREF(key);
3522 Py_INCREF(value);
3523 result = di->di_result;
3524 if (Py_REFCNT(result) == 1) {
3525 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3526 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3527 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3528 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003529 Py_INCREF(result);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003530 Py_DECREF(oldkey);
3531 Py_DECREF(oldvalue);
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003532 }
3533 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003534 result = PyTuple_New(2);
3535 if (result == NULL)
3536 return NULL;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003537 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3538 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003539 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003540 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003541
3542fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003543 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003544 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003545 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003546}
3547
3548PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003549 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3550 "dict_itemiterator", /* tp_name */
3551 sizeof(dictiterobject), /* tp_basicsize */
3552 0, /* tp_itemsize */
3553 /* methods */
3554 (destructor)dictiter_dealloc, /* tp_dealloc */
3555 0, /* tp_print */
3556 0, /* tp_getattr */
3557 0, /* tp_setattr */
3558 0, /* tp_reserved */
3559 0, /* tp_repr */
3560 0, /* tp_as_number */
3561 0, /* tp_as_sequence */
3562 0, /* tp_as_mapping */
3563 0, /* tp_hash */
3564 0, /* tp_call */
3565 0, /* tp_str */
3566 PyObject_GenericGetAttr, /* tp_getattro */
3567 0, /* tp_setattro */
3568 0, /* tp_as_buffer */
3569 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3570 0, /* tp_doc */
3571 (traverseproc)dictiter_traverse, /* tp_traverse */
3572 0, /* tp_clear */
3573 0, /* tp_richcompare */
3574 0, /* tp_weaklistoffset */
3575 PyObject_SelfIter, /* tp_iter */
3576 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3577 dictiter_methods, /* tp_methods */
3578 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003579};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003580
3581
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003582static PyObject *
3583dictiter_reduce(dictiterobject *di)
3584{
3585 PyObject *list;
3586 dictiterobject tmp;
3587
3588 list = PyList_New(0);
3589 if (!list)
3590 return NULL;
3591
3592 /* copy the itertor state */
3593 tmp = *di;
3594 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003595
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003596 /* iterate the temporary into a list */
3597 for(;;) {
3598 PyObject *element = 0;
3599 if (Py_TYPE(di) == &PyDictIterItem_Type)
3600 element = dictiter_iternextitem(&tmp);
3601 else if (Py_TYPE(di) == &PyDictIterKey_Type)
3602 element = dictiter_iternextkey(&tmp);
3603 else if (Py_TYPE(di) == &PyDictIterValue_Type)
3604 element = dictiter_iternextvalue(&tmp);
3605 else
3606 assert(0);
3607 if (element) {
3608 if (PyList_Append(list, element)) {
3609 Py_DECREF(element);
3610 Py_DECREF(list);
3611 Py_XDECREF(tmp.di_dict);
3612 return NULL;
3613 }
3614 Py_DECREF(element);
3615 } else
3616 break;
3617 }
3618 Py_XDECREF(tmp.di_dict);
3619 /* check for error */
3620 if (tmp.di_dict != NULL) {
3621 /* we have an error */
3622 Py_DECREF(list);
3623 return NULL;
3624 }
Antoine Pitroua7013882012-04-05 00:04:20 +02003625 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003626}
3627
Guido van Rossum3ac67412007-02-10 18:55:06 +00003628/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003629/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003630/***********************************************/
3631
Guido van Rossumb90c8482007-02-10 01:11:45 +00003632/* The instance lay-out is the same for all three; but the type differs. */
3633
Guido van Rossumb90c8482007-02-10 01:11:45 +00003634static void
Eric Snow96c6af92015-05-29 22:21:39 -06003635dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003636{
INADA Naokia6296d32017-08-24 14:55:17 +09003637 /* bpo-31095: UnTrack is needed before calling any callbacks */
3638 _PyObject_GC_UNTRACK(dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003639 Py_XDECREF(dv->dv_dict);
3640 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003641}
3642
3643static int
Eric Snow96c6af92015-05-29 22:21:39 -06003644dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003645{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003646 Py_VISIT(dv->dv_dict);
3647 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003648}
3649
Guido van Rossum83825ac2007-02-10 04:54:19 +00003650static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003651dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003652{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003653 Py_ssize_t len = 0;
3654 if (dv->dv_dict != NULL)
3655 len = dv->dv_dict->ma_used;
3656 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003657}
3658
Eric Snow96c6af92015-05-29 22:21:39 -06003659PyObject *
3660_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003661{
Eric Snow96c6af92015-05-29 22:21:39 -06003662 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003663 if (dict == NULL) {
3664 PyErr_BadInternalCall();
3665 return NULL;
3666 }
3667 if (!PyDict_Check(dict)) {
3668 /* XXX Get rid of this restriction later */
3669 PyErr_Format(PyExc_TypeError,
3670 "%s() requires a dict argument, not '%s'",
3671 type->tp_name, dict->ob_type->tp_name);
3672 return NULL;
3673 }
Eric Snow96c6af92015-05-29 22:21:39 -06003674 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003675 if (dv == NULL)
3676 return NULL;
3677 Py_INCREF(dict);
3678 dv->dv_dict = (PyDictObject *)dict;
3679 _PyObject_GC_TRACK(dv);
3680 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003681}
3682
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003683/* TODO(guido): The views objects are not complete:
3684
3685 * support more set operations
3686 * support arbitrary mappings?
3687 - either these should be static or exported in dictobject.h
3688 - if public then they should probably be in builtins
3689*/
3690
Guido van Rossumaac530c2007-08-24 22:33:45 +00003691/* Return 1 if self is a subset of other, iterating over self;
3692 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003693static int
3694all_contained_in(PyObject *self, PyObject *other)
3695{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003696 PyObject *iter = PyObject_GetIter(self);
3697 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003699 if (iter == NULL)
3700 return -1;
3701 for (;;) {
3702 PyObject *next = PyIter_Next(iter);
3703 if (next == NULL) {
3704 if (PyErr_Occurred())
3705 ok = -1;
3706 break;
3707 }
3708 ok = PySequence_Contains(other, next);
3709 Py_DECREF(next);
3710 if (ok <= 0)
3711 break;
3712 }
3713 Py_DECREF(iter);
3714 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003715}
3716
3717static PyObject *
3718dictview_richcompare(PyObject *self, PyObject *other, int op)
3719{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003720 Py_ssize_t len_self, len_other;
3721 int ok;
3722 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003724 assert(self != NULL);
3725 assert(PyDictViewSet_Check(self));
3726 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003727
Brian Curtindfc80e32011-08-10 20:28:54 -05003728 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3729 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003730
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003731 len_self = PyObject_Size(self);
3732 if (len_self < 0)
3733 return NULL;
3734 len_other = PyObject_Size(other);
3735 if (len_other < 0)
3736 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003737
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003738 ok = 0;
3739 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003741 case Py_NE:
3742 case Py_EQ:
3743 if (len_self == len_other)
3744 ok = all_contained_in(self, other);
3745 if (op == Py_NE && ok >= 0)
3746 ok = !ok;
3747 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003748
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003749 case Py_LT:
3750 if (len_self < len_other)
3751 ok = all_contained_in(self, other);
3752 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003754 case Py_LE:
3755 if (len_self <= len_other)
3756 ok = all_contained_in(self, other);
3757 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003759 case Py_GT:
3760 if (len_self > len_other)
3761 ok = all_contained_in(other, self);
3762 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003764 case Py_GE:
3765 if (len_self >= len_other)
3766 ok = all_contained_in(other, self);
3767 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003769 }
3770 if (ok < 0)
3771 return NULL;
3772 result = ok ? Py_True : Py_False;
3773 Py_INCREF(result);
3774 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003775}
3776
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003777static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003778dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003779{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003780 PyObject *seq;
3781 PyObject *result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003782
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003783 seq = PySequence_List((PyObject *)dv);
3784 if (seq == NULL)
3785 return NULL;
3786
3787 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
3788 Py_DECREF(seq);
3789 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003790}
3791
Guido van Rossum3ac67412007-02-10 18:55:06 +00003792/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003793
3794static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003795dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003796{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003797 if (dv->dv_dict == NULL) {
3798 Py_RETURN_NONE;
3799 }
3800 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003801}
3802
3803static int
Eric Snow96c6af92015-05-29 22:21:39 -06003804dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00003805{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003806 if (dv->dv_dict == NULL)
3807 return 0;
3808 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003809}
3810
Guido van Rossum83825ac2007-02-10 04:54:19 +00003811static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003812 (lenfunc)dictview_len, /* sq_length */
3813 0, /* sq_concat */
3814 0, /* sq_repeat */
3815 0, /* sq_item */
3816 0, /* sq_slice */
3817 0, /* sq_ass_item */
3818 0, /* sq_ass_slice */
3819 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003820};
3821
Guido van Rossum523259b2007-08-24 23:41:22 +00003822static PyObject*
3823dictviews_sub(PyObject* self, PyObject *other)
3824{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003825 PyObject *result = PySet_New(self);
3826 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003827 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003829 if (result == NULL)
3830 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003831
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003832 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003833 if (tmp == NULL) {
3834 Py_DECREF(result);
3835 return NULL;
3836 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003838 Py_DECREF(tmp);
3839 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003840}
3841
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003842PyObject*
3843_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00003844{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003845 PyObject *result = PySet_New(self);
3846 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003847 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003848
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003849 if (result == NULL)
3850 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003851
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003852 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_intersection_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003853 if (tmp == NULL) {
3854 Py_DECREF(result);
3855 return NULL;
3856 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003858 Py_DECREF(tmp);
3859 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003860}
3861
3862static PyObject*
3863dictviews_or(PyObject* self, PyObject *other)
3864{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003865 PyObject *result = PySet_New(self);
3866 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003867 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003868
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003869 if (result == NULL)
3870 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003871
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003872 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003873 if (tmp == NULL) {
3874 Py_DECREF(result);
3875 return NULL;
3876 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003878 Py_DECREF(tmp);
3879 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003880}
3881
3882static PyObject*
3883dictviews_xor(PyObject* self, PyObject *other)
3884{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003885 PyObject *result = PySet_New(self);
3886 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003887 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003889 if (result == NULL)
3890 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003891
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003892 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_symmetric_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003893 if (tmp == NULL) {
3894 Py_DECREF(result);
3895 return NULL;
3896 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003898 Py_DECREF(tmp);
3899 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003900}
3901
3902static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003903 0, /*nb_add*/
3904 (binaryfunc)dictviews_sub, /*nb_subtract*/
3905 0, /*nb_multiply*/
3906 0, /*nb_remainder*/
3907 0, /*nb_divmod*/
3908 0, /*nb_power*/
3909 0, /*nb_negative*/
3910 0, /*nb_positive*/
3911 0, /*nb_absolute*/
3912 0, /*nb_bool*/
3913 0, /*nb_invert*/
3914 0, /*nb_lshift*/
3915 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003916 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003917 (binaryfunc)dictviews_xor, /*nb_xor*/
3918 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00003919};
3920
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003921static PyObject*
3922dictviews_isdisjoint(PyObject *self, PyObject *other)
3923{
3924 PyObject *it;
3925 PyObject *item = NULL;
3926
3927 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06003928 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003929 Py_RETURN_TRUE;
3930 else
3931 Py_RETURN_FALSE;
3932 }
3933
3934 /* Iterate over the shorter object (only if other is a set,
3935 * because PySequence_Contains may be expensive otherwise): */
3936 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06003937 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003938 Py_ssize_t len_other = PyObject_Size(other);
3939 if (len_other == -1)
3940 return NULL;
3941
3942 if ((len_other > len_self)) {
3943 PyObject *tmp = other;
3944 other = self;
3945 self = tmp;
3946 }
3947 }
3948
3949 it = PyObject_GetIter(other);
3950 if (it == NULL)
3951 return NULL;
3952
3953 while ((item = PyIter_Next(it)) != NULL) {
3954 int contains = PySequence_Contains(self, item);
3955 Py_DECREF(item);
3956 if (contains == -1) {
3957 Py_DECREF(it);
3958 return NULL;
3959 }
3960
3961 if (contains) {
3962 Py_DECREF(it);
3963 Py_RETURN_FALSE;
3964 }
3965 }
3966 Py_DECREF(it);
3967 if (PyErr_Occurred())
3968 return NULL; /* PyIter_Next raised an exception. */
3969 Py_RETURN_TRUE;
3970}
3971
3972PyDoc_STRVAR(isdisjoint_doc,
3973"Return True if the view and the given iterable have a null intersection.");
3974
Guido van Rossumb90c8482007-02-10 01:11:45 +00003975static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003976 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3977 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003978 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003979};
3980
3981PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003982 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3983 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06003984 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003985 0, /* tp_itemsize */
3986 /* methods */
3987 (destructor)dictview_dealloc, /* tp_dealloc */
3988 0, /* tp_print */
3989 0, /* tp_getattr */
3990 0, /* tp_setattr */
3991 0, /* tp_reserved */
3992 (reprfunc)dictview_repr, /* tp_repr */
3993 &dictviews_as_number, /* tp_as_number */
3994 &dictkeys_as_sequence, /* tp_as_sequence */
3995 0, /* tp_as_mapping */
3996 0, /* tp_hash */
3997 0, /* tp_call */
3998 0, /* tp_str */
3999 PyObject_GenericGetAttr, /* tp_getattro */
4000 0, /* tp_setattro */
4001 0, /* tp_as_buffer */
4002 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4003 0, /* tp_doc */
4004 (traverseproc)dictview_traverse, /* tp_traverse */
4005 0, /* tp_clear */
4006 dictview_richcompare, /* tp_richcompare */
4007 0, /* tp_weaklistoffset */
4008 (getiterfunc)dictkeys_iter, /* tp_iter */
4009 0, /* tp_iternext */
4010 dictkeys_methods, /* tp_methods */
4011 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004012};
4013
4014static PyObject *
4015dictkeys_new(PyObject *dict)
4016{
Eric Snow96c6af92015-05-29 22:21:39 -06004017 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004018}
4019
Guido van Rossum3ac67412007-02-10 18:55:06 +00004020/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004021
4022static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004023dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004024{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004025 if (dv->dv_dict == NULL) {
4026 Py_RETURN_NONE;
4027 }
4028 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004029}
4030
4031static int
Eric Snow96c6af92015-05-29 22:21:39 -06004032dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004033{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004034 int result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004035 PyObject *key, *value, *found;
4036 if (dv->dv_dict == NULL)
4037 return 0;
4038 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
4039 return 0;
4040 key = PyTuple_GET_ITEM(obj, 0);
4041 value = PyTuple_GET_ITEM(obj, 1);
Raymond Hettinger6692f012016-09-18 21:46:08 -07004042 found = PyDict_GetItemWithError((PyObject *)dv->dv_dict, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004043 if (found == NULL) {
4044 if (PyErr_Occurred())
4045 return -1;
4046 return 0;
4047 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004048 Py_INCREF(found);
4049 result = PyObject_RichCompareBool(value, found, Py_EQ);
4050 Py_DECREF(found);
4051 return result;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004052}
4053
Guido van Rossum83825ac2007-02-10 04:54:19 +00004054static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004055 (lenfunc)dictview_len, /* sq_length */
4056 0, /* sq_concat */
4057 0, /* sq_repeat */
4058 0, /* sq_item */
4059 0, /* sq_slice */
4060 0, /* sq_ass_item */
4061 0, /* sq_ass_slice */
4062 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004063};
4064
Guido van Rossumb90c8482007-02-10 01:11:45 +00004065static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004066 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4067 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004068 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004069};
4070
4071PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004072 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4073 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004074 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004075 0, /* tp_itemsize */
4076 /* methods */
4077 (destructor)dictview_dealloc, /* tp_dealloc */
4078 0, /* tp_print */
4079 0, /* tp_getattr */
4080 0, /* tp_setattr */
4081 0, /* tp_reserved */
4082 (reprfunc)dictview_repr, /* tp_repr */
4083 &dictviews_as_number, /* tp_as_number */
4084 &dictitems_as_sequence, /* tp_as_sequence */
4085 0, /* tp_as_mapping */
4086 0, /* tp_hash */
4087 0, /* tp_call */
4088 0, /* tp_str */
4089 PyObject_GenericGetAttr, /* tp_getattro */
4090 0, /* tp_setattro */
4091 0, /* tp_as_buffer */
4092 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4093 0, /* tp_doc */
4094 (traverseproc)dictview_traverse, /* tp_traverse */
4095 0, /* tp_clear */
4096 dictview_richcompare, /* tp_richcompare */
4097 0, /* tp_weaklistoffset */
4098 (getiterfunc)dictitems_iter, /* tp_iter */
4099 0, /* tp_iternext */
4100 dictitems_methods, /* tp_methods */
4101 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004102};
4103
4104static PyObject *
4105dictitems_new(PyObject *dict)
4106{
Eric Snow96c6af92015-05-29 22:21:39 -06004107 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004108}
4109
Guido van Rossum3ac67412007-02-10 18:55:06 +00004110/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004111
4112static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004113dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004114{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004115 if (dv->dv_dict == NULL) {
4116 Py_RETURN_NONE;
4117 }
4118 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004119}
4120
Guido van Rossum83825ac2007-02-10 04:54:19 +00004121static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004122 (lenfunc)dictview_len, /* sq_length */
4123 0, /* sq_concat */
4124 0, /* sq_repeat */
4125 0, /* sq_item */
4126 0, /* sq_slice */
4127 0, /* sq_ass_item */
4128 0, /* sq_ass_slice */
4129 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004130};
4131
Guido van Rossumb90c8482007-02-10 01:11:45 +00004132static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004133 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004134};
4135
4136PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004137 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4138 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004139 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004140 0, /* tp_itemsize */
4141 /* methods */
4142 (destructor)dictview_dealloc, /* tp_dealloc */
4143 0, /* tp_print */
4144 0, /* tp_getattr */
4145 0, /* tp_setattr */
4146 0, /* tp_reserved */
4147 (reprfunc)dictview_repr, /* tp_repr */
4148 0, /* tp_as_number */
4149 &dictvalues_as_sequence, /* tp_as_sequence */
4150 0, /* tp_as_mapping */
4151 0, /* tp_hash */
4152 0, /* tp_call */
4153 0, /* tp_str */
4154 PyObject_GenericGetAttr, /* tp_getattro */
4155 0, /* tp_setattro */
4156 0, /* tp_as_buffer */
4157 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4158 0, /* tp_doc */
4159 (traverseproc)dictview_traverse, /* tp_traverse */
4160 0, /* tp_clear */
4161 0, /* tp_richcompare */
4162 0, /* tp_weaklistoffset */
4163 (getiterfunc)dictvalues_iter, /* tp_iter */
4164 0, /* tp_iternext */
4165 dictvalues_methods, /* tp_methods */
4166 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004167};
4168
4169static PyObject *
4170dictvalues_new(PyObject *dict)
4171{
Eric Snow96c6af92015-05-29 22:21:39 -06004172 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004173}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004174
4175/* Returns NULL if cannot allocate a new PyDictKeysObject,
4176 but does not set an error */
4177PyDictKeysObject *
4178_PyDict_NewKeysForClass(void)
4179{
Victor Stinner742da042016-09-07 17:40:12 -07004180 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004181 if (keys == NULL)
4182 PyErr_Clear();
4183 else
4184 keys->dk_lookup = lookdict_split;
4185 return keys;
4186}
4187
4188#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
4189
4190PyObject *
4191PyObject_GenericGetDict(PyObject *obj, void *context)
4192{
4193 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
4194 if (dictptr == NULL) {
4195 PyErr_SetString(PyExc_AttributeError,
4196 "This object has no __dict__");
4197 return NULL;
4198 }
4199 dict = *dictptr;
4200 if (dict == NULL) {
4201 PyTypeObject *tp = Py_TYPE(obj);
4202 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
4203 DK_INCREF(CACHED_KEYS(tp));
4204 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
4205 }
4206 else {
4207 *dictptr = dict = PyDict_New();
4208 }
4209 }
4210 Py_XINCREF(dict);
4211 return dict;
4212}
4213
4214int
4215_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
Victor Stinner742da042016-09-07 17:40:12 -07004216 PyObject *key, PyObject *value)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004217{
4218 PyObject *dict;
4219 int res;
4220 PyDictKeysObject *cached;
4221
4222 assert(dictptr != NULL);
4223 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
4224 assert(dictptr != NULL);
4225 dict = *dictptr;
4226 if (dict == NULL) {
4227 DK_INCREF(cached);
4228 dict = new_dict_with_shared_keys(cached);
4229 if (dict == NULL)
4230 return -1;
4231 *dictptr = dict;
4232 }
4233 if (value == NULL) {
4234 res = PyDict_DelItem(dict, key);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004235 // Since key sharing dict doesn't allow deletion, PyDict_DelItem()
4236 // always converts dict to combined form.
4237 if ((cached = CACHED_KEYS(tp)) != NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004238 CACHED_KEYS(tp) = NULL;
4239 DK_DECREF(cached);
4240 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01004241 }
4242 else {
INADA Naoki2294f3a2017-02-12 13:51:30 +09004243 int was_shared = (cached == ((PyDictObject *)dict)->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004244 res = PyDict_SetItem(dict, key, value);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004245 if (was_shared &&
4246 (cached = CACHED_KEYS(tp)) != NULL &&
4247 cached != ((PyDictObject *)dict)->ma_keys) {
Victor Stinner3d3f2642016-12-15 17:21:23 +01004248 /* PyDict_SetItem() may call dictresize and convert split table
4249 * into combined table. In such case, convert it to split
4250 * table again and update type's shared key only when this is
4251 * the only dict sharing key with the type.
4252 *
4253 * This is to allow using shared key in class like this:
4254 *
4255 * class C:
4256 * def __init__(self):
4257 * # one dict resize happens
4258 * self.a, self.b, self.c = 1, 2, 3
4259 * self.d, self.e, self.f = 4, 5, 6
4260 * a = C()
4261 */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004262 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004263 CACHED_KEYS(tp) = make_keys_shared(dict);
Victor Stinner742da042016-09-07 17:40:12 -07004264 }
4265 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004266 CACHED_KEYS(tp) = NULL;
4267 }
4268 DK_DECREF(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004269 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
4270 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004271 }
4272 }
4273 } else {
4274 dict = *dictptr;
4275 if (dict == NULL) {
4276 dict = PyDict_New();
4277 if (dict == NULL)
4278 return -1;
4279 *dictptr = dict;
4280 }
4281 if (value == NULL) {
4282 res = PyDict_DelItem(dict, key);
4283 } else {
4284 res = PyDict_SetItem(dict, key, value);
4285 }
4286 }
4287 return res;
4288}
4289
4290void
4291_PyDictKeys_DecRef(PyDictKeysObject *keys)
4292{
4293 DK_DECREF(keys);
4294}