blob: bb4ea1f5f9c522b5ef296b103c8d605d4c412edb [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
Yury Selivanovb0a7a032018-01-22 11:54:41 -0500618
619static PyObject *
620clone_combined_dict(PyDictObject *orig)
621{
622 assert(PyDict_CheckExact(orig));
623 assert(orig->ma_values == NULL);
624 assert(orig->ma_keys->dk_refcnt == 1);
625
626 Py_ssize_t keys_size = _PyDict_KeysSize(orig->ma_keys);
627 PyDictKeysObject *keys = PyObject_Malloc(keys_size);
628 if (keys == NULL) {
629 PyErr_NoMemory();
630 return NULL;
631 }
632
633 memcpy(keys, orig->ma_keys, keys_size);
634
635 /* After copying key/value pairs, we need to incref all
636 keys and values and they are about to be co-owned by a
637 new dict object. */
638 PyDictKeyEntry *ep0 = DK_ENTRIES(keys);
639 Py_ssize_t n = keys->dk_nentries;
640 for (Py_ssize_t i = 0; i < n; i++) {
641 PyDictKeyEntry *entry = &ep0[i];
642 PyObject *value = entry->me_value;
643 if (value != NULL) {
644 Py_INCREF(value);
645 Py_INCREF(entry->me_key);
646 }
647 }
648
649 PyDictObject *new = (PyDictObject *)new_dict(keys, NULL);
650 if (new == NULL) {
651 /* In case of an error, `new_dict()` takes care of
652 cleaning up `keys`. */
653 return NULL;
654 }
655 new->ma_used = orig->ma_used;
656 assert(_PyDict_CheckConsistency(new));
657 if (_PyObject_GC_IS_TRACKED(orig)) {
658 /* Maintain tracking. */
659 _PyObject_GC_TRACK(new);
660 }
661 return (PyObject *)new;
662}
663
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400664PyObject *
665PyDict_New(void)
666{
Victor Stinner742da042016-09-07 17:40:12 -0700667 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200668 if (keys == NULL)
669 return NULL;
670 return new_dict(keys, NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400671}
672
Victor Stinner742da042016-09-07 17:40:12 -0700673/* Search index of hash table from offset of entry table */
674static Py_ssize_t
675lookdict_index(PyDictKeysObject *k, Py_hash_t hash, Py_ssize_t index)
676{
Victor Stinner742da042016-09-07 17:40:12 -0700677 size_t mask = DK_MASK(k);
INADA Naoki073ae482017-06-23 15:22:50 +0900678 size_t perturb = (size_t)hash;
679 size_t i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700680
INADA Naoki073ae482017-06-23 15:22:50 +0900681 for (;;) {
682 Py_ssize_t ix = dk_get_index(k, i);
Victor Stinner742da042016-09-07 17:40:12 -0700683 if (ix == index) {
684 return i;
685 }
686 if (ix == DKIX_EMPTY) {
687 return DKIX_EMPTY;
688 }
INADA Naoki073ae482017-06-23 15:22:50 +0900689 perturb >>= PERTURB_SHIFT;
690 i = mask & (i*5 + perturb + 1);
Victor Stinner742da042016-09-07 17:40:12 -0700691 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700692 Py_UNREACHABLE();
Victor Stinner742da042016-09-07 17:40:12 -0700693}
694
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000695/*
696The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000697This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000698Open addressing is preferred over chaining since the link overhead for
699chaining would be substantial (100% with typical malloc overhead).
700
Tim Peterseb28ef22001-06-02 05:27:19 +0000701The initial probe index is computed as hash mod the table size. Subsequent
702probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000703
704All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000705
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000706The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000707contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000708Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000709
Victor Stinner742da042016-09-07 17:40:12 -0700710lookdict() is general-purpose, and may return DKIX_ERROR if (and only if) a
Victor Stinnera4348cc2016-09-08 12:01:25 -0700711comparison raises an exception.
Guido van Rossum89d8c602007-09-18 17:26:56 +0000712lookdict_unicode() below is specialized to string keys, comparison of which can
INADA Naoki1b8df102017-02-20 22:48:10 +0900713never raise an exception; that function can never return DKIX_ERROR when key
714is string. Otherwise, it falls back to lookdict().
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400715lookdict_unicode_nodummy is further specialized for string keys that cannot be
716the <dummy> value.
INADA Naoki778928b2017-08-03 23:45:15 +0900717For both, when the key isn't found a DKIX_EMPTY is returned.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000718*/
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100719static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400720lookdict(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900721 Py_hash_t hash, PyObject **value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000722{
INADA Naoki778928b2017-08-03 23:45:15 +0900723 size_t i, mask, perturb;
Victor Stinner742da042016-09-07 17:40:12 -0700724 PyDictKeysObject *dk;
INADA Naoki778928b2017-08-03 23:45:15 +0900725 PyDictKeyEntry *ep0;
Tim Peterseb28ef22001-06-02 05:27:19 +0000726
Antoine Pitrou9a234902012-05-13 20:48:01 +0200727top:
Victor Stinner742da042016-09-07 17:40:12 -0700728 dk = mp->ma_keys;
Victor Stinner742da042016-09-07 17:40:12 -0700729 ep0 = DK_ENTRIES(dk);
INADA Naoki778928b2017-08-03 23:45:15 +0900730 mask = DK_MASK(dk);
731 perturb = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700733
INADA Naoki778928b2017-08-03 23:45:15 +0900734 for (;;) {
735 Py_ssize_t ix = dk_get_index(dk, i);
Victor Stinner742da042016-09-07 17:40:12 -0700736 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700737 *value_addr = NULL;
738 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400739 }
INADA Naoki778928b2017-08-03 23:45:15 +0900740 if (ix >= 0) {
741 PyDictKeyEntry *ep = &ep0[ix];
742 assert(ep->me_key != NULL);
743 if (ep->me_key == key) {
744 *value_addr = ep->me_value;
745 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700746 }
INADA Naoki778928b2017-08-03 23:45:15 +0900747 if (ep->me_hash == hash) {
748 PyObject *startkey = ep->me_key;
749 Py_INCREF(startkey);
750 int cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
751 Py_DECREF(startkey);
752 if (cmp < 0) {
753 *value_addr = NULL;
754 return DKIX_ERROR;
755 }
756 if (dk == mp->ma_keys && ep->me_key == startkey) {
757 if (cmp > 0) {
758 *value_addr = ep->me_value;
759 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700760 }
INADA Naoki778928b2017-08-03 23:45:15 +0900761 }
762 else {
763 /* The dict was mutated, restart */
764 goto top;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400765 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 }
INADA Naoki778928b2017-08-03 23:45:15 +0900768 perturb >>= PERTURB_SHIFT;
769 i = (i*5 + perturb + 1) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700771 Py_UNREACHABLE();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000772}
773
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400774/* Specialized version for string-only keys */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100775static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400776lookdict_unicode(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900777 Py_hash_t hash, PyObject **value_addr)
Fred Drake1bff34a2000-08-31 19:31:38 +0000778{
Victor Stinner742da042016-09-07 17:40:12 -0700779 assert(mp->ma_values == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 /* Make sure this function doesn't have to handle non-unicode keys,
781 including subclasses of str; e.g., one reason to subclass
782 unicodes is to override __eq__, and for speed we don't cater to
783 that here. */
784 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400785 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900786 return lookdict(mp, key, hash, value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 }
Tim Peters15d49292001-05-27 07:39:22 +0000788
INADA Naoki778928b2017-08-03 23:45:15 +0900789 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
790 size_t mask = DK_MASK(mp->ma_keys);
791 size_t perturb = (size_t)hash;
792 size_t i = (size_t)hash & mask;
793
794 for (;;) {
795 Py_ssize_t ix = dk_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700796 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700797 *value_addr = NULL;
798 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400799 }
INADA Naoki778928b2017-08-03 23:45:15 +0900800 if (ix >= 0) {
801 PyDictKeyEntry *ep = &ep0[ix];
802 assert(ep->me_key != NULL);
803 assert(PyUnicode_CheckExact(ep->me_key));
804 if (ep->me_key == key ||
805 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
806 *value_addr = ep->me_value;
807 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700808 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400809 }
INADA Naoki778928b2017-08-03 23:45:15 +0900810 perturb >>= PERTURB_SHIFT;
811 i = mask & (i*5 + perturb + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700813 Py_UNREACHABLE();
Fred Drake1bff34a2000-08-31 19:31:38 +0000814}
815
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400816/* Faster version of lookdict_unicode when it is known that no <dummy> keys
817 * will be present. */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100818static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400819lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900820 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400821{
Victor Stinner742da042016-09-07 17:40:12 -0700822 assert(mp->ma_values == NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400823 /* Make sure this function doesn't have to handle non-unicode keys,
824 including subclasses of str; e.g., one reason to subclass
825 unicodes is to override __eq__, and for speed we don't cater to
826 that here. */
827 if (!PyUnicode_CheckExact(key)) {
828 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900829 return lookdict(mp, key, hash, value_addr);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400830 }
INADA Naoki778928b2017-08-03 23:45:15 +0900831
832 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
833 size_t mask = DK_MASK(mp->ma_keys);
834 size_t perturb = (size_t)hash;
835 size_t i = (size_t)hash & mask;
836
837 for (;;) {
838 Py_ssize_t ix = dk_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700839 assert (ix != DKIX_DUMMY);
840 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700841 *value_addr = NULL;
842 return DKIX_EMPTY;
843 }
INADA Naoki778928b2017-08-03 23:45:15 +0900844 PyDictKeyEntry *ep = &ep0[ix];
845 assert(ep->me_key != NULL);
846 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700847 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400848 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900849 *value_addr = ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700850 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400851 }
INADA Naoki778928b2017-08-03 23:45:15 +0900852 perturb >>= PERTURB_SHIFT;
853 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400854 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700855 Py_UNREACHABLE();
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400856}
857
858/* Version of lookdict for split tables.
859 * All split tables and only split tables use this lookup function.
860 * Split tables only contain unicode keys and no dummy keys,
861 * so algorithm is the same as lookdict_unicode_nodummy.
862 */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100863static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400864lookdict_split(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900865 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400866{
Victor Stinner742da042016-09-07 17:40:12 -0700867 /* mp must split table */
868 assert(mp->ma_values != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400869 if (!PyUnicode_CheckExact(key)) {
INADA Naoki778928b2017-08-03 23:45:15 +0900870 Py_ssize_t ix = lookdict(mp, key, hash, value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700871 if (ix >= 0) {
INADA Naokiba609772016-12-07 20:41:42 +0900872 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700873 }
874 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400875 }
Victor Stinner742da042016-09-07 17:40:12 -0700876
INADA Naoki778928b2017-08-03 23:45:15 +0900877 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
878 size_t mask = DK_MASK(mp->ma_keys);
879 size_t perturb = (size_t)hash;
880 size_t i = (size_t)hash & mask;
881
882 for (;;) {
883 Py_ssize_t ix = dk_get_index(mp->ma_keys, i);
884 assert (ix != DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -0700885 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700886 *value_addr = NULL;
887 return DKIX_EMPTY;
888 }
INADA Naoki778928b2017-08-03 23:45:15 +0900889 PyDictKeyEntry *ep = &ep0[ix];
890 assert(ep->me_key != NULL);
891 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700892 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400893 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900894 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700895 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400896 }
INADA Naoki778928b2017-08-03 23:45:15 +0900897 perturb >>= PERTURB_SHIFT;
898 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400899 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700900 Py_UNREACHABLE();
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400901}
902
Benjamin Petersonfb886362010-04-24 18:21:17 +0000903int
904_PyDict_HasOnlyStringKeys(PyObject *dict)
905{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 Py_ssize_t pos = 0;
907 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000908 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400910 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 return 1;
912 while (PyDict_Next(dict, &pos, &key, &value))
913 if (!PyUnicode_Check(key))
914 return 0;
915 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000916}
917
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000918#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 do { \
920 if (!_PyObject_GC_IS_TRACKED(mp)) { \
921 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
922 _PyObject_GC_MAY_BE_TRACKED(value)) { \
923 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 } \
925 } \
926 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000927
928void
929_PyDict_MaybeUntrack(PyObject *op)
930{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 PyDictObject *mp;
932 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -0700933 Py_ssize_t i, numentries;
934 PyDictKeyEntry *ep0;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000935
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
937 return;
938
939 mp = (PyDictObject *) op;
Victor Stinner742da042016-09-07 17:40:12 -0700940 ep0 = DK_ENTRIES(mp->ma_keys);
941 numentries = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400942 if (_PyDict_HasSplitTable(mp)) {
Victor Stinner742da042016-09-07 17:40:12 -0700943 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400944 if ((value = mp->ma_values[i]) == NULL)
945 continue;
946 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
Victor Stinner742da042016-09-07 17:40:12 -0700947 assert(!_PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400948 return;
949 }
950 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400952 else {
Victor Stinner742da042016-09-07 17:40:12 -0700953 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400954 if ((value = ep0[i].me_value) == NULL)
955 continue;
956 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
957 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
958 return;
959 }
960 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000962}
963
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400964/* Internal function to find slot for an item from its hash
Victor Stinner3c336c52016-09-12 14:17:40 +0200965 when it is known that the key is not present in the dict.
966
967 The dict must be combined. */
INADA Naokiba609772016-12-07 20:41:42 +0900968static Py_ssize_t
INADA Naoki778928b2017-08-03 23:45:15 +0900969find_empty_slot(PyDictKeysObject *keys, Py_hash_t hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000970{
INADA Naoki778928b2017-08-03 23:45:15 +0900971 assert(keys != NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000972
INADA Naoki778928b2017-08-03 23:45:15 +0900973 const size_t mask = DK_MASK(keys);
974 size_t i = hash & mask;
975 Py_ssize_t ix = dk_get_index(keys, i);
976 for (size_t perturb = hash; ix >= 0;) {
INADA Naoki267941c2016-10-06 15:19:07 +0900977 perturb >>= PERTURB_SHIFT;
INADA Naoki778928b2017-08-03 23:45:15 +0900978 i = (i*5 + perturb + 1) & mask;
979 ix = dk_get_index(keys, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 }
INADA Naoki778928b2017-08-03 23:45:15 +0900981 return i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000982}
983
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400984static int
985insertion_resize(PyDictObject *mp)
986{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700987 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400988}
Antoine Pitroue965d972012-02-27 00:45:12 +0100989
990/*
991Internal routine to insert a new item into the table.
992Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +0100993Returns -1 if an error occurred, or 0 on success.
994*/
995static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400996insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +0100997{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400998 PyObject *old_value;
INADA Naokiba609772016-12-07 20:41:42 +0900999 PyDictKeyEntry *ep;
Antoine Pitroue965d972012-02-27 00:45:12 +01001000
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001001 Py_INCREF(key);
1002 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001003 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
1004 if (insertion_resize(mp) < 0)
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001005 goto Fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001006 }
1007
INADA Naoki778928b2017-08-03 23:45:15 +09001008 Py_ssize_t ix = mp->ma_keys->dk_lookup(mp, key, hash, &old_value);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001009 if (ix == DKIX_ERROR)
1010 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001011
Antoine Pitroud6967322014-10-18 00:35:00 +02001012 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001013 MAINTAIN_TRACKING(mp, key, value);
Victor Stinner742da042016-09-07 17:40:12 -07001014
1015 /* When insertion order is different from shared key, we can't share
1016 * the key anymore. Convert this instance to combine table.
1017 */
1018 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09001019 ((ix >= 0 && old_value == NULL && mp->ma_used != ix) ||
Victor Stinner742da042016-09-07 17:40:12 -07001020 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001021 if (insertion_resize(mp) < 0)
1022 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001023 ix = DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001024 }
Victor Stinner742da042016-09-07 17:40:12 -07001025
1026 if (ix == DKIX_EMPTY) {
1027 /* Insert into new slot. */
INADA Naokiba609772016-12-07 20:41:42 +09001028 assert(old_value == NULL);
Victor Stinner742da042016-09-07 17:40:12 -07001029 if (mp->ma_keys->dk_usable <= 0) {
1030 /* Need to resize. */
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001031 if (insertion_resize(mp) < 0)
1032 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001033 }
INADA Naoki778928b2017-08-03 23:45:15 +09001034 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naokiba609772016-12-07 20:41:42 +09001035 ep = &DK_ENTRIES(mp->ma_keys)[mp->ma_keys->dk_nentries];
Victor Stinner742da042016-09-07 17:40:12 -07001036 dk_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Victor Stinner742da042016-09-07 17:40:12 -07001037 ep->me_key = key;
1038 ep->me_hash = hash;
1039 if (mp->ma_values) {
1040 assert (mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
1041 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001042 }
1043 else {
Victor Stinner742da042016-09-07 17:40:12 -07001044 ep->me_value = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001045 }
1046 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001047 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07001048 mp->ma_keys->dk_usable--;
1049 mp->ma_keys->dk_nentries++;
1050 assert(mp->ma_keys->dk_usable >= 0);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001051 assert(_PyDict_CheckConsistency(mp));
Victor Stinner742da042016-09-07 17:40:12 -07001052 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001053 }
Victor Stinner742da042016-09-07 17:40:12 -07001054
INADA Naokiba609772016-12-07 20:41:42 +09001055 if (_PyDict_HasSplitTable(mp)) {
1056 mp->ma_values[ix] = value;
1057 if (old_value == NULL) {
1058 /* pending state */
1059 assert(ix == mp->ma_used);
1060 mp->ma_used++;
1061 }
1062 }
1063 else {
1064 assert(old_value != NULL);
1065 DK_ENTRIES(mp->ma_keys)[ix].me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07001066 }
1067
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001068 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokiba609772016-12-07 20:41:42 +09001069 Py_XDECREF(old_value); /* which **CAN** re-enter (see issue #22653) */
Victor Stinner611b0fa2016-09-14 15:02:01 +02001070 assert(_PyDict_CheckConsistency(mp));
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001071 Py_DECREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001072 return 0;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001073
1074Fail:
1075 Py_DECREF(value);
1076 Py_DECREF(key);
1077 return -1;
Antoine Pitroue965d972012-02-27 00:45:12 +01001078}
1079
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001080/*
luzpaza5293b42017-11-05 07:37:50 -06001081Internal routine used by dictresize() to build a hashtable of entries.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001082*/
1083static void
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001084build_indices(PyDictKeysObject *keys, PyDictKeyEntry *ep, Py_ssize_t n)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001085{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001086 size_t mask = (size_t)DK_SIZE(keys) - 1;
1087 for (Py_ssize_t ix = 0; ix != n; ix++, ep++) {
1088 Py_hash_t hash = ep->me_hash;
1089 size_t i = hash & mask;
1090 for (size_t perturb = hash; dk_get_index(keys, i) != DKIX_EMPTY;) {
1091 perturb >>= PERTURB_SHIFT;
INADA Naoki870c2862017-06-24 09:03:19 +09001092 i = mask & (i*5 + perturb + 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001093 }
1094 dk_set_index(keys, i, ix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001096}
1097
1098/*
1099Restructure the table by allocating a new table and reinserting all
1100items again. When entries have been deleted, the new table may
1101actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001102If a table is split (its keys and hashes are shared, its values are not),
1103then the values are temporarily copied into the table, it is resized as
1104a combined table, then the me_value slots in the old table are NULLed out.
1105After resizing a table is always combined,
1106but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001107*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001108static int
Victor Stinner3d3f2642016-12-15 17:21:23 +01001109dictresize(PyDictObject *mp, Py_ssize_t minsize)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001110{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001111 Py_ssize_t newsize, numentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001112 PyDictKeysObject *oldkeys;
1113 PyObject **oldvalues;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001114 PyDictKeyEntry *oldentries, *newentries;
Tim Peters91a364d2001-05-19 07:04:38 +00001115
Victor Stinner742da042016-09-07 17:40:12 -07001116 /* Find the smallest table size > minused. */
1117 for (newsize = PyDict_MINSIZE;
Victor Stinner3d3f2642016-12-15 17:21:23 +01001118 newsize < minsize && newsize > 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 newsize <<= 1)
1120 ;
1121 if (newsize <= 0) {
1122 PyErr_NoMemory();
1123 return -1;
1124 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001125
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001126 oldkeys = mp->ma_keys;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001127
1128 /* NOTE: Current odict checks mp->ma_keys to detect resize happen.
1129 * So we can't reuse oldkeys even if oldkeys->dk_size == newsize.
1130 * TODO: Try reusing oldkeys when reimplement odict.
1131 */
1132
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001133 /* Allocate a new table. */
1134 mp->ma_keys = new_keys_object(newsize);
1135 if (mp->ma_keys == NULL) {
1136 mp->ma_keys = oldkeys;
1137 return -1;
1138 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01001139 // New table must be large enough.
1140 assert(mp->ma_keys->dk_usable >= mp->ma_used);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001141 if (oldkeys->dk_lookup == lookdict)
1142 mp->ma_keys->dk_lookup = lookdict;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001143
1144 numentries = mp->ma_used;
1145 oldentries = DK_ENTRIES(oldkeys);
1146 newentries = DK_ENTRIES(mp->ma_keys);
1147 oldvalues = mp->ma_values;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001148 if (oldvalues != NULL) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001149 /* Convert split table into new combined table.
1150 * We must incref keys; we can transfer values.
1151 * Note that values of split table is always dense.
1152 */
1153 for (Py_ssize_t i = 0; i < numentries; i++) {
1154 assert(oldvalues[i] != NULL);
1155 PyDictKeyEntry *ep = &oldentries[i];
1156 PyObject *key = ep->me_key;
1157 Py_INCREF(key);
1158 newentries[i].me_key = key;
1159 newentries[i].me_hash = ep->me_hash;
1160 newentries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001162
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001163 DK_DECREF(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001164 mp->ma_values = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001165 if (oldvalues != empty_values) {
1166 free_values(oldvalues);
1167 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001168 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001169 else { // combined table.
1170 if (oldkeys->dk_nentries == numentries) {
1171 memcpy(newentries, oldentries, numentries * sizeof(PyDictKeyEntry));
1172 }
1173 else {
1174 PyDictKeyEntry *ep = oldentries;
1175 for (Py_ssize_t i = 0; i < numentries; i++) {
1176 while (ep->me_value == NULL)
1177 ep++;
1178 newentries[i] = *ep++;
1179 }
1180 }
1181
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001182 assert(oldkeys->dk_lookup != lookdict_split);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001183 assert(oldkeys->dk_refcnt == 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001184 if (oldkeys->dk_size == PyDict_MINSIZE &&
1185 numfreekeys < PyDict_MAXFREELIST) {
1186 DK_DEBUG_DECREF keys_free_list[numfreekeys++] = oldkeys;
1187 }
1188 else {
1189 DK_DEBUG_DECREF PyObject_FREE(oldkeys);
1190 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001191 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001192
1193 build_indices(mp->ma_keys, newentries, numentries);
1194 mp->ma_keys->dk_usable -= numentries;
1195 mp->ma_keys->dk_nentries = numentries;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001197}
1198
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001199/* Returns NULL if unable to split table.
1200 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001201static PyDictKeysObject *
1202make_keys_shared(PyObject *op)
1203{
1204 Py_ssize_t i;
1205 Py_ssize_t size;
1206 PyDictObject *mp = (PyDictObject *)op;
1207
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001208 if (!PyDict_CheckExact(op))
1209 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001210 if (!_PyDict_HasSplitTable(mp)) {
1211 PyDictKeyEntry *ep0;
1212 PyObject **values;
1213 assert(mp->ma_keys->dk_refcnt == 1);
1214 if (mp->ma_keys->dk_lookup == lookdict) {
1215 return NULL;
1216 }
1217 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1218 /* Remove dummy keys */
1219 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1220 return NULL;
1221 }
1222 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1223 /* Copy values into a new array */
Victor Stinner742da042016-09-07 17:40:12 -07001224 ep0 = DK_ENTRIES(mp->ma_keys);
1225 size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001226 values = new_values(size);
1227 if (values == NULL) {
1228 PyErr_SetString(PyExc_MemoryError,
1229 "Not enough memory to allocate new values array");
1230 return NULL;
1231 }
1232 for (i = 0; i < size; i++) {
1233 values[i] = ep0[i].me_value;
1234 ep0[i].me_value = NULL;
1235 }
1236 mp->ma_keys->dk_lookup = lookdict_split;
1237 mp->ma_values = values;
1238 }
1239 DK_INCREF(mp->ma_keys);
1240 return mp->ma_keys;
1241}
Christian Heimes99170a52007-12-19 02:07:34 +00001242
1243PyObject *
1244_PyDict_NewPresized(Py_ssize_t minused)
1245{
INADA Naoki92c50ee2016-11-22 00:57:02 +09001246 const Py_ssize_t max_presize = 128 * 1024;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001247 Py_ssize_t newsize;
1248 PyDictKeysObject *new_keys;
INADA Naoki92c50ee2016-11-22 00:57:02 +09001249
1250 /* There are no strict guarantee that returned dict can contain minused
1251 * items without resize. So we create medium size dict instead of very
1252 * large dict or MemoryError.
1253 */
1254 if (minused > USABLE_FRACTION(max_presize)) {
1255 newsize = max_presize;
1256 }
1257 else {
1258 Py_ssize_t minsize = ESTIMATE_SIZE(minused);
1259 newsize = PyDict_MINSIZE;
1260 while (newsize < minsize) {
1261 newsize <<= 1;
1262 }
1263 }
1264 assert(IS_POWER_OF_2(newsize));
1265
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001266 new_keys = new_keys_object(newsize);
1267 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001269 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001270}
1271
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001272/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1273 * that may occur (originally dicts supported only string keys, and exceptions
1274 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001275 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001276 * (suppressed) occurred while computing the key's hash, or that some error
1277 * (suppressed) occurred when comparing keys in the dict's internal probe
1278 * sequence. A nasty example of the latter is when a Python-coded comparison
1279 * function hits a stack-depth error, which can cause this to return NULL
1280 * even if the key is present.
1281 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001282PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001283PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001284{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001285 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07001286 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 PyDictObject *mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 PyThreadState *tstate;
INADA Naokiba609772016-12-07 20:41:42 +09001289 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 if (!PyDict_Check(op))
1292 return NULL;
1293 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001294 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 {
1296 hash = PyObject_Hash(key);
1297 if (hash == -1) {
1298 PyErr_Clear();
1299 return NULL;
1300 }
1301 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 /* We can arrive here with a NULL tstate during initialization: try
1304 running "python -Wi" for an example related to string interning.
1305 Let's just hope that no exception occurs then... This must be
1306 _PyThreadState_Current and not PyThreadState_GET() because in debug
1307 mode, the latter complains if tstate is NULL. */
Victor Stinner0cae6092016-11-11 01:43:56 +01001308 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 if (tstate != NULL && tstate->curexc_type != NULL) {
1310 /* preserve the existing exception */
1311 PyObject *err_type, *err_value, *err_tb;
1312 PyErr_Fetch(&err_type, &err_value, &err_tb);
INADA Naoki778928b2017-08-03 23:45:15 +09001313 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 /* ignore errors */
1315 PyErr_Restore(err_type, err_value, err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001316 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 return NULL;
1318 }
1319 else {
INADA Naoki778928b2017-08-03 23:45:15 +09001320 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001321 if (ix < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 PyErr_Clear();
1323 return NULL;
1324 }
1325 }
INADA Naokiba609772016-12-07 20:41:42 +09001326 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001327}
1328
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001329/* Same as PyDict_GetItemWithError() but with hash supplied by caller.
1330 This returns NULL *with* an exception set if an exception occurred.
1331 It returns NULL *without* an exception set if the key wasn't present.
1332*/
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001333PyObject *
1334_PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1335{
Victor Stinner742da042016-09-07 17:40:12 -07001336 Py_ssize_t ix;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001337 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001338 PyObject *value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001339
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001340 if (!PyDict_Check(op)) {
1341 PyErr_BadInternalCall();
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001342 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001343 }
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001344
INADA Naoki778928b2017-08-03 23:45:15 +09001345 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001346 if (ix < 0) {
1347 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001348 }
INADA Naokiba609772016-12-07 20:41:42 +09001349 return value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001350}
1351
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001352/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1353 This returns NULL *with* an exception set if an exception occurred.
1354 It returns NULL *without* an exception set if the key wasn't present.
1355*/
1356PyObject *
1357PyDict_GetItemWithError(PyObject *op, PyObject *key)
1358{
Victor Stinner742da042016-09-07 17:40:12 -07001359 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001360 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 PyDictObject*mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001362 PyObject *value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 if (!PyDict_Check(op)) {
1365 PyErr_BadInternalCall();
1366 return NULL;
1367 }
1368 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001369 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 {
1371 hash = PyObject_Hash(key);
1372 if (hash == -1) {
1373 return NULL;
1374 }
1375 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001376
INADA Naoki778928b2017-08-03 23:45:15 +09001377 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001378 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001380 return value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001381}
1382
Brett Cannonfd074152012-04-14 14:10:13 -04001383PyObject *
1384_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1385{
1386 PyObject *kv;
1387 kv = _PyUnicode_FromId(key); /* borrowed */
1388 if (kv == NULL)
1389 return NULL;
1390 return PyDict_GetItemWithError(dp, kv);
1391}
1392
Victor Stinnerb4efc962015-11-20 09:24:02 +01001393/* Fast version of global value lookup (LOAD_GLOBAL).
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001394 * Lookup in globals, then builtins.
Victor Stinnerb4efc962015-11-20 09:24:02 +01001395 *
1396 * Raise an exception and return NULL if an error occurred (ex: computing the
1397 * key hash failed, key comparison failed, ...). Return NULL if the key doesn't
1398 * exist. Return the value if the key exists.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001399 */
1400PyObject *
1401_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001402{
Victor Stinner742da042016-09-07 17:40:12 -07001403 Py_ssize_t ix;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001404 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09001405 PyObject *value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001406
1407 if (!PyUnicode_CheckExact(key) ||
1408 (hash = ((PyASCIIObject *) key)->hash) == -1)
1409 {
1410 hash = PyObject_Hash(key);
1411 if (hash == -1)
1412 return NULL;
Antoine Pitroue965d972012-02-27 00:45:12 +01001413 }
Victor Stinnerb4efc962015-11-20 09:24:02 +01001414
1415 /* namespace 1: globals */
INADA Naoki778928b2017-08-03 23:45:15 +09001416 ix = globals->ma_keys->dk_lookup(globals, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001417 if (ix == DKIX_ERROR)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001418 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001419 if (ix != DKIX_EMPTY && value != NULL)
1420 return value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001421
1422 /* namespace 2: builtins */
INADA Naoki778928b2017-08-03 23:45:15 +09001423 ix = builtins->ma_keys->dk_lookup(builtins, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001424 if (ix < 0)
Victor Stinnerb4efc962015-11-20 09:24:02 +01001425 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001426 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001427}
1428
Antoine Pitroue965d972012-02-27 00:45:12 +01001429/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1430 * dictionary if it's merely replacing the value for an existing key.
1431 * This means that it's safe to loop over a dictionary with PyDict_Next()
1432 * and occasionally replace a value -- but you can't insert new keys or
1433 * remove them.
1434 */
1435int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001436PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001437{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001438 PyDictObject *mp;
1439 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001440 if (!PyDict_Check(op)) {
1441 PyErr_BadInternalCall();
1442 return -1;
1443 }
1444 assert(key);
1445 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001446 mp = (PyDictObject *)op;
1447 if (!PyUnicode_CheckExact(key) ||
1448 (hash = ((PyASCIIObject *) key)->hash) == -1)
1449 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001450 hash = PyObject_Hash(key);
1451 if (hash == -1)
1452 return -1;
1453 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001454
1455 /* insertdict() handles any resizing that might be necessary */
1456 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001457}
1458
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001459int
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001460_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1461 Py_hash_t hash)
1462{
1463 PyDictObject *mp;
1464
1465 if (!PyDict_Check(op)) {
1466 PyErr_BadInternalCall();
1467 return -1;
1468 }
1469 assert(key);
1470 assert(value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001471 assert(hash != -1);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001472 mp = (PyDictObject *)op;
1473
1474 /* insertdict() handles any resizing that might be necessary */
1475 return insertdict(mp, key, hash, value);
1476}
1477
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001478static int
INADA Naoki778928b2017-08-03 23:45:15 +09001479delitem_common(PyDictObject *mp, Py_hash_t hash, Py_ssize_t ix,
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001480 PyObject *old_value)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001481{
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001482 PyObject *old_key;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001483 PyDictKeyEntry *ep;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001484
INADA Naoki778928b2017-08-03 23:45:15 +09001485 Py_ssize_t hashpos = lookdict_index(mp->ma_keys, hash, ix);
1486 assert(hashpos >= 0);
1487
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001488 mp->ma_used--;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001489 mp->ma_version_tag = DICT_NEXT_VERSION();
1490 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1491 dk_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
1492 ENSURE_ALLOWS_DELETIONS(mp);
1493 old_key = ep->me_key;
1494 ep->me_key = NULL;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001495 ep->me_value = NULL;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001496 Py_DECREF(old_key);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001497 Py_DECREF(old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001498
1499 assert(_PyDict_CheckConsistency(mp));
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001500 return 0;
1501}
1502
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001503int
Tim Peters1f5871e2000-07-04 17:44:48 +00001504PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001505{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001506 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 assert(key);
1508 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001509 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 hash = PyObject_Hash(key);
1511 if (hash == -1)
1512 return -1;
1513 }
Victor Stinner742da042016-09-07 17:40:12 -07001514
1515 return _PyDict_DelItem_KnownHash(op, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001516}
1517
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001518int
1519_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1520{
INADA Naoki778928b2017-08-03 23:45:15 +09001521 Py_ssize_t ix;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001522 PyDictObject *mp;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001523 PyObject *old_value;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001524
1525 if (!PyDict_Check(op)) {
1526 PyErr_BadInternalCall();
1527 return -1;
1528 }
1529 assert(key);
1530 assert(hash != -1);
1531 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001532 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001533 if (ix == DKIX_ERROR)
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001534 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09001535 if (ix == DKIX_EMPTY || old_value == NULL) {
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001536 _PyErr_SetKeyError(key);
1537 return -1;
1538 }
Victor Stinner78601a32016-09-09 19:28:36 -07001539
1540 // Split table doesn't allow deletion. Combine it.
1541 if (_PyDict_HasSplitTable(mp)) {
1542 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1543 return -1;
1544 }
INADA Naoki778928b2017-08-03 23:45:15 +09001545 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001546 assert(ix >= 0);
1547 }
1548
INADA Naoki778928b2017-08-03 23:45:15 +09001549 return delitem_common(mp, hash, ix, old_value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001550}
1551
Antoine Pitroud741ed42016-12-27 14:23:43 +01001552/* This function promises that the predicate -> deletion sequence is atomic
1553 * (i.e. protected by the GIL), assuming the predicate itself doesn't
1554 * release the GIL.
1555 */
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001556int
1557_PyDict_DelItemIf(PyObject *op, PyObject *key,
1558 int (*predicate)(PyObject *value))
1559{
Antoine Pitroud741ed42016-12-27 14:23:43 +01001560 Py_ssize_t hashpos, ix;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001561 PyDictObject *mp;
1562 Py_hash_t hash;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001563 PyObject *old_value;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001564 int res;
1565
1566 if (!PyDict_Check(op)) {
1567 PyErr_BadInternalCall();
1568 return -1;
1569 }
1570 assert(key);
1571 hash = PyObject_Hash(key);
1572 if (hash == -1)
1573 return -1;
1574 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001575 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001576 if (ix == DKIX_ERROR)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001577 return -1;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001578 if (ix == DKIX_EMPTY || old_value == NULL) {
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001579 _PyErr_SetKeyError(key);
1580 return -1;
1581 }
Antoine Pitroud741ed42016-12-27 14:23:43 +01001582
1583 // Split table doesn't allow deletion. Combine it.
1584 if (_PyDict_HasSplitTable(mp)) {
1585 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1586 return -1;
1587 }
INADA Naoki778928b2017-08-03 23:45:15 +09001588 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001589 assert(ix >= 0);
1590 }
1591
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001592 res = predicate(old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001593 if (res == -1)
1594 return -1;
INADA Naoki778928b2017-08-03 23:45:15 +09001595
1596 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1597 assert(hashpos >= 0);
1598
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001599 if (res > 0)
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001600 return delitem_common(mp, hashpos, ix, old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001601 else
1602 return 0;
1603}
1604
1605
Guido van Rossum25831651993-05-19 14:50:45 +00001606void
Tim Peters1f5871e2000-07-04 17:44:48 +00001607PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001608{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001610 PyDictKeysObject *oldkeys;
1611 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 if (!PyDict_Check(op))
1615 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001616 mp = ((PyDictObject *)op);
1617 oldkeys = mp->ma_keys;
1618 oldvalues = mp->ma_values;
1619 if (oldvalues == empty_values)
1620 return;
1621 /* Empty the dict... */
1622 DK_INCREF(Py_EMPTY_KEYS);
1623 mp->ma_keys = Py_EMPTY_KEYS;
1624 mp->ma_values = empty_values;
1625 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001626 mp->ma_version_tag = DICT_NEXT_VERSION();
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001627 /* ...then clear the keys and values */
1628 if (oldvalues != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001629 n = oldkeys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001630 for (i = 0; i < n; i++)
1631 Py_CLEAR(oldvalues[i]);
1632 free_values(oldvalues);
1633 DK_DECREF(oldkeys);
1634 }
1635 else {
1636 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001637 DK_DECREF(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001638 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02001639 assert(_PyDict_CheckConsistency(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001640}
1641
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001642/* Internal version of PyDict_Next that returns a hash value in addition
1643 * to the key and value.
1644 * Return 1 on success, return 0 when the reached the end of the dictionary
1645 * (or if op is not a dictionary)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001646 */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001647int
1648_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1649 PyObject **pvalue, Py_hash_t *phash)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001650{
INADA Naokica2d8be2016-11-04 16:59:10 +09001651 Py_ssize_t i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001652 PyDictObject *mp;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001653 PyDictKeyEntry *entry_ptr;
1654 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001655
1656 if (!PyDict_Check(op))
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001657 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 mp = (PyDictObject *)op;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001659 i = *ppos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001660 if (mp->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09001661 if (i < 0 || i >= mp->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001662 return 0;
INADA Naokica2d8be2016-11-04 16:59:10 +09001663 /* values of split table is always dense */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001664 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
INADA Naokica2d8be2016-11-04 16:59:10 +09001665 value = mp->ma_values[i];
1666 assert(value != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001668 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09001669 Py_ssize_t n = mp->ma_keys->dk_nentries;
1670 if (i < 0 || i >= n)
1671 return 0;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001672 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
1673 while (i < n && entry_ptr->me_value == NULL) {
1674 entry_ptr++;
1675 i++;
Victor Stinner742da042016-09-07 17:40:12 -07001676 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001677 if (i >= n)
1678 return 0;
1679 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001681 *ppos = i+1;
1682 if (pkey)
1683 *pkey = entry_ptr->me_key;
1684 if (phash)
1685 *phash = entry_ptr->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001686 if (pvalue)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001687 *pvalue = value;
1688 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001689}
1690
Tim Peters080c88b2003-02-15 03:01:11 +00001691/*
1692 * Iterate over a dict. Use like so:
1693 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001694 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001695 * PyObject *key, *value;
1696 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001697 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001698 * Refer to borrowed references in key and value.
Tim Peters080c88b2003-02-15 03:01:11 +00001699 * }
1700 *
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001701 * Return 1 on success, return 0 when the reached the end of the dictionary
1702 * (or if op is not a dictionary)
1703 *
Tim Peters080c88b2003-02-15 03:01:11 +00001704 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001705 * mutates the dict. One exception: it is safe if the loop merely changes
1706 * the values associated with the keys (but doesn't insert new keys or
1707 * delete keys), via PyDict_SetItem().
1708 */
Guido van Rossum25831651993-05-19 14:50:45 +00001709int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001710PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001711{
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001712 return _PyDict_Next(op, ppos, pkey, pvalue, NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001713}
1714
Eric Snow96c6af92015-05-29 22:21:39 -06001715/* Internal version of dict.pop(). */
1716PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001717_PyDict_Pop_KnownHash(PyObject *dict, PyObject *key, Py_hash_t hash, PyObject *deflt)
Eric Snow96c6af92015-05-29 22:21:39 -06001718{
Victor Stinner742da042016-09-07 17:40:12 -07001719 Py_ssize_t ix, hashpos;
Eric Snow96c6af92015-05-29 22:21:39 -06001720 PyObject *old_value, *old_key;
1721 PyDictKeyEntry *ep;
Yury Selivanov684ef2c2016-10-28 19:01:21 -04001722 PyDictObject *mp;
1723
1724 assert(PyDict_Check(dict));
1725 mp = (PyDictObject *)dict;
Eric Snow96c6af92015-05-29 22:21:39 -06001726
1727 if (mp->ma_used == 0) {
1728 if (deflt) {
1729 Py_INCREF(deflt);
1730 return deflt;
1731 }
1732 _PyErr_SetKeyError(key);
1733 return NULL;
1734 }
INADA Naoki778928b2017-08-03 23:45:15 +09001735 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001736 if (ix == DKIX_ERROR)
Eric Snow96c6af92015-05-29 22:21:39 -06001737 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001738 if (ix == DKIX_EMPTY || old_value == NULL) {
Eric Snow96c6af92015-05-29 22:21:39 -06001739 if (deflt) {
1740 Py_INCREF(deflt);
1741 return deflt;
1742 }
1743 _PyErr_SetKeyError(key);
1744 return NULL;
1745 }
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001746
Victor Stinner78601a32016-09-09 19:28:36 -07001747 // Split table doesn't allow deletion. Combine it.
1748 if (_PyDict_HasSplitTable(mp)) {
1749 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1750 return NULL;
1751 }
INADA Naoki778928b2017-08-03 23:45:15 +09001752 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001753 assert(ix >= 0);
1754 }
1755
INADA Naoki778928b2017-08-03 23:45:15 +09001756 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1757 assert(hashpos >= 0);
Victor Stinner78601a32016-09-09 19:28:36 -07001758 assert(old_value != NULL);
Eric Snow96c6af92015-05-29 22:21:39 -06001759 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001760 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner78601a32016-09-09 19:28:36 -07001761 dk_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
1762 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1763 ENSURE_ALLOWS_DELETIONS(mp);
1764 old_key = ep->me_key;
1765 ep->me_key = NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001766 ep->me_value = NULL;
Victor Stinner78601a32016-09-09 19:28:36 -07001767 Py_DECREF(old_key);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001768
1769 assert(_PyDict_CheckConsistency(mp));
Eric Snow96c6af92015-05-29 22:21:39 -06001770 return old_value;
1771}
1772
Serhiy Storchaka67796522017-01-12 18:34:33 +02001773PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001774_PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt)
Serhiy Storchaka67796522017-01-12 18:34:33 +02001775{
1776 Py_hash_t hash;
1777
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001778 if (((PyDictObject *)dict)->ma_used == 0) {
Serhiy Storchaka67796522017-01-12 18:34:33 +02001779 if (deflt) {
1780 Py_INCREF(deflt);
1781 return deflt;
1782 }
1783 _PyErr_SetKeyError(key);
1784 return NULL;
1785 }
1786 if (!PyUnicode_CheckExact(key) ||
1787 (hash = ((PyASCIIObject *) key)->hash) == -1) {
1788 hash = PyObject_Hash(key);
1789 if (hash == -1)
1790 return NULL;
1791 }
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001792 return _PyDict_Pop_KnownHash(dict, key, hash, deflt);
Serhiy Storchaka67796522017-01-12 18:34:33 +02001793}
1794
Eric Snow96c6af92015-05-29 22:21:39 -06001795/* Internal version of dict.from_keys(). It is subclass-friendly. */
1796PyObject *
1797_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1798{
1799 PyObject *it; /* iter(iterable) */
1800 PyObject *key;
1801 PyObject *d;
1802 int status;
1803
Victor Stinnera5ed5f02016-12-06 18:45:50 +01001804 d = _PyObject_CallNoArg(cls);
Eric Snow96c6af92015-05-29 22:21:39 -06001805 if (d == NULL)
1806 return NULL;
1807
1808 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
1809 if (PyDict_CheckExact(iterable)) {
1810 PyDictObject *mp = (PyDictObject *)d;
1811 PyObject *oldvalue;
1812 Py_ssize_t pos = 0;
1813 PyObject *key;
1814 Py_hash_t hash;
1815
Serhiy Storchakac61ac162017-03-21 08:52:38 +02001816 if (dictresize(mp, ESTIMATE_SIZE(PyDict_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001817 Py_DECREF(d);
1818 return NULL;
1819 }
1820
1821 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
1822 if (insertdict(mp, key, hash, value)) {
1823 Py_DECREF(d);
1824 return NULL;
1825 }
1826 }
1827 return d;
1828 }
1829 if (PyAnySet_CheckExact(iterable)) {
1830 PyDictObject *mp = (PyDictObject *)d;
1831 Py_ssize_t pos = 0;
1832 PyObject *key;
1833 Py_hash_t hash;
1834
Victor Stinner742da042016-09-07 17:40:12 -07001835 if (dictresize(mp, ESTIMATE_SIZE(PySet_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001836 Py_DECREF(d);
1837 return NULL;
1838 }
1839
1840 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
1841 if (insertdict(mp, key, hash, value)) {
1842 Py_DECREF(d);
1843 return NULL;
1844 }
1845 }
1846 return d;
1847 }
1848 }
1849
1850 it = PyObject_GetIter(iterable);
1851 if (it == NULL){
1852 Py_DECREF(d);
1853 return NULL;
1854 }
1855
1856 if (PyDict_CheckExact(d)) {
1857 while ((key = PyIter_Next(it)) != NULL) {
1858 status = PyDict_SetItem(d, key, value);
1859 Py_DECREF(key);
1860 if (status < 0)
1861 goto Fail;
1862 }
1863 } else {
1864 while ((key = PyIter_Next(it)) != NULL) {
1865 status = PyObject_SetItem(d, key, value);
1866 Py_DECREF(key);
1867 if (status < 0)
1868 goto Fail;
1869 }
1870 }
1871
1872 if (PyErr_Occurred())
1873 goto Fail;
1874 Py_DECREF(it);
1875 return d;
1876
1877Fail:
1878 Py_DECREF(it);
1879 Py_DECREF(d);
1880 return NULL;
1881}
1882
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001883/* Methods */
1884
1885static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001886dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001887{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001888 PyObject **values = mp->ma_values;
1889 PyDictKeysObject *keys = mp->ma_keys;
1890 Py_ssize_t i, n;
INADA Naokia6296d32017-08-24 14:55:17 +09001891
1892 /* bpo-31095: UnTrack is needed before calling any callbacks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 PyObject_GC_UnTrack(mp);
1894 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001895 if (values != NULL) {
1896 if (values != empty_values) {
Victor Stinner742da042016-09-07 17:40:12 -07001897 for (i = 0, n = mp->ma_keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001898 Py_XDECREF(values[i]);
1899 }
1900 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001901 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001902 DK_DECREF(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02001904 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001905 assert(keys->dk_refcnt == 1);
1906 DK_DECREF(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001907 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001908 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1909 free_list[numfree++] = mp;
1910 else
1911 Py_TYPE(mp)->tp_free((PyObject *)mp);
1912 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001913}
1914
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001915
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001916static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001917dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001918{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001920 PyObject *key = NULL, *value = NULL;
1921 _PyUnicodeWriter writer;
1922 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00001923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 i = Py_ReprEnter((PyObject *)mp);
1925 if (i != 0) {
1926 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1927 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001929 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001930 Py_ReprLeave((PyObject *)mp);
1931 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 }
Tim Petersa7259592001-06-16 05:11:17 +00001933
Victor Stinnerf91929b2013-11-19 13:07:38 +01001934 _PyUnicodeWriter_Init(&writer);
1935 writer.overallocate = 1;
1936 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
1937 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00001938
Victor Stinnerf91929b2013-11-19 13:07:38 +01001939 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
1940 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001942 /* Do repr() on each key+value pair, and insert ": " between them.
1943 Note that repr may mutate the dict. */
1944 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001945 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001946 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001947 PyObject *s;
1948 int res;
1949
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001950 /* Prevent repr from deleting key or value during key format. */
1951 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02001953
Victor Stinnerf91929b2013-11-19 13:07:38 +01001954 if (!first) {
1955 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
1956 goto error;
1957 }
1958 first = 0;
1959
1960 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01001962 goto error;
1963 res = _PyUnicodeWriter_WriteStr(&writer, s);
1964 Py_DECREF(s);
1965 if (res < 0)
1966 goto error;
1967
1968 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
1969 goto error;
1970
1971 s = PyObject_Repr(value);
1972 if (s == NULL)
1973 goto error;
1974 res = _PyUnicodeWriter_WriteStr(&writer, s);
1975 Py_DECREF(s);
1976 if (res < 0)
1977 goto error;
1978
1979 Py_CLEAR(key);
1980 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 }
Tim Petersa7259592001-06-16 05:11:17 +00001982
Victor Stinnerf91929b2013-11-19 13:07:38 +01001983 writer.overallocate = 0;
1984 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
1985 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01001988
1989 return _PyUnicodeWriter_Finish(&writer);
1990
1991error:
1992 Py_ReprLeave((PyObject *)mp);
1993 _PyUnicodeWriter_Dealloc(&writer);
1994 Py_XDECREF(key);
1995 Py_XDECREF(value);
1996 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001997}
1998
Martin v. Löwis18e16552006-02-15 17:27:45 +00001999static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002000dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002001{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002002 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002003}
2004
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002005static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002006dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002007{
Victor Stinner742da042016-09-07 17:40:12 -07002008 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002009 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09002010 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002012 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002013 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002014 hash = PyObject_Hash(key);
2015 if (hash == -1)
2016 return NULL;
2017 }
INADA Naoki778928b2017-08-03 23:45:15 +09002018 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002019 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002020 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002021 if (ix == DKIX_EMPTY || value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002022 if (!PyDict_CheckExact(mp)) {
2023 /* Look up __missing__ method if we're a subclass. */
2024 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05002025 _Py_IDENTIFIER(__missing__);
2026 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 if (missing != NULL) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002028 res = PyObject_CallFunctionObjArgs(missing,
2029 key, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002030 Py_DECREF(missing);
2031 return res;
2032 }
2033 else if (PyErr_Occurred())
2034 return NULL;
2035 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07002036 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002037 return NULL;
2038 }
INADA Naokiba609772016-12-07 20:41:42 +09002039 Py_INCREF(value);
2040 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002041}
2042
2043static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002044dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002045{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002046 if (w == NULL)
2047 return PyDict_DelItem((PyObject *)mp, v);
2048 else
2049 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002050}
2051
Guido van Rossuma9e7a811997-05-13 21:02:11 +00002052static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 (lenfunc)dict_length, /*mp_length*/
2054 (binaryfunc)dict_subscript, /*mp_subscript*/
2055 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002056};
2057
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002058static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002059dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002060{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002061 PyObject *v;
2062 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002063 PyDictKeyEntry *ep;
2064 Py_ssize_t size, n, offset;
2065 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002066
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002067 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002068 n = mp->ma_used;
2069 v = PyList_New(n);
2070 if (v == NULL)
2071 return NULL;
2072 if (n != mp->ma_used) {
2073 /* Durnit. The allocations caused the dict to resize.
2074 * Just start over, this shouldn't normally happen.
2075 */
2076 Py_DECREF(v);
2077 goto again;
2078 }
Victor Stinner742da042016-09-07 17:40:12 -07002079 ep = DK_ENTRIES(mp->ma_keys);
2080 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002081 if (mp->ma_values) {
2082 value_ptr = mp->ma_values;
2083 offset = sizeof(PyObject *);
2084 }
2085 else {
2086 value_ptr = &ep[0].me_value;
2087 offset = sizeof(PyDictKeyEntry);
2088 }
2089 for (i = 0, j = 0; i < size; i++) {
2090 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002091 PyObject *key = ep[i].me_key;
2092 Py_INCREF(key);
2093 PyList_SET_ITEM(v, j, key);
2094 j++;
2095 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002096 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002097 }
2098 assert(j == n);
2099 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002100}
2101
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002102static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002103dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002104{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002105 PyObject *v;
2106 Py_ssize_t i, j;
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002107 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002108 Py_ssize_t size, n, offset;
2109 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002110
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002111 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112 n = mp->ma_used;
2113 v = PyList_New(n);
2114 if (v == NULL)
2115 return NULL;
2116 if (n != mp->ma_used) {
2117 /* Durnit. The allocations caused the dict to resize.
2118 * Just start over, this shouldn't normally happen.
2119 */
2120 Py_DECREF(v);
2121 goto again;
2122 }
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002123 ep = DK_ENTRIES(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002124 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002125 if (mp->ma_values) {
2126 value_ptr = mp->ma_values;
2127 offset = sizeof(PyObject *);
2128 }
2129 else {
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002130 value_ptr = &ep[0].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002131 offset = sizeof(PyDictKeyEntry);
2132 }
2133 for (i = 0, j = 0; i < size; i++) {
2134 PyObject *value = *value_ptr;
2135 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2136 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002137 Py_INCREF(value);
2138 PyList_SET_ITEM(v, j, value);
2139 j++;
2140 }
2141 }
2142 assert(j == n);
2143 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002144}
2145
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002146static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002147dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002148{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002149 PyObject *v;
2150 Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002151 Py_ssize_t size, offset;
2152 PyObject *item, *key;
2153 PyDictKeyEntry *ep;
2154 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002156 /* Preallocate the list of tuples, to avoid allocations during
2157 * the loop over the items, which could trigger GC, which
2158 * could resize the dict. :-(
2159 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002160 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002161 n = mp->ma_used;
2162 v = PyList_New(n);
2163 if (v == NULL)
2164 return NULL;
2165 for (i = 0; i < n; i++) {
2166 item = PyTuple_New(2);
2167 if (item == NULL) {
2168 Py_DECREF(v);
2169 return NULL;
2170 }
2171 PyList_SET_ITEM(v, i, item);
2172 }
2173 if (n != mp->ma_used) {
2174 /* Durnit. The allocations caused the dict to resize.
2175 * Just start over, this shouldn't normally happen.
2176 */
2177 Py_DECREF(v);
2178 goto again;
2179 }
2180 /* Nothing we do below makes any function calls. */
Victor Stinner742da042016-09-07 17:40:12 -07002181 ep = DK_ENTRIES(mp->ma_keys);
2182 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002183 if (mp->ma_values) {
2184 value_ptr = mp->ma_values;
2185 offset = sizeof(PyObject *);
2186 }
2187 else {
2188 value_ptr = &ep[0].me_value;
2189 offset = sizeof(PyDictKeyEntry);
2190 }
2191 for (i = 0, j = 0; i < size; i++) {
2192 PyObject *value = *value_ptr;
2193 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2194 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 key = ep[i].me_key;
2196 item = PyList_GET_ITEM(v, j);
2197 Py_INCREF(key);
2198 PyTuple_SET_ITEM(item, 0, key);
2199 Py_INCREF(value);
2200 PyTuple_SET_ITEM(item, 1, value);
2201 j++;
2202 }
2203 }
2204 assert(j == n);
2205 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002206}
2207
Larry Hastings5c661892014-01-24 06:17:25 -08002208/*[clinic input]
2209@classmethod
2210dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08002211 iterable: object
2212 value: object=None
2213 /
2214
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002215Create a new dictionary with keys from iterable and values set to value.
Larry Hastings5c661892014-01-24 06:17:25 -08002216[clinic start generated code]*/
2217
Larry Hastings5c661892014-01-24 06:17:25 -08002218static PyObject *
2219dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002220/*[clinic end generated code: output=8fb98e4b10384999 input=382ba4855d0f74c3]*/
Larry Hastings5c661892014-01-24 06:17:25 -08002221{
Eric Snow96c6af92015-05-29 22:21:39 -06002222 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002223}
2224
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002225static int
Victor Stinner742da042016-09-07 17:40:12 -07002226dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
2227 const char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002228{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002229 PyObject *arg = NULL;
2230 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002231
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002232 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002233 result = -1;
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002234 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002235 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002236 _Py_IDENTIFIER(keys);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002237 PyObject *func;
2238 if (_PyObject_LookupAttrId(arg, &PyId_keys, &func) < 0) {
2239 result = -1;
2240 }
2241 else if (func != NULL) {
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002242 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002243 result = PyDict_Merge(self, arg, 1);
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002244 }
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002245 else {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02002246 result = PyDict_MergeFromSeq2(self, arg, 1);
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002247 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002248 }
Serhiy Storchaka60c3d352017-11-11 16:19:56 +02002249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002250 if (result == 0 && kwds != NULL) {
2251 if (PyArg_ValidateKeywordArguments(kwds))
2252 result = PyDict_Merge(self, kwds, 1);
2253 else
2254 result = -1;
2255 }
2256 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002257}
2258
Victor Stinner91f0d4a2017-01-19 12:45:06 +01002259/* Note: dict.update() uses the METH_VARARGS|METH_KEYWORDS calling convention.
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002260 Using METH_FASTCALL|METH_KEYWORDS would make dict.update(**dict2) calls
2261 slower, see the issue #29312. */
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002262static PyObject *
2263dict_update(PyObject *self, PyObject *args, PyObject *kwds)
2264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265 if (dict_update_common(self, args, kwds, "update") != -1)
2266 Py_RETURN_NONE;
2267 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002268}
2269
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002270/* Update unconditionally replaces existing items.
2271 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00002272 otherwise it leaves existing items unchanged.
2273
2274 PyDict_{Update,Merge} update/merge from a mapping object.
2275
Tim Petersf582b822001-12-11 18:51:08 +00002276 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00002277 producing iterable objects of length 2.
2278*/
2279
Tim Petersf582b822001-12-11 18:51:08 +00002280int
Tim Peters1fc240e2001-10-26 05:06:50 +00002281PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
2282{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002283 PyObject *it; /* iter(seq2) */
2284 Py_ssize_t i; /* index into seq2 of current element */
2285 PyObject *item; /* seq2[i] */
2286 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00002287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002288 assert(d != NULL);
2289 assert(PyDict_Check(d));
2290 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00002291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292 it = PyObject_GetIter(seq2);
2293 if (it == NULL)
2294 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002296 for (i = 0; ; ++i) {
2297 PyObject *key, *value;
2298 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00002299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002300 fast = NULL;
2301 item = PyIter_Next(it);
2302 if (item == NULL) {
2303 if (PyErr_Occurred())
2304 goto Fail;
2305 break;
2306 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002308 /* Convert item to sequence, and verify length 2. */
2309 fast = PySequence_Fast(item, "");
2310 if (fast == NULL) {
2311 if (PyErr_ExceptionMatches(PyExc_TypeError))
2312 PyErr_Format(PyExc_TypeError,
2313 "cannot convert dictionary update "
2314 "sequence element #%zd to a sequence",
2315 i);
2316 goto Fail;
2317 }
2318 n = PySequence_Fast_GET_SIZE(fast);
2319 if (n != 2) {
2320 PyErr_Format(PyExc_ValueError,
2321 "dictionary update sequence element #%zd "
2322 "has length %zd; 2 is required",
2323 i, n);
2324 goto Fail;
2325 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002327 /* Update/merge with this (key, value) pair. */
2328 key = PySequence_Fast_GET_ITEM(fast, 0);
2329 value = PySequence_Fast_GET_ITEM(fast, 1);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002330 Py_INCREF(key);
2331 Py_INCREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002332 if (override || PyDict_GetItem(d, key) == NULL) {
2333 int status = PyDict_SetItem(d, key, value);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002334 if (status < 0) {
2335 Py_DECREF(key);
2336 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002337 goto Fail;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002338 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002339 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002340 Py_DECREF(key);
2341 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002342 Py_DECREF(fast);
2343 Py_DECREF(item);
2344 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 i = 0;
Victor Stinner611b0fa2016-09-14 15:02:01 +02002347 assert(_PyDict_CheckConsistency((PyDictObject *)d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002348 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002349Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002350 Py_XDECREF(item);
2351 Py_XDECREF(fast);
2352 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002353Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 Py_DECREF(it);
2355 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002356}
2357
doko@ubuntu.comc96df682016-10-11 08:04:02 +02002358static int
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002359dict_merge(PyObject *a, PyObject *b, int override)
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002360{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002361 PyDictObject *mp, *other;
2362 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -07002363 PyDictKeyEntry *entry, *ep0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002364
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002365 assert(0 <= override && override <= 2);
2366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 /* We accept for the argument either a concrete dictionary object,
2368 * or an abstract "mapping" object. For the former, we can do
2369 * things quite efficiently. For the latter, we only require that
2370 * PyMapping_Keys() and PyObject_GetItem() be supported.
2371 */
2372 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2373 PyErr_BadInternalCall();
2374 return -1;
2375 }
2376 mp = (PyDictObject*)a;
2377 if (PyDict_Check(b)) {
2378 other = (PyDictObject*)b;
2379 if (other == mp || other->ma_used == 0)
2380 /* a.update(a) or a.update({}); nothing to do */
2381 return 0;
2382 if (mp->ma_used == 0)
2383 /* Since the target dict is empty, PyDict_GetItem()
2384 * always returns NULL. Setting override to 1
2385 * skips the unnecessary test.
2386 */
2387 override = 1;
2388 /* Do one big resize at the start, rather than
2389 * incrementally resizing as we insert new items. Expect
2390 * that there will be no (or few) overlapping keys.
2391 */
INADA Naokib1152be2016-10-27 19:26:50 +09002392 if (USABLE_FRACTION(mp->ma_keys->dk_size) < other->ma_used) {
2393 if (dictresize(mp, ESTIMATE_SIZE(mp->ma_used + other->ma_used))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 return -1;
INADA Naokib1152be2016-10-27 19:26:50 +09002395 }
2396 }
Victor Stinner742da042016-09-07 17:40:12 -07002397 ep0 = DK_ENTRIES(other->ma_keys);
2398 for (i = 0, n = other->ma_keys->dk_nentries; i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002399 PyObject *key, *value;
2400 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002401 entry = &ep0[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002402 key = entry->me_key;
2403 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002404 if (other->ma_values)
2405 value = other->ma_values[i];
2406 else
2407 value = entry->me_value;
2408
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002409 if (value != NULL) {
2410 int err = 0;
2411 Py_INCREF(key);
2412 Py_INCREF(value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002413 if (override == 1)
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002414 err = insertdict(mp, key, hash, value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002415 else if (_PyDict_GetItem_KnownHash(a, key, hash) == NULL) {
2416 if (PyErr_Occurred()) {
2417 Py_DECREF(value);
2418 Py_DECREF(key);
2419 return -1;
2420 }
2421 err = insertdict(mp, key, hash, value);
2422 }
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002423 else if (override != 0) {
2424 _PyErr_SetKeyError(key);
2425 Py_DECREF(value);
2426 Py_DECREF(key);
2427 return -1;
2428 }
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002429 Py_DECREF(value);
2430 Py_DECREF(key);
2431 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002433
Victor Stinner742da042016-09-07 17:40:12 -07002434 if (n != other->ma_keys->dk_nentries) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002435 PyErr_SetString(PyExc_RuntimeError,
2436 "dict mutated during update");
2437 return -1;
2438 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 }
2440 }
2441 }
2442 else {
2443 /* Do it the generic, slower way */
2444 PyObject *keys = PyMapping_Keys(b);
2445 PyObject *iter;
2446 PyObject *key, *value;
2447 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 if (keys == NULL)
2450 /* Docstring says this is equivalent to E.keys() so
2451 * if E doesn't have a .keys() method we want
2452 * AttributeError to percolate up. Might as well
2453 * do the same for any other error.
2454 */
2455 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002457 iter = PyObject_GetIter(keys);
2458 Py_DECREF(keys);
2459 if (iter == NULL)
2460 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002462 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002463 if (override != 1 && PyDict_GetItem(a, key) != NULL) {
2464 if (override != 0) {
2465 _PyErr_SetKeyError(key);
2466 Py_DECREF(key);
2467 Py_DECREF(iter);
2468 return -1;
2469 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002470 Py_DECREF(key);
2471 continue;
2472 }
2473 value = PyObject_GetItem(b, key);
2474 if (value == NULL) {
2475 Py_DECREF(iter);
2476 Py_DECREF(key);
2477 return -1;
2478 }
2479 status = PyDict_SetItem(a, key, value);
2480 Py_DECREF(key);
2481 Py_DECREF(value);
2482 if (status < 0) {
2483 Py_DECREF(iter);
2484 return -1;
2485 }
2486 }
2487 Py_DECREF(iter);
2488 if (PyErr_Occurred())
2489 /* Iterator completed, via error */
2490 return -1;
2491 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02002492 assert(_PyDict_CheckConsistency((PyDictObject *)a));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002493 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002494}
2495
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002496int
2497PyDict_Update(PyObject *a, PyObject *b)
2498{
2499 return dict_merge(a, b, 1);
2500}
2501
2502int
2503PyDict_Merge(PyObject *a, PyObject *b, int override)
2504{
2505 /* XXX Deprecate override not in (0, 1). */
2506 return dict_merge(a, b, override != 0);
2507}
2508
2509int
2510_PyDict_MergeEx(PyObject *a, PyObject *b, int override)
2511{
2512 return dict_merge(a, b, override);
2513}
2514
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002515static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002516dict_copy(PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002517{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002518 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002519}
2520
2521PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002522PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002523{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002524 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002525 PyDictObject *mp;
2526 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002528 if (o == NULL || !PyDict_Check(o)) {
2529 PyErr_BadInternalCall();
2530 return NULL;
2531 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002532
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002533 mp = (PyDictObject *)o;
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002534 if (mp->ma_used == 0) {
2535 /* The dict is empty; just return a new dict. */
2536 return PyDict_New();
2537 }
2538
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002539 if (_PyDict_HasSplitTable(mp)) {
2540 PyDictObject *split_copy;
Victor Stinner742da042016-09-07 17:40:12 -07002541 Py_ssize_t size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
2542 PyObject **newvalues;
2543 newvalues = new_values(size);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002544 if (newvalues == NULL)
2545 return PyErr_NoMemory();
2546 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2547 if (split_copy == NULL) {
2548 free_values(newvalues);
2549 return NULL;
2550 }
2551 split_copy->ma_values = newvalues;
2552 split_copy->ma_keys = mp->ma_keys;
2553 split_copy->ma_used = mp->ma_used;
2554 DK_INCREF(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002555 for (i = 0, n = size; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002556 PyObject *value = mp->ma_values[i];
2557 Py_XINCREF(value);
2558 split_copy->ma_values[i] = value;
2559 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002560 if (_PyObject_GC_IS_TRACKED(mp))
2561 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002562 return (PyObject *)split_copy;
2563 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002564
2565 if (PyDict_CheckExact(mp) && mp->ma_values == NULL &&
2566 (mp->ma_used >= (mp->ma_keys->dk_nentries * 2) / 3))
2567 {
2568 /* Use fast-copy if:
2569
2570 (1) 'mp' is an instance of a subclassed dict; and
2571
2572 (2) 'mp' is not a split-dict; and
2573
2574 (3) if 'mp' is non-compact ('del' operation does not resize dicts),
2575 do fast-copy only if it has at most 1/3 non-used keys.
2576
2577 The last condition (3) is important to guard against a pathalogical
2578 case when a large dict is almost emptied with multiple del/pop
2579 operations and copied after that. In cases like this, we defer to
2580 PyDict_Merge, which produces a compacted copy.
2581 */
2582 return clone_combined_dict(mp);
2583 }
2584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002585 copy = PyDict_New();
2586 if (copy == NULL)
2587 return NULL;
2588 if (PyDict_Merge(copy, o, 1) == 0)
2589 return copy;
2590 Py_DECREF(copy);
2591 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002592}
2593
Martin v. Löwis18e16552006-02-15 17:27:45 +00002594Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002595PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002596{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002597 if (mp == NULL || !PyDict_Check(mp)) {
2598 PyErr_BadInternalCall();
2599 return -1;
2600 }
2601 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002602}
2603
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002604PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002605PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002606{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002607 if (mp == NULL || !PyDict_Check(mp)) {
2608 PyErr_BadInternalCall();
2609 return NULL;
2610 }
2611 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002612}
2613
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002614PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002615PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002616{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002617 if (mp == NULL || !PyDict_Check(mp)) {
2618 PyErr_BadInternalCall();
2619 return NULL;
2620 }
2621 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002622}
2623
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002624PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002625PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002626{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002627 if (mp == NULL || !PyDict_Check(mp)) {
2628 PyErr_BadInternalCall();
2629 return NULL;
2630 }
2631 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002632}
2633
Tim Peterse63415e2001-05-08 04:38:29 +00002634/* Return 1 if dicts equal, 0 if not, -1 if error.
2635 * Gets out as soon as any difference is detected.
2636 * Uses only Py_EQ comparison.
2637 */
2638static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002639dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002640{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002641 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002643 if (a->ma_used != b->ma_used)
2644 /* can't be equal if # of entries differ */
2645 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002646 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Victor Stinner742da042016-09-07 17:40:12 -07002647 for (i = 0; i < a->ma_keys->dk_nentries; i++) {
2648 PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002649 PyObject *aval;
2650 if (a->ma_values)
2651 aval = a->ma_values[i];
2652 else
2653 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002654 if (aval != NULL) {
2655 int cmp;
2656 PyObject *bval;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002657 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002658 /* temporarily bump aval's refcount to ensure it stays
2659 alive until we're done with it */
2660 Py_INCREF(aval);
2661 /* ditto for key */
2662 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002663 /* reuse the known hash value */
INADA Naoki778928b2017-08-03 23:45:15 +09002664 b->ma_keys->dk_lookup(b, key, ep->me_hash, &bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002665 if (bval == NULL) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002666 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002667 Py_DECREF(aval);
2668 if (PyErr_Occurred())
2669 return -1;
2670 return 0;
2671 }
2672 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002673 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002674 Py_DECREF(aval);
2675 if (cmp <= 0) /* error or not equal */
2676 return cmp;
2677 }
2678 }
2679 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002680}
Tim Peterse63415e2001-05-08 04:38:29 +00002681
2682static PyObject *
2683dict_richcompare(PyObject *v, PyObject *w, int op)
2684{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002685 int cmp;
2686 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002688 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2689 res = Py_NotImplemented;
2690 }
2691 else if (op == Py_EQ || op == Py_NE) {
2692 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2693 if (cmp < 0)
2694 return NULL;
2695 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2696 }
2697 else
2698 res = Py_NotImplemented;
2699 Py_INCREF(res);
2700 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002701}
Tim Peterse63415e2001-05-08 04:38:29 +00002702
Larry Hastings61272b72014-01-07 12:41:53 -08002703/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002704
2705@coexist
2706dict.__contains__
2707
2708 key: object
2709 /
2710
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002711True if the dictionary has the specified key, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002712[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002713
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002714static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002715dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka19d25972017-02-04 08:05:07 +02002716/*[clinic end generated code: output=a3d03db709ed6e6b input=fe1cb42ad831e820]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002717{
Larry Hastingsc2047262014-01-25 20:43:29 -08002718 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002719 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002720 Py_ssize_t ix;
INADA Naokiba609772016-12-07 20:41:42 +09002721 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002723 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002724 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002725 hash = PyObject_Hash(key);
2726 if (hash == -1)
2727 return NULL;
2728 }
INADA Naoki778928b2017-08-03 23:45:15 +09002729 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002730 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002731 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002732 if (ix == DKIX_EMPTY || value == NULL)
Victor Stinner742da042016-09-07 17:40:12 -07002733 Py_RETURN_FALSE;
2734 Py_RETURN_TRUE;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002735}
2736
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002737/*[clinic input]
2738dict.get
2739
2740 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002741 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002742 /
2743
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002744Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002745[clinic start generated code]*/
2746
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002747static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002748dict_get_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002749/*[clinic end generated code: output=bba707729dee05bf input=279ddb5790b6b107]*/
Barry Warsawc38c5da1997-10-06 17:49:20 +00002750{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002752 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002753 Py_ssize_t ix;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002755 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002756 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 hash = PyObject_Hash(key);
2758 if (hash == -1)
2759 return NULL;
2760 }
INADA Naoki778928b2017-08-03 23:45:15 +09002761 ix = (self->ma_keys->dk_lookup) (self, key, hash, &val);
Victor Stinner742da042016-09-07 17:40:12 -07002762 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002763 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002764 if (ix == DKIX_EMPTY || val == NULL) {
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002765 val = default_value;
INADA Naokiba609772016-12-07 20:41:42 +09002766 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002767 Py_INCREF(val);
2768 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002769}
2770
Benjamin Peterson00e98862013-03-07 22:16:29 -05002771PyObject *
2772PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002773{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002774 PyDictObject *mp = (PyDictObject *)d;
INADA Naoki93f26f72016-11-02 18:45:16 +09002775 PyObject *value;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002776 Py_hash_t hash;
Guido van Rossum164452c2000-08-08 16:12:54 +00002777
Benjamin Peterson00e98862013-03-07 22:16:29 -05002778 if (!PyDict_Check(d)) {
2779 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002780 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002781 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002782
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002783 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002784 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002785 hash = PyObject_Hash(key);
2786 if (hash == -1)
2787 return NULL;
2788 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002789
2790 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
2791 if (insertion_resize(mp) < 0)
2792 return NULL;
2793 }
2794
INADA Naoki778928b2017-08-03 23:45:15 +09002795 Py_ssize_t ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002796 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002797 return NULL;
INADA Naoki93f26f72016-11-02 18:45:16 +09002798
2799 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09002800 ((ix >= 0 && value == NULL && mp->ma_used != ix) ||
INADA Naoki93f26f72016-11-02 18:45:16 +09002801 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
2802 if (insertion_resize(mp) < 0) {
2803 return NULL;
2804 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002805 ix = DKIX_EMPTY;
2806 }
2807
2808 if (ix == DKIX_EMPTY) {
2809 PyDictKeyEntry *ep, *ep0;
2810 value = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002811 if (mp->ma_keys->dk_usable <= 0) {
Victor Stinner3c336c52016-09-12 14:17:40 +02002812 if (insertion_resize(mp) < 0) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002813 return NULL;
Victor Stinner3c336c52016-09-12 14:17:40 +02002814 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002815 }
INADA Naoki778928b2017-08-03 23:45:15 +09002816 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naoki93f26f72016-11-02 18:45:16 +09002817 ep0 = DK_ENTRIES(mp->ma_keys);
2818 ep = &ep0[mp->ma_keys->dk_nentries];
2819 dk_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002820 Py_INCREF(key);
INADA Naoki93f26f72016-11-02 18:45:16 +09002821 Py_INCREF(value);
2822 MAINTAIN_TRACKING(mp, key, value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002823 ep->me_key = key;
2824 ep->me_hash = hash;
INADA Naokiba609772016-12-07 20:41:42 +09002825 if (_PyDict_HasSplitTable(mp)) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002826 assert(mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
2827 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Victor Stinner742da042016-09-07 17:40:12 -07002828 }
2829 else {
INADA Naoki93f26f72016-11-02 18:45:16 +09002830 ep->me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07002831 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002832 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002833 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naoki93f26f72016-11-02 18:45:16 +09002834 mp->ma_keys->dk_usable--;
2835 mp->ma_keys->dk_nentries++;
2836 assert(mp->ma_keys->dk_usable >= 0);
2837 }
INADA Naokiba609772016-12-07 20:41:42 +09002838 else if (value == NULL) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002839 value = defaultobj;
2840 assert(_PyDict_HasSplitTable(mp));
2841 assert(ix == mp->ma_used);
2842 Py_INCREF(value);
2843 MAINTAIN_TRACKING(mp, key, value);
INADA Naokiba609772016-12-07 20:41:42 +09002844 mp->ma_values[ix] = value;
INADA Naoki93f26f72016-11-02 18:45:16 +09002845 mp->ma_used++;
2846 mp->ma_version_tag = DICT_NEXT_VERSION();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002847 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002848
2849 assert(_PyDict_CheckConsistency(mp));
2850 return value;
Guido van Rossum164452c2000-08-08 16:12:54 +00002851}
2852
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002853/*[clinic input]
2854dict.setdefault
2855
2856 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002857 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002858 /
2859
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002860Insert key with a value of default if key is not in the dictionary.
2861
2862Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002863[clinic start generated code]*/
2864
Benjamin Peterson00e98862013-03-07 22:16:29 -05002865static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002866dict_setdefault_impl(PyDictObject *self, PyObject *key,
2867 PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002868/*[clinic end generated code: output=f8c1101ebf69e220 input=0f063756e815fd9d]*/
Benjamin Peterson00e98862013-03-07 22:16:29 -05002869{
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002870 PyObject *val;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002871
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002872 val = PyDict_SetDefault((PyObject *)self, key, default_value);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002873 Py_XINCREF(val);
2874 return val;
2875}
Guido van Rossum164452c2000-08-08 16:12:54 +00002876
2877static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002878dict_clear(PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002879{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 PyDict_Clear((PyObject *)mp);
2881 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002882}
2883
Guido van Rossumba6ab842000-12-12 22:02:18 +00002884static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002885dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002886{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002887 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00002888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002889 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2890 return NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06002891
Yury Selivanov684ef2c2016-10-28 19:01:21 -04002892 return _PyDict_Pop((PyObject*)mp, key, deflt);
Guido van Rossume027d982002-04-12 15:11:59 +00002893}
2894
2895static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002896dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002897{
Victor Stinner742da042016-09-07 17:40:12 -07002898 Py_ssize_t i, j;
2899 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002900 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002902 /* Allocate the result tuple before checking the size. Believe it
2903 * or not, this allocation could trigger a garbage collection which
2904 * could empty the dict, so if we checked the size first and that
2905 * happened, the result would be an infinite loop (searching for an
2906 * entry that no longer exists). Note that the usual popitem()
2907 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2908 * tuple away if the dict *is* empty isn't a significant
2909 * inefficiency -- possible, but unlikely in practice.
2910 */
2911 res = PyTuple_New(2);
2912 if (res == NULL)
2913 return NULL;
2914 if (mp->ma_used == 0) {
2915 Py_DECREF(res);
2916 PyErr_SetString(PyExc_KeyError,
2917 "popitem(): dictionary is empty");
2918 return NULL;
2919 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002920 /* Convert split table to combined table */
2921 if (mp->ma_keys->dk_lookup == lookdict_split) {
2922 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2923 Py_DECREF(res);
2924 return NULL;
2925 }
2926 }
2927 ENSURE_ALLOWS_DELETIONS(mp);
Victor Stinner742da042016-09-07 17:40:12 -07002928
2929 /* Pop last item */
2930 ep0 = DK_ENTRIES(mp->ma_keys);
2931 i = mp->ma_keys->dk_nentries - 1;
2932 while (i >= 0 && ep0[i].me_value == NULL) {
2933 i--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002934 }
Victor Stinner742da042016-09-07 17:40:12 -07002935 assert(i >= 0);
2936
2937 ep = &ep0[i];
2938 j = lookdict_index(mp->ma_keys, ep->me_hash, i);
2939 assert(j >= 0);
2940 assert(dk_get_index(mp->ma_keys, j) == i);
2941 dk_set_index(mp->ma_keys, j, DKIX_DUMMY);
2942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002943 PyTuple_SET_ITEM(res, 0, ep->me_key);
2944 PyTuple_SET_ITEM(res, 1, ep->me_value);
Victor Stinner742da042016-09-07 17:40:12 -07002945 ep->me_key = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 ep->me_value = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002947 /* We can't dk_usable++ since there is DKIX_DUMMY in indices */
2948 mp->ma_keys->dk_nentries = i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002949 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002950 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02002951 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002952 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002953}
2954
Jeremy Hylton8caad492000-06-23 14:18:11 +00002955static int
2956dict_traverse(PyObject *op, visitproc visit, void *arg)
2957{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002958 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson55f44522016-09-05 12:12:59 -07002959 PyDictKeysObject *keys = mp->ma_keys;
Serhiy Storchaka46825d22016-09-26 21:29:34 +03002960 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Victor Stinner742da042016-09-07 17:40:12 -07002961 Py_ssize_t i, n = keys->dk_nentries;
2962
Benjamin Peterson55f44522016-09-05 12:12:59 -07002963 if (keys->dk_lookup == lookdict) {
2964 for (i = 0; i < n; i++) {
2965 if (entries[i].me_value != NULL) {
2966 Py_VISIT(entries[i].me_value);
2967 Py_VISIT(entries[i].me_key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002968 }
2969 }
Victor Stinner742da042016-09-07 17:40:12 -07002970 }
2971 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002972 if (mp->ma_values != NULL) {
Benjamin Peterson55f44522016-09-05 12:12:59 -07002973 for (i = 0; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002974 Py_VISIT(mp->ma_values[i]);
2975 }
2976 }
2977 else {
Benjamin Peterson55f44522016-09-05 12:12:59 -07002978 for (i = 0; i < n; i++) {
2979 Py_VISIT(entries[i].me_value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002980 }
2981 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002982 }
2983 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002984}
2985
2986static int
2987dict_tp_clear(PyObject *op)
2988{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002989 PyDict_Clear(op);
2990 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002991}
2992
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002993static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002994
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002995Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06002996_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002997{
Victor Stinner742da042016-09-07 17:40:12 -07002998 Py_ssize_t size, usable, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002999
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003000 size = DK_SIZE(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07003001 usable = USABLE_FRACTION(size);
3002
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02003003 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003004 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -07003005 res += usable * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003006 /* If the dictionary is split, the keys portion is accounted-for
3007 in the type object. */
3008 if (mp->ma_keys->dk_refcnt == 1)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003009 res += (sizeof(PyDictKeysObject)
3010 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
3011 + DK_IXSIZE(mp->ma_keys) * size
3012 + sizeof(PyDictKeyEntry) * usable);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003013 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003014}
3015
3016Py_ssize_t
3017_PyDict_KeysSize(PyDictKeysObject *keys)
3018{
Victor Stinner98ee9d52016-09-08 09:33:56 -07003019 return (sizeof(PyDictKeysObject)
3020 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
3021 + DK_IXSIZE(keys) * DK_SIZE(keys)
3022 + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003023}
3024
doko@ubuntu.com17210f52016-01-14 14:04:59 +01003025static PyObject *
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003026dict_sizeof(PyDictObject *mp)
3027{
3028 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
3029}
3030
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00003031PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
3032
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003033PyDoc_STRVAR(sizeof__doc__,
3034"D.__sizeof__() -> size of D in memory, in bytes");
3035
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003036PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00003037"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00003038If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00003039
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003040PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00003041"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000030422-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003043
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003044PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04003045"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
3046If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
3047If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
3048In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00003049
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003050PyDoc_STRVAR(clear__doc__,
3051"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003052
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003053PyDoc_STRVAR(copy__doc__,
3054"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00003055
Guido van Rossumb90c8482007-02-10 01:11:45 +00003056/* Forward */
3057static PyObject *dictkeys_new(PyObject *);
3058static PyObject *dictitems_new(PyObject *);
3059static PyObject *dictvalues_new(PyObject *);
3060
Guido van Rossum45c85d12007-07-27 16:31:40 +00003061PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003062 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003063PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003064 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003065PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003066 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00003067
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003068static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07003069 DICT___CONTAINS___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003070 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
3071 getitem__doc__},
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003072 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003073 sizeof__doc__},
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003074 DICT_GET_METHODDEF
3075 DICT_SETDEFAULT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003076 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
3077 pop__doc__},
3078 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
3079 popitem__doc__},
3080 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
3081 keys__doc__},
3082 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
3083 items__doc__},
3084 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
3085 values__doc__},
3086 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
3087 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08003088 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003089 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
3090 clear__doc__},
3091 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
3092 copy__doc__},
3093 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003094};
3095
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003096/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00003097int
3098PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003099{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003100 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07003101 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003102 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003103 PyObject *value;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003104
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003105 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003106 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003107 hash = PyObject_Hash(key);
3108 if (hash == -1)
3109 return -1;
3110 }
INADA Naoki778928b2017-08-03 23:45:15 +09003111 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003112 if (ix == DKIX_ERROR)
3113 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003114 return (ix != DKIX_EMPTY && value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003115}
3116
Thomas Wouterscf297e42007-02-23 15:07:44 +00003117/* Internal version of PyDict_Contains used when the hash value is already known */
3118int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003119_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00003120{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003121 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003122 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07003123 Py_ssize_t ix;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003124
INADA Naoki778928b2017-08-03 23:45:15 +09003125 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003126 if (ix == DKIX_ERROR)
3127 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003128 return (ix != DKIX_EMPTY && value != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00003129}
3130
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003131/* Hack to implement "key in dict" */
3132static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003133 0, /* sq_length */
3134 0, /* sq_concat */
3135 0, /* sq_repeat */
3136 0, /* sq_item */
3137 0, /* sq_slice */
3138 0, /* sq_ass_item */
3139 0, /* sq_ass_slice */
3140 PyDict_Contains, /* sq_contains */
3141 0, /* sq_inplace_concat */
3142 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003143};
3144
Guido van Rossum09e563a2001-05-01 12:10:21 +00003145static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003146dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3147{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003148 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003149 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003151 assert(type != NULL && type->tp_alloc != NULL);
3152 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02003153 if (self == NULL)
3154 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003155 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003156
Victor Stinnera9f61a52013-07-16 22:17:26 +02003157 /* The object has been implicitly tracked by tp_alloc */
3158 if (type == &PyDict_Type)
3159 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003160
3161 d->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003162 d->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07003163 d->ma_keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003164 if (d->ma_keys == NULL) {
3165 Py_DECREF(self);
3166 return NULL;
3167 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02003168 assert(_PyDict_CheckConsistency(d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003169 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003170}
3171
Tim Peters25786c02001-09-02 08:22:48 +00003172static int
3173dict_init(PyObject *self, PyObject *args, PyObject *kwds)
3174{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003175 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00003176}
3177
Tim Peters6d6c1a32001-08-02 04:15:00 +00003178static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003179dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00003180{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003181 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003182}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003183
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003184PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00003185"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00003186"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003187" (key, value) pairs\n"
3188"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00003189" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003190" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00003191" d[k] = v\n"
3192"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
3193" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00003194
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003195PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003196 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3197 "dict",
3198 sizeof(PyDictObject),
3199 0,
3200 (destructor)dict_dealloc, /* tp_dealloc */
3201 0, /* tp_print */
3202 0, /* tp_getattr */
3203 0, /* tp_setattr */
3204 0, /* tp_reserved */
3205 (reprfunc)dict_repr, /* tp_repr */
3206 0, /* tp_as_number */
3207 &dict_as_sequence, /* tp_as_sequence */
3208 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003209 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003210 0, /* tp_call */
3211 0, /* tp_str */
3212 PyObject_GenericGetAttr, /* tp_getattro */
3213 0, /* tp_setattro */
3214 0, /* tp_as_buffer */
3215 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3216 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
3217 dictionary_doc, /* tp_doc */
3218 dict_traverse, /* tp_traverse */
3219 dict_tp_clear, /* tp_clear */
3220 dict_richcompare, /* tp_richcompare */
3221 0, /* tp_weaklistoffset */
3222 (getiterfunc)dict_iter, /* tp_iter */
3223 0, /* tp_iternext */
3224 mapp_methods, /* tp_methods */
3225 0, /* tp_members */
3226 0, /* tp_getset */
3227 0, /* tp_base */
3228 0, /* tp_dict */
3229 0, /* tp_descr_get */
3230 0, /* tp_descr_set */
3231 0, /* tp_dictoffset */
3232 dict_init, /* tp_init */
3233 PyType_GenericAlloc, /* tp_alloc */
3234 dict_new, /* tp_new */
3235 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003236};
3237
Victor Stinner3c1e4812012-03-26 22:10:51 +02003238PyObject *
3239_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
3240{
3241 PyObject *kv;
3242 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02003243 if (kv == NULL) {
3244 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003245 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02003246 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02003247 return PyDict_GetItem(dp, kv);
3248}
3249
Guido van Rossum3cca2451997-05-16 14:23:33 +00003250/* For backward compatibility with old dictionary interface */
3251
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003252PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003253PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003254{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003255 PyObject *kv, *rv;
3256 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003257 if (kv == NULL) {
3258 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003259 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003260 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003261 rv = PyDict_GetItem(v, kv);
3262 Py_DECREF(kv);
3263 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003264}
3265
3266int
Victor Stinner3c1e4812012-03-26 22:10:51 +02003267_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
3268{
3269 PyObject *kv;
3270 kv = _PyUnicode_FromId(key); /* borrowed */
3271 if (kv == NULL)
3272 return -1;
3273 return PyDict_SetItem(v, kv, item);
3274}
3275
3276int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003277PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003278{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003279 PyObject *kv;
3280 int err;
3281 kv = PyUnicode_FromString(key);
3282 if (kv == NULL)
3283 return -1;
3284 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
3285 err = PyDict_SetItem(v, kv, item);
3286 Py_DECREF(kv);
3287 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003288}
3289
3290int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01003291_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
3292{
3293 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3294 if (kv == NULL)
3295 return -1;
3296 return PyDict_DelItem(v, kv);
3297}
3298
3299int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003300PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003301{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003302 PyObject *kv;
3303 int err;
3304 kv = PyUnicode_FromString(key);
3305 if (kv == NULL)
3306 return -1;
3307 err = PyDict_DelItem(v, kv);
3308 Py_DECREF(kv);
3309 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003310}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003311
Raymond Hettinger019a1482004-03-18 02:41:19 +00003312/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003313
3314typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003315 PyObject_HEAD
3316 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
3317 Py_ssize_t di_used;
3318 Py_ssize_t di_pos;
3319 PyObject* di_result; /* reusable result tuple for iteritems */
3320 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003321} dictiterobject;
3322
3323static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003324dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003325{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003326 dictiterobject *di;
3327 di = PyObject_GC_New(dictiterobject, itertype);
3328 if (di == NULL)
3329 return NULL;
3330 Py_INCREF(dict);
3331 di->di_dict = dict;
3332 di->di_used = dict->ma_used;
3333 di->di_pos = 0;
3334 di->len = dict->ma_used;
3335 if (itertype == &PyDictIterItem_Type) {
3336 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
3337 if (di->di_result == NULL) {
3338 Py_DECREF(di);
3339 return NULL;
3340 }
3341 }
3342 else
3343 di->di_result = NULL;
3344 _PyObject_GC_TRACK(di);
3345 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003346}
3347
3348static void
3349dictiter_dealloc(dictiterobject *di)
3350{
INADA Naokia6296d32017-08-24 14:55:17 +09003351 /* bpo-31095: UnTrack is needed before calling any callbacks */
3352 _PyObject_GC_UNTRACK(di);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003353 Py_XDECREF(di->di_dict);
3354 Py_XDECREF(di->di_result);
3355 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003356}
3357
3358static int
3359dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
3360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003361 Py_VISIT(di->di_dict);
3362 Py_VISIT(di->di_result);
3363 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003364}
3365
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003366static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003367dictiter_len(dictiterobject *di)
3368{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003369 Py_ssize_t len = 0;
3370 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
3371 len = di->len;
3372 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003373}
3374
Guido van Rossumb90c8482007-02-10 01:11:45 +00003375PyDoc_STRVAR(length_hint_doc,
3376 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003377
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003378static PyObject *
3379dictiter_reduce(dictiterobject *di);
3380
3381PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3382
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003383static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003384 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
3385 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003386 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
3387 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003388 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003389};
3390
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003391static PyObject*
3392dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00003393{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003394 PyObject *key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003395 Py_ssize_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003396 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003397 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00003398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003399 if (d == NULL)
3400 return NULL;
3401 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00003402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003403 if (di->di_used != d->ma_used) {
3404 PyErr_SetString(PyExc_RuntimeError,
3405 "dictionary changed size during iteration");
3406 di->di_used = -1; /* Make this state sticky */
3407 return NULL;
3408 }
Guido van Rossum2147df72002-07-16 20:30:22 +00003409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003410 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003411 k = d->ma_keys;
INADA Naokica2d8be2016-11-04 16:59:10 +09003412 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003413 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003414 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003415 goto fail;
3416 key = DK_ENTRIES(k)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003417 assert(d->ma_values[i] != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003418 }
3419 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003420 Py_ssize_t n = k->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003421 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3422 while (i < n && entry_ptr->me_value == NULL) {
3423 entry_ptr++;
3424 i++;
3425 }
3426 if (i >= n)
3427 goto fail;
3428 key = entry_ptr->me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003429 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003430 di->di_pos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003431 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003432 Py_INCREF(key);
3433 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003434
3435fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003436 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003437 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003438 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003439}
3440
Raymond Hettinger019a1482004-03-18 02:41:19 +00003441PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003442 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3443 "dict_keyiterator", /* tp_name */
3444 sizeof(dictiterobject), /* tp_basicsize */
3445 0, /* tp_itemsize */
3446 /* methods */
3447 (destructor)dictiter_dealloc, /* tp_dealloc */
3448 0, /* tp_print */
3449 0, /* tp_getattr */
3450 0, /* tp_setattr */
3451 0, /* tp_reserved */
3452 0, /* tp_repr */
3453 0, /* tp_as_number */
3454 0, /* tp_as_sequence */
3455 0, /* tp_as_mapping */
3456 0, /* tp_hash */
3457 0, /* tp_call */
3458 0, /* tp_str */
3459 PyObject_GenericGetAttr, /* tp_getattro */
3460 0, /* tp_setattro */
3461 0, /* tp_as_buffer */
3462 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3463 0, /* tp_doc */
3464 (traverseproc)dictiter_traverse, /* tp_traverse */
3465 0, /* tp_clear */
3466 0, /* tp_richcompare */
3467 0, /* tp_weaklistoffset */
3468 PyObject_SelfIter, /* tp_iter */
3469 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3470 dictiter_methods, /* tp_methods */
3471 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003472};
3473
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003474static PyObject *
3475dictiter_iternextvalue(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003476{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003477 PyObject *value;
INADA Naokica2d8be2016-11-04 16:59:10 +09003478 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003479 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003481 if (d == NULL)
3482 return NULL;
3483 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003485 if (di->di_used != d->ma_used) {
3486 PyErr_SetString(PyExc_RuntimeError,
3487 "dictionary changed size during iteration");
3488 di->di_used = -1; /* Make this state sticky */
3489 return NULL;
3490 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003492 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003493 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003494 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003495 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003496 goto fail;
INADA Naokica2d8be2016-11-04 16:59:10 +09003497 value = d->ma_values[i];
3498 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003499 }
3500 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003501 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003502 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3503 while (i < n && entry_ptr->me_value == NULL) {
3504 entry_ptr++;
3505 i++;
3506 }
3507 if (i >= n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003508 goto fail;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003509 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003510 }
3511 di->di_pos = i+1;
3512 di->len--;
3513 Py_INCREF(value);
3514 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003515
3516fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003517 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003518 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003519 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003520}
3521
3522PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003523 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3524 "dict_valueiterator", /* tp_name */
3525 sizeof(dictiterobject), /* tp_basicsize */
3526 0, /* tp_itemsize */
3527 /* methods */
3528 (destructor)dictiter_dealloc, /* tp_dealloc */
3529 0, /* tp_print */
3530 0, /* tp_getattr */
3531 0, /* tp_setattr */
3532 0, /* tp_reserved */
3533 0, /* tp_repr */
3534 0, /* tp_as_number */
3535 0, /* tp_as_sequence */
3536 0, /* tp_as_mapping */
3537 0, /* tp_hash */
3538 0, /* tp_call */
3539 0, /* tp_str */
3540 PyObject_GenericGetAttr, /* tp_getattro */
3541 0, /* tp_setattro */
3542 0, /* tp_as_buffer */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003543 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003544 0, /* tp_doc */
3545 (traverseproc)dictiter_traverse, /* tp_traverse */
3546 0, /* tp_clear */
3547 0, /* tp_richcompare */
3548 0, /* tp_weaklistoffset */
3549 PyObject_SelfIter, /* tp_iter */
3550 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3551 dictiter_methods, /* tp_methods */
3552 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003553};
3554
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003555static PyObject *
3556dictiter_iternextitem(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003557{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003558 PyObject *key, *value, *result;
INADA Naokica2d8be2016-11-04 16:59:10 +09003559 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003560 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003562 if (d == NULL)
3563 return NULL;
3564 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003566 if (di->di_used != d->ma_used) {
3567 PyErr_SetString(PyExc_RuntimeError,
3568 "dictionary changed size during iteration");
3569 di->di_used = -1; /* Make this state sticky */
3570 return NULL;
3571 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003573 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003574 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003575 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003576 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003577 goto fail;
3578 key = DK_ENTRIES(d->ma_keys)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003579 value = d->ma_values[i];
3580 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003581 }
3582 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003583 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003584 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3585 while (i < n && entry_ptr->me_value == NULL) {
3586 entry_ptr++;
3587 i++;
3588 }
3589 if (i >= n)
3590 goto fail;
3591 key = entry_ptr->me_key;
3592 value = entry_ptr->me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003593 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003594 di->di_pos = i+1;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003595 di->len--;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003596 Py_INCREF(key);
3597 Py_INCREF(value);
3598 result = di->di_result;
3599 if (Py_REFCNT(result) == 1) {
3600 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3601 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3602 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3603 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003604 Py_INCREF(result);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003605 Py_DECREF(oldkey);
3606 Py_DECREF(oldvalue);
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003607 }
3608 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003609 result = PyTuple_New(2);
3610 if (result == NULL)
3611 return NULL;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003612 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3613 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003614 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003615 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003616
3617fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003618 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003619 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003620 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003621}
3622
3623PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003624 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3625 "dict_itemiterator", /* tp_name */
3626 sizeof(dictiterobject), /* tp_basicsize */
3627 0, /* tp_itemsize */
3628 /* methods */
3629 (destructor)dictiter_dealloc, /* tp_dealloc */
3630 0, /* tp_print */
3631 0, /* tp_getattr */
3632 0, /* tp_setattr */
3633 0, /* tp_reserved */
3634 0, /* tp_repr */
3635 0, /* tp_as_number */
3636 0, /* tp_as_sequence */
3637 0, /* tp_as_mapping */
3638 0, /* tp_hash */
3639 0, /* tp_call */
3640 0, /* tp_str */
3641 PyObject_GenericGetAttr, /* tp_getattro */
3642 0, /* tp_setattro */
3643 0, /* tp_as_buffer */
3644 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3645 0, /* tp_doc */
3646 (traverseproc)dictiter_traverse, /* tp_traverse */
3647 0, /* tp_clear */
3648 0, /* tp_richcompare */
3649 0, /* tp_weaklistoffset */
3650 PyObject_SelfIter, /* tp_iter */
3651 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3652 dictiter_methods, /* tp_methods */
3653 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003654};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003655
3656
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003657static PyObject *
3658dictiter_reduce(dictiterobject *di)
3659{
3660 PyObject *list;
3661 dictiterobject tmp;
3662
3663 list = PyList_New(0);
3664 if (!list)
3665 return NULL;
3666
3667 /* copy the itertor state */
3668 tmp = *di;
3669 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003670
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003671 /* iterate the temporary into a list */
3672 for(;;) {
3673 PyObject *element = 0;
3674 if (Py_TYPE(di) == &PyDictIterItem_Type)
3675 element = dictiter_iternextitem(&tmp);
3676 else if (Py_TYPE(di) == &PyDictIterKey_Type)
3677 element = dictiter_iternextkey(&tmp);
3678 else if (Py_TYPE(di) == &PyDictIterValue_Type)
3679 element = dictiter_iternextvalue(&tmp);
3680 else
Barry Warsawb2e57942017-09-14 18:13:16 -07003681 Py_UNREACHABLE();
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003682 if (element) {
3683 if (PyList_Append(list, element)) {
3684 Py_DECREF(element);
3685 Py_DECREF(list);
3686 Py_XDECREF(tmp.di_dict);
3687 return NULL;
3688 }
3689 Py_DECREF(element);
3690 } else
3691 break;
3692 }
3693 Py_XDECREF(tmp.di_dict);
3694 /* check for error */
3695 if (tmp.di_dict != NULL) {
3696 /* we have an error */
3697 Py_DECREF(list);
3698 return NULL;
3699 }
Antoine Pitroua7013882012-04-05 00:04:20 +02003700 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003701}
3702
Guido van Rossum3ac67412007-02-10 18:55:06 +00003703/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003704/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003705/***********************************************/
3706
Guido van Rossumb90c8482007-02-10 01:11:45 +00003707/* The instance lay-out is the same for all three; but the type differs. */
3708
Guido van Rossumb90c8482007-02-10 01:11:45 +00003709static void
Eric Snow96c6af92015-05-29 22:21:39 -06003710dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003711{
INADA Naokia6296d32017-08-24 14:55:17 +09003712 /* bpo-31095: UnTrack is needed before calling any callbacks */
3713 _PyObject_GC_UNTRACK(dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003714 Py_XDECREF(dv->dv_dict);
3715 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003716}
3717
3718static int
Eric Snow96c6af92015-05-29 22:21:39 -06003719dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003720{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003721 Py_VISIT(dv->dv_dict);
3722 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003723}
3724
Guido van Rossum83825ac2007-02-10 04:54:19 +00003725static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003726dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003727{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003728 Py_ssize_t len = 0;
3729 if (dv->dv_dict != NULL)
3730 len = dv->dv_dict->ma_used;
3731 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003732}
3733
Eric Snow96c6af92015-05-29 22:21:39 -06003734PyObject *
3735_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003736{
Eric Snow96c6af92015-05-29 22:21:39 -06003737 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003738 if (dict == NULL) {
3739 PyErr_BadInternalCall();
3740 return NULL;
3741 }
3742 if (!PyDict_Check(dict)) {
3743 /* XXX Get rid of this restriction later */
3744 PyErr_Format(PyExc_TypeError,
3745 "%s() requires a dict argument, not '%s'",
3746 type->tp_name, dict->ob_type->tp_name);
3747 return NULL;
3748 }
Eric Snow96c6af92015-05-29 22:21:39 -06003749 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003750 if (dv == NULL)
3751 return NULL;
3752 Py_INCREF(dict);
3753 dv->dv_dict = (PyDictObject *)dict;
3754 _PyObject_GC_TRACK(dv);
3755 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003756}
3757
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003758/* TODO(guido): The views objects are not complete:
3759
3760 * support more set operations
3761 * support arbitrary mappings?
3762 - either these should be static or exported in dictobject.h
3763 - if public then they should probably be in builtins
3764*/
3765
Guido van Rossumaac530c2007-08-24 22:33:45 +00003766/* Return 1 if self is a subset of other, iterating over self;
3767 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003768static int
3769all_contained_in(PyObject *self, PyObject *other)
3770{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003771 PyObject *iter = PyObject_GetIter(self);
3772 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003774 if (iter == NULL)
3775 return -1;
3776 for (;;) {
3777 PyObject *next = PyIter_Next(iter);
3778 if (next == NULL) {
3779 if (PyErr_Occurred())
3780 ok = -1;
3781 break;
3782 }
3783 ok = PySequence_Contains(other, next);
3784 Py_DECREF(next);
3785 if (ok <= 0)
3786 break;
3787 }
3788 Py_DECREF(iter);
3789 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003790}
3791
3792static PyObject *
3793dictview_richcompare(PyObject *self, PyObject *other, int op)
3794{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003795 Py_ssize_t len_self, len_other;
3796 int ok;
3797 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003799 assert(self != NULL);
3800 assert(PyDictViewSet_Check(self));
3801 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003802
Brian Curtindfc80e32011-08-10 20:28:54 -05003803 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3804 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003806 len_self = PyObject_Size(self);
3807 if (len_self < 0)
3808 return NULL;
3809 len_other = PyObject_Size(other);
3810 if (len_other < 0)
3811 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003813 ok = 0;
3814 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003816 case Py_NE:
3817 case Py_EQ:
3818 if (len_self == len_other)
3819 ok = all_contained_in(self, other);
3820 if (op == Py_NE && ok >= 0)
3821 ok = !ok;
3822 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003824 case Py_LT:
3825 if (len_self < len_other)
3826 ok = all_contained_in(self, other);
3827 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003829 case Py_LE:
3830 if (len_self <= len_other)
3831 ok = all_contained_in(self, other);
3832 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003834 case Py_GT:
3835 if (len_self > len_other)
3836 ok = all_contained_in(other, self);
3837 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003839 case Py_GE:
3840 if (len_self >= len_other)
3841 ok = all_contained_in(other, self);
3842 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003844 }
3845 if (ok < 0)
3846 return NULL;
3847 result = ok ? Py_True : Py_False;
3848 Py_INCREF(result);
3849 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003850}
3851
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003852static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003853dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003854{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003855 PyObject *seq;
bennorthd7773d92018-01-26 15:46:01 +00003856 PyObject *result = NULL;
3857 Py_ssize_t rc;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003858
bennorthd7773d92018-01-26 15:46:01 +00003859 rc = Py_ReprEnter((PyObject *)dv);
3860 if (rc != 0) {
3861 return rc > 0 ? PyUnicode_FromString("...") : NULL;
3862 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003863 seq = PySequence_List((PyObject *)dv);
bennorthd7773d92018-01-26 15:46:01 +00003864 if (seq == NULL) {
3865 goto Done;
3866 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003867 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
3868 Py_DECREF(seq);
bennorthd7773d92018-01-26 15:46:01 +00003869
3870Done:
3871 Py_ReprLeave((PyObject *)dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003872 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003873}
3874
Guido van Rossum3ac67412007-02-10 18:55:06 +00003875/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003876
3877static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003878dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003879{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003880 if (dv->dv_dict == NULL) {
3881 Py_RETURN_NONE;
3882 }
3883 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003884}
3885
3886static int
Eric Snow96c6af92015-05-29 22:21:39 -06003887dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00003888{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003889 if (dv->dv_dict == NULL)
3890 return 0;
3891 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003892}
3893
Guido van Rossum83825ac2007-02-10 04:54:19 +00003894static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003895 (lenfunc)dictview_len, /* sq_length */
3896 0, /* sq_concat */
3897 0, /* sq_repeat */
3898 0, /* sq_item */
3899 0, /* sq_slice */
3900 0, /* sq_ass_item */
3901 0, /* sq_ass_slice */
3902 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003903};
3904
Guido van Rossum523259b2007-08-24 23:41:22 +00003905static PyObject*
3906dictviews_sub(PyObject* self, PyObject *other)
3907{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003908 PyObject *result = PySet_New(self);
3909 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003910 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003912 if (result == NULL)
3913 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003914
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003915 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003916 if (tmp == NULL) {
3917 Py_DECREF(result);
3918 return NULL;
3919 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003921 Py_DECREF(tmp);
3922 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003923}
3924
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003925PyObject*
3926_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00003927{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003928 PyObject *result = PySet_New(self);
3929 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003930 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003932 if (result == NULL)
3933 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003934
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003935 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_intersection_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003936 if (tmp == NULL) {
3937 Py_DECREF(result);
3938 return NULL;
3939 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003940
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003941 Py_DECREF(tmp);
3942 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003943}
3944
3945static PyObject*
3946dictviews_or(PyObject* self, PyObject *other)
3947{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003948 PyObject *result = PySet_New(self);
3949 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003950 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003952 if (result == NULL)
3953 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003954
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003955 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003956 if (tmp == NULL) {
3957 Py_DECREF(result);
3958 return NULL;
3959 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003961 Py_DECREF(tmp);
3962 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003963}
3964
3965static PyObject*
3966dictviews_xor(PyObject* self, PyObject *other)
3967{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003968 PyObject *result = PySet_New(self);
3969 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003970 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003972 if (result == NULL)
3973 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003974
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003975 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_symmetric_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003976 if (tmp == NULL) {
3977 Py_DECREF(result);
3978 return NULL;
3979 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003981 Py_DECREF(tmp);
3982 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003983}
3984
3985static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003986 0, /*nb_add*/
3987 (binaryfunc)dictviews_sub, /*nb_subtract*/
3988 0, /*nb_multiply*/
3989 0, /*nb_remainder*/
3990 0, /*nb_divmod*/
3991 0, /*nb_power*/
3992 0, /*nb_negative*/
3993 0, /*nb_positive*/
3994 0, /*nb_absolute*/
3995 0, /*nb_bool*/
3996 0, /*nb_invert*/
3997 0, /*nb_lshift*/
3998 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003999 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004000 (binaryfunc)dictviews_xor, /*nb_xor*/
4001 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00004002};
4003
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004004static PyObject*
4005dictviews_isdisjoint(PyObject *self, PyObject *other)
4006{
4007 PyObject *it;
4008 PyObject *item = NULL;
4009
4010 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06004011 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004012 Py_RETURN_TRUE;
4013 else
4014 Py_RETURN_FALSE;
4015 }
4016
4017 /* Iterate over the shorter object (only if other is a set,
4018 * because PySequence_Contains may be expensive otherwise): */
4019 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06004020 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004021 Py_ssize_t len_other = PyObject_Size(other);
4022 if (len_other == -1)
4023 return NULL;
4024
4025 if ((len_other > len_self)) {
4026 PyObject *tmp = other;
4027 other = self;
4028 self = tmp;
4029 }
4030 }
4031
4032 it = PyObject_GetIter(other);
4033 if (it == NULL)
4034 return NULL;
4035
4036 while ((item = PyIter_Next(it)) != NULL) {
4037 int contains = PySequence_Contains(self, item);
4038 Py_DECREF(item);
4039 if (contains == -1) {
4040 Py_DECREF(it);
4041 return NULL;
4042 }
4043
4044 if (contains) {
4045 Py_DECREF(it);
4046 Py_RETURN_FALSE;
4047 }
4048 }
4049 Py_DECREF(it);
4050 if (PyErr_Occurred())
4051 return NULL; /* PyIter_Next raised an exception. */
4052 Py_RETURN_TRUE;
4053}
4054
4055PyDoc_STRVAR(isdisjoint_doc,
4056"Return True if the view and the given iterable have a null intersection.");
4057
Guido van Rossumb90c8482007-02-10 01:11:45 +00004058static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004059 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4060 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004061 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004062};
4063
4064PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004065 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4066 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004067 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004068 0, /* tp_itemsize */
4069 /* methods */
4070 (destructor)dictview_dealloc, /* tp_dealloc */
4071 0, /* tp_print */
4072 0, /* tp_getattr */
4073 0, /* tp_setattr */
4074 0, /* tp_reserved */
4075 (reprfunc)dictview_repr, /* tp_repr */
4076 &dictviews_as_number, /* tp_as_number */
4077 &dictkeys_as_sequence, /* tp_as_sequence */
4078 0, /* tp_as_mapping */
4079 0, /* tp_hash */
4080 0, /* tp_call */
4081 0, /* tp_str */
4082 PyObject_GenericGetAttr, /* tp_getattro */
4083 0, /* tp_setattro */
4084 0, /* tp_as_buffer */
4085 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4086 0, /* tp_doc */
4087 (traverseproc)dictview_traverse, /* tp_traverse */
4088 0, /* tp_clear */
4089 dictview_richcompare, /* tp_richcompare */
4090 0, /* tp_weaklistoffset */
4091 (getiterfunc)dictkeys_iter, /* tp_iter */
4092 0, /* tp_iternext */
4093 dictkeys_methods, /* tp_methods */
4094 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004095};
4096
4097static PyObject *
4098dictkeys_new(PyObject *dict)
4099{
Eric Snow96c6af92015-05-29 22:21:39 -06004100 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004101}
4102
Guido van Rossum3ac67412007-02-10 18:55:06 +00004103/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004104
4105static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004106dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004107{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004108 if (dv->dv_dict == NULL) {
4109 Py_RETURN_NONE;
4110 }
4111 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004112}
4113
4114static int
Eric Snow96c6af92015-05-29 22:21:39 -06004115dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004116{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004117 int result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004118 PyObject *key, *value, *found;
4119 if (dv->dv_dict == NULL)
4120 return 0;
4121 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
4122 return 0;
4123 key = PyTuple_GET_ITEM(obj, 0);
4124 value = PyTuple_GET_ITEM(obj, 1);
Raymond Hettinger6692f012016-09-18 21:46:08 -07004125 found = PyDict_GetItemWithError((PyObject *)dv->dv_dict, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004126 if (found == NULL) {
4127 if (PyErr_Occurred())
4128 return -1;
4129 return 0;
4130 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004131 Py_INCREF(found);
4132 result = PyObject_RichCompareBool(value, found, Py_EQ);
4133 Py_DECREF(found);
4134 return result;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004135}
4136
Guido van Rossum83825ac2007-02-10 04:54:19 +00004137static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004138 (lenfunc)dictview_len, /* sq_length */
4139 0, /* sq_concat */
4140 0, /* sq_repeat */
4141 0, /* sq_item */
4142 0, /* sq_slice */
4143 0, /* sq_ass_item */
4144 0, /* sq_ass_slice */
4145 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004146};
4147
Guido van Rossumb90c8482007-02-10 01:11:45 +00004148static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004149 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4150 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004151 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004152};
4153
4154PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004155 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4156 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004157 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004158 0, /* tp_itemsize */
4159 /* methods */
4160 (destructor)dictview_dealloc, /* tp_dealloc */
4161 0, /* tp_print */
4162 0, /* tp_getattr */
4163 0, /* tp_setattr */
4164 0, /* tp_reserved */
4165 (reprfunc)dictview_repr, /* tp_repr */
4166 &dictviews_as_number, /* tp_as_number */
4167 &dictitems_as_sequence, /* tp_as_sequence */
4168 0, /* tp_as_mapping */
4169 0, /* tp_hash */
4170 0, /* tp_call */
4171 0, /* tp_str */
4172 PyObject_GenericGetAttr, /* tp_getattro */
4173 0, /* tp_setattro */
4174 0, /* tp_as_buffer */
4175 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4176 0, /* tp_doc */
4177 (traverseproc)dictview_traverse, /* tp_traverse */
4178 0, /* tp_clear */
4179 dictview_richcompare, /* tp_richcompare */
4180 0, /* tp_weaklistoffset */
4181 (getiterfunc)dictitems_iter, /* tp_iter */
4182 0, /* tp_iternext */
4183 dictitems_methods, /* tp_methods */
4184 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004185};
4186
4187static PyObject *
4188dictitems_new(PyObject *dict)
4189{
Eric Snow96c6af92015-05-29 22:21:39 -06004190 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004191}
4192
Guido van Rossum3ac67412007-02-10 18:55:06 +00004193/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004194
4195static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004196dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004198 if (dv->dv_dict == NULL) {
4199 Py_RETURN_NONE;
4200 }
4201 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004202}
4203
Guido van Rossum83825ac2007-02-10 04:54:19 +00004204static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004205 (lenfunc)dictview_len, /* sq_length */
4206 0, /* sq_concat */
4207 0, /* sq_repeat */
4208 0, /* sq_item */
4209 0, /* sq_slice */
4210 0, /* sq_ass_item */
4211 0, /* sq_ass_slice */
4212 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004213};
4214
Guido van Rossumb90c8482007-02-10 01:11:45 +00004215static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004216 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004217};
4218
4219PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004220 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4221 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004222 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004223 0, /* tp_itemsize */
4224 /* methods */
4225 (destructor)dictview_dealloc, /* tp_dealloc */
4226 0, /* tp_print */
4227 0, /* tp_getattr */
4228 0, /* tp_setattr */
4229 0, /* tp_reserved */
4230 (reprfunc)dictview_repr, /* tp_repr */
4231 0, /* tp_as_number */
4232 &dictvalues_as_sequence, /* tp_as_sequence */
4233 0, /* tp_as_mapping */
4234 0, /* tp_hash */
4235 0, /* tp_call */
4236 0, /* tp_str */
4237 PyObject_GenericGetAttr, /* tp_getattro */
4238 0, /* tp_setattro */
4239 0, /* tp_as_buffer */
4240 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4241 0, /* tp_doc */
4242 (traverseproc)dictview_traverse, /* tp_traverse */
4243 0, /* tp_clear */
4244 0, /* tp_richcompare */
4245 0, /* tp_weaklistoffset */
4246 (getiterfunc)dictvalues_iter, /* tp_iter */
4247 0, /* tp_iternext */
4248 dictvalues_methods, /* tp_methods */
4249 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004250};
4251
4252static PyObject *
4253dictvalues_new(PyObject *dict)
4254{
Eric Snow96c6af92015-05-29 22:21:39 -06004255 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004256}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004257
4258/* Returns NULL if cannot allocate a new PyDictKeysObject,
4259 but does not set an error */
4260PyDictKeysObject *
4261_PyDict_NewKeysForClass(void)
4262{
Victor Stinner742da042016-09-07 17:40:12 -07004263 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004264 if (keys == NULL)
4265 PyErr_Clear();
4266 else
4267 keys->dk_lookup = lookdict_split;
4268 return keys;
4269}
4270
4271#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
4272
4273PyObject *
4274PyObject_GenericGetDict(PyObject *obj, void *context)
4275{
4276 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
4277 if (dictptr == NULL) {
4278 PyErr_SetString(PyExc_AttributeError,
4279 "This object has no __dict__");
4280 return NULL;
4281 }
4282 dict = *dictptr;
4283 if (dict == NULL) {
4284 PyTypeObject *tp = Py_TYPE(obj);
4285 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
4286 DK_INCREF(CACHED_KEYS(tp));
4287 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
4288 }
4289 else {
4290 *dictptr = dict = PyDict_New();
4291 }
4292 }
4293 Py_XINCREF(dict);
4294 return dict;
4295}
4296
4297int
4298_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
Victor Stinner742da042016-09-07 17:40:12 -07004299 PyObject *key, PyObject *value)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004300{
4301 PyObject *dict;
4302 int res;
4303 PyDictKeysObject *cached;
4304
4305 assert(dictptr != NULL);
4306 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
4307 assert(dictptr != NULL);
4308 dict = *dictptr;
4309 if (dict == NULL) {
4310 DK_INCREF(cached);
4311 dict = new_dict_with_shared_keys(cached);
4312 if (dict == NULL)
4313 return -1;
4314 *dictptr = dict;
4315 }
4316 if (value == NULL) {
4317 res = PyDict_DelItem(dict, key);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004318 // Since key sharing dict doesn't allow deletion, PyDict_DelItem()
4319 // always converts dict to combined form.
4320 if ((cached = CACHED_KEYS(tp)) != NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004321 CACHED_KEYS(tp) = NULL;
4322 DK_DECREF(cached);
4323 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01004324 }
4325 else {
INADA Naoki2294f3a2017-02-12 13:51:30 +09004326 int was_shared = (cached == ((PyDictObject *)dict)->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004327 res = PyDict_SetItem(dict, key, value);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004328 if (was_shared &&
4329 (cached = CACHED_KEYS(tp)) != NULL &&
4330 cached != ((PyDictObject *)dict)->ma_keys) {
Victor Stinner3d3f2642016-12-15 17:21:23 +01004331 /* PyDict_SetItem() may call dictresize and convert split table
4332 * into combined table. In such case, convert it to split
4333 * table again and update type's shared key only when this is
4334 * the only dict sharing key with the type.
4335 *
4336 * This is to allow using shared key in class like this:
4337 *
4338 * class C:
4339 * def __init__(self):
4340 * # one dict resize happens
4341 * self.a, self.b, self.c = 1, 2, 3
4342 * self.d, self.e, self.f = 4, 5, 6
4343 * a = C()
4344 */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004345 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004346 CACHED_KEYS(tp) = make_keys_shared(dict);
Victor Stinner742da042016-09-07 17:40:12 -07004347 }
4348 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004349 CACHED_KEYS(tp) = NULL;
4350 }
4351 DK_DECREF(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004352 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
4353 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004354 }
4355 }
4356 } else {
4357 dict = *dictptr;
4358 if (dict == NULL) {
4359 dict = PyDict_New();
4360 if (dict == NULL)
4361 return -1;
4362 *dictptr = dict;
4363 }
4364 if (value == NULL) {
4365 res = PyDict_DelItem(dict, key);
4366 } else {
4367 res = PyDict_SetItem(dict, key, value);
4368 }
4369 }
4370 return res;
4371}
4372
4373void
4374_PyDictKeys_DecRef(PyDictKeysObject *keys)
4375{
4376 DK_DECREF(keys);
4377}