blob: be895d4c1524257f18f4a31d5dd7ceae3bb89d99 [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;
Miss Islington (bot)de755122018-04-02 20:00:26 -07002554 split_copy->ma_version_tag = DICT_NEXT_VERSION();
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002555 DK_INCREF(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002556 for (i = 0, n = size; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002557 PyObject *value = mp->ma_values[i];
2558 Py_XINCREF(value);
2559 split_copy->ma_values[i] = value;
2560 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002561 if (_PyObject_GC_IS_TRACKED(mp))
2562 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002563 return (PyObject *)split_copy;
2564 }
Yury Selivanovb0a7a032018-01-22 11:54:41 -05002565
2566 if (PyDict_CheckExact(mp) && mp->ma_values == NULL &&
2567 (mp->ma_used >= (mp->ma_keys->dk_nentries * 2) / 3))
2568 {
2569 /* Use fast-copy if:
2570
2571 (1) 'mp' is an instance of a subclassed dict; and
2572
2573 (2) 'mp' is not a split-dict; and
2574
2575 (3) if 'mp' is non-compact ('del' operation does not resize dicts),
2576 do fast-copy only if it has at most 1/3 non-used keys.
2577
2578 The last condition (3) is important to guard against a pathalogical
2579 case when a large dict is almost emptied with multiple del/pop
2580 operations and copied after that. In cases like this, we defer to
2581 PyDict_Merge, which produces a compacted copy.
2582 */
2583 return clone_combined_dict(mp);
2584 }
2585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002586 copy = PyDict_New();
2587 if (copy == NULL)
2588 return NULL;
2589 if (PyDict_Merge(copy, o, 1) == 0)
2590 return copy;
2591 Py_DECREF(copy);
2592 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002593}
2594
Martin v. Löwis18e16552006-02-15 17:27:45 +00002595Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002596PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002597{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002598 if (mp == NULL || !PyDict_Check(mp)) {
2599 PyErr_BadInternalCall();
2600 return -1;
2601 }
2602 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002603}
2604
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002605PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002606PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002607{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002608 if (mp == NULL || !PyDict_Check(mp)) {
2609 PyErr_BadInternalCall();
2610 return NULL;
2611 }
2612 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002613}
2614
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002615PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002616PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002617{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002618 if (mp == NULL || !PyDict_Check(mp)) {
2619 PyErr_BadInternalCall();
2620 return NULL;
2621 }
2622 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002623}
2624
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002625PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002626PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002627{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002628 if (mp == NULL || !PyDict_Check(mp)) {
2629 PyErr_BadInternalCall();
2630 return NULL;
2631 }
2632 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002633}
2634
Tim Peterse63415e2001-05-08 04:38:29 +00002635/* Return 1 if dicts equal, 0 if not, -1 if error.
2636 * Gets out as soon as any difference is detected.
2637 * Uses only Py_EQ comparison.
2638 */
2639static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002640dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002641{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002642 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002644 if (a->ma_used != b->ma_used)
2645 /* can't be equal if # of entries differ */
2646 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002647 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Victor Stinner742da042016-09-07 17:40:12 -07002648 for (i = 0; i < a->ma_keys->dk_nentries; i++) {
2649 PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002650 PyObject *aval;
2651 if (a->ma_values)
2652 aval = a->ma_values[i];
2653 else
2654 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002655 if (aval != NULL) {
2656 int cmp;
2657 PyObject *bval;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002658 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002659 /* temporarily bump aval's refcount to ensure it stays
2660 alive until we're done with it */
2661 Py_INCREF(aval);
2662 /* ditto for key */
2663 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002664 /* reuse the known hash value */
INADA Naoki778928b2017-08-03 23:45:15 +09002665 b->ma_keys->dk_lookup(b, key, ep->me_hash, &bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002666 if (bval == NULL) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002667 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002668 Py_DECREF(aval);
2669 if (PyErr_Occurred())
2670 return -1;
2671 return 0;
2672 }
2673 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002674 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002675 Py_DECREF(aval);
2676 if (cmp <= 0) /* error or not equal */
2677 return cmp;
2678 }
2679 }
2680 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002681}
Tim Peterse63415e2001-05-08 04:38:29 +00002682
2683static PyObject *
2684dict_richcompare(PyObject *v, PyObject *w, int op)
2685{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002686 int cmp;
2687 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002689 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2690 res = Py_NotImplemented;
2691 }
2692 else if (op == Py_EQ || op == Py_NE) {
2693 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2694 if (cmp < 0)
2695 return NULL;
2696 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2697 }
2698 else
2699 res = Py_NotImplemented;
2700 Py_INCREF(res);
2701 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002702}
Tim Peterse63415e2001-05-08 04:38:29 +00002703
Larry Hastings61272b72014-01-07 12:41:53 -08002704/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002705
2706@coexist
2707dict.__contains__
2708
2709 key: object
2710 /
2711
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002712True if the dictionary has the specified key, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002713[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002714
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002715static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002716dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka19d25972017-02-04 08:05:07 +02002717/*[clinic end generated code: output=a3d03db709ed6e6b input=fe1cb42ad831e820]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002718{
Larry Hastingsc2047262014-01-25 20:43:29 -08002719 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002720 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002721 Py_ssize_t ix;
INADA Naokiba609772016-12-07 20:41:42 +09002722 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002724 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002725 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002726 hash = PyObject_Hash(key);
2727 if (hash == -1)
2728 return NULL;
2729 }
INADA Naoki778928b2017-08-03 23:45:15 +09002730 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002731 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002732 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002733 if (ix == DKIX_EMPTY || value == NULL)
Victor Stinner742da042016-09-07 17:40:12 -07002734 Py_RETURN_FALSE;
2735 Py_RETURN_TRUE;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002736}
2737
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002738/*[clinic input]
2739dict.get
2740
2741 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002742 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002743 /
2744
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002745Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002746[clinic start generated code]*/
2747
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002748static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002749dict_get_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002750/*[clinic end generated code: output=bba707729dee05bf input=279ddb5790b6b107]*/
Barry Warsawc38c5da1997-10-06 17:49:20 +00002751{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002752 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002753 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002754 Py_ssize_t ix;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002756 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002757 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002758 hash = PyObject_Hash(key);
2759 if (hash == -1)
2760 return NULL;
2761 }
INADA Naoki778928b2017-08-03 23:45:15 +09002762 ix = (self->ma_keys->dk_lookup) (self, key, hash, &val);
Victor Stinner742da042016-09-07 17:40:12 -07002763 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002764 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002765 if (ix == DKIX_EMPTY || val == NULL) {
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002766 val = default_value;
INADA Naokiba609772016-12-07 20:41:42 +09002767 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002768 Py_INCREF(val);
2769 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002770}
2771
Benjamin Peterson00e98862013-03-07 22:16:29 -05002772PyObject *
2773PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002774{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002775 PyDictObject *mp = (PyDictObject *)d;
INADA Naoki93f26f72016-11-02 18:45:16 +09002776 PyObject *value;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002777 Py_hash_t hash;
Guido van Rossum164452c2000-08-08 16:12:54 +00002778
Benjamin Peterson00e98862013-03-07 22:16:29 -05002779 if (!PyDict_Check(d)) {
2780 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002781 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002782 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002784 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002785 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002786 hash = PyObject_Hash(key);
2787 if (hash == -1)
2788 return NULL;
2789 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002790
2791 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
2792 if (insertion_resize(mp) < 0)
2793 return NULL;
2794 }
2795
INADA Naoki778928b2017-08-03 23:45:15 +09002796 Py_ssize_t ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002797 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002798 return NULL;
INADA Naoki93f26f72016-11-02 18:45:16 +09002799
2800 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09002801 ((ix >= 0 && value == NULL && mp->ma_used != ix) ||
INADA Naoki93f26f72016-11-02 18:45:16 +09002802 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
2803 if (insertion_resize(mp) < 0) {
2804 return NULL;
2805 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002806 ix = DKIX_EMPTY;
2807 }
2808
2809 if (ix == DKIX_EMPTY) {
2810 PyDictKeyEntry *ep, *ep0;
2811 value = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002812 if (mp->ma_keys->dk_usable <= 0) {
Victor Stinner3c336c52016-09-12 14:17:40 +02002813 if (insertion_resize(mp) < 0) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002814 return NULL;
Victor Stinner3c336c52016-09-12 14:17:40 +02002815 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002816 }
INADA Naoki778928b2017-08-03 23:45:15 +09002817 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naoki93f26f72016-11-02 18:45:16 +09002818 ep0 = DK_ENTRIES(mp->ma_keys);
2819 ep = &ep0[mp->ma_keys->dk_nentries];
2820 dk_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002821 Py_INCREF(key);
INADA Naoki93f26f72016-11-02 18:45:16 +09002822 Py_INCREF(value);
2823 MAINTAIN_TRACKING(mp, key, value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002824 ep->me_key = key;
2825 ep->me_hash = hash;
INADA Naokiba609772016-12-07 20:41:42 +09002826 if (_PyDict_HasSplitTable(mp)) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002827 assert(mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
2828 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Victor Stinner742da042016-09-07 17:40:12 -07002829 }
2830 else {
INADA Naoki93f26f72016-11-02 18:45:16 +09002831 ep->me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07002832 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002833 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002834 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naoki93f26f72016-11-02 18:45:16 +09002835 mp->ma_keys->dk_usable--;
2836 mp->ma_keys->dk_nentries++;
2837 assert(mp->ma_keys->dk_usable >= 0);
2838 }
INADA Naokiba609772016-12-07 20:41:42 +09002839 else if (value == NULL) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002840 value = defaultobj;
2841 assert(_PyDict_HasSplitTable(mp));
2842 assert(ix == mp->ma_used);
2843 Py_INCREF(value);
2844 MAINTAIN_TRACKING(mp, key, value);
INADA Naokiba609772016-12-07 20:41:42 +09002845 mp->ma_values[ix] = value;
INADA Naoki93f26f72016-11-02 18:45:16 +09002846 mp->ma_used++;
2847 mp->ma_version_tag = DICT_NEXT_VERSION();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002848 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002849
2850 assert(_PyDict_CheckConsistency(mp));
2851 return value;
Guido van Rossum164452c2000-08-08 16:12:54 +00002852}
2853
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002854/*[clinic input]
2855dict.setdefault
2856
2857 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002858 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002859 /
2860
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002861Insert key with a value of default if key is not in the dictionary.
2862
2863Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002864[clinic start generated code]*/
2865
Benjamin Peterson00e98862013-03-07 22:16:29 -05002866static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002867dict_setdefault_impl(PyDictObject *self, PyObject *key,
2868 PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002869/*[clinic end generated code: output=f8c1101ebf69e220 input=0f063756e815fd9d]*/
Benjamin Peterson00e98862013-03-07 22:16:29 -05002870{
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002871 PyObject *val;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002872
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002873 val = PyDict_SetDefault((PyObject *)self, key, default_value);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002874 Py_XINCREF(val);
2875 return val;
2876}
Guido van Rossum164452c2000-08-08 16:12:54 +00002877
2878static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002879dict_clear(PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002880{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 PyDict_Clear((PyObject *)mp);
2882 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002883}
2884
Guido van Rossumba6ab842000-12-12 22:02:18 +00002885static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002886dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002887{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002888 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00002889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002890 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2891 return NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06002892
Yury Selivanov684ef2c2016-10-28 19:01:21 -04002893 return _PyDict_Pop((PyObject*)mp, key, deflt);
Guido van Rossume027d982002-04-12 15:11:59 +00002894}
2895
2896static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002897dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002898{
Victor Stinner742da042016-09-07 17:40:12 -07002899 Py_ssize_t i, j;
2900 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 /* Allocate the result tuple before checking the size. Believe it
2904 * or not, this allocation could trigger a garbage collection which
2905 * could empty the dict, so if we checked the size first and that
2906 * happened, the result would be an infinite loop (searching for an
2907 * entry that no longer exists). Note that the usual popitem()
2908 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2909 * tuple away if the dict *is* empty isn't a significant
2910 * inefficiency -- possible, but unlikely in practice.
2911 */
2912 res = PyTuple_New(2);
2913 if (res == NULL)
2914 return NULL;
2915 if (mp->ma_used == 0) {
2916 Py_DECREF(res);
2917 PyErr_SetString(PyExc_KeyError,
2918 "popitem(): dictionary is empty");
2919 return NULL;
2920 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002921 /* Convert split table to combined table */
2922 if (mp->ma_keys->dk_lookup == lookdict_split) {
2923 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2924 Py_DECREF(res);
2925 return NULL;
2926 }
2927 }
2928 ENSURE_ALLOWS_DELETIONS(mp);
Victor Stinner742da042016-09-07 17:40:12 -07002929
2930 /* Pop last item */
2931 ep0 = DK_ENTRIES(mp->ma_keys);
2932 i = mp->ma_keys->dk_nentries - 1;
2933 while (i >= 0 && ep0[i].me_value == NULL) {
2934 i--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002935 }
Victor Stinner742da042016-09-07 17:40:12 -07002936 assert(i >= 0);
2937
2938 ep = &ep0[i];
2939 j = lookdict_index(mp->ma_keys, ep->me_hash, i);
2940 assert(j >= 0);
2941 assert(dk_get_index(mp->ma_keys, j) == i);
2942 dk_set_index(mp->ma_keys, j, DKIX_DUMMY);
2943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002944 PyTuple_SET_ITEM(res, 0, ep->me_key);
2945 PyTuple_SET_ITEM(res, 1, ep->me_value);
Victor Stinner742da042016-09-07 17:40:12 -07002946 ep->me_key = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002947 ep->me_value = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002948 /* We can't dk_usable++ since there is DKIX_DUMMY in indices */
2949 mp->ma_keys->dk_nentries = i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002950 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002951 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02002952 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002953 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002954}
2955
Jeremy Hylton8caad492000-06-23 14:18:11 +00002956static int
2957dict_traverse(PyObject *op, visitproc visit, void *arg)
2958{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002959 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson55f44522016-09-05 12:12:59 -07002960 PyDictKeysObject *keys = mp->ma_keys;
Serhiy Storchaka46825d22016-09-26 21:29:34 +03002961 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Victor Stinner742da042016-09-07 17:40:12 -07002962 Py_ssize_t i, n = keys->dk_nentries;
2963
Benjamin Peterson55f44522016-09-05 12:12:59 -07002964 if (keys->dk_lookup == lookdict) {
2965 for (i = 0; i < n; i++) {
2966 if (entries[i].me_value != NULL) {
2967 Py_VISIT(entries[i].me_value);
2968 Py_VISIT(entries[i].me_key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002969 }
2970 }
Victor Stinner742da042016-09-07 17:40:12 -07002971 }
2972 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002973 if (mp->ma_values != NULL) {
Benjamin Peterson55f44522016-09-05 12:12:59 -07002974 for (i = 0; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002975 Py_VISIT(mp->ma_values[i]);
2976 }
2977 }
2978 else {
Benjamin Peterson55f44522016-09-05 12:12:59 -07002979 for (i = 0; i < n; i++) {
2980 Py_VISIT(entries[i].me_value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002981 }
2982 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002983 }
2984 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002985}
2986
2987static int
2988dict_tp_clear(PyObject *op)
2989{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002990 PyDict_Clear(op);
2991 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002992}
2993
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002994static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002995
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002996Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06002997_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002998{
Victor Stinner742da042016-09-07 17:40:12 -07002999 Py_ssize_t size, usable, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003000
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003001 size = DK_SIZE(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07003002 usable = USABLE_FRACTION(size);
3003
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02003004 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003005 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -07003006 res += usable * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003007 /* If the dictionary is split, the keys portion is accounted-for
3008 in the type object. */
3009 if (mp->ma_keys->dk_refcnt == 1)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003010 res += (sizeof(PyDictKeysObject)
3011 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
3012 + DK_IXSIZE(mp->ma_keys) * size
3013 + sizeof(PyDictKeyEntry) * usable);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003014 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003015}
3016
3017Py_ssize_t
3018_PyDict_KeysSize(PyDictKeysObject *keys)
3019{
Victor Stinner98ee9d52016-09-08 09:33:56 -07003020 return (sizeof(PyDictKeysObject)
3021 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
3022 + DK_IXSIZE(keys) * DK_SIZE(keys)
3023 + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003024}
3025
doko@ubuntu.com17210f52016-01-14 14:04:59 +01003026static PyObject *
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003027dict_sizeof(PyDictObject *mp)
3028{
3029 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
3030}
3031
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00003032PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
3033
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003034PyDoc_STRVAR(sizeof__doc__,
3035"D.__sizeof__() -> size of D in memory, in bytes");
3036
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003037PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00003038"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00003039If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00003040
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003041PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00003042"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000030432-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003044
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003045PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04003046"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
3047If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
3048If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
3049In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00003050
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003051PyDoc_STRVAR(clear__doc__,
3052"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003053
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003054PyDoc_STRVAR(copy__doc__,
3055"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00003056
Guido van Rossumb90c8482007-02-10 01:11:45 +00003057/* Forward */
3058static PyObject *dictkeys_new(PyObject *);
3059static PyObject *dictitems_new(PyObject *);
3060static PyObject *dictvalues_new(PyObject *);
3061
Guido van Rossum45c85d12007-07-27 16:31:40 +00003062PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003063 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003064PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003065 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003066PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003067 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00003068
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003069static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07003070 DICT___CONTAINS___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003071 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
3072 getitem__doc__},
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003073 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003074 sizeof__doc__},
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003075 DICT_GET_METHODDEF
3076 DICT_SETDEFAULT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003077 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
3078 pop__doc__},
3079 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
3080 popitem__doc__},
3081 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
3082 keys__doc__},
3083 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
3084 items__doc__},
3085 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
3086 values__doc__},
3087 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
3088 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08003089 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003090 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
3091 clear__doc__},
3092 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
3093 copy__doc__},
3094 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003095};
3096
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003097/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00003098int
3099PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003100{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003101 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07003102 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003103 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003104 PyObject *value;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003106 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003107 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003108 hash = PyObject_Hash(key);
3109 if (hash == -1)
3110 return -1;
3111 }
INADA Naoki778928b2017-08-03 23:45:15 +09003112 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003113 if (ix == DKIX_ERROR)
3114 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003115 return (ix != DKIX_EMPTY && value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003116}
3117
Thomas Wouterscf297e42007-02-23 15:07:44 +00003118/* Internal version of PyDict_Contains used when the hash value is already known */
3119int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003120_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00003121{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003122 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003123 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07003124 Py_ssize_t ix;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003125
INADA Naoki778928b2017-08-03 23:45:15 +09003126 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003127 if (ix == DKIX_ERROR)
3128 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003129 return (ix != DKIX_EMPTY && value != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00003130}
3131
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003132/* Hack to implement "key in dict" */
3133static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003134 0, /* sq_length */
3135 0, /* sq_concat */
3136 0, /* sq_repeat */
3137 0, /* sq_item */
3138 0, /* sq_slice */
3139 0, /* sq_ass_item */
3140 0, /* sq_ass_slice */
3141 PyDict_Contains, /* sq_contains */
3142 0, /* sq_inplace_concat */
3143 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003144};
3145
Guido van Rossum09e563a2001-05-01 12:10:21 +00003146static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003147dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3148{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003149 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003150 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003152 assert(type != NULL && type->tp_alloc != NULL);
3153 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02003154 if (self == NULL)
3155 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003156 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003157
Victor Stinnera9f61a52013-07-16 22:17:26 +02003158 /* The object has been implicitly tracked by tp_alloc */
3159 if (type == &PyDict_Type)
3160 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003161
3162 d->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003163 d->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07003164 d->ma_keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003165 if (d->ma_keys == NULL) {
3166 Py_DECREF(self);
3167 return NULL;
3168 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02003169 assert(_PyDict_CheckConsistency(d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003170 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003171}
3172
Tim Peters25786c02001-09-02 08:22:48 +00003173static int
3174dict_init(PyObject *self, PyObject *args, PyObject *kwds)
3175{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003176 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00003177}
3178
Tim Peters6d6c1a32001-08-02 04:15:00 +00003179static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003180dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00003181{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003182 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003183}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003184
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003185PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00003186"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00003187"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003188" (key, value) pairs\n"
3189"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00003190" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003191" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00003192" d[k] = v\n"
3193"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
3194" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00003195
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003196PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003197 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3198 "dict",
3199 sizeof(PyDictObject),
3200 0,
3201 (destructor)dict_dealloc, /* tp_dealloc */
3202 0, /* tp_print */
3203 0, /* tp_getattr */
3204 0, /* tp_setattr */
3205 0, /* tp_reserved */
3206 (reprfunc)dict_repr, /* tp_repr */
3207 0, /* tp_as_number */
3208 &dict_as_sequence, /* tp_as_sequence */
3209 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003210 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003211 0, /* tp_call */
3212 0, /* tp_str */
3213 PyObject_GenericGetAttr, /* tp_getattro */
3214 0, /* tp_setattro */
3215 0, /* tp_as_buffer */
3216 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3217 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
3218 dictionary_doc, /* tp_doc */
3219 dict_traverse, /* tp_traverse */
3220 dict_tp_clear, /* tp_clear */
3221 dict_richcompare, /* tp_richcompare */
3222 0, /* tp_weaklistoffset */
3223 (getiterfunc)dict_iter, /* tp_iter */
3224 0, /* tp_iternext */
3225 mapp_methods, /* tp_methods */
3226 0, /* tp_members */
3227 0, /* tp_getset */
3228 0, /* tp_base */
3229 0, /* tp_dict */
3230 0, /* tp_descr_get */
3231 0, /* tp_descr_set */
3232 0, /* tp_dictoffset */
3233 dict_init, /* tp_init */
3234 PyType_GenericAlloc, /* tp_alloc */
3235 dict_new, /* tp_new */
3236 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003237};
3238
Victor Stinner3c1e4812012-03-26 22:10:51 +02003239PyObject *
3240_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
3241{
3242 PyObject *kv;
3243 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02003244 if (kv == NULL) {
3245 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003246 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02003247 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02003248 return PyDict_GetItem(dp, kv);
3249}
3250
Guido van Rossum3cca2451997-05-16 14:23:33 +00003251/* For backward compatibility with old dictionary interface */
3252
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003253PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003254PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003255{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003256 PyObject *kv, *rv;
3257 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003258 if (kv == NULL) {
3259 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003260 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003261 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003262 rv = PyDict_GetItem(v, kv);
3263 Py_DECREF(kv);
3264 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003265}
3266
3267int
Victor Stinner3c1e4812012-03-26 22:10:51 +02003268_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
3269{
3270 PyObject *kv;
3271 kv = _PyUnicode_FromId(key); /* borrowed */
3272 if (kv == NULL)
3273 return -1;
3274 return PyDict_SetItem(v, kv, item);
3275}
3276
3277int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003278PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003279{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003280 PyObject *kv;
3281 int err;
3282 kv = PyUnicode_FromString(key);
3283 if (kv == NULL)
3284 return -1;
3285 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
3286 err = PyDict_SetItem(v, kv, item);
3287 Py_DECREF(kv);
3288 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003289}
3290
3291int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01003292_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
3293{
3294 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3295 if (kv == NULL)
3296 return -1;
3297 return PyDict_DelItem(v, kv);
3298}
3299
3300int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003301PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003302{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003303 PyObject *kv;
3304 int err;
3305 kv = PyUnicode_FromString(key);
3306 if (kv == NULL)
3307 return -1;
3308 err = PyDict_DelItem(v, kv);
3309 Py_DECREF(kv);
3310 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003311}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003312
Raymond Hettinger019a1482004-03-18 02:41:19 +00003313/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003314
3315typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003316 PyObject_HEAD
3317 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
3318 Py_ssize_t di_used;
3319 Py_ssize_t di_pos;
3320 PyObject* di_result; /* reusable result tuple for iteritems */
3321 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003322} dictiterobject;
3323
3324static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003325dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003327 dictiterobject *di;
3328 di = PyObject_GC_New(dictiterobject, itertype);
3329 if (di == NULL)
3330 return NULL;
3331 Py_INCREF(dict);
3332 di->di_dict = dict;
3333 di->di_used = dict->ma_used;
3334 di->di_pos = 0;
3335 di->len = dict->ma_used;
3336 if (itertype == &PyDictIterItem_Type) {
3337 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
3338 if (di->di_result == NULL) {
3339 Py_DECREF(di);
3340 return NULL;
3341 }
3342 }
3343 else
3344 di->di_result = NULL;
3345 _PyObject_GC_TRACK(di);
3346 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003347}
3348
3349static void
3350dictiter_dealloc(dictiterobject *di)
3351{
INADA Naokia6296d32017-08-24 14:55:17 +09003352 /* bpo-31095: UnTrack is needed before calling any callbacks */
3353 _PyObject_GC_UNTRACK(di);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003354 Py_XDECREF(di->di_dict);
3355 Py_XDECREF(di->di_result);
3356 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003357}
3358
3359static int
3360dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
3361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003362 Py_VISIT(di->di_dict);
3363 Py_VISIT(di->di_result);
3364 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003365}
3366
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003367static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003368dictiter_len(dictiterobject *di)
3369{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003370 Py_ssize_t len = 0;
3371 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
3372 len = di->len;
3373 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003374}
3375
Guido van Rossumb90c8482007-02-10 01:11:45 +00003376PyDoc_STRVAR(length_hint_doc,
3377 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003378
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003379static PyObject *
3380dictiter_reduce(dictiterobject *di);
3381
3382PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3383
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003384static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003385 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
3386 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003387 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
3388 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003389 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003390};
3391
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003392static PyObject*
3393dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00003394{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003395 PyObject *key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003396 Py_ssize_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003397 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003398 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00003399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003400 if (d == NULL)
3401 return NULL;
3402 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00003403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003404 if (di->di_used != d->ma_used) {
3405 PyErr_SetString(PyExc_RuntimeError,
3406 "dictionary changed size during iteration");
3407 di->di_used = -1; /* Make this state sticky */
3408 return NULL;
3409 }
Guido van Rossum2147df72002-07-16 20:30:22 +00003410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003411 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003412 k = d->ma_keys;
INADA Naokica2d8be2016-11-04 16:59:10 +09003413 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003414 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003415 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003416 goto fail;
3417 key = DK_ENTRIES(k)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003418 assert(d->ma_values[i] != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003419 }
3420 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003421 Py_ssize_t n = k->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003422 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3423 while (i < n && entry_ptr->me_value == NULL) {
3424 entry_ptr++;
3425 i++;
3426 }
3427 if (i >= n)
3428 goto fail;
3429 key = entry_ptr->me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003430 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003431 di->di_pos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003432 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003433 Py_INCREF(key);
3434 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003435
3436fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003437 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003438 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003439 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003440}
3441
Raymond Hettinger019a1482004-03-18 02:41:19 +00003442PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003443 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3444 "dict_keyiterator", /* tp_name */
3445 sizeof(dictiterobject), /* tp_basicsize */
3446 0, /* tp_itemsize */
3447 /* methods */
3448 (destructor)dictiter_dealloc, /* tp_dealloc */
3449 0, /* tp_print */
3450 0, /* tp_getattr */
3451 0, /* tp_setattr */
3452 0, /* tp_reserved */
3453 0, /* tp_repr */
3454 0, /* tp_as_number */
3455 0, /* tp_as_sequence */
3456 0, /* tp_as_mapping */
3457 0, /* tp_hash */
3458 0, /* tp_call */
3459 0, /* tp_str */
3460 PyObject_GenericGetAttr, /* tp_getattro */
3461 0, /* tp_setattro */
3462 0, /* tp_as_buffer */
3463 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3464 0, /* tp_doc */
3465 (traverseproc)dictiter_traverse, /* tp_traverse */
3466 0, /* tp_clear */
3467 0, /* tp_richcompare */
3468 0, /* tp_weaklistoffset */
3469 PyObject_SelfIter, /* tp_iter */
3470 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3471 dictiter_methods, /* tp_methods */
3472 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003473};
3474
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003475static PyObject *
3476dictiter_iternextvalue(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003477{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003478 PyObject *value;
INADA Naokica2d8be2016-11-04 16:59:10 +09003479 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003480 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003482 if (d == NULL)
3483 return NULL;
3484 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003486 if (di->di_used != d->ma_used) {
3487 PyErr_SetString(PyExc_RuntimeError,
3488 "dictionary changed size during iteration");
3489 di->di_used = -1; /* Make this state sticky */
3490 return NULL;
3491 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003493 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003494 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003495 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003496 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003497 goto fail;
INADA Naokica2d8be2016-11-04 16:59:10 +09003498 value = d->ma_values[i];
3499 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003500 }
3501 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003502 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003503 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3504 while (i < n && entry_ptr->me_value == NULL) {
3505 entry_ptr++;
3506 i++;
3507 }
3508 if (i >= n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003509 goto fail;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003510 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003511 }
3512 di->di_pos = i+1;
3513 di->len--;
3514 Py_INCREF(value);
3515 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003516
3517fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003518 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003519 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003520 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003521}
3522
3523PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003524 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3525 "dict_valueiterator", /* tp_name */
3526 sizeof(dictiterobject), /* tp_basicsize */
3527 0, /* tp_itemsize */
3528 /* methods */
3529 (destructor)dictiter_dealloc, /* tp_dealloc */
3530 0, /* tp_print */
3531 0, /* tp_getattr */
3532 0, /* tp_setattr */
3533 0, /* tp_reserved */
3534 0, /* tp_repr */
3535 0, /* tp_as_number */
3536 0, /* tp_as_sequence */
3537 0, /* tp_as_mapping */
3538 0, /* tp_hash */
3539 0, /* tp_call */
3540 0, /* tp_str */
3541 PyObject_GenericGetAttr, /* tp_getattro */
3542 0, /* tp_setattro */
3543 0, /* tp_as_buffer */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003544 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003545 0, /* tp_doc */
3546 (traverseproc)dictiter_traverse, /* tp_traverse */
3547 0, /* tp_clear */
3548 0, /* tp_richcompare */
3549 0, /* tp_weaklistoffset */
3550 PyObject_SelfIter, /* tp_iter */
3551 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3552 dictiter_methods, /* tp_methods */
3553 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003554};
3555
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003556static PyObject *
3557dictiter_iternextitem(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003558{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003559 PyObject *key, *value, *result;
INADA Naokica2d8be2016-11-04 16:59:10 +09003560 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003561 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003563 if (d == NULL)
3564 return NULL;
3565 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003567 if (di->di_used != d->ma_used) {
3568 PyErr_SetString(PyExc_RuntimeError,
3569 "dictionary changed size during iteration");
3570 di->di_used = -1; /* Make this state sticky */
3571 return NULL;
3572 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003574 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003575 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003576 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003577 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003578 goto fail;
3579 key = DK_ENTRIES(d->ma_keys)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003580 value = d->ma_values[i];
3581 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003582 }
3583 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003584 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003585 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3586 while (i < n && entry_ptr->me_value == NULL) {
3587 entry_ptr++;
3588 i++;
3589 }
3590 if (i >= n)
3591 goto fail;
3592 key = entry_ptr->me_key;
3593 value = entry_ptr->me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003594 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003595 di->di_pos = i+1;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003596 di->len--;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003597 Py_INCREF(key);
3598 Py_INCREF(value);
3599 result = di->di_result;
3600 if (Py_REFCNT(result) == 1) {
3601 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3602 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3603 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3604 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003605 Py_INCREF(result);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003606 Py_DECREF(oldkey);
3607 Py_DECREF(oldvalue);
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003608 }
3609 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003610 result = PyTuple_New(2);
3611 if (result == NULL)
3612 return NULL;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003613 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3614 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003615 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003616 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003617
3618fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003619 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003620 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003621 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003622}
3623
3624PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003625 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3626 "dict_itemiterator", /* tp_name */
3627 sizeof(dictiterobject), /* tp_basicsize */
3628 0, /* tp_itemsize */
3629 /* methods */
3630 (destructor)dictiter_dealloc, /* tp_dealloc */
3631 0, /* tp_print */
3632 0, /* tp_getattr */
3633 0, /* tp_setattr */
3634 0, /* tp_reserved */
3635 0, /* tp_repr */
3636 0, /* tp_as_number */
3637 0, /* tp_as_sequence */
3638 0, /* tp_as_mapping */
3639 0, /* tp_hash */
3640 0, /* tp_call */
3641 0, /* tp_str */
3642 PyObject_GenericGetAttr, /* tp_getattro */
3643 0, /* tp_setattro */
3644 0, /* tp_as_buffer */
3645 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3646 0, /* tp_doc */
3647 (traverseproc)dictiter_traverse, /* tp_traverse */
3648 0, /* tp_clear */
3649 0, /* tp_richcompare */
3650 0, /* tp_weaklistoffset */
3651 PyObject_SelfIter, /* tp_iter */
3652 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3653 dictiter_methods, /* tp_methods */
3654 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003655};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003656
3657
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003658static PyObject *
3659dictiter_reduce(dictiterobject *di)
3660{
3661 PyObject *list;
3662 dictiterobject tmp;
3663
3664 list = PyList_New(0);
3665 if (!list)
3666 return NULL;
3667
3668 /* copy the itertor state */
3669 tmp = *di;
3670 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003671
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003672 /* iterate the temporary into a list */
3673 for(;;) {
3674 PyObject *element = 0;
3675 if (Py_TYPE(di) == &PyDictIterItem_Type)
3676 element = dictiter_iternextitem(&tmp);
3677 else if (Py_TYPE(di) == &PyDictIterKey_Type)
3678 element = dictiter_iternextkey(&tmp);
3679 else if (Py_TYPE(di) == &PyDictIterValue_Type)
3680 element = dictiter_iternextvalue(&tmp);
3681 else
Barry Warsawb2e57942017-09-14 18:13:16 -07003682 Py_UNREACHABLE();
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003683 if (element) {
3684 if (PyList_Append(list, element)) {
3685 Py_DECREF(element);
3686 Py_DECREF(list);
3687 Py_XDECREF(tmp.di_dict);
3688 return NULL;
3689 }
3690 Py_DECREF(element);
3691 } else
3692 break;
3693 }
3694 Py_XDECREF(tmp.di_dict);
3695 /* check for error */
3696 if (tmp.di_dict != NULL) {
3697 /* we have an error */
3698 Py_DECREF(list);
3699 return NULL;
3700 }
Antoine Pitroua7013882012-04-05 00:04:20 +02003701 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003702}
3703
Guido van Rossum3ac67412007-02-10 18:55:06 +00003704/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003705/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003706/***********************************************/
3707
Guido van Rossumb90c8482007-02-10 01:11:45 +00003708/* The instance lay-out is the same for all three; but the type differs. */
3709
Guido van Rossumb90c8482007-02-10 01:11:45 +00003710static void
Eric Snow96c6af92015-05-29 22:21:39 -06003711dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003712{
INADA Naokia6296d32017-08-24 14:55:17 +09003713 /* bpo-31095: UnTrack is needed before calling any callbacks */
3714 _PyObject_GC_UNTRACK(dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003715 Py_XDECREF(dv->dv_dict);
3716 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003717}
3718
3719static int
Eric Snow96c6af92015-05-29 22:21:39 -06003720dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003721{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003722 Py_VISIT(dv->dv_dict);
3723 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003724}
3725
Guido van Rossum83825ac2007-02-10 04:54:19 +00003726static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003727dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003728{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003729 Py_ssize_t len = 0;
3730 if (dv->dv_dict != NULL)
3731 len = dv->dv_dict->ma_used;
3732 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003733}
3734
Eric Snow96c6af92015-05-29 22:21:39 -06003735PyObject *
3736_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003737{
Eric Snow96c6af92015-05-29 22:21:39 -06003738 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003739 if (dict == NULL) {
3740 PyErr_BadInternalCall();
3741 return NULL;
3742 }
3743 if (!PyDict_Check(dict)) {
3744 /* XXX Get rid of this restriction later */
3745 PyErr_Format(PyExc_TypeError,
3746 "%s() requires a dict argument, not '%s'",
3747 type->tp_name, dict->ob_type->tp_name);
3748 return NULL;
3749 }
Eric Snow96c6af92015-05-29 22:21:39 -06003750 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003751 if (dv == NULL)
3752 return NULL;
3753 Py_INCREF(dict);
3754 dv->dv_dict = (PyDictObject *)dict;
3755 _PyObject_GC_TRACK(dv);
3756 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003757}
3758
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003759/* TODO(guido): The views objects are not complete:
3760
3761 * support more set operations
3762 * support arbitrary mappings?
3763 - either these should be static or exported in dictobject.h
3764 - if public then they should probably be in builtins
3765*/
3766
Guido van Rossumaac530c2007-08-24 22:33:45 +00003767/* Return 1 if self is a subset of other, iterating over self;
3768 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003769static int
3770all_contained_in(PyObject *self, PyObject *other)
3771{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003772 PyObject *iter = PyObject_GetIter(self);
3773 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003775 if (iter == NULL)
3776 return -1;
3777 for (;;) {
3778 PyObject *next = PyIter_Next(iter);
3779 if (next == NULL) {
3780 if (PyErr_Occurred())
3781 ok = -1;
3782 break;
3783 }
3784 ok = PySequence_Contains(other, next);
3785 Py_DECREF(next);
3786 if (ok <= 0)
3787 break;
3788 }
3789 Py_DECREF(iter);
3790 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003791}
3792
3793static PyObject *
3794dictview_richcompare(PyObject *self, PyObject *other, int op)
3795{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003796 Py_ssize_t len_self, len_other;
3797 int ok;
3798 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003800 assert(self != NULL);
3801 assert(PyDictViewSet_Check(self));
3802 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003803
Brian Curtindfc80e32011-08-10 20:28:54 -05003804 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3805 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003807 len_self = PyObject_Size(self);
3808 if (len_self < 0)
3809 return NULL;
3810 len_other = PyObject_Size(other);
3811 if (len_other < 0)
3812 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003814 ok = 0;
3815 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003817 case Py_NE:
3818 case Py_EQ:
3819 if (len_self == len_other)
3820 ok = all_contained_in(self, other);
3821 if (op == Py_NE && ok >= 0)
3822 ok = !ok;
3823 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003825 case Py_LT:
3826 if (len_self < len_other)
3827 ok = all_contained_in(self, other);
3828 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003830 case Py_LE:
3831 if (len_self <= len_other)
3832 ok = all_contained_in(self, other);
3833 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003835 case Py_GT:
3836 if (len_self > len_other)
3837 ok = all_contained_in(other, self);
3838 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003840 case Py_GE:
3841 if (len_self >= len_other)
3842 ok = all_contained_in(other, self);
3843 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003844
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003845 }
3846 if (ok < 0)
3847 return NULL;
3848 result = ok ? Py_True : Py_False;
3849 Py_INCREF(result);
3850 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003851}
3852
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003853static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003854dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003855{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003856 PyObject *seq;
bennorthd7773d92018-01-26 15:46:01 +00003857 PyObject *result = NULL;
3858 Py_ssize_t rc;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003859
bennorthd7773d92018-01-26 15:46:01 +00003860 rc = Py_ReprEnter((PyObject *)dv);
3861 if (rc != 0) {
3862 return rc > 0 ? PyUnicode_FromString("...") : NULL;
3863 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003864 seq = PySequence_List((PyObject *)dv);
bennorthd7773d92018-01-26 15:46:01 +00003865 if (seq == NULL) {
3866 goto Done;
3867 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003868 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
3869 Py_DECREF(seq);
bennorthd7773d92018-01-26 15:46:01 +00003870
3871Done:
3872 Py_ReprLeave((PyObject *)dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003873 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003874}
3875
Guido van Rossum3ac67412007-02-10 18:55:06 +00003876/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003877
3878static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003879dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003880{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003881 if (dv->dv_dict == NULL) {
3882 Py_RETURN_NONE;
3883 }
3884 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003885}
3886
3887static int
Eric Snow96c6af92015-05-29 22:21:39 -06003888dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00003889{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003890 if (dv->dv_dict == NULL)
3891 return 0;
3892 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003893}
3894
Guido van Rossum83825ac2007-02-10 04:54:19 +00003895static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003896 (lenfunc)dictview_len, /* sq_length */
3897 0, /* sq_concat */
3898 0, /* sq_repeat */
3899 0, /* sq_item */
3900 0, /* sq_slice */
3901 0, /* sq_ass_item */
3902 0, /* sq_ass_slice */
3903 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003904};
3905
Guido van Rossum523259b2007-08-24 23:41:22 +00003906static PyObject*
3907dictviews_sub(PyObject* self, PyObject *other)
3908{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003909 PyObject *result = PySet_New(self);
3910 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003911 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003913 if (result == NULL)
3914 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003915
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003916 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003917 if (tmp == NULL) {
3918 Py_DECREF(result);
3919 return NULL;
3920 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003922 Py_DECREF(tmp);
3923 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003924}
3925
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003926PyObject*
3927_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00003928{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003929 PyObject *result = PySet_New(self);
3930 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003931 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003933 if (result == NULL)
3934 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003935
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003936 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_intersection_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003937 if (tmp == NULL) {
3938 Py_DECREF(result);
3939 return NULL;
3940 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003942 Py_DECREF(tmp);
3943 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003944}
3945
3946static PyObject*
3947dictviews_or(PyObject* self, PyObject *other)
3948{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003949 PyObject *result = PySet_New(self);
3950 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003951 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003953 if (result == NULL)
3954 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003955
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003956 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003957 if (tmp == NULL) {
3958 Py_DECREF(result);
3959 return NULL;
3960 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003962 Py_DECREF(tmp);
3963 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003964}
3965
3966static PyObject*
3967dictviews_xor(PyObject* self, PyObject *other)
3968{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003969 PyObject *result = PySet_New(self);
3970 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003971 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003973 if (result == NULL)
3974 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003975
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003976 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_symmetric_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003977 if (tmp == NULL) {
3978 Py_DECREF(result);
3979 return NULL;
3980 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003982 Py_DECREF(tmp);
3983 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003984}
3985
3986static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003987 0, /*nb_add*/
3988 (binaryfunc)dictviews_sub, /*nb_subtract*/
3989 0, /*nb_multiply*/
3990 0, /*nb_remainder*/
3991 0, /*nb_divmod*/
3992 0, /*nb_power*/
3993 0, /*nb_negative*/
3994 0, /*nb_positive*/
3995 0, /*nb_absolute*/
3996 0, /*nb_bool*/
3997 0, /*nb_invert*/
3998 0, /*nb_lshift*/
3999 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004000 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004001 (binaryfunc)dictviews_xor, /*nb_xor*/
4002 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00004003};
4004
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004005static PyObject*
4006dictviews_isdisjoint(PyObject *self, PyObject *other)
4007{
4008 PyObject *it;
4009 PyObject *item = NULL;
4010
4011 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06004012 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004013 Py_RETURN_TRUE;
4014 else
4015 Py_RETURN_FALSE;
4016 }
4017
4018 /* Iterate over the shorter object (only if other is a set,
4019 * because PySequence_Contains may be expensive otherwise): */
4020 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06004021 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004022 Py_ssize_t len_other = PyObject_Size(other);
4023 if (len_other == -1)
4024 return NULL;
4025
4026 if ((len_other > len_self)) {
4027 PyObject *tmp = other;
4028 other = self;
4029 self = tmp;
4030 }
4031 }
4032
4033 it = PyObject_GetIter(other);
4034 if (it == NULL)
4035 return NULL;
4036
4037 while ((item = PyIter_Next(it)) != NULL) {
4038 int contains = PySequence_Contains(self, item);
4039 Py_DECREF(item);
4040 if (contains == -1) {
4041 Py_DECREF(it);
4042 return NULL;
4043 }
4044
4045 if (contains) {
4046 Py_DECREF(it);
4047 Py_RETURN_FALSE;
4048 }
4049 }
4050 Py_DECREF(it);
4051 if (PyErr_Occurred())
4052 return NULL; /* PyIter_Next raised an exception. */
4053 Py_RETURN_TRUE;
4054}
4055
4056PyDoc_STRVAR(isdisjoint_doc,
4057"Return True if the view and the given iterable have a null intersection.");
4058
Guido van Rossumb90c8482007-02-10 01:11:45 +00004059static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004060 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4061 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004062 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004063};
4064
4065PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004066 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4067 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004068 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004069 0, /* tp_itemsize */
4070 /* methods */
4071 (destructor)dictview_dealloc, /* tp_dealloc */
4072 0, /* tp_print */
4073 0, /* tp_getattr */
4074 0, /* tp_setattr */
4075 0, /* tp_reserved */
4076 (reprfunc)dictview_repr, /* tp_repr */
4077 &dictviews_as_number, /* tp_as_number */
4078 &dictkeys_as_sequence, /* tp_as_sequence */
4079 0, /* tp_as_mapping */
4080 0, /* tp_hash */
4081 0, /* tp_call */
4082 0, /* tp_str */
4083 PyObject_GenericGetAttr, /* tp_getattro */
4084 0, /* tp_setattro */
4085 0, /* tp_as_buffer */
4086 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4087 0, /* tp_doc */
4088 (traverseproc)dictview_traverse, /* tp_traverse */
4089 0, /* tp_clear */
4090 dictview_richcompare, /* tp_richcompare */
4091 0, /* tp_weaklistoffset */
4092 (getiterfunc)dictkeys_iter, /* tp_iter */
4093 0, /* tp_iternext */
4094 dictkeys_methods, /* tp_methods */
4095 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004096};
4097
4098static PyObject *
4099dictkeys_new(PyObject *dict)
4100{
Eric Snow96c6af92015-05-29 22:21:39 -06004101 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004102}
4103
Guido van Rossum3ac67412007-02-10 18:55:06 +00004104/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004105
4106static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004107dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004108{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004109 if (dv->dv_dict == NULL) {
4110 Py_RETURN_NONE;
4111 }
4112 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004113}
4114
4115static int
Eric Snow96c6af92015-05-29 22:21:39 -06004116dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004117{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004118 int result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004119 PyObject *key, *value, *found;
4120 if (dv->dv_dict == NULL)
4121 return 0;
4122 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
4123 return 0;
4124 key = PyTuple_GET_ITEM(obj, 0);
4125 value = PyTuple_GET_ITEM(obj, 1);
Raymond Hettinger6692f012016-09-18 21:46:08 -07004126 found = PyDict_GetItemWithError((PyObject *)dv->dv_dict, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004127 if (found == NULL) {
4128 if (PyErr_Occurred())
4129 return -1;
4130 return 0;
4131 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004132 Py_INCREF(found);
4133 result = PyObject_RichCompareBool(value, found, Py_EQ);
4134 Py_DECREF(found);
4135 return result;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004136}
4137
Guido van Rossum83825ac2007-02-10 04:54:19 +00004138static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004139 (lenfunc)dictview_len, /* sq_length */
4140 0, /* sq_concat */
4141 0, /* sq_repeat */
4142 0, /* sq_item */
4143 0, /* sq_slice */
4144 0, /* sq_ass_item */
4145 0, /* sq_ass_slice */
4146 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004147};
4148
Guido van Rossumb90c8482007-02-10 01:11:45 +00004149static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004150 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4151 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004152 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004153};
4154
4155PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004156 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4157 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004158 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004159 0, /* tp_itemsize */
4160 /* methods */
4161 (destructor)dictview_dealloc, /* tp_dealloc */
4162 0, /* tp_print */
4163 0, /* tp_getattr */
4164 0, /* tp_setattr */
4165 0, /* tp_reserved */
4166 (reprfunc)dictview_repr, /* tp_repr */
4167 &dictviews_as_number, /* tp_as_number */
4168 &dictitems_as_sequence, /* tp_as_sequence */
4169 0, /* tp_as_mapping */
4170 0, /* tp_hash */
4171 0, /* tp_call */
4172 0, /* tp_str */
4173 PyObject_GenericGetAttr, /* tp_getattro */
4174 0, /* tp_setattro */
4175 0, /* tp_as_buffer */
4176 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4177 0, /* tp_doc */
4178 (traverseproc)dictview_traverse, /* tp_traverse */
4179 0, /* tp_clear */
4180 dictview_richcompare, /* tp_richcompare */
4181 0, /* tp_weaklistoffset */
4182 (getiterfunc)dictitems_iter, /* tp_iter */
4183 0, /* tp_iternext */
4184 dictitems_methods, /* tp_methods */
4185 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004186};
4187
4188static PyObject *
4189dictitems_new(PyObject *dict)
4190{
Eric Snow96c6af92015-05-29 22:21:39 -06004191 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004192}
4193
Guido van Rossum3ac67412007-02-10 18:55:06 +00004194/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004195
4196static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004197dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004198{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004199 if (dv->dv_dict == NULL) {
4200 Py_RETURN_NONE;
4201 }
4202 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004203}
4204
Guido van Rossum83825ac2007-02-10 04:54:19 +00004205static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004206 (lenfunc)dictview_len, /* sq_length */
4207 0, /* sq_concat */
4208 0, /* sq_repeat */
4209 0, /* sq_item */
4210 0, /* sq_slice */
4211 0, /* sq_ass_item */
4212 0, /* sq_ass_slice */
4213 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004214};
4215
Guido van Rossumb90c8482007-02-10 01:11:45 +00004216static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004217 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004218};
4219
4220PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004221 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4222 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004223 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004224 0, /* tp_itemsize */
4225 /* methods */
4226 (destructor)dictview_dealloc, /* tp_dealloc */
4227 0, /* tp_print */
4228 0, /* tp_getattr */
4229 0, /* tp_setattr */
4230 0, /* tp_reserved */
4231 (reprfunc)dictview_repr, /* tp_repr */
4232 0, /* tp_as_number */
4233 &dictvalues_as_sequence, /* tp_as_sequence */
4234 0, /* tp_as_mapping */
4235 0, /* tp_hash */
4236 0, /* tp_call */
4237 0, /* tp_str */
4238 PyObject_GenericGetAttr, /* tp_getattro */
4239 0, /* tp_setattro */
4240 0, /* tp_as_buffer */
4241 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4242 0, /* tp_doc */
4243 (traverseproc)dictview_traverse, /* tp_traverse */
4244 0, /* tp_clear */
4245 0, /* tp_richcompare */
4246 0, /* tp_weaklistoffset */
4247 (getiterfunc)dictvalues_iter, /* tp_iter */
4248 0, /* tp_iternext */
4249 dictvalues_methods, /* tp_methods */
4250 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004251};
4252
4253static PyObject *
4254dictvalues_new(PyObject *dict)
4255{
Eric Snow96c6af92015-05-29 22:21:39 -06004256 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004257}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004258
4259/* Returns NULL if cannot allocate a new PyDictKeysObject,
4260 but does not set an error */
4261PyDictKeysObject *
4262_PyDict_NewKeysForClass(void)
4263{
Victor Stinner742da042016-09-07 17:40:12 -07004264 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004265 if (keys == NULL)
4266 PyErr_Clear();
4267 else
4268 keys->dk_lookup = lookdict_split;
4269 return keys;
4270}
4271
4272#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
4273
4274PyObject *
4275PyObject_GenericGetDict(PyObject *obj, void *context)
4276{
4277 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
4278 if (dictptr == NULL) {
4279 PyErr_SetString(PyExc_AttributeError,
4280 "This object has no __dict__");
4281 return NULL;
4282 }
4283 dict = *dictptr;
4284 if (dict == NULL) {
4285 PyTypeObject *tp = Py_TYPE(obj);
4286 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
4287 DK_INCREF(CACHED_KEYS(tp));
4288 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
4289 }
4290 else {
4291 *dictptr = dict = PyDict_New();
4292 }
4293 }
4294 Py_XINCREF(dict);
4295 return dict;
4296}
4297
4298int
4299_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
Victor Stinner742da042016-09-07 17:40:12 -07004300 PyObject *key, PyObject *value)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004301{
4302 PyObject *dict;
4303 int res;
4304 PyDictKeysObject *cached;
4305
4306 assert(dictptr != NULL);
4307 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
4308 assert(dictptr != NULL);
4309 dict = *dictptr;
4310 if (dict == NULL) {
4311 DK_INCREF(cached);
4312 dict = new_dict_with_shared_keys(cached);
4313 if (dict == NULL)
4314 return -1;
4315 *dictptr = dict;
4316 }
4317 if (value == NULL) {
4318 res = PyDict_DelItem(dict, key);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004319 // Since key sharing dict doesn't allow deletion, PyDict_DelItem()
4320 // always converts dict to combined form.
4321 if ((cached = CACHED_KEYS(tp)) != NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004322 CACHED_KEYS(tp) = NULL;
4323 DK_DECREF(cached);
4324 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01004325 }
4326 else {
INADA Naoki2294f3a2017-02-12 13:51:30 +09004327 int was_shared = (cached == ((PyDictObject *)dict)->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004328 res = PyDict_SetItem(dict, key, value);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004329 if (was_shared &&
4330 (cached = CACHED_KEYS(tp)) != NULL &&
4331 cached != ((PyDictObject *)dict)->ma_keys) {
Victor Stinner3d3f2642016-12-15 17:21:23 +01004332 /* PyDict_SetItem() may call dictresize and convert split table
4333 * into combined table. In such case, convert it to split
4334 * table again and update type's shared key only when this is
4335 * the only dict sharing key with the type.
4336 *
4337 * This is to allow using shared key in class like this:
4338 *
4339 * class C:
4340 * def __init__(self):
4341 * # one dict resize happens
4342 * self.a, self.b, self.c = 1, 2, 3
4343 * self.d, self.e, self.f = 4, 5, 6
4344 * a = C()
4345 */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004346 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004347 CACHED_KEYS(tp) = make_keys_shared(dict);
Victor Stinner742da042016-09-07 17:40:12 -07004348 }
4349 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004350 CACHED_KEYS(tp) = NULL;
4351 }
4352 DK_DECREF(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004353 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
4354 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004355 }
4356 }
4357 } else {
4358 dict = *dictptr;
4359 if (dict == NULL) {
4360 dict = PyDict_New();
4361 if (dict == NULL)
4362 return -1;
4363 *dictptr = dict;
4364 }
4365 if (value == NULL) {
4366 res = PyDict_DelItem(dict, key);
4367 } else {
4368 res = PyDict_SetItem(dict, key, value);
4369 }
4370 }
4371 return res;
4372}
4373
4374void
4375_PyDictKeys_DecRef(PyDictKeysObject *keys)
4376{
4377 DK_DECREF(keys);
4378}