blob: f055170750ded01a56430524eb8a0524edab97b9 [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;
INADA Naokia6296d32017-08-24 14:55:17 +09001850
1851 /* bpo-31095: UnTrack is needed before calling any callbacks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001852 PyObject_GC_UnTrack(mp);
1853 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001854 if (values != NULL) {
1855 if (values != empty_values) {
Victor Stinner742da042016-09-07 17:40:12 -07001856 for (i = 0, n = mp->ma_keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001857 Py_XDECREF(values[i]);
1858 }
1859 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001861 DK_DECREF(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02001863 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001864 assert(keys->dk_refcnt == 1);
1865 DK_DECREF(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001866 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001867 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1868 free_list[numfree++] = mp;
1869 else
1870 Py_TYPE(mp)->tp_free((PyObject *)mp);
1871 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001872}
1873
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001874
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001875static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001876dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001877{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001879 PyObject *key = NULL, *value = NULL;
1880 _PyUnicodeWriter writer;
1881 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00001882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001883 i = Py_ReprEnter((PyObject *)mp);
1884 if (i != 0) {
1885 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1886 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001889 Py_ReprLeave((PyObject *)mp);
1890 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 }
Tim Petersa7259592001-06-16 05:11:17 +00001892
Victor Stinnerf91929b2013-11-19 13:07:38 +01001893 _PyUnicodeWriter_Init(&writer);
1894 writer.overallocate = 1;
1895 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
1896 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00001897
Victor Stinnerf91929b2013-11-19 13:07:38 +01001898 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
1899 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001901 /* Do repr() on each key+value pair, and insert ": " between them.
1902 Note that repr may mutate the dict. */
1903 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001904 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001906 PyObject *s;
1907 int res;
1908
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001909 /* Prevent repr from deleting key or value during key format. */
1910 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001911 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02001912
Victor Stinnerf91929b2013-11-19 13:07:38 +01001913 if (!first) {
1914 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
1915 goto error;
1916 }
1917 first = 0;
1918
1919 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01001921 goto error;
1922 res = _PyUnicodeWriter_WriteStr(&writer, s);
1923 Py_DECREF(s);
1924 if (res < 0)
1925 goto error;
1926
1927 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
1928 goto error;
1929
1930 s = PyObject_Repr(value);
1931 if (s == NULL)
1932 goto error;
1933 res = _PyUnicodeWriter_WriteStr(&writer, s);
1934 Py_DECREF(s);
1935 if (res < 0)
1936 goto error;
1937
1938 Py_CLEAR(key);
1939 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 }
Tim Petersa7259592001-06-16 05:11:17 +00001941
Victor Stinnerf91929b2013-11-19 13:07:38 +01001942 writer.overallocate = 0;
1943 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
1944 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001946 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01001947
1948 return _PyUnicodeWriter_Finish(&writer);
1949
1950error:
1951 Py_ReprLeave((PyObject *)mp);
1952 _PyUnicodeWriter_Dealloc(&writer);
1953 Py_XDECREF(key);
1954 Py_XDECREF(value);
1955 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001956}
1957
Martin v. Löwis18e16552006-02-15 17:27:45 +00001958static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001959dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001960{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001962}
1963
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001964static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001965dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001966{
Victor Stinner742da042016-09-07 17:40:12 -07001967 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001968 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09001969 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001970
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001971 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001972 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001973 hash = PyObject_Hash(key);
1974 if (hash == -1)
1975 return NULL;
1976 }
INADA Naoki778928b2017-08-03 23:45:15 +09001977 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07001978 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001980 if (ix == DKIX_EMPTY || value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 if (!PyDict_CheckExact(mp)) {
1982 /* Look up __missing__ method if we're a subclass. */
1983 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05001984 _Py_IDENTIFIER(__missing__);
1985 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001986 if (missing != NULL) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001987 res = PyObject_CallFunctionObjArgs(missing,
1988 key, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 Py_DECREF(missing);
1990 return res;
1991 }
1992 else if (PyErr_Occurred())
1993 return NULL;
1994 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07001995 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001996 return NULL;
1997 }
INADA Naokiba609772016-12-07 20:41:42 +09001998 Py_INCREF(value);
1999 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002000}
2001
2002static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002003dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002004{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002005 if (w == NULL)
2006 return PyDict_DelItem((PyObject *)mp, v);
2007 else
2008 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002009}
2010
Guido van Rossuma9e7a811997-05-13 21:02:11 +00002011static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002012 (lenfunc)dict_length, /*mp_length*/
2013 (binaryfunc)dict_subscript, /*mp_subscript*/
2014 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002015};
2016
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002017static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002018dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002019{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002020 PyObject *v;
2021 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002022 PyDictKeyEntry *ep;
2023 Py_ssize_t size, n, offset;
2024 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002025
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002026 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 n = mp->ma_used;
2028 v = PyList_New(n);
2029 if (v == NULL)
2030 return NULL;
2031 if (n != mp->ma_used) {
2032 /* Durnit. The allocations caused the dict to resize.
2033 * Just start over, this shouldn't normally happen.
2034 */
2035 Py_DECREF(v);
2036 goto again;
2037 }
Victor Stinner742da042016-09-07 17:40:12 -07002038 ep = DK_ENTRIES(mp->ma_keys);
2039 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002040 if (mp->ma_values) {
2041 value_ptr = mp->ma_values;
2042 offset = sizeof(PyObject *);
2043 }
2044 else {
2045 value_ptr = &ep[0].me_value;
2046 offset = sizeof(PyDictKeyEntry);
2047 }
2048 for (i = 0, j = 0; i < size; i++) {
2049 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002050 PyObject *key = ep[i].me_key;
2051 Py_INCREF(key);
2052 PyList_SET_ITEM(v, j, key);
2053 j++;
2054 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002055 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002056 }
2057 assert(j == n);
2058 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002059}
2060
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002061static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002062dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002063{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002064 PyObject *v;
2065 Py_ssize_t i, j;
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002066 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002067 Py_ssize_t size, n, offset;
2068 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002069
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002070 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071 n = mp->ma_used;
2072 v = PyList_New(n);
2073 if (v == NULL)
2074 return NULL;
2075 if (n != mp->ma_used) {
2076 /* Durnit. The allocations caused the dict to resize.
2077 * Just start over, this shouldn't normally happen.
2078 */
2079 Py_DECREF(v);
2080 goto again;
2081 }
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002082 ep = DK_ENTRIES(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002083 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002084 if (mp->ma_values) {
2085 value_ptr = mp->ma_values;
2086 offset = sizeof(PyObject *);
2087 }
2088 else {
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002089 value_ptr = &ep[0].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002090 offset = sizeof(PyDictKeyEntry);
2091 }
2092 for (i = 0, j = 0; i < size; i++) {
2093 PyObject *value = *value_ptr;
2094 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2095 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096 Py_INCREF(value);
2097 PyList_SET_ITEM(v, j, value);
2098 j++;
2099 }
2100 }
2101 assert(j == n);
2102 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002103}
2104
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002105static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002106dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002107{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002108 PyObject *v;
2109 Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002110 Py_ssize_t size, offset;
2111 PyObject *item, *key;
2112 PyDictKeyEntry *ep;
2113 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002115 /* Preallocate the list of tuples, to avoid allocations during
2116 * the loop over the items, which could trigger GC, which
2117 * could resize the dict. :-(
2118 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002119 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002120 n = mp->ma_used;
2121 v = PyList_New(n);
2122 if (v == NULL)
2123 return NULL;
2124 for (i = 0; i < n; i++) {
2125 item = PyTuple_New(2);
2126 if (item == NULL) {
2127 Py_DECREF(v);
2128 return NULL;
2129 }
2130 PyList_SET_ITEM(v, i, item);
2131 }
2132 if (n != mp->ma_used) {
2133 /* Durnit. The allocations caused the dict to resize.
2134 * Just start over, this shouldn't normally happen.
2135 */
2136 Py_DECREF(v);
2137 goto again;
2138 }
2139 /* Nothing we do below makes any function calls. */
Victor Stinner742da042016-09-07 17:40:12 -07002140 ep = DK_ENTRIES(mp->ma_keys);
2141 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002142 if (mp->ma_values) {
2143 value_ptr = mp->ma_values;
2144 offset = sizeof(PyObject *);
2145 }
2146 else {
2147 value_ptr = &ep[0].me_value;
2148 offset = sizeof(PyDictKeyEntry);
2149 }
2150 for (i = 0, j = 0; i < size; i++) {
2151 PyObject *value = *value_ptr;
2152 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2153 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154 key = ep[i].me_key;
2155 item = PyList_GET_ITEM(v, j);
2156 Py_INCREF(key);
2157 PyTuple_SET_ITEM(item, 0, key);
2158 Py_INCREF(value);
2159 PyTuple_SET_ITEM(item, 1, value);
2160 j++;
2161 }
2162 }
2163 assert(j == n);
2164 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002165}
2166
Larry Hastings5c661892014-01-24 06:17:25 -08002167/*[clinic input]
2168@classmethod
2169dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08002170 iterable: object
2171 value: object=None
2172 /
2173
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002174Create a new dictionary with keys from iterable and values set to value.
Larry Hastings5c661892014-01-24 06:17:25 -08002175[clinic start generated code]*/
2176
Larry Hastings5c661892014-01-24 06:17:25 -08002177static PyObject *
2178dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002179/*[clinic end generated code: output=8fb98e4b10384999 input=382ba4855d0f74c3]*/
Larry Hastings5c661892014-01-24 06:17:25 -08002180{
Eric Snow96c6af92015-05-29 22:21:39 -06002181 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002182}
2183
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002184static int
Victor Stinner742da042016-09-07 17:40:12 -07002185dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
2186 const char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002188 PyObject *arg = NULL;
2189 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
2192 result = -1;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002195 _Py_IDENTIFIER(keys);
2196 if (_PyObject_HasAttrId(arg, &PyId_keys))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 result = PyDict_Merge(self, arg, 1);
2198 else
2199 result = PyDict_MergeFromSeq2(self, arg, 1);
2200 }
2201 if (result == 0 && kwds != NULL) {
2202 if (PyArg_ValidateKeywordArguments(kwds))
2203 result = PyDict_Merge(self, kwds, 1);
2204 else
2205 result = -1;
2206 }
2207 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002208}
2209
Victor Stinner91f0d4a2017-01-19 12:45:06 +01002210/* Note: dict.update() uses the METH_VARARGS|METH_KEYWORDS calling convention.
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +03002211 Using METH_FASTCALL|METH_KEYWORDS would make dict.update(**dict2) calls
2212 slower, see the issue #29312. */
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002213static PyObject *
2214dict_update(PyObject *self, PyObject *args, PyObject *kwds)
2215{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002216 if (dict_update_common(self, args, kwds, "update") != -1)
2217 Py_RETURN_NONE;
2218 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002219}
2220
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002221/* Update unconditionally replaces existing items.
2222 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00002223 otherwise it leaves existing items unchanged.
2224
2225 PyDict_{Update,Merge} update/merge from a mapping object.
2226
Tim Petersf582b822001-12-11 18:51:08 +00002227 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00002228 producing iterable objects of length 2.
2229*/
2230
Tim Petersf582b822001-12-11 18:51:08 +00002231int
Tim Peters1fc240e2001-10-26 05:06:50 +00002232PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
2233{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002234 PyObject *it; /* iter(seq2) */
2235 Py_ssize_t i; /* index into seq2 of current element */
2236 PyObject *item; /* seq2[i] */
2237 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00002238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002239 assert(d != NULL);
2240 assert(PyDict_Check(d));
2241 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00002242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002243 it = PyObject_GetIter(seq2);
2244 if (it == NULL)
2245 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002247 for (i = 0; ; ++i) {
2248 PyObject *key, *value;
2249 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00002250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002251 fast = NULL;
2252 item = PyIter_Next(it);
2253 if (item == NULL) {
2254 if (PyErr_Occurred())
2255 goto Fail;
2256 break;
2257 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002259 /* Convert item to sequence, and verify length 2. */
2260 fast = PySequence_Fast(item, "");
2261 if (fast == NULL) {
2262 if (PyErr_ExceptionMatches(PyExc_TypeError))
2263 PyErr_Format(PyExc_TypeError,
2264 "cannot convert dictionary update "
2265 "sequence element #%zd to a sequence",
2266 i);
2267 goto Fail;
2268 }
2269 n = PySequence_Fast_GET_SIZE(fast);
2270 if (n != 2) {
2271 PyErr_Format(PyExc_ValueError,
2272 "dictionary update sequence element #%zd "
2273 "has length %zd; 2 is required",
2274 i, n);
2275 goto Fail;
2276 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 /* Update/merge with this (key, value) pair. */
2279 key = PySequence_Fast_GET_ITEM(fast, 0);
2280 value = PySequence_Fast_GET_ITEM(fast, 1);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002281 Py_INCREF(key);
2282 Py_INCREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002283 if (override || PyDict_GetItem(d, key) == NULL) {
2284 int status = PyDict_SetItem(d, key, value);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002285 if (status < 0) {
2286 Py_DECREF(key);
2287 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002288 goto Fail;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002289 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002290 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002291 Py_DECREF(key);
2292 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002293 Py_DECREF(fast);
2294 Py_DECREF(item);
2295 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002297 i = 0;
Victor Stinner611b0fa2016-09-14 15:02:01 +02002298 assert(_PyDict_CheckConsistency((PyDictObject *)d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002299 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002300Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002301 Py_XDECREF(item);
2302 Py_XDECREF(fast);
2303 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002304Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002305 Py_DECREF(it);
2306 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002307}
2308
doko@ubuntu.comc96df682016-10-11 08:04:02 +02002309static int
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002310dict_merge(PyObject *a, PyObject *b, int override)
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002311{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002312 PyDictObject *mp, *other;
2313 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -07002314 PyDictKeyEntry *entry, *ep0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002315
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002316 assert(0 <= override && override <= 2);
2317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 /* We accept for the argument either a concrete dictionary object,
2319 * or an abstract "mapping" object. For the former, we can do
2320 * things quite efficiently. For the latter, we only require that
2321 * PyMapping_Keys() and PyObject_GetItem() be supported.
2322 */
2323 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2324 PyErr_BadInternalCall();
2325 return -1;
2326 }
2327 mp = (PyDictObject*)a;
2328 if (PyDict_Check(b)) {
2329 other = (PyDictObject*)b;
2330 if (other == mp || other->ma_used == 0)
2331 /* a.update(a) or a.update({}); nothing to do */
2332 return 0;
2333 if (mp->ma_used == 0)
2334 /* Since the target dict is empty, PyDict_GetItem()
2335 * always returns NULL. Setting override to 1
2336 * skips the unnecessary test.
2337 */
2338 override = 1;
2339 /* Do one big resize at the start, rather than
2340 * incrementally resizing as we insert new items. Expect
2341 * that there will be no (or few) overlapping keys.
2342 */
INADA Naokib1152be2016-10-27 19:26:50 +09002343 if (USABLE_FRACTION(mp->ma_keys->dk_size) < other->ma_used) {
2344 if (dictresize(mp, ESTIMATE_SIZE(mp->ma_used + other->ma_used))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002345 return -1;
INADA Naokib1152be2016-10-27 19:26:50 +09002346 }
2347 }
Victor Stinner742da042016-09-07 17:40:12 -07002348 ep0 = DK_ENTRIES(other->ma_keys);
2349 for (i = 0, n = other->ma_keys->dk_nentries; i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002350 PyObject *key, *value;
2351 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002352 entry = &ep0[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002353 key = entry->me_key;
2354 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002355 if (other->ma_values)
2356 value = other->ma_values[i];
2357 else
2358 value = entry->me_value;
2359
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002360 if (value != NULL) {
2361 int err = 0;
2362 Py_INCREF(key);
2363 Py_INCREF(value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002364 if (override == 1)
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002365 err = insertdict(mp, key, hash, value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002366 else if (_PyDict_GetItem_KnownHash(a, key, hash) == NULL) {
2367 if (PyErr_Occurred()) {
2368 Py_DECREF(value);
2369 Py_DECREF(key);
2370 return -1;
2371 }
2372 err = insertdict(mp, key, hash, value);
2373 }
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002374 else if (override != 0) {
2375 _PyErr_SetKeyError(key);
2376 Py_DECREF(value);
2377 Py_DECREF(key);
2378 return -1;
2379 }
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002380 Py_DECREF(value);
2381 Py_DECREF(key);
2382 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002384
Victor Stinner742da042016-09-07 17:40:12 -07002385 if (n != other->ma_keys->dk_nentries) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002386 PyErr_SetString(PyExc_RuntimeError,
2387 "dict mutated during update");
2388 return -1;
2389 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002390 }
2391 }
2392 }
2393 else {
2394 /* Do it the generic, slower way */
2395 PyObject *keys = PyMapping_Keys(b);
2396 PyObject *iter;
2397 PyObject *key, *value;
2398 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002400 if (keys == NULL)
2401 /* Docstring says this is equivalent to E.keys() so
2402 * if E doesn't have a .keys() method we want
2403 * AttributeError to percolate up. Might as well
2404 * do the same for any other error.
2405 */
2406 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002408 iter = PyObject_GetIter(keys);
2409 Py_DECREF(keys);
2410 if (iter == NULL)
2411 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002413 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002414 if (override != 1 && PyDict_GetItem(a, key) != NULL) {
2415 if (override != 0) {
2416 _PyErr_SetKeyError(key);
2417 Py_DECREF(key);
2418 Py_DECREF(iter);
2419 return -1;
2420 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002421 Py_DECREF(key);
2422 continue;
2423 }
2424 value = PyObject_GetItem(b, key);
2425 if (value == NULL) {
2426 Py_DECREF(iter);
2427 Py_DECREF(key);
2428 return -1;
2429 }
2430 status = PyDict_SetItem(a, key, value);
2431 Py_DECREF(key);
2432 Py_DECREF(value);
2433 if (status < 0) {
2434 Py_DECREF(iter);
2435 return -1;
2436 }
2437 }
2438 Py_DECREF(iter);
2439 if (PyErr_Occurred())
2440 /* Iterator completed, via error */
2441 return -1;
2442 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02002443 assert(_PyDict_CheckConsistency((PyDictObject *)a));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002444 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002445}
2446
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002447int
2448PyDict_Update(PyObject *a, PyObject *b)
2449{
2450 return dict_merge(a, b, 1);
2451}
2452
2453int
2454PyDict_Merge(PyObject *a, PyObject *b, int override)
2455{
2456 /* XXX Deprecate override not in (0, 1). */
2457 return dict_merge(a, b, override != 0);
2458}
2459
2460int
2461_PyDict_MergeEx(PyObject *a, PyObject *b, int override)
2462{
2463 return dict_merge(a, b, override);
2464}
2465
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002466static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002467dict_copy(PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002468{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002469 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002470}
2471
2472PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002473PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002474{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002475 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002476 PyDictObject *mp;
2477 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002479 if (o == NULL || !PyDict_Check(o)) {
2480 PyErr_BadInternalCall();
2481 return NULL;
2482 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002483 mp = (PyDictObject *)o;
2484 if (_PyDict_HasSplitTable(mp)) {
2485 PyDictObject *split_copy;
Victor Stinner742da042016-09-07 17:40:12 -07002486 Py_ssize_t size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
2487 PyObject **newvalues;
2488 newvalues = new_values(size);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002489 if (newvalues == NULL)
2490 return PyErr_NoMemory();
2491 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2492 if (split_copy == NULL) {
2493 free_values(newvalues);
2494 return NULL;
2495 }
2496 split_copy->ma_values = newvalues;
2497 split_copy->ma_keys = mp->ma_keys;
2498 split_copy->ma_used = mp->ma_used;
2499 DK_INCREF(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002500 for (i = 0, n = size; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002501 PyObject *value = mp->ma_values[i];
2502 Py_XINCREF(value);
2503 split_copy->ma_values[i] = value;
2504 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002505 if (_PyObject_GC_IS_TRACKED(mp))
2506 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002507 return (PyObject *)split_copy;
2508 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002509 copy = PyDict_New();
2510 if (copy == NULL)
2511 return NULL;
2512 if (PyDict_Merge(copy, o, 1) == 0)
2513 return copy;
2514 Py_DECREF(copy);
2515 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002516}
2517
Martin v. Löwis18e16552006-02-15 17:27:45 +00002518Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002519PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002520{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002521 if (mp == NULL || !PyDict_Check(mp)) {
2522 PyErr_BadInternalCall();
2523 return -1;
2524 }
2525 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002526}
2527
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002528PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002529PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002530{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002531 if (mp == NULL || !PyDict_Check(mp)) {
2532 PyErr_BadInternalCall();
2533 return NULL;
2534 }
2535 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002536}
2537
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002538PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002539PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002540{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002541 if (mp == NULL || !PyDict_Check(mp)) {
2542 PyErr_BadInternalCall();
2543 return NULL;
2544 }
2545 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002546}
2547
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002548PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002549PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002551 if (mp == NULL || !PyDict_Check(mp)) {
2552 PyErr_BadInternalCall();
2553 return NULL;
2554 }
2555 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002556}
2557
Tim Peterse63415e2001-05-08 04:38:29 +00002558/* Return 1 if dicts equal, 0 if not, -1 if error.
2559 * Gets out as soon as any difference is detected.
2560 * Uses only Py_EQ comparison.
2561 */
2562static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002563dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002564{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002565 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002567 if (a->ma_used != b->ma_used)
2568 /* can't be equal if # of entries differ */
2569 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002570 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Victor Stinner742da042016-09-07 17:40:12 -07002571 for (i = 0; i < a->ma_keys->dk_nentries; i++) {
2572 PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002573 PyObject *aval;
2574 if (a->ma_values)
2575 aval = a->ma_values[i];
2576 else
2577 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002578 if (aval != NULL) {
2579 int cmp;
2580 PyObject *bval;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002581 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002582 /* temporarily bump aval's refcount to ensure it stays
2583 alive until we're done with it */
2584 Py_INCREF(aval);
2585 /* ditto for key */
2586 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002587 /* reuse the known hash value */
INADA Naoki778928b2017-08-03 23:45:15 +09002588 b->ma_keys->dk_lookup(b, key, ep->me_hash, &bval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002589 if (bval == NULL) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002590 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002591 Py_DECREF(aval);
2592 if (PyErr_Occurred())
2593 return -1;
2594 return 0;
2595 }
2596 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002597 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002598 Py_DECREF(aval);
2599 if (cmp <= 0) /* error or not equal */
2600 return cmp;
2601 }
2602 }
2603 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002604}
Tim Peterse63415e2001-05-08 04:38:29 +00002605
2606static PyObject *
2607dict_richcompare(PyObject *v, PyObject *w, int op)
2608{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002609 int cmp;
2610 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002612 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2613 res = Py_NotImplemented;
2614 }
2615 else if (op == Py_EQ || op == Py_NE) {
2616 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2617 if (cmp < 0)
2618 return NULL;
2619 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2620 }
2621 else
2622 res = Py_NotImplemented;
2623 Py_INCREF(res);
2624 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002625}
Tim Peterse63415e2001-05-08 04:38:29 +00002626
Larry Hastings61272b72014-01-07 12:41:53 -08002627/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002628
2629@coexist
2630dict.__contains__
2631
2632 key: object
2633 /
2634
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002635True if the dictionary has the specified key, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002636[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002637
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002638static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002639dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka19d25972017-02-04 08:05:07 +02002640/*[clinic end generated code: output=a3d03db709ed6e6b input=fe1cb42ad831e820]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002641{
Larry Hastingsc2047262014-01-25 20:43:29 -08002642 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002643 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002644 Py_ssize_t ix;
INADA Naokiba609772016-12-07 20:41:42 +09002645 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002647 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002648 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002649 hash = PyObject_Hash(key);
2650 if (hash == -1)
2651 return NULL;
2652 }
INADA Naoki778928b2017-08-03 23:45:15 +09002653 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002654 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002655 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002656 if (ix == DKIX_EMPTY || value == NULL)
Victor Stinner742da042016-09-07 17:40:12 -07002657 Py_RETURN_FALSE;
2658 Py_RETURN_TRUE;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002659}
2660
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002661/*[clinic input]
2662dict.get
2663
2664 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002665 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002666 /
2667
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002668Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002669[clinic start generated code]*/
2670
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002671static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002672dict_get_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002673/*[clinic end generated code: output=bba707729dee05bf input=279ddb5790b6b107]*/
Barry Warsawc38c5da1997-10-06 17:49:20 +00002674{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002675 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002676 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002677 Py_ssize_t ix;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002679 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002680 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002681 hash = PyObject_Hash(key);
2682 if (hash == -1)
2683 return NULL;
2684 }
INADA Naoki778928b2017-08-03 23:45:15 +09002685 ix = (self->ma_keys->dk_lookup) (self, key, hash, &val);
Victor Stinner742da042016-09-07 17:40:12 -07002686 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002687 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002688 if (ix == DKIX_EMPTY || val == NULL) {
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002689 val = default_value;
INADA Naokiba609772016-12-07 20:41:42 +09002690 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002691 Py_INCREF(val);
2692 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002693}
2694
Benjamin Peterson00e98862013-03-07 22:16:29 -05002695PyObject *
2696PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002697{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002698 PyDictObject *mp = (PyDictObject *)d;
INADA Naoki93f26f72016-11-02 18:45:16 +09002699 PyObject *value;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002700 Py_hash_t hash;
Guido van Rossum164452c2000-08-08 16:12:54 +00002701
Benjamin Peterson00e98862013-03-07 22:16:29 -05002702 if (!PyDict_Check(d)) {
2703 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002704 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002705 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002707 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002708 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002709 hash = PyObject_Hash(key);
2710 if (hash == -1)
2711 return NULL;
2712 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002713
2714 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
2715 if (insertion_resize(mp) < 0)
2716 return NULL;
2717 }
2718
INADA Naoki778928b2017-08-03 23:45:15 +09002719 Py_ssize_t ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07002720 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002721 return NULL;
INADA Naoki93f26f72016-11-02 18:45:16 +09002722
2723 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09002724 ((ix >= 0 && value == NULL && mp->ma_used != ix) ||
INADA Naoki93f26f72016-11-02 18:45:16 +09002725 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
2726 if (insertion_resize(mp) < 0) {
2727 return NULL;
2728 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002729 ix = DKIX_EMPTY;
2730 }
2731
2732 if (ix == DKIX_EMPTY) {
2733 PyDictKeyEntry *ep, *ep0;
2734 value = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002735 if (mp->ma_keys->dk_usable <= 0) {
Victor Stinner3c336c52016-09-12 14:17:40 +02002736 if (insertion_resize(mp) < 0) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002737 return NULL;
Victor Stinner3c336c52016-09-12 14:17:40 +02002738 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002739 }
INADA Naoki778928b2017-08-03 23:45:15 +09002740 Py_ssize_t hashpos = find_empty_slot(mp->ma_keys, hash);
INADA Naoki93f26f72016-11-02 18:45:16 +09002741 ep0 = DK_ENTRIES(mp->ma_keys);
2742 ep = &ep0[mp->ma_keys->dk_nentries];
2743 dk_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002744 Py_INCREF(key);
INADA Naoki93f26f72016-11-02 18:45:16 +09002745 Py_INCREF(value);
2746 MAINTAIN_TRACKING(mp, key, value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002747 ep->me_key = key;
2748 ep->me_hash = hash;
INADA Naokiba609772016-12-07 20:41:42 +09002749 if (_PyDict_HasSplitTable(mp)) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002750 assert(mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
2751 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Victor Stinner742da042016-09-07 17:40:12 -07002752 }
2753 else {
INADA Naoki93f26f72016-11-02 18:45:16 +09002754 ep->me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07002755 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002756 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002757 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naoki93f26f72016-11-02 18:45:16 +09002758 mp->ma_keys->dk_usable--;
2759 mp->ma_keys->dk_nentries++;
2760 assert(mp->ma_keys->dk_usable >= 0);
2761 }
INADA Naokiba609772016-12-07 20:41:42 +09002762 else if (value == NULL) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002763 value = defaultobj;
2764 assert(_PyDict_HasSplitTable(mp));
2765 assert(ix == mp->ma_used);
2766 Py_INCREF(value);
2767 MAINTAIN_TRACKING(mp, key, value);
INADA Naokiba609772016-12-07 20:41:42 +09002768 mp->ma_values[ix] = value;
INADA Naoki93f26f72016-11-02 18:45:16 +09002769 mp->ma_used++;
2770 mp->ma_version_tag = DICT_NEXT_VERSION();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002771 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002772
2773 assert(_PyDict_CheckConsistency(mp));
2774 return value;
Guido van Rossum164452c2000-08-08 16:12:54 +00002775}
2776
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002777/*[clinic input]
2778dict.setdefault
2779
2780 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002781 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002782 /
2783
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002784Insert key with a value of default if key is not in the dictionary.
2785
2786Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002787[clinic start generated code]*/
2788
Benjamin Peterson00e98862013-03-07 22:16:29 -05002789static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002790dict_setdefault_impl(PyDictObject *self, PyObject *key,
2791 PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002792/*[clinic end generated code: output=f8c1101ebf69e220 input=0f063756e815fd9d]*/
Benjamin Peterson00e98862013-03-07 22:16:29 -05002793{
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002794 PyObject *val;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002795
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002796 val = PyDict_SetDefault((PyObject *)self, key, default_value);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002797 Py_XINCREF(val);
2798 return val;
2799}
Guido van Rossum164452c2000-08-08 16:12:54 +00002800
2801static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002802dict_clear(PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002803{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002804 PyDict_Clear((PyObject *)mp);
2805 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002806}
2807
Guido van Rossumba6ab842000-12-12 22:02:18 +00002808static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002809dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002810{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002811 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00002812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002813 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2814 return NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06002815
Yury Selivanov684ef2c2016-10-28 19:01:21 -04002816 return _PyDict_Pop((PyObject*)mp, key, deflt);
Guido van Rossume027d982002-04-12 15:11:59 +00002817}
2818
2819static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002820dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002821{
Victor Stinner742da042016-09-07 17:40:12 -07002822 Py_ssize_t i, j;
2823 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002824 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002826 /* Allocate the result tuple before checking the size. Believe it
2827 * or not, this allocation could trigger a garbage collection which
2828 * could empty the dict, so if we checked the size first and that
2829 * happened, the result would be an infinite loop (searching for an
2830 * entry that no longer exists). Note that the usual popitem()
2831 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2832 * tuple away if the dict *is* empty isn't a significant
2833 * inefficiency -- possible, but unlikely in practice.
2834 */
2835 res = PyTuple_New(2);
2836 if (res == NULL)
2837 return NULL;
2838 if (mp->ma_used == 0) {
2839 Py_DECREF(res);
2840 PyErr_SetString(PyExc_KeyError,
2841 "popitem(): dictionary is empty");
2842 return NULL;
2843 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002844 /* Convert split table to combined table */
2845 if (mp->ma_keys->dk_lookup == lookdict_split) {
2846 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2847 Py_DECREF(res);
2848 return NULL;
2849 }
2850 }
2851 ENSURE_ALLOWS_DELETIONS(mp);
Victor Stinner742da042016-09-07 17:40:12 -07002852
2853 /* Pop last item */
2854 ep0 = DK_ENTRIES(mp->ma_keys);
2855 i = mp->ma_keys->dk_nentries - 1;
2856 while (i >= 0 && ep0[i].me_value == NULL) {
2857 i--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002858 }
Victor Stinner742da042016-09-07 17:40:12 -07002859 assert(i >= 0);
2860
2861 ep = &ep0[i];
2862 j = lookdict_index(mp->ma_keys, ep->me_hash, i);
2863 assert(j >= 0);
2864 assert(dk_get_index(mp->ma_keys, j) == i);
2865 dk_set_index(mp->ma_keys, j, DKIX_DUMMY);
2866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002867 PyTuple_SET_ITEM(res, 0, ep->me_key);
2868 PyTuple_SET_ITEM(res, 1, ep->me_value);
Victor Stinner742da042016-09-07 17:40:12 -07002869 ep->me_key = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 ep->me_value = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002871 /* We can't dk_usable++ since there is DKIX_DUMMY in indices */
2872 mp->ma_keys->dk_nentries = i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002873 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002874 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02002875 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002877}
2878
Jeremy Hylton8caad492000-06-23 14:18:11 +00002879static int
2880dict_traverse(PyObject *op, visitproc visit, void *arg)
2881{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002882 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson55f44522016-09-05 12:12:59 -07002883 PyDictKeysObject *keys = mp->ma_keys;
Serhiy Storchaka46825d22016-09-26 21:29:34 +03002884 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Victor Stinner742da042016-09-07 17:40:12 -07002885 Py_ssize_t i, n = keys->dk_nentries;
2886
Benjamin Peterson55f44522016-09-05 12:12:59 -07002887 if (keys->dk_lookup == lookdict) {
2888 for (i = 0; i < n; i++) {
2889 if (entries[i].me_value != NULL) {
2890 Py_VISIT(entries[i].me_value);
2891 Py_VISIT(entries[i].me_key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002892 }
2893 }
Victor Stinner742da042016-09-07 17:40:12 -07002894 }
2895 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002896 if (mp->ma_values != NULL) {
Benjamin Peterson55f44522016-09-05 12:12:59 -07002897 for (i = 0; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002898 Py_VISIT(mp->ma_values[i]);
2899 }
2900 }
2901 else {
Benjamin Peterson55f44522016-09-05 12:12:59 -07002902 for (i = 0; i < n; i++) {
2903 Py_VISIT(entries[i].me_value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002904 }
2905 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002906 }
2907 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002908}
2909
2910static int
2911dict_tp_clear(PyObject *op)
2912{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 PyDict_Clear(op);
2914 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002915}
2916
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002917static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002918
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002919Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06002920_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002921{
Victor Stinner742da042016-09-07 17:40:12 -07002922 Py_ssize_t size, usable, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002923
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002924 size = DK_SIZE(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002925 usable = USABLE_FRACTION(size);
2926
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002927 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002928 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -07002929 res += usable * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002930 /* If the dictionary is split, the keys portion is accounted-for
2931 in the type object. */
2932 if (mp->ma_keys->dk_refcnt == 1)
Victor Stinner98ee9d52016-09-08 09:33:56 -07002933 res += (sizeof(PyDictKeysObject)
2934 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
2935 + DK_IXSIZE(mp->ma_keys) * size
2936 + sizeof(PyDictKeyEntry) * usable);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002937 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002938}
2939
2940Py_ssize_t
2941_PyDict_KeysSize(PyDictKeysObject *keys)
2942{
Victor Stinner98ee9d52016-09-08 09:33:56 -07002943 return (sizeof(PyDictKeysObject)
2944 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
2945 + DK_IXSIZE(keys) * DK_SIZE(keys)
2946 + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002947}
2948
doko@ubuntu.com17210f52016-01-14 14:04:59 +01002949static PyObject *
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002950dict_sizeof(PyDictObject *mp)
2951{
2952 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
2953}
2954
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002955PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
2956
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002957PyDoc_STRVAR(sizeof__doc__,
2958"D.__sizeof__() -> size of D in memory, in bytes");
2959
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002960PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002961"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002962If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00002963
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002964PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00002965"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000029662-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002967
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002968PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04002969"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
2970If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
2971If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
2972In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00002973
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002974PyDoc_STRVAR(clear__doc__,
2975"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002976
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002977PyDoc_STRVAR(copy__doc__,
2978"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00002979
Guido van Rossumb90c8482007-02-10 01:11:45 +00002980/* Forward */
2981static PyObject *dictkeys_new(PyObject *);
2982static PyObject *dictitems_new(PyObject *);
2983static PyObject *dictvalues_new(PyObject *);
2984
Guido van Rossum45c85d12007-07-27 16:31:40 +00002985PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002986 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002987PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002988 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002989PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002990 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00002991
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002992static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07002993 DICT___CONTAINS___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002994 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
2995 getitem__doc__},
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002996 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002997 sizeof__doc__},
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002998 DICT_GET_METHODDEF
2999 DICT_SETDEFAULT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003000 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
3001 pop__doc__},
3002 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
3003 popitem__doc__},
3004 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
3005 keys__doc__},
3006 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
3007 items__doc__},
3008 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
3009 values__doc__},
3010 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
3011 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08003012 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003013 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
3014 clear__doc__},
3015 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
3016 copy__doc__},
3017 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003018};
3019
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003020/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00003021int
3022PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003023{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003024 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07003025 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003026 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003027 PyObject *value;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003029 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003030 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003031 hash = PyObject_Hash(key);
3032 if (hash == -1)
3033 return -1;
3034 }
INADA Naoki778928b2017-08-03 23:45:15 +09003035 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003036 if (ix == DKIX_ERROR)
3037 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003038 return (ix != DKIX_EMPTY && value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003039}
3040
Thomas Wouterscf297e42007-02-23 15:07:44 +00003041/* Internal version of PyDict_Contains used when the hash value is already known */
3042int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003043_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00003044{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003045 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003046 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07003047 Py_ssize_t ix;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003048
INADA Naoki778928b2017-08-03 23:45:15 +09003049 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value);
Victor Stinner742da042016-09-07 17:40:12 -07003050 if (ix == DKIX_ERROR)
3051 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003052 return (ix != DKIX_EMPTY && value != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00003053}
3054
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003055/* Hack to implement "key in dict" */
3056static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003057 0, /* sq_length */
3058 0, /* sq_concat */
3059 0, /* sq_repeat */
3060 0, /* sq_item */
3061 0, /* sq_slice */
3062 0, /* sq_ass_item */
3063 0, /* sq_ass_slice */
3064 PyDict_Contains, /* sq_contains */
3065 0, /* sq_inplace_concat */
3066 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003067};
3068
Guido van Rossum09e563a2001-05-01 12:10:21 +00003069static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003070dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3071{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003072 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003073 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003075 assert(type != NULL && type->tp_alloc != NULL);
3076 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02003077 if (self == NULL)
3078 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003079 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003080
Victor Stinnera9f61a52013-07-16 22:17:26 +02003081 /* The object has been implicitly tracked by tp_alloc */
3082 if (type == &PyDict_Type)
3083 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003084
3085 d->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003086 d->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07003087 d->ma_keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003088 if (d->ma_keys == NULL) {
3089 Py_DECREF(self);
3090 return NULL;
3091 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02003092 assert(_PyDict_CheckConsistency(d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003093 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003094}
3095
Tim Peters25786c02001-09-02 08:22:48 +00003096static int
3097dict_init(PyObject *self, PyObject *args, PyObject *kwds)
3098{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003099 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00003100}
3101
Tim Peters6d6c1a32001-08-02 04:15:00 +00003102static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003103dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00003104{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003105 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003106}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003107
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003108PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00003109"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00003110"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003111" (key, value) pairs\n"
3112"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00003113" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003114" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00003115" d[k] = v\n"
3116"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
3117" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00003118
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003119PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003120 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3121 "dict",
3122 sizeof(PyDictObject),
3123 0,
3124 (destructor)dict_dealloc, /* tp_dealloc */
3125 0, /* tp_print */
3126 0, /* tp_getattr */
3127 0, /* tp_setattr */
3128 0, /* tp_reserved */
3129 (reprfunc)dict_repr, /* tp_repr */
3130 0, /* tp_as_number */
3131 &dict_as_sequence, /* tp_as_sequence */
3132 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003133 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003134 0, /* tp_call */
3135 0, /* tp_str */
3136 PyObject_GenericGetAttr, /* tp_getattro */
3137 0, /* tp_setattro */
3138 0, /* tp_as_buffer */
3139 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3140 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
3141 dictionary_doc, /* tp_doc */
3142 dict_traverse, /* tp_traverse */
3143 dict_tp_clear, /* tp_clear */
3144 dict_richcompare, /* tp_richcompare */
3145 0, /* tp_weaklistoffset */
3146 (getiterfunc)dict_iter, /* tp_iter */
3147 0, /* tp_iternext */
3148 mapp_methods, /* tp_methods */
3149 0, /* tp_members */
3150 0, /* tp_getset */
3151 0, /* tp_base */
3152 0, /* tp_dict */
3153 0, /* tp_descr_get */
3154 0, /* tp_descr_set */
3155 0, /* tp_dictoffset */
3156 dict_init, /* tp_init */
3157 PyType_GenericAlloc, /* tp_alloc */
3158 dict_new, /* tp_new */
3159 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003160};
3161
Victor Stinner3c1e4812012-03-26 22:10:51 +02003162PyObject *
3163_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
3164{
3165 PyObject *kv;
3166 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02003167 if (kv == NULL) {
3168 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003169 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02003170 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02003171 return PyDict_GetItem(dp, kv);
3172}
3173
Guido van Rossum3cca2451997-05-16 14:23:33 +00003174/* For backward compatibility with old dictionary interface */
3175
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003176PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003177PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003178{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003179 PyObject *kv, *rv;
3180 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003181 if (kv == NULL) {
3182 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003183 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003184 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003185 rv = PyDict_GetItem(v, kv);
3186 Py_DECREF(kv);
3187 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003188}
3189
3190int
Victor Stinner3c1e4812012-03-26 22:10:51 +02003191_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
3192{
3193 PyObject *kv;
3194 kv = _PyUnicode_FromId(key); /* borrowed */
3195 if (kv == NULL)
3196 return -1;
3197 return PyDict_SetItem(v, kv, item);
3198}
3199
3200int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003201PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003202{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003203 PyObject *kv;
3204 int err;
3205 kv = PyUnicode_FromString(key);
3206 if (kv == NULL)
3207 return -1;
3208 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
3209 err = PyDict_SetItem(v, kv, item);
3210 Py_DECREF(kv);
3211 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003212}
3213
3214int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01003215_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
3216{
3217 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3218 if (kv == NULL)
3219 return -1;
3220 return PyDict_DelItem(v, kv);
3221}
3222
3223int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003224PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003226 PyObject *kv;
3227 int err;
3228 kv = PyUnicode_FromString(key);
3229 if (kv == NULL)
3230 return -1;
3231 err = PyDict_DelItem(v, kv);
3232 Py_DECREF(kv);
3233 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003234}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003235
Raymond Hettinger019a1482004-03-18 02:41:19 +00003236/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003237
3238typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003239 PyObject_HEAD
3240 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
3241 Py_ssize_t di_used;
3242 Py_ssize_t di_pos;
3243 PyObject* di_result; /* reusable result tuple for iteritems */
3244 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003245} dictiterobject;
3246
3247static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003248dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003249{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003250 dictiterobject *di;
3251 di = PyObject_GC_New(dictiterobject, itertype);
3252 if (di == NULL)
3253 return NULL;
3254 Py_INCREF(dict);
3255 di->di_dict = dict;
3256 di->di_used = dict->ma_used;
3257 di->di_pos = 0;
3258 di->len = dict->ma_used;
3259 if (itertype == &PyDictIterItem_Type) {
3260 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
3261 if (di->di_result == NULL) {
3262 Py_DECREF(di);
3263 return NULL;
3264 }
3265 }
3266 else
3267 di->di_result = NULL;
3268 _PyObject_GC_TRACK(di);
3269 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003270}
3271
3272static void
3273dictiter_dealloc(dictiterobject *di)
3274{
INADA Naokia6296d32017-08-24 14:55:17 +09003275 /* bpo-31095: UnTrack is needed before calling any callbacks */
3276 _PyObject_GC_UNTRACK(di);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003277 Py_XDECREF(di->di_dict);
3278 Py_XDECREF(di->di_result);
3279 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003280}
3281
3282static int
3283dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
3284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003285 Py_VISIT(di->di_dict);
3286 Py_VISIT(di->di_result);
3287 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003288}
3289
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003290static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003291dictiter_len(dictiterobject *di)
3292{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003293 Py_ssize_t len = 0;
3294 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
3295 len = di->len;
3296 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003297}
3298
Guido van Rossumb90c8482007-02-10 01:11:45 +00003299PyDoc_STRVAR(length_hint_doc,
3300 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003301
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003302static PyObject *
3303dictiter_reduce(dictiterobject *di);
3304
3305PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3306
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003307static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003308 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
3309 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003310 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
3311 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003312 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003313};
3314
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003315static PyObject*
3316dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00003317{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003318 PyObject *key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003319 Py_ssize_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003320 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003321 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00003322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003323 if (d == NULL)
3324 return NULL;
3325 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00003326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003327 if (di->di_used != d->ma_used) {
3328 PyErr_SetString(PyExc_RuntimeError,
3329 "dictionary changed size during iteration");
3330 di->di_used = -1; /* Make this state sticky */
3331 return NULL;
3332 }
Guido van Rossum2147df72002-07-16 20:30:22 +00003333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003334 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003335 k = d->ma_keys;
INADA Naokica2d8be2016-11-04 16:59:10 +09003336 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003337 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003338 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003339 goto fail;
3340 key = DK_ENTRIES(k)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003341 assert(d->ma_values[i] != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003342 }
3343 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003344 Py_ssize_t n = k->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003345 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3346 while (i < n && entry_ptr->me_value == NULL) {
3347 entry_ptr++;
3348 i++;
3349 }
3350 if (i >= n)
3351 goto fail;
3352 key = entry_ptr->me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003353 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003354 di->di_pos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003355 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003356 Py_INCREF(key);
3357 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003358
3359fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003360 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003361 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003362 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003363}
3364
Raymond Hettinger019a1482004-03-18 02:41:19 +00003365PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003366 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3367 "dict_keyiterator", /* tp_name */
3368 sizeof(dictiterobject), /* tp_basicsize */
3369 0, /* tp_itemsize */
3370 /* methods */
3371 (destructor)dictiter_dealloc, /* tp_dealloc */
3372 0, /* tp_print */
3373 0, /* tp_getattr */
3374 0, /* tp_setattr */
3375 0, /* tp_reserved */
3376 0, /* tp_repr */
3377 0, /* tp_as_number */
3378 0, /* tp_as_sequence */
3379 0, /* tp_as_mapping */
3380 0, /* tp_hash */
3381 0, /* tp_call */
3382 0, /* tp_str */
3383 PyObject_GenericGetAttr, /* tp_getattro */
3384 0, /* tp_setattro */
3385 0, /* tp_as_buffer */
3386 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3387 0, /* tp_doc */
3388 (traverseproc)dictiter_traverse, /* tp_traverse */
3389 0, /* tp_clear */
3390 0, /* tp_richcompare */
3391 0, /* tp_weaklistoffset */
3392 PyObject_SelfIter, /* tp_iter */
3393 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3394 dictiter_methods, /* tp_methods */
3395 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003396};
3397
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003398static PyObject *
3399dictiter_iternextvalue(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003400{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003401 PyObject *value;
INADA Naokica2d8be2016-11-04 16:59:10 +09003402 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003403 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003405 if (d == NULL)
3406 return NULL;
3407 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003409 if (di->di_used != d->ma_used) {
3410 PyErr_SetString(PyExc_RuntimeError,
3411 "dictionary changed size during iteration");
3412 di->di_used = -1; /* Make this state sticky */
3413 return NULL;
3414 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003416 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003417 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003418 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003419 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003420 goto fail;
INADA Naokica2d8be2016-11-04 16:59:10 +09003421 value = d->ma_values[i];
3422 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003423 }
3424 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003425 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003426 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3427 while (i < n && entry_ptr->me_value == NULL) {
3428 entry_ptr++;
3429 i++;
3430 }
3431 if (i >= n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003432 goto fail;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003433 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003434 }
3435 di->di_pos = i+1;
3436 di->len--;
3437 Py_INCREF(value);
3438 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003439
3440fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003441 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003442 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003443 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003444}
3445
3446PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003447 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3448 "dict_valueiterator", /* tp_name */
3449 sizeof(dictiterobject), /* tp_basicsize */
3450 0, /* tp_itemsize */
3451 /* methods */
3452 (destructor)dictiter_dealloc, /* tp_dealloc */
3453 0, /* tp_print */
3454 0, /* tp_getattr */
3455 0, /* tp_setattr */
3456 0, /* tp_reserved */
3457 0, /* tp_repr */
3458 0, /* tp_as_number */
3459 0, /* tp_as_sequence */
3460 0, /* tp_as_mapping */
3461 0, /* tp_hash */
3462 0, /* tp_call */
3463 0, /* tp_str */
3464 PyObject_GenericGetAttr, /* tp_getattro */
3465 0, /* tp_setattro */
3466 0, /* tp_as_buffer */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003467 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003468 0, /* tp_doc */
3469 (traverseproc)dictiter_traverse, /* tp_traverse */
3470 0, /* tp_clear */
3471 0, /* tp_richcompare */
3472 0, /* tp_weaklistoffset */
3473 PyObject_SelfIter, /* tp_iter */
3474 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3475 dictiter_methods, /* tp_methods */
3476 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003477};
3478
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003479static PyObject *
3480dictiter_iternextitem(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003481{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003482 PyObject *key, *value, *result;
INADA Naokica2d8be2016-11-04 16:59:10 +09003483 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003484 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003486 if (d == NULL)
3487 return NULL;
3488 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003490 if (di->di_used != d->ma_used) {
3491 PyErr_SetString(PyExc_RuntimeError,
3492 "dictionary changed size during iteration");
3493 di->di_used = -1; /* Make this state sticky */
3494 return NULL;
3495 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003497 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003498 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003499 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003500 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003501 goto fail;
3502 key = DK_ENTRIES(d->ma_keys)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003503 value = d->ma_values[i];
3504 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003505 }
3506 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003507 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003508 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3509 while (i < n && entry_ptr->me_value == NULL) {
3510 entry_ptr++;
3511 i++;
3512 }
3513 if (i >= n)
3514 goto fail;
3515 key = entry_ptr->me_key;
3516 value = entry_ptr->me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003517 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003518 di->di_pos = i+1;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003519 di->len--;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003520 Py_INCREF(key);
3521 Py_INCREF(value);
3522 result = di->di_result;
3523 if (Py_REFCNT(result) == 1) {
3524 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3525 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3526 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3527 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003528 Py_INCREF(result);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003529 Py_DECREF(oldkey);
3530 Py_DECREF(oldvalue);
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003531 }
3532 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003533 result = PyTuple_New(2);
3534 if (result == NULL)
3535 return NULL;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003536 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3537 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003538 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003539 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003540
3541fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003542 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003543 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003544 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003545}
3546
3547PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003548 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3549 "dict_itemiterator", /* tp_name */
3550 sizeof(dictiterobject), /* tp_basicsize */
3551 0, /* tp_itemsize */
3552 /* methods */
3553 (destructor)dictiter_dealloc, /* tp_dealloc */
3554 0, /* tp_print */
3555 0, /* tp_getattr */
3556 0, /* tp_setattr */
3557 0, /* tp_reserved */
3558 0, /* tp_repr */
3559 0, /* tp_as_number */
3560 0, /* tp_as_sequence */
3561 0, /* tp_as_mapping */
3562 0, /* tp_hash */
3563 0, /* tp_call */
3564 0, /* tp_str */
3565 PyObject_GenericGetAttr, /* tp_getattro */
3566 0, /* tp_setattro */
3567 0, /* tp_as_buffer */
3568 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3569 0, /* tp_doc */
3570 (traverseproc)dictiter_traverse, /* tp_traverse */
3571 0, /* tp_clear */
3572 0, /* tp_richcompare */
3573 0, /* tp_weaklistoffset */
3574 PyObject_SelfIter, /* tp_iter */
3575 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3576 dictiter_methods, /* tp_methods */
3577 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003578};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003579
3580
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003581static PyObject *
3582dictiter_reduce(dictiterobject *di)
3583{
3584 PyObject *list;
3585 dictiterobject tmp;
3586
3587 list = PyList_New(0);
3588 if (!list)
3589 return NULL;
3590
3591 /* copy the itertor state */
3592 tmp = *di;
3593 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003594
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003595 /* iterate the temporary into a list */
3596 for(;;) {
3597 PyObject *element = 0;
3598 if (Py_TYPE(di) == &PyDictIterItem_Type)
3599 element = dictiter_iternextitem(&tmp);
3600 else if (Py_TYPE(di) == &PyDictIterKey_Type)
3601 element = dictiter_iternextkey(&tmp);
3602 else if (Py_TYPE(di) == &PyDictIterValue_Type)
3603 element = dictiter_iternextvalue(&tmp);
3604 else
3605 assert(0);
3606 if (element) {
3607 if (PyList_Append(list, element)) {
3608 Py_DECREF(element);
3609 Py_DECREF(list);
3610 Py_XDECREF(tmp.di_dict);
3611 return NULL;
3612 }
3613 Py_DECREF(element);
3614 } else
3615 break;
3616 }
3617 Py_XDECREF(tmp.di_dict);
3618 /* check for error */
3619 if (tmp.di_dict != NULL) {
3620 /* we have an error */
3621 Py_DECREF(list);
3622 return NULL;
3623 }
Antoine Pitroua7013882012-04-05 00:04:20 +02003624 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003625}
3626
Guido van Rossum3ac67412007-02-10 18:55:06 +00003627/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003628/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003629/***********************************************/
3630
Guido van Rossumb90c8482007-02-10 01:11:45 +00003631/* The instance lay-out is the same for all three; but the type differs. */
3632
Guido van Rossumb90c8482007-02-10 01:11:45 +00003633static void
Eric Snow96c6af92015-05-29 22:21:39 -06003634dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003635{
INADA Naokia6296d32017-08-24 14:55:17 +09003636 /* bpo-31095: UnTrack is needed before calling any callbacks */
3637 _PyObject_GC_UNTRACK(dv);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003638 Py_XDECREF(dv->dv_dict);
3639 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003640}
3641
3642static int
Eric Snow96c6af92015-05-29 22:21:39 -06003643dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003644{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003645 Py_VISIT(dv->dv_dict);
3646 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003647}
3648
Guido van Rossum83825ac2007-02-10 04:54:19 +00003649static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003650dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003651{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003652 Py_ssize_t len = 0;
3653 if (dv->dv_dict != NULL)
3654 len = dv->dv_dict->ma_used;
3655 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003656}
3657
Eric Snow96c6af92015-05-29 22:21:39 -06003658PyObject *
3659_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003660{
Eric Snow96c6af92015-05-29 22:21:39 -06003661 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003662 if (dict == NULL) {
3663 PyErr_BadInternalCall();
3664 return NULL;
3665 }
3666 if (!PyDict_Check(dict)) {
3667 /* XXX Get rid of this restriction later */
3668 PyErr_Format(PyExc_TypeError,
3669 "%s() requires a dict argument, not '%s'",
3670 type->tp_name, dict->ob_type->tp_name);
3671 return NULL;
3672 }
Eric Snow96c6af92015-05-29 22:21:39 -06003673 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003674 if (dv == NULL)
3675 return NULL;
3676 Py_INCREF(dict);
3677 dv->dv_dict = (PyDictObject *)dict;
3678 _PyObject_GC_TRACK(dv);
3679 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003680}
3681
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003682/* TODO(guido): The views objects are not complete:
3683
3684 * support more set operations
3685 * support arbitrary mappings?
3686 - either these should be static or exported in dictobject.h
3687 - if public then they should probably be in builtins
3688*/
3689
Guido van Rossumaac530c2007-08-24 22:33:45 +00003690/* Return 1 if self is a subset of other, iterating over self;
3691 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003692static int
3693all_contained_in(PyObject *self, PyObject *other)
3694{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003695 PyObject *iter = PyObject_GetIter(self);
3696 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003698 if (iter == NULL)
3699 return -1;
3700 for (;;) {
3701 PyObject *next = PyIter_Next(iter);
3702 if (next == NULL) {
3703 if (PyErr_Occurred())
3704 ok = -1;
3705 break;
3706 }
3707 ok = PySequence_Contains(other, next);
3708 Py_DECREF(next);
3709 if (ok <= 0)
3710 break;
3711 }
3712 Py_DECREF(iter);
3713 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003714}
3715
3716static PyObject *
3717dictview_richcompare(PyObject *self, PyObject *other, int op)
3718{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003719 Py_ssize_t len_self, len_other;
3720 int ok;
3721 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003723 assert(self != NULL);
3724 assert(PyDictViewSet_Check(self));
3725 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003726
Brian Curtindfc80e32011-08-10 20:28:54 -05003727 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3728 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003730 len_self = PyObject_Size(self);
3731 if (len_self < 0)
3732 return NULL;
3733 len_other = PyObject_Size(other);
3734 if (len_other < 0)
3735 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003737 ok = 0;
3738 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003740 case Py_NE:
3741 case Py_EQ:
3742 if (len_self == len_other)
3743 ok = all_contained_in(self, other);
3744 if (op == Py_NE && ok >= 0)
3745 ok = !ok;
3746 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003747
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003748 case Py_LT:
3749 if (len_self < len_other)
3750 ok = all_contained_in(self, other);
3751 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003753 case Py_LE:
3754 if (len_self <= len_other)
3755 ok = all_contained_in(self, other);
3756 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003757
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003758 case Py_GT:
3759 if (len_self > len_other)
3760 ok = all_contained_in(other, self);
3761 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003763 case Py_GE:
3764 if (len_self >= len_other)
3765 ok = all_contained_in(other, self);
3766 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003768 }
3769 if (ok < 0)
3770 return NULL;
3771 result = ok ? Py_True : Py_False;
3772 Py_INCREF(result);
3773 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003774}
3775
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003776static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003777dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003778{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003779 PyObject *seq;
3780 PyObject *result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003781
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003782 seq = PySequence_List((PyObject *)dv);
3783 if (seq == NULL)
3784 return NULL;
3785
3786 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
3787 Py_DECREF(seq);
3788 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003789}
3790
Guido van Rossum3ac67412007-02-10 18:55:06 +00003791/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003792
3793static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003794dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003795{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003796 if (dv->dv_dict == NULL) {
3797 Py_RETURN_NONE;
3798 }
3799 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003800}
3801
3802static int
Eric Snow96c6af92015-05-29 22:21:39 -06003803dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00003804{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003805 if (dv->dv_dict == NULL)
3806 return 0;
3807 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003808}
3809
Guido van Rossum83825ac2007-02-10 04:54:19 +00003810static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003811 (lenfunc)dictview_len, /* sq_length */
3812 0, /* sq_concat */
3813 0, /* sq_repeat */
3814 0, /* sq_item */
3815 0, /* sq_slice */
3816 0, /* sq_ass_item */
3817 0, /* sq_ass_slice */
3818 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003819};
3820
Guido van Rossum523259b2007-08-24 23:41:22 +00003821static PyObject*
3822dictviews_sub(PyObject* self, PyObject *other)
3823{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003824 PyObject *result = PySet_New(self);
3825 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003826 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003828 if (result == NULL)
3829 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003830
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003831 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003832 if (tmp == NULL) {
3833 Py_DECREF(result);
3834 return NULL;
3835 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003837 Py_DECREF(tmp);
3838 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003839}
3840
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003841PyObject*
3842_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00003843{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003844 PyObject *result = PySet_New(self);
3845 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003846 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003848 if (result == NULL)
3849 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003850
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003851 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_intersection_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003852 if (tmp == NULL) {
3853 Py_DECREF(result);
3854 return NULL;
3855 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003857 Py_DECREF(tmp);
3858 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003859}
3860
3861static PyObject*
3862dictviews_or(PyObject* self, PyObject *other)
3863{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003864 PyObject *result = PySet_New(self);
3865 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003866 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003868 if (result == NULL)
3869 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003870
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003871 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003872 if (tmp == NULL) {
3873 Py_DECREF(result);
3874 return NULL;
3875 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003876
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003877 Py_DECREF(tmp);
3878 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003879}
3880
3881static PyObject*
3882dictviews_xor(PyObject* self, PyObject *other)
3883{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003884 PyObject *result = PySet_New(self);
3885 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003886 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003888 if (result == NULL)
3889 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003890
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003891 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_symmetric_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003892 if (tmp == NULL) {
3893 Py_DECREF(result);
3894 return NULL;
3895 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003896
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003897 Py_DECREF(tmp);
3898 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003899}
3900
3901static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003902 0, /*nb_add*/
3903 (binaryfunc)dictviews_sub, /*nb_subtract*/
3904 0, /*nb_multiply*/
3905 0, /*nb_remainder*/
3906 0, /*nb_divmod*/
3907 0, /*nb_power*/
3908 0, /*nb_negative*/
3909 0, /*nb_positive*/
3910 0, /*nb_absolute*/
3911 0, /*nb_bool*/
3912 0, /*nb_invert*/
3913 0, /*nb_lshift*/
3914 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003915 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003916 (binaryfunc)dictviews_xor, /*nb_xor*/
3917 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00003918};
3919
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003920static PyObject*
3921dictviews_isdisjoint(PyObject *self, PyObject *other)
3922{
3923 PyObject *it;
3924 PyObject *item = NULL;
3925
3926 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06003927 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003928 Py_RETURN_TRUE;
3929 else
3930 Py_RETURN_FALSE;
3931 }
3932
3933 /* Iterate over the shorter object (only if other is a set,
3934 * because PySequence_Contains may be expensive otherwise): */
3935 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06003936 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003937 Py_ssize_t len_other = PyObject_Size(other);
3938 if (len_other == -1)
3939 return NULL;
3940
3941 if ((len_other > len_self)) {
3942 PyObject *tmp = other;
3943 other = self;
3944 self = tmp;
3945 }
3946 }
3947
3948 it = PyObject_GetIter(other);
3949 if (it == NULL)
3950 return NULL;
3951
3952 while ((item = PyIter_Next(it)) != NULL) {
3953 int contains = PySequence_Contains(self, item);
3954 Py_DECREF(item);
3955 if (contains == -1) {
3956 Py_DECREF(it);
3957 return NULL;
3958 }
3959
3960 if (contains) {
3961 Py_DECREF(it);
3962 Py_RETURN_FALSE;
3963 }
3964 }
3965 Py_DECREF(it);
3966 if (PyErr_Occurred())
3967 return NULL; /* PyIter_Next raised an exception. */
3968 Py_RETURN_TRUE;
3969}
3970
3971PyDoc_STRVAR(isdisjoint_doc,
3972"Return True if the view and the given iterable have a null intersection.");
3973
Guido van Rossumb90c8482007-02-10 01:11:45 +00003974static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003975 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3976 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003977 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003978};
3979
3980PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003981 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3982 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06003983 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003984 0, /* tp_itemsize */
3985 /* methods */
3986 (destructor)dictview_dealloc, /* tp_dealloc */
3987 0, /* tp_print */
3988 0, /* tp_getattr */
3989 0, /* tp_setattr */
3990 0, /* tp_reserved */
3991 (reprfunc)dictview_repr, /* tp_repr */
3992 &dictviews_as_number, /* tp_as_number */
3993 &dictkeys_as_sequence, /* tp_as_sequence */
3994 0, /* tp_as_mapping */
3995 0, /* tp_hash */
3996 0, /* tp_call */
3997 0, /* tp_str */
3998 PyObject_GenericGetAttr, /* tp_getattro */
3999 0, /* tp_setattro */
4000 0, /* tp_as_buffer */
4001 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4002 0, /* tp_doc */
4003 (traverseproc)dictview_traverse, /* tp_traverse */
4004 0, /* tp_clear */
4005 dictview_richcompare, /* tp_richcompare */
4006 0, /* tp_weaklistoffset */
4007 (getiterfunc)dictkeys_iter, /* tp_iter */
4008 0, /* tp_iternext */
4009 dictkeys_methods, /* tp_methods */
4010 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004011};
4012
4013static PyObject *
4014dictkeys_new(PyObject *dict)
4015{
Eric Snow96c6af92015-05-29 22:21:39 -06004016 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004017}
4018
Guido van Rossum3ac67412007-02-10 18:55:06 +00004019/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004020
4021static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004022dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004023{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004024 if (dv->dv_dict == NULL) {
4025 Py_RETURN_NONE;
4026 }
4027 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004028}
4029
4030static int
Eric Snow96c6af92015-05-29 22:21:39 -06004031dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004032{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004033 int result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004034 PyObject *key, *value, *found;
4035 if (dv->dv_dict == NULL)
4036 return 0;
4037 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
4038 return 0;
4039 key = PyTuple_GET_ITEM(obj, 0);
4040 value = PyTuple_GET_ITEM(obj, 1);
Raymond Hettinger6692f012016-09-18 21:46:08 -07004041 found = PyDict_GetItemWithError((PyObject *)dv->dv_dict, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004042 if (found == NULL) {
4043 if (PyErr_Occurred())
4044 return -1;
4045 return 0;
4046 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004047 Py_INCREF(found);
4048 result = PyObject_RichCompareBool(value, found, Py_EQ);
4049 Py_DECREF(found);
4050 return result;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004051}
4052
Guido van Rossum83825ac2007-02-10 04:54:19 +00004053static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004054 (lenfunc)dictview_len, /* sq_length */
4055 0, /* sq_concat */
4056 0, /* sq_repeat */
4057 0, /* sq_item */
4058 0, /* sq_slice */
4059 0, /* sq_ass_item */
4060 0, /* sq_ass_slice */
4061 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004062};
4063
Guido van Rossumb90c8482007-02-10 01:11:45 +00004064static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004065 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4066 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004067 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004068};
4069
4070PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004071 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4072 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004073 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004074 0, /* tp_itemsize */
4075 /* methods */
4076 (destructor)dictview_dealloc, /* tp_dealloc */
4077 0, /* tp_print */
4078 0, /* tp_getattr */
4079 0, /* tp_setattr */
4080 0, /* tp_reserved */
4081 (reprfunc)dictview_repr, /* tp_repr */
4082 &dictviews_as_number, /* tp_as_number */
4083 &dictitems_as_sequence, /* tp_as_sequence */
4084 0, /* tp_as_mapping */
4085 0, /* tp_hash */
4086 0, /* tp_call */
4087 0, /* tp_str */
4088 PyObject_GenericGetAttr, /* tp_getattro */
4089 0, /* tp_setattro */
4090 0, /* tp_as_buffer */
4091 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4092 0, /* tp_doc */
4093 (traverseproc)dictview_traverse, /* tp_traverse */
4094 0, /* tp_clear */
4095 dictview_richcompare, /* tp_richcompare */
4096 0, /* tp_weaklistoffset */
4097 (getiterfunc)dictitems_iter, /* tp_iter */
4098 0, /* tp_iternext */
4099 dictitems_methods, /* tp_methods */
4100 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004101};
4102
4103static PyObject *
4104dictitems_new(PyObject *dict)
4105{
Eric Snow96c6af92015-05-29 22:21:39 -06004106 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004107}
4108
Guido van Rossum3ac67412007-02-10 18:55:06 +00004109/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004110
4111static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004112dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004114 if (dv->dv_dict == NULL) {
4115 Py_RETURN_NONE;
4116 }
4117 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004118}
4119
Guido van Rossum83825ac2007-02-10 04:54:19 +00004120static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004121 (lenfunc)dictview_len, /* sq_length */
4122 0, /* sq_concat */
4123 0, /* sq_repeat */
4124 0, /* sq_item */
4125 0, /* sq_slice */
4126 0, /* sq_ass_item */
4127 0, /* sq_ass_slice */
4128 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004129};
4130
Guido van Rossumb90c8482007-02-10 01:11:45 +00004131static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004132 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004133};
4134
4135PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004136 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4137 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004138 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004139 0, /* tp_itemsize */
4140 /* methods */
4141 (destructor)dictview_dealloc, /* tp_dealloc */
4142 0, /* tp_print */
4143 0, /* tp_getattr */
4144 0, /* tp_setattr */
4145 0, /* tp_reserved */
4146 (reprfunc)dictview_repr, /* tp_repr */
4147 0, /* tp_as_number */
4148 &dictvalues_as_sequence, /* tp_as_sequence */
4149 0, /* tp_as_mapping */
4150 0, /* tp_hash */
4151 0, /* tp_call */
4152 0, /* tp_str */
4153 PyObject_GenericGetAttr, /* tp_getattro */
4154 0, /* tp_setattro */
4155 0, /* tp_as_buffer */
4156 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4157 0, /* tp_doc */
4158 (traverseproc)dictview_traverse, /* tp_traverse */
4159 0, /* tp_clear */
4160 0, /* tp_richcompare */
4161 0, /* tp_weaklistoffset */
4162 (getiterfunc)dictvalues_iter, /* tp_iter */
4163 0, /* tp_iternext */
4164 dictvalues_methods, /* tp_methods */
4165 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004166};
4167
4168static PyObject *
4169dictvalues_new(PyObject *dict)
4170{
Eric Snow96c6af92015-05-29 22:21:39 -06004171 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004172}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004173
4174/* Returns NULL if cannot allocate a new PyDictKeysObject,
4175 but does not set an error */
4176PyDictKeysObject *
4177_PyDict_NewKeysForClass(void)
4178{
Victor Stinner742da042016-09-07 17:40:12 -07004179 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004180 if (keys == NULL)
4181 PyErr_Clear();
4182 else
4183 keys->dk_lookup = lookdict_split;
4184 return keys;
4185}
4186
4187#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
4188
4189PyObject *
4190PyObject_GenericGetDict(PyObject *obj, void *context)
4191{
4192 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
4193 if (dictptr == NULL) {
4194 PyErr_SetString(PyExc_AttributeError,
4195 "This object has no __dict__");
4196 return NULL;
4197 }
4198 dict = *dictptr;
4199 if (dict == NULL) {
4200 PyTypeObject *tp = Py_TYPE(obj);
4201 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
4202 DK_INCREF(CACHED_KEYS(tp));
4203 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
4204 }
4205 else {
4206 *dictptr = dict = PyDict_New();
4207 }
4208 }
4209 Py_XINCREF(dict);
4210 return dict;
4211}
4212
4213int
4214_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
Victor Stinner742da042016-09-07 17:40:12 -07004215 PyObject *key, PyObject *value)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004216{
4217 PyObject *dict;
4218 int res;
4219 PyDictKeysObject *cached;
4220
4221 assert(dictptr != NULL);
4222 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
4223 assert(dictptr != NULL);
4224 dict = *dictptr;
4225 if (dict == NULL) {
4226 DK_INCREF(cached);
4227 dict = new_dict_with_shared_keys(cached);
4228 if (dict == NULL)
4229 return -1;
4230 *dictptr = dict;
4231 }
4232 if (value == NULL) {
4233 res = PyDict_DelItem(dict, key);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004234 // Since key sharing dict doesn't allow deletion, PyDict_DelItem()
4235 // always converts dict to combined form.
4236 if ((cached = CACHED_KEYS(tp)) != NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004237 CACHED_KEYS(tp) = NULL;
4238 DK_DECREF(cached);
4239 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01004240 }
4241 else {
INADA Naoki2294f3a2017-02-12 13:51:30 +09004242 int was_shared = (cached == ((PyDictObject *)dict)->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004243 res = PyDict_SetItem(dict, key, value);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004244 if (was_shared &&
4245 (cached = CACHED_KEYS(tp)) != NULL &&
4246 cached != ((PyDictObject *)dict)->ma_keys) {
Victor Stinner3d3f2642016-12-15 17:21:23 +01004247 /* PyDict_SetItem() may call dictresize and convert split table
4248 * into combined table. In such case, convert it to split
4249 * table again and update type's shared key only when this is
4250 * the only dict sharing key with the type.
4251 *
4252 * This is to allow using shared key in class like this:
4253 *
4254 * class C:
4255 * def __init__(self):
4256 * # one dict resize happens
4257 * self.a, self.b, self.c = 1, 2, 3
4258 * self.d, self.e, self.f = 4, 5, 6
4259 * a = C()
4260 */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004261 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004262 CACHED_KEYS(tp) = make_keys_shared(dict);
Victor Stinner742da042016-09-07 17:40:12 -07004263 }
4264 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004265 CACHED_KEYS(tp) = NULL;
4266 }
4267 DK_DECREF(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004268 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
4269 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004270 }
4271 }
4272 } else {
4273 dict = *dictptr;
4274 if (dict == NULL) {
4275 dict = PyDict_New();
4276 if (dict == NULL)
4277 return -1;
4278 *dictptr = dict;
4279 }
4280 if (value == NULL) {
4281 res = PyDict_DelItem(dict, key);
4282 } else {
4283 res = PyDict_SetItem(dict, key, value);
4284 }
4285 }
4286 return res;
4287}
4288
4289void
4290_PyDictKeys_DecRef(PyDictKeysObject *keys)
4291{
4292 DK_DECREF(keys);
4293}