blob: b74941ac2973f29ec70c475eaf801561d5d982c9 [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,
226 Py_hash_t hash, PyObject ***value_addr,
227 Py_ssize_t *hashpos);
228static Py_ssize_t lookdict_unicode(PyDictObject *mp, PyObject *key,
229 Py_hash_t hash, PyObject ***value_addr,
230 Py_ssize_t *hashpos);
231static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400232lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
Victor Stinner742da042016-09-07 17:40:12 -0700233 Py_hash_t hash, PyObject ***value_addr,
234 Py_ssize_t *hashpos);
235static Py_ssize_t lookdict_split(PyDictObject *mp, PyObject *key,
236 Py_hash_t hash, PyObject ***value_addr,
237 Py_ssize_t *hashpos);
Fred Drake1bff34a2000-08-31 19:31:38 +0000238
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400239static int dictresize(PyDictObject *mp, Py_ssize_t minused);
Tim Petersdea48ec2001-05-22 20:40:22 +0000240
Benjamin Peterson3c569292016-09-08 13:16:41 -0700241/*Global counter used to set ma_version_tag field of dictionary.
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700242 * It is incremented each time that a dictionary is created and each
243 * time that a dictionary is modified. */
244static uint64_t pydict_global_version = 0;
245
246#define DICT_NEXT_VERSION() (++pydict_global_version)
247
Victor Stinner742da042016-09-07 17:40:12 -0700248/* Dictionary reuse scheme to save calls to malloc and free */
Christian Heimes2202f872008-02-06 14:31:34 +0000249#ifndef PyDict_MAXFREELIST
250#define PyDict_MAXFREELIST 80
251#endif
252static PyDictObject *free_list[PyDict_MAXFREELIST];
253static int numfree = 0;
Victor Stinner742da042016-09-07 17:40:12 -0700254static PyDictKeysObject *keys_free_list[PyDict_MAXFREELIST];
255static int numfreekeys = 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000256
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300257#include "clinic/dictobject.c.h"
258
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100259int
260PyDict_ClearFreeList(void)
Christian Heimes77c02eb2008-02-09 02:18:51 +0000261{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 PyDictObject *op;
Victor Stinner742da042016-09-07 17:40:12 -0700263 int ret = numfree + numfreekeys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 while (numfree) {
265 op = free_list[--numfree];
266 assert(PyDict_CheckExact(op));
267 PyObject_GC_Del(op);
268 }
Victor Stinner742da042016-09-07 17:40:12 -0700269 while (numfreekeys) {
270 PyObject_FREE(keys_free_list[--numfreekeys]);
271 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100272 return ret;
273}
274
David Malcolm49526f42012-06-22 14:55:41 -0400275/* Print summary info about the state of the optimized allocator */
276void
277_PyDict_DebugMallocStats(FILE *out)
278{
279 _PyDebugAllocatorStats(out,
280 "free PyDictObject", numfree, sizeof(PyDictObject));
281}
282
283
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100284void
285PyDict_Fini(void)
286{
287 PyDict_ClearFreeList();
Christian Heimes77c02eb2008-02-09 02:18:51 +0000288}
289
Victor Stinner742da042016-09-07 17:40:12 -0700290#define DK_SIZE(dk) ((dk)->dk_size)
291#if SIZEOF_VOID_P > 4
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700292#define DK_IXSIZE(dk) \
293 (DK_SIZE(dk) <= 0xff ? \
294 1 : DK_SIZE(dk) <= 0xffff ? \
295 2 : DK_SIZE(dk) <= 0xffffffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700296 4 : sizeof(int64_t))
Victor Stinner742da042016-09-07 17:40:12 -0700297#else
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700298#define DK_IXSIZE(dk) \
299 (DK_SIZE(dk) <= 0xff ? \
300 1 : DK_SIZE(dk) <= 0xffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700301 2 : sizeof(int32_t))
Victor Stinner742da042016-09-07 17:40:12 -0700302#endif
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700303#define DK_ENTRIES(dk) \
Benjamin Peterson186122e2016-09-08 12:20:12 -0700304 ((PyDictKeyEntry*)(&(dk)->dk_indices.as_1[DK_SIZE(dk) * DK_IXSIZE(dk)]))
Victor Stinner742da042016-09-07 17:40:12 -0700305
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200306#define DK_DEBUG_INCREF _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA
307#define DK_DEBUG_DECREF _Py_DEC_REFTOTAL _Py_REF_DEBUG_COMMA
308
309#define DK_INCREF(dk) (DK_DEBUG_INCREF ++(dk)->dk_refcnt)
310#define DK_DECREF(dk) if (DK_DEBUG_DECREF (--(dk)->dk_refcnt) == 0) free_keys_object(dk)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400311#define DK_MASK(dk) (((dk)->dk_size)-1)
312#define IS_POWER_OF_2(x) (((x) & (x-1)) == 0)
313
Victor Stinner742da042016-09-07 17:40:12 -0700314/* lookup indices. returns DKIX_EMPTY, DKIX_DUMMY, or ix >=0 */
Benjamin Peterson73222252016-09-08 09:58:47 -0700315static inline Py_ssize_t
Victor Stinner742da042016-09-07 17:40:12 -0700316dk_get_index(PyDictKeysObject *keys, Py_ssize_t i)
317{
318 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700319 Py_ssize_t ix;
320
Victor Stinner742da042016-09-07 17:40:12 -0700321 if (s <= 0xff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700322 int8_t *indices = keys->dk_indices.as_1;
Victor Stinner208857e2016-09-08 11:35:46 -0700323 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700324 }
325 else if (s <= 0xffff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700326 int16_t *indices = keys->dk_indices.as_2;
Victor Stinner208857e2016-09-08 11:35:46 -0700327 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700328 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700329#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300330 else if (s > 0xffffffff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700331 int64_t *indices = keys->dk_indices.as_8;
Victor Stinner208857e2016-09-08 11:35:46 -0700332 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700333 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700334#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300335 else {
336 int32_t *indices = keys->dk_indices.as_4;
337 ix = indices[i];
338 }
Victor Stinner71211e32016-09-08 10:52:46 -0700339 assert(ix >= DKIX_DUMMY);
340 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700341}
342
343/* write to indices. */
Benjamin Peterson73222252016-09-08 09:58:47 -0700344static inline void
Victor Stinner742da042016-09-07 17:40:12 -0700345dk_set_index(PyDictKeysObject *keys, Py_ssize_t i, Py_ssize_t ix)
346{
347 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700348
349 assert(ix >= DKIX_DUMMY);
350
Victor Stinner742da042016-09-07 17:40:12 -0700351 if (s <= 0xff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700352 int8_t *indices = keys->dk_indices.as_1;
Victor Stinner71211e32016-09-08 10:52:46 -0700353 assert(ix <= 0x7f);
Victor Stinner208857e2016-09-08 11:35:46 -0700354 indices[i] = (char)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700355 }
356 else if (s <= 0xffff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700357 int16_t *indices = keys->dk_indices.as_2;
Victor Stinner71211e32016-09-08 10:52:46 -0700358 assert(ix <= 0x7fff);
Victor Stinner208857e2016-09-08 11:35:46 -0700359 indices[i] = (int16_t)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700360 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700361#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300362 else if (s > 0xffffffff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700363 int64_t *indices = keys->dk_indices.as_8;
Victor Stinner208857e2016-09-08 11:35:46 -0700364 indices[i] = ix;
Victor Stinner742da042016-09-07 17:40:12 -0700365 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700366#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300367 else {
368 int32_t *indices = keys->dk_indices.as_4;
369 assert(ix <= 0x7fffffff);
370 indices[i] = (int32_t)ix;
371 }
Victor Stinner742da042016-09-07 17:40:12 -0700372}
373
374
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200375/* USABLE_FRACTION is the maximum dictionary load.
Victor Stinner742da042016-09-07 17:40:12 -0700376 * Increasing this ratio makes dictionaries more dense resulting in more
377 * collisions. Decreasing it improves sparseness at the expense of spreading
378 * indices over more cache lines and at the cost of total memory consumed.
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200379 *
380 * USABLE_FRACTION must obey the following:
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400381 * (0 < USABLE_FRACTION(n) < n) for all n >= 2
382 *
Victor Stinner742da042016-09-07 17:40:12 -0700383 * USABLE_FRACTION should be quick to calculate.
384 * Fractions around 1/2 to 2/3 seem to work well in practice.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400385 */
Victor Stinner742da042016-09-07 17:40:12 -0700386#define USABLE_FRACTION(n) (((n) << 1)/3)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400387
Victor Stinner742da042016-09-07 17:40:12 -0700388/* ESTIMATE_SIZE is reverse function of USABLE_FRACTION.
389 * This can be used to reserve enough size to insert n entries without
390 * resizing.
391 */
INADA Naoki92c50ee2016-11-22 00:57:02 +0900392#define ESTIMATE_SIZE(n) (((n)*3+1) >> 1)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400393
Victor Stinner742da042016-09-07 17:40:12 -0700394/* Alternative fraction that is otherwise close enough to 2n/3 to make
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400395 * little difference. 8 * 2/3 == 8 * 5/8 == 5. 16 * 2/3 == 16 * 5/8 == 10.
396 * 32 * 2/3 = 21, 32 * 5/8 = 20.
397 * Its advantage is that it is faster to compute on machines with slow division.
398 * #define USABLE_FRACTION(n) (((n) >> 1) + ((n) >> 2) - ((n) >> 3))
Victor Stinner742da042016-09-07 17:40:12 -0700399 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400400
Victor Stinnera9f61a52013-07-16 22:17:26 +0200401/* GROWTH_RATE. Growth rate upon hitting maximum load.
402 * Currently set to used*2 + capacity/2.
403 * This means that dicts double in size when growing without deletions,
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700404 * but have more head room when the number of deletions is on a par with the
405 * number of insertions.
406 * Raising this to used*4 doubles memory consumption depending on the size of
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200407 * the dictionary, but results in half the number of resizes, less effort to
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700408 * resize.
409 * GROWTH_RATE was set to used*4 up to version 3.2.
410 * GROWTH_RATE was set to used*2 in version 3.3.0
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200411 */
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700412#define GROWTH_RATE(d) (((d)->ma_used*2)+((d)->ma_keys->dk_size>>1))
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400413
414#define ENSURE_ALLOWS_DELETIONS(d) \
415 if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \
416 (d)->ma_keys->dk_lookup = lookdict_unicode; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400418
419/* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
420 * (which cannot fail and thus can do no allocation).
421 */
422static PyDictKeysObject empty_keys_struct = {
Serhiy Storchaka97932e42016-09-26 23:01:23 +0300423 1, /* dk_refcnt */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400424 1, /* dk_size */
425 lookdict_split, /* dk_lookup */
426 0, /* dk_usable (immutable) */
Victor Stinner742da042016-09-07 17:40:12 -0700427 0, /* dk_nentries */
Benjamin Peterson186122e2016-09-08 12:20:12 -0700428 .dk_indices = { .as_1 = {DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY,
429 DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY}},
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400430};
431
432static PyObject *empty_values[1] = { NULL };
433
434#define Py_EMPTY_KEYS &empty_keys_struct
435
Victor Stinner611b0fa2016-09-14 15:02:01 +0200436/* Uncomment to check the dict content in _PyDict_CheckConsistency() */
437/* #define DEBUG_PYDICT */
438
439
440#ifdef Py_DEBUG
441static int
442_PyDict_CheckConsistency(PyDictObject *mp)
443{
444 PyDictKeysObject *keys = mp->ma_keys;
445 int splitted = _PyDict_HasSplitTable(mp);
446 Py_ssize_t usable = USABLE_FRACTION(keys->dk_size);
447#ifdef DEBUG_PYDICT
448 PyDictKeyEntry *entries = DK_ENTRIES(keys);
449 Py_ssize_t i;
450#endif
451
452 assert(0 <= mp->ma_used && mp->ma_used <= usable);
453 assert(IS_POWER_OF_2(keys->dk_size));
454 assert(0 <= keys->dk_usable
455 && keys->dk_usable <= usable);
456 assert(0 <= keys->dk_nentries
457 && keys->dk_nentries <= usable);
458 assert(keys->dk_usable + keys->dk_nentries <= usable);
459
460 if (!splitted) {
461 /* combined table */
462 assert(keys->dk_refcnt == 1);
463 }
464
465#ifdef DEBUG_PYDICT
466 for (i=0; i < keys->dk_size; i++) {
467 Py_ssize_t ix = dk_get_index(keys, i);
468 assert(DKIX_DUMMY <= ix && ix <= usable);
469 }
470
471 for (i=0; i < usable; i++) {
472 PyDictKeyEntry *entry = &entries[i];
473 PyObject *key = entry->me_key;
474
475 if (key != NULL) {
476 if (PyUnicode_CheckExact(key)) {
477 Py_hash_t hash = ((PyASCIIObject *)key)->hash;
478 assert(hash != -1);
479 assert(entry->me_hash == hash);
480 }
481 else {
482 /* test_dict fails if PyObject_Hash() is called again */
483 assert(entry->me_hash != -1);
484 }
485 if (!splitted) {
486 assert(entry->me_value != NULL);
487 }
488 }
489
490 if (splitted) {
491 assert(entry->me_value == NULL);
492 }
493 }
494
495 if (splitted) {
496 /* splitted table */
497 for (i=0; i < mp->ma_used; i++) {
498 assert(mp->ma_values[i] != NULL);
499 }
500 }
501#endif
502
503 return 1;
504}
505#endif
506
507
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400508static PyDictKeysObject *new_keys_object(Py_ssize_t size)
509{
510 PyDictKeysObject *dk;
Victor Stinner742da042016-09-07 17:40:12 -0700511 Py_ssize_t es, usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400512
Victor Stinner742da042016-09-07 17:40:12 -0700513 assert(size >= PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400514 assert(IS_POWER_OF_2(size));
Victor Stinner742da042016-09-07 17:40:12 -0700515
516 usable = USABLE_FRACTION(size);
517 if (size <= 0xff) {
518 es = 1;
519 }
520 else if (size <= 0xffff) {
521 es = 2;
522 }
523#if SIZEOF_VOID_P > 4
524 else if (size <= 0xffffffff) {
525 es = 4;
526 }
527#endif
528 else {
529 es = sizeof(Py_ssize_t);
530 }
531
532 if (size == PyDict_MINSIZE && numfreekeys > 0) {
533 dk = keys_free_list[--numfreekeys];
534 }
535 else {
Victor Stinner98ee9d52016-09-08 09:33:56 -0700536 dk = PyObject_MALLOC(sizeof(PyDictKeysObject)
537 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
538 + es * size
539 + sizeof(PyDictKeyEntry) * usable);
Victor Stinner742da042016-09-07 17:40:12 -0700540 if (dk == NULL) {
541 PyErr_NoMemory();
542 return NULL;
543 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400544 }
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200545 DK_DEBUG_INCREF dk->dk_refcnt = 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400546 dk->dk_size = size;
Victor Stinner742da042016-09-07 17:40:12 -0700547 dk->dk_usable = usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400548 dk->dk_lookup = lookdict_unicode_nodummy;
Victor Stinner742da042016-09-07 17:40:12 -0700549 dk->dk_nentries = 0;
Benjamin Peterson186122e2016-09-08 12:20:12 -0700550 memset(&dk->dk_indices.as_1[0], 0xff, es * size);
Victor Stinner742da042016-09-07 17:40:12 -0700551 memset(DK_ENTRIES(dk), 0, sizeof(PyDictKeyEntry) * usable);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400552 return dk;
553}
554
555static void
556free_keys_object(PyDictKeysObject *keys)
557{
Victor Stinner742da042016-09-07 17:40:12 -0700558 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400559 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -0700560 for (i = 0, n = keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400561 Py_XDECREF(entries[i].me_key);
562 Py_XDECREF(entries[i].me_value);
563 }
Victor Stinner742da042016-09-07 17:40:12 -0700564 if (keys->dk_size == PyDict_MINSIZE && numfreekeys < PyDict_MAXFREELIST) {
565 keys_free_list[numfreekeys++] = keys;
566 return;
567 }
Raymond Hettingerce5179f2016-01-31 08:56:21 -0800568 PyObject_FREE(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400569}
570
571#define new_values(size) PyMem_NEW(PyObject *, size)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400572#define free_values(values) PyMem_FREE(values)
573
574/* Consumes a reference to the keys object */
575static PyObject *
576new_dict(PyDictKeysObject *keys, PyObject **values)
577{
578 PyDictObject *mp;
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200579 assert(keys != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 if (numfree) {
581 mp = free_list[--numfree];
582 assert (mp != NULL);
583 assert (Py_TYPE(mp) == &PyDict_Type);
584 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400586 else {
587 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
588 if (mp == NULL) {
589 DK_DECREF(keys);
590 free_values(values);
591 return NULL;
592 }
593 }
594 mp->ma_keys = keys;
595 mp->ma_values = values;
596 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700597 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +0200598 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000600}
601
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400602/* Consumes a reference to the keys object */
603static PyObject *
604new_dict_with_shared_keys(PyDictKeysObject *keys)
605{
606 PyObject **values;
607 Py_ssize_t i, size;
608
Victor Stinner742da042016-09-07 17:40:12 -0700609 size = USABLE_FRACTION(DK_SIZE(keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400610 values = new_values(size);
611 if (values == NULL) {
612 DK_DECREF(keys);
613 return PyErr_NoMemory();
614 }
615 for (i = 0; i < size; i++) {
616 values[i] = NULL;
617 }
618 return new_dict(keys, values);
619}
620
621PyObject *
622PyDict_New(void)
623{
Victor Stinner742da042016-09-07 17:40:12 -0700624 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200625 if (keys == NULL)
626 return NULL;
627 return new_dict(keys, NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400628}
629
Victor Stinner742da042016-09-07 17:40:12 -0700630/* Search index of hash table from offset of entry table */
631static Py_ssize_t
632lookdict_index(PyDictKeysObject *k, Py_hash_t hash, Py_ssize_t index)
633{
INADA Naoki267941c2016-10-06 15:19:07 +0900634 size_t i;
Victor Stinner742da042016-09-07 17:40:12 -0700635 size_t mask = DK_MASK(k);
636 Py_ssize_t ix;
637
638 i = (size_t)hash & mask;
639 ix = dk_get_index(k, i);
640 if (ix == index) {
641 return i;
642 }
643 if (ix == DKIX_EMPTY) {
644 return DKIX_EMPTY;
645 }
646
INADA Naoki267941c2016-10-06 15:19:07 +0900647 for (size_t perturb = hash;;) {
648 perturb >>= PERTURB_SHIFT;
Victor Stinner742da042016-09-07 17:40:12 -0700649 i = mask & ((i << 2) + i + perturb + 1);
650 ix = dk_get_index(k, i);
651 if (ix == index) {
652 return i;
653 }
654 if (ix == DKIX_EMPTY) {
655 return DKIX_EMPTY;
656 }
657 }
658 assert(0); /* NOT REACHED */
659 return DKIX_ERROR;
660}
661
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000662/*
663The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000664This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000665Open addressing is preferred over chaining since the link overhead for
666chaining would be substantial (100% with typical malloc overhead).
667
Tim Peterseb28ef22001-06-02 05:27:19 +0000668The initial probe index is computed as hash mod the table size. Subsequent
669probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000670
671All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000672
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000673The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000674contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000675Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000676
Victor Stinner742da042016-09-07 17:40:12 -0700677lookdict() is general-purpose, and may return DKIX_ERROR if (and only if) a
Victor Stinnera4348cc2016-09-08 12:01:25 -0700678comparison raises an exception.
Guido van Rossum89d8c602007-09-18 17:26:56 +0000679lookdict_unicode() below is specialized to string keys, comparison of which can
Victor Stinner742da042016-09-07 17:40:12 -0700680never raise an exception; that function can never return DKIX_ERROR.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400681lookdict_unicode_nodummy is further specialized for string keys that cannot be
682the <dummy> value.
Victor Stinner742da042016-09-07 17:40:12 -0700683For both, when the key isn't found a DKIX_EMPTY is returned. hashpos returns
684where the key index should be inserted.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000685*/
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100686static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400687lookdict(PyDictObject *mp, PyObject *key,
Victor Stinner742da042016-09-07 17:40:12 -0700688 Py_hash_t hash, PyObject ***value_addr, Py_ssize_t *hashpos)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000689{
INADA Naoki267941c2016-10-06 15:19:07 +0900690 size_t i, mask;
Victor Stinner742da042016-09-07 17:40:12 -0700691 Py_ssize_t ix, freeslot;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200692 int cmp;
Victor Stinner742da042016-09-07 17:40:12 -0700693 PyDictKeysObject *dk;
694 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000696
Antoine Pitrou9a234902012-05-13 20:48:01 +0200697top:
Victor Stinner742da042016-09-07 17:40:12 -0700698 dk = mp->ma_keys;
699 mask = DK_MASK(dk);
700 ep0 = DK_ENTRIES(dk);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700702
703 ix = dk_get_index(dk, i);
704 if (ix == DKIX_EMPTY) {
705 if (hashpos != NULL)
706 *hashpos = i;
707 *value_addr = NULL;
708 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400709 }
Victor Stinner742da042016-09-07 17:40:12 -0700710 if (ix == DKIX_DUMMY) {
711 freeslot = i;
712 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 else {
Victor Stinner742da042016-09-07 17:40:12 -0700714 ep = &ep0[ix];
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300715 assert(ep->me_key != NULL);
Victor Stinner742da042016-09-07 17:40:12 -0700716 if (ep->me_key == key) {
717 *value_addr = &ep->me_value;
718 if (hashpos != NULL)
719 *hashpos = i;
720 return ix;
721 }
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300722 if (ep->me_hash == hash) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 startkey = ep->me_key;
724 Py_INCREF(startkey);
725 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
726 Py_DECREF(startkey);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +0200727 if (cmp < 0) {
728 *value_addr = NULL;
Victor Stinner742da042016-09-07 17:40:12 -0700729 return DKIX_ERROR;
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +0200730 }
Victor Stinner742da042016-09-07 17:40:12 -0700731 if (dk == mp->ma_keys && ep->me_key == startkey) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400732 if (cmp > 0) {
733 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700734 if (hashpos != NULL)
735 *hashpos = i;
736 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400737 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 }
739 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200740 /* The dict was mutated, restart */
741 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 }
743 }
Victor Stinner742da042016-09-07 17:40:12 -0700744 freeslot = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 }
Tim Peters15d49292001-05-27 07:39:22 +0000746
INADA Naoki267941c2016-10-06 15:19:07 +0900747 for (size_t perturb = hash;;) {
748 perturb >>= PERTURB_SHIFT;
Victor Stinner742da042016-09-07 17:40:12 -0700749 i = ((i << 2) + i + perturb + 1) & mask;
750 ix = dk_get_index(dk, i);
751 if (ix == DKIX_EMPTY) {
752 if (hashpos != NULL) {
753 *hashpos = (freeslot == -1) ? (Py_ssize_t)i : freeslot;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400754 }
Victor Stinner742da042016-09-07 17:40:12 -0700755 *value_addr = NULL;
756 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400757 }
Victor Stinner742da042016-09-07 17:40:12 -0700758 if (ix == DKIX_DUMMY) {
759 if (freeslot == -1)
760 freeslot = i;
761 continue;
762 }
763 ep = &ep0[ix];
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300764 assert(ep->me_key != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400765 if (ep->me_key == key) {
Victor Stinner742da042016-09-07 17:40:12 -0700766 if (hashpos != NULL) {
767 *hashpos = i;
768 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400769 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700770 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400771 }
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300772 if (ep->me_hash == hash) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 startkey = ep->me_key;
774 Py_INCREF(startkey);
775 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
776 Py_DECREF(startkey);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400777 if (cmp < 0) {
778 *value_addr = NULL;
Victor Stinner742da042016-09-07 17:40:12 -0700779 return DKIX_ERROR;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400780 }
Victor Stinner742da042016-09-07 17:40:12 -0700781 if (dk == mp->ma_keys && ep->me_key == startkey) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400782 if (cmp > 0) {
Victor Stinner742da042016-09-07 17:40:12 -0700783 if (hashpos != NULL) {
784 *hashpos = i;
785 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400786 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700787 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400788 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 }
790 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200791 /* The dict was mutated, restart */
792 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 }
794 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 }
796 assert(0); /* NOT REACHED */
797 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000798}
799
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400800/* Specialized version for string-only keys */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100801static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400802lookdict_unicode(PyDictObject *mp, PyObject *key,
Victor Stinner742da042016-09-07 17:40:12 -0700803 Py_hash_t hash, PyObject ***value_addr, Py_ssize_t *hashpos)
Fred Drake1bff34a2000-08-31 19:31:38 +0000804{
INADA Naoki267941c2016-10-06 15:19:07 +0900805 size_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200806 size_t mask = DK_MASK(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -0700807 Py_ssize_t ix, freeslot;
808 PyDictKeyEntry *ep, *ep0 = DK_ENTRIES(mp->ma_keys);
Fred Drake1bff34a2000-08-31 19:31:38 +0000809
Victor Stinner742da042016-09-07 17:40:12 -0700810 assert(mp->ma_values == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 /* Make sure this function doesn't have to handle non-unicode keys,
812 including subclasses of str; e.g., one reason to subclass
813 unicodes is to override __eq__, and for speed we don't cater to
814 that here. */
815 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400816 mp->ma_keys->dk_lookup = lookdict;
Victor Stinner742da042016-09-07 17:40:12 -0700817 return lookdict(mp, key, hash, value_addr, hashpos);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 }
Mark Dickinson57e683e2011-09-24 18:18:40 +0100819 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700820 ix = dk_get_index(mp->ma_keys, i);
821 if (ix == DKIX_EMPTY) {
822 if (hashpos != NULL)
823 *hashpos = i;
824 *value_addr = NULL;
825 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400826 }
Victor Stinner742da042016-09-07 17:40:12 -0700827 if (ix == DKIX_DUMMY) {
828 freeslot = i;
829 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 else {
Victor Stinner742da042016-09-07 17:40:12 -0700831 ep = &ep0[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700832 assert(ep->me_key != NULL);
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300833 if (ep->me_key == key
834 || (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700835 if (hashpos != NULL)
836 *hashpos = i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400837 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700838 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400839 }
Victor Stinner742da042016-09-07 17:40:12 -0700840 freeslot = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 }
Tim Peters15d49292001-05-27 07:39:22 +0000842
INADA Naoki267941c2016-10-06 15:19:07 +0900843 for (size_t perturb = hash;;) {
844 perturb >>= PERTURB_SHIFT;
Victor Stinner742da042016-09-07 17:40:12 -0700845 i = mask & ((i << 2) + i + perturb + 1);
846 ix = dk_get_index(mp->ma_keys, i);
847 if (ix == DKIX_EMPTY) {
848 if (hashpos != NULL) {
849 *hashpos = (freeslot == -1) ? (Py_ssize_t)i : freeslot;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400850 }
Victor Stinner742da042016-09-07 17:40:12 -0700851 *value_addr = NULL;
852 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400853 }
Victor Stinner742da042016-09-07 17:40:12 -0700854 if (ix == DKIX_DUMMY) {
855 if (freeslot == -1)
856 freeslot = i;
857 continue;
858 }
859 ep = &ep0[ix];
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300860 assert(ep->me_key != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 if (ep->me_key == key
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300862 || (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400863 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700864 if (hashpos != NULL) {
865 *hashpos = i;
866 }
867 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400868 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 }
870 assert(0); /* NOT REACHED */
871 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000872}
873
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400874/* Faster version of lookdict_unicode when it is known that no <dummy> keys
875 * will be present. */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100876static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400877lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
Victor Stinner742da042016-09-07 17:40:12 -0700878 Py_hash_t hash, PyObject ***value_addr,
879 Py_ssize_t *hashpos)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400880{
INADA Naoki267941c2016-10-06 15:19:07 +0900881 size_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200882 size_t mask = DK_MASK(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -0700883 Py_ssize_t ix;
884 PyDictKeyEntry *ep, *ep0 = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400885
Victor Stinner742da042016-09-07 17:40:12 -0700886 assert(mp->ma_values == NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400887 /* Make sure this function doesn't have to handle non-unicode keys,
888 including subclasses of str; e.g., one reason to subclass
889 unicodes is to override __eq__, and for speed we don't cater to
890 that here. */
891 if (!PyUnicode_CheckExact(key)) {
892 mp->ma_keys->dk_lookup = lookdict;
Victor Stinner742da042016-09-07 17:40:12 -0700893 return lookdict(mp, key, hash, value_addr, hashpos);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400894 }
895 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700896 ix = dk_get_index(mp->ma_keys, i);
897 assert (ix != DKIX_DUMMY);
898 if (ix == DKIX_EMPTY) {
899 if (hashpos != NULL)
900 *hashpos = i;
901 *value_addr = NULL;
902 return DKIX_EMPTY;
903 }
904 ep = &ep0[ix];
Victor Stinnerdee6e252016-09-08 11:16:07 -0700905 assert(ep->me_key != NULL);
906 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700907 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400908 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700909 if (hashpos != NULL)
910 *hashpos = i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400911 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700912 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400913 }
INADA Naoki267941c2016-10-06 15:19:07 +0900914 for (size_t perturb = hash;;) {
915 perturb >>= PERTURB_SHIFT;
Victor Stinner742da042016-09-07 17:40:12 -0700916 i = mask & ((i << 2) + i + perturb + 1);
917 ix = dk_get_index(mp->ma_keys, i);
918 assert (ix != DKIX_DUMMY);
919 if (ix == DKIX_EMPTY) {
920 if (hashpos != NULL)
921 *hashpos = i;
922 *value_addr = NULL;
923 return DKIX_EMPTY;
924 }
925 ep = &ep0[ix];
926 assert(ep->me_key != NULL && PyUnicode_CheckExact(ep->me_key));
927 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400928 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700929 if (hashpos != NULL)
930 *hashpos = i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400931 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700932 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400933 }
934 }
935 assert(0); /* NOT REACHED */
936 return 0;
937}
938
939/* Version of lookdict for split tables.
940 * All split tables and only split tables use this lookup function.
941 * Split tables only contain unicode keys and no dummy keys,
942 * so algorithm is the same as lookdict_unicode_nodummy.
943 */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100944static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400945lookdict_split(PyDictObject *mp, PyObject *key,
Victor Stinner742da042016-09-07 17:40:12 -0700946 Py_hash_t hash, PyObject ***value_addr, Py_ssize_t *hashpos)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400947{
INADA Naoki267941c2016-10-06 15:19:07 +0900948 size_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200949 size_t mask = DK_MASK(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -0700950 Py_ssize_t ix;
951 PyDictKeyEntry *ep, *ep0 = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400952
Victor Stinner742da042016-09-07 17:40:12 -0700953 /* mp must split table */
954 assert(mp->ma_values != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400955 if (!PyUnicode_CheckExact(key)) {
Victor Stinner742da042016-09-07 17:40:12 -0700956 ix = lookdict(mp, key, hash, value_addr, hashpos);
957 if (ix >= 0) {
958 *value_addr = &mp->ma_values[ix];
959 }
960 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400961 }
Victor Stinner742da042016-09-07 17:40:12 -0700962
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400963 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700964 ix = dk_get_index(mp->ma_keys, i);
965 if (ix == DKIX_EMPTY) {
966 if (hashpos != NULL)
967 *hashpos = i;
968 *value_addr = NULL;
969 return DKIX_EMPTY;
970 }
971 assert(ix >= 0);
972 ep = &ep0[ix];
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300973 assert(ep->me_key != NULL && PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700974 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400975 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700976 if (hashpos != NULL)
977 *hashpos = i;
978 *value_addr = &mp->ma_values[ix];
979 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400980 }
INADA Naoki267941c2016-10-06 15:19:07 +0900981 for (size_t perturb = hash;;) {
982 perturb >>= PERTURB_SHIFT;
Victor Stinner742da042016-09-07 17:40:12 -0700983 i = mask & ((i << 2) + i + perturb + 1);
984 ix = dk_get_index(mp->ma_keys, i);
985 if (ix == DKIX_EMPTY) {
986 if (hashpos != NULL)
987 *hashpos = i;
988 *value_addr = NULL;
989 return DKIX_EMPTY;
990 }
991 assert(ix >= 0);
992 ep = &ep0[ix];
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300993 assert(ep->me_key != NULL && PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700994 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400995 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700996 if (hashpos != NULL)
997 *hashpos = i;
998 *value_addr = &mp->ma_values[ix];
999 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001000 }
1001 }
1002 assert(0); /* NOT REACHED */
1003 return 0;
1004}
1005
Benjamin Petersonfb886362010-04-24 18:21:17 +00001006int
1007_PyDict_HasOnlyStringKeys(PyObject *dict)
1008{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 Py_ssize_t pos = 0;
1010 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +00001011 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001013 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 return 1;
1015 while (PyDict_Next(dict, &pos, &key, &value))
1016 if (!PyUnicode_Check(key))
1017 return 0;
1018 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +00001019}
1020
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001021#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 do { \
1023 if (!_PyObject_GC_IS_TRACKED(mp)) { \
1024 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
1025 _PyObject_GC_MAY_BE_TRACKED(value)) { \
1026 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 } \
1028 } \
1029 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001030
1031void
1032_PyDict_MaybeUntrack(PyObject *op)
1033{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 PyDictObject *mp;
1035 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07001036 Py_ssize_t i, numentries;
1037 PyDictKeyEntry *ep0;
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001038
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
1040 return;
1041
1042 mp = (PyDictObject *) op;
Victor Stinner742da042016-09-07 17:40:12 -07001043 ep0 = DK_ENTRIES(mp->ma_keys);
1044 numentries = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001045 if (_PyDict_HasSplitTable(mp)) {
Victor Stinner742da042016-09-07 17:40:12 -07001046 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001047 if ((value = mp->ma_values[i]) == NULL)
1048 continue;
1049 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
Victor Stinner742da042016-09-07 17:40:12 -07001050 assert(!_PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001051 return;
1052 }
1053 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001055 else {
Victor Stinner742da042016-09-07 17:40:12 -07001056 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001057 if ((value = ep0[i].me_value) == NULL)
1058 continue;
1059 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
1060 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
1061 return;
1062 }
1063 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001065}
1066
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001067/* Internal function to find slot for an item from its hash
Victor Stinner3c336c52016-09-12 14:17:40 +02001068 when it is known that the key is not present in the dict.
1069
1070 The dict must be combined. */
Victor Stinner99264802016-09-13 09:38:29 +02001071static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001072find_empty_slot(PyDictObject *mp, PyObject *key, Py_hash_t hash,
Victor Stinner742da042016-09-07 17:40:12 -07001073 PyObject ***value_addr, Py_ssize_t *hashpos)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001074{
INADA Naoki267941c2016-10-06 15:19:07 +09001075 size_t i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001076 size_t mask = DK_MASK(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07001077 Py_ssize_t ix;
1078 PyDictKeyEntry *ep, *ep0 = DK_ENTRIES(mp->ma_keys);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001079
Victor Stinner3c336c52016-09-12 14:17:40 +02001080 assert(!_PyDict_HasSplitTable(mp));
Victor Stinner742da042016-09-07 17:40:12 -07001081 assert(hashpos != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001082 assert(key != NULL);
Victor Stinner3c336c52016-09-12 14:17:40 +02001083
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001084 if (!PyUnicode_CheckExact(key))
1085 mp->ma_keys->dk_lookup = lookdict;
1086 i = hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -07001087 ix = dk_get_index(mp->ma_keys, i);
INADA Naoki267941c2016-10-06 15:19:07 +09001088 for (size_t perturb = hash; ix != DKIX_EMPTY;) {
1089 perturb >>= PERTURB_SHIFT;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001090 i = (i << 2) + i + perturb + 1;
Victor Stinner742da042016-09-07 17:40:12 -07001091 ix = dk_get_index(mp->ma_keys, i & mask);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 }
Victor Stinner742da042016-09-07 17:40:12 -07001093 ep = &ep0[mp->ma_keys->dk_nentries];
1094 *hashpos = i & mask;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001095 assert(ep->me_value == NULL);
Victor Stinner99264802016-09-13 09:38:29 +02001096 *value_addr = &ep->me_value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001097}
1098
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001099static int
1100insertion_resize(PyDictObject *mp)
1101{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -07001102 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001103}
Antoine Pitroue965d972012-02-27 00:45:12 +01001104
1105/*
1106Internal routine to insert a new item into the table.
1107Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +01001108Returns -1 if an error occurred, or 0 on success.
1109*/
1110static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001111insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001112{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001113 PyObject *old_value;
1114 PyObject **value_addr;
Victor Stinner742da042016-09-07 17:40:12 -07001115 PyDictKeyEntry *ep, *ep0;
1116 Py_ssize_t hashpos, ix;
Antoine Pitroue965d972012-02-27 00:45:12 +01001117
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001118 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
1119 if (insertion_resize(mp) < 0)
1120 return -1;
1121 }
1122
Victor Stinner742da042016-09-07 17:40:12 -07001123 ix = mp->ma_keys->dk_lookup(mp, key, hash, &value_addr, &hashpos);
1124 if (ix == DKIX_ERROR) {
Antoine Pitroue965d972012-02-27 00:45:12 +01001125 return -1;
1126 }
Victor Stinner742da042016-09-07 17:40:12 -07001127
Antoine Pitroud6967322014-10-18 00:35:00 +02001128 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
Benjamin Petersona6f195e2012-04-30 10:23:40 -04001129 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001130 MAINTAIN_TRACKING(mp, key, value);
Victor Stinner742da042016-09-07 17:40:12 -07001131
1132 /* When insertion order is different from shared key, we can't share
1133 * the key anymore. Convert this instance to combine table.
1134 */
1135 if (_PyDict_HasSplitTable(mp) &&
1136 ((ix >= 0 && *value_addr == NULL && mp->ma_used != ix) ||
1137 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
1138 if (insertion_resize(mp) < 0) {
1139 Py_DECREF(value);
1140 return -1;
1141 }
1142 find_empty_slot(mp, key, hash, &value_addr, &hashpos);
1143 ix = DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001144 }
Victor Stinner742da042016-09-07 17:40:12 -07001145
1146 if (ix == DKIX_EMPTY) {
1147 /* Insert into new slot. */
1148 if (mp->ma_keys->dk_usable <= 0) {
1149 /* Need to resize. */
1150 if (insertion_resize(mp) < 0) {
1151 Py_DECREF(value);
1152 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001153 }
Victor Stinner742da042016-09-07 17:40:12 -07001154 find_empty_slot(mp, key, hash, &value_addr, &hashpos);
1155 }
1156 ep0 = DK_ENTRIES(mp->ma_keys);
1157 ep = &ep0[mp->ma_keys->dk_nentries];
1158 dk_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
1159 Py_INCREF(key);
1160 ep->me_key = key;
1161 ep->me_hash = hash;
1162 if (mp->ma_values) {
1163 assert (mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
1164 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001165 }
1166 else {
Victor Stinner742da042016-09-07 17:40:12 -07001167 ep->me_value = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001168 }
1169 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001170 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07001171 mp->ma_keys->dk_usable--;
1172 mp->ma_keys->dk_nentries++;
1173 assert(mp->ma_keys->dk_usable >= 0);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001174 assert(_PyDict_CheckConsistency(mp));
Victor Stinner742da042016-09-07 17:40:12 -07001175 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001176 }
Victor Stinner742da042016-09-07 17:40:12 -07001177
1178 assert(value_addr != NULL);
1179
1180 old_value = *value_addr;
1181 if (old_value != NULL) {
1182 *value_addr = value;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001183 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02001184 assert(_PyDict_CheckConsistency(mp));
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001185
Victor Stinner742da042016-09-07 17:40:12 -07001186 Py_DECREF(old_value); /* which **CAN** re-enter (see issue #22653) */
1187 return 0;
1188 }
1189
1190 /* pending state */
1191 assert(_PyDict_HasSplitTable(mp));
1192 assert(ix == mp->ma_used);
1193 *value_addr = value;
1194 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001195 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02001196 assert(_PyDict_CheckConsistency(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001197 return 0;
Antoine Pitroue965d972012-02-27 00:45:12 +01001198}
1199
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001200/*
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001201Internal routine used by dictresize() to buid a hashtable of entries.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001202*/
1203static void
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001204build_indices(PyDictKeysObject *keys, PyDictKeyEntry *ep, Py_ssize_t n)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001205{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001206 size_t mask = (size_t)DK_SIZE(keys) - 1;
1207 for (Py_ssize_t ix = 0; ix != n; ix++, ep++) {
1208 Py_hash_t hash = ep->me_hash;
1209 size_t i = hash & mask;
1210 for (size_t perturb = hash; dk_get_index(keys, i) != DKIX_EMPTY;) {
1211 perturb >>= PERTURB_SHIFT;
1212 i = mask & ((i << 2) + i + perturb + 1);
1213 }
1214 dk_set_index(keys, i, ix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001216}
1217
1218/*
1219Restructure the table by allocating a new table and reinserting all
1220items again. When entries have been deleted, the new table may
1221actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001222If a table is split (its keys and hashes are shared, its values are not),
1223then the values are temporarily copied into the table, it is resized as
1224a combined table, then the me_value slots in the old table are NULLed out.
1225After resizing a table is always combined,
1226but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001227*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001228static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001229dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001230{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001231 Py_ssize_t newsize, numentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001232 PyDictKeysObject *oldkeys;
1233 PyObject **oldvalues;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001234 PyDictKeyEntry *oldentries, *newentries;
Tim Peters91a364d2001-05-19 07:04:38 +00001235
Victor Stinner742da042016-09-07 17:40:12 -07001236 /* Find the smallest table size > minused. */
1237 for (newsize = PyDict_MINSIZE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 newsize <= minused && newsize > 0;
1239 newsize <<= 1)
1240 ;
1241 if (newsize <= 0) {
1242 PyErr_NoMemory();
1243 return -1;
1244 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001245
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001246 oldkeys = mp->ma_keys;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001247
1248 /* NOTE: Current odict checks mp->ma_keys to detect resize happen.
1249 * So we can't reuse oldkeys even if oldkeys->dk_size == newsize.
1250 * TODO: Try reusing oldkeys when reimplement odict.
1251 */
1252
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001253 /* Allocate a new table. */
1254 mp->ma_keys = new_keys_object(newsize);
1255 if (mp->ma_keys == NULL) {
1256 mp->ma_keys = oldkeys;
1257 return -1;
1258 }
1259 if (oldkeys->dk_lookup == lookdict)
1260 mp->ma_keys->dk_lookup = lookdict;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001261
1262 numentries = mp->ma_used;
1263 oldentries = DK_ENTRIES(oldkeys);
1264 newentries = DK_ENTRIES(mp->ma_keys);
1265 oldvalues = mp->ma_values;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001266 if (oldvalues != NULL) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001267 /* Convert split table into new combined table.
1268 * We must incref keys; we can transfer values.
1269 * Note that values of split table is always dense.
1270 */
1271 for (Py_ssize_t i = 0; i < numentries; i++) {
1272 assert(oldvalues[i] != NULL);
1273 PyDictKeyEntry *ep = &oldentries[i];
1274 PyObject *key = ep->me_key;
1275 Py_INCREF(key);
1276 newentries[i].me_key = key;
1277 newentries[i].me_hash = ep->me_hash;
1278 newentries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001280
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001281 DK_DECREF(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001282 mp->ma_values = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001283 if (oldvalues != empty_values) {
1284 free_values(oldvalues);
1285 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001286 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001287 else { // combined table.
1288 if (oldkeys->dk_nentries == numentries) {
1289 memcpy(newentries, oldentries, numentries * sizeof(PyDictKeyEntry));
1290 }
1291 else {
1292 PyDictKeyEntry *ep = oldentries;
1293 for (Py_ssize_t i = 0; i < numentries; i++) {
1294 while (ep->me_value == NULL)
1295 ep++;
1296 newentries[i] = *ep++;
1297 }
1298 }
1299
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001300 assert(oldkeys->dk_lookup != lookdict_split);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001301 assert(oldkeys->dk_refcnt == 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001302 if (oldkeys->dk_size == PyDict_MINSIZE &&
1303 numfreekeys < PyDict_MAXFREELIST) {
1304 DK_DEBUG_DECREF keys_free_list[numfreekeys++] = oldkeys;
1305 }
1306 else {
1307 DK_DEBUG_DECREF PyObject_FREE(oldkeys);
1308 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001309 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001310
1311 build_indices(mp->ma_keys, newentries, numentries);
1312 mp->ma_keys->dk_usable -= numentries;
1313 mp->ma_keys->dk_nentries = numentries;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001315}
1316
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001317/* Returns NULL if unable to split table.
1318 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001319static PyDictKeysObject *
1320make_keys_shared(PyObject *op)
1321{
1322 Py_ssize_t i;
1323 Py_ssize_t size;
1324 PyDictObject *mp = (PyDictObject *)op;
1325
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001326 if (!PyDict_CheckExact(op))
1327 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001328 if (!_PyDict_HasSplitTable(mp)) {
1329 PyDictKeyEntry *ep0;
1330 PyObject **values;
1331 assert(mp->ma_keys->dk_refcnt == 1);
1332 if (mp->ma_keys->dk_lookup == lookdict) {
1333 return NULL;
1334 }
1335 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1336 /* Remove dummy keys */
1337 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1338 return NULL;
1339 }
1340 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1341 /* Copy values into a new array */
Victor Stinner742da042016-09-07 17:40:12 -07001342 ep0 = DK_ENTRIES(mp->ma_keys);
1343 size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001344 values = new_values(size);
1345 if (values == NULL) {
1346 PyErr_SetString(PyExc_MemoryError,
1347 "Not enough memory to allocate new values array");
1348 return NULL;
1349 }
1350 for (i = 0; i < size; i++) {
1351 values[i] = ep0[i].me_value;
1352 ep0[i].me_value = NULL;
1353 }
1354 mp->ma_keys->dk_lookup = lookdict_split;
1355 mp->ma_values = values;
1356 }
1357 DK_INCREF(mp->ma_keys);
1358 return mp->ma_keys;
1359}
Christian Heimes99170a52007-12-19 02:07:34 +00001360
1361PyObject *
1362_PyDict_NewPresized(Py_ssize_t minused)
1363{
INADA Naoki92c50ee2016-11-22 00:57:02 +09001364 const Py_ssize_t max_presize = 128 * 1024;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001365 Py_ssize_t newsize;
1366 PyDictKeysObject *new_keys;
INADA Naoki92c50ee2016-11-22 00:57:02 +09001367
1368 /* There are no strict guarantee that returned dict can contain minused
1369 * items without resize. So we create medium size dict instead of very
1370 * large dict or MemoryError.
1371 */
1372 if (minused > USABLE_FRACTION(max_presize)) {
1373 newsize = max_presize;
1374 }
1375 else {
1376 Py_ssize_t minsize = ESTIMATE_SIZE(minused);
1377 newsize = PyDict_MINSIZE;
1378 while (newsize < minsize) {
1379 newsize <<= 1;
1380 }
1381 }
1382 assert(IS_POWER_OF_2(newsize));
1383
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001384 new_keys = new_keys_object(newsize);
1385 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001387 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001388}
1389
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001390/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1391 * that may occur (originally dicts supported only string keys, and exceptions
1392 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001393 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001394 * (suppressed) occurred while computing the key's hash, or that some error
1395 * (suppressed) occurred when comparing keys in the dict's internal probe
1396 * sequence. A nasty example of the latter is when a Python-coded comparison
1397 * function hits a stack-depth error, which can cause this to return NULL
1398 * even if the key is present.
1399 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001400PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001401PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001402{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001403 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07001404 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 PyDictObject *mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 PyThreadState *tstate;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001407 PyObject **value_addr;
1408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 if (!PyDict_Check(op))
1410 return NULL;
1411 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001412 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 {
1414 hash = PyObject_Hash(key);
1415 if (hash == -1) {
1416 PyErr_Clear();
1417 return NULL;
1418 }
1419 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 /* We can arrive here with a NULL tstate during initialization: try
1422 running "python -Wi" for an example related to string interning.
1423 Let's just hope that no exception occurs then... This must be
1424 _PyThreadState_Current and not PyThreadState_GET() because in debug
1425 mode, the latter complains if tstate is NULL. */
Victor Stinner0cae6092016-11-11 01:43:56 +01001426 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 if (tstate != NULL && tstate->curexc_type != NULL) {
1428 /* preserve the existing exception */
1429 PyObject *err_type, *err_value, *err_tb;
1430 PyErr_Fetch(&err_type, &err_value, &err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001431 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 /* ignore errors */
1433 PyErr_Restore(err_type, err_value, err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001434 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 return NULL;
1436 }
1437 else {
Victor Stinner742da042016-09-07 17:40:12 -07001438 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
1439 if (ix < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 PyErr_Clear();
1441 return NULL;
1442 }
1443 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001444 return *value_addr;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001445}
1446
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001447/* Same as PyDict_GetItemWithError() but with hash supplied by caller.
1448 This returns NULL *with* an exception set if an exception occurred.
1449 It returns NULL *without* an exception set if the key wasn't present.
1450*/
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001451PyObject *
1452_PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1453{
Victor Stinner742da042016-09-07 17:40:12 -07001454 Py_ssize_t ix;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001455 PyDictObject *mp = (PyDictObject *)op;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001456 PyObject **value_addr;
1457
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001458 if (!PyDict_Check(op)) {
1459 PyErr_BadInternalCall();
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001460 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001461 }
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001462
1463 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
1464 if (ix < 0) {
1465 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001466 }
1467 return *value_addr;
1468}
1469
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001470/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1471 This returns NULL *with* an exception set if an exception occurred.
1472 It returns NULL *without* an exception set if the key wasn't present.
1473*/
1474PyObject *
1475PyDict_GetItemWithError(PyObject *op, PyObject *key)
1476{
Victor Stinner742da042016-09-07 17:40:12 -07001477 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001478 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 PyDictObject*mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001480 PyObject **value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 if (!PyDict_Check(op)) {
1483 PyErr_BadInternalCall();
1484 return NULL;
1485 }
1486 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001487 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001488 {
1489 hash = PyObject_Hash(key);
1490 if (hash == -1) {
1491 return NULL;
1492 }
1493 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001494
Victor Stinner742da042016-09-07 17:40:12 -07001495 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
1496 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001498 return *value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001499}
1500
Brett Cannonfd074152012-04-14 14:10:13 -04001501PyObject *
1502_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1503{
1504 PyObject *kv;
1505 kv = _PyUnicode_FromId(key); /* borrowed */
1506 if (kv == NULL)
1507 return NULL;
1508 return PyDict_GetItemWithError(dp, kv);
1509}
1510
Victor Stinnerb4efc962015-11-20 09:24:02 +01001511/* Fast version of global value lookup (LOAD_GLOBAL).
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001512 * Lookup in globals, then builtins.
Victor Stinnerb4efc962015-11-20 09:24:02 +01001513 *
1514 * Raise an exception and return NULL if an error occurred (ex: computing the
1515 * key hash failed, key comparison failed, ...). Return NULL if the key doesn't
1516 * exist. Return the value if the key exists.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001517 */
1518PyObject *
1519_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001520{
Victor Stinner742da042016-09-07 17:40:12 -07001521 Py_ssize_t ix;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001522 Py_hash_t hash;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001523 PyObject **value_addr;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001524
1525 if (!PyUnicode_CheckExact(key) ||
1526 (hash = ((PyASCIIObject *) key)->hash) == -1)
1527 {
1528 hash = PyObject_Hash(key);
1529 if (hash == -1)
1530 return NULL;
Antoine Pitroue965d972012-02-27 00:45:12 +01001531 }
Victor Stinnerb4efc962015-11-20 09:24:02 +01001532
1533 /* namespace 1: globals */
Victor Stinner742da042016-09-07 17:40:12 -07001534 ix = globals->ma_keys->dk_lookup(globals, key, hash, &value_addr, NULL);
1535 if (ix == DKIX_ERROR)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001536 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001537 if (ix != DKIX_EMPTY && *value_addr != NULL)
1538 return *value_addr;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001539
1540 /* namespace 2: builtins */
Victor Stinner742da042016-09-07 17:40:12 -07001541 ix = builtins->ma_keys->dk_lookup(builtins, key, hash, &value_addr, NULL);
1542 if (ix < 0)
Victor Stinnerb4efc962015-11-20 09:24:02 +01001543 return NULL;
1544 return *value_addr;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001545}
1546
Antoine Pitroue965d972012-02-27 00:45:12 +01001547/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1548 * dictionary if it's merely replacing the value for an existing key.
1549 * This means that it's safe to loop over a dictionary with PyDict_Next()
1550 * and occasionally replace a value -- but you can't insert new keys or
1551 * remove them.
1552 */
1553int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001554PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001555{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001556 PyDictObject *mp;
1557 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001558 if (!PyDict_Check(op)) {
1559 PyErr_BadInternalCall();
1560 return -1;
1561 }
1562 assert(key);
1563 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001564 mp = (PyDictObject *)op;
1565 if (!PyUnicode_CheckExact(key) ||
1566 (hash = ((PyASCIIObject *) key)->hash) == -1)
1567 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001568 hash = PyObject_Hash(key);
1569 if (hash == -1)
1570 return -1;
1571 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001572
1573 /* insertdict() handles any resizing that might be necessary */
1574 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001575}
1576
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001577int
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001578_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1579 Py_hash_t hash)
1580{
1581 PyDictObject *mp;
1582
1583 if (!PyDict_Check(op)) {
1584 PyErr_BadInternalCall();
1585 return -1;
1586 }
1587 assert(key);
1588 assert(value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001589 assert(hash != -1);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001590 mp = (PyDictObject *)op;
1591
1592 /* insertdict() handles any resizing that might be necessary */
1593 return insertdict(mp, key, hash, value);
1594}
1595
1596int
Tim Peters1f5871e2000-07-04 17:44:48 +00001597PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001598{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001599 Py_hash_t hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +00001600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 assert(key);
1602 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001603 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 hash = PyObject_Hash(key);
1605 if (hash == -1)
1606 return -1;
1607 }
Victor Stinner742da042016-09-07 17:40:12 -07001608
1609 return _PyDict_DelItem_KnownHash(op, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001610}
1611
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001612int
1613_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1614{
Victor Stinner742da042016-09-07 17:40:12 -07001615 Py_ssize_t hashpos, ix;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001616 PyDictObject *mp;
1617 PyDictKeyEntry *ep;
1618 PyObject *old_key, *old_value;
1619 PyObject **value_addr;
1620
1621 if (!PyDict_Check(op)) {
1622 PyErr_BadInternalCall();
1623 return -1;
1624 }
1625 assert(key);
1626 assert(hash != -1);
1627 mp = (PyDictObject *)op;
Victor Stinner742da042016-09-07 17:40:12 -07001628 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
1629 if (ix == DKIX_ERROR)
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001630 return -1;
Victor Stinner742da042016-09-07 17:40:12 -07001631 if (ix == DKIX_EMPTY || *value_addr == NULL) {
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001632 _PyErr_SetKeyError(key);
1633 return -1;
1634 }
Victor Stinner742da042016-09-07 17:40:12 -07001635 assert(dk_get_index(mp->ma_keys, hashpos) == ix);
Victor Stinner78601a32016-09-09 19:28:36 -07001636
1637 // Split table doesn't allow deletion. Combine it.
1638 if (_PyDict_HasSplitTable(mp)) {
1639 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1640 return -1;
1641 }
1642 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
1643 assert(ix >= 0);
1644 }
1645
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001646 old_value = *value_addr;
Victor Stinner78601a32016-09-09 19:28:36 -07001647 assert(old_value != NULL);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001648 *value_addr = NULL;
1649 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001650 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner78601a32016-09-09 19:28:36 -07001651 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1652 dk_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
1653 ENSURE_ALLOWS_DELETIONS(mp);
1654 old_key = ep->me_key;
1655 ep->me_key = NULL;
1656 Py_DECREF(old_key);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001657 Py_DECREF(old_value);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001658
1659 assert(_PyDict_CheckConsistency(mp));
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001660 return 0;
1661}
1662
Guido van Rossum25831651993-05-19 14:50:45 +00001663void
Tim Peters1f5871e2000-07-04 17:44:48 +00001664PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001665{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001667 PyDictKeysObject *oldkeys;
1668 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 if (!PyDict_Check(op))
1672 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001673 mp = ((PyDictObject *)op);
1674 oldkeys = mp->ma_keys;
1675 oldvalues = mp->ma_values;
1676 if (oldvalues == empty_values)
1677 return;
1678 /* Empty the dict... */
1679 DK_INCREF(Py_EMPTY_KEYS);
1680 mp->ma_keys = Py_EMPTY_KEYS;
1681 mp->ma_values = empty_values;
1682 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001683 mp->ma_version_tag = DICT_NEXT_VERSION();
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001684 /* ...then clear the keys and values */
1685 if (oldvalues != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001686 n = oldkeys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001687 for (i = 0; i < n; i++)
1688 Py_CLEAR(oldvalues[i]);
1689 free_values(oldvalues);
1690 DK_DECREF(oldkeys);
1691 }
1692 else {
1693 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001694 DK_DECREF(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001695 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02001696 assert(_PyDict_CheckConsistency(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001697}
1698
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001699/* Internal version of PyDict_Next that returns a hash value in addition
1700 * to the key and value.
1701 * Return 1 on success, return 0 when the reached the end of the dictionary
1702 * (or if op is not a dictionary)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001703 */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001704int
1705_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1706 PyObject **pvalue, Py_hash_t *phash)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001707{
INADA Naokica2d8be2016-11-04 16:59:10 +09001708 Py_ssize_t i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001709 PyDictObject *mp;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001710 PyDictKeyEntry *entry_ptr;
1711 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001712
1713 if (!PyDict_Check(op))
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001714 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 mp = (PyDictObject *)op;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001716 i = *ppos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001717 if (mp->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09001718 if (i < 0 || i >= mp->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001719 return 0;
INADA Naokica2d8be2016-11-04 16:59:10 +09001720 /* values of split table is always dense */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001721 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
INADA Naokica2d8be2016-11-04 16:59:10 +09001722 value = mp->ma_values[i];
1723 assert(value != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001725 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09001726 Py_ssize_t n = mp->ma_keys->dk_nentries;
1727 if (i < 0 || i >= n)
1728 return 0;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001729 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
1730 while (i < n && entry_ptr->me_value == NULL) {
1731 entry_ptr++;
1732 i++;
Victor Stinner742da042016-09-07 17:40:12 -07001733 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001734 if (i >= n)
1735 return 0;
1736 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001738 *ppos = i+1;
1739 if (pkey)
1740 *pkey = entry_ptr->me_key;
1741 if (phash)
1742 *phash = entry_ptr->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001743 if (pvalue)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001744 *pvalue = value;
1745 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001746}
1747
Tim Peters080c88b2003-02-15 03:01:11 +00001748/*
1749 * Iterate over a dict. Use like so:
1750 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001751 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001752 * PyObject *key, *value;
1753 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001754 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001755 * Refer to borrowed references in key and value.
Tim Peters080c88b2003-02-15 03:01:11 +00001756 * }
1757 *
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001758 * Return 1 on success, return 0 when the reached the end of the dictionary
1759 * (or if op is not a dictionary)
1760 *
Tim Peters080c88b2003-02-15 03:01:11 +00001761 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001762 * mutates the dict. One exception: it is safe if the loop merely changes
1763 * the values associated with the keys (but doesn't insert new keys or
1764 * delete keys), via PyDict_SetItem().
1765 */
Guido van Rossum25831651993-05-19 14:50:45 +00001766int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001767PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001768{
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001769 return _PyDict_Next(op, ppos, pkey, pvalue, NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001770}
1771
Eric Snow96c6af92015-05-29 22:21:39 -06001772/* Internal version of dict.pop(). */
1773PyObject *
Yury Selivanov684ef2c2016-10-28 19:01:21 -04001774_PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt)
Eric Snow96c6af92015-05-29 22:21:39 -06001775{
1776 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07001777 Py_ssize_t ix, hashpos;
Eric Snow96c6af92015-05-29 22:21:39 -06001778 PyObject *old_value, *old_key;
1779 PyDictKeyEntry *ep;
1780 PyObject **value_addr;
Yury Selivanov684ef2c2016-10-28 19:01:21 -04001781 PyDictObject *mp;
1782
1783 assert(PyDict_Check(dict));
1784 mp = (PyDictObject *)dict;
Eric Snow96c6af92015-05-29 22:21:39 -06001785
1786 if (mp->ma_used == 0) {
1787 if (deflt) {
1788 Py_INCREF(deflt);
1789 return deflt;
1790 }
1791 _PyErr_SetKeyError(key);
1792 return NULL;
1793 }
1794 if (!PyUnicode_CheckExact(key) ||
1795 (hash = ((PyASCIIObject *) key)->hash) == -1) {
1796 hash = PyObject_Hash(key);
1797 if (hash == -1)
1798 return NULL;
1799 }
Victor Stinner742da042016-09-07 17:40:12 -07001800 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
1801 if (ix == DKIX_ERROR)
Eric Snow96c6af92015-05-29 22:21:39 -06001802 return NULL;
Victor Stinnerd0ad11f2016-09-13 16:56:38 +02001803 if (ix == DKIX_EMPTY || *value_addr == NULL) {
Eric Snow96c6af92015-05-29 22:21:39 -06001804 if (deflt) {
1805 Py_INCREF(deflt);
1806 return deflt;
1807 }
1808 _PyErr_SetKeyError(key);
1809 return NULL;
1810 }
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001811
Victor Stinner78601a32016-09-09 19:28:36 -07001812 // Split table doesn't allow deletion. Combine it.
1813 if (_PyDict_HasSplitTable(mp)) {
1814 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1815 return NULL;
1816 }
1817 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
1818 assert(ix >= 0);
1819 }
1820
Victor Stinner742da042016-09-07 17:40:12 -07001821 old_value = *value_addr;
Victor Stinner78601a32016-09-09 19:28:36 -07001822 assert(old_value != NULL);
Eric Snow96c6af92015-05-29 22:21:39 -06001823 *value_addr = NULL;
1824 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001825 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner78601a32016-09-09 19:28:36 -07001826 dk_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
1827 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1828 ENSURE_ALLOWS_DELETIONS(mp);
1829 old_key = ep->me_key;
1830 ep->me_key = NULL;
1831 Py_DECREF(old_key);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001832
1833 assert(_PyDict_CheckConsistency(mp));
Eric Snow96c6af92015-05-29 22:21:39 -06001834 return old_value;
1835}
1836
1837/* Internal version of dict.from_keys(). It is subclass-friendly. */
1838PyObject *
1839_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1840{
1841 PyObject *it; /* iter(iterable) */
1842 PyObject *key;
1843 PyObject *d;
1844 int status;
1845
1846 d = PyObject_CallObject(cls, NULL);
1847 if (d == NULL)
1848 return NULL;
1849
1850 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
1851 if (PyDict_CheckExact(iterable)) {
1852 PyDictObject *mp = (PyDictObject *)d;
1853 PyObject *oldvalue;
1854 Py_ssize_t pos = 0;
1855 PyObject *key;
1856 Py_hash_t hash;
1857
Victor Stinner742da042016-09-07 17:40:12 -07001858 if (dictresize(mp, ESTIMATE_SIZE(Py_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001859 Py_DECREF(d);
1860 return NULL;
1861 }
1862
1863 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
1864 if (insertdict(mp, key, hash, value)) {
1865 Py_DECREF(d);
1866 return NULL;
1867 }
1868 }
1869 return d;
1870 }
1871 if (PyAnySet_CheckExact(iterable)) {
1872 PyDictObject *mp = (PyDictObject *)d;
1873 Py_ssize_t pos = 0;
1874 PyObject *key;
1875 Py_hash_t hash;
1876
Victor Stinner742da042016-09-07 17:40:12 -07001877 if (dictresize(mp, ESTIMATE_SIZE(PySet_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001878 Py_DECREF(d);
1879 return NULL;
1880 }
1881
1882 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
1883 if (insertdict(mp, key, hash, value)) {
1884 Py_DECREF(d);
1885 return NULL;
1886 }
1887 }
1888 return d;
1889 }
1890 }
1891
1892 it = PyObject_GetIter(iterable);
1893 if (it == NULL){
1894 Py_DECREF(d);
1895 return NULL;
1896 }
1897
1898 if (PyDict_CheckExact(d)) {
1899 while ((key = PyIter_Next(it)) != NULL) {
1900 status = PyDict_SetItem(d, key, value);
1901 Py_DECREF(key);
1902 if (status < 0)
1903 goto Fail;
1904 }
1905 } else {
1906 while ((key = PyIter_Next(it)) != NULL) {
1907 status = PyObject_SetItem(d, key, value);
1908 Py_DECREF(key);
1909 if (status < 0)
1910 goto Fail;
1911 }
1912 }
1913
1914 if (PyErr_Occurred())
1915 goto Fail;
1916 Py_DECREF(it);
1917 return d;
1918
1919Fail:
1920 Py_DECREF(it);
1921 Py_DECREF(d);
1922 return NULL;
1923}
1924
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001925/* Methods */
1926
1927static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001928dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001929{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001930 PyObject **values = mp->ma_values;
1931 PyDictKeysObject *keys = mp->ma_keys;
1932 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001933 PyObject_GC_UnTrack(mp);
1934 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001935 if (values != NULL) {
1936 if (values != empty_values) {
Victor Stinner742da042016-09-07 17:40:12 -07001937 for (i = 0, n = mp->ma_keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001938 Py_XDECREF(values[i]);
1939 }
1940 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001942 DK_DECREF(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001943 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02001944 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001945 assert(keys->dk_refcnt == 1);
1946 DK_DECREF(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001947 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1949 free_list[numfree++] = mp;
1950 else
1951 Py_TYPE(mp)->tp_free((PyObject *)mp);
1952 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001953}
1954
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001955
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001956static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001957dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001958{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001960 PyObject *key = NULL, *value = NULL;
1961 _PyUnicodeWriter writer;
1962 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00001963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 i = Py_ReprEnter((PyObject *)mp);
1965 if (i != 0) {
1966 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1967 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001968
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001970 Py_ReprLeave((PyObject *)mp);
1971 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 }
Tim Petersa7259592001-06-16 05:11:17 +00001973
Victor Stinnerf91929b2013-11-19 13:07:38 +01001974 _PyUnicodeWriter_Init(&writer);
1975 writer.overallocate = 1;
1976 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
1977 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00001978
Victor Stinnerf91929b2013-11-19 13:07:38 +01001979 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
1980 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001982 /* Do repr() on each key+value pair, and insert ": " between them.
1983 Note that repr may mutate the dict. */
1984 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001985 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001986 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001987 PyObject *s;
1988 int res;
1989
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001990 /* Prevent repr from deleting key or value during key format. */
1991 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001992 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02001993
Victor Stinnerf91929b2013-11-19 13:07:38 +01001994 if (!first) {
1995 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
1996 goto error;
1997 }
1998 first = 0;
1999
2000 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002001 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01002002 goto error;
2003 res = _PyUnicodeWriter_WriteStr(&writer, s);
2004 Py_DECREF(s);
2005 if (res < 0)
2006 goto error;
2007
2008 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
2009 goto error;
2010
2011 s = PyObject_Repr(value);
2012 if (s == NULL)
2013 goto error;
2014 res = _PyUnicodeWriter_WriteStr(&writer, s);
2015 Py_DECREF(s);
2016 if (res < 0)
2017 goto error;
2018
2019 Py_CLEAR(key);
2020 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002021 }
Tim Petersa7259592001-06-16 05:11:17 +00002022
Victor Stinnerf91929b2013-11-19 13:07:38 +01002023 writer.overallocate = 0;
2024 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
2025 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00002026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01002028
2029 return _PyUnicodeWriter_Finish(&writer);
2030
2031error:
2032 Py_ReprLeave((PyObject *)mp);
2033 _PyUnicodeWriter_Dealloc(&writer);
2034 Py_XDECREF(key);
2035 Py_XDECREF(value);
2036 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002037}
2038
Martin v. Löwis18e16552006-02-15 17:27:45 +00002039static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002040dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002041{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002042 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002043}
2044
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002045static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002046dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002047{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048 PyObject *v;
Victor Stinner742da042016-09-07 17:40:12 -07002049 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002050 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002051 PyObject **value_addr;
2052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002054 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002055 hash = PyObject_Hash(key);
2056 if (hash == -1)
2057 return NULL;
2058 }
Victor Stinner742da042016-09-07 17:40:12 -07002059 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
2060 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002062 if (ix == DKIX_EMPTY || *value_addr == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002063 if (!PyDict_CheckExact(mp)) {
2064 /* Look up __missing__ method if we're a subclass. */
2065 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05002066 _Py_IDENTIFIER(__missing__);
2067 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002068 if (missing != NULL) {
Victor Stinner27580c12016-12-01 14:43:22 +01002069 res = _PyObject_CallArg1(missing, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002070 Py_DECREF(missing);
2071 return res;
2072 }
2073 else if (PyErr_Occurred())
2074 return NULL;
2075 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07002076 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077 return NULL;
2078 }
Victor Stinner742da042016-09-07 17:40:12 -07002079 v = *value_addr;
2080 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002081 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002082}
2083
2084static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002085dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002086{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002087 if (w == NULL)
2088 return PyDict_DelItem((PyObject *)mp, v);
2089 else
2090 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002091}
2092
Guido van Rossuma9e7a811997-05-13 21:02:11 +00002093static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002094 (lenfunc)dict_length, /*mp_length*/
2095 (binaryfunc)dict_subscript, /*mp_subscript*/
2096 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002097};
2098
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002099static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002100dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002101{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002102 PyObject *v;
2103 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002104 PyDictKeyEntry *ep;
2105 Py_ssize_t size, n, offset;
2106 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002107
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002108 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002109 n = mp->ma_used;
2110 v = PyList_New(n);
2111 if (v == NULL)
2112 return NULL;
2113 if (n != mp->ma_used) {
2114 /* Durnit. The allocations caused the dict to resize.
2115 * Just start over, this shouldn't normally happen.
2116 */
2117 Py_DECREF(v);
2118 goto again;
2119 }
Victor Stinner742da042016-09-07 17:40:12 -07002120 ep = DK_ENTRIES(mp->ma_keys);
2121 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002122 if (mp->ma_values) {
2123 value_ptr = mp->ma_values;
2124 offset = sizeof(PyObject *);
2125 }
2126 else {
2127 value_ptr = &ep[0].me_value;
2128 offset = sizeof(PyDictKeyEntry);
2129 }
2130 for (i = 0, j = 0; i < size; i++) {
2131 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002132 PyObject *key = ep[i].me_key;
2133 Py_INCREF(key);
2134 PyList_SET_ITEM(v, j, key);
2135 j++;
2136 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002137 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138 }
2139 assert(j == n);
2140 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002141}
2142
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002143static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002144dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002145{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002146 PyObject *v;
2147 Py_ssize_t i, j;
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002148 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002149 Py_ssize_t size, n, offset;
2150 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002151
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002152 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002153 n = mp->ma_used;
2154 v = PyList_New(n);
2155 if (v == NULL)
2156 return NULL;
2157 if (n != mp->ma_used) {
2158 /* Durnit. The allocations caused the dict to resize.
2159 * Just start over, this shouldn't normally happen.
2160 */
2161 Py_DECREF(v);
2162 goto again;
2163 }
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002164 ep = DK_ENTRIES(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002165 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002166 if (mp->ma_values) {
2167 value_ptr = mp->ma_values;
2168 offset = sizeof(PyObject *);
2169 }
2170 else {
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002171 value_ptr = &ep[0].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002172 offset = sizeof(PyDictKeyEntry);
2173 }
2174 for (i = 0, j = 0; i < size; i++) {
2175 PyObject *value = *value_ptr;
2176 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2177 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002178 Py_INCREF(value);
2179 PyList_SET_ITEM(v, j, value);
2180 j++;
2181 }
2182 }
2183 assert(j == n);
2184 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002185}
2186
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002187static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002188dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002189{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002190 PyObject *v;
2191 Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002192 Py_ssize_t size, offset;
2193 PyObject *item, *key;
2194 PyDictKeyEntry *ep;
2195 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 /* Preallocate the list of tuples, to avoid allocations during
2198 * the loop over the items, which could trigger GC, which
2199 * could resize the dict. :-(
2200 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002201 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002202 n = mp->ma_used;
2203 v = PyList_New(n);
2204 if (v == NULL)
2205 return NULL;
2206 for (i = 0; i < n; i++) {
2207 item = PyTuple_New(2);
2208 if (item == NULL) {
2209 Py_DECREF(v);
2210 return NULL;
2211 }
2212 PyList_SET_ITEM(v, i, item);
2213 }
2214 if (n != mp->ma_used) {
2215 /* Durnit. The allocations caused the dict to resize.
2216 * Just start over, this shouldn't normally happen.
2217 */
2218 Py_DECREF(v);
2219 goto again;
2220 }
2221 /* Nothing we do below makes any function calls. */
Victor Stinner742da042016-09-07 17:40:12 -07002222 ep = DK_ENTRIES(mp->ma_keys);
2223 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002224 if (mp->ma_values) {
2225 value_ptr = mp->ma_values;
2226 offset = sizeof(PyObject *);
2227 }
2228 else {
2229 value_ptr = &ep[0].me_value;
2230 offset = sizeof(PyDictKeyEntry);
2231 }
2232 for (i = 0, j = 0; i < size; i++) {
2233 PyObject *value = *value_ptr;
2234 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2235 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 key = ep[i].me_key;
2237 item = PyList_GET_ITEM(v, j);
2238 Py_INCREF(key);
2239 PyTuple_SET_ITEM(item, 0, key);
2240 Py_INCREF(value);
2241 PyTuple_SET_ITEM(item, 1, value);
2242 j++;
2243 }
2244 }
2245 assert(j == n);
2246 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002247}
2248
Larry Hastings5c661892014-01-24 06:17:25 -08002249/*[clinic input]
2250@classmethod
2251dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08002252 iterable: object
2253 value: object=None
2254 /
2255
2256Returns a new dict with keys from iterable and values equal to value.
2257[clinic start generated code]*/
2258
Larry Hastings5c661892014-01-24 06:17:25 -08002259static PyObject *
2260dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002261/*[clinic end generated code: output=8fb98e4b10384999 input=b85a667f9bf4669d]*/
Larry Hastings5c661892014-01-24 06:17:25 -08002262{
Eric Snow96c6af92015-05-29 22:21:39 -06002263 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002264}
2265
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002266static int
Victor Stinner742da042016-09-07 17:40:12 -07002267dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
2268 const char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 PyObject *arg = NULL;
2271 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002273 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
2274 result = -1;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002276 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002277 _Py_IDENTIFIER(keys);
2278 if (_PyObject_HasAttrId(arg, &PyId_keys))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002279 result = PyDict_Merge(self, arg, 1);
2280 else
2281 result = PyDict_MergeFromSeq2(self, arg, 1);
2282 }
2283 if (result == 0 && kwds != NULL) {
2284 if (PyArg_ValidateKeywordArguments(kwds))
2285 result = PyDict_Merge(self, kwds, 1);
2286 else
2287 result = -1;
2288 }
2289 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002290}
2291
2292static PyObject *
2293dict_update(PyObject *self, PyObject *args, PyObject *kwds)
2294{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002295 if (dict_update_common(self, args, kwds, "update") != -1)
2296 Py_RETURN_NONE;
2297 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002298}
2299
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002300/* Update unconditionally replaces existing items.
2301 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00002302 otherwise it leaves existing items unchanged.
2303
2304 PyDict_{Update,Merge} update/merge from a mapping object.
2305
Tim Petersf582b822001-12-11 18:51:08 +00002306 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00002307 producing iterable objects of length 2.
2308*/
2309
Tim Petersf582b822001-12-11 18:51:08 +00002310int
Tim Peters1fc240e2001-10-26 05:06:50 +00002311PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
2312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002313 PyObject *it; /* iter(seq2) */
2314 Py_ssize_t i; /* index into seq2 of current element */
2315 PyObject *item; /* seq2[i] */
2316 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00002317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 assert(d != NULL);
2319 assert(PyDict_Check(d));
2320 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00002321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002322 it = PyObject_GetIter(seq2);
2323 if (it == NULL)
2324 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002326 for (i = 0; ; ++i) {
2327 PyObject *key, *value;
2328 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00002329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002330 fast = NULL;
2331 item = PyIter_Next(it);
2332 if (item == NULL) {
2333 if (PyErr_Occurred())
2334 goto Fail;
2335 break;
2336 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002338 /* Convert item to sequence, and verify length 2. */
2339 fast = PySequence_Fast(item, "");
2340 if (fast == NULL) {
2341 if (PyErr_ExceptionMatches(PyExc_TypeError))
2342 PyErr_Format(PyExc_TypeError,
2343 "cannot convert dictionary update "
2344 "sequence element #%zd to a sequence",
2345 i);
2346 goto Fail;
2347 }
2348 n = PySequence_Fast_GET_SIZE(fast);
2349 if (n != 2) {
2350 PyErr_Format(PyExc_ValueError,
2351 "dictionary update sequence element #%zd "
2352 "has length %zd; 2 is required",
2353 i, n);
2354 goto Fail;
2355 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 /* Update/merge with this (key, value) pair. */
2358 key = PySequence_Fast_GET_ITEM(fast, 0);
2359 value = PySequence_Fast_GET_ITEM(fast, 1);
2360 if (override || PyDict_GetItem(d, key) == NULL) {
2361 int status = PyDict_SetItem(d, key, value);
2362 if (status < 0)
2363 goto Fail;
2364 }
2365 Py_DECREF(fast);
2366 Py_DECREF(item);
2367 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 i = 0;
Victor Stinner611b0fa2016-09-14 15:02:01 +02002370 assert(_PyDict_CheckConsistency((PyDictObject *)d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002372Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373 Py_XDECREF(item);
2374 Py_XDECREF(fast);
2375 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002376Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002377 Py_DECREF(it);
2378 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002379}
2380
doko@ubuntu.comc96df682016-10-11 08:04:02 +02002381static int
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002382dict_merge(PyObject *a, PyObject *b, int override)
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002383{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002384 PyDictObject *mp, *other;
2385 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -07002386 PyDictKeyEntry *entry, *ep0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002387
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002388 assert(0 <= override && override <= 2);
2389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002390 /* We accept for the argument either a concrete dictionary object,
2391 * or an abstract "mapping" object. For the former, we can do
2392 * things quite efficiently. For the latter, we only require that
2393 * PyMapping_Keys() and PyObject_GetItem() be supported.
2394 */
2395 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2396 PyErr_BadInternalCall();
2397 return -1;
2398 }
2399 mp = (PyDictObject*)a;
2400 if (PyDict_Check(b)) {
2401 other = (PyDictObject*)b;
2402 if (other == mp || other->ma_used == 0)
2403 /* a.update(a) or a.update({}); nothing to do */
2404 return 0;
2405 if (mp->ma_used == 0)
2406 /* Since the target dict is empty, PyDict_GetItem()
2407 * always returns NULL. Setting override to 1
2408 * skips the unnecessary test.
2409 */
2410 override = 1;
2411 /* Do one big resize at the start, rather than
2412 * incrementally resizing as we insert new items. Expect
2413 * that there will be no (or few) overlapping keys.
2414 */
INADA Naokib1152be2016-10-27 19:26:50 +09002415 if (USABLE_FRACTION(mp->ma_keys->dk_size) < other->ma_used) {
2416 if (dictresize(mp, ESTIMATE_SIZE(mp->ma_used + other->ma_used))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002417 return -1;
INADA Naokib1152be2016-10-27 19:26:50 +09002418 }
2419 }
Victor Stinner742da042016-09-07 17:40:12 -07002420 ep0 = DK_ENTRIES(other->ma_keys);
2421 for (i = 0, n = other->ma_keys->dk_nentries; i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002422 PyObject *key, *value;
2423 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002424 entry = &ep0[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002425 key = entry->me_key;
2426 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002427 if (other->ma_values)
2428 value = other->ma_values[i];
2429 else
2430 value = entry->me_value;
2431
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002432 if (value != NULL) {
2433 int err = 0;
2434 Py_INCREF(key);
2435 Py_INCREF(value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002436 if (override == 1)
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002437 err = insertdict(mp, key, hash, value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002438 else if (_PyDict_GetItem_KnownHash(a, key, hash) == NULL) {
2439 if (PyErr_Occurred()) {
2440 Py_DECREF(value);
2441 Py_DECREF(key);
2442 return -1;
2443 }
2444 err = insertdict(mp, key, hash, value);
2445 }
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002446 else if (override != 0) {
2447 _PyErr_SetKeyError(key);
2448 Py_DECREF(value);
2449 Py_DECREF(key);
2450 return -1;
2451 }
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002452 Py_DECREF(value);
2453 Py_DECREF(key);
2454 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002455 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002456
Victor Stinner742da042016-09-07 17:40:12 -07002457 if (n != other->ma_keys->dk_nentries) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002458 PyErr_SetString(PyExc_RuntimeError,
2459 "dict mutated during update");
2460 return -1;
2461 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002462 }
2463 }
2464 }
2465 else {
2466 /* Do it the generic, slower way */
2467 PyObject *keys = PyMapping_Keys(b);
2468 PyObject *iter;
2469 PyObject *key, *value;
2470 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002472 if (keys == NULL)
2473 /* Docstring says this is equivalent to E.keys() so
2474 * if E doesn't have a .keys() method we want
2475 * AttributeError to percolate up. Might as well
2476 * do the same for any other error.
2477 */
2478 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002480 iter = PyObject_GetIter(keys);
2481 Py_DECREF(keys);
2482 if (iter == NULL)
2483 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002485 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002486 if (override != 1 && PyDict_GetItem(a, key) != NULL) {
2487 if (override != 0) {
2488 _PyErr_SetKeyError(key);
2489 Py_DECREF(key);
2490 Py_DECREF(iter);
2491 return -1;
2492 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002493 Py_DECREF(key);
2494 continue;
2495 }
2496 value = PyObject_GetItem(b, key);
2497 if (value == NULL) {
2498 Py_DECREF(iter);
2499 Py_DECREF(key);
2500 return -1;
2501 }
2502 status = PyDict_SetItem(a, key, value);
2503 Py_DECREF(key);
2504 Py_DECREF(value);
2505 if (status < 0) {
2506 Py_DECREF(iter);
2507 return -1;
2508 }
2509 }
2510 Py_DECREF(iter);
2511 if (PyErr_Occurred())
2512 /* Iterator completed, via error */
2513 return -1;
2514 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02002515 assert(_PyDict_CheckConsistency((PyDictObject *)a));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002516 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002517}
2518
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002519int
2520PyDict_Update(PyObject *a, PyObject *b)
2521{
2522 return dict_merge(a, b, 1);
2523}
2524
2525int
2526PyDict_Merge(PyObject *a, PyObject *b, int override)
2527{
2528 /* XXX Deprecate override not in (0, 1). */
2529 return dict_merge(a, b, override != 0);
2530}
2531
2532int
2533_PyDict_MergeEx(PyObject *a, PyObject *b, int override)
2534{
2535 return dict_merge(a, b, override);
2536}
2537
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002538static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002539dict_copy(PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002540{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002541 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002542}
2543
2544PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002545PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002546{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002547 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002548 PyDictObject *mp;
2549 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002551 if (o == NULL || !PyDict_Check(o)) {
2552 PyErr_BadInternalCall();
2553 return NULL;
2554 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002555 mp = (PyDictObject *)o;
2556 if (_PyDict_HasSplitTable(mp)) {
2557 PyDictObject *split_copy;
Victor Stinner742da042016-09-07 17:40:12 -07002558 Py_ssize_t size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
2559 PyObject **newvalues;
2560 newvalues = new_values(size);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002561 if (newvalues == NULL)
2562 return PyErr_NoMemory();
2563 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2564 if (split_copy == NULL) {
2565 free_values(newvalues);
2566 return NULL;
2567 }
2568 split_copy->ma_values = newvalues;
2569 split_copy->ma_keys = mp->ma_keys;
2570 split_copy->ma_used = mp->ma_used;
2571 DK_INCREF(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002572 for (i = 0, n = size; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002573 PyObject *value = mp->ma_values[i];
2574 Py_XINCREF(value);
2575 split_copy->ma_values[i] = value;
2576 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002577 if (_PyObject_GC_IS_TRACKED(mp))
2578 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002579 return (PyObject *)split_copy;
2580 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002581 copy = PyDict_New();
2582 if (copy == NULL)
2583 return NULL;
2584 if (PyDict_Merge(copy, o, 1) == 0)
2585 return copy;
2586 Py_DECREF(copy);
2587 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002588}
2589
Martin v. Löwis18e16552006-02-15 17:27:45 +00002590Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002591PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002592{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002593 if (mp == NULL || !PyDict_Check(mp)) {
2594 PyErr_BadInternalCall();
2595 return -1;
2596 }
2597 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002598}
2599
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002600PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002601PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002602{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002603 if (mp == NULL || !PyDict_Check(mp)) {
2604 PyErr_BadInternalCall();
2605 return NULL;
2606 }
2607 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002608}
2609
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002610PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002611PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002612{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002613 if (mp == NULL || !PyDict_Check(mp)) {
2614 PyErr_BadInternalCall();
2615 return NULL;
2616 }
2617 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002618}
2619
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002620PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002621PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002622{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002623 if (mp == NULL || !PyDict_Check(mp)) {
2624 PyErr_BadInternalCall();
2625 return NULL;
2626 }
2627 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002628}
2629
Tim Peterse63415e2001-05-08 04:38:29 +00002630/* Return 1 if dicts equal, 0 if not, -1 if error.
2631 * Gets out as soon as any difference is detected.
2632 * Uses only Py_EQ comparison.
2633 */
2634static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002635dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002636{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002637 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002639 if (a->ma_used != b->ma_used)
2640 /* can't be equal if # of entries differ */
2641 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002642 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Victor Stinner742da042016-09-07 17:40:12 -07002643 for (i = 0; i < a->ma_keys->dk_nentries; i++) {
2644 PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002645 PyObject *aval;
2646 if (a->ma_values)
2647 aval = a->ma_values[i];
2648 else
2649 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002650 if (aval != NULL) {
2651 int cmp;
2652 PyObject *bval;
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002653 PyObject **vaddr;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002654 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002655 /* temporarily bump aval's refcount to ensure it stays
2656 alive until we're done with it */
2657 Py_INCREF(aval);
2658 /* ditto for key */
2659 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002660 /* reuse the known hash value */
Victor Stinner742da042016-09-07 17:40:12 -07002661 if ((b->ma_keys->dk_lookup)(b, key, ep->me_hash, &vaddr, NULL) < 0)
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002662 bval = NULL;
2663 else
2664 bval = *vaddr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002665 Py_DECREF(key);
2666 if (bval == NULL) {
2667 Py_DECREF(aval);
2668 if (PyErr_Occurred())
2669 return -1;
2670 return 0;
2671 }
2672 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
2673 Py_DECREF(aval);
2674 if (cmp <= 0) /* error or not equal */
2675 return cmp;
2676 }
2677 }
2678 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002679}
Tim Peterse63415e2001-05-08 04:38:29 +00002680
2681static PyObject *
2682dict_richcompare(PyObject *v, PyObject *w, int op)
2683{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002684 int cmp;
2685 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002686
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002687 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2688 res = Py_NotImplemented;
2689 }
2690 else if (op == Py_EQ || op == Py_NE) {
2691 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2692 if (cmp < 0)
2693 return NULL;
2694 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2695 }
2696 else
2697 res = Py_NotImplemented;
2698 Py_INCREF(res);
2699 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002700}
Tim Peterse63415e2001-05-08 04:38:29 +00002701
Larry Hastings61272b72014-01-07 12:41:53 -08002702/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002703
2704@coexist
2705dict.__contains__
2706
2707 key: object
2708 /
2709
Meador Ingee02de8c2014-01-14 16:48:31 -06002710True if D has a key k, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002711[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002712
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002713static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002714dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002715/*[clinic end generated code: output=a3d03db709ed6e6b input=b852b2a19b51ab24]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002716{
Larry Hastingsc2047262014-01-25 20:43:29 -08002717 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002718 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002719 Py_ssize_t ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002720 PyObject **value_addr;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002722 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002723 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002724 hash = PyObject_Hash(key);
2725 if (hash == -1)
2726 return NULL;
2727 }
Victor Stinner742da042016-09-07 17:40:12 -07002728 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
2729 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002730 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002731 if (ix == DKIX_EMPTY || *value_addr == NULL)
2732 Py_RETURN_FALSE;
2733 Py_RETURN_TRUE;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002734}
2735
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002736static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002737dict_get(PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00002738{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002739 PyObject *key;
2740 PyObject *failobj = Py_None;
2741 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002742 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002743 Py_ssize_t ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002744 PyObject **value_addr;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002746 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
2747 return NULL;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002748
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002749 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002750 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 hash = PyObject_Hash(key);
2752 if (hash == -1)
2753 return NULL;
2754 }
Victor Stinner742da042016-09-07 17:40:12 -07002755 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
2756 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002758 if (ix == DKIX_EMPTY || *value_addr == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002759 val = failobj;
Victor Stinner742da042016-09-07 17:40:12 -07002760 else
2761 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002762 Py_INCREF(val);
2763 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002764}
2765
Benjamin Peterson00e98862013-03-07 22:16:29 -05002766PyObject *
2767PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002768{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002769 PyDictObject *mp = (PyDictObject *)d;
INADA Naoki93f26f72016-11-02 18:45:16 +09002770 PyObject *value;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002771 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002772 Py_ssize_t hashpos, ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002773 PyObject **value_addr;
Guido van Rossum164452c2000-08-08 16:12:54 +00002774
Benjamin Peterson00e98862013-03-07 22:16:29 -05002775 if (!PyDict_Check(d)) {
2776 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002777 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002778 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002780 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002781 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002782 hash = PyObject_Hash(key);
2783 if (hash == -1)
2784 return NULL;
2785 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002786
2787 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
2788 if (insertion_resize(mp) < 0)
2789 return NULL;
2790 }
2791
Victor Stinner742da042016-09-07 17:40:12 -07002792 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
2793 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002794 return NULL;
INADA Naoki93f26f72016-11-02 18:45:16 +09002795
2796 if (_PyDict_HasSplitTable(mp) &&
2797 ((ix >= 0 && *value_addr == NULL && mp->ma_used != ix) ||
2798 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
2799 if (insertion_resize(mp) < 0) {
2800 return NULL;
2801 }
2802 find_empty_slot(mp, key, hash, &value_addr, &hashpos);
2803 ix = DKIX_EMPTY;
2804 }
2805
2806 if (ix == DKIX_EMPTY) {
2807 PyDictKeyEntry *ep, *ep0;
2808 value = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002809 if (mp->ma_keys->dk_usable <= 0) {
Victor Stinner3c336c52016-09-12 14:17:40 +02002810 if (insertion_resize(mp) < 0) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002811 return NULL;
Victor Stinner3c336c52016-09-12 14:17:40 +02002812 }
Victor Stinner742da042016-09-07 17:40:12 -07002813 find_empty_slot(mp, key, hash, &value_addr, &hashpos);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002814 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002815 ep0 = DK_ENTRIES(mp->ma_keys);
2816 ep = &ep0[mp->ma_keys->dk_nentries];
2817 dk_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002818 Py_INCREF(key);
INADA Naoki93f26f72016-11-02 18:45:16 +09002819 Py_INCREF(value);
2820 MAINTAIN_TRACKING(mp, key, value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002821 ep->me_key = key;
2822 ep->me_hash = hash;
Victor Stinner742da042016-09-07 17:40:12 -07002823 if (mp->ma_values) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002824 assert(mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
2825 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Victor Stinner742da042016-09-07 17:40:12 -07002826 }
2827 else {
INADA Naoki93f26f72016-11-02 18:45:16 +09002828 ep->me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07002829 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002830 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002831 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naoki93f26f72016-11-02 18:45:16 +09002832 mp->ma_keys->dk_usable--;
2833 mp->ma_keys->dk_nentries++;
2834 assert(mp->ma_keys->dk_usable >= 0);
2835 }
2836 else if (*value_addr == NULL) {
2837 value = defaultobj;
2838 assert(_PyDict_HasSplitTable(mp));
2839 assert(ix == mp->ma_used);
2840 Py_INCREF(value);
2841 MAINTAIN_TRACKING(mp, key, value);
2842 *value_addr = value;
2843 mp->ma_used++;
2844 mp->ma_version_tag = DICT_NEXT_VERSION();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002845 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02002846 else {
INADA Naoki93f26f72016-11-02 18:45:16 +09002847 value = *value_addr;
Victor Stinner611b0fa2016-09-14 15:02:01 +02002848 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002849
2850 assert(_PyDict_CheckConsistency(mp));
2851 return value;
Guido van Rossum164452c2000-08-08 16:12:54 +00002852}
2853
Benjamin Peterson00e98862013-03-07 22:16:29 -05002854static PyObject *
2855dict_setdefault(PyDictObject *mp, PyObject *args)
2856{
2857 PyObject *key, *val;
2858 PyObject *defaultobj = Py_None;
2859
2860 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &defaultobj))
2861 return NULL;
2862
Benjamin Peterson55898502013-03-08 08:36:49 -05002863 val = PyDict_SetDefault((PyObject *)mp, key, defaultobj);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002864 Py_XINCREF(val);
2865 return val;
2866}
Guido van Rossum164452c2000-08-08 16:12:54 +00002867
2868static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002869dict_clear(PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002870{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002871 PyDict_Clear((PyObject *)mp);
2872 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002873}
2874
Guido van Rossumba6ab842000-12-12 22:02:18 +00002875static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002876dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002877{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002878 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00002879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2881 return NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06002882
Yury Selivanov684ef2c2016-10-28 19:01:21 -04002883 return _PyDict_Pop((PyObject*)mp, key, deflt);
Guido van Rossume027d982002-04-12 15:11:59 +00002884}
2885
2886static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002887dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002888{
Victor Stinner742da042016-09-07 17:40:12 -07002889 Py_ssize_t i, j;
2890 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002891 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002893 /* Allocate the result tuple before checking the size. Believe it
2894 * or not, this allocation could trigger a garbage collection which
2895 * could empty the dict, so if we checked the size first and that
2896 * happened, the result would be an infinite loop (searching for an
2897 * entry that no longer exists). Note that the usual popitem()
2898 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2899 * tuple away if the dict *is* empty isn't a significant
2900 * inefficiency -- possible, but unlikely in practice.
2901 */
2902 res = PyTuple_New(2);
2903 if (res == NULL)
2904 return NULL;
2905 if (mp->ma_used == 0) {
2906 Py_DECREF(res);
2907 PyErr_SetString(PyExc_KeyError,
2908 "popitem(): dictionary is empty");
2909 return NULL;
2910 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002911 /* Convert split table to combined table */
2912 if (mp->ma_keys->dk_lookup == lookdict_split) {
2913 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2914 Py_DECREF(res);
2915 return NULL;
2916 }
2917 }
2918 ENSURE_ALLOWS_DELETIONS(mp);
Victor Stinner742da042016-09-07 17:40:12 -07002919
2920 /* Pop last item */
2921 ep0 = DK_ENTRIES(mp->ma_keys);
2922 i = mp->ma_keys->dk_nentries - 1;
2923 while (i >= 0 && ep0[i].me_value == NULL) {
2924 i--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002925 }
Victor Stinner742da042016-09-07 17:40:12 -07002926 assert(i >= 0);
2927
2928 ep = &ep0[i];
2929 j = lookdict_index(mp->ma_keys, ep->me_hash, i);
2930 assert(j >= 0);
2931 assert(dk_get_index(mp->ma_keys, j) == i);
2932 dk_set_index(mp->ma_keys, j, DKIX_DUMMY);
2933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002934 PyTuple_SET_ITEM(res, 0, ep->me_key);
2935 PyTuple_SET_ITEM(res, 1, ep->me_value);
Victor Stinner742da042016-09-07 17:40:12 -07002936 ep->me_key = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002937 ep->me_value = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002938 /* We can't dk_usable++ since there is DKIX_DUMMY in indices */
2939 mp->ma_keys->dk_nentries = i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002940 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002941 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02002942 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002943 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002944}
2945
Jeremy Hylton8caad492000-06-23 14:18:11 +00002946static int
2947dict_traverse(PyObject *op, visitproc visit, void *arg)
2948{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002949 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson55f44522016-09-05 12:12:59 -07002950 PyDictKeysObject *keys = mp->ma_keys;
Serhiy Storchaka46825d22016-09-26 21:29:34 +03002951 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Victor Stinner742da042016-09-07 17:40:12 -07002952 Py_ssize_t i, n = keys->dk_nentries;
2953
Benjamin Peterson55f44522016-09-05 12:12:59 -07002954 if (keys->dk_lookup == lookdict) {
2955 for (i = 0; i < n; i++) {
2956 if (entries[i].me_value != NULL) {
2957 Py_VISIT(entries[i].me_value);
2958 Py_VISIT(entries[i].me_key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002959 }
2960 }
Victor Stinner742da042016-09-07 17:40:12 -07002961 }
2962 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002963 if (mp->ma_values != NULL) {
Benjamin Peterson55f44522016-09-05 12:12:59 -07002964 for (i = 0; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002965 Py_VISIT(mp->ma_values[i]);
2966 }
2967 }
2968 else {
Benjamin Peterson55f44522016-09-05 12:12:59 -07002969 for (i = 0; i < n; i++) {
2970 Py_VISIT(entries[i].me_value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002971 }
2972 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002973 }
2974 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002975}
2976
2977static int
2978dict_tp_clear(PyObject *op)
2979{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002980 PyDict_Clear(op);
2981 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002982}
2983
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002984static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002985
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002986Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06002987_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002988{
Victor Stinner742da042016-09-07 17:40:12 -07002989 Py_ssize_t size, usable, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002990
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002991 size = DK_SIZE(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002992 usable = USABLE_FRACTION(size);
2993
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002994 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002995 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -07002996 res += usable * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002997 /* If the dictionary is split, the keys portion is accounted-for
2998 in the type object. */
2999 if (mp->ma_keys->dk_refcnt == 1)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003000 res += (sizeof(PyDictKeysObject)
3001 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
3002 + DK_IXSIZE(mp->ma_keys) * size
3003 + sizeof(PyDictKeyEntry) * usable);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003004 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003005}
3006
3007Py_ssize_t
3008_PyDict_KeysSize(PyDictKeysObject *keys)
3009{
Victor Stinner98ee9d52016-09-08 09:33:56 -07003010 return (sizeof(PyDictKeysObject)
3011 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
3012 + DK_IXSIZE(keys) * DK_SIZE(keys)
3013 + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003014}
3015
doko@ubuntu.com17210f52016-01-14 14:04:59 +01003016static PyObject *
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003017dict_sizeof(PyDictObject *mp)
3018{
3019 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
3020}
3021
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00003022PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
3023
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003024PyDoc_STRVAR(sizeof__doc__,
3025"D.__sizeof__() -> size of D in memory, in bytes");
3026
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003027PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00003028"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003029
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003030PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00003031"D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D");
Tim Petersf7f88b12000-12-13 23:18:45 +00003032
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003033PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00003034"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00003035If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00003036
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003037PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00003038"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000030392-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003040
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003041PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04003042"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
3043If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
3044If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
3045In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00003046
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003047PyDoc_STRVAR(clear__doc__,
3048"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003049
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003050PyDoc_STRVAR(copy__doc__,
3051"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00003052
Guido van Rossumb90c8482007-02-10 01:11:45 +00003053/* Forward */
3054static PyObject *dictkeys_new(PyObject *);
3055static PyObject *dictitems_new(PyObject *);
3056static PyObject *dictvalues_new(PyObject *);
3057
Guido van Rossum45c85d12007-07-27 16:31:40 +00003058PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003059 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003060PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003061 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003062PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003063 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00003064
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003065static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07003066 DICT___CONTAINS___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003067 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
3068 getitem__doc__},
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003069 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003070 sizeof__doc__},
3071 {"get", (PyCFunction)dict_get, METH_VARARGS,
3072 get__doc__},
3073 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
3074 setdefault_doc__},
3075 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
3076 pop__doc__},
3077 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
3078 popitem__doc__},
3079 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
3080 keys__doc__},
3081 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
3082 items__doc__},
3083 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
3084 values__doc__},
3085 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
3086 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08003087 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003088 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
3089 clear__doc__},
3090 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
3091 copy__doc__},
3092 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003093};
3094
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003095/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00003096int
3097PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003098{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003099 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07003100 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003101 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003102 PyObject **value_addr;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003104 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003105 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003106 hash = PyObject_Hash(key);
3107 if (hash == -1)
3108 return -1;
3109 }
Victor Stinner742da042016-09-07 17:40:12 -07003110 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
3111 if (ix == DKIX_ERROR)
3112 return -1;
3113 return (ix != DKIX_EMPTY && *value_addr != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003114}
3115
Thomas Wouterscf297e42007-02-23 15:07:44 +00003116/* Internal version of PyDict_Contains used when the hash value is already known */
3117int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003118_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00003119{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003120 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003121 PyObject **value_addr;
Victor Stinner742da042016-09-07 17:40:12 -07003122 Py_ssize_t ix;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003123
Victor Stinner742da042016-09-07 17:40:12 -07003124 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
3125 if (ix == DKIX_ERROR)
3126 return -1;
3127 return (ix != DKIX_EMPTY && *value_addr != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00003128}
3129
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003130/* Hack to implement "key in dict" */
3131static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003132 0, /* sq_length */
3133 0, /* sq_concat */
3134 0, /* sq_repeat */
3135 0, /* sq_item */
3136 0, /* sq_slice */
3137 0, /* sq_ass_item */
3138 0, /* sq_ass_slice */
3139 PyDict_Contains, /* sq_contains */
3140 0, /* sq_inplace_concat */
3141 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003142};
3143
Guido van Rossum09e563a2001-05-01 12:10:21 +00003144static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003145dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3146{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003147 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003148 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003150 assert(type != NULL && type->tp_alloc != NULL);
3151 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02003152 if (self == NULL)
3153 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003154 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003155
Victor Stinnera9f61a52013-07-16 22:17:26 +02003156 /* The object has been implicitly tracked by tp_alloc */
3157 if (type == &PyDict_Type)
3158 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003159
3160 d->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003161 d->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07003162 d->ma_keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003163 if (d->ma_keys == NULL) {
3164 Py_DECREF(self);
3165 return NULL;
3166 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02003167 assert(_PyDict_CheckConsistency(d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003168 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003169}
3170
Tim Peters25786c02001-09-02 08:22:48 +00003171static int
3172dict_init(PyObject *self, PyObject *args, PyObject *kwds)
3173{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003174 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00003175}
3176
Tim Peters6d6c1a32001-08-02 04:15:00 +00003177static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003178dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00003179{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003180 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003181}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003182
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003183PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00003184"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00003185"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003186" (key, value) pairs\n"
3187"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00003188" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003189" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00003190" d[k] = v\n"
3191"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
3192" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00003193
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003194PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003195 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3196 "dict",
3197 sizeof(PyDictObject),
3198 0,
3199 (destructor)dict_dealloc, /* tp_dealloc */
3200 0, /* tp_print */
3201 0, /* tp_getattr */
3202 0, /* tp_setattr */
3203 0, /* tp_reserved */
3204 (reprfunc)dict_repr, /* tp_repr */
3205 0, /* tp_as_number */
3206 &dict_as_sequence, /* tp_as_sequence */
3207 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003208 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003209 0, /* tp_call */
3210 0, /* tp_str */
3211 PyObject_GenericGetAttr, /* tp_getattro */
3212 0, /* tp_setattro */
3213 0, /* tp_as_buffer */
3214 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3215 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
3216 dictionary_doc, /* tp_doc */
3217 dict_traverse, /* tp_traverse */
3218 dict_tp_clear, /* tp_clear */
3219 dict_richcompare, /* tp_richcompare */
3220 0, /* tp_weaklistoffset */
3221 (getiterfunc)dict_iter, /* tp_iter */
3222 0, /* tp_iternext */
3223 mapp_methods, /* tp_methods */
3224 0, /* tp_members */
3225 0, /* tp_getset */
3226 0, /* tp_base */
3227 0, /* tp_dict */
3228 0, /* tp_descr_get */
3229 0, /* tp_descr_set */
3230 0, /* tp_dictoffset */
3231 dict_init, /* tp_init */
3232 PyType_GenericAlloc, /* tp_alloc */
3233 dict_new, /* tp_new */
3234 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003235};
3236
Victor Stinner3c1e4812012-03-26 22:10:51 +02003237PyObject *
3238_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
3239{
3240 PyObject *kv;
3241 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02003242 if (kv == NULL) {
3243 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003244 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02003245 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02003246 return PyDict_GetItem(dp, kv);
3247}
3248
Guido van Rossum3cca2451997-05-16 14:23:33 +00003249/* For backward compatibility with old dictionary interface */
3250
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003251PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003252PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003253{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003254 PyObject *kv, *rv;
3255 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003256 if (kv == NULL) {
3257 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003258 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003259 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003260 rv = PyDict_GetItem(v, kv);
3261 Py_DECREF(kv);
3262 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003263}
3264
3265int
Victor Stinner3c1e4812012-03-26 22:10:51 +02003266_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
3267{
3268 PyObject *kv;
3269 kv = _PyUnicode_FromId(key); /* borrowed */
3270 if (kv == NULL)
3271 return -1;
3272 return PyDict_SetItem(v, kv, item);
3273}
3274
3275int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003276PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003277{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003278 PyObject *kv;
3279 int err;
3280 kv = PyUnicode_FromString(key);
3281 if (kv == NULL)
3282 return -1;
3283 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
3284 err = PyDict_SetItem(v, kv, item);
3285 Py_DECREF(kv);
3286 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003287}
3288
3289int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01003290_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
3291{
3292 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3293 if (kv == NULL)
3294 return -1;
3295 return PyDict_DelItem(v, kv);
3296}
3297
3298int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003299PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003300{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003301 PyObject *kv;
3302 int err;
3303 kv = PyUnicode_FromString(key);
3304 if (kv == NULL)
3305 return -1;
3306 err = PyDict_DelItem(v, kv);
3307 Py_DECREF(kv);
3308 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003309}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003310
Raymond Hettinger019a1482004-03-18 02:41:19 +00003311/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003312
3313typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003314 PyObject_HEAD
3315 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
3316 Py_ssize_t di_used;
3317 Py_ssize_t di_pos;
3318 PyObject* di_result; /* reusable result tuple for iteritems */
3319 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003320} dictiterobject;
3321
3322static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003323dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003324{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003325 dictiterobject *di;
3326 di = PyObject_GC_New(dictiterobject, itertype);
3327 if (di == NULL)
3328 return NULL;
3329 Py_INCREF(dict);
3330 di->di_dict = dict;
3331 di->di_used = dict->ma_used;
3332 di->di_pos = 0;
3333 di->len = dict->ma_used;
3334 if (itertype == &PyDictIterItem_Type) {
3335 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
3336 if (di->di_result == NULL) {
3337 Py_DECREF(di);
3338 return NULL;
3339 }
3340 }
3341 else
3342 di->di_result = NULL;
3343 _PyObject_GC_TRACK(di);
3344 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003345}
3346
3347static void
3348dictiter_dealloc(dictiterobject *di)
3349{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003350 Py_XDECREF(di->di_dict);
3351 Py_XDECREF(di->di_result);
3352 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003353}
3354
3355static int
3356dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
3357{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003358 Py_VISIT(di->di_dict);
3359 Py_VISIT(di->di_result);
3360 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003361}
3362
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003363static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003364dictiter_len(dictiterobject *di)
3365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003366 Py_ssize_t len = 0;
3367 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
3368 len = di->len;
3369 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003370}
3371
Guido van Rossumb90c8482007-02-10 01:11:45 +00003372PyDoc_STRVAR(length_hint_doc,
3373 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003374
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003375static PyObject *
3376dictiter_reduce(dictiterobject *di);
3377
3378PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3379
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003380static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003381 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
3382 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003383 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
3384 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003385 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003386};
3387
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003388static PyObject*
3389dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00003390{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003391 PyObject *key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003392 Py_ssize_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003393 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003394 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00003395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003396 if (d == NULL)
3397 return NULL;
3398 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00003399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003400 if (di->di_used != d->ma_used) {
3401 PyErr_SetString(PyExc_RuntimeError,
3402 "dictionary changed size during iteration");
3403 di->di_used = -1; /* Make this state sticky */
3404 return NULL;
3405 }
Guido van Rossum2147df72002-07-16 20:30:22 +00003406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003407 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003408 k = d->ma_keys;
INADA Naokica2d8be2016-11-04 16:59:10 +09003409 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003410 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003411 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003412 goto fail;
3413 key = DK_ENTRIES(k)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003414 assert(d->ma_values[i] != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003415 }
3416 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003417 Py_ssize_t n = k->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003418 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3419 while (i < n && entry_ptr->me_value == NULL) {
3420 entry_ptr++;
3421 i++;
3422 }
3423 if (i >= n)
3424 goto fail;
3425 key = entry_ptr->me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003426 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003427 di->di_pos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003428 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003429 Py_INCREF(key);
3430 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003431
3432fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003433 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003434 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003435 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003436}
3437
Raymond Hettinger019a1482004-03-18 02:41:19 +00003438PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003439 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3440 "dict_keyiterator", /* tp_name */
3441 sizeof(dictiterobject), /* tp_basicsize */
3442 0, /* tp_itemsize */
3443 /* methods */
3444 (destructor)dictiter_dealloc, /* tp_dealloc */
3445 0, /* tp_print */
3446 0, /* tp_getattr */
3447 0, /* tp_setattr */
3448 0, /* tp_reserved */
3449 0, /* tp_repr */
3450 0, /* tp_as_number */
3451 0, /* tp_as_sequence */
3452 0, /* tp_as_mapping */
3453 0, /* tp_hash */
3454 0, /* tp_call */
3455 0, /* tp_str */
3456 PyObject_GenericGetAttr, /* tp_getattro */
3457 0, /* tp_setattro */
3458 0, /* tp_as_buffer */
3459 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3460 0, /* tp_doc */
3461 (traverseproc)dictiter_traverse, /* tp_traverse */
3462 0, /* tp_clear */
3463 0, /* tp_richcompare */
3464 0, /* tp_weaklistoffset */
3465 PyObject_SelfIter, /* tp_iter */
3466 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3467 dictiter_methods, /* tp_methods */
3468 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003469};
3470
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003471static PyObject *
3472dictiter_iternextvalue(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003473{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003474 PyObject *value;
INADA Naokica2d8be2016-11-04 16:59:10 +09003475 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003476 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003478 if (d == NULL)
3479 return NULL;
3480 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003482 if (di->di_used != d->ma_used) {
3483 PyErr_SetString(PyExc_RuntimeError,
3484 "dictionary changed size during iteration");
3485 di->di_used = -1; /* Make this state sticky */
3486 return NULL;
3487 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003489 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003490 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003491 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003492 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003493 goto fail;
INADA Naokica2d8be2016-11-04 16:59:10 +09003494 value = d->ma_values[i];
3495 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003496 }
3497 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003498 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003499 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3500 while (i < n && entry_ptr->me_value == NULL) {
3501 entry_ptr++;
3502 i++;
3503 }
3504 if (i >= n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003505 goto fail;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003506 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003507 }
3508 di->di_pos = i+1;
3509 di->len--;
3510 Py_INCREF(value);
3511 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003512
3513fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003514 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003515 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003516 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003517}
3518
3519PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003520 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3521 "dict_valueiterator", /* tp_name */
3522 sizeof(dictiterobject), /* tp_basicsize */
3523 0, /* tp_itemsize */
3524 /* methods */
3525 (destructor)dictiter_dealloc, /* tp_dealloc */
3526 0, /* tp_print */
3527 0, /* tp_getattr */
3528 0, /* tp_setattr */
3529 0, /* tp_reserved */
3530 0, /* tp_repr */
3531 0, /* tp_as_number */
3532 0, /* tp_as_sequence */
3533 0, /* tp_as_mapping */
3534 0, /* tp_hash */
3535 0, /* tp_call */
3536 0, /* tp_str */
3537 PyObject_GenericGetAttr, /* tp_getattro */
3538 0, /* tp_setattro */
3539 0, /* tp_as_buffer */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003540 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003541 0, /* tp_doc */
3542 (traverseproc)dictiter_traverse, /* tp_traverse */
3543 0, /* tp_clear */
3544 0, /* tp_richcompare */
3545 0, /* tp_weaklistoffset */
3546 PyObject_SelfIter, /* tp_iter */
3547 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3548 dictiter_methods, /* tp_methods */
3549 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003550};
3551
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003552static PyObject *
3553dictiter_iternextitem(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003554{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003555 PyObject *key, *value, *result = di->di_result;
INADA Naokica2d8be2016-11-04 16:59:10 +09003556 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003557 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003559 if (d == NULL)
3560 return NULL;
3561 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003563 if (di->di_used != d->ma_used) {
3564 PyErr_SetString(PyExc_RuntimeError,
3565 "dictionary changed size during iteration");
3566 di->di_used = -1; /* Make this state sticky */
3567 return NULL;
3568 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003570 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003571 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003572 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003573 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003574 goto fail;
3575 key = DK_ENTRIES(d->ma_keys)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003576 value = d->ma_values[i];
3577 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003578 }
3579 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003580 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003581 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3582 while (i < n && entry_ptr->me_value == NULL) {
3583 entry_ptr++;
3584 i++;
3585 }
3586 if (i >= n)
3587 goto fail;
3588 key = entry_ptr->me_key;
3589 value = entry_ptr->me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003590 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003591 di->di_pos = i+1;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003592 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003593 if (result->ob_refcnt == 1) {
3594 Py_INCREF(result);
3595 Py_DECREF(PyTuple_GET_ITEM(result, 0));
3596 Py_DECREF(PyTuple_GET_ITEM(result, 1));
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003597 }
3598 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003599 result = PyTuple_New(2);
3600 if (result == NULL)
3601 return NULL;
3602 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003603 Py_INCREF(key);
3604 Py_INCREF(value);
Eric Snow96c6af92015-05-29 22:21:39 -06003605 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3606 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003607 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003608
3609fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003610 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003611 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003612 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003613}
3614
3615PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003616 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3617 "dict_itemiterator", /* tp_name */
3618 sizeof(dictiterobject), /* tp_basicsize */
3619 0, /* tp_itemsize */
3620 /* methods */
3621 (destructor)dictiter_dealloc, /* tp_dealloc */
3622 0, /* tp_print */
3623 0, /* tp_getattr */
3624 0, /* tp_setattr */
3625 0, /* tp_reserved */
3626 0, /* tp_repr */
3627 0, /* tp_as_number */
3628 0, /* tp_as_sequence */
3629 0, /* tp_as_mapping */
3630 0, /* tp_hash */
3631 0, /* tp_call */
3632 0, /* tp_str */
3633 PyObject_GenericGetAttr, /* tp_getattro */
3634 0, /* tp_setattro */
3635 0, /* tp_as_buffer */
3636 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3637 0, /* tp_doc */
3638 (traverseproc)dictiter_traverse, /* tp_traverse */
3639 0, /* tp_clear */
3640 0, /* tp_richcompare */
3641 0, /* tp_weaklistoffset */
3642 PyObject_SelfIter, /* tp_iter */
3643 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3644 dictiter_methods, /* tp_methods */
3645 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003646};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003647
3648
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003649static PyObject *
3650dictiter_reduce(dictiterobject *di)
3651{
3652 PyObject *list;
3653 dictiterobject tmp;
3654
3655 list = PyList_New(0);
3656 if (!list)
3657 return NULL;
3658
3659 /* copy the itertor state */
3660 tmp = *di;
3661 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003662
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003663 /* iterate the temporary into a list */
3664 for(;;) {
3665 PyObject *element = 0;
3666 if (Py_TYPE(di) == &PyDictIterItem_Type)
3667 element = dictiter_iternextitem(&tmp);
3668 else if (Py_TYPE(di) == &PyDictIterKey_Type)
3669 element = dictiter_iternextkey(&tmp);
3670 else if (Py_TYPE(di) == &PyDictIterValue_Type)
3671 element = dictiter_iternextvalue(&tmp);
3672 else
3673 assert(0);
3674 if (element) {
3675 if (PyList_Append(list, element)) {
3676 Py_DECREF(element);
3677 Py_DECREF(list);
3678 Py_XDECREF(tmp.di_dict);
3679 return NULL;
3680 }
3681 Py_DECREF(element);
3682 } else
3683 break;
3684 }
3685 Py_XDECREF(tmp.di_dict);
3686 /* check for error */
3687 if (tmp.di_dict != NULL) {
3688 /* we have an error */
3689 Py_DECREF(list);
3690 return NULL;
3691 }
Antoine Pitroua7013882012-04-05 00:04:20 +02003692 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003693}
3694
Guido van Rossum3ac67412007-02-10 18:55:06 +00003695/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003696/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003697/***********************************************/
3698
Guido van Rossumb90c8482007-02-10 01:11:45 +00003699/* The instance lay-out is the same for all three; but the type differs. */
3700
Guido van Rossumb90c8482007-02-10 01:11:45 +00003701static void
Eric Snow96c6af92015-05-29 22:21:39 -06003702dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003703{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003704 Py_XDECREF(dv->dv_dict);
3705 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003706}
3707
3708static int
Eric Snow96c6af92015-05-29 22:21:39 -06003709dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003710{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003711 Py_VISIT(dv->dv_dict);
3712 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003713}
3714
Guido van Rossum83825ac2007-02-10 04:54:19 +00003715static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003716dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003718 Py_ssize_t len = 0;
3719 if (dv->dv_dict != NULL)
3720 len = dv->dv_dict->ma_used;
3721 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003722}
3723
Eric Snow96c6af92015-05-29 22:21:39 -06003724PyObject *
3725_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003726{
Eric Snow96c6af92015-05-29 22:21:39 -06003727 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003728 if (dict == NULL) {
3729 PyErr_BadInternalCall();
3730 return NULL;
3731 }
3732 if (!PyDict_Check(dict)) {
3733 /* XXX Get rid of this restriction later */
3734 PyErr_Format(PyExc_TypeError,
3735 "%s() requires a dict argument, not '%s'",
3736 type->tp_name, dict->ob_type->tp_name);
3737 return NULL;
3738 }
Eric Snow96c6af92015-05-29 22:21:39 -06003739 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003740 if (dv == NULL)
3741 return NULL;
3742 Py_INCREF(dict);
3743 dv->dv_dict = (PyDictObject *)dict;
3744 _PyObject_GC_TRACK(dv);
3745 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003746}
3747
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003748/* TODO(guido): The views objects are not complete:
3749
3750 * support more set operations
3751 * support arbitrary mappings?
3752 - either these should be static or exported in dictobject.h
3753 - if public then they should probably be in builtins
3754*/
3755
Guido van Rossumaac530c2007-08-24 22:33:45 +00003756/* Return 1 if self is a subset of other, iterating over self;
3757 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003758static int
3759all_contained_in(PyObject *self, PyObject *other)
3760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003761 PyObject *iter = PyObject_GetIter(self);
3762 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003764 if (iter == NULL)
3765 return -1;
3766 for (;;) {
3767 PyObject *next = PyIter_Next(iter);
3768 if (next == NULL) {
3769 if (PyErr_Occurred())
3770 ok = -1;
3771 break;
3772 }
3773 ok = PySequence_Contains(other, next);
3774 Py_DECREF(next);
3775 if (ok <= 0)
3776 break;
3777 }
3778 Py_DECREF(iter);
3779 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003780}
3781
3782static PyObject *
3783dictview_richcompare(PyObject *self, PyObject *other, int op)
3784{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003785 Py_ssize_t len_self, len_other;
3786 int ok;
3787 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003788
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003789 assert(self != NULL);
3790 assert(PyDictViewSet_Check(self));
3791 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003792
Brian Curtindfc80e32011-08-10 20:28:54 -05003793 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3794 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003796 len_self = PyObject_Size(self);
3797 if (len_self < 0)
3798 return NULL;
3799 len_other = PyObject_Size(other);
3800 if (len_other < 0)
3801 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003803 ok = 0;
3804 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003806 case Py_NE:
3807 case Py_EQ:
3808 if (len_self == len_other)
3809 ok = all_contained_in(self, other);
3810 if (op == Py_NE && ok >= 0)
3811 ok = !ok;
3812 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003814 case Py_LT:
3815 if (len_self < len_other)
3816 ok = all_contained_in(self, other);
3817 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003819 case Py_LE:
3820 if (len_self <= len_other)
3821 ok = all_contained_in(self, other);
3822 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003824 case Py_GT:
3825 if (len_self > len_other)
3826 ok = all_contained_in(other, self);
3827 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003829 case Py_GE:
3830 if (len_self >= len_other)
3831 ok = all_contained_in(other, self);
3832 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003834 }
3835 if (ok < 0)
3836 return NULL;
3837 result = ok ? Py_True : Py_False;
3838 Py_INCREF(result);
3839 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003840}
3841
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003842static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003843dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003844{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003845 PyObject *seq;
3846 PyObject *result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003848 seq = PySequence_List((PyObject *)dv);
3849 if (seq == NULL)
3850 return NULL;
3851
3852 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
3853 Py_DECREF(seq);
3854 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003855}
3856
Guido van Rossum3ac67412007-02-10 18:55:06 +00003857/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003858
3859static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003860dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003861{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003862 if (dv->dv_dict == NULL) {
3863 Py_RETURN_NONE;
3864 }
3865 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003866}
3867
3868static int
Eric Snow96c6af92015-05-29 22:21:39 -06003869dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00003870{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003871 if (dv->dv_dict == NULL)
3872 return 0;
3873 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003874}
3875
Guido van Rossum83825ac2007-02-10 04:54:19 +00003876static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003877 (lenfunc)dictview_len, /* sq_length */
3878 0, /* sq_concat */
3879 0, /* sq_repeat */
3880 0, /* sq_item */
3881 0, /* sq_slice */
3882 0, /* sq_ass_item */
3883 0, /* sq_ass_slice */
3884 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003885};
3886
Guido van Rossum523259b2007-08-24 23:41:22 +00003887static PyObject*
3888dictviews_sub(PyObject* self, PyObject *other)
3889{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003890 PyObject *result = PySet_New(self);
3891 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003892 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003894 if (result == NULL)
3895 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003896
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003897 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003898 if (tmp == NULL) {
3899 Py_DECREF(result);
3900 return NULL;
3901 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003903 Py_DECREF(tmp);
3904 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003905}
3906
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003907PyObject*
3908_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00003909{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003910 PyObject *result = PySet_New(self);
3911 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003912 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003914 if (result == NULL)
3915 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003916
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003917 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_intersection_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003918 if (tmp == NULL) {
3919 Py_DECREF(result);
3920 return NULL;
3921 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003923 Py_DECREF(tmp);
3924 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003925}
3926
3927static PyObject*
3928dictviews_or(PyObject* self, PyObject *other)
3929{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003930 PyObject *result = PySet_New(self);
3931 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003932 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003934 if (result == NULL)
3935 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003936
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003937 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003938 if (tmp == NULL) {
3939 Py_DECREF(result);
3940 return NULL;
3941 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003943 Py_DECREF(tmp);
3944 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003945}
3946
3947static PyObject*
3948dictviews_xor(PyObject* self, PyObject *other)
3949{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003950 PyObject *result = PySet_New(self);
3951 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003952 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003954 if (result == NULL)
3955 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003956
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003957 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_symmetric_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003958 if (tmp == NULL) {
3959 Py_DECREF(result);
3960 return NULL;
3961 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003963 Py_DECREF(tmp);
3964 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003965}
3966
3967static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003968 0, /*nb_add*/
3969 (binaryfunc)dictviews_sub, /*nb_subtract*/
3970 0, /*nb_multiply*/
3971 0, /*nb_remainder*/
3972 0, /*nb_divmod*/
3973 0, /*nb_power*/
3974 0, /*nb_negative*/
3975 0, /*nb_positive*/
3976 0, /*nb_absolute*/
3977 0, /*nb_bool*/
3978 0, /*nb_invert*/
3979 0, /*nb_lshift*/
3980 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003981 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003982 (binaryfunc)dictviews_xor, /*nb_xor*/
3983 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00003984};
3985
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003986static PyObject*
3987dictviews_isdisjoint(PyObject *self, PyObject *other)
3988{
3989 PyObject *it;
3990 PyObject *item = NULL;
3991
3992 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06003993 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003994 Py_RETURN_TRUE;
3995 else
3996 Py_RETURN_FALSE;
3997 }
3998
3999 /* Iterate over the shorter object (only if other is a set,
4000 * because PySequence_Contains may be expensive otherwise): */
4001 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06004002 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004003 Py_ssize_t len_other = PyObject_Size(other);
4004 if (len_other == -1)
4005 return NULL;
4006
4007 if ((len_other > len_self)) {
4008 PyObject *tmp = other;
4009 other = self;
4010 self = tmp;
4011 }
4012 }
4013
4014 it = PyObject_GetIter(other);
4015 if (it == NULL)
4016 return NULL;
4017
4018 while ((item = PyIter_Next(it)) != NULL) {
4019 int contains = PySequence_Contains(self, item);
4020 Py_DECREF(item);
4021 if (contains == -1) {
4022 Py_DECREF(it);
4023 return NULL;
4024 }
4025
4026 if (contains) {
4027 Py_DECREF(it);
4028 Py_RETURN_FALSE;
4029 }
4030 }
4031 Py_DECREF(it);
4032 if (PyErr_Occurred())
4033 return NULL; /* PyIter_Next raised an exception. */
4034 Py_RETURN_TRUE;
4035}
4036
4037PyDoc_STRVAR(isdisjoint_doc,
4038"Return True if the view and the given iterable have a null intersection.");
4039
Guido van Rossumb90c8482007-02-10 01:11:45 +00004040static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004041 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4042 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004043 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004044};
4045
4046PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004047 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4048 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004049 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004050 0, /* tp_itemsize */
4051 /* methods */
4052 (destructor)dictview_dealloc, /* tp_dealloc */
4053 0, /* tp_print */
4054 0, /* tp_getattr */
4055 0, /* tp_setattr */
4056 0, /* tp_reserved */
4057 (reprfunc)dictview_repr, /* tp_repr */
4058 &dictviews_as_number, /* tp_as_number */
4059 &dictkeys_as_sequence, /* tp_as_sequence */
4060 0, /* tp_as_mapping */
4061 0, /* tp_hash */
4062 0, /* tp_call */
4063 0, /* tp_str */
4064 PyObject_GenericGetAttr, /* tp_getattro */
4065 0, /* tp_setattro */
4066 0, /* tp_as_buffer */
4067 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4068 0, /* tp_doc */
4069 (traverseproc)dictview_traverse, /* tp_traverse */
4070 0, /* tp_clear */
4071 dictview_richcompare, /* tp_richcompare */
4072 0, /* tp_weaklistoffset */
4073 (getiterfunc)dictkeys_iter, /* tp_iter */
4074 0, /* tp_iternext */
4075 dictkeys_methods, /* tp_methods */
4076 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004077};
4078
4079static PyObject *
4080dictkeys_new(PyObject *dict)
4081{
Eric Snow96c6af92015-05-29 22:21:39 -06004082 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004083}
4084
Guido van Rossum3ac67412007-02-10 18:55:06 +00004085/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004086
4087static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004088dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004089{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004090 if (dv->dv_dict == NULL) {
4091 Py_RETURN_NONE;
4092 }
4093 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004094}
4095
4096static int
Eric Snow96c6af92015-05-29 22:21:39 -06004097dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004098{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004099 PyObject *key, *value, *found;
4100 if (dv->dv_dict == NULL)
4101 return 0;
4102 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
4103 return 0;
4104 key = PyTuple_GET_ITEM(obj, 0);
4105 value = PyTuple_GET_ITEM(obj, 1);
Raymond Hettinger6692f012016-09-18 21:46:08 -07004106 found = PyDict_GetItemWithError((PyObject *)dv->dv_dict, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004107 if (found == NULL) {
4108 if (PyErr_Occurred())
4109 return -1;
4110 return 0;
4111 }
4112 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004113}
4114
Guido van Rossum83825ac2007-02-10 04:54:19 +00004115static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004116 (lenfunc)dictview_len, /* sq_length */
4117 0, /* sq_concat */
4118 0, /* sq_repeat */
4119 0, /* sq_item */
4120 0, /* sq_slice */
4121 0, /* sq_ass_item */
4122 0, /* sq_ass_slice */
4123 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004124};
4125
Guido van Rossumb90c8482007-02-10 01:11:45 +00004126static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004127 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4128 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004129 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004130};
4131
4132PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004133 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4134 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004135 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004136 0, /* tp_itemsize */
4137 /* methods */
4138 (destructor)dictview_dealloc, /* tp_dealloc */
4139 0, /* tp_print */
4140 0, /* tp_getattr */
4141 0, /* tp_setattr */
4142 0, /* tp_reserved */
4143 (reprfunc)dictview_repr, /* tp_repr */
4144 &dictviews_as_number, /* tp_as_number */
4145 &dictitems_as_sequence, /* tp_as_sequence */
4146 0, /* tp_as_mapping */
4147 0, /* tp_hash */
4148 0, /* tp_call */
4149 0, /* tp_str */
4150 PyObject_GenericGetAttr, /* tp_getattro */
4151 0, /* tp_setattro */
4152 0, /* tp_as_buffer */
4153 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4154 0, /* tp_doc */
4155 (traverseproc)dictview_traverse, /* tp_traverse */
4156 0, /* tp_clear */
4157 dictview_richcompare, /* tp_richcompare */
4158 0, /* tp_weaklistoffset */
4159 (getiterfunc)dictitems_iter, /* tp_iter */
4160 0, /* tp_iternext */
4161 dictitems_methods, /* tp_methods */
4162 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004163};
4164
4165static PyObject *
4166dictitems_new(PyObject *dict)
4167{
Eric Snow96c6af92015-05-29 22:21:39 -06004168 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004169}
4170
Guido van Rossum3ac67412007-02-10 18:55:06 +00004171/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004172
4173static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004174dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004175{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004176 if (dv->dv_dict == NULL) {
4177 Py_RETURN_NONE;
4178 }
4179 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004180}
4181
Guido van Rossum83825ac2007-02-10 04:54:19 +00004182static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004183 (lenfunc)dictview_len, /* sq_length */
4184 0, /* sq_concat */
4185 0, /* sq_repeat */
4186 0, /* sq_item */
4187 0, /* sq_slice */
4188 0, /* sq_ass_item */
4189 0, /* sq_ass_slice */
4190 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004191};
4192
Guido van Rossumb90c8482007-02-10 01:11:45 +00004193static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004194 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004195};
4196
4197PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004198 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4199 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004200 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004201 0, /* tp_itemsize */
4202 /* methods */
4203 (destructor)dictview_dealloc, /* tp_dealloc */
4204 0, /* tp_print */
4205 0, /* tp_getattr */
4206 0, /* tp_setattr */
4207 0, /* tp_reserved */
4208 (reprfunc)dictview_repr, /* tp_repr */
4209 0, /* tp_as_number */
4210 &dictvalues_as_sequence, /* tp_as_sequence */
4211 0, /* tp_as_mapping */
4212 0, /* tp_hash */
4213 0, /* tp_call */
4214 0, /* tp_str */
4215 PyObject_GenericGetAttr, /* tp_getattro */
4216 0, /* tp_setattro */
4217 0, /* tp_as_buffer */
4218 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4219 0, /* tp_doc */
4220 (traverseproc)dictview_traverse, /* tp_traverse */
4221 0, /* tp_clear */
4222 0, /* tp_richcompare */
4223 0, /* tp_weaklistoffset */
4224 (getiterfunc)dictvalues_iter, /* tp_iter */
4225 0, /* tp_iternext */
4226 dictvalues_methods, /* tp_methods */
4227 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004228};
4229
4230static PyObject *
4231dictvalues_new(PyObject *dict)
4232{
Eric Snow96c6af92015-05-29 22:21:39 -06004233 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004234}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004235
4236/* Returns NULL if cannot allocate a new PyDictKeysObject,
4237 but does not set an error */
4238PyDictKeysObject *
4239_PyDict_NewKeysForClass(void)
4240{
Victor Stinner742da042016-09-07 17:40:12 -07004241 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004242 if (keys == NULL)
4243 PyErr_Clear();
4244 else
4245 keys->dk_lookup = lookdict_split;
4246 return keys;
4247}
4248
4249#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
4250
4251PyObject *
4252PyObject_GenericGetDict(PyObject *obj, void *context)
4253{
4254 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
4255 if (dictptr == NULL) {
4256 PyErr_SetString(PyExc_AttributeError,
4257 "This object has no __dict__");
4258 return NULL;
4259 }
4260 dict = *dictptr;
4261 if (dict == NULL) {
4262 PyTypeObject *tp = Py_TYPE(obj);
4263 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
4264 DK_INCREF(CACHED_KEYS(tp));
4265 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
4266 }
4267 else {
4268 *dictptr = dict = PyDict_New();
4269 }
4270 }
4271 Py_XINCREF(dict);
4272 return dict;
4273}
4274
4275int
4276_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
Victor Stinner742da042016-09-07 17:40:12 -07004277 PyObject *key, PyObject *value)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004278{
4279 PyObject *dict;
4280 int res;
4281 PyDictKeysObject *cached;
4282
4283 assert(dictptr != NULL);
4284 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
4285 assert(dictptr != NULL);
4286 dict = *dictptr;
4287 if (dict == NULL) {
4288 DK_INCREF(cached);
4289 dict = new_dict_with_shared_keys(cached);
4290 if (dict == NULL)
4291 return -1;
4292 *dictptr = dict;
4293 }
4294 if (value == NULL) {
4295 res = PyDict_DelItem(dict, key);
4296 if (cached != ((PyDictObject *)dict)->ma_keys) {
4297 CACHED_KEYS(tp) = NULL;
4298 DK_DECREF(cached);
4299 }
4300 } else {
4301 res = PyDict_SetItem(dict, key, value);
4302 if (cached != ((PyDictObject *)dict)->ma_keys) {
4303 /* Either update tp->ht_cached_keys or delete it */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004304 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004305 CACHED_KEYS(tp) = make_keys_shared(dict);
Victor Stinner742da042016-09-07 17:40:12 -07004306 }
4307 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004308 CACHED_KEYS(tp) = NULL;
4309 }
4310 DK_DECREF(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004311 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
4312 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004313 }
4314 }
4315 } else {
4316 dict = *dictptr;
4317 if (dict == NULL) {
4318 dict = PyDict_New();
4319 if (dict == NULL)
4320 return -1;
4321 *dictptr = dict;
4322 }
4323 if (value == NULL) {
4324 res = PyDict_DelItem(dict, key);
4325 } else {
4326 res = PyDict_SetItem(dict, key, value);
4327 }
4328 }
4329 return res;
4330}
4331
4332void
4333_PyDictKeys_DecRef(PyDictKeysObject *keys)
4334{
4335 DK_DECREF(keys);
4336}