blob: b20b85c909e3331f440591736b524db66917b239 [file] [log] [blame]
Guido van Rossum2bc13791999-03-24 19:06:42 +00001/* Dictionary object implementation using a hash table */
Guido van Rossum9bfef441993-03-29 10:43:31 +00002
Raymond Hettinger930427b2003-05-03 06:51:59 +00003/* The distribution includes a separate file, Objects/dictnotes.txt,
Tim Peters60b29962006-01-01 01:19:23 +00004 describing explorations into dictionary design and optimization.
Raymond Hettinger930427b2003-05-03 06:51:59 +00005 It covers typical dictionary use patterns, the parameters for
6 tuning dictionaries, and several ideas for possible optimizations.
7*/
8
Victor Stinner742da042016-09-07 17:40:12 -07009/* PyDictKeysObject
10
11This implements the dictionary's hashtable.
12
Raymond Hettingerb12785d2016-10-22 09:58:14 -070013As of Python 3.6, this is compact and ordered. Basic idea is described here:
14* https://mail.python.org/pipermail/python-dev/2012-December/123028.html
15* https://morepypy.blogspot.com/2015/01/faster-more-memory-efficient-and-more.html
Victor Stinner742da042016-09-07 17:40:12 -070016
17layout:
18
19+---------------+
20| dk_refcnt |
21| dk_size |
22| dk_lookup |
23| dk_usable |
24| dk_nentries |
25+---------------+
26| dk_indices |
27| |
28+---------------+
29| dk_entries |
30| |
31+---------------+
32
33dk_indices is actual hashtable. It holds index in entries, or DKIX_EMPTY(-1)
34or DKIX_DUMMY(-2).
35Size of indices is dk_size. Type of each index in indices is vary on dk_size:
36
37* int8 for dk_size <= 128
38* int16 for 256 <= dk_size <= 2**15
39* int32 for 2**16 <= dk_size <= 2**31
40* int64 for 2**32 <= dk_size
41
42dk_entries is array of PyDictKeyEntry. It's size is USABLE_FRACTION(dk_size).
43DK_ENTRIES(dk) can be used to get pointer to entries.
44
45NOTE: Since negative value is used for DKIX_EMPTY and DKIX_DUMMY, type of
46dk_indices entry is signed integer and int16 is used for table which
47dk_size == 256.
48*/
49
Benjamin Peterson7d95e402012-04-23 11:24:50 -040050
51/*
Benjamin Peterson7d95e402012-04-23 11:24:50 -040052The DictObject can be in one of two forms.
Victor Stinner742da042016-09-07 17:40:12 -070053
Benjamin Peterson7d95e402012-04-23 11:24:50 -040054Either:
55 A combined table:
56 ma_values == NULL, dk_refcnt == 1.
57 Values are stored in the me_value field of the PyDictKeysObject.
Benjamin Peterson7d95e402012-04-23 11:24:50 -040058Or:
59 A split table:
60 ma_values != NULL, dk_refcnt >= 1
61 Values are stored in the ma_values array.
Victor Stinner742da042016-09-07 17:40:12 -070062 Only string (unicode) keys are allowed.
63 All dicts sharing same key must have same insertion order.
Benjamin Peterson7d95e402012-04-23 11:24:50 -040064
Victor Stinner742da042016-09-07 17:40:12 -070065There are four kinds of slots in the table (slot is index, and
66DK_ENTRIES(keys)[index] if index >= 0):
67
681. Unused. index == DKIX_EMPTY
69 Does not hold an active (key, value) pair now and never did. Unused can
70 transition to Active upon key insertion. This is each slot's initial state.
71
722. Active. index >= 0, me_key != NULL and me_value != NULL
73 Holds an active (key, value) pair. Active can transition to Dummy or
74 Pending upon key deletion (for combined and split tables respectively).
75 This is the only case in which me_value != NULL.
76
773. Dummy. index == DKIX_DUMMY (combined only)
78 Previously held an active (key, value) pair, but that was deleted and an
79 active pair has not yet overwritten the slot. Dummy can transition to
80 Active upon key insertion. Dummy slots cannot be made Unused again
81 else the probe sequence in case of collision would have no way to know
82 they were once active.
83
844. Pending. index >= 0, key != NULL, and value == NULL (split only)
85 Not yet inserted in split-table.
Benjamin Peterson7d95e402012-04-23 11:24:50 -040086*/
87
Victor Stinner742da042016-09-07 17:40:12 -070088/*
89Preserving insertion order
Benjamin Peterson7d95e402012-04-23 11:24:50 -040090
Victor Stinner742da042016-09-07 17:40:12 -070091It's simple for combined table. Since dk_entries is mostly append only, we can
92get insertion order by just iterating dk_entries.
93
94One exception is .popitem(). It removes last item in dk_entries and decrement
95dk_nentries to achieve amortized O(1). Since there are DKIX_DUMMY remains in
96dk_indices, we can't increment dk_usable even though dk_nentries is
97decremented.
98
99In split table, inserting into pending entry is allowed only for dk_entries[ix]
100where ix == mp->ma_used. Inserting into other index and deleting item cause
101converting the dict to the combined table.
102*/
103
104/* PyDict_MINSIZE is the starting size for any new dict.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400105 * 8 allows dicts with no more than 5 active entries; experiments suggested
106 * this suffices for the majority of dicts (consisting mostly of usually-small
107 * dicts created to pass keyword arguments).
108 * Making this 8, rather than 4 reduces the number of resizes for most
109 * dictionaries, without any significant extra memory use.
110 */
Victor Stinner742da042016-09-07 17:40:12 -0700111#define PyDict_MINSIZE 8
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400112
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000113#include "Python.h"
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600114#include "internal/pystate.h"
Eric Snow96c6af92015-05-29 22:21:39 -0600115#include "dict-common.h"
Victor Stinner990397e2016-09-09 20:22:59 -0700116#include "stringlib/eq.h" /* to get unicode_eq() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000117
Larry Hastings61272b72014-01-07 12:41:53 -0800118/*[clinic input]
Larry Hastingsc2047262014-01-25 20:43:29 -0800119class dict "PyDictObject *" "&PyDict_Type"
Larry Hastings61272b72014-01-07 12:41:53 -0800120[clinic start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -0800121/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f157a5a0ce9589d6]*/
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800122
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400123
124/*
125To ensure the lookup algorithm terminates, there must be at least one Unused
126slot (NULL key) in the table.
127To avoid slowing down lookups on a near-full table, we resize the table when
128it's USABLE_FRACTION (currently two-thirds) full.
129*/
Guido van Rossum16e93a81997-01-28 00:00:11 +0000130
Tim Peterseb28ef22001-06-02 05:27:19 +0000131#define PERTURB_SHIFT 5
132
Guido van Rossum16e93a81997-01-28 00:00:11 +0000133/*
Tim Peterseb28ef22001-06-02 05:27:19 +0000134Major subtleties ahead: Most hash schemes depend on having a "good" hash
135function, in the sense of simulating randomness. Python doesn't: its most
R David Murray537ad7a2016-07-10 12:33:18 -0400136important hash functions (for ints) are very regular in common
Tim Peterseb28ef22001-06-02 05:27:19 +0000137cases:
Tim Peters15d49292001-05-27 07:39:22 +0000138
R David Murray537ad7a2016-07-10 12:33:18 -0400139 >>>[hash(i) for i in range(4)]
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000140 [0, 1, 2, 3]
Tim Peters15d49292001-05-27 07:39:22 +0000141
Tim Peterseb28ef22001-06-02 05:27:19 +0000142This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
143the low-order i bits as the initial table index is extremely fast, and there
R David Murray537ad7a2016-07-10 12:33:18 -0400144are no collisions at all for dicts indexed by a contiguous range of ints. So
145this gives better-than-random behavior in common cases, and that's very
146desirable.
Tim Peters15d49292001-05-27 07:39:22 +0000147
Tim Peterseb28ef22001-06-02 05:27:19 +0000148OTOH, when collisions occur, the tendency to fill contiguous slices of the
149hash table makes a good collision resolution strategy crucial. Taking only
150the last i bits of the hash code is also vulnerable: for example, consider
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000152their own hash codes, and this fits in a dict of size 2**15, the last 15 bits
153 of every hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +0000154
Tim Peterseb28ef22001-06-02 05:27:19 +0000155But catering to unusual cases should not slow the usual ones, so we just take
156the last i bits anyway. It's up to collision resolution to do the rest. If
157we *usually* find the key we're looking for on the first try (and, it turns
158out, we usually do -- the table load factor is kept under 2/3, so the odds
159are solidly in our favor), then it makes best sense to keep the initial index
160computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +0000161
Tim Peterseb28ef22001-06-02 05:27:19 +0000162The first half of collision resolution is to visit table indices via this
163recurrence:
Tim Peters15d49292001-05-27 07:39:22 +0000164
Tim Peterseb28ef22001-06-02 05:27:19 +0000165 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +0000166
Tim Peterseb28ef22001-06-02 05:27:19 +0000167For any initial j in range(2**i), repeating that 2**i times generates each
168int in range(2**i) exactly once (see any text on random-number generation for
169proof). By itself, this doesn't help much: like linear probing (setting
170j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
171order. This would be bad, except that's not the only thing we do, and it's
172actually *good* in the common cases where hash keys are consecutive. In an
173example that's really too small to make this entirely clear, for a table of
174size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +0000175
Tim Peterseb28ef22001-06-02 05:27:19 +0000176 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
177
178If two things come in at index 5, the first place we look after is index 2,
179not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
180Linear probing is deadly in this case because there the fixed probe order
181is the *same* as the order consecutive keys are likely to arrive. But it's
182extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
183and certain that consecutive hash codes do not.
184
185The other half of the strategy is to get the other bits of the hash code
186into play. This is done by initializing a (unsigned) vrbl "perturb" to the
187full hash code, and changing the recurrence to:
188
Tim Peterseb28ef22001-06-02 05:27:19 +0000189 perturb >>= PERTURB_SHIFT;
INADA Naoki267941c2016-10-06 15:19:07 +0900190 j = (5*j) + 1 + perturb;
Tim Peterseb28ef22001-06-02 05:27:19 +0000191 use j % 2**i as the next table index;
192
193Now the probe sequence depends (eventually) on every bit in the hash code,
194and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
195because it quickly magnifies small differences in the bits that didn't affect
196the initial index. Note that because perturb is unsigned, if the recurrence
197is executed often enough perturb eventually becomes and remains 0. At that
198point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
199that's certain to find an empty slot eventually (since it generates every int
200in range(2**i), and we make sure there's always at least one empty slot).
201
202Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
203small so that the high bits of the hash code continue to affect the probe
204sequence across iterations; but you want it large so that in really bad cases
205the high-order hash bits have an effect on early iterations. 5 was "the
206best" in minimizing total collisions across experiments Tim Peters ran (on
207both normal and pathological cases), but 4 and 6 weren't significantly worse.
208
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000209Historical: Reimer Behrends contributed the idea of using a polynomial-based
Tim Peterseb28ef22001-06-02 05:27:19 +0000210approach, using repeated multiplication by x in GF(2**n) where an irreducible
211polynomial for each table size was chosen such that x was a primitive root.
212Christian Tismer later extended that to use division by x instead, as an
213efficient way to get the high bits of the hash code into play. This scheme
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000214also gave excellent collision statistics, but was more expensive: two
215if-tests were required inside the loop; computing "the next" index took about
216the same number of operations but without as much potential parallelism
217(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
218above, and then shifting perturb can be done while the table index is being
219masked); and the PyDictObject struct required a member to hold the table's
220polynomial. In Tim's experiments the current scheme ran faster, produced
221equally good collision statistics, needed less code & used less memory.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000222
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000223*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000224
Fred Drake1bff34a2000-08-31 19:31:38 +0000225/* forward declarations */
Victor Stinner742da042016-09-07 17:40:12 -0700226static Py_ssize_t lookdict(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900227 Py_hash_t hash, PyObject **value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700228static Py_ssize_t lookdict_unicode(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900229 Py_hash_t hash, PyObject **value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700230static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400231lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900232 Py_hash_t hash, PyObject **value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700233static Py_ssize_t lookdict_split(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900234 Py_hash_t hash, PyObject **value_addr);
Fred Drake1bff34a2000-08-31 19:31:38 +0000235
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400236static int dictresize(PyDictObject *mp, Py_ssize_t minused);
Tim Petersdea48ec2001-05-22 20:40:22 +0000237
Benjamin Peterson3c569292016-09-08 13:16:41 -0700238/*Global counter used to set ma_version_tag field of dictionary.
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700239 * It is incremented each time that a dictionary is created and each
240 * time that a dictionary is modified. */
241static uint64_t pydict_global_version = 0;
242
243#define DICT_NEXT_VERSION() (++pydict_global_version)
244
Victor Stinner742da042016-09-07 17:40:12 -0700245/* Dictionary reuse scheme to save calls to malloc and free */
Christian Heimes2202f872008-02-06 14:31:34 +0000246#ifndef PyDict_MAXFREELIST
247#define PyDict_MAXFREELIST 80
248#endif
249static PyDictObject *free_list[PyDict_MAXFREELIST];
250static int numfree = 0;
Victor Stinner742da042016-09-07 17:40:12 -0700251static PyDictKeysObject *keys_free_list[PyDict_MAXFREELIST];
252static int numfreekeys = 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000253
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300254#include "clinic/dictobject.c.h"
255
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100256int
257PyDict_ClearFreeList(void)
Christian Heimes77c02eb2008-02-09 02:18:51 +0000258{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 PyDictObject *op;
Victor Stinner742da042016-09-07 17:40:12 -0700260 int ret = numfree + numfreekeys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 while (numfree) {
262 op = free_list[--numfree];
263 assert(PyDict_CheckExact(op));
264 PyObject_GC_Del(op);
265 }
Victor Stinner742da042016-09-07 17:40:12 -0700266 while (numfreekeys) {
267 PyObject_FREE(keys_free_list[--numfreekeys]);
268 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100269 return ret;
270}
271
David Malcolm49526f42012-06-22 14:55:41 -0400272/* Print summary info about the state of the optimized allocator */
273void
274_PyDict_DebugMallocStats(FILE *out)
275{
276 _PyDebugAllocatorStats(out,
277 "free PyDictObject", numfree, sizeof(PyDictObject));
278}
279
280
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100281void
282PyDict_Fini(void)
283{
284 PyDict_ClearFreeList();
Christian Heimes77c02eb2008-02-09 02:18:51 +0000285}
286
Victor Stinner742da042016-09-07 17:40:12 -0700287#define DK_SIZE(dk) ((dk)->dk_size)
288#if SIZEOF_VOID_P > 4
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700289#define DK_IXSIZE(dk) \
290 (DK_SIZE(dk) <= 0xff ? \
291 1 : DK_SIZE(dk) <= 0xffff ? \
292 2 : DK_SIZE(dk) <= 0xffffffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700293 4 : sizeof(int64_t))
Victor Stinner742da042016-09-07 17:40:12 -0700294#else
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700295#define DK_IXSIZE(dk) \
296 (DK_SIZE(dk) <= 0xff ? \
297 1 : DK_SIZE(dk) <= 0xffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700298 2 : sizeof(int32_t))
Victor Stinner742da042016-09-07 17:40:12 -0700299#endif
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700300#define DK_ENTRIES(dk) \
Benjamin Peterson186122e2016-09-08 12:20:12 -0700301 ((PyDictKeyEntry*)(&(dk)->dk_indices.as_1[DK_SIZE(dk) * DK_IXSIZE(dk)]))
Victor Stinner742da042016-09-07 17:40:12 -0700302
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200303#define DK_DEBUG_INCREF _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA
304#define DK_DEBUG_DECREF _Py_DEC_REFTOTAL _Py_REF_DEBUG_COMMA
305
306#define DK_INCREF(dk) (DK_DEBUG_INCREF ++(dk)->dk_refcnt)
307#define DK_DECREF(dk) if (DK_DEBUG_DECREF (--(dk)->dk_refcnt) == 0) free_keys_object(dk)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400308#define DK_MASK(dk) (((dk)->dk_size)-1)
309#define IS_POWER_OF_2(x) (((x) & (x-1)) == 0)
310
Victor Stinner742da042016-09-07 17:40:12 -0700311/* lookup indices. returns DKIX_EMPTY, DKIX_DUMMY, or ix >=0 */
Benjamin Peterson73222252016-09-08 09:58:47 -0700312static inline Py_ssize_t
Victor Stinner742da042016-09-07 17:40:12 -0700313dk_get_index(PyDictKeysObject *keys, Py_ssize_t i)
314{
315 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700316 Py_ssize_t ix;
317
Victor Stinner742da042016-09-07 17:40:12 -0700318 if (s <= 0xff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700319 int8_t *indices = keys->dk_indices.as_1;
Victor Stinner208857e2016-09-08 11:35:46 -0700320 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700321 }
322 else if (s <= 0xffff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700323 int16_t *indices = keys->dk_indices.as_2;
Victor Stinner208857e2016-09-08 11:35:46 -0700324 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700325 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700326#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300327 else if (s > 0xffffffff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700328 int64_t *indices = keys->dk_indices.as_8;
Victor Stinner208857e2016-09-08 11:35:46 -0700329 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700330 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700331#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300332 else {
333 int32_t *indices = keys->dk_indices.as_4;
334 ix = indices[i];
335 }
Victor Stinner71211e32016-09-08 10:52:46 -0700336 assert(ix >= DKIX_DUMMY);
337 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700338}
339
340/* write to indices. */
Benjamin Peterson73222252016-09-08 09:58:47 -0700341static inline void
Victor Stinner742da042016-09-07 17:40:12 -0700342dk_set_index(PyDictKeysObject *keys, Py_ssize_t i, Py_ssize_t ix)
343{
344 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700345
346 assert(ix >= DKIX_DUMMY);
347
Victor Stinner742da042016-09-07 17:40:12 -0700348 if (s <= 0xff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700349 int8_t *indices = keys->dk_indices.as_1;
Victor Stinner71211e32016-09-08 10:52:46 -0700350 assert(ix <= 0x7f);
Victor Stinner208857e2016-09-08 11:35:46 -0700351 indices[i] = (char)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700352 }
353 else if (s <= 0xffff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700354 int16_t *indices = keys->dk_indices.as_2;
Victor Stinner71211e32016-09-08 10:52:46 -0700355 assert(ix <= 0x7fff);
Victor Stinner208857e2016-09-08 11:35:46 -0700356 indices[i] = (int16_t)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700357 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700358#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300359 else if (s > 0xffffffff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700360 int64_t *indices = keys->dk_indices.as_8;
Victor Stinner208857e2016-09-08 11:35:46 -0700361 indices[i] = ix;
Victor Stinner742da042016-09-07 17:40:12 -0700362 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700363#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300364 else {
365 int32_t *indices = keys->dk_indices.as_4;
366 assert(ix <= 0x7fffffff);
367 indices[i] = (int32_t)ix;
368 }
Victor Stinner742da042016-09-07 17:40:12 -0700369}
370
371
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200372/* USABLE_FRACTION is the maximum dictionary load.
Victor Stinner742da042016-09-07 17:40:12 -0700373 * Increasing this ratio makes dictionaries more dense resulting in more
374 * collisions. Decreasing it improves sparseness at the expense of spreading
375 * indices over more cache lines and at the cost of total memory consumed.
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200376 *
377 * USABLE_FRACTION must obey the following:
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400378 * (0 < USABLE_FRACTION(n) < n) for all n >= 2
379 *
Victor Stinner742da042016-09-07 17:40:12 -0700380 * USABLE_FRACTION should be quick to calculate.
381 * Fractions around 1/2 to 2/3 seem to work well in practice.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400382 */
Victor Stinner742da042016-09-07 17:40:12 -0700383#define USABLE_FRACTION(n) (((n) << 1)/3)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400384
Victor Stinner742da042016-09-07 17:40:12 -0700385/* ESTIMATE_SIZE is reverse function of USABLE_FRACTION.
386 * This can be used to reserve enough size to insert n entries without
387 * resizing.
388 */
INADA Naoki92c50ee2016-11-22 00:57:02 +0900389#define ESTIMATE_SIZE(n) (((n)*3+1) >> 1)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400390
Victor Stinner742da042016-09-07 17:40:12 -0700391/* Alternative fraction that is otherwise close enough to 2n/3 to make
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400392 * little difference. 8 * 2/3 == 8 * 5/8 == 5. 16 * 2/3 == 16 * 5/8 == 10.
393 * 32 * 2/3 = 21, 32 * 5/8 = 20.
394 * Its advantage is that it is faster to compute on machines with slow division.
395 * #define USABLE_FRACTION(n) (((n) >> 1) + ((n) >> 2) - ((n) >> 3))
Victor Stinner742da042016-09-07 17:40:12 -0700396 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400397
Victor Stinnera9f61a52013-07-16 22:17:26 +0200398/* GROWTH_RATE. Growth rate upon hitting maximum load.
399 * Currently set to used*2 + capacity/2.
400 * This means that dicts double in size when growing without deletions,
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700401 * but have more head room when the number of deletions is on a par with the
402 * number of insertions.
403 * Raising this to used*4 doubles memory consumption depending on the size of
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200404 * the dictionary, but results in half the number of resizes, less effort to
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700405 * resize.
406 * GROWTH_RATE was set to used*4 up to version 3.2.
407 * GROWTH_RATE was set to used*2 in version 3.3.0
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200408 */
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700409#define GROWTH_RATE(d) (((d)->ma_used*2)+((d)->ma_keys->dk_size>>1))
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400410
411#define ENSURE_ALLOWS_DELETIONS(d) \
412 if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \
413 (d)->ma_keys->dk_lookup = lookdict_unicode; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400415
416/* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
417 * (which cannot fail and thus can do no allocation).
418 */
419static PyDictKeysObject empty_keys_struct = {
Serhiy Storchaka97932e42016-09-26 23:01:23 +0300420 1, /* dk_refcnt */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400421 1, /* dk_size */
422 lookdict_split, /* dk_lookup */
423 0, /* dk_usable (immutable) */
Victor Stinner742da042016-09-07 17:40:12 -0700424 0, /* dk_nentries */
Benjamin Peterson186122e2016-09-08 12:20:12 -0700425 .dk_indices = { .as_1 = {DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY,
426 DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY}},
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400427};
428
429static PyObject *empty_values[1] = { NULL };
430
431#define Py_EMPTY_KEYS &empty_keys_struct
432
Victor Stinner611b0fa2016-09-14 15:02:01 +0200433/* Uncomment to check the dict content in _PyDict_CheckConsistency() */
434/* #define DEBUG_PYDICT */
435
436
T. Woutersa00c3fd2017-03-31 09:14:41 -0700437#ifndef NDEBUG
Victor Stinner611b0fa2016-09-14 15:02:01 +0200438static int
439_PyDict_CheckConsistency(PyDictObject *mp)
440{
441 PyDictKeysObject *keys = mp->ma_keys;
442 int splitted = _PyDict_HasSplitTable(mp);
443 Py_ssize_t usable = USABLE_FRACTION(keys->dk_size);
444#ifdef DEBUG_PYDICT
445 PyDictKeyEntry *entries = DK_ENTRIES(keys);
446 Py_ssize_t i;
447#endif
448
449 assert(0 <= mp->ma_used && mp->ma_used <= usable);
450 assert(IS_POWER_OF_2(keys->dk_size));
451 assert(0 <= keys->dk_usable
452 && keys->dk_usable <= usable);
453 assert(0 <= keys->dk_nentries
454 && keys->dk_nentries <= usable);
455 assert(keys->dk_usable + keys->dk_nentries <= usable);
456
457 if (!splitted) {
458 /* combined table */
459 assert(keys->dk_refcnt == 1);
460 }
461
462#ifdef DEBUG_PYDICT
463 for (i=0; i < keys->dk_size; i++) {
464 Py_ssize_t ix = dk_get_index(keys, i);
465 assert(DKIX_DUMMY <= ix && ix <= usable);
466 }
467
468 for (i=0; i < usable; i++) {
469 PyDictKeyEntry *entry = &entries[i];
470 PyObject *key = entry->me_key;
471
472 if (key != NULL) {
473 if (PyUnicode_CheckExact(key)) {
474 Py_hash_t hash = ((PyASCIIObject *)key)->hash;
475 assert(hash != -1);
476 assert(entry->me_hash == hash);
477 }
478 else {
479 /* test_dict fails if PyObject_Hash() is called again */
480 assert(entry->me_hash != -1);
481 }
482 if (!splitted) {
483 assert(entry->me_value != NULL);
484 }
485 }
486
487 if (splitted) {
488 assert(entry->me_value == NULL);
489 }
490 }
491
492 if (splitted) {
493 /* splitted table */
494 for (i=0; i < mp->ma_used; i++) {
495 assert(mp->ma_values[i] != NULL);
496 }
497 }
498#endif
499
500 return 1;
501}
502#endif
503
504
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400505static PyDictKeysObject *new_keys_object(Py_ssize_t size)
506{
507 PyDictKeysObject *dk;
Victor Stinner742da042016-09-07 17:40:12 -0700508 Py_ssize_t es, usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400509
Victor Stinner742da042016-09-07 17:40:12 -0700510 assert(size >= PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400511 assert(IS_POWER_OF_2(size));
Victor Stinner742da042016-09-07 17:40:12 -0700512
513 usable = USABLE_FRACTION(size);
514 if (size <= 0xff) {
515 es = 1;
516 }
517 else if (size <= 0xffff) {
518 es = 2;
519 }
520#if SIZEOF_VOID_P > 4
521 else if (size <= 0xffffffff) {
522 es = 4;
523 }
524#endif
525 else {
526 es = sizeof(Py_ssize_t);
527 }
528
529 if (size == PyDict_MINSIZE && numfreekeys > 0) {
530 dk = keys_free_list[--numfreekeys];
531 }
532 else {
Victor Stinner98ee9d52016-09-08 09:33:56 -0700533 dk = PyObject_MALLOC(sizeof(PyDictKeysObject)
534 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
535 + es * size
536 + sizeof(PyDictKeyEntry) * usable);
Victor Stinner742da042016-09-07 17:40:12 -0700537 if (dk == NULL) {
538 PyErr_NoMemory();
539 return NULL;
540 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400541 }
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200542 DK_DEBUG_INCREF dk->dk_refcnt = 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400543 dk->dk_size = size;
Victor Stinner742da042016-09-07 17:40:12 -0700544 dk->dk_usable = usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400545 dk->dk_lookup = lookdict_unicode_nodummy;
Victor Stinner742da042016-09-07 17:40:12 -0700546 dk->dk_nentries = 0;
Benjamin Peterson186122e2016-09-08 12:20:12 -0700547 memset(&dk->dk_indices.as_1[0], 0xff, es * size);
Victor Stinner742da042016-09-07 17:40:12 -0700548 memset(DK_ENTRIES(dk), 0, sizeof(PyDictKeyEntry) * usable);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400549 return dk;
550}
551
552static void
553free_keys_object(PyDictKeysObject *keys)
554{
Victor Stinner742da042016-09-07 17:40:12 -0700555 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400556 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -0700557 for (i = 0, n = keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400558 Py_XDECREF(entries[i].me_key);
559 Py_XDECREF(entries[i].me_value);
560 }
Victor Stinner742da042016-09-07 17:40:12 -0700561 if (keys->dk_size == PyDict_MINSIZE && numfreekeys < PyDict_MAXFREELIST) {
562 keys_free_list[numfreekeys++] = keys;
563 return;
564 }
Raymond Hettingerce5179f2016-01-31 08:56:21 -0800565 PyObject_FREE(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400566}
567
568#define new_values(size) PyMem_NEW(PyObject *, size)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400569#define free_values(values) PyMem_FREE(values)
570
571/* Consumes a reference to the keys object */
572static PyObject *
573new_dict(PyDictKeysObject *keys, PyObject **values)
574{
575 PyDictObject *mp;
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200576 assert(keys != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 if (numfree) {
578 mp = free_list[--numfree];
579 assert (mp != NULL);
580 assert (Py_TYPE(mp) == &PyDict_Type);
581 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400583 else {
584 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
585 if (mp == NULL) {
586 DK_DECREF(keys);
587 free_values(values);
588 return NULL;
589 }
590 }
591 mp->ma_keys = keys;
592 mp->ma_values = values;
593 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700594 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +0200595 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000597}
598
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400599/* Consumes a reference to the keys object */
600static PyObject *
601new_dict_with_shared_keys(PyDictKeysObject *keys)
602{
603 PyObject **values;
604 Py_ssize_t i, size;
605
Victor Stinner742da042016-09-07 17:40:12 -0700606 size = USABLE_FRACTION(DK_SIZE(keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400607 values = new_values(size);
608 if (values == NULL) {
609 DK_DECREF(keys);
610 return PyErr_NoMemory();
611 }
612 for (i = 0; i < size; i++) {
613 values[i] = NULL;
614 }
615 return new_dict(keys, values);
616}
617
618PyObject *
619PyDict_New(void)
620{
Victor Stinner742da042016-09-07 17:40:12 -0700621 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200622 if (keys == NULL)
623 return NULL;
624 return new_dict(keys, NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400625}
626
Victor Stinner742da042016-09-07 17:40:12 -0700627/* Search index of hash table from offset of entry table */
628static Py_ssize_t
629lookdict_index(PyDictKeysObject *k, Py_hash_t hash, Py_ssize_t index)
630{
Victor Stinner742da042016-09-07 17:40:12 -0700631 size_t mask = DK_MASK(k);
INADA Naoki073ae482017-06-23 15:22:50 +0900632 size_t perturb = (size_t)hash;
633 size_t i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700634
INADA Naoki073ae482017-06-23 15:22:50 +0900635 for (;;) {
636 Py_ssize_t ix = dk_get_index(k, i);
Victor Stinner742da042016-09-07 17:40:12 -0700637 if (ix == index) {
638 return i;
639 }
640 if (ix == DKIX_EMPTY) {
641 return DKIX_EMPTY;
642 }
INADA Naoki073ae482017-06-23 15:22:50 +0900643 perturb >>= PERTURB_SHIFT;
644 i = mask & (i*5 + perturb + 1);
Victor Stinner742da042016-09-07 17:40:12 -0700645 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700646 Py_UNREACHABLE();
Victor Stinner742da042016-09-07 17:40:12 -0700647}
648
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000649/*
650The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000651This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000652Open addressing is preferred over chaining since the link overhead for
653chaining would be substantial (100% with typical malloc overhead).
654
Tim Peterseb28ef22001-06-02 05:27:19 +0000655The initial probe index is computed as hash mod the table size. Subsequent
656probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000657
658All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000659
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000660The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000661contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000662Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000663
Victor Stinner742da042016-09-07 17:40:12 -0700664lookdict() is general-purpose, and may return DKIX_ERROR if (and only if) a
Victor Stinnera4348cc2016-09-08 12:01:25 -0700665comparison raises an exception.
Guido van Rossum89d8c602007-09-18 17:26:56 +0000666lookdict_unicode() below is specialized to string keys, comparison of which can
INADA Naoki1b8df102017-02-20 22:48:10 +0900667never raise an exception; that function can never return DKIX_ERROR when key
668is string. Otherwise, it falls back to lookdict().
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400669lookdict_unicode_nodummy is further specialized for string keys that cannot be
670the <dummy> value.
INADA Naoki778928b2017-08-03 23:45:15 +0900671For both, when the key isn't found a DKIX_EMPTY is returned.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000672*/
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100673static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400674lookdict(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900675 Py_hash_t hash, PyObject **value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000676{
INADA Naoki778928b2017-08-03 23:45:15 +0900677 size_t i, mask, perturb;
Victor Stinner742da042016-09-07 17:40:12 -0700678 PyDictKeysObject *dk;
INADA Naoki778928b2017-08-03 23:45:15 +0900679 PyDictKeyEntry *ep0;
Tim Peterseb28ef22001-06-02 05:27:19 +0000680
Antoine Pitrou9a234902012-05-13 20:48:01 +0200681top:
Victor Stinner742da042016-09-07 17:40:12 -0700682 dk = mp->ma_keys;
Victor Stinner742da042016-09-07 17:40:12 -0700683 ep0 = DK_ENTRIES(dk);
INADA Naoki778928b2017-08-03 23:45:15 +0900684 mask = DK_MASK(dk);
685 perturb = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700687
INADA Naoki778928b2017-08-03 23:45:15 +0900688 for (;;) {
689 Py_ssize_t ix = dk_get_index(dk, i);
Victor Stinner742da042016-09-07 17:40:12 -0700690 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700691 *value_addr = NULL;
692 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400693 }
INADA Naoki778928b2017-08-03 23:45:15 +0900694 if (ix >= 0) {
695 PyDictKeyEntry *ep = &ep0[ix];
696 assert(ep->me_key != NULL);
697 if (ep->me_key == key) {
698 *value_addr = ep->me_value;
699 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700700 }
INADA Naoki778928b2017-08-03 23:45:15 +0900701 if (ep->me_hash == hash) {
702 PyObject *startkey = ep->me_key;
703 Py_INCREF(startkey);
704 int cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
705 Py_DECREF(startkey);
706 if (cmp < 0) {
707 *value_addr = NULL;
708 return DKIX_ERROR;
709 }
710 if (dk == mp->ma_keys && ep->me_key == startkey) {
711 if (cmp > 0) {
712 *value_addr = ep->me_value;
713 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700714 }
INADA Naoki778928b2017-08-03 23:45:15 +0900715 }
716 else {
717 /* The dict was mutated, restart */
718 goto top;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400719 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 }
INADA Naoki778928b2017-08-03 23:45:15 +0900722 perturb >>= PERTURB_SHIFT;
723 i = (i*5 + perturb + 1) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700725 Py_UNREACHABLE();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000726}
727
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400728/* Specialized version for string-only keys */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100729static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400730lookdict_unicode(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900731 Py_hash_t hash, PyObject **value_addr)
Fred Drake1bff34a2000-08-31 19:31:38 +0000732{
Victor Stinner742da042016-09-07 17:40:12 -0700733 assert(mp->ma_values == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 /* Make sure this function doesn't have to handle non-unicode keys,
735 including subclasses of str; e.g., one reason to subclass
736 unicodes is to override __eq__, and for speed we don't cater to
737 that here. */
738 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400739 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900740 return lookdict(mp, key, hash, value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 }
Tim Peters15d49292001-05-27 07:39:22 +0000742
INADA Naoki778928b2017-08-03 23:45:15 +0900743 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
744 size_t mask = DK_MASK(mp->ma_keys);
745 size_t perturb = (size_t)hash;
746 size_t i = (size_t)hash & mask;
747
748 for (;;) {
749 Py_ssize_t ix = dk_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700750 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700751 *value_addr = NULL;
752 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400753 }
INADA Naoki778928b2017-08-03 23:45:15 +0900754 if (ix >= 0) {
755 PyDictKeyEntry *ep = &ep0[ix];
756 assert(ep->me_key != NULL);
757 assert(PyUnicode_CheckExact(ep->me_key));
758 if (ep->me_key == key ||
759 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
760 *value_addr = ep->me_value;
761 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700762 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400763 }
INADA Naoki778928b2017-08-03 23:45:15 +0900764 perturb >>= PERTURB_SHIFT;
765 i = mask & (i*5 + perturb + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700767 Py_UNREACHABLE();
Fred Drake1bff34a2000-08-31 19:31:38 +0000768}
769
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400770/* Faster version of lookdict_unicode when it is known that no <dummy> keys
771 * will be present. */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100772static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400773lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900774 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400775{
Victor Stinner742da042016-09-07 17:40:12 -0700776 assert(mp->ma_values == NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400777 /* Make sure this function doesn't have to handle non-unicode keys,
778 including subclasses of str; e.g., one reason to subclass
779 unicodes is to override __eq__, and for speed we don't cater to
780 that here. */
781 if (!PyUnicode_CheckExact(key)) {
782 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900783 return lookdict(mp, key, hash, value_addr);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400784 }
INADA Naoki778928b2017-08-03 23:45:15 +0900785
786 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
787 size_t mask = DK_MASK(mp->ma_keys);
788 size_t perturb = (size_t)hash;
789 size_t i = (size_t)hash & mask;
790
791 for (;;) {
792 Py_ssize_t ix = dk_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700793 assert (ix != DKIX_DUMMY);
794 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700795 *value_addr = NULL;
796 return DKIX_EMPTY;
797 }
INADA Naoki778928b2017-08-03 23:45:15 +0900798 PyDictKeyEntry *ep = &ep0[ix];
799 assert(ep->me_key != NULL);
800 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700801 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400802 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900803 *value_addr = ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700804 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400805 }
INADA Naoki778928b2017-08-03 23:45:15 +0900806 perturb >>= PERTURB_SHIFT;
807 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400808 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700809 Py_UNREACHABLE();
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400810}
811
812/* Version of lookdict for split tables.
813 * All split tables and only split tables use this lookup function.
814 * Split tables only contain unicode keys and no dummy keys,
815 * so algorithm is the same as lookdict_unicode_nodummy.
816 */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100817static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400818lookdict_split(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900819 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400820{
Victor Stinner742da042016-09-07 17:40:12 -0700821 /* mp must split table */
822 assert(mp->ma_values != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400823 if (!PyUnicode_CheckExact(key)) {
INADA Naoki778928b2017-08-03 23:45:15 +0900824 Py_ssize_t ix = lookdict(mp, key, hash, value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700825 if (ix >= 0) {
INADA Naokiba609772016-12-07 20:41:42 +0900826 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700827 }
828 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400829 }
Victor Stinner742da042016-09-07 17:40:12 -0700830
INADA Naoki778928b2017-08-03 23:45:15 +0900831 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
832 size_t mask = DK_MASK(mp->ma_keys);
833 size_t perturb = (size_t)hash;
834 size_t i = (size_t)hash & mask;
835
836 for (;;) {
837 Py_ssize_t ix = dk_get_index(mp->ma_keys, i);
838 assert (ix != DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -0700839 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700840 *value_addr = NULL;
841 return DKIX_EMPTY;
842 }
INADA Naoki778928b2017-08-03 23:45:15 +0900843 PyDictKeyEntry *ep = &ep0[ix];
844 assert(ep->me_key != NULL);
845 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700846 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400847 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900848 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700849 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400850 }
INADA Naoki778928b2017-08-03 23:45:15 +0900851 perturb >>= PERTURB_SHIFT;
852 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400853 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700854 Py_UNREACHABLE();
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400855}
856
Benjamin Petersonfb886362010-04-24 18:21:17 +0000857int
858_PyDict_HasOnlyStringKeys(PyObject *dict)
859{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 Py_ssize_t pos = 0;
861 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000862 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400864 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 return 1;
866 while (PyDict_Next(dict, &pos, &key, &value))
867 if (!PyUnicode_Check(key))
868 return 0;
869 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000870}
871
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000872#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 do { \
874 if (!_PyObject_GC_IS_TRACKED(mp)) { \
875 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
876 _PyObject_GC_MAY_BE_TRACKED(value)) { \
877 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 } \
879 } \
880 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000881
882void
883_PyDict_MaybeUntrack(PyObject *op)
884{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 PyDictObject *mp;
886 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -0700887 Py_ssize_t i, numentries;
888 PyDictKeyEntry *ep0;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
891 return;
892
893 mp = (PyDictObject *) op;
Victor Stinner742da042016-09-07 17:40:12 -0700894 ep0 = DK_ENTRIES(mp->ma_keys);
895 numentries = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400896 if (_PyDict_HasSplitTable(mp)) {
Victor Stinner742da042016-09-07 17:40:12 -0700897 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400898 if ((value = mp->ma_values[i]) == NULL)
899 continue;
900 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
Victor Stinner742da042016-09-07 17:40:12 -0700901 assert(!_PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400902 return;
903 }
904 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400906 else {
Victor Stinner742da042016-09-07 17:40:12 -0700907 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400908 if ((value = ep0[i].me_value) == NULL)
909 continue;
910 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
911 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
912 return;
913 }
914 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000916}
917
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400918/* Internal function to find slot for an item from its hash
Victor Stinner3c336c52016-09-12 14:17:40 +0200919 when it is known that the key is not present in the dict.
920
921 The dict must be combined. */
INADA Naokiba609772016-12-07 20:41:42 +0900922static Py_ssize_t
INADA Naoki778928b2017-08-03 23:45:15 +0900923find_empty_slot(PyDictKeysObject *keys, Py_hash_t hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000924{
INADA Naoki778928b2017-08-03 23:45:15 +0900925 assert(keys != NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000926
INADA Naoki778928b2017-08-03 23:45:15 +0900927 const size_t mask = DK_MASK(keys);
928 size_t i = hash & mask;
929 Py_ssize_t ix = dk_get_index(keys, i);
930 for (size_t perturb = hash; ix >= 0;) {
INADA Naoki267941c2016-10-06 15:19:07 +0900931 perturb >>= PERTURB_SHIFT;
INADA Naoki778928b2017-08-03 23:45:15 +0900932 i = (i*5 + perturb + 1) & mask;
933 ix = dk_get_index(keys, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 }
INADA Naoki778928b2017-08-03 23:45:15 +0900935 return i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000936}
937
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400938static int
939insertion_resize(PyDictObject *mp)
940{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700941 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400942}
Antoine Pitroue965d972012-02-27 00:45:12 +0100943
944/*
945Internal routine to insert a new item into the table.
946Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +0100947Returns -1 if an error occurred, or 0 on success.
948*/
949static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400950insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +0100951{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400952 PyObject *old_value;
INADA Naokiba609772016-12-07 20:41:42 +0900953 PyDictKeyEntry *ep;
Antoine Pitroue965d972012-02-27 00:45:12 +0100954
Serhiy Storchaka753bca32017-05-20 12:30:02 +0300955 Py_INCREF(key);
956 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400957 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
958 if (insertion_resize(mp) < 0)
Serhiy Storchaka753bca32017-05-20 12:30:02 +0300959 goto Fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400960 }
961
INADA Naoki778928b2017-08-03 23:45:15 +0900962 Py_ssize_t ix = mp->ma_keys->dk_lookup(mp, key, hash, &old_value);
Serhiy Storchaka753bca32017-05-20 12:30:02 +0300963 if (ix == DKIX_ERROR)
964 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -0700965
Antoine Pitroud6967322014-10-18 00:35:00 +0200966 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400967 MAINTAIN_TRACKING(mp, key, value);
Victor Stinner742da042016-09-07 17:40:12 -0700968
969 /* When insertion order is different from shared key, we can't share
970 * the key anymore. Convert this instance to combine table.
971 */
972 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +0900973 ((ix >= 0 && old_value == NULL && mp->ma_used != ix) ||
Victor Stinner742da042016-09-07 17:40:12 -0700974 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +0300975 if (insertion_resize(mp) < 0)
976 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -0700977 ix = DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400978 }
Victor Stinner742da042016-09-07 17:40:12 -0700979
980 if (ix == DKIX_EMPTY) {
981 /* Insert into new slot. */
INADA Naokiba609772016-12-07 20:41:42 +0900982 assert(old_value == NULL);
Victor Stinner742da042016-09-07 17:40:12 -0700983 if (mp->ma_keys->dk_usable <= 0) {
984 /* Need to resize. */
Serhiy Storchaka753bca32017-05-20 12:30:02 +0300985 if (insertion_resize(mp) < 0)
986 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -0700987 }
INADA Naoki778928b2017-08-03 23:45:15 +0900988 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naokiba609772016-12-07 20:41:42 +0900989 ep = &DK_ENTRIES(mp->ma_keys)[mp->ma_keys->dk_nentries];
Victor Stinner742da042016-09-07 17:40:12 -0700990 dk_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Victor Stinner742da042016-09-07 17:40:12 -0700991 ep->me_key = key;
992 ep->me_hash = hash;
993 if (mp->ma_values) {
994 assert (mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
995 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400996 }
997 else {
Victor Stinner742da042016-09-07 17:40:12 -0700998 ep->me_value = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400999 }
1000 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001001 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07001002 mp->ma_keys->dk_usable--;
1003 mp->ma_keys->dk_nentries++;
1004 assert(mp->ma_keys->dk_usable >= 0);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001005 assert(_PyDict_CheckConsistency(mp));
Victor Stinner742da042016-09-07 17:40:12 -07001006 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001007 }
Victor Stinner742da042016-09-07 17:40:12 -07001008
INADA Naokiba609772016-12-07 20:41:42 +09001009 if (_PyDict_HasSplitTable(mp)) {
1010 mp->ma_values[ix] = value;
1011 if (old_value == NULL) {
1012 /* pending state */
1013 assert(ix == mp->ma_used);
1014 mp->ma_used++;
1015 }
1016 }
1017 else {
1018 assert(old_value != NULL);
1019 DK_ENTRIES(mp->ma_keys)[ix].me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07001020 }
1021
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001022 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokiba609772016-12-07 20:41:42 +09001023 Py_XDECREF(old_value); /* which **CAN** re-enter (see issue #22653) */
Victor Stinner611b0fa2016-09-14 15:02:01 +02001024 assert(_PyDict_CheckConsistency(mp));
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001025 Py_DECREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001026 return 0;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001027
1028Fail:
1029 Py_DECREF(value);
1030 Py_DECREF(key);
1031 return -1;
Antoine Pitroue965d972012-02-27 00:45:12 +01001032}
1033
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001034/*
luzpaza5293b42017-11-05 07:37:50 -06001035Internal routine used by dictresize() to build a hashtable of entries.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001036*/
1037static void
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001038build_indices(PyDictKeysObject *keys, PyDictKeyEntry *ep, Py_ssize_t n)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001039{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001040 size_t mask = (size_t)DK_SIZE(keys) - 1;
1041 for (Py_ssize_t ix = 0; ix != n; ix++, ep++) {
1042 Py_hash_t hash = ep->me_hash;
1043 size_t i = hash & mask;
1044 for (size_t perturb = hash; dk_get_index(keys, i) != DKIX_EMPTY;) {
1045 perturb >>= PERTURB_SHIFT;
INADA Naoki870c2862017-06-24 09:03:19 +09001046 i = mask & (i*5 + perturb + 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001047 }
1048 dk_set_index(keys, i, ix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001050}
1051
1052/*
1053Restructure the table by allocating a new table and reinserting all
1054items again. When entries have been deleted, the new table may
1055actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001056If a table is split (its keys and hashes are shared, its values are not),
1057then the values are temporarily copied into the table, it is resized as
1058a combined table, then the me_value slots in the old table are NULLed out.
1059After resizing a table is always combined,
1060but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001061*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001062static int
Victor Stinner3d3f2642016-12-15 17:21:23 +01001063dictresize(PyDictObject *mp, Py_ssize_t minsize)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001064{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001065 Py_ssize_t newsize, numentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001066 PyDictKeysObject *oldkeys;
1067 PyObject **oldvalues;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001068 PyDictKeyEntry *oldentries, *newentries;
Tim Peters91a364d2001-05-19 07:04:38 +00001069
Victor Stinner742da042016-09-07 17:40:12 -07001070 /* Find the smallest table size > minused. */
1071 for (newsize = PyDict_MINSIZE;
Victor Stinner3d3f2642016-12-15 17:21:23 +01001072 newsize < minsize && newsize > 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 newsize <<= 1)
1074 ;
1075 if (newsize <= 0) {
1076 PyErr_NoMemory();
1077 return -1;
1078 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001079
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001080 oldkeys = mp->ma_keys;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001081
1082 /* NOTE: Current odict checks mp->ma_keys to detect resize happen.
1083 * So we can't reuse oldkeys even if oldkeys->dk_size == newsize.
1084 * TODO: Try reusing oldkeys when reimplement odict.
1085 */
1086
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001087 /* Allocate a new table. */
1088 mp->ma_keys = new_keys_object(newsize);
1089 if (mp->ma_keys == NULL) {
1090 mp->ma_keys = oldkeys;
1091 return -1;
1092 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01001093 // New table must be large enough.
1094 assert(mp->ma_keys->dk_usable >= mp->ma_used);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001095 if (oldkeys->dk_lookup == lookdict)
1096 mp->ma_keys->dk_lookup = lookdict;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001097
1098 numentries = mp->ma_used;
1099 oldentries = DK_ENTRIES(oldkeys);
1100 newentries = DK_ENTRIES(mp->ma_keys);
1101 oldvalues = mp->ma_values;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001102 if (oldvalues != NULL) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001103 /* Convert split table into new combined table.
1104 * We must incref keys; we can transfer values.
1105 * Note that values of split table is always dense.
1106 */
1107 for (Py_ssize_t i = 0; i < numentries; i++) {
1108 assert(oldvalues[i] != NULL);
1109 PyDictKeyEntry *ep = &oldentries[i];
1110 PyObject *key = ep->me_key;
1111 Py_INCREF(key);
1112 newentries[i].me_key = key;
1113 newentries[i].me_hash = ep->me_hash;
1114 newentries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001116
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001117 DK_DECREF(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001118 mp->ma_values = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001119 if (oldvalues != empty_values) {
1120 free_values(oldvalues);
1121 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001122 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001123 else { // combined table.
1124 if (oldkeys->dk_nentries == numentries) {
1125 memcpy(newentries, oldentries, numentries * sizeof(PyDictKeyEntry));
1126 }
1127 else {
1128 PyDictKeyEntry *ep = oldentries;
1129 for (Py_ssize_t i = 0; i < numentries; i++) {
1130 while (ep->me_value == NULL)
1131 ep++;
1132 newentries[i] = *ep++;
1133 }
1134 }
1135
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001136 assert(oldkeys->dk_lookup != lookdict_split);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001137 assert(oldkeys->dk_refcnt == 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001138 if (oldkeys->dk_size == PyDict_MINSIZE &&
1139 numfreekeys < PyDict_MAXFREELIST) {
1140 DK_DEBUG_DECREF keys_free_list[numfreekeys++] = oldkeys;
1141 }
1142 else {
1143 DK_DEBUG_DECREF PyObject_FREE(oldkeys);
1144 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001145 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001146
1147 build_indices(mp->ma_keys, newentries, numentries);
1148 mp->ma_keys->dk_usable -= numentries;
1149 mp->ma_keys->dk_nentries = numentries;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001151}
1152
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001153/* Returns NULL if unable to split table.
1154 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001155static PyDictKeysObject *
1156make_keys_shared(PyObject *op)
1157{
1158 Py_ssize_t i;
1159 Py_ssize_t size;
1160 PyDictObject *mp = (PyDictObject *)op;
1161
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001162 if (!PyDict_CheckExact(op))
1163 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001164 if (!_PyDict_HasSplitTable(mp)) {
1165 PyDictKeyEntry *ep0;
1166 PyObject **values;
1167 assert(mp->ma_keys->dk_refcnt == 1);
1168 if (mp->ma_keys->dk_lookup == lookdict) {
1169 return NULL;
1170 }
1171 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1172 /* Remove dummy keys */
1173 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1174 return NULL;
1175 }
1176 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1177 /* Copy values into a new array */
Victor Stinner742da042016-09-07 17:40:12 -07001178 ep0 = DK_ENTRIES(mp->ma_keys);
1179 size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001180 values = new_values(size);
1181 if (values == NULL) {
1182 PyErr_SetString(PyExc_MemoryError,
1183 "Not enough memory to allocate new values array");
1184 return NULL;
1185 }
1186 for (i = 0; i < size; i++) {
1187 values[i] = ep0[i].me_value;
1188 ep0[i].me_value = NULL;
1189 }
1190 mp->ma_keys->dk_lookup = lookdict_split;
1191 mp->ma_values = values;
1192 }
1193 DK_INCREF(mp->ma_keys);
1194 return mp->ma_keys;
1195}
Christian Heimes99170a52007-12-19 02:07:34 +00001196
1197PyObject *
1198_PyDict_NewPresized(Py_ssize_t minused)
1199{
INADA Naoki92c50ee2016-11-22 00:57:02 +09001200 const Py_ssize_t max_presize = 128 * 1024;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001201 Py_ssize_t newsize;
1202 PyDictKeysObject *new_keys;
INADA Naoki92c50ee2016-11-22 00:57:02 +09001203
1204 /* There are no strict guarantee that returned dict can contain minused
1205 * items without resize. So we create medium size dict instead of very
1206 * large dict or MemoryError.
1207 */
1208 if (minused > USABLE_FRACTION(max_presize)) {
1209 newsize = max_presize;
1210 }
1211 else {
1212 Py_ssize_t minsize = ESTIMATE_SIZE(minused);
1213 newsize = PyDict_MINSIZE;
1214 while (newsize < minsize) {
1215 newsize <<= 1;
1216 }
1217 }
1218 assert(IS_POWER_OF_2(newsize));
1219
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001220 new_keys = new_keys_object(newsize);
1221 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001223 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001224}
1225
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001226/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1227 * that may occur (originally dicts supported only string keys, and exceptions
1228 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001229 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001230 * (suppressed) occurred while computing the key's hash, or that some error
1231 * (suppressed) occurred when comparing keys in the dict's internal probe
1232 * sequence. A nasty example of the latter is when a Python-coded comparison
1233 * function hits a stack-depth error, which can cause this to return NULL
1234 * even if the key is present.
1235 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001236PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001237PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001238{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001239 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07001240 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 PyDictObject *mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 PyThreadState *tstate;
INADA Naokiba609772016-12-07 20:41:42 +09001243 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 if (!PyDict_Check(op))
1246 return NULL;
1247 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001248 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 {
1250 hash = PyObject_Hash(key);
1251 if (hash == -1) {
1252 PyErr_Clear();
1253 return NULL;
1254 }
1255 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 /* We can arrive here with a NULL tstate during initialization: try
1258 running "python -Wi" for an example related to string interning.
1259 Let's just hope that no exception occurs then... This must be
1260 _PyThreadState_Current and not PyThreadState_GET() because in debug
1261 mode, the latter complains if tstate is NULL. */
Victor Stinner0cae6092016-11-11 01:43:56 +01001262 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 if (tstate != NULL && tstate->curexc_type != NULL) {
1264 /* preserve the existing exception */
1265 PyObject *err_type, *err_value, *err_tb;
1266 PyErr_Fetch(&err_type, &err_value, &err_tb);
INADA Naoki778928b2017-08-03 23:45:15 +09001267 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 /* ignore errors */
1269 PyErr_Restore(err_type, err_value, err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001270 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 return NULL;
1272 }
1273 else {
INADA Naoki778928b2017-08-03 23:45:15 +09001274 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001275 if (ix < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 PyErr_Clear();
1277 return NULL;
1278 }
1279 }
INADA Naokiba609772016-12-07 20:41:42 +09001280 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001281}
1282
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001283/* Same as PyDict_GetItemWithError() but with hash supplied by caller.
1284 This returns NULL *with* an exception set if an exception occurred.
1285 It returns NULL *without* an exception set if the key wasn't present.
1286*/
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001287PyObject *
1288_PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1289{
Victor Stinner742da042016-09-07 17:40:12 -07001290 Py_ssize_t ix;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001291 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001292 PyObject *value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001293
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001294 if (!PyDict_Check(op)) {
1295 PyErr_BadInternalCall();
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001296 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001297 }
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001298
INADA Naoki778928b2017-08-03 23:45:15 +09001299 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001300 if (ix < 0) {
1301 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001302 }
INADA Naokiba609772016-12-07 20:41:42 +09001303 return value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001304}
1305
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001306/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1307 This returns NULL *with* an exception set if an exception occurred.
1308 It returns NULL *without* an exception set if the key wasn't present.
1309*/
1310PyObject *
1311PyDict_GetItemWithError(PyObject *op, PyObject *key)
1312{
Victor Stinner742da042016-09-07 17:40:12 -07001313 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001314 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 PyDictObject*mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001316 PyObject *value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 if (!PyDict_Check(op)) {
1319 PyErr_BadInternalCall();
1320 return NULL;
1321 }
1322 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001323 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 {
1325 hash = PyObject_Hash(key);
1326 if (hash == -1) {
1327 return NULL;
1328 }
1329 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001330
INADA Naoki778928b2017-08-03 23:45:15 +09001331 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001332 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001334 return value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001335}
1336
Brett Cannonfd074152012-04-14 14:10:13 -04001337PyObject *
1338_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1339{
1340 PyObject *kv;
1341 kv = _PyUnicode_FromId(key); /* borrowed */
1342 if (kv == NULL)
1343 return NULL;
1344 return PyDict_GetItemWithError(dp, kv);
1345}
1346
Victor Stinnerb4efc962015-11-20 09:24:02 +01001347/* Fast version of global value lookup (LOAD_GLOBAL).
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001348 * Lookup in globals, then builtins.
Victor Stinnerb4efc962015-11-20 09:24:02 +01001349 *
1350 * Raise an exception and return NULL if an error occurred (ex: computing the
1351 * key hash failed, key comparison failed, ...). Return NULL if the key doesn't
1352 * exist. Return the value if the key exists.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001353 */
1354PyObject *
1355_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001356{
Victor Stinner742da042016-09-07 17:40:12 -07001357 Py_ssize_t ix;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001358 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09001359 PyObject *value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001360
1361 if (!PyUnicode_CheckExact(key) ||
1362 (hash = ((PyASCIIObject *) key)->hash) == -1)
1363 {
1364 hash = PyObject_Hash(key);
1365 if (hash == -1)
1366 return NULL;
Antoine Pitroue965d972012-02-27 00:45:12 +01001367 }
Victor Stinnerb4efc962015-11-20 09:24:02 +01001368
1369 /* namespace 1: globals */
INADA Naoki778928b2017-08-03 23:45:15 +09001370 ix = globals->ma_keys->dk_lookup(globals, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001371 if (ix == DKIX_ERROR)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001372 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001373 if (ix != DKIX_EMPTY && value != NULL)
1374 return value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001375
1376 /* namespace 2: builtins */
INADA Naoki778928b2017-08-03 23:45:15 +09001377 ix = builtins->ma_keys->dk_lookup(builtins, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001378 if (ix < 0)
Victor Stinnerb4efc962015-11-20 09:24:02 +01001379 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001380 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001381}
1382
Antoine Pitroue965d972012-02-27 00:45:12 +01001383/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1384 * dictionary if it's merely replacing the value for an existing key.
1385 * This means that it's safe to loop over a dictionary with PyDict_Next()
1386 * and occasionally replace a value -- but you can't insert new keys or
1387 * remove them.
1388 */
1389int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001390PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001391{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001392 PyDictObject *mp;
1393 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001394 if (!PyDict_Check(op)) {
1395 PyErr_BadInternalCall();
1396 return -1;
1397 }
1398 assert(key);
1399 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001400 mp = (PyDictObject *)op;
1401 if (!PyUnicode_CheckExact(key) ||
1402 (hash = ((PyASCIIObject *) key)->hash) == -1)
1403 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001404 hash = PyObject_Hash(key);
1405 if (hash == -1)
1406 return -1;
1407 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001408
1409 /* insertdict() handles any resizing that might be necessary */
1410 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001411}
1412
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001413int
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001414_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1415 Py_hash_t hash)
1416{
1417 PyDictObject *mp;
1418
1419 if (!PyDict_Check(op)) {
1420 PyErr_BadInternalCall();
1421 return -1;
1422 }
1423 assert(key);
1424 assert(value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001425 assert(hash != -1);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001426 mp = (PyDictObject *)op;
1427
1428 /* insertdict() handles any resizing that might be necessary */
1429 return insertdict(mp, key, hash, value);
1430}
1431
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001432static int
INADA Naoki778928b2017-08-03 23:45:15 +09001433delitem_common(PyDictObject *mp, Py_hash_t hash, Py_ssize_t ix,
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001434 PyObject *old_value)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001435{
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001436 PyObject *old_key;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001437 PyDictKeyEntry *ep;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001438
INADA Naoki778928b2017-08-03 23:45:15 +09001439 Py_ssize_t hashpos = lookdict_index(mp->ma_keys, hash, ix);
1440 assert(hashpos >= 0);
1441
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001442 mp->ma_used--;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001443 mp->ma_version_tag = DICT_NEXT_VERSION();
1444 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1445 dk_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
1446 ENSURE_ALLOWS_DELETIONS(mp);
1447 old_key = ep->me_key;
1448 ep->me_key = NULL;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001449 ep->me_value = NULL;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001450 Py_DECREF(old_key);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001451 Py_DECREF(old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001452
1453 assert(_PyDict_CheckConsistency(mp));
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001454 return 0;
1455}
1456
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001457int
Tim Peters1f5871e2000-07-04 17:44:48 +00001458PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001459{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001460 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 assert(key);
1462 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001463 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 hash = PyObject_Hash(key);
1465 if (hash == -1)
1466 return -1;
1467 }
Victor Stinner742da042016-09-07 17:40:12 -07001468
1469 return _PyDict_DelItem_KnownHash(op, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001470}
1471
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001472int
1473_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1474{
INADA Naoki778928b2017-08-03 23:45:15 +09001475 Py_ssize_t ix;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001476 PyDictObject *mp;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001477 PyObject *old_value;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001478
1479 if (!PyDict_Check(op)) {
1480 PyErr_BadInternalCall();
1481 return -1;
1482 }
1483 assert(key);
1484 assert(hash != -1);
1485 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001486 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001487 if (ix == DKIX_ERROR)
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001488 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09001489 if (ix == DKIX_EMPTY || old_value == NULL) {
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001490 _PyErr_SetKeyError(key);
1491 return -1;
1492 }
Victor Stinner78601a32016-09-09 19:28:36 -07001493
1494 // Split table doesn't allow deletion. Combine it.
1495 if (_PyDict_HasSplitTable(mp)) {
1496 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1497 return -1;
1498 }
INADA Naoki778928b2017-08-03 23:45:15 +09001499 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001500 assert(ix >= 0);
1501 }
1502
INADA Naoki778928b2017-08-03 23:45:15 +09001503 return delitem_common(mp, hash, ix, old_value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001504}
1505
Antoine Pitroud741ed42016-12-27 14:23:43 +01001506/* This function promises that the predicate -> deletion sequence is atomic
1507 * (i.e. protected by the GIL), assuming the predicate itself doesn't
1508 * release the GIL.
1509 */
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001510int
1511_PyDict_DelItemIf(PyObject *op, PyObject *key,
1512 int (*predicate)(PyObject *value))
1513{
Antoine Pitroud741ed42016-12-27 14:23:43 +01001514 Py_ssize_t hashpos, ix;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001515 PyDictObject *mp;
1516 Py_hash_t hash;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001517 PyObject *old_value;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001518 int res;
1519
1520 if (!PyDict_Check(op)) {
1521 PyErr_BadInternalCall();
1522 return -1;
1523 }
1524 assert(key);
1525 hash = PyObject_Hash(key);
1526 if (hash == -1)
1527 return -1;
1528 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001529 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001530 if (ix == DKIX_ERROR)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001531 return -1;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001532 if (ix == DKIX_EMPTY || old_value == NULL) {
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001533 _PyErr_SetKeyError(key);
1534 return -1;
1535 }
Antoine Pitroud741ed42016-12-27 14:23:43 +01001536
1537 // Split table doesn't allow deletion. Combine it.
1538 if (_PyDict_HasSplitTable(mp)) {
1539 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1540 return -1;
1541 }
INADA Naoki778928b2017-08-03 23:45:15 +09001542 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001543 assert(ix >= 0);
1544 }
1545
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001546 res = predicate(old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001547 if (res == -1)
1548 return -1;
INADA Naoki778928b2017-08-03 23:45:15 +09001549
1550 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1551 assert(hashpos >= 0);
1552
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001553 if (res > 0)
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001554 return delitem_common(mp, hashpos, ix, old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001555 else
1556 return 0;
1557}
1558
1559
Guido van Rossum25831651993-05-19 14:50:45 +00001560void
Tim Peters1f5871e2000-07-04 17:44:48 +00001561PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001562{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001564 PyDictKeysObject *oldkeys;
1565 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 if (!PyDict_Check(op))
1569 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001570 mp = ((PyDictObject *)op);
1571 oldkeys = mp->ma_keys;
1572 oldvalues = mp->ma_values;
1573 if (oldvalues == empty_values)
1574 return;
1575 /* Empty the dict... */
1576 DK_INCREF(Py_EMPTY_KEYS);
1577 mp->ma_keys = Py_EMPTY_KEYS;
1578 mp->ma_values = empty_values;
1579 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001580 mp->ma_version_tag = DICT_NEXT_VERSION();
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001581 /* ...then clear the keys and values */
1582 if (oldvalues != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001583 n = oldkeys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001584 for (i = 0; i < n; i++)
1585 Py_CLEAR(oldvalues[i]);
1586 free_values(oldvalues);
1587 DK_DECREF(oldkeys);
1588 }
1589 else {
1590 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001591 DK_DECREF(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001592 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02001593 assert(_PyDict_CheckConsistency(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001594}
1595
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001596/* Internal version of PyDict_Next that returns a hash value in addition
1597 * to the key and value.
1598 * Return 1 on success, return 0 when the reached the end of the dictionary
1599 * (or if op is not a dictionary)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001600 */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001601int
1602_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1603 PyObject **pvalue, Py_hash_t *phash)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001604{
INADA Naokica2d8be2016-11-04 16:59:10 +09001605 Py_ssize_t i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001606 PyDictObject *mp;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001607 PyDictKeyEntry *entry_ptr;
1608 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001609
1610 if (!PyDict_Check(op))
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001611 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 mp = (PyDictObject *)op;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001613 i = *ppos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001614 if (mp->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09001615 if (i < 0 || i >= mp->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001616 return 0;
INADA Naokica2d8be2016-11-04 16:59:10 +09001617 /* values of split table is always dense */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001618 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
INADA Naokica2d8be2016-11-04 16:59:10 +09001619 value = mp->ma_values[i];
1620 assert(value != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001622 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09001623 Py_ssize_t n = mp->ma_keys->dk_nentries;
1624 if (i < 0 || i >= n)
1625 return 0;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001626 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
1627 while (i < n && entry_ptr->me_value == NULL) {
1628 entry_ptr++;
1629 i++;
Victor Stinner742da042016-09-07 17:40:12 -07001630 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001631 if (i >= n)
1632 return 0;
1633 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001635 *ppos = i+1;
1636 if (pkey)
1637 *pkey = entry_ptr->me_key;
1638 if (phash)
1639 *phash = entry_ptr->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001640 if (pvalue)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001641 *pvalue = value;
1642 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001643}
1644
Tim Peters080c88b2003-02-15 03:01:11 +00001645/*
1646 * Iterate over a dict. Use like so:
1647 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001648 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001649 * PyObject *key, *value;
1650 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001651 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001652 * Refer to borrowed references in key and value.
Tim Peters080c88b2003-02-15 03:01:11 +00001653 * }
1654 *
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001655 * Return 1 on success, return 0 when the reached the end of the dictionary
1656 * (or if op is not a dictionary)
1657 *
Tim Peters080c88b2003-02-15 03:01:11 +00001658 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001659 * mutates the dict. One exception: it is safe if the loop merely changes
1660 * the values associated with the keys (but doesn't insert new keys or
1661 * delete keys), via PyDict_SetItem().
1662 */
Guido van Rossum25831651993-05-19 14:50:45 +00001663int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001664PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001665{
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001666 return _PyDict_Next(op, ppos, pkey, pvalue, NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001667}
1668
Eric Snow96c6af92015-05-29 22:21:39 -06001669/* Internal version of dict.pop(). */
1670PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001671_PyDict_Pop_KnownHash(PyObject *dict, PyObject *key, Py_hash_t hash, PyObject *deflt)
Eric Snow96c6af92015-05-29 22:21:39 -06001672{
Victor Stinner742da042016-09-07 17:40:12 -07001673 Py_ssize_t ix, hashpos;
Eric Snow96c6af92015-05-29 22:21:39 -06001674 PyObject *old_value, *old_key;
1675 PyDictKeyEntry *ep;
Yury Selivanov684ef2c2016-10-28 19:01:21 -04001676 PyDictObject *mp;
1677
1678 assert(PyDict_Check(dict));
1679 mp = (PyDictObject *)dict;
Eric Snow96c6af92015-05-29 22:21:39 -06001680
1681 if (mp->ma_used == 0) {
1682 if (deflt) {
1683 Py_INCREF(deflt);
1684 return deflt;
1685 }
1686 _PyErr_SetKeyError(key);
1687 return NULL;
1688 }
INADA Naoki778928b2017-08-03 23:45:15 +09001689 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001690 if (ix == DKIX_ERROR)
Eric Snow96c6af92015-05-29 22:21:39 -06001691 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001692 if (ix == DKIX_EMPTY || old_value == NULL) {
Eric Snow96c6af92015-05-29 22:21:39 -06001693 if (deflt) {
1694 Py_INCREF(deflt);
1695 return deflt;
1696 }
1697 _PyErr_SetKeyError(key);
1698 return NULL;
1699 }
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001700
Victor Stinner78601a32016-09-09 19:28:36 -07001701 // Split table doesn't allow deletion. Combine it.
1702 if (_PyDict_HasSplitTable(mp)) {
1703 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1704 return NULL;
1705 }
INADA Naoki778928b2017-08-03 23:45:15 +09001706 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001707 assert(ix >= 0);
1708 }
1709
INADA Naoki778928b2017-08-03 23:45:15 +09001710 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1711 assert(hashpos >= 0);
Victor Stinner78601a32016-09-09 19:28:36 -07001712 assert(old_value != NULL);
Eric Snow96c6af92015-05-29 22:21:39 -06001713 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001714 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner78601a32016-09-09 19:28:36 -07001715 dk_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
1716 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1717 ENSURE_ALLOWS_DELETIONS(mp);
1718 old_key = ep->me_key;
1719 ep->me_key = NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001720 ep->me_value = NULL;
Victor Stinner78601a32016-09-09 19:28:36 -07001721 Py_DECREF(old_key);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001722
1723 assert(_PyDict_CheckConsistency(mp));
Eric Snow96c6af92015-05-29 22:21:39 -06001724 return old_value;
1725}
1726
Serhiy Storchaka67796522017-01-12 18:34:33 +02001727PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001728_PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt)
Serhiy Storchaka67796522017-01-12 18:34:33 +02001729{
1730 Py_hash_t hash;
1731
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001732 if (((PyDictObject *)dict)->ma_used == 0) {
Serhiy Storchaka67796522017-01-12 18:34:33 +02001733 if (deflt) {
1734 Py_INCREF(deflt);
1735 return deflt;
1736 }
1737 _PyErr_SetKeyError(key);
1738 return NULL;
1739 }
1740 if (!PyUnicode_CheckExact(key) ||
1741 (hash = ((PyASCIIObject *) key)->hash) == -1) {
1742 hash = PyObject_Hash(key);
1743 if (hash == -1)
1744 return NULL;
1745 }
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001746 return _PyDict_Pop_KnownHash(dict, key, hash, deflt);
Serhiy Storchaka67796522017-01-12 18:34:33 +02001747}
1748
Eric Snow96c6af92015-05-29 22:21:39 -06001749/* Internal version of dict.from_keys(). It is subclass-friendly. */
1750PyObject *
1751_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1752{
1753 PyObject *it; /* iter(iterable) */
1754 PyObject *key;
1755 PyObject *d;
1756 int status;
1757
Victor Stinnera5ed5f02016-12-06 18:45:50 +01001758 d = _PyObject_CallNoArg(cls);
Eric Snow96c6af92015-05-29 22:21:39 -06001759 if (d == NULL)
1760 return NULL;
1761
1762 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
1763 if (PyDict_CheckExact(iterable)) {
1764 PyDictObject *mp = (PyDictObject *)d;
1765 PyObject *oldvalue;
1766 Py_ssize_t pos = 0;
1767 PyObject *key;
1768 Py_hash_t hash;
1769
Serhiy Storchakac61ac162017-03-21 08:52:38 +02001770 if (dictresize(mp, ESTIMATE_SIZE(PyDict_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001771 Py_DECREF(d);
1772 return NULL;
1773 }
1774
1775 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
1776 if (insertdict(mp, key, hash, value)) {
1777 Py_DECREF(d);
1778 return NULL;
1779 }
1780 }
1781 return d;
1782 }
1783 if (PyAnySet_CheckExact(iterable)) {
1784 PyDictObject *mp = (PyDictObject *)d;
1785 Py_ssize_t pos = 0;
1786 PyObject *key;
1787 Py_hash_t hash;
1788
Victor Stinner742da042016-09-07 17:40:12 -07001789 if (dictresize(mp, ESTIMATE_SIZE(PySet_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001790 Py_DECREF(d);
1791 return NULL;
1792 }
1793
1794 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
1795 if (insertdict(mp, key, hash, value)) {
1796 Py_DECREF(d);
1797 return NULL;
1798 }
1799 }
1800 return d;
1801 }
1802 }
1803
1804 it = PyObject_GetIter(iterable);
1805 if (it == NULL){
1806 Py_DECREF(d);
1807 return NULL;
1808 }
1809
1810 if (PyDict_CheckExact(d)) {
1811 while ((key = PyIter_Next(it)) != NULL) {
1812 status = PyDict_SetItem(d, key, value);
1813 Py_DECREF(key);
1814 if (status < 0)
1815 goto Fail;
1816 }
1817 } else {
1818 while ((key = PyIter_Next(it)) != NULL) {
1819 status = PyObject_SetItem(d, key, value);
1820 Py_DECREF(key);
1821 if (status < 0)
1822 goto Fail;
1823 }
1824 }
1825
1826 if (PyErr_Occurred())
1827 goto Fail;
1828 Py_DECREF(it);
1829 return d;
1830
1831Fail:
1832 Py_DECREF(it);
1833 Py_DECREF(d);
1834 return NULL;
1835}
1836
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001837/* Methods */
1838
1839static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001840dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001841{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001842 PyObject **values = mp->ma_values;
1843 PyDictKeysObject *keys = mp->ma_keys;
1844 Py_ssize_t i, n;
INADA Naokia6296d32017-08-24 14:55:17 +09001845
1846 /* bpo-31095: UnTrack is needed before calling any callbacks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 PyObject_GC_UnTrack(mp);
1848 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001849 if (values != NULL) {
1850 if (values != empty_values) {
Victor Stinner742da042016-09-07 17:40:12 -07001851 for (i = 0, n = mp->ma_keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001852 Py_XDECREF(values[i]);
1853 }
1854 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001856 DK_DECREF(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02001858 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001859 assert(keys->dk_refcnt == 1);
1860 DK_DECREF(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001861 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1863 free_list[numfree++] = mp;
1864 else
1865 Py_TYPE(mp)->tp_free((PyObject *)mp);
1866 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001867}
1868
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001869
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001870static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001871dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001872{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001873 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001874 PyObject *key = NULL, *value = NULL;
1875 _PyUnicodeWriter writer;
1876 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00001877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878 i = Py_ReprEnter((PyObject *)mp);
1879 if (i != 0) {
1880 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1881 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001883 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001884 Py_ReprLeave((PyObject *)mp);
1885 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 }
Tim Petersa7259592001-06-16 05:11:17 +00001887
Victor Stinnerf91929b2013-11-19 13:07:38 +01001888 _PyUnicodeWriter_Init(&writer);
1889 writer.overallocate = 1;
1890 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
1891 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00001892
Victor Stinnerf91929b2013-11-19 13:07:38 +01001893 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
1894 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 /* Do repr() on each key+value pair, and insert ": " between them.
1897 Note that repr may mutate the dict. */
1898 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001899 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001901 PyObject *s;
1902 int res;
1903
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001904 /* Prevent repr from deleting key or value during key format. */
1905 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02001907
Victor Stinnerf91929b2013-11-19 13:07:38 +01001908 if (!first) {
1909 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
1910 goto error;
1911 }
1912 first = 0;
1913
1914 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01001916 goto error;
1917 res = _PyUnicodeWriter_WriteStr(&writer, s);
1918 Py_DECREF(s);
1919 if (res < 0)
1920 goto error;
1921
1922 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
1923 goto error;
1924
1925 s = PyObject_Repr(value);
1926 if (s == NULL)
1927 goto error;
1928 res = _PyUnicodeWriter_WriteStr(&writer, s);
1929 Py_DECREF(s);
1930 if (res < 0)
1931 goto error;
1932
1933 Py_CLEAR(key);
1934 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 }
Tim Petersa7259592001-06-16 05:11:17 +00001936
Victor Stinnerf91929b2013-11-19 13:07:38 +01001937 writer.overallocate = 0;
1938 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
1939 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001940
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01001942
1943 return _PyUnicodeWriter_Finish(&writer);
1944
1945error:
1946 Py_ReprLeave((PyObject *)mp);
1947 _PyUnicodeWriter_Dealloc(&writer);
1948 Py_XDECREF(key);
1949 Py_XDECREF(value);
1950 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001951}
1952
Martin v. Löwis18e16552006-02-15 17:27:45 +00001953static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001954dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001955{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001957}
1958
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001959static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001960dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001961{
Victor Stinner742da042016-09-07 17:40:12 -07001962 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001963 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09001964 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001966 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001967 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968 hash = PyObject_Hash(key);
1969 if (hash == -1)
1970 return NULL;
1971 }
INADA Naoki778928b2017-08-03 23:45:15 +09001972 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001973 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001975 if (ix == DKIX_EMPTY || value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001976 if (!PyDict_CheckExact(mp)) {
1977 /* Look up __missing__ method if we're a subclass. */
1978 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05001979 _Py_IDENTIFIER(__missing__);
1980 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 if (missing != NULL) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001982 res = PyObject_CallFunctionObjArgs(missing,
1983 key, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 Py_DECREF(missing);
1985 return res;
1986 }
1987 else if (PyErr_Occurred())
1988 return NULL;
1989 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07001990 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001991 return NULL;
1992 }
INADA Naokiba609772016-12-07 20:41:42 +09001993 Py_INCREF(value);
1994 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001995}
1996
1997static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001998dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001999{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000 if (w == NULL)
2001 return PyDict_DelItem((PyObject *)mp, v);
2002 else
2003 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002004}
2005
Guido van Rossuma9e7a811997-05-13 21:02:11 +00002006static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002007 (lenfunc)dict_length, /*mp_length*/
2008 (binaryfunc)dict_subscript, /*mp_subscript*/
2009 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002010};
2011
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002012static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002013dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002014{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002015 PyObject *v;
2016 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002017 PyDictKeyEntry *ep;
2018 Py_ssize_t size, n, offset;
2019 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002020
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002021 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002022 n = mp->ma_used;
2023 v = PyList_New(n);
2024 if (v == NULL)
2025 return NULL;
2026 if (n != mp->ma_used) {
2027 /* Durnit. The allocations caused the dict to resize.
2028 * Just start over, this shouldn't normally happen.
2029 */
2030 Py_DECREF(v);
2031 goto again;
2032 }
Victor Stinner742da042016-09-07 17:40:12 -07002033 ep = DK_ENTRIES(mp->ma_keys);
2034 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002035 if (mp->ma_values) {
2036 value_ptr = mp->ma_values;
2037 offset = sizeof(PyObject *);
2038 }
2039 else {
2040 value_ptr = &ep[0].me_value;
2041 offset = sizeof(PyDictKeyEntry);
2042 }
2043 for (i = 0, j = 0; i < size; i++) {
2044 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045 PyObject *key = ep[i].me_key;
2046 Py_INCREF(key);
2047 PyList_SET_ITEM(v, j, key);
2048 j++;
2049 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002050 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002051 }
2052 assert(j == n);
2053 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002054}
2055
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002056static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002057dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002058{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002059 PyObject *v;
2060 Py_ssize_t i, j;
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002061 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002062 Py_ssize_t size, n, offset;
2063 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002064
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002065 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002066 n = mp->ma_used;
2067 v = PyList_New(n);
2068 if (v == NULL)
2069 return NULL;
2070 if (n != mp->ma_used) {
2071 /* Durnit. The allocations caused the dict to resize.
2072 * Just start over, this shouldn't normally happen.
2073 */
2074 Py_DECREF(v);
2075 goto again;
2076 }
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002077 ep = DK_ENTRIES(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002078 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002079 if (mp->ma_values) {
2080 value_ptr = mp->ma_values;
2081 offset = sizeof(PyObject *);
2082 }
2083 else {
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002084 value_ptr = &ep[0].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002085 offset = sizeof(PyDictKeyEntry);
2086 }
2087 for (i = 0, j = 0; i < size; i++) {
2088 PyObject *value = *value_ptr;
2089 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2090 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002091 Py_INCREF(value);
2092 PyList_SET_ITEM(v, j, value);
2093 j++;
2094 }
2095 }
2096 assert(j == n);
2097 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002098}
2099
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002100static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002101dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002102{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002103 PyObject *v;
2104 Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002105 Py_ssize_t size, offset;
2106 PyObject *item, *key;
2107 PyDictKeyEntry *ep;
2108 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002110 /* Preallocate the list of tuples, to avoid allocations during
2111 * the loop over the items, which could trigger GC, which
2112 * could resize the dict. :-(
2113 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002114 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002115 n = mp->ma_used;
2116 v = PyList_New(n);
2117 if (v == NULL)
2118 return NULL;
2119 for (i = 0; i < n; i++) {
2120 item = PyTuple_New(2);
2121 if (item == NULL) {
2122 Py_DECREF(v);
2123 return NULL;
2124 }
2125 PyList_SET_ITEM(v, i, item);
2126 }
2127 if (n != mp->ma_used) {
2128 /* Durnit. The allocations caused the dict to resize.
2129 * Just start over, this shouldn't normally happen.
2130 */
2131 Py_DECREF(v);
2132 goto again;
2133 }
2134 /* Nothing we do below makes any function calls. */
Victor Stinner742da042016-09-07 17:40:12 -07002135 ep = DK_ENTRIES(mp->ma_keys);
2136 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002137 if (mp->ma_values) {
2138 value_ptr = mp->ma_values;
2139 offset = sizeof(PyObject *);
2140 }
2141 else {
2142 value_ptr = &ep[0].me_value;
2143 offset = sizeof(PyDictKeyEntry);
2144 }
2145 for (i = 0, j = 0; i < size; i++) {
2146 PyObject *value = *value_ptr;
2147 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2148 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002149 key = ep[i].me_key;
2150 item = PyList_GET_ITEM(v, j);
2151 Py_INCREF(key);
2152 PyTuple_SET_ITEM(item, 0, key);
2153 Py_INCREF(value);
2154 PyTuple_SET_ITEM(item, 1, value);
2155 j++;
2156 }
2157 }
2158 assert(j == n);
2159 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002160}
2161
Larry Hastings5c661892014-01-24 06:17:25 -08002162/*[clinic input]
2163@classmethod
2164dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08002165 iterable: object
2166 value: object=None
2167 /
2168
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002169Create a new dictionary with keys from iterable and values set to value.
Larry Hastings5c661892014-01-24 06:17:25 -08002170[clinic start generated code]*/
2171
Larry Hastings5c661892014-01-24 06:17:25 -08002172static PyObject *
2173dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002174/*[clinic end generated code: output=8fb98e4b10384999 input=382ba4855d0f74c3]*/
Larry Hastings5c661892014-01-24 06:17:25 -08002175{
Eric Snow96c6af92015-05-29 22:21:39 -06002176 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002177}
2178
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002179static int
Victor Stinner742da042016-09-07 17:40:12 -07002180dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
2181 const char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002182{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002183 PyObject *arg = NULL;
2184 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002185
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002186 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002187 result = -1;
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002188 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002189 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002190 _Py_IDENTIFIER(keys);
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002191 PyObject *func = _PyObject_GetAttrId(arg, &PyId_keys);
2192 if (func != NULL) {
2193 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 result = PyDict_Merge(self, arg, 1);
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002195 }
2196 else if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2197 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198 result = PyDict_MergeFromSeq2(self, arg, 1);
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002199 }
2200 else {
2201 result = -1;
2202 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002203 }
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002204
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002205 if (result == 0 && kwds != NULL) {
2206 if (PyArg_ValidateKeywordArguments(kwds))
2207 result = PyDict_Merge(self, kwds, 1);
2208 else
2209 result = -1;
2210 }
2211 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002212}
2213
Victor Stinner91f0d4a2017-01-19 12:45:06 +01002214/* Note: dict.update() uses the METH_VARARGS|METH_KEYWORDS calling convention.
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002215 Using METH_FASTCALL|METH_KEYWORDS would make dict.update(**dict2) calls
2216 slower, see the issue #29312. */
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002217static PyObject *
2218dict_update(PyObject *self, PyObject *args, PyObject *kwds)
2219{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002220 if (dict_update_common(self, args, kwds, "update") != -1)
2221 Py_RETURN_NONE;
2222 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002223}
2224
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002225/* Update unconditionally replaces existing items.
2226 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00002227 otherwise it leaves existing items unchanged.
2228
2229 PyDict_{Update,Merge} update/merge from a mapping object.
2230
Tim Petersf582b822001-12-11 18:51:08 +00002231 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00002232 producing iterable objects of length 2.
2233*/
2234
Tim Petersf582b822001-12-11 18:51:08 +00002235int
Tim Peters1fc240e2001-10-26 05:06:50 +00002236PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
2237{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002238 PyObject *it; /* iter(seq2) */
2239 Py_ssize_t i; /* index into seq2 of current element */
2240 PyObject *item; /* seq2[i] */
2241 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00002242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002243 assert(d != NULL);
2244 assert(PyDict_Check(d));
2245 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00002246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002247 it = PyObject_GetIter(seq2);
2248 if (it == NULL)
2249 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002251 for (i = 0; ; ++i) {
2252 PyObject *key, *value;
2253 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00002254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002255 fast = NULL;
2256 item = PyIter_Next(it);
2257 if (item == NULL) {
2258 if (PyErr_Occurred())
2259 goto Fail;
2260 break;
2261 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002263 /* Convert item to sequence, and verify length 2. */
2264 fast = PySequence_Fast(item, "");
2265 if (fast == NULL) {
2266 if (PyErr_ExceptionMatches(PyExc_TypeError))
2267 PyErr_Format(PyExc_TypeError,
2268 "cannot convert dictionary update "
2269 "sequence element #%zd to a sequence",
2270 i);
2271 goto Fail;
2272 }
2273 n = PySequence_Fast_GET_SIZE(fast);
2274 if (n != 2) {
2275 PyErr_Format(PyExc_ValueError,
2276 "dictionary update sequence element #%zd "
2277 "has length %zd; 2 is required",
2278 i, n);
2279 goto Fail;
2280 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002282 /* Update/merge with this (key, value) pair. */
2283 key = PySequence_Fast_GET_ITEM(fast, 0);
2284 value = PySequence_Fast_GET_ITEM(fast, 1);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002285 Py_INCREF(key);
2286 Py_INCREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287 if (override || PyDict_GetItem(d, key) == NULL) {
2288 int status = PyDict_SetItem(d, key, value);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002289 if (status < 0) {
2290 Py_DECREF(key);
2291 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292 goto Fail;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002293 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002295 Py_DECREF(key);
2296 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002297 Py_DECREF(fast);
2298 Py_DECREF(item);
2299 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002301 i = 0;
Victor Stinner611b0fa2016-09-14 15:02:01 +02002302 assert(_PyDict_CheckConsistency((PyDictObject *)d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002303 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002304Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002305 Py_XDECREF(item);
2306 Py_XDECREF(fast);
2307 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002308Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002309 Py_DECREF(it);
2310 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002311}
2312
doko@ubuntu.comc96df682016-10-11 08:04:02 +02002313static int
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002314dict_merge(PyObject *a, PyObject *b, int override)
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002315{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002316 PyDictObject *mp, *other;
2317 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -07002318 PyDictKeyEntry *entry, *ep0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002319
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002320 assert(0 <= override && override <= 2);
2321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002322 /* We accept for the argument either a concrete dictionary object,
2323 * or an abstract "mapping" object. For the former, we can do
2324 * things quite efficiently. For the latter, we only require that
2325 * PyMapping_Keys() and PyObject_GetItem() be supported.
2326 */
2327 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2328 PyErr_BadInternalCall();
2329 return -1;
2330 }
2331 mp = (PyDictObject*)a;
2332 if (PyDict_Check(b)) {
2333 other = (PyDictObject*)b;
2334 if (other == mp || other->ma_used == 0)
2335 /* a.update(a) or a.update({}); nothing to do */
2336 return 0;
2337 if (mp->ma_used == 0)
2338 /* Since the target dict is empty, PyDict_GetItem()
2339 * always returns NULL. Setting override to 1
2340 * skips the unnecessary test.
2341 */
2342 override = 1;
2343 /* Do one big resize at the start, rather than
2344 * incrementally resizing as we insert new items. Expect
2345 * that there will be no (or few) overlapping keys.
2346 */
INADA Naokib1152be2016-10-27 19:26:50 +09002347 if (USABLE_FRACTION(mp->ma_keys->dk_size) < other->ma_used) {
2348 if (dictresize(mp, ESTIMATE_SIZE(mp->ma_used + other->ma_used))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002349 return -1;
INADA Naokib1152be2016-10-27 19:26:50 +09002350 }
2351 }
Victor Stinner742da042016-09-07 17:40:12 -07002352 ep0 = DK_ENTRIES(other->ma_keys);
2353 for (i = 0, n = other->ma_keys->dk_nentries; i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002354 PyObject *key, *value;
2355 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002356 entry = &ep0[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002357 key = entry->me_key;
2358 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002359 if (other->ma_values)
2360 value = other->ma_values[i];
2361 else
2362 value = entry->me_value;
2363
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002364 if (value != NULL) {
2365 int err = 0;
2366 Py_INCREF(key);
2367 Py_INCREF(value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002368 if (override == 1)
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002369 err = insertdict(mp, key, hash, value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002370 else if (_PyDict_GetItem_KnownHash(a, key, hash) == NULL) {
2371 if (PyErr_Occurred()) {
2372 Py_DECREF(value);
2373 Py_DECREF(key);
2374 return -1;
2375 }
2376 err = insertdict(mp, key, hash, value);
2377 }
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002378 else if (override != 0) {
2379 _PyErr_SetKeyError(key);
2380 Py_DECREF(value);
2381 Py_DECREF(key);
2382 return -1;
2383 }
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002384 Py_DECREF(value);
2385 Py_DECREF(key);
2386 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002387 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002388
Victor Stinner742da042016-09-07 17:40:12 -07002389 if (n != other->ma_keys->dk_nentries) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002390 PyErr_SetString(PyExc_RuntimeError,
2391 "dict mutated during update");
2392 return -1;
2393 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 }
2395 }
2396 }
2397 else {
2398 /* Do it the generic, slower way */
2399 PyObject *keys = PyMapping_Keys(b);
2400 PyObject *iter;
2401 PyObject *key, *value;
2402 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002404 if (keys == NULL)
2405 /* Docstring says this is equivalent to E.keys() so
2406 * if E doesn't have a .keys() method we want
2407 * AttributeError to percolate up. Might as well
2408 * do the same for any other error.
2409 */
2410 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002412 iter = PyObject_GetIter(keys);
2413 Py_DECREF(keys);
2414 if (iter == NULL)
2415 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002417 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002418 if (override != 1 && PyDict_GetItem(a, key) != NULL) {
2419 if (override != 0) {
2420 _PyErr_SetKeyError(key);
2421 Py_DECREF(key);
2422 Py_DECREF(iter);
2423 return -1;
2424 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002425 Py_DECREF(key);
2426 continue;
2427 }
2428 value = PyObject_GetItem(b, key);
2429 if (value == NULL) {
2430 Py_DECREF(iter);
2431 Py_DECREF(key);
2432 return -1;
2433 }
2434 status = PyDict_SetItem(a, key, value);
2435 Py_DECREF(key);
2436 Py_DECREF(value);
2437 if (status < 0) {
2438 Py_DECREF(iter);
2439 return -1;
2440 }
2441 }
2442 Py_DECREF(iter);
2443 if (PyErr_Occurred())
2444 /* Iterator completed, via error */
2445 return -1;
2446 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02002447 assert(_PyDict_CheckConsistency((PyDictObject *)a));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002448 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002449}
2450
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002451int
2452PyDict_Update(PyObject *a, PyObject *b)
2453{
2454 return dict_merge(a, b, 1);
2455}
2456
2457int
2458PyDict_Merge(PyObject *a, PyObject *b, int override)
2459{
2460 /* XXX Deprecate override not in (0, 1). */
2461 return dict_merge(a, b, override != 0);
2462}
2463
2464int
2465_PyDict_MergeEx(PyObject *a, PyObject *b, int override)
2466{
2467 return dict_merge(a, b, override);
2468}
2469
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002470static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002471dict_copy(PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002472{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002473 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002474}
2475
2476PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002477PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002478{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002479 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002480 PyDictObject *mp;
2481 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002483 if (o == NULL || !PyDict_Check(o)) {
2484 PyErr_BadInternalCall();
2485 return NULL;
2486 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002487 mp = (PyDictObject *)o;
2488 if (_PyDict_HasSplitTable(mp)) {
2489 PyDictObject *split_copy;
Victor Stinner742da042016-09-07 17:40:12 -07002490 Py_ssize_t size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
2491 PyObject **newvalues;
2492 newvalues = new_values(size);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002493 if (newvalues == NULL)
2494 return PyErr_NoMemory();
2495 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2496 if (split_copy == NULL) {
2497 free_values(newvalues);
2498 return NULL;
2499 }
2500 split_copy->ma_values = newvalues;
2501 split_copy->ma_keys = mp->ma_keys;
2502 split_copy->ma_used = mp->ma_used;
2503 DK_INCREF(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002504 for (i = 0, n = size; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002505 PyObject *value = mp->ma_values[i];
2506 Py_XINCREF(value);
2507 split_copy->ma_values[i] = value;
2508 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002509 if (_PyObject_GC_IS_TRACKED(mp))
2510 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002511 return (PyObject *)split_copy;
2512 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002513 copy = PyDict_New();
2514 if (copy == NULL)
2515 return NULL;
2516 if (PyDict_Merge(copy, o, 1) == 0)
2517 return copy;
2518 Py_DECREF(copy);
2519 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002520}
2521
Martin v. Löwis18e16552006-02-15 17:27:45 +00002522Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002523PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002524{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002525 if (mp == NULL || !PyDict_Check(mp)) {
2526 PyErr_BadInternalCall();
2527 return -1;
2528 }
2529 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002530}
2531
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002532PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002533PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002534{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002535 if (mp == NULL || !PyDict_Check(mp)) {
2536 PyErr_BadInternalCall();
2537 return NULL;
2538 }
2539 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002540}
2541
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002542PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002543PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002544{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002545 if (mp == NULL || !PyDict_Check(mp)) {
2546 PyErr_BadInternalCall();
2547 return NULL;
2548 }
2549 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002550}
2551
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002552PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002553PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002554{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002555 if (mp == NULL || !PyDict_Check(mp)) {
2556 PyErr_BadInternalCall();
2557 return NULL;
2558 }
2559 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002560}
2561
Tim Peterse63415e2001-05-08 04:38:29 +00002562/* Return 1 if dicts equal, 0 if not, -1 if error.
2563 * Gets out as soon as any difference is detected.
2564 * Uses only Py_EQ comparison.
2565 */
2566static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002567dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002568{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002569 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002571 if (a->ma_used != b->ma_used)
2572 /* can't be equal if # of entries differ */
2573 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002574 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Victor Stinner742da042016-09-07 17:40:12 -07002575 for (i = 0; i < a->ma_keys->dk_nentries; i++) {
2576 PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002577 PyObject *aval;
2578 if (a->ma_values)
2579 aval = a->ma_values[i];
2580 else
2581 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002582 if (aval != NULL) {
2583 int cmp;
2584 PyObject *bval;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002585 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002586 /* temporarily bump aval's refcount to ensure it stays
2587 alive until we're done with it */
2588 Py_INCREF(aval);
2589 /* ditto for key */
2590 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002591 /* reuse the known hash value */
INADA Naoki778928b2017-08-03 23:45:15 +09002592 b->ma_keys->dk_lookup(b, key, ep->me_hash, &bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002593 if (bval == NULL) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002594 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002595 Py_DECREF(aval);
2596 if (PyErr_Occurred())
2597 return -1;
2598 return 0;
2599 }
2600 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002601 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002602 Py_DECREF(aval);
2603 if (cmp <= 0) /* error or not equal */
2604 return cmp;
2605 }
2606 }
2607 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002608}
Tim Peterse63415e2001-05-08 04:38:29 +00002609
2610static PyObject *
2611dict_richcompare(PyObject *v, PyObject *w, int op)
2612{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002613 int cmp;
2614 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002616 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2617 res = Py_NotImplemented;
2618 }
2619 else if (op == Py_EQ || op == Py_NE) {
2620 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2621 if (cmp < 0)
2622 return NULL;
2623 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2624 }
2625 else
2626 res = Py_NotImplemented;
2627 Py_INCREF(res);
2628 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002629}
Tim Peterse63415e2001-05-08 04:38:29 +00002630
Larry Hastings61272b72014-01-07 12:41:53 -08002631/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002632
2633@coexist
2634dict.__contains__
2635
2636 key: object
2637 /
2638
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002639True if the dictionary has the specified key, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002640[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002641
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002642static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002643dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka19d25972017-02-04 08:05:07 +02002644/*[clinic end generated code: output=a3d03db709ed6e6b input=fe1cb42ad831e820]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002645{
Larry Hastingsc2047262014-01-25 20:43:29 -08002646 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002647 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002648 Py_ssize_t ix;
INADA Naokiba609772016-12-07 20:41:42 +09002649 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002651 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002652 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002653 hash = PyObject_Hash(key);
2654 if (hash == -1)
2655 return NULL;
2656 }
INADA Naoki778928b2017-08-03 23:45:15 +09002657 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002658 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002659 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002660 if (ix == DKIX_EMPTY || value == NULL)
Victor Stinner742da042016-09-07 17:40:12 -07002661 Py_RETURN_FALSE;
2662 Py_RETURN_TRUE;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002663}
2664
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002665/*[clinic input]
2666dict.get
2667
2668 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002669 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002670 /
2671
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002672Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002673[clinic start generated code]*/
2674
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002675static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002676dict_get_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002677/*[clinic end generated code: output=bba707729dee05bf input=279ddb5790b6b107]*/
Barry Warsawc38c5da1997-10-06 17:49:20 +00002678{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002679 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002680 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002681 Py_ssize_t ix;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002683 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002684 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002685 hash = PyObject_Hash(key);
2686 if (hash == -1)
2687 return NULL;
2688 }
INADA Naoki778928b2017-08-03 23:45:15 +09002689 ix = (self->ma_keys->dk_lookup) (self, key, hash, &val);
Victor Stinner742da042016-09-07 17:40:12 -07002690 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002691 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002692 if (ix == DKIX_EMPTY || val == NULL) {
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002693 val = default_value;
INADA Naokiba609772016-12-07 20:41:42 +09002694 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002695 Py_INCREF(val);
2696 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002697}
2698
Benjamin Peterson00e98862013-03-07 22:16:29 -05002699PyObject *
2700PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002701{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002702 PyDictObject *mp = (PyDictObject *)d;
INADA Naoki93f26f72016-11-02 18:45:16 +09002703 PyObject *value;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002704 Py_hash_t hash;
Guido van Rossum164452c2000-08-08 16:12:54 +00002705
Benjamin Peterson00e98862013-03-07 22:16:29 -05002706 if (!PyDict_Check(d)) {
2707 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002708 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002709 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002711 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002712 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002713 hash = PyObject_Hash(key);
2714 if (hash == -1)
2715 return NULL;
2716 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002717
2718 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
2719 if (insertion_resize(mp) < 0)
2720 return NULL;
2721 }
2722
INADA Naoki778928b2017-08-03 23:45:15 +09002723 Py_ssize_t ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002724 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002725 return NULL;
INADA Naoki93f26f72016-11-02 18:45:16 +09002726
2727 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09002728 ((ix >= 0 && value == NULL && mp->ma_used != ix) ||
INADA Naoki93f26f72016-11-02 18:45:16 +09002729 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
2730 if (insertion_resize(mp) < 0) {
2731 return NULL;
2732 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002733 ix = DKIX_EMPTY;
2734 }
2735
2736 if (ix == DKIX_EMPTY) {
2737 PyDictKeyEntry *ep, *ep0;
2738 value = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002739 if (mp->ma_keys->dk_usable <= 0) {
Victor Stinner3c336c52016-09-12 14:17:40 +02002740 if (insertion_resize(mp) < 0) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002741 return NULL;
Victor Stinner3c336c52016-09-12 14:17:40 +02002742 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002743 }
INADA Naoki778928b2017-08-03 23:45:15 +09002744 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naoki93f26f72016-11-02 18:45:16 +09002745 ep0 = DK_ENTRIES(mp->ma_keys);
2746 ep = &ep0[mp->ma_keys->dk_nentries];
2747 dk_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002748 Py_INCREF(key);
INADA Naoki93f26f72016-11-02 18:45:16 +09002749 Py_INCREF(value);
2750 MAINTAIN_TRACKING(mp, key, value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002751 ep->me_key = key;
2752 ep->me_hash = hash;
INADA Naokiba609772016-12-07 20:41:42 +09002753 if (_PyDict_HasSplitTable(mp)) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002754 assert(mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
2755 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Victor Stinner742da042016-09-07 17:40:12 -07002756 }
2757 else {
INADA Naoki93f26f72016-11-02 18:45:16 +09002758 ep->me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07002759 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002760 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002761 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naoki93f26f72016-11-02 18:45:16 +09002762 mp->ma_keys->dk_usable--;
2763 mp->ma_keys->dk_nentries++;
2764 assert(mp->ma_keys->dk_usable >= 0);
2765 }
INADA Naokiba609772016-12-07 20:41:42 +09002766 else if (value == NULL) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002767 value = defaultobj;
2768 assert(_PyDict_HasSplitTable(mp));
2769 assert(ix == mp->ma_used);
2770 Py_INCREF(value);
2771 MAINTAIN_TRACKING(mp, key, value);
INADA Naokiba609772016-12-07 20:41:42 +09002772 mp->ma_values[ix] = value;
INADA Naoki93f26f72016-11-02 18:45:16 +09002773 mp->ma_used++;
2774 mp->ma_version_tag = DICT_NEXT_VERSION();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002775 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002776
2777 assert(_PyDict_CheckConsistency(mp));
2778 return value;
Guido van Rossum164452c2000-08-08 16:12:54 +00002779}
2780
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002781/*[clinic input]
2782dict.setdefault
2783
2784 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002785 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002786 /
2787
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002788Insert key with a value of default if key is not in the dictionary.
2789
2790Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002791[clinic start generated code]*/
2792
Benjamin Peterson00e98862013-03-07 22:16:29 -05002793static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002794dict_setdefault_impl(PyDictObject *self, PyObject *key,
2795 PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002796/*[clinic end generated code: output=f8c1101ebf69e220 input=0f063756e815fd9d]*/
Benjamin Peterson00e98862013-03-07 22:16:29 -05002797{
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002798 PyObject *val;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002799
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002800 val = PyDict_SetDefault((PyObject *)self, key, default_value);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002801 Py_XINCREF(val);
2802 return val;
2803}
Guido van Rossum164452c2000-08-08 16:12:54 +00002804
2805static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002806dict_clear(PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002807{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002808 PyDict_Clear((PyObject *)mp);
2809 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002810}
2811
Guido van Rossumba6ab842000-12-12 22:02:18 +00002812static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002813dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002814{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002815 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00002816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002817 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2818 return NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06002819
Yury Selivanov684ef2c2016-10-28 19:01:21 -04002820 return _PyDict_Pop((PyObject*)mp, key, deflt);
Guido van Rossume027d982002-04-12 15:11:59 +00002821}
2822
2823static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002824dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002825{
Victor Stinner742da042016-09-07 17:40:12 -07002826 Py_ssize_t i, j;
2827 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002828 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002830 /* Allocate the result tuple before checking the size. Believe it
2831 * or not, this allocation could trigger a garbage collection which
2832 * could empty the dict, so if we checked the size first and that
2833 * happened, the result would be an infinite loop (searching for an
2834 * entry that no longer exists). Note that the usual popitem()
2835 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2836 * tuple away if the dict *is* empty isn't a significant
2837 * inefficiency -- possible, but unlikely in practice.
2838 */
2839 res = PyTuple_New(2);
2840 if (res == NULL)
2841 return NULL;
2842 if (mp->ma_used == 0) {
2843 Py_DECREF(res);
2844 PyErr_SetString(PyExc_KeyError,
2845 "popitem(): dictionary is empty");
2846 return NULL;
2847 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002848 /* Convert split table to combined table */
2849 if (mp->ma_keys->dk_lookup == lookdict_split) {
2850 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2851 Py_DECREF(res);
2852 return NULL;
2853 }
2854 }
2855 ENSURE_ALLOWS_DELETIONS(mp);
Victor Stinner742da042016-09-07 17:40:12 -07002856
2857 /* Pop last item */
2858 ep0 = DK_ENTRIES(mp->ma_keys);
2859 i = mp->ma_keys->dk_nentries - 1;
2860 while (i >= 0 && ep0[i].me_value == NULL) {
2861 i--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002862 }
Victor Stinner742da042016-09-07 17:40:12 -07002863 assert(i >= 0);
2864
2865 ep = &ep0[i];
2866 j = lookdict_index(mp->ma_keys, ep->me_hash, i);
2867 assert(j >= 0);
2868 assert(dk_get_index(mp->ma_keys, j) == i);
2869 dk_set_index(mp->ma_keys, j, DKIX_DUMMY);
2870
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002871 PyTuple_SET_ITEM(res, 0, ep->me_key);
2872 PyTuple_SET_ITEM(res, 1, ep->me_value);
Victor Stinner742da042016-09-07 17:40:12 -07002873 ep->me_key = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 ep->me_value = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002875 /* We can't dk_usable++ since there is DKIX_DUMMY in indices */
2876 mp->ma_keys->dk_nentries = i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002877 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002878 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02002879 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002881}
2882
Jeremy Hylton8caad492000-06-23 14:18:11 +00002883static int
2884dict_traverse(PyObject *op, visitproc visit, void *arg)
2885{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002886 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson55f44522016-09-05 12:12:59 -07002887 PyDictKeysObject *keys = mp->ma_keys;
Serhiy Storchaka46825d22016-09-26 21:29:34 +03002888 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Victor Stinner742da042016-09-07 17:40:12 -07002889 Py_ssize_t i, n = keys->dk_nentries;
2890
Benjamin Peterson55f44522016-09-05 12:12:59 -07002891 if (keys->dk_lookup == lookdict) {
2892 for (i = 0; i < n; i++) {
2893 if (entries[i].me_value != NULL) {
2894 Py_VISIT(entries[i].me_value);
2895 Py_VISIT(entries[i].me_key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002896 }
2897 }
Victor Stinner742da042016-09-07 17:40:12 -07002898 }
2899 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002900 if (mp->ma_values != NULL) {
Benjamin Peterson55f44522016-09-05 12:12:59 -07002901 for (i = 0; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002902 Py_VISIT(mp->ma_values[i]);
2903 }
2904 }
2905 else {
Benjamin Peterson55f44522016-09-05 12:12:59 -07002906 for (i = 0; i < n; i++) {
2907 Py_VISIT(entries[i].me_value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002908 }
2909 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002910 }
2911 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002912}
2913
2914static int
2915dict_tp_clear(PyObject *op)
2916{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 PyDict_Clear(op);
2918 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002919}
2920
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002921static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002922
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002923Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06002924_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002925{
Victor Stinner742da042016-09-07 17:40:12 -07002926 Py_ssize_t size, usable, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002927
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002928 size = DK_SIZE(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002929 usable = USABLE_FRACTION(size);
2930
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002931 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002932 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -07002933 res += usable * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002934 /* If the dictionary is split, the keys portion is accounted-for
2935 in the type object. */
2936 if (mp->ma_keys->dk_refcnt == 1)
Victor Stinner98ee9d52016-09-08 09:33:56 -07002937 res += (sizeof(PyDictKeysObject)
2938 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
2939 + DK_IXSIZE(mp->ma_keys) * size
2940 + sizeof(PyDictKeyEntry) * usable);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002941 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002942}
2943
2944Py_ssize_t
2945_PyDict_KeysSize(PyDictKeysObject *keys)
2946{
Victor Stinner98ee9d52016-09-08 09:33:56 -07002947 return (sizeof(PyDictKeysObject)
2948 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
2949 + DK_IXSIZE(keys) * DK_SIZE(keys)
2950 + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002951}
2952
doko@ubuntu.com17210f52016-01-14 14:04:59 +01002953static PyObject *
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002954dict_sizeof(PyDictObject *mp)
2955{
2956 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
2957}
2958
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002959PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
2960
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002961PyDoc_STRVAR(sizeof__doc__,
2962"D.__sizeof__() -> size of D in memory, in bytes");
2963
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002964PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002965"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002966If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00002967
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002968PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00002969"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000029702-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002971
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002972PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04002973"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
2974If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
2975If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
2976In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00002977
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002978PyDoc_STRVAR(clear__doc__,
2979"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002980
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002981PyDoc_STRVAR(copy__doc__,
2982"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00002983
Guido van Rossumb90c8482007-02-10 01:11:45 +00002984/* Forward */
2985static PyObject *dictkeys_new(PyObject *);
2986static PyObject *dictitems_new(PyObject *);
2987static PyObject *dictvalues_new(PyObject *);
2988
Guido van Rossum45c85d12007-07-27 16:31:40 +00002989PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002990 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002991PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002992 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002993PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002994 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00002995
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002996static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07002997 DICT___CONTAINS___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002998 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
2999 getitem__doc__},
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003000 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003001 sizeof__doc__},
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003002 DICT_GET_METHODDEF
3003 DICT_SETDEFAULT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003004 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
3005 pop__doc__},
3006 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
3007 popitem__doc__},
3008 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
3009 keys__doc__},
3010 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
3011 items__doc__},
3012 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
3013 values__doc__},
3014 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
3015 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08003016 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003017 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
3018 clear__doc__},
3019 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
3020 copy__doc__},
3021 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003022};
3023
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003024/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00003025int
3026PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003027{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003028 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07003029 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003030 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003031 PyObject *value;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003032
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003033 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003034 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003035 hash = PyObject_Hash(key);
3036 if (hash == -1)
3037 return -1;
3038 }
INADA Naoki778928b2017-08-03 23:45:15 +09003039 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003040 if (ix == DKIX_ERROR)
3041 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003042 return (ix != DKIX_EMPTY && value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003043}
3044
Thomas Wouterscf297e42007-02-23 15:07:44 +00003045/* Internal version of PyDict_Contains used when the hash value is already known */
3046int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003047_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00003048{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003049 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003050 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07003051 Py_ssize_t ix;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003052
INADA Naoki778928b2017-08-03 23:45:15 +09003053 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003054 if (ix == DKIX_ERROR)
3055 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003056 return (ix != DKIX_EMPTY && value != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00003057}
3058
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003059/* Hack to implement "key in dict" */
3060static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003061 0, /* sq_length */
3062 0, /* sq_concat */
3063 0, /* sq_repeat */
3064 0, /* sq_item */
3065 0, /* sq_slice */
3066 0, /* sq_ass_item */
3067 0, /* sq_ass_slice */
3068 PyDict_Contains, /* sq_contains */
3069 0, /* sq_inplace_concat */
3070 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003071};
3072
Guido van Rossum09e563a2001-05-01 12:10:21 +00003073static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003074dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3075{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003076 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003077 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003079 assert(type != NULL && type->tp_alloc != NULL);
3080 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02003081 if (self == NULL)
3082 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003083 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003084
Victor Stinnera9f61a52013-07-16 22:17:26 +02003085 /* The object has been implicitly tracked by tp_alloc */
3086 if (type == &PyDict_Type)
3087 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003088
3089 d->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003090 d->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07003091 d->ma_keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003092 if (d->ma_keys == NULL) {
3093 Py_DECREF(self);
3094 return NULL;
3095 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02003096 assert(_PyDict_CheckConsistency(d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003097 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003098}
3099
Tim Peters25786c02001-09-02 08:22:48 +00003100static int
3101dict_init(PyObject *self, PyObject *args, PyObject *kwds)
3102{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003103 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00003104}
3105
Tim Peters6d6c1a32001-08-02 04:15:00 +00003106static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003107dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00003108{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003109 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003110}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003111
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003112PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00003113"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00003114"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003115" (key, value) pairs\n"
3116"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00003117" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003118" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00003119" d[k] = v\n"
3120"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
3121" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00003122
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003123PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003124 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3125 "dict",
3126 sizeof(PyDictObject),
3127 0,
3128 (destructor)dict_dealloc, /* tp_dealloc */
3129 0, /* tp_print */
3130 0, /* tp_getattr */
3131 0, /* tp_setattr */
3132 0, /* tp_reserved */
3133 (reprfunc)dict_repr, /* tp_repr */
3134 0, /* tp_as_number */
3135 &dict_as_sequence, /* tp_as_sequence */
3136 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003137 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003138 0, /* tp_call */
3139 0, /* tp_str */
3140 PyObject_GenericGetAttr, /* tp_getattro */
3141 0, /* tp_setattro */
3142 0, /* tp_as_buffer */
3143 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3144 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
3145 dictionary_doc, /* tp_doc */
3146 dict_traverse, /* tp_traverse */
3147 dict_tp_clear, /* tp_clear */
3148 dict_richcompare, /* tp_richcompare */
3149 0, /* tp_weaklistoffset */
3150 (getiterfunc)dict_iter, /* tp_iter */
3151 0, /* tp_iternext */
3152 mapp_methods, /* tp_methods */
3153 0, /* tp_members */
3154 0, /* tp_getset */
3155 0, /* tp_base */
3156 0, /* tp_dict */
3157 0, /* tp_descr_get */
3158 0, /* tp_descr_set */
3159 0, /* tp_dictoffset */
3160 dict_init, /* tp_init */
3161 PyType_GenericAlloc, /* tp_alloc */
3162 dict_new, /* tp_new */
3163 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003164};
3165
Victor Stinner3c1e4812012-03-26 22:10:51 +02003166PyObject *
3167_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
3168{
3169 PyObject *kv;
3170 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02003171 if (kv == NULL) {
3172 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003173 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02003174 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02003175 return PyDict_GetItem(dp, kv);
3176}
3177
Guido van Rossum3cca2451997-05-16 14:23:33 +00003178/* For backward compatibility with old dictionary interface */
3179
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003180PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003181PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003182{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003183 PyObject *kv, *rv;
3184 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003185 if (kv == NULL) {
3186 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003187 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003188 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003189 rv = PyDict_GetItem(v, kv);
3190 Py_DECREF(kv);
3191 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003192}
3193
3194int
Victor Stinner3c1e4812012-03-26 22:10:51 +02003195_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
3196{
3197 PyObject *kv;
3198 kv = _PyUnicode_FromId(key); /* borrowed */
3199 if (kv == NULL)
3200 return -1;
3201 return PyDict_SetItem(v, kv, item);
3202}
3203
3204int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003205PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003206{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003207 PyObject *kv;
3208 int err;
3209 kv = PyUnicode_FromString(key);
3210 if (kv == NULL)
3211 return -1;
3212 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
3213 err = PyDict_SetItem(v, kv, item);
3214 Py_DECREF(kv);
3215 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003216}
3217
3218int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01003219_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
3220{
3221 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3222 if (kv == NULL)
3223 return -1;
3224 return PyDict_DelItem(v, kv);
3225}
3226
3227int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003228PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003229{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003230 PyObject *kv;
3231 int err;
3232 kv = PyUnicode_FromString(key);
3233 if (kv == NULL)
3234 return -1;
3235 err = PyDict_DelItem(v, kv);
3236 Py_DECREF(kv);
3237 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003238}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003239
Raymond Hettinger019a1482004-03-18 02:41:19 +00003240/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003241
3242typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003243 PyObject_HEAD
3244 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
3245 Py_ssize_t di_used;
3246 Py_ssize_t di_pos;
3247 PyObject* di_result; /* reusable result tuple for iteritems */
3248 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003249} dictiterobject;
3250
3251static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003252dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003253{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003254 dictiterobject *di;
3255 di = PyObject_GC_New(dictiterobject, itertype);
3256 if (di == NULL)
3257 return NULL;
3258 Py_INCREF(dict);
3259 di->di_dict = dict;
3260 di->di_used = dict->ma_used;
3261 di->di_pos = 0;
3262 di->len = dict->ma_used;
3263 if (itertype == &PyDictIterItem_Type) {
3264 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
3265 if (di->di_result == NULL) {
3266 Py_DECREF(di);
3267 return NULL;
3268 }
3269 }
3270 else
3271 di->di_result = NULL;
3272 _PyObject_GC_TRACK(di);
3273 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003274}
3275
3276static void
3277dictiter_dealloc(dictiterobject *di)
3278{
INADA Naokia6296d32017-08-24 14:55:17 +09003279 /* bpo-31095: UnTrack is needed before calling any callbacks */
3280 _PyObject_GC_UNTRACK(di);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003281 Py_XDECREF(di->di_dict);
3282 Py_XDECREF(di->di_result);
3283 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003284}
3285
3286static int
3287dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
3288{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003289 Py_VISIT(di->di_dict);
3290 Py_VISIT(di->di_result);
3291 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003292}
3293
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003294static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003295dictiter_len(dictiterobject *di)
3296{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003297 Py_ssize_t len = 0;
3298 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
3299 len = di->len;
3300 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003301}
3302
Guido van Rossumb90c8482007-02-10 01:11:45 +00003303PyDoc_STRVAR(length_hint_doc,
3304 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003305
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003306static PyObject *
3307dictiter_reduce(dictiterobject *di);
3308
3309PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3310
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003311static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003312 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
3313 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003314 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
3315 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003316 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003317};
3318
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003319static PyObject*
3320dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00003321{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003322 PyObject *key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003323 Py_ssize_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003324 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003325 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00003326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003327 if (d == NULL)
3328 return NULL;
3329 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00003330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003331 if (di->di_used != d->ma_used) {
3332 PyErr_SetString(PyExc_RuntimeError,
3333 "dictionary changed size during iteration");
3334 di->di_used = -1; /* Make this state sticky */
3335 return NULL;
3336 }
Guido van Rossum2147df72002-07-16 20:30:22 +00003337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003338 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003339 k = d->ma_keys;
INADA Naokica2d8be2016-11-04 16:59:10 +09003340 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003341 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003342 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003343 goto fail;
3344 key = DK_ENTRIES(k)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003345 assert(d->ma_values[i] != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003346 }
3347 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003348 Py_ssize_t n = k->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003349 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3350 while (i < n && entry_ptr->me_value == NULL) {
3351 entry_ptr++;
3352 i++;
3353 }
3354 if (i >= n)
3355 goto fail;
3356 key = entry_ptr->me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003357 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003358 di->di_pos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003359 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003360 Py_INCREF(key);
3361 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003362
3363fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003364 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003365 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003366 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003367}
3368
Raymond Hettinger019a1482004-03-18 02:41:19 +00003369PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003370 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3371 "dict_keyiterator", /* tp_name */
3372 sizeof(dictiterobject), /* tp_basicsize */
3373 0, /* tp_itemsize */
3374 /* methods */
3375 (destructor)dictiter_dealloc, /* tp_dealloc */
3376 0, /* tp_print */
3377 0, /* tp_getattr */
3378 0, /* tp_setattr */
3379 0, /* tp_reserved */
3380 0, /* tp_repr */
3381 0, /* tp_as_number */
3382 0, /* tp_as_sequence */
3383 0, /* tp_as_mapping */
3384 0, /* tp_hash */
3385 0, /* tp_call */
3386 0, /* tp_str */
3387 PyObject_GenericGetAttr, /* tp_getattro */
3388 0, /* tp_setattro */
3389 0, /* tp_as_buffer */
3390 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3391 0, /* tp_doc */
3392 (traverseproc)dictiter_traverse, /* tp_traverse */
3393 0, /* tp_clear */
3394 0, /* tp_richcompare */
3395 0, /* tp_weaklistoffset */
3396 PyObject_SelfIter, /* tp_iter */
3397 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3398 dictiter_methods, /* tp_methods */
3399 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003400};
3401
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003402static PyObject *
3403dictiter_iternextvalue(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003404{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003405 PyObject *value;
INADA Naokica2d8be2016-11-04 16:59:10 +09003406 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003407 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003409 if (d == NULL)
3410 return NULL;
3411 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003413 if (di->di_used != d->ma_used) {
3414 PyErr_SetString(PyExc_RuntimeError,
3415 "dictionary changed size during iteration");
3416 di->di_used = -1; /* Make this state sticky */
3417 return NULL;
3418 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003420 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003421 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003422 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003423 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003424 goto fail;
INADA Naokica2d8be2016-11-04 16:59:10 +09003425 value = d->ma_values[i];
3426 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003427 }
3428 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003429 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003430 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3431 while (i < n && entry_ptr->me_value == NULL) {
3432 entry_ptr++;
3433 i++;
3434 }
3435 if (i >= n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003436 goto fail;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003437 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003438 }
3439 di->di_pos = i+1;
3440 di->len--;
3441 Py_INCREF(value);
3442 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003443
3444fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003445 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003446 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003447 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003448}
3449
3450PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003451 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3452 "dict_valueiterator", /* tp_name */
3453 sizeof(dictiterobject), /* tp_basicsize */
3454 0, /* tp_itemsize */
3455 /* methods */
3456 (destructor)dictiter_dealloc, /* tp_dealloc */
3457 0, /* tp_print */
3458 0, /* tp_getattr */
3459 0, /* tp_setattr */
3460 0, /* tp_reserved */
3461 0, /* tp_repr */
3462 0, /* tp_as_number */
3463 0, /* tp_as_sequence */
3464 0, /* tp_as_mapping */
3465 0, /* tp_hash */
3466 0, /* tp_call */
3467 0, /* tp_str */
3468 PyObject_GenericGetAttr, /* tp_getattro */
3469 0, /* tp_setattro */
3470 0, /* tp_as_buffer */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003471 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003472 0, /* tp_doc */
3473 (traverseproc)dictiter_traverse, /* tp_traverse */
3474 0, /* tp_clear */
3475 0, /* tp_richcompare */
3476 0, /* tp_weaklistoffset */
3477 PyObject_SelfIter, /* tp_iter */
3478 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3479 dictiter_methods, /* tp_methods */
3480 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003481};
3482
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003483static PyObject *
3484dictiter_iternextitem(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003485{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003486 PyObject *key, *value, *result;
INADA Naokica2d8be2016-11-04 16:59:10 +09003487 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003488 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003490 if (d == NULL)
3491 return NULL;
3492 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003494 if (di->di_used != d->ma_used) {
3495 PyErr_SetString(PyExc_RuntimeError,
3496 "dictionary changed size during iteration");
3497 di->di_used = -1; /* Make this state sticky */
3498 return NULL;
3499 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003501 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003502 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003503 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003504 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003505 goto fail;
3506 key = DK_ENTRIES(d->ma_keys)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003507 value = d->ma_values[i];
3508 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003509 }
3510 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003511 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003512 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3513 while (i < n && entry_ptr->me_value == NULL) {
3514 entry_ptr++;
3515 i++;
3516 }
3517 if (i >= n)
3518 goto fail;
3519 key = entry_ptr->me_key;
3520 value = entry_ptr->me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003521 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003522 di->di_pos = i+1;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003523 di->len--;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003524 Py_INCREF(key);
3525 Py_INCREF(value);
3526 result = di->di_result;
3527 if (Py_REFCNT(result) == 1) {
3528 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3529 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3530 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3531 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003532 Py_INCREF(result);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003533 Py_DECREF(oldkey);
3534 Py_DECREF(oldvalue);
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003535 }
3536 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003537 result = PyTuple_New(2);
3538 if (result == NULL)
3539 return NULL;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003540 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3541 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003542 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003543 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003544
3545fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003546 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003547 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003548 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003549}
3550
3551PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003552 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3553 "dict_itemiterator", /* tp_name */
3554 sizeof(dictiterobject), /* tp_basicsize */
3555 0, /* tp_itemsize */
3556 /* methods */
3557 (destructor)dictiter_dealloc, /* tp_dealloc */
3558 0, /* tp_print */
3559 0, /* tp_getattr */
3560 0, /* tp_setattr */
3561 0, /* tp_reserved */
3562 0, /* tp_repr */
3563 0, /* tp_as_number */
3564 0, /* tp_as_sequence */
3565 0, /* tp_as_mapping */
3566 0, /* tp_hash */
3567 0, /* tp_call */
3568 0, /* tp_str */
3569 PyObject_GenericGetAttr, /* tp_getattro */
3570 0, /* tp_setattro */
3571 0, /* tp_as_buffer */
3572 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3573 0, /* tp_doc */
3574 (traverseproc)dictiter_traverse, /* tp_traverse */
3575 0, /* tp_clear */
3576 0, /* tp_richcompare */
3577 0, /* tp_weaklistoffset */
3578 PyObject_SelfIter, /* tp_iter */
3579 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3580 dictiter_methods, /* tp_methods */
3581 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003582};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003583
3584
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003585static PyObject *
3586dictiter_reduce(dictiterobject *di)
3587{
3588 PyObject *list;
3589 dictiterobject tmp;
3590
3591 list = PyList_New(0);
3592 if (!list)
3593 return NULL;
3594
3595 /* copy the itertor state */
3596 tmp = *di;
3597 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003598
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003599 /* iterate the temporary into a list */
3600 for(;;) {
3601 PyObject *element = 0;
3602 if (Py_TYPE(di) == &PyDictIterItem_Type)
3603 element = dictiter_iternextitem(&tmp);
3604 else if (Py_TYPE(di) == &PyDictIterKey_Type)
3605 element = dictiter_iternextkey(&tmp);
3606 else if (Py_TYPE(di) == &PyDictIterValue_Type)
3607 element = dictiter_iternextvalue(&tmp);
3608 else
Barry Warsawb2e57942017-09-14 18:13:16 -07003609 Py_UNREACHABLE();
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003610 if (element) {
3611 if (PyList_Append(list, element)) {
3612 Py_DECREF(element);
3613 Py_DECREF(list);
3614 Py_XDECREF(tmp.di_dict);
3615 return NULL;
3616 }
3617 Py_DECREF(element);
3618 } else
3619 break;
3620 }
3621 Py_XDECREF(tmp.di_dict);
3622 /* check for error */
3623 if (tmp.di_dict != NULL) {
3624 /* we have an error */
3625 Py_DECREF(list);
3626 return NULL;
3627 }
Antoine Pitroua7013882012-04-05 00:04:20 +02003628 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003629}
3630
Guido van Rossum3ac67412007-02-10 18:55:06 +00003631/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003632/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003633/***********************************************/
3634
Guido van Rossumb90c8482007-02-10 01:11:45 +00003635/* The instance lay-out is the same for all three; but the type differs. */
3636
Guido van Rossumb90c8482007-02-10 01:11:45 +00003637static void
Eric Snow96c6af92015-05-29 22:21:39 -06003638dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003639{
INADA Naokia6296d32017-08-24 14:55:17 +09003640 /* bpo-31095: UnTrack is needed before calling any callbacks */
3641 _PyObject_GC_UNTRACK(dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003642 Py_XDECREF(dv->dv_dict);
3643 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003644}
3645
3646static int
Eric Snow96c6af92015-05-29 22:21:39 -06003647dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003648{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003649 Py_VISIT(dv->dv_dict);
3650 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003651}
3652
Guido van Rossum83825ac2007-02-10 04:54:19 +00003653static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003654dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003655{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003656 Py_ssize_t len = 0;
3657 if (dv->dv_dict != NULL)
3658 len = dv->dv_dict->ma_used;
3659 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003660}
3661
Eric Snow96c6af92015-05-29 22:21:39 -06003662PyObject *
3663_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003664{
Eric Snow96c6af92015-05-29 22:21:39 -06003665 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003666 if (dict == NULL) {
3667 PyErr_BadInternalCall();
3668 return NULL;
3669 }
3670 if (!PyDict_Check(dict)) {
3671 /* XXX Get rid of this restriction later */
3672 PyErr_Format(PyExc_TypeError,
3673 "%s() requires a dict argument, not '%s'",
3674 type->tp_name, dict->ob_type->tp_name);
3675 return NULL;
3676 }
Eric Snow96c6af92015-05-29 22:21:39 -06003677 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003678 if (dv == NULL)
3679 return NULL;
3680 Py_INCREF(dict);
3681 dv->dv_dict = (PyDictObject *)dict;
3682 _PyObject_GC_TRACK(dv);
3683 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003684}
3685
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003686/* TODO(guido): The views objects are not complete:
3687
3688 * support more set operations
3689 * support arbitrary mappings?
3690 - either these should be static or exported in dictobject.h
3691 - if public then they should probably be in builtins
3692*/
3693
Guido van Rossumaac530c2007-08-24 22:33:45 +00003694/* Return 1 if self is a subset of other, iterating over self;
3695 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003696static int
3697all_contained_in(PyObject *self, PyObject *other)
3698{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003699 PyObject *iter = PyObject_GetIter(self);
3700 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003702 if (iter == NULL)
3703 return -1;
3704 for (;;) {
3705 PyObject *next = PyIter_Next(iter);
3706 if (next == NULL) {
3707 if (PyErr_Occurred())
3708 ok = -1;
3709 break;
3710 }
3711 ok = PySequence_Contains(other, next);
3712 Py_DECREF(next);
3713 if (ok <= 0)
3714 break;
3715 }
3716 Py_DECREF(iter);
3717 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003718}
3719
3720static PyObject *
3721dictview_richcompare(PyObject *self, PyObject *other, int op)
3722{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003723 Py_ssize_t len_self, len_other;
3724 int ok;
3725 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003727 assert(self != NULL);
3728 assert(PyDictViewSet_Check(self));
3729 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003730
Brian Curtindfc80e32011-08-10 20:28:54 -05003731 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3732 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003734 len_self = PyObject_Size(self);
3735 if (len_self < 0)
3736 return NULL;
3737 len_other = PyObject_Size(other);
3738 if (len_other < 0)
3739 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003741 ok = 0;
3742 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003744 case Py_NE:
3745 case Py_EQ:
3746 if (len_self == len_other)
3747 ok = all_contained_in(self, other);
3748 if (op == Py_NE && ok >= 0)
3749 ok = !ok;
3750 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003752 case Py_LT:
3753 if (len_self < len_other)
3754 ok = all_contained_in(self, other);
3755 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003757 case Py_LE:
3758 if (len_self <= len_other)
3759 ok = all_contained_in(self, other);
3760 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003762 case Py_GT:
3763 if (len_self > len_other)
3764 ok = all_contained_in(other, self);
3765 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003767 case Py_GE:
3768 if (len_self >= len_other)
3769 ok = all_contained_in(other, self);
3770 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003772 }
3773 if (ok < 0)
3774 return NULL;
3775 result = ok ? Py_True : Py_False;
3776 Py_INCREF(result);
3777 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003778}
3779
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003780static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003781dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003782{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003783 PyObject *seq;
3784 PyObject *result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003786 seq = PySequence_List((PyObject *)dv);
3787 if (seq == NULL)
3788 return NULL;
3789
3790 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
3791 Py_DECREF(seq);
3792 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003793}
3794
Guido van Rossum3ac67412007-02-10 18:55:06 +00003795/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003796
3797static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003798dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003799{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003800 if (dv->dv_dict == NULL) {
3801 Py_RETURN_NONE;
3802 }
3803 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003804}
3805
3806static int
Eric Snow96c6af92015-05-29 22:21:39 -06003807dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00003808{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003809 if (dv->dv_dict == NULL)
3810 return 0;
3811 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003812}
3813
Guido van Rossum83825ac2007-02-10 04:54:19 +00003814static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003815 (lenfunc)dictview_len, /* sq_length */
3816 0, /* sq_concat */
3817 0, /* sq_repeat */
3818 0, /* sq_item */
3819 0, /* sq_slice */
3820 0, /* sq_ass_item */
3821 0, /* sq_ass_slice */
3822 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003823};
3824
Guido van Rossum523259b2007-08-24 23:41:22 +00003825static PyObject*
3826dictviews_sub(PyObject* self, PyObject *other)
3827{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003828 PyObject *result = PySet_New(self);
3829 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003830 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003832 if (result == NULL)
3833 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003834
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003835 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003836 if (tmp == NULL) {
3837 Py_DECREF(result);
3838 return NULL;
3839 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003840
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003841 Py_DECREF(tmp);
3842 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003843}
3844
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003845PyObject*
3846_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00003847{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003848 PyObject *result = PySet_New(self);
3849 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003850 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003852 if (result == NULL)
3853 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003854
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003855 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_intersection_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003856 if (tmp == NULL) {
3857 Py_DECREF(result);
3858 return NULL;
3859 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003861 Py_DECREF(tmp);
3862 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003863}
3864
3865static PyObject*
3866dictviews_or(PyObject* self, PyObject *other)
3867{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003868 PyObject *result = PySet_New(self);
3869 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003870 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003872 if (result == NULL)
3873 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003874
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003875 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003876 if (tmp == NULL) {
3877 Py_DECREF(result);
3878 return NULL;
3879 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003881 Py_DECREF(tmp);
3882 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003883}
3884
3885static PyObject*
3886dictviews_xor(PyObject* self, PyObject *other)
3887{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003888 PyObject *result = PySet_New(self);
3889 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003890 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003892 if (result == NULL)
3893 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003894
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003895 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_symmetric_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003896 if (tmp == NULL) {
3897 Py_DECREF(result);
3898 return NULL;
3899 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003901 Py_DECREF(tmp);
3902 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003903}
3904
3905static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003906 0, /*nb_add*/
3907 (binaryfunc)dictviews_sub, /*nb_subtract*/
3908 0, /*nb_multiply*/
3909 0, /*nb_remainder*/
3910 0, /*nb_divmod*/
3911 0, /*nb_power*/
3912 0, /*nb_negative*/
3913 0, /*nb_positive*/
3914 0, /*nb_absolute*/
3915 0, /*nb_bool*/
3916 0, /*nb_invert*/
3917 0, /*nb_lshift*/
3918 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003919 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003920 (binaryfunc)dictviews_xor, /*nb_xor*/
3921 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00003922};
3923
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003924static PyObject*
3925dictviews_isdisjoint(PyObject *self, PyObject *other)
3926{
3927 PyObject *it;
3928 PyObject *item = NULL;
3929
3930 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06003931 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003932 Py_RETURN_TRUE;
3933 else
3934 Py_RETURN_FALSE;
3935 }
3936
3937 /* Iterate over the shorter object (only if other is a set,
3938 * because PySequence_Contains may be expensive otherwise): */
3939 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06003940 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003941 Py_ssize_t len_other = PyObject_Size(other);
3942 if (len_other == -1)
3943 return NULL;
3944
3945 if ((len_other > len_self)) {
3946 PyObject *tmp = other;
3947 other = self;
3948 self = tmp;
3949 }
3950 }
3951
3952 it = PyObject_GetIter(other);
3953 if (it == NULL)
3954 return NULL;
3955
3956 while ((item = PyIter_Next(it)) != NULL) {
3957 int contains = PySequence_Contains(self, item);
3958 Py_DECREF(item);
3959 if (contains == -1) {
3960 Py_DECREF(it);
3961 return NULL;
3962 }
3963
3964 if (contains) {
3965 Py_DECREF(it);
3966 Py_RETURN_FALSE;
3967 }
3968 }
3969 Py_DECREF(it);
3970 if (PyErr_Occurred())
3971 return NULL; /* PyIter_Next raised an exception. */
3972 Py_RETURN_TRUE;
3973}
3974
3975PyDoc_STRVAR(isdisjoint_doc,
3976"Return True if the view and the given iterable have a null intersection.");
3977
Guido van Rossumb90c8482007-02-10 01:11:45 +00003978static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003979 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3980 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003981 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003982};
3983
3984PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003985 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3986 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06003987 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003988 0, /* tp_itemsize */
3989 /* methods */
3990 (destructor)dictview_dealloc, /* tp_dealloc */
3991 0, /* tp_print */
3992 0, /* tp_getattr */
3993 0, /* tp_setattr */
3994 0, /* tp_reserved */
3995 (reprfunc)dictview_repr, /* tp_repr */
3996 &dictviews_as_number, /* tp_as_number */
3997 &dictkeys_as_sequence, /* tp_as_sequence */
3998 0, /* tp_as_mapping */
3999 0, /* tp_hash */
4000 0, /* tp_call */
4001 0, /* tp_str */
4002 PyObject_GenericGetAttr, /* tp_getattro */
4003 0, /* tp_setattro */
4004 0, /* tp_as_buffer */
4005 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4006 0, /* tp_doc */
4007 (traverseproc)dictview_traverse, /* tp_traverse */
4008 0, /* tp_clear */
4009 dictview_richcompare, /* tp_richcompare */
4010 0, /* tp_weaklistoffset */
4011 (getiterfunc)dictkeys_iter, /* tp_iter */
4012 0, /* tp_iternext */
4013 dictkeys_methods, /* tp_methods */
4014 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004015};
4016
4017static PyObject *
4018dictkeys_new(PyObject *dict)
4019{
Eric Snow96c6af92015-05-29 22:21:39 -06004020 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004021}
4022
Guido van Rossum3ac67412007-02-10 18:55:06 +00004023/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004024
4025static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004026dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004027{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004028 if (dv->dv_dict == NULL) {
4029 Py_RETURN_NONE;
4030 }
4031 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004032}
4033
4034static int
Eric Snow96c6af92015-05-29 22:21:39 -06004035dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004036{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004037 int result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004038 PyObject *key, *value, *found;
4039 if (dv->dv_dict == NULL)
4040 return 0;
4041 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
4042 return 0;
4043 key = PyTuple_GET_ITEM(obj, 0);
4044 value = PyTuple_GET_ITEM(obj, 1);
Raymond Hettinger6692f012016-09-18 21:46:08 -07004045 found = PyDict_GetItemWithError((PyObject *)dv->dv_dict, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004046 if (found == NULL) {
4047 if (PyErr_Occurred())
4048 return -1;
4049 return 0;
4050 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004051 Py_INCREF(found);
4052 result = PyObject_RichCompareBool(value, found, Py_EQ);
4053 Py_DECREF(found);
4054 return result;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004055}
4056
Guido van Rossum83825ac2007-02-10 04:54:19 +00004057static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004058 (lenfunc)dictview_len, /* sq_length */
4059 0, /* sq_concat */
4060 0, /* sq_repeat */
4061 0, /* sq_item */
4062 0, /* sq_slice */
4063 0, /* sq_ass_item */
4064 0, /* sq_ass_slice */
4065 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004066};
4067
Guido van Rossumb90c8482007-02-10 01:11:45 +00004068static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004069 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4070 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004071 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004072};
4073
4074PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004075 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4076 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004077 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004078 0, /* tp_itemsize */
4079 /* methods */
4080 (destructor)dictview_dealloc, /* tp_dealloc */
4081 0, /* tp_print */
4082 0, /* tp_getattr */
4083 0, /* tp_setattr */
4084 0, /* tp_reserved */
4085 (reprfunc)dictview_repr, /* tp_repr */
4086 &dictviews_as_number, /* tp_as_number */
4087 &dictitems_as_sequence, /* tp_as_sequence */
4088 0, /* tp_as_mapping */
4089 0, /* tp_hash */
4090 0, /* tp_call */
4091 0, /* tp_str */
4092 PyObject_GenericGetAttr, /* tp_getattro */
4093 0, /* tp_setattro */
4094 0, /* tp_as_buffer */
4095 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4096 0, /* tp_doc */
4097 (traverseproc)dictview_traverse, /* tp_traverse */
4098 0, /* tp_clear */
4099 dictview_richcompare, /* tp_richcompare */
4100 0, /* tp_weaklistoffset */
4101 (getiterfunc)dictitems_iter, /* tp_iter */
4102 0, /* tp_iternext */
4103 dictitems_methods, /* tp_methods */
4104 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004105};
4106
4107static PyObject *
4108dictitems_new(PyObject *dict)
4109{
Eric Snow96c6af92015-05-29 22:21:39 -06004110 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004111}
4112
Guido van Rossum3ac67412007-02-10 18:55:06 +00004113/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004114
4115static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004116dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004117{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004118 if (dv->dv_dict == NULL) {
4119 Py_RETURN_NONE;
4120 }
4121 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004122}
4123
Guido van Rossum83825ac2007-02-10 04:54:19 +00004124static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004125 (lenfunc)dictview_len, /* sq_length */
4126 0, /* sq_concat */
4127 0, /* sq_repeat */
4128 0, /* sq_item */
4129 0, /* sq_slice */
4130 0, /* sq_ass_item */
4131 0, /* sq_ass_slice */
4132 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004133};
4134
Guido van Rossumb90c8482007-02-10 01:11:45 +00004135static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004136 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004137};
4138
4139PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004140 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4141 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004142 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004143 0, /* tp_itemsize */
4144 /* methods */
4145 (destructor)dictview_dealloc, /* tp_dealloc */
4146 0, /* tp_print */
4147 0, /* tp_getattr */
4148 0, /* tp_setattr */
4149 0, /* tp_reserved */
4150 (reprfunc)dictview_repr, /* tp_repr */
4151 0, /* tp_as_number */
4152 &dictvalues_as_sequence, /* tp_as_sequence */
4153 0, /* tp_as_mapping */
4154 0, /* tp_hash */
4155 0, /* tp_call */
4156 0, /* tp_str */
4157 PyObject_GenericGetAttr, /* tp_getattro */
4158 0, /* tp_setattro */
4159 0, /* tp_as_buffer */
4160 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4161 0, /* tp_doc */
4162 (traverseproc)dictview_traverse, /* tp_traverse */
4163 0, /* tp_clear */
4164 0, /* tp_richcompare */
4165 0, /* tp_weaklistoffset */
4166 (getiterfunc)dictvalues_iter, /* tp_iter */
4167 0, /* tp_iternext */
4168 dictvalues_methods, /* tp_methods */
4169 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004170};
4171
4172static PyObject *
4173dictvalues_new(PyObject *dict)
4174{
Eric Snow96c6af92015-05-29 22:21:39 -06004175 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004176}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004177
4178/* Returns NULL if cannot allocate a new PyDictKeysObject,
4179 but does not set an error */
4180PyDictKeysObject *
4181_PyDict_NewKeysForClass(void)
4182{
Victor Stinner742da042016-09-07 17:40:12 -07004183 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004184 if (keys == NULL)
4185 PyErr_Clear();
4186 else
4187 keys->dk_lookup = lookdict_split;
4188 return keys;
4189}
4190
4191#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
4192
4193PyObject *
4194PyObject_GenericGetDict(PyObject *obj, void *context)
4195{
4196 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
4197 if (dictptr == NULL) {
4198 PyErr_SetString(PyExc_AttributeError,
4199 "This object has no __dict__");
4200 return NULL;
4201 }
4202 dict = *dictptr;
4203 if (dict == NULL) {
4204 PyTypeObject *tp = Py_TYPE(obj);
4205 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
4206 DK_INCREF(CACHED_KEYS(tp));
4207 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
4208 }
4209 else {
4210 *dictptr = dict = PyDict_New();
4211 }
4212 }
4213 Py_XINCREF(dict);
4214 return dict;
4215}
4216
4217int
4218_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
Victor Stinner742da042016-09-07 17:40:12 -07004219 PyObject *key, PyObject *value)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004220{
4221 PyObject *dict;
4222 int res;
4223 PyDictKeysObject *cached;
4224
4225 assert(dictptr != NULL);
4226 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
4227 assert(dictptr != NULL);
4228 dict = *dictptr;
4229 if (dict == NULL) {
4230 DK_INCREF(cached);
4231 dict = new_dict_with_shared_keys(cached);
4232 if (dict == NULL)
4233 return -1;
4234 *dictptr = dict;
4235 }
4236 if (value == NULL) {
4237 res = PyDict_DelItem(dict, key);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004238 // Since key sharing dict doesn't allow deletion, PyDict_DelItem()
4239 // always converts dict to combined form.
4240 if ((cached = CACHED_KEYS(tp)) != NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004241 CACHED_KEYS(tp) = NULL;
4242 DK_DECREF(cached);
4243 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01004244 }
4245 else {
INADA Naoki2294f3a2017-02-12 13:51:30 +09004246 int was_shared = (cached == ((PyDictObject *)dict)->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004247 res = PyDict_SetItem(dict, key, value);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004248 if (was_shared &&
4249 (cached = CACHED_KEYS(tp)) != NULL &&
4250 cached != ((PyDictObject *)dict)->ma_keys) {
Victor Stinner3d3f2642016-12-15 17:21:23 +01004251 /* PyDict_SetItem() may call dictresize and convert split table
4252 * into combined table. In such case, convert it to split
4253 * table again and update type's shared key only when this is
4254 * the only dict sharing key with the type.
4255 *
4256 * This is to allow using shared key in class like this:
4257 *
4258 * class C:
4259 * def __init__(self):
4260 * # one dict resize happens
4261 * self.a, self.b, self.c = 1, 2, 3
4262 * self.d, self.e, self.f = 4, 5, 6
4263 * a = C()
4264 */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004265 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004266 CACHED_KEYS(tp) = make_keys_shared(dict);
Victor Stinner742da042016-09-07 17:40:12 -07004267 }
4268 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004269 CACHED_KEYS(tp) = NULL;
4270 }
4271 DK_DECREF(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004272 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
4273 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004274 }
4275 }
4276 } else {
4277 dict = *dictptr;
4278 if (dict == NULL) {
4279 dict = PyDict_New();
4280 if (dict == NULL)
4281 return -1;
4282 *dictptr = dict;
4283 }
4284 if (value == NULL) {
4285 res = PyDict_DelItem(dict, key);
4286 } else {
4287 res = PyDict_SetItem(dict, key, value);
4288 }
4289 }
4290 return res;
4291}
4292
4293void
4294_PyDictKeys_DecRef(PyDictKeysObject *keys)
4295{
4296 DK_DECREF(keys);
4297}