blob: e395f4dabb3e6c87c674e415e62c59b616c9c271 [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 Snow96c6af92015-05-29 22:21:39 -0600114#include "dict-common.h"
Victor Stinner990397e2016-09-09 20:22:59 -0700115#include "stringlib/eq.h" /* to get unicode_eq() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000116
Larry Hastings61272b72014-01-07 12:41:53 -0800117/*[clinic input]
Larry Hastingsc2047262014-01-25 20:43:29 -0800118class dict "PyDictObject *" "&PyDict_Type"
Larry Hastings61272b72014-01-07 12:41:53 -0800119[clinic start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -0800120/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f157a5a0ce9589d6]*/
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800121
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400122
123/*
124To ensure the lookup algorithm terminates, there must be at least one Unused
125slot (NULL key) in the table.
126To avoid slowing down lookups on a near-full table, we resize the table when
127it's USABLE_FRACTION (currently two-thirds) full.
128*/
Guido van Rossum16e93a81997-01-28 00:00:11 +0000129
Tim Peterseb28ef22001-06-02 05:27:19 +0000130#define PERTURB_SHIFT 5
131
Guido van Rossum16e93a81997-01-28 00:00:11 +0000132/*
Tim Peterseb28ef22001-06-02 05:27:19 +0000133Major subtleties ahead: Most hash schemes depend on having a "good" hash
134function, in the sense of simulating randomness. Python doesn't: its most
R David Murray537ad7a2016-07-10 12:33:18 -0400135important hash functions (for ints) are very regular in common
Tim Peterseb28ef22001-06-02 05:27:19 +0000136cases:
Tim Peters15d49292001-05-27 07:39:22 +0000137
R David Murray537ad7a2016-07-10 12:33:18 -0400138 >>>[hash(i) for i in range(4)]
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000139 [0, 1, 2, 3]
Tim Peters15d49292001-05-27 07:39:22 +0000140
Tim Peterseb28ef22001-06-02 05:27:19 +0000141This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
142the low-order i bits as the initial table index is extremely fast, and there
R David Murray537ad7a2016-07-10 12:33:18 -0400143are no collisions at all for dicts indexed by a contiguous range of ints. So
144this gives better-than-random behavior in common cases, and that's very
145desirable.
Tim Peters15d49292001-05-27 07:39:22 +0000146
Tim Peterseb28ef22001-06-02 05:27:19 +0000147OTOH, when collisions occur, the tendency to fill contiguous slices of the
148hash table makes a good collision resolution strategy crucial. Taking only
149the last i bits of the hash code is also vulnerable: for example, consider
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000151their own hash codes, and this fits in a dict of size 2**15, the last 15 bits
152 of every hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +0000153
Tim Peterseb28ef22001-06-02 05:27:19 +0000154But catering to unusual cases should not slow the usual ones, so we just take
155the last i bits anyway. It's up to collision resolution to do the rest. If
156we *usually* find the key we're looking for on the first try (and, it turns
157out, we usually do -- the table load factor is kept under 2/3, so the odds
158are solidly in our favor), then it makes best sense to keep the initial index
159computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +0000160
Tim Peterseb28ef22001-06-02 05:27:19 +0000161The first half of collision resolution is to visit table indices via this
162recurrence:
Tim Peters15d49292001-05-27 07:39:22 +0000163
Tim Peterseb28ef22001-06-02 05:27:19 +0000164 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +0000165
Tim Peterseb28ef22001-06-02 05:27:19 +0000166For any initial j in range(2**i), repeating that 2**i times generates each
167int in range(2**i) exactly once (see any text on random-number generation for
168proof). By itself, this doesn't help much: like linear probing (setting
169j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
170order. This would be bad, except that's not the only thing we do, and it's
171actually *good* in the common cases where hash keys are consecutive. In an
172example that's really too small to make this entirely clear, for a table of
173size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +0000174
Tim Peterseb28ef22001-06-02 05:27:19 +0000175 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
176
177If two things come in at index 5, the first place we look after is index 2,
178not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
179Linear probing is deadly in this case because there the fixed probe order
180is the *same* as the order consecutive keys are likely to arrive. But it's
181extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
182and certain that consecutive hash codes do not.
183
184The other half of the strategy is to get the other bits of the hash code
185into play. This is done by initializing a (unsigned) vrbl "perturb" to the
186full hash code, and changing the recurrence to:
187
Tim Peterseb28ef22001-06-02 05:27:19 +0000188 perturb >>= PERTURB_SHIFT;
INADA Naoki267941c2016-10-06 15:19:07 +0900189 j = (5*j) + 1 + perturb;
Tim Peterseb28ef22001-06-02 05:27:19 +0000190 use j % 2**i as the next table index;
191
192Now the probe sequence depends (eventually) on every bit in the hash code,
193and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
194because it quickly magnifies small differences in the bits that didn't affect
195the initial index. Note that because perturb is unsigned, if the recurrence
196is executed often enough perturb eventually becomes and remains 0. At that
197point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
198that's certain to find an empty slot eventually (since it generates every int
199in range(2**i), and we make sure there's always at least one empty slot).
200
201Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
202small so that the high bits of the hash code continue to affect the probe
203sequence across iterations; but you want it large so that in really bad cases
204the high-order hash bits have an effect on early iterations. 5 was "the
205best" in minimizing total collisions across experiments Tim Peters ran (on
206both normal and pathological cases), but 4 and 6 weren't significantly worse.
207
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000208Historical: Reimer Behrends contributed the idea of using a polynomial-based
Tim Peterseb28ef22001-06-02 05:27:19 +0000209approach, using repeated multiplication by x in GF(2**n) where an irreducible
210polynomial for each table size was chosen such that x was a primitive root.
211Christian Tismer later extended that to use division by x instead, as an
212efficient way to get the high bits of the hash code into play. This scheme
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000213also gave excellent collision statistics, but was more expensive: two
214if-tests were required inside the loop; computing "the next" index took about
215the same number of operations but without as much potential parallelism
216(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
217above, and then shifting perturb can be done while the table index is being
218masked); and the PyDictObject struct required a member to hold the table's
219polynomial. In Tim's experiments the current scheme ran faster, produced
220equally good collision statistics, needed less code & used less memory.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000221
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000222*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000223
Fred Drake1bff34a2000-08-31 19:31:38 +0000224/* forward declarations */
Victor Stinner742da042016-09-07 17:40:12 -0700225static Py_ssize_t lookdict(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900226 Py_hash_t hash, PyObject **value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700227static Py_ssize_t lookdict_unicode(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900228 Py_hash_t hash, PyObject **value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700229static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400230lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900231 Py_hash_t hash, PyObject **value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700232static Py_ssize_t lookdict_split(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900233 Py_hash_t hash, PyObject **value_addr);
Fred Drake1bff34a2000-08-31 19:31:38 +0000234
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400235static int dictresize(PyDictObject *mp, Py_ssize_t minused);
Tim Petersdea48ec2001-05-22 20:40:22 +0000236
Benjamin Peterson3c569292016-09-08 13:16:41 -0700237/*Global counter used to set ma_version_tag field of dictionary.
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700238 * It is incremented each time that a dictionary is created and each
239 * time that a dictionary is modified. */
240static uint64_t pydict_global_version = 0;
241
242#define DICT_NEXT_VERSION() (++pydict_global_version)
243
Victor Stinner742da042016-09-07 17:40:12 -0700244/* Dictionary reuse scheme to save calls to malloc and free */
Christian Heimes2202f872008-02-06 14:31:34 +0000245#ifndef PyDict_MAXFREELIST
246#define PyDict_MAXFREELIST 80
247#endif
248static PyDictObject *free_list[PyDict_MAXFREELIST];
249static int numfree = 0;
Victor Stinner742da042016-09-07 17:40:12 -0700250static PyDictKeysObject *keys_free_list[PyDict_MAXFREELIST];
251static int numfreekeys = 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000252
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300253#include "clinic/dictobject.c.h"
254
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100255int
256PyDict_ClearFreeList(void)
Christian Heimes77c02eb2008-02-09 02:18:51 +0000257{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 PyDictObject *op;
Victor Stinner742da042016-09-07 17:40:12 -0700259 int ret = numfree + numfreekeys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 while (numfree) {
261 op = free_list[--numfree];
262 assert(PyDict_CheckExact(op));
263 PyObject_GC_Del(op);
264 }
Victor Stinner742da042016-09-07 17:40:12 -0700265 while (numfreekeys) {
266 PyObject_FREE(keys_free_list[--numfreekeys]);
267 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100268 return ret;
269}
270
David Malcolm49526f42012-06-22 14:55:41 -0400271/* Print summary info about the state of the optimized allocator */
272void
273_PyDict_DebugMallocStats(FILE *out)
274{
275 _PyDebugAllocatorStats(out,
276 "free PyDictObject", numfree, sizeof(PyDictObject));
277}
278
279
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100280void
281PyDict_Fini(void)
282{
283 PyDict_ClearFreeList();
Christian Heimes77c02eb2008-02-09 02:18:51 +0000284}
285
Victor Stinner742da042016-09-07 17:40:12 -0700286#define DK_SIZE(dk) ((dk)->dk_size)
287#if SIZEOF_VOID_P > 4
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700288#define DK_IXSIZE(dk) \
289 (DK_SIZE(dk) <= 0xff ? \
290 1 : DK_SIZE(dk) <= 0xffff ? \
291 2 : DK_SIZE(dk) <= 0xffffffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700292 4 : sizeof(int64_t))
Victor Stinner742da042016-09-07 17:40:12 -0700293#else
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700294#define DK_IXSIZE(dk) \
295 (DK_SIZE(dk) <= 0xff ? \
296 1 : DK_SIZE(dk) <= 0xffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700297 2 : sizeof(int32_t))
Victor Stinner742da042016-09-07 17:40:12 -0700298#endif
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700299#define DK_ENTRIES(dk) \
Benjamin Peterson186122e2016-09-08 12:20:12 -0700300 ((PyDictKeyEntry*)(&(dk)->dk_indices.as_1[DK_SIZE(dk) * DK_IXSIZE(dk)]))
Victor Stinner742da042016-09-07 17:40:12 -0700301
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200302#define DK_DEBUG_INCREF _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA
303#define DK_DEBUG_DECREF _Py_DEC_REFTOTAL _Py_REF_DEBUG_COMMA
304
305#define DK_INCREF(dk) (DK_DEBUG_INCREF ++(dk)->dk_refcnt)
306#define DK_DECREF(dk) if (DK_DEBUG_DECREF (--(dk)->dk_refcnt) == 0) free_keys_object(dk)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400307#define DK_MASK(dk) (((dk)->dk_size)-1)
308#define IS_POWER_OF_2(x) (((x) & (x-1)) == 0)
309
Victor Stinner742da042016-09-07 17:40:12 -0700310/* lookup indices. returns DKIX_EMPTY, DKIX_DUMMY, or ix >=0 */
Benjamin Peterson73222252016-09-08 09:58:47 -0700311static inline Py_ssize_t
Victor Stinner742da042016-09-07 17:40:12 -0700312dk_get_index(PyDictKeysObject *keys, Py_ssize_t i)
313{
314 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700315 Py_ssize_t ix;
316
Victor Stinner742da042016-09-07 17:40:12 -0700317 if (s <= 0xff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700318 int8_t *indices = keys->dk_indices.as_1;
Victor Stinner208857e2016-09-08 11:35:46 -0700319 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700320 }
321 else if (s <= 0xffff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700322 int16_t *indices = keys->dk_indices.as_2;
Victor Stinner208857e2016-09-08 11:35:46 -0700323 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700324 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700325#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300326 else if (s > 0xffffffff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700327 int64_t *indices = keys->dk_indices.as_8;
Victor Stinner208857e2016-09-08 11:35:46 -0700328 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700329 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700330#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300331 else {
332 int32_t *indices = keys->dk_indices.as_4;
333 ix = indices[i];
334 }
Victor Stinner71211e32016-09-08 10:52:46 -0700335 assert(ix >= DKIX_DUMMY);
336 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700337}
338
339/* write to indices. */
Benjamin Peterson73222252016-09-08 09:58:47 -0700340static inline void
Victor Stinner742da042016-09-07 17:40:12 -0700341dk_set_index(PyDictKeysObject *keys, Py_ssize_t i, Py_ssize_t ix)
342{
343 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700344
345 assert(ix >= DKIX_DUMMY);
346
Victor Stinner742da042016-09-07 17:40:12 -0700347 if (s <= 0xff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700348 int8_t *indices = keys->dk_indices.as_1;
Victor Stinner71211e32016-09-08 10:52:46 -0700349 assert(ix <= 0x7f);
Victor Stinner208857e2016-09-08 11:35:46 -0700350 indices[i] = (char)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700351 }
352 else if (s <= 0xffff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700353 int16_t *indices = keys->dk_indices.as_2;
Victor Stinner71211e32016-09-08 10:52:46 -0700354 assert(ix <= 0x7fff);
Victor Stinner208857e2016-09-08 11:35:46 -0700355 indices[i] = (int16_t)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700356 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700357#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300358 else if (s > 0xffffffff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700359 int64_t *indices = keys->dk_indices.as_8;
Victor Stinner208857e2016-09-08 11:35:46 -0700360 indices[i] = ix;
Victor Stinner742da042016-09-07 17:40:12 -0700361 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700362#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300363 else {
364 int32_t *indices = keys->dk_indices.as_4;
365 assert(ix <= 0x7fffffff);
366 indices[i] = (int32_t)ix;
367 }
Victor Stinner742da042016-09-07 17:40:12 -0700368}
369
370
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200371/* USABLE_FRACTION is the maximum dictionary load.
Victor Stinner742da042016-09-07 17:40:12 -0700372 * Increasing this ratio makes dictionaries more dense resulting in more
373 * collisions. Decreasing it improves sparseness at the expense of spreading
374 * indices over more cache lines and at the cost of total memory consumed.
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200375 *
376 * USABLE_FRACTION must obey the following:
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400377 * (0 < USABLE_FRACTION(n) < n) for all n >= 2
378 *
Victor Stinner742da042016-09-07 17:40:12 -0700379 * USABLE_FRACTION should be quick to calculate.
380 * Fractions around 1/2 to 2/3 seem to work well in practice.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400381 */
Victor Stinner742da042016-09-07 17:40:12 -0700382#define USABLE_FRACTION(n) (((n) << 1)/3)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400383
Victor Stinner742da042016-09-07 17:40:12 -0700384/* ESTIMATE_SIZE is reverse function of USABLE_FRACTION.
385 * This can be used to reserve enough size to insert n entries without
386 * resizing.
387 */
INADA Naoki92c50ee2016-11-22 00:57:02 +0900388#define ESTIMATE_SIZE(n) (((n)*3+1) >> 1)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400389
Victor Stinner742da042016-09-07 17:40:12 -0700390/* Alternative fraction that is otherwise close enough to 2n/3 to make
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400391 * little difference. 8 * 2/3 == 8 * 5/8 == 5. 16 * 2/3 == 16 * 5/8 == 10.
392 * 32 * 2/3 = 21, 32 * 5/8 = 20.
393 * Its advantage is that it is faster to compute on machines with slow division.
394 * #define USABLE_FRACTION(n) (((n) >> 1) + ((n) >> 2) - ((n) >> 3))
Victor Stinner742da042016-09-07 17:40:12 -0700395 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400396
Victor Stinnera9f61a52013-07-16 22:17:26 +0200397/* GROWTH_RATE. Growth rate upon hitting maximum load.
398 * Currently set to used*2 + capacity/2.
399 * This means that dicts double in size when growing without deletions,
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700400 * but have more head room when the number of deletions is on a par with the
401 * number of insertions.
402 * Raising this to used*4 doubles memory consumption depending on the size of
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200403 * the dictionary, but results in half the number of resizes, less effort to
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700404 * resize.
405 * GROWTH_RATE was set to used*4 up to version 3.2.
406 * GROWTH_RATE was set to used*2 in version 3.3.0
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200407 */
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700408#define GROWTH_RATE(d) (((d)->ma_used*2)+((d)->ma_keys->dk_size>>1))
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400409
410#define ENSURE_ALLOWS_DELETIONS(d) \
411 if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \
412 (d)->ma_keys->dk_lookup = lookdict_unicode; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400414
415/* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
416 * (which cannot fail and thus can do no allocation).
417 */
418static PyDictKeysObject empty_keys_struct = {
Serhiy Storchaka97932e42016-09-26 23:01:23 +0300419 1, /* dk_refcnt */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400420 1, /* dk_size */
421 lookdict_split, /* dk_lookup */
422 0, /* dk_usable (immutable) */
Victor Stinner742da042016-09-07 17:40:12 -0700423 0, /* dk_nentries */
Benjamin Peterson186122e2016-09-08 12:20:12 -0700424 .dk_indices = { .as_1 = {DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY,
425 DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY}},
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400426};
427
428static PyObject *empty_values[1] = { NULL };
429
430#define Py_EMPTY_KEYS &empty_keys_struct
431
Victor Stinner611b0fa2016-09-14 15:02:01 +0200432/* Uncomment to check the dict content in _PyDict_CheckConsistency() */
433/* #define DEBUG_PYDICT */
434
435
T. Woutersa00c3fd2017-03-31 09:14:41 -0700436#ifndef NDEBUG
Victor Stinner611b0fa2016-09-14 15:02:01 +0200437static int
438_PyDict_CheckConsistency(PyDictObject *mp)
439{
440 PyDictKeysObject *keys = mp->ma_keys;
441 int splitted = _PyDict_HasSplitTable(mp);
442 Py_ssize_t usable = USABLE_FRACTION(keys->dk_size);
443#ifdef DEBUG_PYDICT
444 PyDictKeyEntry *entries = DK_ENTRIES(keys);
445 Py_ssize_t i;
446#endif
447
448 assert(0 <= mp->ma_used && mp->ma_used <= usable);
449 assert(IS_POWER_OF_2(keys->dk_size));
450 assert(0 <= keys->dk_usable
451 && keys->dk_usable <= usable);
452 assert(0 <= keys->dk_nentries
453 && keys->dk_nentries <= usable);
454 assert(keys->dk_usable + keys->dk_nentries <= usable);
455
456 if (!splitted) {
457 /* combined table */
458 assert(keys->dk_refcnt == 1);
459 }
460
461#ifdef DEBUG_PYDICT
462 for (i=0; i < keys->dk_size; i++) {
463 Py_ssize_t ix = dk_get_index(keys, i);
464 assert(DKIX_DUMMY <= ix && ix <= usable);
465 }
466
467 for (i=0; i < usable; i++) {
468 PyDictKeyEntry *entry = &entries[i];
469 PyObject *key = entry->me_key;
470
471 if (key != NULL) {
472 if (PyUnicode_CheckExact(key)) {
473 Py_hash_t hash = ((PyASCIIObject *)key)->hash;
474 assert(hash != -1);
475 assert(entry->me_hash == hash);
476 }
477 else {
478 /* test_dict fails if PyObject_Hash() is called again */
479 assert(entry->me_hash != -1);
480 }
481 if (!splitted) {
482 assert(entry->me_value != NULL);
483 }
484 }
485
486 if (splitted) {
487 assert(entry->me_value == NULL);
488 }
489 }
490
491 if (splitted) {
492 /* splitted table */
493 for (i=0; i < mp->ma_used; i++) {
494 assert(mp->ma_values[i] != NULL);
495 }
496 }
497#endif
498
499 return 1;
500}
501#endif
502
503
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400504static PyDictKeysObject *new_keys_object(Py_ssize_t size)
505{
506 PyDictKeysObject *dk;
Victor Stinner742da042016-09-07 17:40:12 -0700507 Py_ssize_t es, usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400508
Victor Stinner742da042016-09-07 17:40:12 -0700509 assert(size >= PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400510 assert(IS_POWER_OF_2(size));
Victor Stinner742da042016-09-07 17:40:12 -0700511
512 usable = USABLE_FRACTION(size);
513 if (size <= 0xff) {
514 es = 1;
515 }
516 else if (size <= 0xffff) {
517 es = 2;
518 }
519#if SIZEOF_VOID_P > 4
520 else if (size <= 0xffffffff) {
521 es = 4;
522 }
523#endif
524 else {
525 es = sizeof(Py_ssize_t);
526 }
527
528 if (size == PyDict_MINSIZE && numfreekeys > 0) {
529 dk = keys_free_list[--numfreekeys];
530 }
531 else {
Victor Stinner98ee9d52016-09-08 09:33:56 -0700532 dk = PyObject_MALLOC(sizeof(PyDictKeysObject)
533 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
534 + es * size
535 + sizeof(PyDictKeyEntry) * usable);
Victor Stinner742da042016-09-07 17:40:12 -0700536 if (dk == NULL) {
537 PyErr_NoMemory();
538 return NULL;
539 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400540 }
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200541 DK_DEBUG_INCREF dk->dk_refcnt = 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400542 dk->dk_size = size;
Victor Stinner742da042016-09-07 17:40:12 -0700543 dk->dk_usable = usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400544 dk->dk_lookup = lookdict_unicode_nodummy;
Victor Stinner742da042016-09-07 17:40:12 -0700545 dk->dk_nentries = 0;
Benjamin Peterson186122e2016-09-08 12:20:12 -0700546 memset(&dk->dk_indices.as_1[0], 0xff, es * size);
Victor Stinner742da042016-09-07 17:40:12 -0700547 memset(DK_ENTRIES(dk), 0, sizeof(PyDictKeyEntry) * usable);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400548 return dk;
549}
550
551static void
552free_keys_object(PyDictKeysObject *keys)
553{
Victor Stinner742da042016-09-07 17:40:12 -0700554 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400555 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -0700556 for (i = 0, n = keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400557 Py_XDECREF(entries[i].me_key);
558 Py_XDECREF(entries[i].me_value);
559 }
Victor Stinner742da042016-09-07 17:40:12 -0700560 if (keys->dk_size == PyDict_MINSIZE && numfreekeys < PyDict_MAXFREELIST) {
561 keys_free_list[numfreekeys++] = keys;
562 return;
563 }
Raymond Hettingerce5179f2016-01-31 08:56:21 -0800564 PyObject_FREE(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400565}
566
567#define new_values(size) PyMem_NEW(PyObject *, size)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400568#define free_values(values) PyMem_FREE(values)
569
570/* Consumes a reference to the keys object */
571static PyObject *
572new_dict(PyDictKeysObject *keys, PyObject **values)
573{
574 PyDictObject *mp;
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200575 assert(keys != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 if (numfree) {
577 mp = free_list[--numfree];
578 assert (mp != NULL);
579 assert (Py_TYPE(mp) == &PyDict_Type);
580 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400582 else {
583 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
584 if (mp == NULL) {
585 DK_DECREF(keys);
586 free_values(values);
587 return NULL;
588 }
589 }
590 mp->ma_keys = keys;
591 mp->ma_values = values;
592 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700593 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +0200594 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000596}
597
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400598/* Consumes a reference to the keys object */
599static PyObject *
600new_dict_with_shared_keys(PyDictKeysObject *keys)
601{
602 PyObject **values;
603 Py_ssize_t i, size;
604
Victor Stinner742da042016-09-07 17:40:12 -0700605 size = USABLE_FRACTION(DK_SIZE(keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400606 values = new_values(size);
607 if (values == NULL) {
608 DK_DECREF(keys);
609 return PyErr_NoMemory();
610 }
611 for (i = 0; i < size; i++) {
612 values[i] = NULL;
613 }
614 return new_dict(keys, values);
615}
616
617PyObject *
618PyDict_New(void)
619{
Victor Stinner742da042016-09-07 17:40:12 -0700620 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200621 if (keys == NULL)
622 return NULL;
623 return new_dict(keys, NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400624}
625
Victor Stinner742da042016-09-07 17:40:12 -0700626/* Search index of hash table from offset of entry table */
627static Py_ssize_t
628lookdict_index(PyDictKeysObject *k, Py_hash_t hash, Py_ssize_t index)
629{
Victor Stinner742da042016-09-07 17:40:12 -0700630 size_t mask = DK_MASK(k);
INADA Naoki073ae482017-06-23 15:22:50 +0900631 size_t perturb = (size_t)hash;
632 size_t i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700633
INADA Naoki073ae482017-06-23 15:22:50 +0900634 for (;;) {
635 Py_ssize_t ix = dk_get_index(k, i);
Victor Stinner742da042016-09-07 17:40:12 -0700636 if (ix == index) {
637 return i;
638 }
639 if (ix == DKIX_EMPTY) {
640 return DKIX_EMPTY;
641 }
INADA Naoki073ae482017-06-23 15:22:50 +0900642 perturb >>= PERTURB_SHIFT;
643 i = mask & (i*5 + perturb + 1);
Victor Stinner742da042016-09-07 17:40:12 -0700644 }
645 assert(0); /* NOT REACHED */
646 return DKIX_ERROR;
647}
648
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000649/*
650The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000651This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000652Open addressing is preferred over chaining since the link overhead for
653chaining would be substantial (100% with typical malloc overhead).
654
Tim Peterseb28ef22001-06-02 05:27:19 +0000655The initial probe index is computed as hash mod the table size. Subsequent
656probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000657
658All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000659
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000660The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000661contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000662Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000663
Victor Stinner742da042016-09-07 17:40:12 -0700664lookdict() is general-purpose, and may return DKIX_ERROR if (and only if) a
Victor Stinnera4348cc2016-09-08 12:01:25 -0700665comparison raises an exception.
Guido van Rossum89d8c602007-09-18 17:26:56 +0000666lookdict_unicode() below is specialized to string keys, comparison of which can
INADA Naoki1b8df102017-02-20 22:48:10 +0900667never raise an exception; that function can never return DKIX_ERROR when key
668is string. Otherwise, it falls back to lookdict().
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400669lookdict_unicode_nodummy is further specialized for string keys that cannot be
670the <dummy> value.
INADA Naoki778928b2017-08-03 23:45:15 +0900671For both, when the key isn't found a DKIX_EMPTY is returned.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000672*/
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100673static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400674lookdict(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900675 Py_hash_t hash, PyObject **value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000676{
INADA Naoki778928b2017-08-03 23:45:15 +0900677 size_t i, mask, perturb;
Victor Stinner742da042016-09-07 17:40:12 -0700678 PyDictKeysObject *dk;
INADA Naoki778928b2017-08-03 23:45:15 +0900679 PyDictKeyEntry *ep0;
Tim Peterseb28ef22001-06-02 05:27:19 +0000680
Antoine Pitrou9a234902012-05-13 20:48:01 +0200681top:
Victor Stinner742da042016-09-07 17:40:12 -0700682 dk = mp->ma_keys;
Victor Stinner742da042016-09-07 17:40:12 -0700683 ep0 = DK_ENTRIES(dk);
INADA Naoki778928b2017-08-03 23:45:15 +0900684 mask = DK_MASK(dk);
685 perturb = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700687
INADA Naoki778928b2017-08-03 23:45:15 +0900688 for (;;) {
689 Py_ssize_t ix = dk_get_index(dk, i);
Victor Stinner742da042016-09-07 17:40:12 -0700690 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700691 *value_addr = NULL;
692 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400693 }
INADA Naoki778928b2017-08-03 23:45:15 +0900694 if (ix >= 0) {
695 PyDictKeyEntry *ep = &ep0[ix];
696 assert(ep->me_key != NULL);
697 if (ep->me_key == key) {
698 *value_addr = ep->me_value;
699 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700700 }
INADA Naoki778928b2017-08-03 23:45:15 +0900701 if (ep->me_hash == hash) {
702 PyObject *startkey = ep->me_key;
703 Py_INCREF(startkey);
704 int cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
705 Py_DECREF(startkey);
706 if (cmp < 0) {
707 *value_addr = NULL;
708 return DKIX_ERROR;
709 }
710 if (dk == mp->ma_keys && ep->me_key == startkey) {
711 if (cmp > 0) {
712 *value_addr = ep->me_value;
713 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700714 }
INADA Naoki778928b2017-08-03 23:45:15 +0900715 }
716 else {
717 /* The dict was mutated, restart */
718 goto top;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400719 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 }
INADA Naoki778928b2017-08-03 23:45:15 +0900722 perturb >>= PERTURB_SHIFT;
723 i = (i*5 + perturb + 1) & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 }
725 assert(0); /* NOT REACHED */
726 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000727}
728
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400729/* Specialized version for string-only keys */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100730static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400731lookdict_unicode(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900732 Py_hash_t hash, PyObject **value_addr)
Fred Drake1bff34a2000-08-31 19:31:38 +0000733{
Victor Stinner742da042016-09-07 17:40:12 -0700734 assert(mp->ma_values == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 /* Make sure this function doesn't have to handle non-unicode keys,
736 including subclasses of str; e.g., one reason to subclass
737 unicodes is to override __eq__, and for speed we don't cater to
738 that here. */
739 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400740 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900741 return lookdict(mp, key, hash, value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 }
Tim Peters15d49292001-05-27 07:39:22 +0000743
INADA Naoki778928b2017-08-03 23:45:15 +0900744 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
745 size_t mask = DK_MASK(mp->ma_keys);
746 size_t perturb = (size_t)hash;
747 size_t i = (size_t)hash & mask;
748
749 for (;;) {
750 Py_ssize_t ix = dk_get_index(mp->ma_keys, i);
Victor Stinner742da042016-09-07 17:40:12 -0700751 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700752 *value_addr = NULL;
753 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400754 }
INADA Naoki778928b2017-08-03 23:45:15 +0900755 if (ix >= 0) {
756 PyDictKeyEntry *ep = &ep0[ix];
757 assert(ep->me_key != NULL);
758 assert(PyUnicode_CheckExact(ep->me_key));
759 if (ep->me_key == key ||
760 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
761 *value_addr = ep->me_value;
762 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700763 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400764 }
INADA Naoki778928b2017-08-03 23:45:15 +0900765 perturb >>= PERTURB_SHIFT;
766 i = mask & (i*5 + perturb + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 }
INADA Naoki778928b2017-08-03 23:45:15 +0900768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 assert(0); /* NOT REACHED */
770 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000771}
772
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400773/* Faster version of lookdict_unicode when it is known that no <dummy> keys
774 * will be present. */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100775static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400776lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900777 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400778{
Victor Stinner742da042016-09-07 17:40:12 -0700779 assert(mp->ma_values == NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400780 /* 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)) {
785 mp->ma_keys->dk_lookup = lookdict;
INADA Naoki778928b2017-08-03 23:45:15 +0900786 return lookdict(mp, key, hash, value_addr);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400787 }
INADA Naoki778928b2017-08-03 23:45:15 +0900788
789 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 assert (ix != DKIX_DUMMY);
797 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700798 *value_addr = NULL;
799 return DKIX_EMPTY;
800 }
INADA Naoki778928b2017-08-03 23:45:15 +0900801 PyDictKeyEntry *ep = &ep0[ix];
802 assert(ep->me_key != NULL);
803 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700804 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400805 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900806 *value_addr = ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700807 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400808 }
INADA Naoki778928b2017-08-03 23:45:15 +0900809 perturb >>= PERTURB_SHIFT;
810 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400811 }
812 assert(0); /* NOT REACHED */
813 return 0;
814}
815
816/* Version of lookdict for split tables.
817 * All split tables and only split tables use this lookup function.
818 * Split tables only contain unicode keys and no dummy keys,
819 * so algorithm is the same as lookdict_unicode_nodummy.
820 */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100821static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400822lookdict_split(PyDictObject *mp, PyObject *key,
INADA Naoki778928b2017-08-03 23:45:15 +0900823 Py_hash_t hash, PyObject **value_addr)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400824{
Victor Stinner742da042016-09-07 17:40:12 -0700825 /* mp must split table */
826 assert(mp->ma_values != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400827 if (!PyUnicode_CheckExact(key)) {
INADA Naoki778928b2017-08-03 23:45:15 +0900828 Py_ssize_t ix = lookdict(mp, key, hash, value_addr);
Victor Stinner742da042016-09-07 17:40:12 -0700829 if (ix >= 0) {
INADA Naokiba609772016-12-07 20:41:42 +0900830 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700831 }
832 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400833 }
Victor Stinner742da042016-09-07 17:40:12 -0700834
INADA Naoki778928b2017-08-03 23:45:15 +0900835 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
836 size_t mask = DK_MASK(mp->ma_keys);
837 size_t perturb = (size_t)hash;
838 size_t i = (size_t)hash & mask;
839
840 for (;;) {
841 Py_ssize_t ix = dk_get_index(mp->ma_keys, i);
842 assert (ix != DKIX_DUMMY);
Victor Stinner742da042016-09-07 17:40:12 -0700843 if (ix == DKIX_EMPTY) {
Victor Stinner742da042016-09-07 17:40:12 -0700844 *value_addr = NULL;
845 return DKIX_EMPTY;
846 }
INADA Naoki778928b2017-08-03 23:45:15 +0900847 PyDictKeyEntry *ep = &ep0[ix];
848 assert(ep->me_key != NULL);
849 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700850 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400851 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900852 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700853 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400854 }
INADA Naoki778928b2017-08-03 23:45:15 +0900855 perturb >>= PERTURB_SHIFT;
856 i = mask & (i*5 + perturb + 1);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400857 }
858 assert(0); /* NOT REACHED */
859 return 0;
860}
861
Benjamin Petersonfb886362010-04-24 18:21:17 +0000862int
863_PyDict_HasOnlyStringKeys(PyObject *dict)
864{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 Py_ssize_t pos = 0;
866 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000867 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400869 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 return 1;
871 while (PyDict_Next(dict, &pos, &key, &value))
872 if (!PyUnicode_Check(key))
873 return 0;
874 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000875}
876
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000877#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 do { \
879 if (!_PyObject_GC_IS_TRACKED(mp)) { \
880 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
881 _PyObject_GC_MAY_BE_TRACKED(value)) { \
882 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 } \
884 } \
885 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000886
887void
888_PyDict_MaybeUntrack(PyObject *op)
889{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 PyDictObject *mp;
891 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -0700892 Py_ssize_t i, numentries;
893 PyDictKeyEntry *ep0;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
896 return;
897
898 mp = (PyDictObject *) op;
Victor Stinner742da042016-09-07 17:40:12 -0700899 ep0 = DK_ENTRIES(mp->ma_keys);
900 numentries = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400901 if (_PyDict_HasSplitTable(mp)) {
Victor Stinner742da042016-09-07 17:40:12 -0700902 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400903 if ((value = mp->ma_values[i]) == NULL)
904 continue;
905 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
Victor Stinner742da042016-09-07 17:40:12 -0700906 assert(!_PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400907 return;
908 }
909 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400911 else {
Victor Stinner742da042016-09-07 17:40:12 -0700912 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400913 if ((value = ep0[i].me_value) == NULL)
914 continue;
915 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
916 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
917 return;
918 }
919 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000921}
922
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400923/* Internal function to find slot for an item from its hash
Victor Stinner3c336c52016-09-12 14:17:40 +0200924 when it is known that the key is not present in the dict.
925
926 The dict must be combined. */
INADA Naokiba609772016-12-07 20:41:42 +0900927static Py_ssize_t
INADA Naoki778928b2017-08-03 23:45:15 +0900928find_empty_slot(PyDictKeysObject *keys, Py_hash_t hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000929{
INADA Naoki778928b2017-08-03 23:45:15 +0900930 assert(keys != NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000931
INADA Naoki778928b2017-08-03 23:45:15 +0900932 const size_t mask = DK_MASK(keys);
933 size_t i = hash & mask;
934 Py_ssize_t ix = dk_get_index(keys, i);
935 for (size_t perturb = hash; ix >= 0;) {
INADA Naoki267941c2016-10-06 15:19:07 +0900936 perturb >>= PERTURB_SHIFT;
INADA Naoki778928b2017-08-03 23:45:15 +0900937 i = (i*5 + perturb + 1) & mask;
938 ix = dk_get_index(keys, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 }
INADA Naoki778928b2017-08-03 23:45:15 +0900940 return i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000941}
942
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400943static int
944insertion_resize(PyDictObject *mp)
945{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700946 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400947}
Antoine Pitroue965d972012-02-27 00:45:12 +0100948
949/*
950Internal routine to insert a new item into the table.
951Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +0100952Returns -1 if an error occurred, or 0 on success.
953*/
954static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400955insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +0100956{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400957 PyObject *old_value;
INADA Naokiba609772016-12-07 20:41:42 +0900958 PyDictKeyEntry *ep;
Antoine Pitroue965d972012-02-27 00:45:12 +0100959
Serhiy Storchaka753bca32017-05-20 12:30:02 +0300960 Py_INCREF(key);
961 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400962 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
963 if (insertion_resize(mp) < 0)
Serhiy Storchaka753bca32017-05-20 12:30:02 +0300964 goto Fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400965 }
966
INADA Naoki778928b2017-08-03 23:45:15 +0900967 Py_ssize_t ix = mp->ma_keys->dk_lookup(mp, key, hash, &old_value);
Serhiy Storchaka753bca32017-05-20 12:30:02 +0300968 if (ix == DKIX_ERROR)
969 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -0700970
Antoine Pitroud6967322014-10-18 00:35:00 +0200971 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400972 MAINTAIN_TRACKING(mp, key, value);
Victor Stinner742da042016-09-07 17:40:12 -0700973
974 /* When insertion order is different from shared key, we can't share
975 * the key anymore. Convert this instance to combine table.
976 */
977 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +0900978 ((ix >= 0 && old_value == NULL && mp->ma_used != ix) ||
Victor Stinner742da042016-09-07 17:40:12 -0700979 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +0300980 if (insertion_resize(mp) < 0)
981 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -0700982 ix = DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400983 }
Victor Stinner742da042016-09-07 17:40:12 -0700984
985 if (ix == DKIX_EMPTY) {
986 /* Insert into new slot. */
INADA Naokiba609772016-12-07 20:41:42 +0900987 assert(old_value == NULL);
Victor Stinner742da042016-09-07 17:40:12 -0700988 if (mp->ma_keys->dk_usable <= 0) {
989 /* Need to resize. */
Serhiy Storchaka753bca32017-05-20 12:30:02 +0300990 if (insertion_resize(mp) < 0)
991 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -0700992 }
INADA Naoki778928b2017-08-03 23:45:15 +0900993 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naokiba609772016-12-07 20:41:42 +0900994 ep = &DK_ENTRIES(mp->ma_keys)[mp->ma_keys->dk_nentries];
Victor Stinner742da042016-09-07 17:40:12 -0700995 dk_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Victor Stinner742da042016-09-07 17:40:12 -0700996 ep->me_key = key;
997 ep->me_hash = hash;
998 if (mp->ma_values) {
999 assert (mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
1000 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001001 }
1002 else {
Victor Stinner742da042016-09-07 17:40:12 -07001003 ep->me_value = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001004 }
1005 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001006 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07001007 mp->ma_keys->dk_usable--;
1008 mp->ma_keys->dk_nentries++;
1009 assert(mp->ma_keys->dk_usable >= 0);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001010 assert(_PyDict_CheckConsistency(mp));
Victor Stinner742da042016-09-07 17:40:12 -07001011 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001012 }
Victor Stinner742da042016-09-07 17:40:12 -07001013
INADA Naokiba609772016-12-07 20:41:42 +09001014 if (_PyDict_HasSplitTable(mp)) {
1015 mp->ma_values[ix] = value;
1016 if (old_value == NULL) {
1017 /* pending state */
1018 assert(ix == mp->ma_used);
1019 mp->ma_used++;
1020 }
1021 }
1022 else {
1023 assert(old_value != NULL);
1024 DK_ENTRIES(mp->ma_keys)[ix].me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07001025 }
1026
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001027 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokiba609772016-12-07 20:41:42 +09001028 Py_XDECREF(old_value); /* which **CAN** re-enter (see issue #22653) */
Victor Stinner611b0fa2016-09-14 15:02:01 +02001029 assert(_PyDict_CheckConsistency(mp));
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001030 Py_DECREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001031 return 0;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001032
1033Fail:
1034 Py_DECREF(value);
1035 Py_DECREF(key);
1036 return -1;
Antoine Pitroue965d972012-02-27 00:45:12 +01001037}
1038
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001039/*
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001040Internal routine used by dictresize() to buid a hashtable of entries.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001041*/
1042static void
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001043build_indices(PyDictKeysObject *keys, PyDictKeyEntry *ep, Py_ssize_t n)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001044{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001045 size_t mask = (size_t)DK_SIZE(keys) - 1;
1046 for (Py_ssize_t ix = 0; ix != n; ix++, ep++) {
1047 Py_hash_t hash = ep->me_hash;
1048 size_t i = hash & mask;
1049 for (size_t perturb = hash; dk_get_index(keys, i) != DKIX_EMPTY;) {
1050 perturb >>= PERTURB_SHIFT;
INADA Naoki870c2862017-06-24 09:03:19 +09001051 i = mask & (i*5 + perturb + 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001052 }
1053 dk_set_index(keys, i, ix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001055}
1056
1057/*
1058Restructure the table by allocating a new table and reinserting all
1059items again. When entries have been deleted, the new table may
1060actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001061If a table is split (its keys and hashes are shared, its values are not),
1062then the values are temporarily copied into the table, it is resized as
1063a combined table, then the me_value slots in the old table are NULLed out.
1064After resizing a table is always combined,
1065but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001066*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001067static int
Victor Stinner3d3f2642016-12-15 17:21:23 +01001068dictresize(PyDictObject *mp, Py_ssize_t minsize)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001069{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001070 Py_ssize_t newsize, numentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001071 PyDictKeysObject *oldkeys;
1072 PyObject **oldvalues;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001073 PyDictKeyEntry *oldentries, *newentries;
Tim Peters91a364d2001-05-19 07:04:38 +00001074
Victor Stinner742da042016-09-07 17:40:12 -07001075 /* Find the smallest table size > minused. */
1076 for (newsize = PyDict_MINSIZE;
Victor Stinner3d3f2642016-12-15 17:21:23 +01001077 newsize < minsize && newsize > 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 newsize <<= 1)
1079 ;
1080 if (newsize <= 0) {
1081 PyErr_NoMemory();
1082 return -1;
1083 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001084
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001085 oldkeys = mp->ma_keys;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001086
1087 /* NOTE: Current odict checks mp->ma_keys to detect resize happen.
1088 * So we can't reuse oldkeys even if oldkeys->dk_size == newsize.
1089 * TODO: Try reusing oldkeys when reimplement odict.
1090 */
1091
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001092 /* Allocate a new table. */
1093 mp->ma_keys = new_keys_object(newsize);
1094 if (mp->ma_keys == NULL) {
1095 mp->ma_keys = oldkeys;
1096 return -1;
1097 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01001098 // New table must be large enough.
1099 assert(mp->ma_keys->dk_usable >= mp->ma_used);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001100 if (oldkeys->dk_lookup == lookdict)
1101 mp->ma_keys->dk_lookup = lookdict;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001102
1103 numentries = mp->ma_used;
1104 oldentries = DK_ENTRIES(oldkeys);
1105 newentries = DK_ENTRIES(mp->ma_keys);
1106 oldvalues = mp->ma_values;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001107 if (oldvalues != NULL) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001108 /* Convert split table into new combined table.
1109 * We must incref keys; we can transfer values.
1110 * Note that values of split table is always dense.
1111 */
1112 for (Py_ssize_t i = 0; i < numentries; i++) {
1113 assert(oldvalues[i] != NULL);
1114 PyDictKeyEntry *ep = &oldentries[i];
1115 PyObject *key = ep->me_key;
1116 Py_INCREF(key);
1117 newentries[i].me_key = key;
1118 newentries[i].me_hash = ep->me_hash;
1119 newentries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001121
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001122 DK_DECREF(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001123 mp->ma_values = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001124 if (oldvalues != empty_values) {
1125 free_values(oldvalues);
1126 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001127 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001128 else { // combined table.
1129 if (oldkeys->dk_nentries == numentries) {
1130 memcpy(newentries, oldentries, numentries * sizeof(PyDictKeyEntry));
1131 }
1132 else {
1133 PyDictKeyEntry *ep = oldentries;
1134 for (Py_ssize_t i = 0; i < numentries; i++) {
1135 while (ep->me_value == NULL)
1136 ep++;
1137 newentries[i] = *ep++;
1138 }
1139 }
1140
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001141 assert(oldkeys->dk_lookup != lookdict_split);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001142 assert(oldkeys->dk_refcnt == 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001143 if (oldkeys->dk_size == PyDict_MINSIZE &&
1144 numfreekeys < PyDict_MAXFREELIST) {
1145 DK_DEBUG_DECREF keys_free_list[numfreekeys++] = oldkeys;
1146 }
1147 else {
1148 DK_DEBUG_DECREF PyObject_FREE(oldkeys);
1149 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001150 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001151
1152 build_indices(mp->ma_keys, newentries, numentries);
1153 mp->ma_keys->dk_usable -= numentries;
1154 mp->ma_keys->dk_nentries = numentries;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001156}
1157
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001158/* Returns NULL if unable to split table.
1159 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001160static PyDictKeysObject *
1161make_keys_shared(PyObject *op)
1162{
1163 Py_ssize_t i;
1164 Py_ssize_t size;
1165 PyDictObject *mp = (PyDictObject *)op;
1166
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001167 if (!PyDict_CheckExact(op))
1168 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001169 if (!_PyDict_HasSplitTable(mp)) {
1170 PyDictKeyEntry *ep0;
1171 PyObject **values;
1172 assert(mp->ma_keys->dk_refcnt == 1);
1173 if (mp->ma_keys->dk_lookup == lookdict) {
1174 return NULL;
1175 }
1176 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1177 /* Remove dummy keys */
1178 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1179 return NULL;
1180 }
1181 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1182 /* Copy values into a new array */
Victor Stinner742da042016-09-07 17:40:12 -07001183 ep0 = DK_ENTRIES(mp->ma_keys);
1184 size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001185 values = new_values(size);
1186 if (values == NULL) {
1187 PyErr_SetString(PyExc_MemoryError,
1188 "Not enough memory to allocate new values array");
1189 return NULL;
1190 }
1191 for (i = 0; i < size; i++) {
1192 values[i] = ep0[i].me_value;
1193 ep0[i].me_value = NULL;
1194 }
1195 mp->ma_keys->dk_lookup = lookdict_split;
1196 mp->ma_values = values;
1197 }
1198 DK_INCREF(mp->ma_keys);
1199 return mp->ma_keys;
1200}
Christian Heimes99170a52007-12-19 02:07:34 +00001201
1202PyObject *
1203_PyDict_NewPresized(Py_ssize_t minused)
1204{
INADA Naoki92c50ee2016-11-22 00:57:02 +09001205 const Py_ssize_t max_presize = 128 * 1024;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001206 Py_ssize_t newsize;
1207 PyDictKeysObject *new_keys;
INADA Naoki92c50ee2016-11-22 00:57:02 +09001208
1209 /* There are no strict guarantee that returned dict can contain minused
1210 * items without resize. So we create medium size dict instead of very
1211 * large dict or MemoryError.
1212 */
1213 if (minused > USABLE_FRACTION(max_presize)) {
1214 newsize = max_presize;
1215 }
1216 else {
1217 Py_ssize_t minsize = ESTIMATE_SIZE(minused);
1218 newsize = PyDict_MINSIZE;
1219 while (newsize < minsize) {
1220 newsize <<= 1;
1221 }
1222 }
1223 assert(IS_POWER_OF_2(newsize));
1224
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001225 new_keys = new_keys_object(newsize);
1226 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001228 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001229}
1230
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001231/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1232 * that may occur (originally dicts supported only string keys, and exceptions
1233 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001234 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001235 * (suppressed) occurred while computing the key's hash, or that some error
1236 * (suppressed) occurred when comparing keys in the dict's internal probe
1237 * sequence. A nasty example of the latter is when a Python-coded comparison
1238 * function hits a stack-depth error, which can cause this to return NULL
1239 * even if the key is present.
1240 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001241PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001242PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001243{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001244 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07001245 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 PyDictObject *mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 PyThreadState *tstate;
INADA Naokiba609772016-12-07 20:41:42 +09001248 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 if (!PyDict_Check(op))
1251 return NULL;
1252 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001253 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 {
1255 hash = PyObject_Hash(key);
1256 if (hash == -1) {
1257 PyErr_Clear();
1258 return NULL;
1259 }
1260 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 /* We can arrive here with a NULL tstate during initialization: try
1263 running "python -Wi" for an example related to string interning.
1264 Let's just hope that no exception occurs then... This must be
1265 _PyThreadState_Current and not PyThreadState_GET() because in debug
1266 mode, the latter complains if tstate is NULL. */
Victor Stinner0cae6092016-11-11 01:43:56 +01001267 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 if (tstate != NULL && tstate->curexc_type != NULL) {
1269 /* preserve the existing exception */
1270 PyObject *err_type, *err_value, *err_tb;
1271 PyErr_Fetch(&err_type, &err_value, &err_tb);
INADA Naoki778928b2017-08-03 23:45:15 +09001272 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 /* ignore errors */
1274 PyErr_Restore(err_type, err_value, err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001275 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 return NULL;
1277 }
1278 else {
INADA Naoki778928b2017-08-03 23:45:15 +09001279 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001280 if (ix < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 PyErr_Clear();
1282 return NULL;
1283 }
1284 }
INADA Naokiba609772016-12-07 20:41:42 +09001285 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001286}
1287
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001288/* Same as PyDict_GetItemWithError() but with hash supplied by caller.
1289 This returns NULL *with* an exception set if an exception occurred.
1290 It returns NULL *without* an exception set if the key wasn't present.
1291*/
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001292PyObject *
1293_PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1294{
Victor Stinner742da042016-09-07 17:40:12 -07001295 Py_ssize_t ix;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001296 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001297 PyObject *value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001298
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001299 if (!PyDict_Check(op)) {
1300 PyErr_BadInternalCall();
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001301 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001302 }
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001303
INADA Naoki778928b2017-08-03 23:45:15 +09001304 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001305 if (ix < 0) {
1306 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001307 }
INADA Naokiba609772016-12-07 20:41:42 +09001308 return value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001309}
1310
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001311/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1312 This returns NULL *with* an exception set if an exception occurred.
1313 It returns NULL *without* an exception set if the key wasn't present.
1314*/
1315PyObject *
1316PyDict_GetItemWithError(PyObject *op, PyObject *key)
1317{
Victor Stinner742da042016-09-07 17:40:12 -07001318 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001319 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 PyDictObject*mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001321 PyObject *value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 if (!PyDict_Check(op)) {
1324 PyErr_BadInternalCall();
1325 return NULL;
1326 }
1327 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001328 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 {
1330 hash = PyObject_Hash(key);
1331 if (hash == -1) {
1332 return NULL;
1333 }
1334 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001335
INADA Naoki778928b2017-08-03 23:45:15 +09001336 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001337 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001339 return value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001340}
1341
Brett Cannonfd074152012-04-14 14:10:13 -04001342PyObject *
1343_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1344{
1345 PyObject *kv;
1346 kv = _PyUnicode_FromId(key); /* borrowed */
1347 if (kv == NULL)
1348 return NULL;
1349 return PyDict_GetItemWithError(dp, kv);
1350}
1351
Victor Stinnerb4efc962015-11-20 09:24:02 +01001352/* Fast version of global value lookup (LOAD_GLOBAL).
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001353 * Lookup in globals, then builtins.
Victor Stinnerb4efc962015-11-20 09:24:02 +01001354 *
1355 * Raise an exception and return NULL if an error occurred (ex: computing the
1356 * key hash failed, key comparison failed, ...). Return NULL if the key doesn't
1357 * exist. Return the value if the key exists.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001358 */
1359PyObject *
1360_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001361{
Victor Stinner742da042016-09-07 17:40:12 -07001362 Py_ssize_t ix;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001363 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09001364 PyObject *value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001365
1366 if (!PyUnicode_CheckExact(key) ||
1367 (hash = ((PyASCIIObject *) key)->hash) == -1)
1368 {
1369 hash = PyObject_Hash(key);
1370 if (hash == -1)
1371 return NULL;
Antoine Pitroue965d972012-02-27 00:45:12 +01001372 }
Victor Stinnerb4efc962015-11-20 09:24:02 +01001373
1374 /* namespace 1: globals */
INADA Naoki778928b2017-08-03 23:45:15 +09001375 ix = globals->ma_keys->dk_lookup(globals, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001376 if (ix == DKIX_ERROR)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001377 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001378 if (ix != DKIX_EMPTY && value != NULL)
1379 return value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001380
1381 /* namespace 2: builtins */
INADA Naoki778928b2017-08-03 23:45:15 +09001382 ix = builtins->ma_keys->dk_lookup(builtins, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001383 if (ix < 0)
Victor Stinnerb4efc962015-11-20 09:24:02 +01001384 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001385 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001386}
1387
Antoine Pitroue965d972012-02-27 00:45:12 +01001388/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1389 * dictionary if it's merely replacing the value for an existing key.
1390 * This means that it's safe to loop over a dictionary with PyDict_Next()
1391 * and occasionally replace a value -- but you can't insert new keys or
1392 * remove them.
1393 */
1394int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001395PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001396{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001397 PyDictObject *mp;
1398 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001399 if (!PyDict_Check(op)) {
1400 PyErr_BadInternalCall();
1401 return -1;
1402 }
1403 assert(key);
1404 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001405 mp = (PyDictObject *)op;
1406 if (!PyUnicode_CheckExact(key) ||
1407 (hash = ((PyASCIIObject *) key)->hash) == -1)
1408 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001409 hash = PyObject_Hash(key);
1410 if (hash == -1)
1411 return -1;
1412 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001413
1414 /* insertdict() handles any resizing that might be necessary */
1415 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001416}
1417
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001418int
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001419_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1420 Py_hash_t hash)
1421{
1422 PyDictObject *mp;
1423
1424 if (!PyDict_Check(op)) {
1425 PyErr_BadInternalCall();
1426 return -1;
1427 }
1428 assert(key);
1429 assert(value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001430 assert(hash != -1);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001431 mp = (PyDictObject *)op;
1432
1433 /* insertdict() handles any resizing that might be necessary */
1434 return insertdict(mp, key, hash, value);
1435}
1436
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001437static int
INADA Naoki778928b2017-08-03 23:45:15 +09001438delitem_common(PyDictObject *mp, Py_hash_t hash, Py_ssize_t ix,
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001439 PyObject *old_value)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001440{
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001441 PyObject *old_key;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001442 PyDictKeyEntry *ep;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001443
INADA Naoki778928b2017-08-03 23:45:15 +09001444 Py_ssize_t hashpos = lookdict_index(mp->ma_keys, hash, ix);
1445 assert(hashpos >= 0);
1446
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001447 mp->ma_used--;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001448 mp->ma_version_tag = DICT_NEXT_VERSION();
1449 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1450 dk_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
1451 ENSURE_ALLOWS_DELETIONS(mp);
1452 old_key = ep->me_key;
1453 ep->me_key = NULL;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001454 ep->me_value = NULL;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001455 Py_DECREF(old_key);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001456 Py_DECREF(old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001457
1458 assert(_PyDict_CheckConsistency(mp));
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001459 return 0;
1460}
1461
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001462int
Tim Peters1f5871e2000-07-04 17:44:48 +00001463PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001464{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001465 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 assert(key);
1467 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001468 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 hash = PyObject_Hash(key);
1470 if (hash == -1)
1471 return -1;
1472 }
Victor Stinner742da042016-09-07 17:40:12 -07001473
1474 return _PyDict_DelItem_KnownHash(op, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001475}
1476
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001477int
1478_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1479{
INADA Naoki778928b2017-08-03 23:45:15 +09001480 Py_ssize_t ix;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001481 PyDictObject *mp;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001482 PyObject *old_value;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001483
1484 if (!PyDict_Check(op)) {
1485 PyErr_BadInternalCall();
1486 return -1;
1487 }
1488 assert(key);
1489 assert(hash != -1);
1490 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001491 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001492 if (ix == DKIX_ERROR)
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001493 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09001494 if (ix == DKIX_EMPTY || old_value == NULL) {
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001495 _PyErr_SetKeyError(key);
1496 return -1;
1497 }
Victor Stinner78601a32016-09-09 19:28:36 -07001498
1499 // Split table doesn't allow deletion. Combine it.
1500 if (_PyDict_HasSplitTable(mp)) {
1501 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1502 return -1;
1503 }
INADA Naoki778928b2017-08-03 23:45:15 +09001504 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001505 assert(ix >= 0);
1506 }
1507
INADA Naoki778928b2017-08-03 23:45:15 +09001508 return delitem_common(mp, hash, ix, old_value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001509}
1510
Antoine Pitroud741ed42016-12-27 14:23:43 +01001511/* This function promises that the predicate -> deletion sequence is atomic
1512 * (i.e. protected by the GIL), assuming the predicate itself doesn't
1513 * release the GIL.
1514 */
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001515int
1516_PyDict_DelItemIf(PyObject *op, PyObject *key,
1517 int (*predicate)(PyObject *value))
1518{
Antoine Pitroud741ed42016-12-27 14:23:43 +01001519 Py_ssize_t hashpos, ix;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001520 PyDictObject *mp;
1521 Py_hash_t hash;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001522 PyObject *old_value;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001523 int res;
1524
1525 if (!PyDict_Check(op)) {
1526 PyErr_BadInternalCall();
1527 return -1;
1528 }
1529 assert(key);
1530 hash = PyObject_Hash(key);
1531 if (hash == -1)
1532 return -1;
1533 mp = (PyDictObject *)op;
INADA Naoki778928b2017-08-03 23:45:15 +09001534 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001535 if (ix == DKIX_ERROR)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001536 return -1;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001537 if (ix == DKIX_EMPTY || old_value == NULL) {
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001538 _PyErr_SetKeyError(key);
1539 return -1;
1540 }
Antoine Pitroud741ed42016-12-27 14:23:43 +01001541
1542 // Split table doesn't allow deletion. Combine it.
1543 if (_PyDict_HasSplitTable(mp)) {
1544 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1545 return -1;
1546 }
INADA Naoki778928b2017-08-03 23:45:15 +09001547 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001548 assert(ix >= 0);
1549 }
1550
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001551 res = predicate(old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001552 if (res == -1)
1553 return -1;
INADA Naoki778928b2017-08-03 23:45:15 +09001554
1555 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1556 assert(hashpos >= 0);
1557
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001558 if (res > 0)
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001559 return delitem_common(mp, hashpos, ix, old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001560 else
1561 return 0;
1562}
1563
1564
Guido van Rossum25831651993-05-19 14:50:45 +00001565void
Tim Peters1f5871e2000-07-04 17:44:48 +00001566PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001567{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001569 PyDictKeysObject *oldkeys;
1570 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001571 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 if (!PyDict_Check(op))
1574 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001575 mp = ((PyDictObject *)op);
1576 oldkeys = mp->ma_keys;
1577 oldvalues = mp->ma_values;
1578 if (oldvalues == empty_values)
1579 return;
1580 /* Empty the dict... */
1581 DK_INCREF(Py_EMPTY_KEYS);
1582 mp->ma_keys = Py_EMPTY_KEYS;
1583 mp->ma_values = empty_values;
1584 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001585 mp->ma_version_tag = DICT_NEXT_VERSION();
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001586 /* ...then clear the keys and values */
1587 if (oldvalues != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001588 n = oldkeys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001589 for (i = 0; i < n; i++)
1590 Py_CLEAR(oldvalues[i]);
1591 free_values(oldvalues);
1592 DK_DECREF(oldkeys);
1593 }
1594 else {
1595 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001596 DK_DECREF(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001597 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02001598 assert(_PyDict_CheckConsistency(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001599}
1600
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001601/* Internal version of PyDict_Next that returns a hash value in addition
1602 * to the key and value.
1603 * Return 1 on success, return 0 when the reached the end of the dictionary
1604 * (or if op is not a dictionary)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001605 */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001606int
1607_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1608 PyObject **pvalue, Py_hash_t *phash)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001609{
INADA Naokica2d8be2016-11-04 16:59:10 +09001610 Py_ssize_t i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001611 PyDictObject *mp;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001612 PyDictKeyEntry *entry_ptr;
1613 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001614
1615 if (!PyDict_Check(op))
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001616 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 mp = (PyDictObject *)op;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001618 i = *ppos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001619 if (mp->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09001620 if (i < 0 || i >= mp->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001621 return 0;
INADA Naokica2d8be2016-11-04 16:59:10 +09001622 /* values of split table is always dense */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001623 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
INADA Naokica2d8be2016-11-04 16:59:10 +09001624 value = mp->ma_values[i];
1625 assert(value != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001627 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09001628 Py_ssize_t n = mp->ma_keys->dk_nentries;
1629 if (i < 0 || i >= n)
1630 return 0;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001631 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
1632 while (i < n && entry_ptr->me_value == NULL) {
1633 entry_ptr++;
1634 i++;
Victor Stinner742da042016-09-07 17:40:12 -07001635 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001636 if (i >= n)
1637 return 0;
1638 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001640 *ppos = i+1;
1641 if (pkey)
1642 *pkey = entry_ptr->me_key;
1643 if (phash)
1644 *phash = entry_ptr->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001645 if (pvalue)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001646 *pvalue = value;
1647 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001648}
1649
Tim Peters080c88b2003-02-15 03:01:11 +00001650/*
1651 * Iterate over a dict. Use like so:
1652 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001653 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001654 * PyObject *key, *value;
1655 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001656 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001657 * Refer to borrowed references in key and value.
Tim Peters080c88b2003-02-15 03:01:11 +00001658 * }
1659 *
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001660 * Return 1 on success, return 0 when the reached the end of the dictionary
1661 * (or if op is not a dictionary)
1662 *
Tim Peters080c88b2003-02-15 03:01:11 +00001663 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001664 * mutates the dict. One exception: it is safe if the loop merely changes
1665 * the values associated with the keys (but doesn't insert new keys or
1666 * delete keys), via PyDict_SetItem().
1667 */
Guido van Rossum25831651993-05-19 14:50:45 +00001668int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001669PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001670{
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001671 return _PyDict_Next(op, ppos, pkey, pvalue, NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001672}
1673
Eric Snow96c6af92015-05-29 22:21:39 -06001674/* Internal version of dict.pop(). */
1675PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001676_PyDict_Pop_KnownHash(PyObject *dict, PyObject *key, Py_hash_t hash, PyObject *deflt)
Eric Snow96c6af92015-05-29 22:21:39 -06001677{
Victor Stinner742da042016-09-07 17:40:12 -07001678 Py_ssize_t ix, hashpos;
Eric Snow96c6af92015-05-29 22:21:39 -06001679 PyObject *old_value, *old_key;
1680 PyDictKeyEntry *ep;
Yury Selivanov684ef2c2016-10-28 19:01:21 -04001681 PyDictObject *mp;
1682
1683 assert(PyDict_Check(dict));
1684 mp = (PyDictObject *)dict;
Eric Snow96c6af92015-05-29 22:21:39 -06001685
1686 if (mp->ma_used == 0) {
1687 if (deflt) {
1688 Py_INCREF(deflt);
1689 return deflt;
1690 }
1691 _PyErr_SetKeyError(key);
1692 return NULL;
1693 }
INADA Naoki778928b2017-08-03 23:45:15 +09001694 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner742da042016-09-07 17:40:12 -07001695 if (ix == DKIX_ERROR)
Eric Snow96c6af92015-05-29 22:21:39 -06001696 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001697 if (ix == DKIX_EMPTY || old_value == NULL) {
Eric Snow96c6af92015-05-29 22:21:39 -06001698 if (deflt) {
1699 Py_INCREF(deflt);
1700 return deflt;
1701 }
1702 _PyErr_SetKeyError(key);
1703 return NULL;
1704 }
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001705
Victor Stinner78601a32016-09-09 19:28:36 -07001706 // Split table doesn't allow deletion. Combine it.
1707 if (_PyDict_HasSplitTable(mp)) {
1708 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1709 return NULL;
1710 }
INADA Naoki778928b2017-08-03 23:45:15 +09001711 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value);
Victor Stinner78601a32016-09-09 19:28:36 -07001712 assert(ix >= 0);
1713 }
1714
INADA Naoki778928b2017-08-03 23:45:15 +09001715 hashpos = lookdict_index(mp->ma_keys, hash, ix);
1716 assert(hashpos >= 0);
Victor Stinner78601a32016-09-09 19:28:36 -07001717 assert(old_value != NULL);
Eric Snow96c6af92015-05-29 22:21:39 -06001718 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001719 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner78601a32016-09-09 19:28:36 -07001720 dk_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
1721 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1722 ENSURE_ALLOWS_DELETIONS(mp);
1723 old_key = ep->me_key;
1724 ep->me_key = NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001725 ep->me_value = NULL;
Victor Stinner78601a32016-09-09 19:28:36 -07001726 Py_DECREF(old_key);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001727
1728 assert(_PyDict_CheckConsistency(mp));
Eric Snow96c6af92015-05-29 22:21:39 -06001729 return old_value;
1730}
1731
Serhiy Storchaka67796522017-01-12 18:34:33 +02001732PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001733_PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt)
Serhiy Storchaka67796522017-01-12 18:34:33 +02001734{
1735 Py_hash_t hash;
1736
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001737 if (((PyDictObject *)dict)->ma_used == 0) {
Serhiy Storchaka67796522017-01-12 18:34:33 +02001738 if (deflt) {
1739 Py_INCREF(deflt);
1740 return deflt;
1741 }
1742 _PyErr_SetKeyError(key);
1743 return NULL;
1744 }
1745 if (!PyUnicode_CheckExact(key) ||
1746 (hash = ((PyASCIIObject *) key)->hash) == -1) {
1747 hash = PyObject_Hash(key);
1748 if (hash == -1)
1749 return NULL;
1750 }
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001751 return _PyDict_Pop_KnownHash(dict, key, hash, deflt);
Serhiy Storchaka67796522017-01-12 18:34:33 +02001752}
1753
Eric Snow96c6af92015-05-29 22:21:39 -06001754/* Internal version of dict.from_keys(). It is subclass-friendly. */
1755PyObject *
1756_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1757{
1758 PyObject *it; /* iter(iterable) */
1759 PyObject *key;
1760 PyObject *d;
1761 int status;
1762
Victor Stinnera5ed5f02016-12-06 18:45:50 +01001763 d = _PyObject_CallNoArg(cls);
Eric Snow96c6af92015-05-29 22:21:39 -06001764 if (d == NULL)
1765 return NULL;
1766
1767 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
1768 if (PyDict_CheckExact(iterable)) {
1769 PyDictObject *mp = (PyDictObject *)d;
1770 PyObject *oldvalue;
1771 Py_ssize_t pos = 0;
1772 PyObject *key;
1773 Py_hash_t hash;
1774
Serhiy Storchakac61ac162017-03-21 08:52:38 +02001775 if (dictresize(mp, ESTIMATE_SIZE(PyDict_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001776 Py_DECREF(d);
1777 return NULL;
1778 }
1779
1780 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
1781 if (insertdict(mp, key, hash, value)) {
1782 Py_DECREF(d);
1783 return NULL;
1784 }
1785 }
1786 return d;
1787 }
1788 if (PyAnySet_CheckExact(iterable)) {
1789 PyDictObject *mp = (PyDictObject *)d;
1790 Py_ssize_t pos = 0;
1791 PyObject *key;
1792 Py_hash_t hash;
1793
Victor Stinner742da042016-09-07 17:40:12 -07001794 if (dictresize(mp, ESTIMATE_SIZE(PySet_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001795 Py_DECREF(d);
1796 return NULL;
1797 }
1798
1799 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
1800 if (insertdict(mp, key, hash, value)) {
1801 Py_DECREF(d);
1802 return NULL;
1803 }
1804 }
1805 return d;
1806 }
1807 }
1808
1809 it = PyObject_GetIter(iterable);
1810 if (it == NULL){
1811 Py_DECREF(d);
1812 return NULL;
1813 }
1814
1815 if (PyDict_CheckExact(d)) {
1816 while ((key = PyIter_Next(it)) != NULL) {
1817 status = PyDict_SetItem(d, key, value);
1818 Py_DECREF(key);
1819 if (status < 0)
1820 goto Fail;
1821 }
1822 } else {
1823 while ((key = PyIter_Next(it)) != NULL) {
1824 status = PyObject_SetItem(d, key, value);
1825 Py_DECREF(key);
1826 if (status < 0)
1827 goto Fail;
1828 }
1829 }
1830
1831 if (PyErr_Occurred())
1832 goto Fail;
1833 Py_DECREF(it);
1834 return d;
1835
1836Fail:
1837 Py_DECREF(it);
1838 Py_DECREF(d);
1839 return NULL;
1840}
1841
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001842/* Methods */
1843
1844static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001845dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001846{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001847 PyObject **values = mp->ma_values;
1848 PyDictKeysObject *keys = mp->ma_keys;
1849 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001850 PyObject_GC_UnTrack(mp);
1851 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001852 if (values != NULL) {
1853 if (values != empty_values) {
Victor Stinner742da042016-09-07 17:40:12 -07001854 for (i = 0, n = mp->ma_keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001855 Py_XDECREF(values[i]);
1856 }
1857 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001859 DK_DECREF(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02001861 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001862 assert(keys->dk_refcnt == 1);
1863 DK_DECREF(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001864 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1866 free_list[numfree++] = mp;
1867 else
1868 Py_TYPE(mp)->tp_free((PyObject *)mp);
1869 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001870}
1871
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001872
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001873static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001874dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001875{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001877 PyObject *key = NULL, *value = NULL;
1878 _PyUnicodeWriter writer;
1879 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00001880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001881 i = Py_ReprEnter((PyObject *)mp);
1882 if (i != 0) {
1883 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1884 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001887 Py_ReprLeave((PyObject *)mp);
1888 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889 }
Tim Petersa7259592001-06-16 05:11:17 +00001890
Victor Stinnerf91929b2013-11-19 13:07:38 +01001891 _PyUnicodeWriter_Init(&writer);
1892 writer.overallocate = 1;
1893 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
1894 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00001895
Victor Stinnerf91929b2013-11-19 13:07:38 +01001896 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
1897 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 /* Do repr() on each key+value pair, and insert ": " between them.
1900 Note that repr may mutate the dict. */
1901 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001902 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001904 PyObject *s;
1905 int res;
1906
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001907 /* Prevent repr from deleting key or value during key format. */
1908 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02001910
Victor Stinnerf91929b2013-11-19 13:07:38 +01001911 if (!first) {
1912 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
1913 goto error;
1914 }
1915 first = 0;
1916
1917 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01001919 goto error;
1920 res = _PyUnicodeWriter_WriteStr(&writer, s);
1921 Py_DECREF(s);
1922 if (res < 0)
1923 goto error;
1924
1925 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
1926 goto error;
1927
1928 s = PyObject_Repr(value);
1929 if (s == NULL)
1930 goto error;
1931 res = _PyUnicodeWriter_WriteStr(&writer, s);
1932 Py_DECREF(s);
1933 if (res < 0)
1934 goto error;
1935
1936 Py_CLEAR(key);
1937 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 }
Tim Petersa7259592001-06-16 05:11:17 +00001939
Victor Stinnerf91929b2013-11-19 13:07:38 +01001940 writer.overallocate = 0;
1941 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
1942 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001944 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01001945
1946 return _PyUnicodeWriter_Finish(&writer);
1947
1948error:
1949 Py_ReprLeave((PyObject *)mp);
1950 _PyUnicodeWriter_Dealloc(&writer);
1951 Py_XDECREF(key);
1952 Py_XDECREF(value);
1953 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001954}
1955
Martin v. Löwis18e16552006-02-15 17:27:45 +00001956static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001957dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001958{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001960}
1961
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001962static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001963dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001964{
Victor Stinner742da042016-09-07 17:40:12 -07001965 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001966 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09001967 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001968
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001970 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001971 hash = PyObject_Hash(key);
1972 if (hash == -1)
1973 return NULL;
1974 }
INADA Naoki778928b2017-08-03 23:45:15 +09001975 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001976 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001977 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001978 if (ix == DKIX_EMPTY || value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 if (!PyDict_CheckExact(mp)) {
1980 /* Look up __missing__ method if we're a subclass. */
1981 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05001982 _Py_IDENTIFIER(__missing__);
1983 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 if (missing != NULL) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001985 res = PyObject_CallFunctionObjArgs(missing,
1986 key, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987 Py_DECREF(missing);
1988 return res;
1989 }
1990 else if (PyErr_Occurred())
1991 return NULL;
1992 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07001993 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001994 return NULL;
1995 }
INADA Naokiba609772016-12-07 20:41:42 +09001996 Py_INCREF(value);
1997 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001998}
1999
2000static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002001dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002002{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003 if (w == NULL)
2004 return PyDict_DelItem((PyObject *)mp, v);
2005 else
2006 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002007}
2008
Guido van Rossuma9e7a811997-05-13 21:02:11 +00002009static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002010 (lenfunc)dict_length, /*mp_length*/
2011 (binaryfunc)dict_subscript, /*mp_subscript*/
2012 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002013};
2014
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002015static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002016dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002017{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002018 PyObject *v;
2019 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002020 PyDictKeyEntry *ep;
2021 Py_ssize_t size, n, offset;
2022 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002023
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002024 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 n = mp->ma_used;
2026 v = PyList_New(n);
2027 if (v == NULL)
2028 return NULL;
2029 if (n != mp->ma_used) {
2030 /* Durnit. The allocations caused the dict to resize.
2031 * Just start over, this shouldn't normally happen.
2032 */
2033 Py_DECREF(v);
2034 goto again;
2035 }
Victor Stinner742da042016-09-07 17:40:12 -07002036 ep = DK_ENTRIES(mp->ma_keys);
2037 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002038 if (mp->ma_values) {
2039 value_ptr = mp->ma_values;
2040 offset = sizeof(PyObject *);
2041 }
2042 else {
2043 value_ptr = &ep[0].me_value;
2044 offset = sizeof(PyDictKeyEntry);
2045 }
2046 for (i = 0, j = 0; i < size; i++) {
2047 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048 PyObject *key = ep[i].me_key;
2049 Py_INCREF(key);
2050 PyList_SET_ITEM(v, j, key);
2051 j++;
2052 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002053 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002054 }
2055 assert(j == n);
2056 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002057}
2058
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002059static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002060dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002061{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002062 PyObject *v;
2063 Py_ssize_t i, j;
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002064 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002065 Py_ssize_t size, n, offset;
2066 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002067
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002068 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 n = mp->ma_used;
2070 v = PyList_New(n);
2071 if (v == NULL)
2072 return NULL;
2073 if (n != mp->ma_used) {
2074 /* Durnit. The allocations caused the dict to resize.
2075 * Just start over, this shouldn't normally happen.
2076 */
2077 Py_DECREF(v);
2078 goto again;
2079 }
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002080 ep = DK_ENTRIES(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002081 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002082 if (mp->ma_values) {
2083 value_ptr = mp->ma_values;
2084 offset = sizeof(PyObject *);
2085 }
2086 else {
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002087 value_ptr = &ep[0].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002088 offset = sizeof(PyDictKeyEntry);
2089 }
2090 for (i = 0, j = 0; i < size; i++) {
2091 PyObject *value = *value_ptr;
2092 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2093 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002094 Py_INCREF(value);
2095 PyList_SET_ITEM(v, j, value);
2096 j++;
2097 }
2098 }
2099 assert(j == n);
2100 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002101}
2102
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002103static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002104dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002105{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002106 PyObject *v;
2107 Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002108 Py_ssize_t size, offset;
2109 PyObject *item, *key;
2110 PyDictKeyEntry *ep;
2111 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002113 /* Preallocate the list of tuples, to avoid allocations during
2114 * the loop over the items, which could trigger GC, which
2115 * could resize the dict. :-(
2116 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002117 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002118 n = mp->ma_used;
2119 v = PyList_New(n);
2120 if (v == NULL)
2121 return NULL;
2122 for (i = 0; i < n; i++) {
2123 item = PyTuple_New(2);
2124 if (item == NULL) {
2125 Py_DECREF(v);
2126 return NULL;
2127 }
2128 PyList_SET_ITEM(v, i, item);
2129 }
2130 if (n != mp->ma_used) {
2131 /* Durnit. The allocations caused the dict to resize.
2132 * Just start over, this shouldn't normally happen.
2133 */
2134 Py_DECREF(v);
2135 goto again;
2136 }
2137 /* Nothing we do below makes any function calls. */
Victor Stinner742da042016-09-07 17:40:12 -07002138 ep = DK_ENTRIES(mp->ma_keys);
2139 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002140 if (mp->ma_values) {
2141 value_ptr = mp->ma_values;
2142 offset = sizeof(PyObject *);
2143 }
2144 else {
2145 value_ptr = &ep[0].me_value;
2146 offset = sizeof(PyDictKeyEntry);
2147 }
2148 for (i = 0, j = 0; i < size; i++) {
2149 PyObject *value = *value_ptr;
2150 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2151 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002152 key = ep[i].me_key;
2153 item = PyList_GET_ITEM(v, j);
2154 Py_INCREF(key);
2155 PyTuple_SET_ITEM(item, 0, key);
2156 Py_INCREF(value);
2157 PyTuple_SET_ITEM(item, 1, value);
2158 j++;
2159 }
2160 }
2161 assert(j == n);
2162 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002163}
2164
Larry Hastings5c661892014-01-24 06:17:25 -08002165/*[clinic input]
2166@classmethod
2167dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08002168 iterable: object
2169 value: object=None
2170 /
2171
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002172Create a new dictionary with keys from iterable and values set to value.
Larry Hastings5c661892014-01-24 06:17:25 -08002173[clinic start generated code]*/
2174
Larry Hastings5c661892014-01-24 06:17:25 -08002175static PyObject *
2176dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002177/*[clinic end generated code: output=8fb98e4b10384999 input=382ba4855d0f74c3]*/
Larry Hastings5c661892014-01-24 06:17:25 -08002178{
Eric Snow96c6af92015-05-29 22:21:39 -06002179 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002180}
2181
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002182static int
Victor Stinner742da042016-09-07 17:40:12 -07002183dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
2184 const char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002185{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002186 PyObject *arg = NULL;
2187 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002189 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
2190 result = -1;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002193 _Py_IDENTIFIER(keys);
2194 if (_PyObject_HasAttrId(arg, &PyId_keys))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 result = PyDict_Merge(self, arg, 1);
2196 else
2197 result = PyDict_MergeFromSeq2(self, arg, 1);
2198 }
2199 if (result == 0 && kwds != NULL) {
2200 if (PyArg_ValidateKeywordArguments(kwds))
2201 result = PyDict_Merge(self, kwds, 1);
2202 else
2203 result = -1;
2204 }
2205 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002206}
2207
Victor Stinner91f0d4a2017-01-19 12:45:06 +01002208/* Note: dict.update() uses the METH_VARARGS|METH_KEYWORDS calling convention.
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002209 Using METH_FASTCALL|METH_KEYWORDS would make dict.update(**dict2) calls
2210 slower, see the issue #29312. */
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002211static PyObject *
2212dict_update(PyObject *self, PyObject *args, PyObject *kwds)
2213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002214 if (dict_update_common(self, args, kwds, "update") != -1)
2215 Py_RETURN_NONE;
2216 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002217}
2218
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002219/* Update unconditionally replaces existing items.
2220 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00002221 otherwise it leaves existing items unchanged.
2222
2223 PyDict_{Update,Merge} update/merge from a mapping object.
2224
Tim Petersf582b822001-12-11 18:51:08 +00002225 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00002226 producing iterable objects of length 2.
2227*/
2228
Tim Petersf582b822001-12-11 18:51:08 +00002229int
Tim Peters1fc240e2001-10-26 05:06:50 +00002230PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
2231{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002232 PyObject *it; /* iter(seq2) */
2233 Py_ssize_t i; /* index into seq2 of current element */
2234 PyObject *item; /* seq2[i] */
2235 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00002236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002237 assert(d != NULL);
2238 assert(PyDict_Check(d));
2239 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00002240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002241 it = PyObject_GetIter(seq2);
2242 if (it == NULL)
2243 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 for (i = 0; ; ++i) {
2246 PyObject *key, *value;
2247 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00002248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002249 fast = NULL;
2250 item = PyIter_Next(it);
2251 if (item == NULL) {
2252 if (PyErr_Occurred())
2253 goto Fail;
2254 break;
2255 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 /* Convert item to sequence, and verify length 2. */
2258 fast = PySequence_Fast(item, "");
2259 if (fast == NULL) {
2260 if (PyErr_ExceptionMatches(PyExc_TypeError))
2261 PyErr_Format(PyExc_TypeError,
2262 "cannot convert dictionary update "
2263 "sequence element #%zd to a sequence",
2264 i);
2265 goto Fail;
2266 }
2267 n = PySequence_Fast_GET_SIZE(fast);
2268 if (n != 2) {
2269 PyErr_Format(PyExc_ValueError,
2270 "dictionary update sequence element #%zd "
2271 "has length %zd; 2 is required",
2272 i, n);
2273 goto Fail;
2274 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002276 /* Update/merge with this (key, value) pair. */
2277 key = PySequence_Fast_GET_ITEM(fast, 0);
2278 value = PySequence_Fast_GET_ITEM(fast, 1);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002279 Py_INCREF(key);
2280 Py_INCREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 if (override || PyDict_GetItem(d, key) == NULL) {
2282 int status = PyDict_SetItem(d, key, value);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002283 if (status < 0) {
2284 Py_DECREF(key);
2285 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002286 goto Fail;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002287 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002288 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002289 Py_DECREF(key);
2290 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 Py_DECREF(fast);
2292 Py_DECREF(item);
2293 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002295 i = 0;
Victor Stinner611b0fa2016-09-14 15:02:01 +02002296 assert(_PyDict_CheckConsistency((PyDictObject *)d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002297 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002298Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002299 Py_XDECREF(item);
2300 Py_XDECREF(fast);
2301 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002302Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002303 Py_DECREF(it);
2304 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002305}
2306
doko@ubuntu.comc96df682016-10-11 08:04:02 +02002307static int
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002308dict_merge(PyObject *a, PyObject *b, int override)
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002309{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002310 PyDictObject *mp, *other;
2311 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -07002312 PyDictKeyEntry *entry, *ep0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002313
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002314 assert(0 <= override && override <= 2);
2315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002316 /* We accept for the argument either a concrete dictionary object,
2317 * or an abstract "mapping" object. For the former, we can do
2318 * things quite efficiently. For the latter, we only require that
2319 * PyMapping_Keys() and PyObject_GetItem() be supported.
2320 */
2321 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2322 PyErr_BadInternalCall();
2323 return -1;
2324 }
2325 mp = (PyDictObject*)a;
2326 if (PyDict_Check(b)) {
2327 other = (PyDictObject*)b;
2328 if (other == mp || other->ma_used == 0)
2329 /* a.update(a) or a.update({}); nothing to do */
2330 return 0;
2331 if (mp->ma_used == 0)
2332 /* Since the target dict is empty, PyDict_GetItem()
2333 * always returns NULL. Setting override to 1
2334 * skips the unnecessary test.
2335 */
2336 override = 1;
2337 /* Do one big resize at the start, rather than
2338 * incrementally resizing as we insert new items. Expect
2339 * that there will be no (or few) overlapping keys.
2340 */
INADA Naokib1152be2016-10-27 19:26:50 +09002341 if (USABLE_FRACTION(mp->ma_keys->dk_size) < other->ma_used) {
2342 if (dictresize(mp, ESTIMATE_SIZE(mp->ma_used + other->ma_used))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002343 return -1;
INADA Naokib1152be2016-10-27 19:26:50 +09002344 }
2345 }
Victor Stinner742da042016-09-07 17:40:12 -07002346 ep0 = DK_ENTRIES(other->ma_keys);
2347 for (i = 0, n = other->ma_keys->dk_nentries; i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002348 PyObject *key, *value;
2349 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002350 entry = &ep0[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002351 key = entry->me_key;
2352 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002353 if (other->ma_values)
2354 value = other->ma_values[i];
2355 else
2356 value = entry->me_value;
2357
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002358 if (value != NULL) {
2359 int err = 0;
2360 Py_INCREF(key);
2361 Py_INCREF(value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002362 if (override == 1)
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002363 err = insertdict(mp, key, hash, value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002364 else if (_PyDict_GetItem_KnownHash(a, key, hash) == NULL) {
2365 if (PyErr_Occurred()) {
2366 Py_DECREF(value);
2367 Py_DECREF(key);
2368 return -1;
2369 }
2370 err = insertdict(mp, key, hash, value);
2371 }
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002372 else if (override != 0) {
2373 _PyErr_SetKeyError(key);
2374 Py_DECREF(value);
2375 Py_DECREF(key);
2376 return -1;
2377 }
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002378 Py_DECREF(value);
2379 Py_DECREF(key);
2380 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002381 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002382
Victor Stinner742da042016-09-07 17:40:12 -07002383 if (n != other->ma_keys->dk_nentries) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002384 PyErr_SetString(PyExc_RuntimeError,
2385 "dict mutated during update");
2386 return -1;
2387 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388 }
2389 }
2390 }
2391 else {
2392 /* Do it the generic, slower way */
2393 PyObject *keys = PyMapping_Keys(b);
2394 PyObject *iter;
2395 PyObject *key, *value;
2396 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 if (keys == NULL)
2399 /* Docstring says this is equivalent to E.keys() so
2400 * if E doesn't have a .keys() method we want
2401 * AttributeError to percolate up. Might as well
2402 * do the same for any other error.
2403 */
2404 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002406 iter = PyObject_GetIter(keys);
2407 Py_DECREF(keys);
2408 if (iter == NULL)
2409 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002411 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002412 if (override != 1 && PyDict_GetItem(a, key) != NULL) {
2413 if (override != 0) {
2414 _PyErr_SetKeyError(key);
2415 Py_DECREF(key);
2416 Py_DECREF(iter);
2417 return -1;
2418 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002419 Py_DECREF(key);
2420 continue;
2421 }
2422 value = PyObject_GetItem(b, key);
2423 if (value == NULL) {
2424 Py_DECREF(iter);
2425 Py_DECREF(key);
2426 return -1;
2427 }
2428 status = PyDict_SetItem(a, key, value);
2429 Py_DECREF(key);
2430 Py_DECREF(value);
2431 if (status < 0) {
2432 Py_DECREF(iter);
2433 return -1;
2434 }
2435 }
2436 Py_DECREF(iter);
2437 if (PyErr_Occurred())
2438 /* Iterator completed, via error */
2439 return -1;
2440 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02002441 assert(_PyDict_CheckConsistency((PyDictObject *)a));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002442 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002443}
2444
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002445int
2446PyDict_Update(PyObject *a, PyObject *b)
2447{
2448 return dict_merge(a, b, 1);
2449}
2450
2451int
2452PyDict_Merge(PyObject *a, PyObject *b, int override)
2453{
2454 /* XXX Deprecate override not in (0, 1). */
2455 return dict_merge(a, b, override != 0);
2456}
2457
2458int
2459_PyDict_MergeEx(PyObject *a, PyObject *b, int override)
2460{
2461 return dict_merge(a, b, override);
2462}
2463
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002464static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002465dict_copy(PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002466{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002467 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002468}
2469
2470PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002471PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002472{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002473 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002474 PyDictObject *mp;
2475 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002477 if (o == NULL || !PyDict_Check(o)) {
2478 PyErr_BadInternalCall();
2479 return NULL;
2480 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002481 mp = (PyDictObject *)o;
2482 if (_PyDict_HasSplitTable(mp)) {
2483 PyDictObject *split_copy;
Victor Stinner742da042016-09-07 17:40:12 -07002484 Py_ssize_t size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
2485 PyObject **newvalues;
2486 newvalues = new_values(size);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002487 if (newvalues == NULL)
2488 return PyErr_NoMemory();
2489 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2490 if (split_copy == NULL) {
2491 free_values(newvalues);
2492 return NULL;
2493 }
2494 split_copy->ma_values = newvalues;
2495 split_copy->ma_keys = mp->ma_keys;
2496 split_copy->ma_used = mp->ma_used;
2497 DK_INCREF(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002498 for (i = 0, n = size; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002499 PyObject *value = mp->ma_values[i];
2500 Py_XINCREF(value);
2501 split_copy->ma_values[i] = value;
2502 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002503 if (_PyObject_GC_IS_TRACKED(mp))
2504 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002505 return (PyObject *)split_copy;
2506 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002507 copy = PyDict_New();
2508 if (copy == NULL)
2509 return NULL;
2510 if (PyDict_Merge(copy, o, 1) == 0)
2511 return copy;
2512 Py_DECREF(copy);
2513 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002514}
2515
Martin v. Löwis18e16552006-02-15 17:27:45 +00002516Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002517PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002518{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002519 if (mp == NULL || !PyDict_Check(mp)) {
2520 PyErr_BadInternalCall();
2521 return -1;
2522 }
2523 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002524}
2525
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002526PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002527PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002528{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002529 if (mp == NULL || !PyDict_Check(mp)) {
2530 PyErr_BadInternalCall();
2531 return NULL;
2532 }
2533 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002534}
2535
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002536PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002537PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002538{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002539 if (mp == NULL || !PyDict_Check(mp)) {
2540 PyErr_BadInternalCall();
2541 return NULL;
2542 }
2543 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002544}
2545
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002546PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002547PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002548{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002549 if (mp == NULL || !PyDict_Check(mp)) {
2550 PyErr_BadInternalCall();
2551 return NULL;
2552 }
2553 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002554}
2555
Tim Peterse63415e2001-05-08 04:38:29 +00002556/* Return 1 if dicts equal, 0 if not, -1 if error.
2557 * Gets out as soon as any difference is detected.
2558 * Uses only Py_EQ comparison.
2559 */
2560static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002561dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002562{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002563 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002565 if (a->ma_used != b->ma_used)
2566 /* can't be equal if # of entries differ */
2567 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002568 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Victor Stinner742da042016-09-07 17:40:12 -07002569 for (i = 0; i < a->ma_keys->dk_nentries; i++) {
2570 PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002571 PyObject *aval;
2572 if (a->ma_values)
2573 aval = a->ma_values[i];
2574 else
2575 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002576 if (aval != NULL) {
2577 int cmp;
2578 PyObject *bval;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002579 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002580 /* temporarily bump aval's refcount to ensure it stays
2581 alive until we're done with it */
2582 Py_INCREF(aval);
2583 /* ditto for key */
2584 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002585 /* reuse the known hash value */
INADA Naoki778928b2017-08-03 23:45:15 +09002586 b->ma_keys->dk_lookup(b, key, ep->me_hash, &bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002587 if (bval == NULL) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002588 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002589 Py_DECREF(aval);
2590 if (PyErr_Occurred())
2591 return -1;
2592 return 0;
2593 }
2594 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002595 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002596 Py_DECREF(aval);
2597 if (cmp <= 0) /* error or not equal */
2598 return cmp;
2599 }
2600 }
2601 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002602}
Tim Peterse63415e2001-05-08 04:38:29 +00002603
2604static PyObject *
2605dict_richcompare(PyObject *v, PyObject *w, int op)
2606{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002607 int cmp;
2608 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002610 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2611 res = Py_NotImplemented;
2612 }
2613 else if (op == Py_EQ || op == Py_NE) {
2614 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2615 if (cmp < 0)
2616 return NULL;
2617 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2618 }
2619 else
2620 res = Py_NotImplemented;
2621 Py_INCREF(res);
2622 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002623}
Tim Peterse63415e2001-05-08 04:38:29 +00002624
Larry Hastings61272b72014-01-07 12:41:53 -08002625/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002626
2627@coexist
2628dict.__contains__
2629
2630 key: object
2631 /
2632
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002633True if the dictionary has the specified key, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002634[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002635
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002636static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002637dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka19d25972017-02-04 08:05:07 +02002638/*[clinic end generated code: output=a3d03db709ed6e6b input=fe1cb42ad831e820]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002639{
Larry Hastingsc2047262014-01-25 20:43:29 -08002640 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002641 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002642 Py_ssize_t ix;
INADA Naokiba609772016-12-07 20:41:42 +09002643 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002646 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002647 hash = PyObject_Hash(key);
2648 if (hash == -1)
2649 return NULL;
2650 }
INADA Naoki778928b2017-08-03 23:45:15 +09002651 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002652 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002653 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002654 if (ix == DKIX_EMPTY || value == NULL)
Victor Stinner742da042016-09-07 17:40:12 -07002655 Py_RETURN_FALSE;
2656 Py_RETURN_TRUE;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002657}
2658
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002659/*[clinic input]
2660dict.get
2661
2662 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002663 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002664 /
2665
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002666Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002667[clinic start generated code]*/
2668
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002669static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002670dict_get_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002671/*[clinic end generated code: output=bba707729dee05bf input=279ddb5790b6b107]*/
Barry Warsawc38c5da1997-10-06 17:49:20 +00002672{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002673 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002674 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002675 Py_ssize_t ix;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002677 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002678 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002679 hash = PyObject_Hash(key);
2680 if (hash == -1)
2681 return NULL;
2682 }
INADA Naoki778928b2017-08-03 23:45:15 +09002683 ix = (self->ma_keys->dk_lookup) (self, key, hash, &val);
Victor Stinner742da042016-09-07 17:40:12 -07002684 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002685 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002686 if (ix == DKIX_EMPTY || val == NULL) {
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002687 val = default_value;
INADA Naokiba609772016-12-07 20:41:42 +09002688 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002689 Py_INCREF(val);
2690 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002691}
2692
Benjamin Peterson00e98862013-03-07 22:16:29 -05002693PyObject *
2694PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002695{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002696 PyDictObject *mp = (PyDictObject *)d;
INADA Naoki93f26f72016-11-02 18:45:16 +09002697 PyObject *value;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002698 Py_hash_t hash;
Guido van Rossum164452c2000-08-08 16:12:54 +00002699
Benjamin Peterson00e98862013-03-07 22:16:29 -05002700 if (!PyDict_Check(d)) {
2701 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002702 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002703 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002705 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002706 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002707 hash = PyObject_Hash(key);
2708 if (hash == -1)
2709 return NULL;
2710 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002711
2712 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
2713 if (insertion_resize(mp) < 0)
2714 return NULL;
2715 }
2716
INADA Naoki778928b2017-08-03 23:45:15 +09002717 Py_ssize_t ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002718 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002719 return NULL;
INADA Naoki93f26f72016-11-02 18:45:16 +09002720
2721 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09002722 ((ix >= 0 && value == NULL && mp->ma_used != ix) ||
INADA Naoki93f26f72016-11-02 18:45:16 +09002723 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
2724 if (insertion_resize(mp) < 0) {
2725 return NULL;
2726 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002727 ix = DKIX_EMPTY;
2728 }
2729
2730 if (ix == DKIX_EMPTY) {
2731 PyDictKeyEntry *ep, *ep0;
2732 value = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002733 if (mp->ma_keys->dk_usable <= 0) {
Victor Stinner3c336c52016-09-12 14:17:40 +02002734 if (insertion_resize(mp) < 0) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002735 return NULL;
Victor Stinner3c336c52016-09-12 14:17:40 +02002736 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002737 }
INADA Naoki778928b2017-08-03 23:45:15 +09002738 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naoki93f26f72016-11-02 18:45:16 +09002739 ep0 = DK_ENTRIES(mp->ma_keys);
2740 ep = &ep0[mp->ma_keys->dk_nentries];
2741 dk_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002742 Py_INCREF(key);
INADA Naoki93f26f72016-11-02 18:45:16 +09002743 Py_INCREF(value);
2744 MAINTAIN_TRACKING(mp, key, value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002745 ep->me_key = key;
2746 ep->me_hash = hash;
INADA Naokiba609772016-12-07 20:41:42 +09002747 if (_PyDict_HasSplitTable(mp)) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002748 assert(mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
2749 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Victor Stinner742da042016-09-07 17:40:12 -07002750 }
2751 else {
INADA Naoki93f26f72016-11-02 18:45:16 +09002752 ep->me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07002753 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002754 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002755 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naoki93f26f72016-11-02 18:45:16 +09002756 mp->ma_keys->dk_usable--;
2757 mp->ma_keys->dk_nentries++;
2758 assert(mp->ma_keys->dk_usable >= 0);
2759 }
INADA Naokiba609772016-12-07 20:41:42 +09002760 else if (value == NULL) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002761 value = defaultobj;
2762 assert(_PyDict_HasSplitTable(mp));
2763 assert(ix == mp->ma_used);
2764 Py_INCREF(value);
2765 MAINTAIN_TRACKING(mp, key, value);
INADA Naokiba609772016-12-07 20:41:42 +09002766 mp->ma_values[ix] = value;
INADA Naoki93f26f72016-11-02 18:45:16 +09002767 mp->ma_used++;
2768 mp->ma_version_tag = DICT_NEXT_VERSION();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002769 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002770
2771 assert(_PyDict_CheckConsistency(mp));
2772 return value;
Guido van Rossum164452c2000-08-08 16:12:54 +00002773}
2774
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002775/*[clinic input]
2776dict.setdefault
2777
2778 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002779 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002780 /
2781
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002782Insert key with a value of default if key is not in the dictionary.
2783
2784Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002785[clinic start generated code]*/
2786
Benjamin Peterson00e98862013-03-07 22:16:29 -05002787static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002788dict_setdefault_impl(PyDictObject *self, PyObject *key,
2789 PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002790/*[clinic end generated code: output=f8c1101ebf69e220 input=0f063756e815fd9d]*/
Benjamin Peterson00e98862013-03-07 22:16:29 -05002791{
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002792 PyObject *val;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002793
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002794 val = PyDict_SetDefault((PyObject *)self, key, default_value);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002795 Py_XINCREF(val);
2796 return val;
2797}
Guido van Rossum164452c2000-08-08 16:12:54 +00002798
2799static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002800dict_clear(PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002801{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002802 PyDict_Clear((PyObject *)mp);
2803 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002804}
2805
Guido van Rossumba6ab842000-12-12 22:02:18 +00002806static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002807dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002808{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002809 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00002810
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002811 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2812 return NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06002813
Yury Selivanov684ef2c2016-10-28 19:01:21 -04002814 return _PyDict_Pop((PyObject*)mp, key, deflt);
Guido van Rossume027d982002-04-12 15:11:59 +00002815}
2816
2817static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002818dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002819{
Victor Stinner742da042016-09-07 17:40:12 -07002820 Py_ssize_t i, j;
2821 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002822 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002824 /* Allocate the result tuple before checking the size. Believe it
2825 * or not, this allocation could trigger a garbage collection which
2826 * could empty the dict, so if we checked the size first and that
2827 * happened, the result would be an infinite loop (searching for an
2828 * entry that no longer exists). Note that the usual popitem()
2829 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2830 * tuple away if the dict *is* empty isn't a significant
2831 * inefficiency -- possible, but unlikely in practice.
2832 */
2833 res = PyTuple_New(2);
2834 if (res == NULL)
2835 return NULL;
2836 if (mp->ma_used == 0) {
2837 Py_DECREF(res);
2838 PyErr_SetString(PyExc_KeyError,
2839 "popitem(): dictionary is empty");
2840 return NULL;
2841 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002842 /* Convert split table to combined table */
2843 if (mp->ma_keys->dk_lookup == lookdict_split) {
2844 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2845 Py_DECREF(res);
2846 return NULL;
2847 }
2848 }
2849 ENSURE_ALLOWS_DELETIONS(mp);
Victor Stinner742da042016-09-07 17:40:12 -07002850
2851 /* Pop last item */
2852 ep0 = DK_ENTRIES(mp->ma_keys);
2853 i = mp->ma_keys->dk_nentries - 1;
2854 while (i >= 0 && ep0[i].me_value == NULL) {
2855 i--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002856 }
Victor Stinner742da042016-09-07 17:40:12 -07002857 assert(i >= 0);
2858
2859 ep = &ep0[i];
2860 j = lookdict_index(mp->ma_keys, ep->me_hash, i);
2861 assert(j >= 0);
2862 assert(dk_get_index(mp->ma_keys, j) == i);
2863 dk_set_index(mp->ma_keys, j, DKIX_DUMMY);
2864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002865 PyTuple_SET_ITEM(res, 0, ep->me_key);
2866 PyTuple_SET_ITEM(res, 1, ep->me_value);
Victor Stinner742da042016-09-07 17:40:12 -07002867 ep->me_key = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 ep->me_value = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002869 /* We can't dk_usable++ since there is DKIX_DUMMY in indices */
2870 mp->ma_keys->dk_nentries = i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002871 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002872 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02002873 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002875}
2876
Jeremy Hylton8caad492000-06-23 14:18:11 +00002877static int
2878dict_traverse(PyObject *op, visitproc visit, void *arg)
2879{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002880 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson55f44522016-09-05 12:12:59 -07002881 PyDictKeysObject *keys = mp->ma_keys;
Serhiy Storchaka46825d22016-09-26 21:29:34 +03002882 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Victor Stinner742da042016-09-07 17:40:12 -07002883 Py_ssize_t i, n = keys->dk_nentries;
2884
Benjamin Peterson55f44522016-09-05 12:12:59 -07002885 if (keys->dk_lookup == lookdict) {
2886 for (i = 0; i < n; i++) {
2887 if (entries[i].me_value != NULL) {
2888 Py_VISIT(entries[i].me_value);
2889 Py_VISIT(entries[i].me_key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002890 }
2891 }
Victor Stinner742da042016-09-07 17:40:12 -07002892 }
2893 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002894 if (mp->ma_values != NULL) {
Benjamin Peterson55f44522016-09-05 12:12:59 -07002895 for (i = 0; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002896 Py_VISIT(mp->ma_values[i]);
2897 }
2898 }
2899 else {
Benjamin Peterson55f44522016-09-05 12:12:59 -07002900 for (i = 0; i < n; i++) {
2901 Py_VISIT(entries[i].me_value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002902 }
2903 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002904 }
2905 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002906}
2907
2908static int
2909dict_tp_clear(PyObject *op)
2910{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002911 PyDict_Clear(op);
2912 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002913}
2914
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002915static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002916
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002917Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06002918_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002919{
Victor Stinner742da042016-09-07 17:40:12 -07002920 Py_ssize_t size, usable, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002921
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002922 size = DK_SIZE(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002923 usable = USABLE_FRACTION(size);
2924
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002925 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002926 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -07002927 res += usable * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002928 /* If the dictionary is split, the keys portion is accounted-for
2929 in the type object. */
2930 if (mp->ma_keys->dk_refcnt == 1)
Victor Stinner98ee9d52016-09-08 09:33:56 -07002931 res += (sizeof(PyDictKeysObject)
2932 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
2933 + DK_IXSIZE(mp->ma_keys) * size
2934 + sizeof(PyDictKeyEntry) * usable);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002935 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002936}
2937
2938Py_ssize_t
2939_PyDict_KeysSize(PyDictKeysObject *keys)
2940{
Victor Stinner98ee9d52016-09-08 09:33:56 -07002941 return (sizeof(PyDictKeysObject)
2942 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
2943 + DK_IXSIZE(keys) * DK_SIZE(keys)
2944 + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002945}
2946
doko@ubuntu.com17210f52016-01-14 14:04:59 +01002947static PyObject *
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002948dict_sizeof(PyDictObject *mp)
2949{
2950 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
2951}
2952
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002953PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
2954
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002955PyDoc_STRVAR(sizeof__doc__,
2956"D.__sizeof__() -> size of D in memory, in bytes");
2957
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002958PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002959"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002960If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00002961
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002962PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00002963"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000029642-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002965
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002966PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04002967"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
2968If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
2969If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
2970In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00002971
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002972PyDoc_STRVAR(clear__doc__,
2973"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002974
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002975PyDoc_STRVAR(copy__doc__,
2976"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00002977
Guido van Rossumb90c8482007-02-10 01:11:45 +00002978/* Forward */
2979static PyObject *dictkeys_new(PyObject *);
2980static PyObject *dictitems_new(PyObject *);
2981static PyObject *dictvalues_new(PyObject *);
2982
Guido van Rossum45c85d12007-07-27 16:31:40 +00002983PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002984 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002985PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002986 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002987PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002988 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00002989
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002990static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07002991 DICT___CONTAINS___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002992 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
2993 getitem__doc__},
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002994 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002995 sizeof__doc__},
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002996 DICT_GET_METHODDEF
2997 DICT_SETDEFAULT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002998 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
2999 pop__doc__},
3000 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
3001 popitem__doc__},
3002 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
3003 keys__doc__},
3004 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
3005 items__doc__},
3006 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
3007 values__doc__},
3008 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
3009 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08003010 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003011 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
3012 clear__doc__},
3013 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
3014 copy__doc__},
3015 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003016};
3017
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003018/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00003019int
3020PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003021{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003022 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07003023 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003024 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003025 PyObject *value;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003027 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003028 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003029 hash = PyObject_Hash(key);
3030 if (hash == -1)
3031 return -1;
3032 }
INADA Naoki778928b2017-08-03 23:45:15 +09003033 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003034 if (ix == DKIX_ERROR)
3035 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003036 return (ix != DKIX_EMPTY && value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003037}
3038
Thomas Wouterscf297e42007-02-23 15:07:44 +00003039/* Internal version of PyDict_Contains used when the hash value is already known */
3040int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003041_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00003042{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003043 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003044 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07003045 Py_ssize_t ix;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003046
INADA Naoki778928b2017-08-03 23:45:15 +09003047 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003048 if (ix == DKIX_ERROR)
3049 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003050 return (ix != DKIX_EMPTY && value != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00003051}
3052
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003053/* Hack to implement "key in dict" */
3054static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003055 0, /* sq_length */
3056 0, /* sq_concat */
3057 0, /* sq_repeat */
3058 0, /* sq_item */
3059 0, /* sq_slice */
3060 0, /* sq_ass_item */
3061 0, /* sq_ass_slice */
3062 PyDict_Contains, /* sq_contains */
3063 0, /* sq_inplace_concat */
3064 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003065};
3066
Guido van Rossum09e563a2001-05-01 12:10:21 +00003067static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003068dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3069{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003070 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003071 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003073 assert(type != NULL && type->tp_alloc != NULL);
3074 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02003075 if (self == NULL)
3076 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003077 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003078
Victor Stinnera9f61a52013-07-16 22:17:26 +02003079 /* The object has been implicitly tracked by tp_alloc */
3080 if (type == &PyDict_Type)
3081 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003082
3083 d->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003084 d->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07003085 d->ma_keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003086 if (d->ma_keys == NULL) {
3087 Py_DECREF(self);
3088 return NULL;
3089 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02003090 assert(_PyDict_CheckConsistency(d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003091 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003092}
3093
Tim Peters25786c02001-09-02 08:22:48 +00003094static int
3095dict_init(PyObject *self, PyObject *args, PyObject *kwds)
3096{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003097 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00003098}
3099
Tim Peters6d6c1a32001-08-02 04:15:00 +00003100static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003101dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00003102{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003103 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003104}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003105
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003106PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00003107"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00003108"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003109" (key, value) pairs\n"
3110"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00003111" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003112" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00003113" d[k] = v\n"
3114"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
3115" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00003116
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003117PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003118 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3119 "dict",
3120 sizeof(PyDictObject),
3121 0,
3122 (destructor)dict_dealloc, /* tp_dealloc */
3123 0, /* tp_print */
3124 0, /* tp_getattr */
3125 0, /* tp_setattr */
3126 0, /* tp_reserved */
3127 (reprfunc)dict_repr, /* tp_repr */
3128 0, /* tp_as_number */
3129 &dict_as_sequence, /* tp_as_sequence */
3130 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003131 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003132 0, /* tp_call */
3133 0, /* tp_str */
3134 PyObject_GenericGetAttr, /* tp_getattro */
3135 0, /* tp_setattro */
3136 0, /* tp_as_buffer */
3137 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3138 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
3139 dictionary_doc, /* tp_doc */
3140 dict_traverse, /* tp_traverse */
3141 dict_tp_clear, /* tp_clear */
3142 dict_richcompare, /* tp_richcompare */
3143 0, /* tp_weaklistoffset */
3144 (getiterfunc)dict_iter, /* tp_iter */
3145 0, /* tp_iternext */
3146 mapp_methods, /* tp_methods */
3147 0, /* tp_members */
3148 0, /* tp_getset */
3149 0, /* tp_base */
3150 0, /* tp_dict */
3151 0, /* tp_descr_get */
3152 0, /* tp_descr_set */
3153 0, /* tp_dictoffset */
3154 dict_init, /* tp_init */
3155 PyType_GenericAlloc, /* tp_alloc */
3156 dict_new, /* tp_new */
3157 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003158};
3159
Victor Stinner3c1e4812012-03-26 22:10:51 +02003160PyObject *
3161_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
3162{
3163 PyObject *kv;
3164 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02003165 if (kv == NULL) {
3166 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003167 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02003168 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02003169 return PyDict_GetItem(dp, kv);
3170}
3171
Guido van Rossum3cca2451997-05-16 14:23:33 +00003172/* For backward compatibility with old dictionary interface */
3173
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003174PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003175PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003176{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003177 PyObject *kv, *rv;
3178 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003179 if (kv == NULL) {
3180 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003181 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003182 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003183 rv = PyDict_GetItem(v, kv);
3184 Py_DECREF(kv);
3185 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003186}
3187
3188int
Victor Stinner3c1e4812012-03-26 22:10:51 +02003189_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
3190{
3191 PyObject *kv;
3192 kv = _PyUnicode_FromId(key); /* borrowed */
3193 if (kv == NULL)
3194 return -1;
3195 return PyDict_SetItem(v, kv, item);
3196}
3197
3198int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003199PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003200{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003201 PyObject *kv;
3202 int err;
3203 kv = PyUnicode_FromString(key);
3204 if (kv == NULL)
3205 return -1;
3206 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
3207 err = PyDict_SetItem(v, kv, item);
3208 Py_DECREF(kv);
3209 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003210}
3211
3212int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01003213_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
3214{
3215 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3216 if (kv == NULL)
3217 return -1;
3218 return PyDict_DelItem(v, kv);
3219}
3220
3221int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003222PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003223{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003224 PyObject *kv;
3225 int err;
3226 kv = PyUnicode_FromString(key);
3227 if (kv == NULL)
3228 return -1;
3229 err = PyDict_DelItem(v, kv);
3230 Py_DECREF(kv);
3231 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003232}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003233
Raymond Hettinger019a1482004-03-18 02:41:19 +00003234/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003235
3236typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003237 PyObject_HEAD
3238 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
3239 Py_ssize_t di_used;
3240 Py_ssize_t di_pos;
3241 PyObject* di_result; /* reusable result tuple for iteritems */
3242 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003243} dictiterobject;
3244
3245static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003246dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003247{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003248 dictiterobject *di;
3249 di = PyObject_GC_New(dictiterobject, itertype);
3250 if (di == NULL)
3251 return NULL;
3252 Py_INCREF(dict);
3253 di->di_dict = dict;
3254 di->di_used = dict->ma_used;
3255 di->di_pos = 0;
3256 di->len = dict->ma_used;
3257 if (itertype == &PyDictIterItem_Type) {
3258 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
3259 if (di->di_result == NULL) {
3260 Py_DECREF(di);
3261 return NULL;
3262 }
3263 }
3264 else
3265 di->di_result = NULL;
3266 _PyObject_GC_TRACK(di);
3267 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003268}
3269
3270static void
3271dictiter_dealloc(dictiterobject *di)
3272{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003273 Py_XDECREF(di->di_dict);
3274 Py_XDECREF(di->di_result);
3275 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003276}
3277
3278static int
3279dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
3280{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003281 Py_VISIT(di->di_dict);
3282 Py_VISIT(di->di_result);
3283 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003284}
3285
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003286static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003287dictiter_len(dictiterobject *di)
3288{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003289 Py_ssize_t len = 0;
3290 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
3291 len = di->len;
3292 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003293}
3294
Guido van Rossumb90c8482007-02-10 01:11:45 +00003295PyDoc_STRVAR(length_hint_doc,
3296 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003297
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003298static PyObject *
3299dictiter_reduce(dictiterobject *di);
3300
3301PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3302
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003303static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003304 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
3305 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003306 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
3307 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003308 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003309};
3310
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003311static PyObject*
3312dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00003313{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003314 PyObject *key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003315 Py_ssize_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003316 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003317 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00003318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003319 if (d == NULL)
3320 return NULL;
3321 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00003322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003323 if (di->di_used != d->ma_used) {
3324 PyErr_SetString(PyExc_RuntimeError,
3325 "dictionary changed size during iteration");
3326 di->di_used = -1; /* Make this state sticky */
3327 return NULL;
3328 }
Guido van Rossum2147df72002-07-16 20:30:22 +00003329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003330 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003331 k = d->ma_keys;
INADA Naokica2d8be2016-11-04 16:59:10 +09003332 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003333 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003334 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003335 goto fail;
3336 key = DK_ENTRIES(k)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003337 assert(d->ma_values[i] != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003338 }
3339 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003340 Py_ssize_t n = k->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003341 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3342 while (i < n && entry_ptr->me_value == NULL) {
3343 entry_ptr++;
3344 i++;
3345 }
3346 if (i >= n)
3347 goto fail;
3348 key = entry_ptr->me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003349 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003350 di->di_pos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003351 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003352 Py_INCREF(key);
3353 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003354
3355fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003356 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003357 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003358 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003359}
3360
Raymond Hettinger019a1482004-03-18 02:41:19 +00003361PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003362 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3363 "dict_keyiterator", /* tp_name */
3364 sizeof(dictiterobject), /* tp_basicsize */
3365 0, /* tp_itemsize */
3366 /* methods */
3367 (destructor)dictiter_dealloc, /* tp_dealloc */
3368 0, /* tp_print */
3369 0, /* tp_getattr */
3370 0, /* tp_setattr */
3371 0, /* tp_reserved */
3372 0, /* tp_repr */
3373 0, /* tp_as_number */
3374 0, /* tp_as_sequence */
3375 0, /* tp_as_mapping */
3376 0, /* tp_hash */
3377 0, /* tp_call */
3378 0, /* tp_str */
3379 PyObject_GenericGetAttr, /* tp_getattro */
3380 0, /* tp_setattro */
3381 0, /* tp_as_buffer */
3382 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3383 0, /* tp_doc */
3384 (traverseproc)dictiter_traverse, /* tp_traverse */
3385 0, /* tp_clear */
3386 0, /* tp_richcompare */
3387 0, /* tp_weaklistoffset */
3388 PyObject_SelfIter, /* tp_iter */
3389 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3390 dictiter_methods, /* tp_methods */
3391 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003392};
3393
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003394static PyObject *
3395dictiter_iternextvalue(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003396{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003397 PyObject *value;
INADA Naokica2d8be2016-11-04 16:59:10 +09003398 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003399 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003401 if (d == NULL)
3402 return NULL;
3403 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003405 if (di->di_used != d->ma_used) {
3406 PyErr_SetString(PyExc_RuntimeError,
3407 "dictionary changed size during iteration");
3408 di->di_used = -1; /* Make this state sticky */
3409 return NULL;
3410 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003412 i = di->di_pos;
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;
INADA Naokica2d8be2016-11-04 16:59:10 +09003417 value = d->ma_values[i];
3418 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003419 }
3420 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003421 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003422 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3423 while (i < n && entry_ptr->me_value == NULL) {
3424 entry_ptr++;
3425 i++;
3426 }
3427 if (i >= n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003428 goto fail;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003429 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003430 }
3431 di->di_pos = i+1;
3432 di->len--;
3433 Py_INCREF(value);
3434 return value;
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;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003440}
3441
3442PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003443 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3444 "dict_valueiterator", /* 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 */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003463 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003464 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_iternextvalue, /* 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_iternextitem(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003477{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003478 PyObject *key, *value, *result;
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;
3498 key = DK_ENTRIES(d->ma_keys)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003499 value = d->ma_values[i];
3500 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003501 }
3502 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003503 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003504 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3505 while (i < n && entry_ptr->me_value == NULL) {
3506 entry_ptr++;
3507 i++;
3508 }
3509 if (i >= n)
3510 goto fail;
3511 key = entry_ptr->me_key;
3512 value = entry_ptr->me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003513 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003514 di->di_pos = i+1;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003515 di->len--;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003516 Py_INCREF(key);
3517 Py_INCREF(value);
3518 result = di->di_result;
3519 if (Py_REFCNT(result) == 1) {
3520 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3521 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3522 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3523 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003524 Py_INCREF(result);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003525 Py_DECREF(oldkey);
3526 Py_DECREF(oldvalue);
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003527 }
3528 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003529 result = PyTuple_New(2);
3530 if (result == NULL)
3531 return NULL;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003532 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3533 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003534 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003535 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003536
3537fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003538 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003539 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003540 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003541}
3542
3543PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003544 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3545 "dict_itemiterator", /* tp_name */
3546 sizeof(dictiterobject), /* tp_basicsize */
3547 0, /* tp_itemsize */
3548 /* methods */
3549 (destructor)dictiter_dealloc, /* tp_dealloc */
3550 0, /* tp_print */
3551 0, /* tp_getattr */
3552 0, /* tp_setattr */
3553 0, /* tp_reserved */
3554 0, /* tp_repr */
3555 0, /* tp_as_number */
3556 0, /* tp_as_sequence */
3557 0, /* tp_as_mapping */
3558 0, /* tp_hash */
3559 0, /* tp_call */
3560 0, /* tp_str */
3561 PyObject_GenericGetAttr, /* tp_getattro */
3562 0, /* tp_setattro */
3563 0, /* tp_as_buffer */
3564 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3565 0, /* tp_doc */
3566 (traverseproc)dictiter_traverse, /* tp_traverse */
3567 0, /* tp_clear */
3568 0, /* tp_richcompare */
3569 0, /* tp_weaklistoffset */
3570 PyObject_SelfIter, /* tp_iter */
3571 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3572 dictiter_methods, /* tp_methods */
3573 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003574};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003575
3576
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003577static PyObject *
3578dictiter_reduce(dictiterobject *di)
3579{
3580 PyObject *list;
3581 dictiterobject tmp;
3582
3583 list = PyList_New(0);
3584 if (!list)
3585 return NULL;
3586
3587 /* copy the itertor state */
3588 tmp = *di;
3589 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003590
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003591 /* iterate the temporary into a list */
3592 for(;;) {
3593 PyObject *element = 0;
3594 if (Py_TYPE(di) == &PyDictIterItem_Type)
3595 element = dictiter_iternextitem(&tmp);
3596 else if (Py_TYPE(di) == &PyDictIterKey_Type)
3597 element = dictiter_iternextkey(&tmp);
3598 else if (Py_TYPE(di) == &PyDictIterValue_Type)
3599 element = dictiter_iternextvalue(&tmp);
3600 else
3601 assert(0);
3602 if (element) {
3603 if (PyList_Append(list, element)) {
3604 Py_DECREF(element);
3605 Py_DECREF(list);
3606 Py_XDECREF(tmp.di_dict);
3607 return NULL;
3608 }
3609 Py_DECREF(element);
3610 } else
3611 break;
3612 }
3613 Py_XDECREF(tmp.di_dict);
3614 /* check for error */
3615 if (tmp.di_dict != NULL) {
3616 /* we have an error */
3617 Py_DECREF(list);
3618 return NULL;
3619 }
Antoine Pitroua7013882012-04-05 00:04:20 +02003620 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003621}
3622
Guido van Rossum3ac67412007-02-10 18:55:06 +00003623/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003624/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003625/***********************************************/
3626
Guido van Rossumb90c8482007-02-10 01:11:45 +00003627/* The instance lay-out is the same for all three; but the type differs. */
3628
Guido van Rossumb90c8482007-02-10 01:11:45 +00003629static void
Eric Snow96c6af92015-05-29 22:21:39 -06003630dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003631{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003632 Py_XDECREF(dv->dv_dict);
3633 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003634}
3635
3636static int
Eric Snow96c6af92015-05-29 22:21:39 -06003637dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003638{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003639 Py_VISIT(dv->dv_dict);
3640 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003641}
3642
Guido van Rossum83825ac2007-02-10 04:54:19 +00003643static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003644dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003645{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003646 Py_ssize_t len = 0;
3647 if (dv->dv_dict != NULL)
3648 len = dv->dv_dict->ma_used;
3649 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003650}
3651
Eric Snow96c6af92015-05-29 22:21:39 -06003652PyObject *
3653_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003654{
Eric Snow96c6af92015-05-29 22:21:39 -06003655 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003656 if (dict == NULL) {
3657 PyErr_BadInternalCall();
3658 return NULL;
3659 }
3660 if (!PyDict_Check(dict)) {
3661 /* XXX Get rid of this restriction later */
3662 PyErr_Format(PyExc_TypeError,
3663 "%s() requires a dict argument, not '%s'",
3664 type->tp_name, dict->ob_type->tp_name);
3665 return NULL;
3666 }
Eric Snow96c6af92015-05-29 22:21:39 -06003667 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003668 if (dv == NULL)
3669 return NULL;
3670 Py_INCREF(dict);
3671 dv->dv_dict = (PyDictObject *)dict;
3672 _PyObject_GC_TRACK(dv);
3673 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003674}
3675
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003676/* TODO(guido): The views objects are not complete:
3677
3678 * support more set operations
3679 * support arbitrary mappings?
3680 - either these should be static or exported in dictobject.h
3681 - if public then they should probably be in builtins
3682*/
3683
Guido van Rossumaac530c2007-08-24 22:33:45 +00003684/* Return 1 if self is a subset of other, iterating over self;
3685 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003686static int
3687all_contained_in(PyObject *self, PyObject *other)
3688{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003689 PyObject *iter = PyObject_GetIter(self);
3690 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003691
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003692 if (iter == NULL)
3693 return -1;
3694 for (;;) {
3695 PyObject *next = PyIter_Next(iter);
3696 if (next == NULL) {
3697 if (PyErr_Occurred())
3698 ok = -1;
3699 break;
3700 }
3701 ok = PySequence_Contains(other, next);
3702 Py_DECREF(next);
3703 if (ok <= 0)
3704 break;
3705 }
3706 Py_DECREF(iter);
3707 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003708}
3709
3710static PyObject *
3711dictview_richcompare(PyObject *self, PyObject *other, int op)
3712{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003713 Py_ssize_t len_self, len_other;
3714 int ok;
3715 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003717 assert(self != NULL);
3718 assert(PyDictViewSet_Check(self));
3719 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003720
Brian Curtindfc80e32011-08-10 20:28:54 -05003721 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3722 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003724 len_self = PyObject_Size(self);
3725 if (len_self < 0)
3726 return NULL;
3727 len_other = PyObject_Size(other);
3728 if (len_other < 0)
3729 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003730
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003731 ok = 0;
3732 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003734 case Py_NE:
3735 case Py_EQ:
3736 if (len_self == len_other)
3737 ok = all_contained_in(self, other);
3738 if (op == Py_NE && ok >= 0)
3739 ok = !ok;
3740 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003742 case Py_LT:
3743 if (len_self < len_other)
3744 ok = all_contained_in(self, other);
3745 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003747 case Py_LE:
3748 if (len_self <= len_other)
3749 ok = all_contained_in(self, other);
3750 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003752 case Py_GT:
3753 if (len_self > len_other)
3754 ok = all_contained_in(other, self);
3755 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003757 case Py_GE:
3758 if (len_self >= len_other)
3759 ok = all_contained_in(other, self);
3760 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003762 }
3763 if (ok < 0)
3764 return NULL;
3765 result = ok ? Py_True : Py_False;
3766 Py_INCREF(result);
3767 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003768}
3769
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003770static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003771dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003772{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003773 PyObject *seq;
3774 PyObject *result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003776 seq = PySequence_List((PyObject *)dv);
3777 if (seq == NULL)
3778 return NULL;
3779
3780 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
3781 Py_DECREF(seq);
3782 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003783}
3784
Guido van Rossum3ac67412007-02-10 18:55:06 +00003785/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003786
3787static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003788dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003789{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003790 if (dv->dv_dict == NULL) {
3791 Py_RETURN_NONE;
3792 }
3793 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003794}
3795
3796static int
Eric Snow96c6af92015-05-29 22:21:39 -06003797dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00003798{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003799 if (dv->dv_dict == NULL)
3800 return 0;
3801 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003802}
3803
Guido van Rossum83825ac2007-02-10 04:54:19 +00003804static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003805 (lenfunc)dictview_len, /* sq_length */
3806 0, /* sq_concat */
3807 0, /* sq_repeat */
3808 0, /* sq_item */
3809 0, /* sq_slice */
3810 0, /* sq_ass_item */
3811 0, /* sq_ass_slice */
3812 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003813};
3814
Guido van Rossum523259b2007-08-24 23:41:22 +00003815static PyObject*
3816dictviews_sub(PyObject* self, PyObject *other)
3817{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003818 PyObject *result = PySet_New(self);
3819 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003820 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003822 if (result == NULL)
3823 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003824
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003825 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003826 if (tmp == NULL) {
3827 Py_DECREF(result);
3828 return NULL;
3829 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003830
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003831 Py_DECREF(tmp);
3832 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003833}
3834
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003835PyObject*
3836_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00003837{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003838 PyObject *result = PySet_New(self);
3839 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003840 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003842 if (result == NULL)
3843 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003844
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003845 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_intersection_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003846 if (tmp == NULL) {
3847 Py_DECREF(result);
3848 return NULL;
3849 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003851 Py_DECREF(tmp);
3852 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003853}
3854
3855static PyObject*
3856dictviews_or(PyObject* self, PyObject *other)
3857{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003858 PyObject *result = PySet_New(self);
3859 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003860 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003862 if (result == NULL)
3863 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003864
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003865 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003866 if (tmp == NULL) {
3867 Py_DECREF(result);
3868 return NULL;
3869 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003870
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003871 Py_DECREF(tmp);
3872 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003873}
3874
3875static PyObject*
3876dictviews_xor(PyObject* self, PyObject *other)
3877{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003878 PyObject *result = PySet_New(self);
3879 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003880 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003882 if (result == NULL)
3883 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003884
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003885 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_symmetric_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003886 if (tmp == NULL) {
3887 Py_DECREF(result);
3888 return NULL;
3889 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003891 Py_DECREF(tmp);
3892 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003893}
3894
3895static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003896 0, /*nb_add*/
3897 (binaryfunc)dictviews_sub, /*nb_subtract*/
3898 0, /*nb_multiply*/
3899 0, /*nb_remainder*/
3900 0, /*nb_divmod*/
3901 0, /*nb_power*/
3902 0, /*nb_negative*/
3903 0, /*nb_positive*/
3904 0, /*nb_absolute*/
3905 0, /*nb_bool*/
3906 0, /*nb_invert*/
3907 0, /*nb_lshift*/
3908 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003909 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003910 (binaryfunc)dictviews_xor, /*nb_xor*/
3911 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00003912};
3913
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003914static PyObject*
3915dictviews_isdisjoint(PyObject *self, PyObject *other)
3916{
3917 PyObject *it;
3918 PyObject *item = NULL;
3919
3920 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06003921 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003922 Py_RETURN_TRUE;
3923 else
3924 Py_RETURN_FALSE;
3925 }
3926
3927 /* Iterate over the shorter object (only if other is a set,
3928 * because PySequence_Contains may be expensive otherwise): */
3929 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06003930 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003931 Py_ssize_t len_other = PyObject_Size(other);
3932 if (len_other == -1)
3933 return NULL;
3934
3935 if ((len_other > len_self)) {
3936 PyObject *tmp = other;
3937 other = self;
3938 self = tmp;
3939 }
3940 }
3941
3942 it = PyObject_GetIter(other);
3943 if (it == NULL)
3944 return NULL;
3945
3946 while ((item = PyIter_Next(it)) != NULL) {
3947 int contains = PySequence_Contains(self, item);
3948 Py_DECREF(item);
3949 if (contains == -1) {
3950 Py_DECREF(it);
3951 return NULL;
3952 }
3953
3954 if (contains) {
3955 Py_DECREF(it);
3956 Py_RETURN_FALSE;
3957 }
3958 }
3959 Py_DECREF(it);
3960 if (PyErr_Occurred())
3961 return NULL; /* PyIter_Next raised an exception. */
3962 Py_RETURN_TRUE;
3963}
3964
3965PyDoc_STRVAR(isdisjoint_doc,
3966"Return True if the view and the given iterable have a null intersection.");
3967
Guido van Rossumb90c8482007-02-10 01:11:45 +00003968static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003969 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3970 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003971 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003972};
3973
3974PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003975 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3976 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06003977 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003978 0, /* tp_itemsize */
3979 /* methods */
3980 (destructor)dictview_dealloc, /* tp_dealloc */
3981 0, /* tp_print */
3982 0, /* tp_getattr */
3983 0, /* tp_setattr */
3984 0, /* tp_reserved */
3985 (reprfunc)dictview_repr, /* tp_repr */
3986 &dictviews_as_number, /* tp_as_number */
3987 &dictkeys_as_sequence, /* tp_as_sequence */
3988 0, /* tp_as_mapping */
3989 0, /* tp_hash */
3990 0, /* tp_call */
3991 0, /* tp_str */
3992 PyObject_GenericGetAttr, /* tp_getattro */
3993 0, /* tp_setattro */
3994 0, /* tp_as_buffer */
3995 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3996 0, /* tp_doc */
3997 (traverseproc)dictview_traverse, /* tp_traverse */
3998 0, /* tp_clear */
3999 dictview_richcompare, /* tp_richcompare */
4000 0, /* tp_weaklistoffset */
4001 (getiterfunc)dictkeys_iter, /* tp_iter */
4002 0, /* tp_iternext */
4003 dictkeys_methods, /* tp_methods */
4004 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004005};
4006
4007static PyObject *
4008dictkeys_new(PyObject *dict)
4009{
Eric Snow96c6af92015-05-29 22:21:39 -06004010 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004011}
4012
Guido van Rossum3ac67412007-02-10 18:55:06 +00004013/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004014
4015static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004016dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004017{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004018 if (dv->dv_dict == NULL) {
4019 Py_RETURN_NONE;
4020 }
4021 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004022}
4023
4024static int
Eric Snow96c6af92015-05-29 22:21:39 -06004025dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004026{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004027 int result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004028 PyObject *key, *value, *found;
4029 if (dv->dv_dict == NULL)
4030 return 0;
4031 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
4032 return 0;
4033 key = PyTuple_GET_ITEM(obj, 0);
4034 value = PyTuple_GET_ITEM(obj, 1);
Raymond Hettinger6692f012016-09-18 21:46:08 -07004035 found = PyDict_GetItemWithError((PyObject *)dv->dv_dict, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004036 if (found == NULL) {
4037 if (PyErr_Occurred())
4038 return -1;
4039 return 0;
4040 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004041 Py_INCREF(found);
4042 result = PyObject_RichCompareBool(value, found, Py_EQ);
4043 Py_DECREF(found);
4044 return result;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004045}
4046
Guido van Rossum83825ac2007-02-10 04:54:19 +00004047static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004048 (lenfunc)dictview_len, /* sq_length */
4049 0, /* sq_concat */
4050 0, /* sq_repeat */
4051 0, /* sq_item */
4052 0, /* sq_slice */
4053 0, /* sq_ass_item */
4054 0, /* sq_ass_slice */
4055 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004056};
4057
Guido van Rossumb90c8482007-02-10 01:11:45 +00004058static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004059 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4060 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004061 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004062};
4063
4064PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004065 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4066 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004067 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004068 0, /* tp_itemsize */
4069 /* methods */
4070 (destructor)dictview_dealloc, /* tp_dealloc */
4071 0, /* tp_print */
4072 0, /* tp_getattr */
4073 0, /* tp_setattr */
4074 0, /* tp_reserved */
4075 (reprfunc)dictview_repr, /* tp_repr */
4076 &dictviews_as_number, /* tp_as_number */
4077 &dictitems_as_sequence, /* tp_as_sequence */
4078 0, /* tp_as_mapping */
4079 0, /* tp_hash */
4080 0, /* tp_call */
4081 0, /* tp_str */
4082 PyObject_GenericGetAttr, /* tp_getattro */
4083 0, /* tp_setattro */
4084 0, /* tp_as_buffer */
4085 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4086 0, /* tp_doc */
4087 (traverseproc)dictview_traverse, /* tp_traverse */
4088 0, /* tp_clear */
4089 dictview_richcompare, /* tp_richcompare */
4090 0, /* tp_weaklistoffset */
4091 (getiterfunc)dictitems_iter, /* tp_iter */
4092 0, /* tp_iternext */
4093 dictitems_methods, /* tp_methods */
4094 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004095};
4096
4097static PyObject *
4098dictitems_new(PyObject *dict)
4099{
Eric Snow96c6af92015-05-29 22:21:39 -06004100 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004101}
4102
Guido van Rossum3ac67412007-02-10 18:55:06 +00004103/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004104
4105static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004106dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004107{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004108 if (dv->dv_dict == NULL) {
4109 Py_RETURN_NONE;
4110 }
4111 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004112}
4113
Guido van Rossum83825ac2007-02-10 04:54:19 +00004114static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004115 (lenfunc)dictview_len, /* sq_length */
4116 0, /* sq_concat */
4117 0, /* sq_repeat */
4118 0, /* sq_item */
4119 0, /* sq_slice */
4120 0, /* sq_ass_item */
4121 0, /* sq_ass_slice */
4122 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004123};
4124
Guido van Rossumb90c8482007-02-10 01:11:45 +00004125static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004126 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004127};
4128
4129PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004130 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4131 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004132 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004133 0, /* tp_itemsize */
4134 /* methods */
4135 (destructor)dictview_dealloc, /* tp_dealloc */
4136 0, /* tp_print */
4137 0, /* tp_getattr */
4138 0, /* tp_setattr */
4139 0, /* tp_reserved */
4140 (reprfunc)dictview_repr, /* tp_repr */
4141 0, /* tp_as_number */
4142 &dictvalues_as_sequence, /* tp_as_sequence */
4143 0, /* tp_as_mapping */
4144 0, /* tp_hash */
4145 0, /* tp_call */
4146 0, /* tp_str */
4147 PyObject_GenericGetAttr, /* tp_getattro */
4148 0, /* tp_setattro */
4149 0, /* tp_as_buffer */
4150 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4151 0, /* tp_doc */
4152 (traverseproc)dictview_traverse, /* tp_traverse */
4153 0, /* tp_clear */
4154 0, /* tp_richcompare */
4155 0, /* tp_weaklistoffset */
4156 (getiterfunc)dictvalues_iter, /* tp_iter */
4157 0, /* tp_iternext */
4158 dictvalues_methods, /* tp_methods */
4159 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004160};
4161
4162static PyObject *
4163dictvalues_new(PyObject *dict)
4164{
Eric Snow96c6af92015-05-29 22:21:39 -06004165 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004166}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004167
4168/* Returns NULL if cannot allocate a new PyDictKeysObject,
4169 but does not set an error */
4170PyDictKeysObject *
4171_PyDict_NewKeysForClass(void)
4172{
Victor Stinner742da042016-09-07 17:40:12 -07004173 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004174 if (keys == NULL)
4175 PyErr_Clear();
4176 else
4177 keys->dk_lookup = lookdict_split;
4178 return keys;
4179}
4180
4181#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
4182
4183PyObject *
4184PyObject_GenericGetDict(PyObject *obj, void *context)
4185{
4186 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
4187 if (dictptr == NULL) {
4188 PyErr_SetString(PyExc_AttributeError,
4189 "This object has no __dict__");
4190 return NULL;
4191 }
4192 dict = *dictptr;
4193 if (dict == NULL) {
4194 PyTypeObject *tp = Py_TYPE(obj);
4195 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
4196 DK_INCREF(CACHED_KEYS(tp));
4197 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
4198 }
4199 else {
4200 *dictptr = dict = PyDict_New();
4201 }
4202 }
4203 Py_XINCREF(dict);
4204 return dict;
4205}
4206
4207int
4208_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
Victor Stinner742da042016-09-07 17:40:12 -07004209 PyObject *key, PyObject *value)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004210{
4211 PyObject *dict;
4212 int res;
4213 PyDictKeysObject *cached;
4214
4215 assert(dictptr != NULL);
4216 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
4217 assert(dictptr != NULL);
4218 dict = *dictptr;
4219 if (dict == NULL) {
4220 DK_INCREF(cached);
4221 dict = new_dict_with_shared_keys(cached);
4222 if (dict == NULL)
4223 return -1;
4224 *dictptr = dict;
4225 }
4226 if (value == NULL) {
4227 res = PyDict_DelItem(dict, key);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004228 // Since key sharing dict doesn't allow deletion, PyDict_DelItem()
4229 // always converts dict to combined form.
4230 if ((cached = CACHED_KEYS(tp)) != NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004231 CACHED_KEYS(tp) = NULL;
4232 DK_DECREF(cached);
4233 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01004234 }
4235 else {
INADA Naoki2294f3a2017-02-12 13:51:30 +09004236 int was_shared = (cached == ((PyDictObject *)dict)->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004237 res = PyDict_SetItem(dict, key, value);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004238 if (was_shared &&
4239 (cached = CACHED_KEYS(tp)) != NULL &&
4240 cached != ((PyDictObject *)dict)->ma_keys) {
Victor Stinner3d3f2642016-12-15 17:21:23 +01004241 /* PyDict_SetItem() may call dictresize and convert split table
4242 * into combined table. In such case, convert it to split
4243 * table again and update type's shared key only when this is
4244 * the only dict sharing key with the type.
4245 *
4246 * This is to allow using shared key in class like this:
4247 *
4248 * class C:
4249 * def __init__(self):
4250 * # one dict resize happens
4251 * self.a, self.b, self.c = 1, 2, 3
4252 * self.d, self.e, self.f = 4, 5, 6
4253 * a = C()
4254 */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004255 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004256 CACHED_KEYS(tp) = make_keys_shared(dict);
Victor Stinner742da042016-09-07 17:40:12 -07004257 }
4258 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004259 CACHED_KEYS(tp) = NULL;
4260 }
4261 DK_DECREF(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004262 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
4263 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004264 }
4265 }
4266 } else {
4267 dict = *dictptr;
4268 if (dict == NULL) {
4269 dict = PyDict_New();
4270 if (dict == NULL)
4271 return -1;
4272 *dictptr = dict;
4273 }
4274 if (value == NULL) {
4275 res = PyDict_DelItem(dict, key);
4276 } else {
4277 res = PyDict_SetItem(dict, key, value);
4278 }
4279 }
4280 return res;
4281}
4282
4283void
4284_PyDictKeys_DecRef(PyDictKeysObject *keys)
4285{
4286 DK_DECREF(keys);
4287}