blob: 808f548a90394e3223bf3ed6ebd33d540b73b12d [file] [log] [blame]
Guido van Rossum2bc13791999-03-24 19:06:42 +00001/* Dictionary object implementation using a hash table */
Guido van Rossum9bfef441993-03-29 10:43:31 +00002
Raymond Hettinger930427b2003-05-03 06:51:59 +00003/* The distribution includes a separate file, Objects/dictnotes.txt,
Tim Peters60b29962006-01-01 01:19:23 +00004 describing explorations into dictionary design and optimization.
Raymond Hettinger930427b2003-05-03 06:51:59 +00005 It covers typical dictionary use patterns, the parameters for
6 tuning dictionaries, and several ideas for possible optimizations.
7*/
8
Victor Stinner742da042016-09-07 17:40:12 -07009/* PyDictKeysObject
10
11This implements the dictionary's hashtable.
12
Raymond Hettingerb12785d2016-10-22 09:58:14 -070013As of Python 3.6, this is compact and ordered. Basic idea is described here:
14* https://mail.python.org/pipermail/python-dev/2012-December/123028.html
15* https://morepypy.blogspot.com/2015/01/faster-more-memory-efficient-and-more.html
Victor Stinner742da042016-09-07 17:40:12 -070016
17layout:
18
19+---------------+
20| dk_refcnt |
21| dk_size |
22| dk_lookup |
23| dk_usable |
24| dk_nentries |
25+---------------+
26| dk_indices |
27| |
28+---------------+
29| dk_entries |
30| |
31+---------------+
32
33dk_indices is actual hashtable. It holds index in entries, or DKIX_EMPTY(-1)
34or DKIX_DUMMY(-2).
35Size of indices is dk_size. Type of each index in indices is vary on dk_size:
36
37* int8 for dk_size <= 128
38* int16 for 256 <= dk_size <= 2**15
39* int32 for 2**16 <= dk_size <= 2**31
40* int64 for 2**32 <= dk_size
41
42dk_entries is array of PyDictKeyEntry. It's size is USABLE_FRACTION(dk_size).
43DK_ENTRIES(dk) can be used to get pointer to entries.
44
45NOTE: Since negative value is used for DKIX_EMPTY and DKIX_DUMMY, type of
46dk_indices entry is signed integer and int16 is used for table which
47dk_size == 256.
48*/
49
Benjamin Peterson7d95e402012-04-23 11:24:50 -040050
51/*
Benjamin Peterson7d95e402012-04-23 11:24:50 -040052The DictObject can be in one of two forms.
Victor Stinner742da042016-09-07 17:40:12 -070053
Benjamin Peterson7d95e402012-04-23 11:24:50 -040054Either:
55 A combined table:
56 ma_values == NULL, dk_refcnt == 1.
57 Values are stored in the me_value field of the PyDictKeysObject.
Benjamin Peterson7d95e402012-04-23 11:24:50 -040058Or:
59 A split table:
60 ma_values != NULL, dk_refcnt >= 1
61 Values are stored in the ma_values array.
Victor Stinner742da042016-09-07 17:40:12 -070062 Only string (unicode) keys are allowed.
63 All dicts sharing same key must have same insertion order.
Benjamin Peterson7d95e402012-04-23 11:24:50 -040064
Victor Stinner742da042016-09-07 17:40:12 -070065There are four kinds of slots in the table (slot is index, and
66DK_ENTRIES(keys)[index] if index >= 0):
67
681. Unused. index == DKIX_EMPTY
69 Does not hold an active (key, value) pair now and never did. Unused can
70 transition to Active upon key insertion. This is each slot's initial state.
71
722. Active. index >= 0, me_key != NULL and me_value != NULL
73 Holds an active (key, value) pair. Active can transition to Dummy or
74 Pending upon key deletion (for combined and split tables respectively).
75 This is the only case in which me_value != NULL.
76
773. Dummy. index == DKIX_DUMMY (combined only)
78 Previously held an active (key, value) pair, but that was deleted and an
79 active pair has not yet overwritten the slot. Dummy can transition to
80 Active upon key insertion. Dummy slots cannot be made Unused again
81 else the probe sequence in case of collision would have no way to know
82 they were once active.
83
844. Pending. index >= 0, key != NULL, and value == NULL (split only)
85 Not yet inserted in split-table.
Benjamin Peterson7d95e402012-04-23 11:24:50 -040086*/
87
Victor Stinner742da042016-09-07 17:40:12 -070088/*
89Preserving insertion order
Benjamin Peterson7d95e402012-04-23 11:24:50 -040090
Victor Stinner742da042016-09-07 17:40:12 -070091It's simple for combined table. Since dk_entries is mostly append only, we can
92get insertion order by just iterating dk_entries.
93
94One exception is .popitem(). It removes last item in dk_entries and decrement
95dk_nentries to achieve amortized O(1). Since there are DKIX_DUMMY remains in
96dk_indices, we can't increment dk_usable even though dk_nentries is
97decremented.
98
99In split table, inserting into pending entry is allowed only for dk_entries[ix]
100where ix == mp->ma_used. Inserting into other index and deleting item cause
101converting the dict to the combined table.
102*/
103
104/* PyDict_MINSIZE is the starting size for any new dict.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400105 * 8 allows dicts with no more than 5 active entries; experiments suggested
106 * this suffices for the majority of dicts (consisting mostly of usually-small
107 * dicts created to pass keyword arguments).
108 * Making this 8, rather than 4 reduces the number of resizes for most
109 * dictionaries, without any significant extra memory use.
110 */
Victor Stinner742da042016-09-07 17:40:12 -0700111#define PyDict_MINSIZE 8
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400112
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000113#include "Python.h"
Eric Snow96c6af92015-05-29 22:21:39 -0600114#include "dict-common.h"
Victor Stinner990397e2016-09-09 20:22:59 -0700115#include "stringlib/eq.h" /* to get unicode_eq() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000116
Larry Hastings61272b72014-01-07 12:41:53 -0800117/*[clinic input]
Larry Hastingsc2047262014-01-25 20:43:29 -0800118class dict "PyDictObject *" "&PyDict_Type"
Larry Hastings61272b72014-01-07 12:41:53 -0800119[clinic start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -0800120/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f157a5a0ce9589d6]*/
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800121
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400122
123/*
124To ensure the lookup algorithm terminates, there must be at least one Unused
125slot (NULL key) in the table.
126To avoid slowing down lookups on a near-full table, we resize the table when
127it's USABLE_FRACTION (currently two-thirds) full.
128*/
Guido van Rossum16e93a81997-01-28 00:00:11 +0000129
Tim Peterseb28ef22001-06-02 05:27:19 +0000130#define PERTURB_SHIFT 5
131
Guido van Rossum16e93a81997-01-28 00:00:11 +0000132/*
Tim Peterseb28ef22001-06-02 05:27:19 +0000133Major subtleties ahead: Most hash schemes depend on having a "good" hash
134function, in the sense of simulating randomness. Python doesn't: its most
R David Murray537ad7a2016-07-10 12:33:18 -0400135important hash functions (for ints) are very regular in common
Tim Peterseb28ef22001-06-02 05:27:19 +0000136cases:
Tim Peters15d49292001-05-27 07:39:22 +0000137
R David Murray537ad7a2016-07-10 12:33:18 -0400138 >>>[hash(i) for i in range(4)]
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000139 [0, 1, 2, 3]
Tim Peters15d49292001-05-27 07:39:22 +0000140
Tim Peterseb28ef22001-06-02 05:27:19 +0000141This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
142the low-order i bits as the initial table index is extremely fast, and there
R David Murray537ad7a2016-07-10 12:33:18 -0400143are no collisions at all for dicts indexed by a contiguous range of ints. So
144this gives better-than-random behavior in common cases, and that's very
145desirable.
Tim Peters15d49292001-05-27 07:39:22 +0000146
Tim Peterseb28ef22001-06-02 05:27:19 +0000147OTOH, when collisions occur, the tendency to fill contiguous slices of the
148hash table makes a good collision resolution strategy crucial. Taking only
149the last i bits of the hash code is also vulnerable: for example, consider
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000151their own hash codes, and this fits in a dict of size 2**15, the last 15 bits
152 of every hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +0000153
Tim Peterseb28ef22001-06-02 05:27:19 +0000154But catering to unusual cases should not slow the usual ones, so we just take
155the last i bits anyway. It's up to collision resolution to do the rest. If
156we *usually* find the key we're looking for on the first try (and, it turns
157out, we usually do -- the table load factor is kept under 2/3, so the odds
158are solidly in our favor), then it makes best sense to keep the initial index
159computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +0000160
Tim Peterseb28ef22001-06-02 05:27:19 +0000161The first half of collision resolution is to visit table indices via this
162recurrence:
Tim Peters15d49292001-05-27 07:39:22 +0000163
Tim Peterseb28ef22001-06-02 05:27:19 +0000164 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +0000165
Tim Peterseb28ef22001-06-02 05:27:19 +0000166For any initial j in range(2**i), repeating that 2**i times generates each
167int in range(2**i) exactly once (see any text on random-number generation for
168proof). By itself, this doesn't help much: like linear probing (setting
169j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
170order. This would be bad, except that's not the only thing we do, and it's
171actually *good* in the common cases where hash keys are consecutive. In an
172example that's really too small to make this entirely clear, for a table of
173size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +0000174
Tim Peterseb28ef22001-06-02 05:27:19 +0000175 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
176
177If two things come in at index 5, the first place we look after is index 2,
178not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
179Linear probing is deadly in this case because there the fixed probe order
180is the *same* as the order consecutive keys are likely to arrive. But it's
181extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
182and certain that consecutive hash codes do not.
183
184The other half of the strategy is to get the other bits of the hash code
185into play. This is done by initializing a (unsigned) vrbl "perturb" to the
186full hash code, and changing the recurrence to:
187
Tim Peterseb28ef22001-06-02 05:27:19 +0000188 perturb >>= PERTURB_SHIFT;
INADA Naoki267941c2016-10-06 15:19:07 +0900189 j = (5*j) + 1 + perturb;
Tim Peterseb28ef22001-06-02 05:27:19 +0000190 use j % 2**i as the next table index;
191
192Now the probe sequence depends (eventually) on every bit in the hash code,
193and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
194because it quickly magnifies small differences in the bits that didn't affect
195the initial index. Note that because perturb is unsigned, if the recurrence
196is executed often enough perturb eventually becomes and remains 0. At that
197point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
198that's certain to find an empty slot eventually (since it generates every int
199in range(2**i), and we make sure there's always at least one empty slot).
200
201Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
202small so that the high bits of the hash code continue to affect the probe
203sequence across iterations; but you want it large so that in really bad cases
204the high-order hash bits have an effect on early iterations. 5 was "the
205best" in minimizing total collisions across experiments Tim Peters ran (on
206both normal and pathological cases), but 4 and 6 weren't significantly worse.
207
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000208Historical: Reimer Behrends contributed the idea of using a polynomial-based
Tim Peterseb28ef22001-06-02 05:27:19 +0000209approach, using repeated multiplication by x in GF(2**n) where an irreducible
210polynomial for each table size was chosen such that x was a primitive root.
211Christian Tismer later extended that to use division by x instead, as an
212efficient way to get the high bits of the hash code into play. This scheme
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000213also gave excellent collision statistics, but was more expensive: two
214if-tests were required inside the loop; computing "the next" index took about
215the same number of operations but without as much potential parallelism
216(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
217above, and then shifting perturb can be done while the table index is being
218masked); and the PyDictObject struct required a member to hold the table's
219polynomial. In Tim's experiments the current scheme ran faster, produced
220equally good collision statistics, needed less code & used less memory.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000221
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000222*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000223
Fred Drake1bff34a2000-08-31 19:31:38 +0000224/* forward declarations */
Victor Stinner742da042016-09-07 17:40:12 -0700225static Py_ssize_t lookdict(PyDictObject *mp, PyObject *key,
INADA Naokiba609772016-12-07 20:41:42 +0900226 Py_hash_t hash, PyObject **value_addr,
Victor Stinner742da042016-09-07 17:40:12 -0700227 Py_ssize_t *hashpos);
228static Py_ssize_t lookdict_unicode(PyDictObject *mp, PyObject *key,
INADA Naokiba609772016-12-07 20:41:42 +0900229 Py_hash_t hash, PyObject **value_addr,
Victor Stinner742da042016-09-07 17:40:12 -0700230 Py_ssize_t *hashpos);
231static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400232lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
INADA Naokiba609772016-12-07 20:41:42 +0900233 Py_hash_t hash, PyObject **value_addr,
Victor Stinner742da042016-09-07 17:40:12 -0700234 Py_ssize_t *hashpos);
235static Py_ssize_t lookdict_split(PyDictObject *mp, PyObject *key,
INADA Naokiba609772016-12-07 20:41:42 +0900236 Py_hash_t hash, PyObject **value_addr,
Victor Stinner742da042016-09-07 17:40:12 -0700237 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
T. Woutersa00c3fd2017-03-31 09:14:41 -0700440#ifndef NDEBUG
Victor Stinner611b0fa2016-09-14 15:02:01 +0200441static 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
INADA Naoki1b8df102017-02-20 22:48:10 +0900680never raise an exception; that function can never return DKIX_ERROR when key
681is string. Otherwise, it falls back to lookdict().
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400682lookdict_unicode_nodummy is further specialized for string keys that cannot be
683the <dummy> value.
Victor Stinner742da042016-09-07 17:40:12 -0700684For both, when the key isn't found a DKIX_EMPTY is returned. hashpos returns
685where the key index should be inserted.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000686*/
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100687static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400688lookdict(PyDictObject *mp, PyObject *key,
INADA Naokiba609772016-12-07 20:41:42 +0900689 Py_hash_t hash, PyObject **value_addr, Py_ssize_t *hashpos)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000690{
INADA Naoki267941c2016-10-06 15:19:07 +0900691 size_t i, mask;
Victor Stinner742da042016-09-07 17:40:12 -0700692 Py_ssize_t ix, freeslot;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200693 int cmp;
Victor Stinner742da042016-09-07 17:40:12 -0700694 PyDictKeysObject *dk;
695 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000697
Antoine Pitrou9a234902012-05-13 20:48:01 +0200698top:
Victor Stinner742da042016-09-07 17:40:12 -0700699 dk = mp->ma_keys;
700 mask = DK_MASK(dk);
701 ep0 = DK_ENTRIES(dk);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700703
704 ix = dk_get_index(dk, i);
705 if (ix == DKIX_EMPTY) {
706 if (hashpos != NULL)
707 *hashpos = i;
708 *value_addr = NULL;
709 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400710 }
Victor Stinner742da042016-09-07 17:40:12 -0700711 if (ix == DKIX_DUMMY) {
712 freeslot = i;
713 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 else {
Victor Stinner742da042016-09-07 17:40:12 -0700715 ep = &ep0[ix];
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300716 assert(ep->me_key != NULL);
Victor Stinner742da042016-09-07 17:40:12 -0700717 if (ep->me_key == key) {
INADA Naokiba609772016-12-07 20:41:42 +0900718 *value_addr = ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700719 if (hashpos != NULL)
720 *hashpos = i;
721 return ix;
722 }
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300723 if (ep->me_hash == hash) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 startkey = ep->me_key;
725 Py_INCREF(startkey);
726 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
727 Py_DECREF(startkey);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +0200728 if (cmp < 0) {
729 *value_addr = NULL;
Victor Stinner742da042016-09-07 17:40:12 -0700730 return DKIX_ERROR;
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +0200731 }
Victor Stinner742da042016-09-07 17:40:12 -0700732 if (dk == mp->ma_keys && ep->me_key == startkey) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400733 if (cmp > 0) {
INADA Naokiba609772016-12-07 20:41:42 +0900734 *value_addr = ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700735 if (hashpos != NULL)
736 *hashpos = i;
737 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400738 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 }
740 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200741 /* The dict was mutated, restart */
742 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 }
744 }
Victor Stinner742da042016-09-07 17:40:12 -0700745 freeslot = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 }
Tim Peters15d49292001-05-27 07:39:22 +0000747
INADA Naoki267941c2016-10-06 15:19:07 +0900748 for (size_t perturb = hash;;) {
749 perturb >>= PERTURB_SHIFT;
Victor Stinner742da042016-09-07 17:40:12 -0700750 i = ((i << 2) + i + perturb + 1) & mask;
751 ix = dk_get_index(dk, i);
752 if (ix == DKIX_EMPTY) {
753 if (hashpos != NULL) {
754 *hashpos = (freeslot == -1) ? (Py_ssize_t)i : freeslot;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400755 }
Victor Stinner742da042016-09-07 17:40:12 -0700756 *value_addr = NULL;
757 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400758 }
Victor Stinner742da042016-09-07 17:40:12 -0700759 if (ix == DKIX_DUMMY) {
760 if (freeslot == -1)
761 freeslot = i;
762 continue;
763 }
764 ep = &ep0[ix];
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300765 assert(ep->me_key != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400766 if (ep->me_key == key) {
Victor Stinner742da042016-09-07 17:40:12 -0700767 if (hashpos != NULL) {
768 *hashpos = i;
769 }
INADA Naokiba609772016-12-07 20:41:42 +0900770 *value_addr = ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700771 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400772 }
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300773 if (ep->me_hash == hash) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 startkey = ep->me_key;
775 Py_INCREF(startkey);
776 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
777 Py_DECREF(startkey);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400778 if (cmp < 0) {
779 *value_addr = NULL;
Victor Stinner742da042016-09-07 17:40:12 -0700780 return DKIX_ERROR;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400781 }
Victor Stinner742da042016-09-07 17:40:12 -0700782 if (dk == mp->ma_keys && ep->me_key == startkey) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400783 if (cmp > 0) {
Victor Stinner742da042016-09-07 17:40:12 -0700784 if (hashpos != NULL) {
785 *hashpos = i;
786 }
INADA Naokiba609772016-12-07 20:41:42 +0900787 *value_addr = ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700788 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400789 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 }
791 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200792 /* The dict was mutated, restart */
793 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 }
795 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 }
797 assert(0); /* NOT REACHED */
798 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000799}
800
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400801/* Specialized version for string-only keys */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100802static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400803lookdict_unicode(PyDictObject *mp, PyObject *key,
INADA Naokiba609772016-12-07 20:41:42 +0900804 Py_hash_t hash, PyObject **value_addr, Py_ssize_t *hashpos)
Fred Drake1bff34a2000-08-31 19:31:38 +0000805{
INADA Naoki267941c2016-10-06 15:19:07 +0900806 size_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200807 size_t mask = DK_MASK(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -0700808 Py_ssize_t ix, freeslot;
809 PyDictKeyEntry *ep, *ep0 = DK_ENTRIES(mp->ma_keys);
Fred Drake1bff34a2000-08-31 19:31:38 +0000810
Victor Stinner742da042016-09-07 17:40:12 -0700811 assert(mp->ma_values == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 /* Make sure this function doesn't have to handle non-unicode keys,
813 including subclasses of str; e.g., one reason to subclass
814 unicodes is to override __eq__, and for speed we don't cater to
815 that here. */
816 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400817 mp->ma_keys->dk_lookup = lookdict;
Victor Stinner742da042016-09-07 17:40:12 -0700818 return lookdict(mp, key, hash, value_addr, hashpos);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 }
Mark Dickinson57e683e2011-09-24 18:18:40 +0100820 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700821 ix = dk_get_index(mp->ma_keys, i);
822 if (ix == DKIX_EMPTY) {
823 if (hashpos != NULL)
824 *hashpos = i;
825 *value_addr = NULL;
826 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400827 }
Victor Stinner742da042016-09-07 17:40:12 -0700828 if (ix == DKIX_DUMMY) {
829 freeslot = i;
830 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 else {
Victor Stinner742da042016-09-07 17:40:12 -0700832 ep = &ep0[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700833 assert(ep->me_key != NULL);
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300834 if (ep->me_key == key
835 || (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700836 if (hashpos != NULL)
837 *hashpos = i;
INADA Naokiba609772016-12-07 20:41:42 +0900838 *value_addr = ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700839 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400840 }
Victor Stinner742da042016-09-07 17:40:12 -0700841 freeslot = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 }
Tim Peters15d49292001-05-27 07:39:22 +0000843
INADA Naoki267941c2016-10-06 15:19:07 +0900844 for (size_t perturb = hash;;) {
845 perturb >>= PERTURB_SHIFT;
Victor Stinner742da042016-09-07 17:40:12 -0700846 i = mask & ((i << 2) + i + perturb + 1);
847 ix = dk_get_index(mp->ma_keys, i);
848 if (ix == DKIX_EMPTY) {
849 if (hashpos != NULL) {
850 *hashpos = (freeslot == -1) ? (Py_ssize_t)i : freeslot;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400851 }
Victor Stinner742da042016-09-07 17:40:12 -0700852 *value_addr = NULL;
853 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400854 }
Victor Stinner742da042016-09-07 17:40:12 -0700855 if (ix == DKIX_DUMMY) {
856 if (freeslot == -1)
857 freeslot = i;
858 continue;
859 }
860 ep = &ep0[ix];
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300861 assert(ep->me_key != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 if (ep->me_key == key
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300863 || (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
INADA Naokiba609772016-12-07 20:41:42 +0900864 *value_addr = ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700865 if (hashpos != NULL) {
866 *hashpos = i;
867 }
868 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400869 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 }
871 assert(0); /* NOT REACHED */
872 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000873}
874
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400875/* Faster version of lookdict_unicode when it is known that no <dummy> keys
876 * will be present. */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100877static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400878lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
INADA Naokiba609772016-12-07 20:41:42 +0900879 Py_hash_t hash, PyObject **value_addr,
Victor Stinner742da042016-09-07 17:40:12 -0700880 Py_ssize_t *hashpos)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400881{
INADA Naoki267941c2016-10-06 15:19:07 +0900882 size_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200883 size_t mask = DK_MASK(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -0700884 Py_ssize_t ix;
885 PyDictKeyEntry *ep, *ep0 = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400886
Victor Stinner742da042016-09-07 17:40:12 -0700887 assert(mp->ma_values == NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400888 /* Make sure this function doesn't have to handle non-unicode keys,
889 including subclasses of str; e.g., one reason to subclass
890 unicodes is to override __eq__, and for speed we don't cater to
891 that here. */
892 if (!PyUnicode_CheckExact(key)) {
893 mp->ma_keys->dk_lookup = lookdict;
Victor Stinner742da042016-09-07 17:40:12 -0700894 return lookdict(mp, key, hash, value_addr, hashpos);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400895 }
896 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700897 ix = dk_get_index(mp->ma_keys, i);
898 assert (ix != DKIX_DUMMY);
899 if (ix == DKIX_EMPTY) {
900 if (hashpos != NULL)
901 *hashpos = i;
902 *value_addr = NULL;
903 return DKIX_EMPTY;
904 }
905 ep = &ep0[ix];
Victor Stinnerdee6e252016-09-08 11:16:07 -0700906 assert(ep->me_key != NULL);
907 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700908 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400909 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700910 if (hashpos != NULL)
911 *hashpos = i;
INADA Naokiba609772016-12-07 20:41:42 +0900912 *value_addr = ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700913 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400914 }
INADA Naoki267941c2016-10-06 15:19:07 +0900915 for (size_t perturb = hash;;) {
916 perturb >>= PERTURB_SHIFT;
Victor Stinner742da042016-09-07 17:40:12 -0700917 i = mask & ((i << 2) + i + perturb + 1);
918 ix = dk_get_index(mp->ma_keys, i);
919 assert (ix != DKIX_DUMMY);
920 if (ix == DKIX_EMPTY) {
921 if (hashpos != NULL)
922 *hashpos = i;
923 *value_addr = NULL;
924 return DKIX_EMPTY;
925 }
926 ep = &ep0[ix];
927 assert(ep->me_key != NULL && PyUnicode_CheckExact(ep->me_key));
928 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400929 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700930 if (hashpos != NULL)
931 *hashpos = i;
INADA Naokiba609772016-12-07 20:41:42 +0900932 *value_addr = ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700933 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400934 }
935 }
936 assert(0); /* NOT REACHED */
937 return 0;
938}
939
940/* Version of lookdict for split tables.
941 * All split tables and only split tables use this lookup function.
942 * Split tables only contain unicode keys and no dummy keys,
943 * so algorithm is the same as lookdict_unicode_nodummy.
944 */
Victor Stinnerc7a8f672016-11-15 15:13:40 +0100945static Py_ssize_t _Py_HOT_FUNCTION
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400946lookdict_split(PyDictObject *mp, PyObject *key,
INADA Naokiba609772016-12-07 20:41:42 +0900947 Py_hash_t hash, PyObject **value_addr, Py_ssize_t *hashpos)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400948{
INADA Naoki267941c2016-10-06 15:19:07 +0900949 size_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200950 size_t mask = DK_MASK(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -0700951 Py_ssize_t ix;
952 PyDictKeyEntry *ep, *ep0 = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400953
Victor Stinner742da042016-09-07 17:40:12 -0700954 /* mp must split table */
955 assert(mp->ma_values != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400956 if (!PyUnicode_CheckExact(key)) {
Victor Stinner742da042016-09-07 17:40:12 -0700957 ix = lookdict(mp, key, hash, value_addr, hashpos);
958 if (ix >= 0) {
INADA Naokiba609772016-12-07 20:41:42 +0900959 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700960 }
961 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400962 }
Victor Stinner742da042016-09-07 17:40:12 -0700963
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400964 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700965 ix = dk_get_index(mp->ma_keys, i);
966 if (ix == DKIX_EMPTY) {
967 if (hashpos != NULL)
968 *hashpos = i;
969 *value_addr = NULL;
970 return DKIX_EMPTY;
971 }
972 assert(ix >= 0);
973 ep = &ep0[ix];
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300974 assert(ep->me_key != NULL && PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700975 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400976 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700977 if (hashpos != NULL)
978 *hashpos = i;
INADA Naokiba609772016-12-07 20:41:42 +0900979 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700980 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400981 }
INADA Naoki267941c2016-10-06 15:19:07 +0900982 for (size_t perturb = hash;;) {
983 perturb >>= PERTURB_SHIFT;
Victor Stinner742da042016-09-07 17:40:12 -0700984 i = mask & ((i << 2) + i + perturb + 1);
985 ix = dk_get_index(mp->ma_keys, i);
986 if (ix == DKIX_EMPTY) {
987 if (hashpos != NULL)
988 *hashpos = i;
989 *value_addr = NULL;
990 return DKIX_EMPTY;
991 }
992 assert(ix >= 0);
993 ep = &ep0[ix];
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300994 assert(ep->me_key != NULL && PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700995 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400996 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700997 if (hashpos != NULL)
998 *hashpos = i;
INADA Naokiba609772016-12-07 20:41:42 +0900999 *value_addr = mp->ma_values[ix];
Victor Stinner742da042016-09-07 17:40:12 -07001000 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001001 }
1002 }
1003 assert(0); /* NOT REACHED */
1004 return 0;
1005}
1006
Benjamin Petersonfb886362010-04-24 18:21:17 +00001007int
1008_PyDict_HasOnlyStringKeys(PyObject *dict)
1009{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 Py_ssize_t pos = 0;
1011 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +00001012 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001014 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 return 1;
1016 while (PyDict_Next(dict, &pos, &key, &value))
1017 if (!PyUnicode_Check(key))
1018 return 0;
1019 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +00001020}
1021
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001022#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 do { \
1024 if (!_PyObject_GC_IS_TRACKED(mp)) { \
1025 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
1026 _PyObject_GC_MAY_BE_TRACKED(value)) { \
1027 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 } \
1029 } \
1030 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001031
1032void
1033_PyDict_MaybeUntrack(PyObject *op)
1034{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 PyDictObject *mp;
1036 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07001037 Py_ssize_t i, numentries;
1038 PyDictKeyEntry *ep0;
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
1041 return;
1042
1043 mp = (PyDictObject *) op;
Victor Stinner742da042016-09-07 17:40:12 -07001044 ep0 = DK_ENTRIES(mp->ma_keys);
1045 numentries = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001046 if (_PyDict_HasSplitTable(mp)) {
Victor Stinner742da042016-09-07 17:40:12 -07001047 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001048 if ((value = mp->ma_values[i]) == NULL)
1049 continue;
1050 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
Victor Stinner742da042016-09-07 17:40:12 -07001051 assert(!_PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001052 return;
1053 }
1054 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001056 else {
Victor Stinner742da042016-09-07 17:40:12 -07001057 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001058 if ((value = ep0[i].me_value) == NULL)
1059 continue;
1060 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
1061 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
1062 return;
1063 }
1064 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001066}
1067
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001068/* Internal function to find slot for an item from its hash
Victor Stinner3c336c52016-09-12 14:17:40 +02001069 when it is known that the key is not present in the dict.
1070
1071 The dict must be combined. */
INADA Naokiba609772016-12-07 20:41:42 +09001072static Py_ssize_t
1073find_empty_slot(PyDictKeysObject *keys, PyObject *key, Py_hash_t hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001074{
INADA Naoki267941c2016-10-06 15:19:07 +09001075 size_t i;
INADA Naokiba609772016-12-07 20:41:42 +09001076 size_t mask = DK_MASK(keys);
Victor Stinner742da042016-09-07 17:40:12 -07001077 Py_ssize_t ix;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001078
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001079 assert(key != NULL);
Victor Stinner3c336c52016-09-12 14:17:40 +02001080
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001081 i = hash & mask;
INADA Naokiba609772016-12-07 20:41:42 +09001082 ix = dk_get_index(keys, i);
INADA Naoki267941c2016-10-06 15:19:07 +09001083 for (size_t perturb = hash; ix != DKIX_EMPTY;) {
1084 perturb >>= PERTURB_SHIFT;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001085 i = (i << 2) + i + perturb + 1;
INADA Naokiba609772016-12-07 20:41:42 +09001086 ix = dk_get_index(keys, i & mask);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 }
INADA Naokiba609772016-12-07 20:41:42 +09001088 assert(DK_ENTRIES(keys)[keys->dk_nentries].me_value == NULL);
1089 return i & mask;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001090}
1091
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001092static int
1093insertion_resize(PyDictObject *mp)
1094{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -07001095 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001096}
Antoine Pitroue965d972012-02-27 00:45:12 +01001097
1098/*
1099Internal routine to insert a new item into the table.
1100Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +01001101Returns -1 if an error occurred, or 0 on success.
1102*/
1103static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001104insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001105{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001106 PyObject *old_value;
INADA Naokiba609772016-12-07 20:41:42 +09001107 PyDictKeyEntry *ep;
Victor Stinner742da042016-09-07 17:40:12 -07001108 Py_ssize_t hashpos, ix;
Antoine Pitroue965d972012-02-27 00:45:12 +01001109
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001110 Py_INCREF(key);
1111 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001112 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
1113 if (insertion_resize(mp) < 0)
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001114 goto Fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001115 }
1116
INADA Naokiba609772016-12-07 20:41:42 +09001117 ix = mp->ma_keys->dk_lookup(mp, key, hash, &old_value, &hashpos);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001118 if (ix == DKIX_ERROR)
1119 goto Fail;
Victor Stinner742da042016-09-07 17:40:12 -07001120
Antoine Pitroud6967322014-10-18 00:35:00 +02001121 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001122 MAINTAIN_TRACKING(mp, key, value);
Victor Stinner742da042016-09-07 17:40:12 -07001123
1124 /* When insertion order is different from shared key, we can't share
1125 * the key anymore. Convert this instance to combine table.
1126 */
1127 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09001128 ((ix >= 0 && old_value == NULL && mp->ma_used != ix) ||
Victor Stinner742da042016-09-07 17:40:12 -07001129 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001130 if (insertion_resize(mp) < 0)
1131 goto Fail;
INADA Naokiba609772016-12-07 20:41:42 +09001132 hashpos = find_empty_slot(mp->ma_keys, key, hash);
Victor Stinner742da042016-09-07 17:40:12 -07001133 ix = DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001134 }
Victor Stinner742da042016-09-07 17:40:12 -07001135
1136 if (ix == DKIX_EMPTY) {
1137 /* Insert into new slot. */
INADA Naokiba609772016-12-07 20:41:42 +09001138 assert(old_value == NULL);
Victor Stinner742da042016-09-07 17:40:12 -07001139 if (mp->ma_keys->dk_usable <= 0) {
1140 /* Need to resize. */
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001141 if (insertion_resize(mp) < 0)
1142 goto Fail;
INADA Naokiba609772016-12-07 20:41:42 +09001143 hashpos = find_empty_slot(mp->ma_keys, key, hash);
Victor Stinner742da042016-09-07 17:40:12 -07001144 }
INADA Naokiba609772016-12-07 20:41:42 +09001145 ep = &DK_ENTRIES(mp->ma_keys)[mp->ma_keys->dk_nentries];
Victor Stinner742da042016-09-07 17:40:12 -07001146 dk_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Victor Stinner742da042016-09-07 17:40:12 -07001147 ep->me_key = key;
1148 ep->me_hash = hash;
1149 if (mp->ma_values) {
1150 assert (mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
1151 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001152 }
1153 else {
Victor Stinner742da042016-09-07 17:40:12 -07001154 ep->me_value = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001155 }
1156 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001157 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07001158 mp->ma_keys->dk_usable--;
1159 mp->ma_keys->dk_nentries++;
1160 assert(mp->ma_keys->dk_usable >= 0);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001161 assert(_PyDict_CheckConsistency(mp));
Victor Stinner742da042016-09-07 17:40:12 -07001162 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001163 }
Victor Stinner742da042016-09-07 17:40:12 -07001164
INADA Naokiba609772016-12-07 20:41:42 +09001165 if (_PyDict_HasSplitTable(mp)) {
1166 mp->ma_values[ix] = value;
1167 if (old_value == NULL) {
1168 /* pending state */
1169 assert(ix == mp->ma_used);
1170 mp->ma_used++;
1171 }
1172 }
1173 else {
1174 assert(old_value != NULL);
1175 DK_ENTRIES(mp->ma_keys)[ix].me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07001176 }
1177
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001178 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naokiba609772016-12-07 20:41:42 +09001179 Py_XDECREF(old_value); /* which **CAN** re-enter (see issue #22653) */
Victor Stinner611b0fa2016-09-14 15:02:01 +02001180 assert(_PyDict_CheckConsistency(mp));
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001181 Py_DECREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001182 return 0;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03001183
1184Fail:
1185 Py_DECREF(value);
1186 Py_DECREF(key);
1187 return -1;
Antoine Pitroue965d972012-02-27 00:45:12 +01001188}
1189
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001190/*
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001191Internal routine used by dictresize() to buid a hashtable of entries.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001192*/
1193static void
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001194build_indices(PyDictKeysObject *keys, PyDictKeyEntry *ep, Py_ssize_t n)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001195{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001196 size_t mask = (size_t)DK_SIZE(keys) - 1;
1197 for (Py_ssize_t ix = 0; ix != n; ix++, ep++) {
1198 Py_hash_t hash = ep->me_hash;
1199 size_t i = hash & mask;
1200 for (size_t perturb = hash; dk_get_index(keys, i) != DKIX_EMPTY;) {
1201 perturb >>= PERTURB_SHIFT;
1202 i = mask & ((i << 2) + i + perturb + 1);
1203 }
1204 dk_set_index(keys, i, ix);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001206}
1207
1208/*
1209Restructure the table by allocating a new table and reinserting all
1210items again. When entries have been deleted, the new table may
1211actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001212If a table is split (its keys and hashes are shared, its values are not),
1213then the values are temporarily copied into the table, it is resized as
1214a combined table, then the me_value slots in the old table are NULLed out.
1215After resizing a table is always combined,
1216but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001217*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001218static int
Victor Stinner3d3f2642016-12-15 17:21:23 +01001219dictresize(PyDictObject *mp, Py_ssize_t minsize)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001220{
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001221 Py_ssize_t newsize, numentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001222 PyDictKeysObject *oldkeys;
1223 PyObject **oldvalues;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001224 PyDictKeyEntry *oldentries, *newentries;
Tim Peters91a364d2001-05-19 07:04:38 +00001225
Victor Stinner742da042016-09-07 17:40:12 -07001226 /* Find the smallest table size > minused. */
1227 for (newsize = PyDict_MINSIZE;
Victor Stinner3d3f2642016-12-15 17:21:23 +01001228 newsize < minsize && newsize > 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 newsize <<= 1)
1230 ;
1231 if (newsize <= 0) {
1232 PyErr_NoMemory();
1233 return -1;
1234 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001235
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001236 oldkeys = mp->ma_keys;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001237
1238 /* NOTE: Current odict checks mp->ma_keys to detect resize happen.
1239 * So we can't reuse oldkeys even if oldkeys->dk_size == newsize.
1240 * TODO: Try reusing oldkeys when reimplement odict.
1241 */
1242
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001243 /* Allocate a new table. */
1244 mp->ma_keys = new_keys_object(newsize);
1245 if (mp->ma_keys == NULL) {
1246 mp->ma_keys = oldkeys;
1247 return -1;
1248 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01001249 // New table must be large enough.
1250 assert(mp->ma_keys->dk_usable >= mp->ma_used);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001251 if (oldkeys->dk_lookup == lookdict)
1252 mp->ma_keys->dk_lookup = lookdict;
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001253
1254 numentries = mp->ma_used;
1255 oldentries = DK_ENTRIES(oldkeys);
1256 newentries = DK_ENTRIES(mp->ma_keys);
1257 oldvalues = mp->ma_values;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001258 if (oldvalues != NULL) {
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001259 /* Convert split table into new combined table.
1260 * We must incref keys; we can transfer values.
1261 * Note that values of split table is always dense.
1262 */
1263 for (Py_ssize_t i = 0; i < numentries; i++) {
1264 assert(oldvalues[i] != NULL);
1265 PyDictKeyEntry *ep = &oldentries[i];
1266 PyObject *key = ep->me_key;
1267 Py_INCREF(key);
1268 newentries[i].me_key = key;
1269 newentries[i].me_hash = ep->me_hash;
1270 newentries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001272
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001273 DK_DECREF(oldkeys);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001274 mp->ma_values = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001275 if (oldvalues != empty_values) {
1276 free_values(oldvalues);
1277 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001278 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001279 else { // combined table.
1280 if (oldkeys->dk_nentries == numentries) {
1281 memcpy(newentries, oldentries, numentries * sizeof(PyDictKeyEntry));
1282 }
1283 else {
1284 PyDictKeyEntry *ep = oldentries;
1285 for (Py_ssize_t i = 0; i < numentries; i++) {
1286 while (ep->me_value == NULL)
1287 ep++;
1288 newentries[i] = *ep++;
1289 }
1290 }
1291
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001292 assert(oldkeys->dk_lookup != lookdict_split);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001293 assert(oldkeys->dk_refcnt == 1);
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001294 if (oldkeys->dk_size == PyDict_MINSIZE &&
1295 numfreekeys < PyDict_MAXFREELIST) {
1296 DK_DEBUG_DECREF keys_free_list[numfreekeys++] = oldkeys;
1297 }
1298 else {
1299 DK_DEBUG_DECREF PyObject_FREE(oldkeys);
1300 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001301 }
Serhiy Storchakae26e20d2016-10-29 10:50:00 +03001302
1303 build_indices(mp->ma_keys, newentries, numentries);
1304 mp->ma_keys->dk_usable -= numentries;
1305 mp->ma_keys->dk_nentries = numentries;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001307}
1308
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001309/* Returns NULL if unable to split table.
1310 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001311static PyDictKeysObject *
1312make_keys_shared(PyObject *op)
1313{
1314 Py_ssize_t i;
1315 Py_ssize_t size;
1316 PyDictObject *mp = (PyDictObject *)op;
1317
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001318 if (!PyDict_CheckExact(op))
1319 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001320 if (!_PyDict_HasSplitTable(mp)) {
1321 PyDictKeyEntry *ep0;
1322 PyObject **values;
1323 assert(mp->ma_keys->dk_refcnt == 1);
1324 if (mp->ma_keys->dk_lookup == lookdict) {
1325 return NULL;
1326 }
1327 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1328 /* Remove dummy keys */
1329 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1330 return NULL;
1331 }
1332 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1333 /* Copy values into a new array */
Victor Stinner742da042016-09-07 17:40:12 -07001334 ep0 = DK_ENTRIES(mp->ma_keys);
1335 size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001336 values = new_values(size);
1337 if (values == NULL) {
1338 PyErr_SetString(PyExc_MemoryError,
1339 "Not enough memory to allocate new values array");
1340 return NULL;
1341 }
1342 for (i = 0; i < size; i++) {
1343 values[i] = ep0[i].me_value;
1344 ep0[i].me_value = NULL;
1345 }
1346 mp->ma_keys->dk_lookup = lookdict_split;
1347 mp->ma_values = values;
1348 }
1349 DK_INCREF(mp->ma_keys);
1350 return mp->ma_keys;
1351}
Christian Heimes99170a52007-12-19 02:07:34 +00001352
1353PyObject *
1354_PyDict_NewPresized(Py_ssize_t minused)
1355{
INADA Naoki92c50ee2016-11-22 00:57:02 +09001356 const Py_ssize_t max_presize = 128 * 1024;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001357 Py_ssize_t newsize;
1358 PyDictKeysObject *new_keys;
INADA Naoki92c50ee2016-11-22 00:57:02 +09001359
1360 /* There are no strict guarantee that returned dict can contain minused
1361 * items without resize. So we create medium size dict instead of very
1362 * large dict or MemoryError.
1363 */
1364 if (minused > USABLE_FRACTION(max_presize)) {
1365 newsize = max_presize;
1366 }
1367 else {
1368 Py_ssize_t minsize = ESTIMATE_SIZE(minused);
1369 newsize = PyDict_MINSIZE;
1370 while (newsize < minsize) {
1371 newsize <<= 1;
1372 }
1373 }
1374 assert(IS_POWER_OF_2(newsize));
1375
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001376 new_keys = new_keys_object(newsize);
1377 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001379 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001380}
1381
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001382/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1383 * that may occur (originally dicts supported only string keys, and exceptions
1384 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001385 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001386 * (suppressed) occurred while computing the key's hash, or that some error
1387 * (suppressed) occurred when comparing keys in the dict's internal probe
1388 * sequence. A nasty example of the latter is when a Python-coded comparison
1389 * function hits a stack-depth error, which can cause this to return NULL
1390 * even if the key is present.
1391 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001392PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001393PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001394{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001395 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07001396 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 PyDictObject *mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 PyThreadState *tstate;
INADA Naokiba609772016-12-07 20:41:42 +09001399 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 if (!PyDict_Check(op))
1402 return NULL;
1403 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001404 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 {
1406 hash = PyObject_Hash(key);
1407 if (hash == -1) {
1408 PyErr_Clear();
1409 return NULL;
1410 }
1411 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 /* We can arrive here with a NULL tstate during initialization: try
1414 running "python -Wi" for an example related to string interning.
1415 Let's just hope that no exception occurs then... This must be
1416 _PyThreadState_Current and not PyThreadState_GET() because in debug
1417 mode, the latter complains if tstate is NULL. */
Victor Stinner0cae6092016-11-11 01:43:56 +01001418 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 if (tstate != NULL && tstate->curexc_type != NULL) {
1420 /* preserve the existing exception */
1421 PyObject *err_type, *err_value, *err_tb;
1422 PyErr_Fetch(&err_type, &err_value, &err_tb);
INADA Naokiba609772016-12-07 20:41:42 +09001423 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 /* ignore errors */
1425 PyErr_Restore(err_type, err_value, err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001426 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 return NULL;
1428 }
1429 else {
INADA Naokiba609772016-12-07 20:41:42 +09001430 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value, NULL);
Victor Stinner742da042016-09-07 17:40:12 -07001431 if (ix < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 PyErr_Clear();
1433 return NULL;
1434 }
1435 }
INADA Naokiba609772016-12-07 20:41:42 +09001436 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001437}
1438
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001439/* Same as PyDict_GetItemWithError() but with hash supplied by caller.
1440 This returns NULL *with* an exception set if an exception occurred.
1441 It returns NULL *without* an exception set if the key wasn't present.
1442*/
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001443PyObject *
1444_PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1445{
Victor Stinner742da042016-09-07 17:40:12 -07001446 Py_ssize_t ix;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001447 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001448 PyObject *value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001449
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001450 if (!PyDict_Check(op)) {
1451 PyErr_BadInternalCall();
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001452 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001453 }
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001454
INADA Naokiba609772016-12-07 20:41:42 +09001455 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value, NULL);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001456 if (ix < 0) {
1457 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001458 }
INADA Naokiba609772016-12-07 20:41:42 +09001459 return value;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001460}
1461
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001462/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1463 This returns NULL *with* an exception set if an exception occurred.
1464 It returns NULL *without* an exception set if the key wasn't present.
1465*/
1466PyObject *
1467PyDict_GetItemWithError(PyObject *op, PyObject *key)
1468{
Victor Stinner742da042016-09-07 17:40:12 -07001469 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001470 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 PyDictObject*mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001472 PyObject *value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 if (!PyDict_Check(op)) {
1475 PyErr_BadInternalCall();
1476 return NULL;
1477 }
1478 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001479 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 {
1481 hash = PyObject_Hash(key);
1482 if (hash == -1) {
1483 return NULL;
1484 }
1485 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001486
INADA Naokiba609772016-12-07 20:41:42 +09001487 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value, NULL);
Victor Stinner742da042016-09-07 17:40:12 -07001488 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001490 return value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001491}
1492
Brett Cannonfd074152012-04-14 14:10:13 -04001493PyObject *
1494_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1495{
1496 PyObject *kv;
1497 kv = _PyUnicode_FromId(key); /* borrowed */
1498 if (kv == NULL)
1499 return NULL;
1500 return PyDict_GetItemWithError(dp, kv);
1501}
1502
Victor Stinnerb4efc962015-11-20 09:24:02 +01001503/* Fast version of global value lookup (LOAD_GLOBAL).
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001504 * Lookup in globals, then builtins.
Victor Stinnerb4efc962015-11-20 09:24:02 +01001505 *
1506 * Raise an exception and return NULL if an error occurred (ex: computing the
1507 * key hash failed, key comparison failed, ...). Return NULL if the key doesn't
1508 * exist. Return the value if the key exists.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001509 */
1510PyObject *
1511_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001512{
Victor Stinner742da042016-09-07 17:40:12 -07001513 Py_ssize_t ix;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001514 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09001515 PyObject *value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001516
1517 if (!PyUnicode_CheckExact(key) ||
1518 (hash = ((PyASCIIObject *) key)->hash) == -1)
1519 {
1520 hash = PyObject_Hash(key);
1521 if (hash == -1)
1522 return NULL;
Antoine Pitroue965d972012-02-27 00:45:12 +01001523 }
Victor Stinnerb4efc962015-11-20 09:24:02 +01001524
1525 /* namespace 1: globals */
INADA Naokiba609772016-12-07 20:41:42 +09001526 ix = globals->ma_keys->dk_lookup(globals, key, hash, &value, NULL);
Victor Stinner742da042016-09-07 17:40:12 -07001527 if (ix == DKIX_ERROR)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001528 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001529 if (ix != DKIX_EMPTY && value != NULL)
1530 return value;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001531
1532 /* namespace 2: builtins */
INADA Naokiba609772016-12-07 20:41:42 +09001533 ix = builtins->ma_keys->dk_lookup(builtins, key, hash, &value, NULL);
Victor Stinner742da042016-09-07 17:40:12 -07001534 if (ix < 0)
Victor Stinnerb4efc962015-11-20 09:24:02 +01001535 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001536 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001537}
1538
Antoine Pitroue965d972012-02-27 00:45:12 +01001539/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1540 * dictionary if it's merely replacing the value for an existing key.
1541 * This means that it's safe to loop over a dictionary with PyDict_Next()
1542 * and occasionally replace a value -- but you can't insert new keys or
1543 * remove them.
1544 */
1545int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001546PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001547{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001548 PyDictObject *mp;
1549 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001550 if (!PyDict_Check(op)) {
1551 PyErr_BadInternalCall();
1552 return -1;
1553 }
1554 assert(key);
1555 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001556 mp = (PyDictObject *)op;
1557 if (!PyUnicode_CheckExact(key) ||
1558 (hash = ((PyASCIIObject *) key)->hash) == -1)
1559 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001560 hash = PyObject_Hash(key);
1561 if (hash == -1)
1562 return -1;
1563 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001564
1565 /* insertdict() handles any resizing that might be necessary */
1566 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001567}
1568
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001569int
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001570_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1571 Py_hash_t hash)
1572{
1573 PyDictObject *mp;
1574
1575 if (!PyDict_Check(op)) {
1576 PyErr_BadInternalCall();
1577 return -1;
1578 }
1579 assert(key);
1580 assert(value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001581 assert(hash != -1);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001582 mp = (PyDictObject *)op;
1583
1584 /* insertdict() handles any resizing that might be necessary */
1585 return insertdict(mp, key, hash, value);
1586}
1587
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001588static int
Antoine Pitroud741ed42016-12-27 14:23:43 +01001589delitem_common(PyDictObject *mp, Py_ssize_t hashpos, Py_ssize_t ix,
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001590 PyObject *old_value)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001591{
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001592 PyObject *old_key;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001593 PyDictKeyEntry *ep;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001594
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001595 mp->ma_used--;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001596 mp->ma_version_tag = DICT_NEXT_VERSION();
1597 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1598 dk_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
1599 ENSURE_ALLOWS_DELETIONS(mp);
1600 old_key = ep->me_key;
1601 ep->me_key = NULL;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001602 ep->me_value = NULL;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001603 Py_DECREF(old_key);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001604 Py_DECREF(old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001605
1606 assert(_PyDict_CheckConsistency(mp));
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001607 return 0;
1608}
1609
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001610int
Tim Peters1f5871e2000-07-04 17:44:48 +00001611PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001612{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001613 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 assert(key);
1615 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001616 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 hash = PyObject_Hash(key);
1618 if (hash == -1)
1619 return -1;
1620 }
Victor Stinner742da042016-09-07 17:40:12 -07001621
1622 return _PyDict_DelItem_KnownHash(op, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001623}
1624
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001625int
1626_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1627{
Victor Stinner742da042016-09-07 17:40:12 -07001628 Py_ssize_t hashpos, ix;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001629 PyDictObject *mp;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001630 PyObject *old_value;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001631
1632 if (!PyDict_Check(op)) {
1633 PyErr_BadInternalCall();
1634 return -1;
1635 }
1636 assert(key);
1637 assert(hash != -1);
1638 mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09001639 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value, &hashpos);
Victor Stinner742da042016-09-07 17:40:12 -07001640 if (ix == DKIX_ERROR)
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001641 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09001642 if (ix == DKIX_EMPTY || old_value == NULL) {
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001643 _PyErr_SetKeyError(key);
1644 return -1;
1645 }
Victor Stinner742da042016-09-07 17:40:12 -07001646 assert(dk_get_index(mp->ma_keys, hashpos) == ix);
Victor Stinner78601a32016-09-09 19:28:36 -07001647
1648 // Split table doesn't allow deletion. Combine it.
1649 if (_PyDict_HasSplitTable(mp)) {
1650 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1651 return -1;
1652 }
INADA Naokiba609772016-12-07 20:41:42 +09001653 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value, &hashpos);
Victor Stinner78601a32016-09-09 19:28:36 -07001654 assert(ix >= 0);
1655 }
1656
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001657 return delitem_common(mp, hashpos, ix, old_value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001658}
1659
Antoine Pitroud741ed42016-12-27 14:23:43 +01001660/* This function promises that the predicate -> deletion sequence is atomic
1661 * (i.e. protected by the GIL), assuming the predicate itself doesn't
1662 * release the GIL.
1663 */
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001664int
1665_PyDict_DelItemIf(PyObject *op, PyObject *key,
1666 int (*predicate)(PyObject *value))
1667{
Antoine Pitroud741ed42016-12-27 14:23:43 +01001668 Py_ssize_t hashpos, ix;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001669 PyDictObject *mp;
1670 Py_hash_t hash;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001671 PyObject *old_value;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001672 int res;
1673
1674 if (!PyDict_Check(op)) {
1675 PyErr_BadInternalCall();
1676 return -1;
1677 }
1678 assert(key);
1679 hash = PyObject_Hash(key);
1680 if (hash == -1)
1681 return -1;
1682 mp = (PyDictObject *)op;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001683 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value, &hashpos);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001684 if (ix == DKIX_ERROR)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001685 return -1;
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001686 if (ix == DKIX_EMPTY || old_value == NULL) {
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001687 _PyErr_SetKeyError(key);
1688 return -1;
1689 }
Antoine Pitroud741ed42016-12-27 14:23:43 +01001690 assert(dk_get_index(mp->ma_keys, hashpos) == ix);
1691
1692 // Split table doesn't allow deletion. Combine it.
1693 if (_PyDict_HasSplitTable(mp)) {
1694 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1695 return -1;
1696 }
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001697 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value, &hashpos);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001698 assert(ix >= 0);
1699 }
1700
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001701 res = predicate(old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001702 if (res == -1)
1703 return -1;
1704 if (res > 0)
Antoine Pitrouc06ae202016-12-27 14:34:54 +01001705 return delitem_common(mp, hashpos, ix, old_value);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001706 else
1707 return 0;
1708}
1709
1710
Guido van Rossum25831651993-05-19 14:50:45 +00001711void
Tim Peters1f5871e2000-07-04 17:44:48 +00001712PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001713{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001715 PyDictKeysObject *oldkeys;
1716 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 if (!PyDict_Check(op))
1720 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001721 mp = ((PyDictObject *)op);
1722 oldkeys = mp->ma_keys;
1723 oldvalues = mp->ma_values;
1724 if (oldvalues == empty_values)
1725 return;
1726 /* Empty the dict... */
1727 DK_INCREF(Py_EMPTY_KEYS);
1728 mp->ma_keys = Py_EMPTY_KEYS;
1729 mp->ma_values = empty_values;
1730 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001731 mp->ma_version_tag = DICT_NEXT_VERSION();
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001732 /* ...then clear the keys and values */
1733 if (oldvalues != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001734 n = oldkeys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001735 for (i = 0; i < n; i++)
1736 Py_CLEAR(oldvalues[i]);
1737 free_values(oldvalues);
1738 DK_DECREF(oldkeys);
1739 }
1740 else {
1741 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001742 DK_DECREF(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001743 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02001744 assert(_PyDict_CheckConsistency(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001745}
1746
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001747/* Internal version of PyDict_Next that returns a hash value in addition
1748 * to the key and value.
1749 * Return 1 on success, return 0 when the reached the end of the dictionary
1750 * (or if op is not a dictionary)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001751 */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001752int
1753_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1754 PyObject **pvalue, Py_hash_t *phash)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001755{
INADA Naokica2d8be2016-11-04 16:59:10 +09001756 Py_ssize_t i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001757 PyDictObject *mp;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001758 PyDictKeyEntry *entry_ptr;
1759 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001760
1761 if (!PyDict_Check(op))
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001762 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763 mp = (PyDictObject *)op;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001764 i = *ppos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001765 if (mp->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09001766 if (i < 0 || i >= mp->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001767 return 0;
INADA Naokica2d8be2016-11-04 16:59:10 +09001768 /* values of split table is always dense */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001769 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
INADA Naokica2d8be2016-11-04 16:59:10 +09001770 value = mp->ma_values[i];
1771 assert(value != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001773 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09001774 Py_ssize_t n = mp->ma_keys->dk_nentries;
1775 if (i < 0 || i >= n)
1776 return 0;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001777 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
1778 while (i < n && entry_ptr->me_value == NULL) {
1779 entry_ptr++;
1780 i++;
Victor Stinner742da042016-09-07 17:40:12 -07001781 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001782 if (i >= n)
1783 return 0;
1784 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001786 *ppos = i+1;
1787 if (pkey)
1788 *pkey = entry_ptr->me_key;
1789 if (phash)
1790 *phash = entry_ptr->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001791 if (pvalue)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001792 *pvalue = value;
1793 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001794}
1795
Tim Peters080c88b2003-02-15 03:01:11 +00001796/*
1797 * Iterate over a dict. Use like so:
1798 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001799 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001800 * PyObject *key, *value;
1801 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001802 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001803 * Refer to borrowed references in key and value.
Tim Peters080c88b2003-02-15 03:01:11 +00001804 * }
1805 *
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001806 * Return 1 on success, return 0 when the reached the end of the dictionary
1807 * (or if op is not a dictionary)
1808 *
Tim Peters080c88b2003-02-15 03:01:11 +00001809 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001810 * mutates the dict. One exception: it is safe if the loop merely changes
1811 * the values associated with the keys (but doesn't insert new keys or
1812 * delete keys), via PyDict_SetItem().
1813 */
Guido van Rossum25831651993-05-19 14:50:45 +00001814int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001815PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001816{
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001817 return _PyDict_Next(op, ppos, pkey, pvalue, NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001818}
1819
Eric Snow96c6af92015-05-29 22:21:39 -06001820/* Internal version of dict.pop(). */
1821PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001822_PyDict_Pop_KnownHash(PyObject *dict, PyObject *key, Py_hash_t hash, PyObject *deflt)
Eric Snow96c6af92015-05-29 22:21:39 -06001823{
Victor Stinner742da042016-09-07 17:40:12 -07001824 Py_ssize_t ix, hashpos;
Eric Snow96c6af92015-05-29 22:21:39 -06001825 PyObject *old_value, *old_key;
1826 PyDictKeyEntry *ep;
Yury Selivanov684ef2c2016-10-28 19:01:21 -04001827 PyDictObject *mp;
1828
1829 assert(PyDict_Check(dict));
1830 mp = (PyDictObject *)dict;
Eric Snow96c6af92015-05-29 22:21:39 -06001831
1832 if (mp->ma_used == 0) {
1833 if (deflt) {
1834 Py_INCREF(deflt);
1835 return deflt;
1836 }
1837 _PyErr_SetKeyError(key);
1838 return NULL;
1839 }
INADA Naokiba609772016-12-07 20:41:42 +09001840 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value, &hashpos);
Victor Stinner742da042016-09-07 17:40:12 -07001841 if (ix == DKIX_ERROR)
Eric Snow96c6af92015-05-29 22:21:39 -06001842 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001843 if (ix == DKIX_EMPTY || old_value == NULL) {
Eric Snow96c6af92015-05-29 22:21:39 -06001844 if (deflt) {
1845 Py_INCREF(deflt);
1846 return deflt;
1847 }
1848 _PyErr_SetKeyError(key);
1849 return NULL;
1850 }
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001851
Victor Stinner78601a32016-09-09 19:28:36 -07001852 // Split table doesn't allow deletion. Combine it.
1853 if (_PyDict_HasSplitTable(mp)) {
1854 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1855 return NULL;
1856 }
INADA Naokiba609772016-12-07 20:41:42 +09001857 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value, &hashpos);
Victor Stinner78601a32016-09-09 19:28:36 -07001858 assert(ix >= 0);
1859 }
1860
Victor Stinner78601a32016-09-09 19:28:36 -07001861 assert(old_value != NULL);
Eric Snow96c6af92015-05-29 22:21:39 -06001862 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001863 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner78601a32016-09-09 19:28:36 -07001864 dk_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
1865 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1866 ENSURE_ALLOWS_DELETIONS(mp);
1867 old_key = ep->me_key;
1868 ep->me_key = NULL;
INADA Naokiba609772016-12-07 20:41:42 +09001869 ep->me_value = NULL;
Victor Stinner78601a32016-09-09 19:28:36 -07001870 Py_DECREF(old_key);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001871
1872 assert(_PyDict_CheckConsistency(mp));
Eric Snow96c6af92015-05-29 22:21:39 -06001873 return old_value;
1874}
1875
Serhiy Storchaka67796522017-01-12 18:34:33 +02001876PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001877_PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt)
Serhiy Storchaka67796522017-01-12 18:34:33 +02001878{
1879 Py_hash_t hash;
1880
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001881 if (((PyDictObject *)dict)->ma_used == 0) {
Serhiy Storchaka67796522017-01-12 18:34:33 +02001882 if (deflt) {
1883 Py_INCREF(deflt);
1884 return deflt;
1885 }
1886 _PyErr_SetKeyError(key);
1887 return NULL;
1888 }
1889 if (!PyUnicode_CheckExact(key) ||
1890 (hash = ((PyASCIIObject *) key)->hash) == -1) {
1891 hash = PyObject_Hash(key);
1892 if (hash == -1)
1893 return NULL;
1894 }
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001895 return _PyDict_Pop_KnownHash(dict, key, hash, deflt);
Serhiy Storchaka67796522017-01-12 18:34:33 +02001896}
1897
Eric Snow96c6af92015-05-29 22:21:39 -06001898/* Internal version of dict.from_keys(). It is subclass-friendly. */
1899PyObject *
1900_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1901{
1902 PyObject *it; /* iter(iterable) */
1903 PyObject *key;
1904 PyObject *d;
1905 int status;
1906
Victor Stinnera5ed5f02016-12-06 18:45:50 +01001907 d = _PyObject_CallNoArg(cls);
Eric Snow96c6af92015-05-29 22:21:39 -06001908 if (d == NULL)
1909 return NULL;
1910
1911 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
1912 if (PyDict_CheckExact(iterable)) {
1913 PyDictObject *mp = (PyDictObject *)d;
1914 PyObject *oldvalue;
1915 Py_ssize_t pos = 0;
1916 PyObject *key;
1917 Py_hash_t hash;
1918
Serhiy Storchakac61ac162017-03-21 08:52:38 +02001919 if (dictresize(mp, ESTIMATE_SIZE(PyDict_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001920 Py_DECREF(d);
1921 return NULL;
1922 }
1923
1924 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
1925 if (insertdict(mp, key, hash, value)) {
1926 Py_DECREF(d);
1927 return NULL;
1928 }
1929 }
1930 return d;
1931 }
1932 if (PyAnySet_CheckExact(iterable)) {
1933 PyDictObject *mp = (PyDictObject *)d;
1934 Py_ssize_t pos = 0;
1935 PyObject *key;
1936 Py_hash_t hash;
1937
Victor Stinner742da042016-09-07 17:40:12 -07001938 if (dictresize(mp, ESTIMATE_SIZE(PySet_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001939 Py_DECREF(d);
1940 return NULL;
1941 }
1942
1943 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
1944 if (insertdict(mp, key, hash, value)) {
1945 Py_DECREF(d);
1946 return NULL;
1947 }
1948 }
1949 return d;
1950 }
1951 }
1952
1953 it = PyObject_GetIter(iterable);
1954 if (it == NULL){
1955 Py_DECREF(d);
1956 return NULL;
1957 }
1958
1959 if (PyDict_CheckExact(d)) {
1960 while ((key = PyIter_Next(it)) != NULL) {
1961 status = PyDict_SetItem(d, key, value);
1962 Py_DECREF(key);
1963 if (status < 0)
1964 goto Fail;
1965 }
1966 } else {
1967 while ((key = PyIter_Next(it)) != NULL) {
1968 status = PyObject_SetItem(d, key, value);
1969 Py_DECREF(key);
1970 if (status < 0)
1971 goto Fail;
1972 }
1973 }
1974
1975 if (PyErr_Occurred())
1976 goto Fail;
1977 Py_DECREF(it);
1978 return d;
1979
1980Fail:
1981 Py_DECREF(it);
1982 Py_DECREF(d);
1983 return NULL;
1984}
1985
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001986/* Methods */
1987
1988static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001989dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001990{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001991 PyObject **values = mp->ma_values;
1992 PyDictKeysObject *keys = mp->ma_keys;
1993 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001994 PyObject_GC_UnTrack(mp);
1995 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001996 if (values != NULL) {
1997 if (values != empty_values) {
Victor Stinner742da042016-09-07 17:40:12 -07001998 for (i = 0, n = mp->ma_keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001999 Py_XDECREF(values[i]);
2000 }
2001 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002002 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002003 DK_DECREF(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002005 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02002006 assert(keys->dk_refcnt == 1);
2007 DK_DECREF(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002008 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
2010 free_list[numfree++] = mp;
2011 else
2012 Py_TYPE(mp)->tp_free((PyObject *)mp);
2013 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002014}
2015
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002016
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002017static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002018dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002019{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002020 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01002021 PyObject *key = NULL, *value = NULL;
2022 _PyUnicodeWriter writer;
2023 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00002024
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 i = Py_ReprEnter((PyObject *)mp);
2026 if (i != 0) {
2027 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
2028 }
Guido van Rossum255443b1998-04-10 22:47:14 +00002029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002030 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01002031 Py_ReprLeave((PyObject *)mp);
2032 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002033 }
Tim Petersa7259592001-06-16 05:11:17 +00002034
Victor Stinnerf91929b2013-11-19 13:07:38 +01002035 _PyUnicodeWriter_Init(&writer);
2036 writer.overallocate = 1;
2037 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
2038 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00002039
Victor Stinnerf91929b2013-11-19 13:07:38 +01002040 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
2041 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00002042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002043 /* Do repr() on each key+value pair, and insert ": " between them.
2044 Note that repr may mutate the dict. */
2045 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01002046 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002047 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01002048 PyObject *s;
2049 int res;
2050
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002051 /* Prevent repr from deleting key or value during key format. */
2052 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02002054
Victor Stinnerf91929b2013-11-19 13:07:38 +01002055 if (!first) {
2056 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
2057 goto error;
2058 }
2059 first = 0;
2060
2061 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01002063 goto error;
2064 res = _PyUnicodeWriter_WriteStr(&writer, s);
2065 Py_DECREF(s);
2066 if (res < 0)
2067 goto error;
2068
2069 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
2070 goto error;
2071
2072 s = PyObject_Repr(value);
2073 if (s == NULL)
2074 goto error;
2075 res = _PyUnicodeWriter_WriteStr(&writer, s);
2076 Py_DECREF(s);
2077 if (res < 0)
2078 goto error;
2079
2080 Py_CLEAR(key);
2081 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002082 }
Tim Petersa7259592001-06-16 05:11:17 +00002083
Victor Stinnerf91929b2013-11-19 13:07:38 +01002084 writer.overallocate = 0;
2085 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
2086 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00002087
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002088 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01002089
2090 return _PyUnicodeWriter_Finish(&writer);
2091
2092error:
2093 Py_ReprLeave((PyObject *)mp);
2094 _PyUnicodeWriter_Dealloc(&writer);
2095 Py_XDECREF(key);
2096 Py_XDECREF(value);
2097 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002098}
2099
Martin v. Löwis18e16552006-02-15 17:27:45 +00002100static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002101dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002102{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002103 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002104}
2105
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002106static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002107dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002108{
Victor Stinner742da042016-09-07 17:40:12 -07002109 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002110 Py_hash_t hash;
INADA Naokiba609772016-12-07 20:41:42 +09002111 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002113 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002114 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002115 hash = PyObject_Hash(key);
2116 if (hash == -1)
2117 return NULL;
2118 }
INADA Naokiba609772016-12-07 20:41:42 +09002119 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value, NULL);
Victor Stinner742da042016-09-07 17:40:12 -07002120 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002122 if (ix == DKIX_EMPTY || value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002123 if (!PyDict_CheckExact(mp)) {
2124 /* Look up __missing__ method if we're a subclass. */
2125 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05002126 _Py_IDENTIFIER(__missing__);
2127 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002128 if (missing != NULL) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01002129 res = PyObject_CallFunctionObjArgs(missing,
2130 key, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002131 Py_DECREF(missing);
2132 return res;
2133 }
2134 else if (PyErr_Occurred())
2135 return NULL;
2136 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07002137 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138 return NULL;
2139 }
INADA Naokiba609772016-12-07 20:41:42 +09002140 Py_INCREF(value);
2141 return value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002142}
2143
2144static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002145dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002146{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002147 if (w == NULL)
2148 return PyDict_DelItem((PyObject *)mp, v);
2149 else
2150 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002151}
2152
Guido van Rossuma9e7a811997-05-13 21:02:11 +00002153static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154 (lenfunc)dict_length, /*mp_length*/
2155 (binaryfunc)dict_subscript, /*mp_subscript*/
2156 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002157};
2158
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002159static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002160dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002161{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002162 PyObject *v;
2163 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002164 PyDictKeyEntry *ep;
2165 Py_ssize_t size, n, offset;
2166 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002167
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002168 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169 n = mp->ma_used;
2170 v = PyList_New(n);
2171 if (v == NULL)
2172 return NULL;
2173 if (n != mp->ma_used) {
2174 /* Durnit. The allocations caused the dict to resize.
2175 * Just start over, this shouldn't normally happen.
2176 */
2177 Py_DECREF(v);
2178 goto again;
2179 }
Victor Stinner742da042016-09-07 17:40:12 -07002180 ep = DK_ENTRIES(mp->ma_keys);
2181 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002182 if (mp->ma_values) {
2183 value_ptr = mp->ma_values;
2184 offset = sizeof(PyObject *);
2185 }
2186 else {
2187 value_ptr = &ep[0].me_value;
2188 offset = sizeof(PyDictKeyEntry);
2189 }
2190 for (i = 0, j = 0; i < size; i++) {
2191 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 PyObject *key = ep[i].me_key;
2193 Py_INCREF(key);
2194 PyList_SET_ITEM(v, j, key);
2195 j++;
2196 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002197 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198 }
2199 assert(j == n);
2200 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002201}
2202
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002203static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002204dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002205{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002206 PyObject *v;
2207 Py_ssize_t i, j;
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002208 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002209 Py_ssize_t size, n, offset;
2210 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002211
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002212 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213 n = mp->ma_used;
2214 v = PyList_New(n);
2215 if (v == NULL)
2216 return NULL;
2217 if (n != mp->ma_used) {
2218 /* Durnit. The allocations caused the dict to resize.
2219 * Just start over, this shouldn't normally happen.
2220 */
2221 Py_DECREF(v);
2222 goto again;
2223 }
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002224 ep = DK_ENTRIES(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002225 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002226 if (mp->ma_values) {
2227 value_ptr = mp->ma_values;
2228 offset = sizeof(PyObject *);
2229 }
2230 else {
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002231 value_ptr = &ep[0].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002232 offset = sizeof(PyDictKeyEntry);
2233 }
2234 for (i = 0, j = 0; i < size; i++) {
2235 PyObject *value = *value_ptr;
2236 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2237 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002238 Py_INCREF(value);
2239 PyList_SET_ITEM(v, j, value);
2240 j++;
2241 }
2242 }
2243 assert(j == n);
2244 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002245}
2246
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002247static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002248dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002249{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002250 PyObject *v;
2251 Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002252 Py_ssize_t size, offset;
2253 PyObject *item, *key;
2254 PyDictKeyEntry *ep;
2255 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 /* Preallocate the list of tuples, to avoid allocations during
2258 * the loop over the items, which could trigger GC, which
2259 * could resize the dict. :-(
2260 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002261 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262 n = mp->ma_used;
2263 v = PyList_New(n);
2264 if (v == NULL)
2265 return NULL;
2266 for (i = 0; i < n; i++) {
2267 item = PyTuple_New(2);
2268 if (item == NULL) {
2269 Py_DECREF(v);
2270 return NULL;
2271 }
2272 PyList_SET_ITEM(v, i, item);
2273 }
2274 if (n != mp->ma_used) {
2275 /* Durnit. The allocations caused the dict to resize.
2276 * Just start over, this shouldn't normally happen.
2277 */
2278 Py_DECREF(v);
2279 goto again;
2280 }
2281 /* Nothing we do below makes any function calls. */
Victor Stinner742da042016-09-07 17:40:12 -07002282 ep = DK_ENTRIES(mp->ma_keys);
2283 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002284 if (mp->ma_values) {
2285 value_ptr = mp->ma_values;
2286 offset = sizeof(PyObject *);
2287 }
2288 else {
2289 value_ptr = &ep[0].me_value;
2290 offset = sizeof(PyDictKeyEntry);
2291 }
2292 for (i = 0, j = 0; i < size; i++) {
2293 PyObject *value = *value_ptr;
2294 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2295 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002296 key = ep[i].me_key;
2297 item = PyList_GET_ITEM(v, j);
2298 Py_INCREF(key);
2299 PyTuple_SET_ITEM(item, 0, key);
2300 Py_INCREF(value);
2301 PyTuple_SET_ITEM(item, 1, value);
2302 j++;
2303 }
2304 }
2305 assert(j == n);
2306 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002307}
2308
Larry Hastings5c661892014-01-24 06:17:25 -08002309/*[clinic input]
2310@classmethod
2311dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08002312 iterable: object
2313 value: object=None
2314 /
2315
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002316Create a new dictionary with keys from iterable and values set to value.
Larry Hastings5c661892014-01-24 06:17:25 -08002317[clinic start generated code]*/
2318
Larry Hastings5c661892014-01-24 06:17:25 -08002319static PyObject *
2320dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002321/*[clinic end generated code: output=8fb98e4b10384999 input=382ba4855d0f74c3]*/
Larry Hastings5c661892014-01-24 06:17:25 -08002322{
Eric Snow96c6af92015-05-29 22:21:39 -06002323 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002324}
2325
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002326static int
Victor Stinner742da042016-09-07 17:40:12 -07002327dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
2328 const char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002329{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002330 PyObject *arg = NULL;
2331 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002333 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
2334 result = -1;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002337 _Py_IDENTIFIER(keys);
2338 if (_PyObject_HasAttrId(arg, &PyId_keys))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002339 result = PyDict_Merge(self, arg, 1);
2340 else
2341 result = PyDict_MergeFromSeq2(self, arg, 1);
2342 }
2343 if (result == 0 && kwds != NULL) {
2344 if (PyArg_ValidateKeywordArguments(kwds))
2345 result = PyDict_Merge(self, kwds, 1);
2346 else
2347 result = -1;
2348 }
2349 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002350}
2351
Victor Stinner91f0d4a2017-01-19 12:45:06 +01002352/* Note: dict.update() uses the METH_VARARGS|METH_KEYWORDS calling convention.
2353 Using METH_FASTCALL would make dict.update(**dict2) calls slower, see the
2354 issue #29312. */
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002355static PyObject *
2356dict_update(PyObject *self, PyObject *args, PyObject *kwds)
2357{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 if (dict_update_common(self, args, kwds, "update") != -1)
2359 Py_RETURN_NONE;
2360 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002361}
2362
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002363/* Update unconditionally replaces existing items.
2364 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00002365 otherwise it leaves existing items unchanged.
2366
2367 PyDict_{Update,Merge} update/merge from a mapping object.
2368
Tim Petersf582b822001-12-11 18:51:08 +00002369 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00002370 producing iterable objects of length 2.
2371*/
2372
Tim Petersf582b822001-12-11 18:51:08 +00002373int
Tim Peters1fc240e2001-10-26 05:06:50 +00002374PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
2375{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 PyObject *it; /* iter(seq2) */
2377 Py_ssize_t i; /* index into seq2 of current element */
2378 PyObject *item; /* seq2[i] */
2379 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00002380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002381 assert(d != NULL);
2382 assert(PyDict_Check(d));
2383 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00002384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002385 it = PyObject_GetIter(seq2);
2386 if (it == NULL)
2387 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 for (i = 0; ; ++i) {
2390 PyObject *key, *value;
2391 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00002392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 fast = NULL;
2394 item = PyIter_Next(it);
2395 if (item == NULL) {
2396 if (PyErr_Occurred())
2397 goto Fail;
2398 break;
2399 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 /* Convert item to sequence, and verify length 2. */
2402 fast = PySequence_Fast(item, "");
2403 if (fast == NULL) {
2404 if (PyErr_ExceptionMatches(PyExc_TypeError))
2405 PyErr_Format(PyExc_TypeError,
2406 "cannot convert dictionary update "
2407 "sequence element #%zd to a sequence",
2408 i);
2409 goto Fail;
2410 }
2411 n = PySequence_Fast_GET_SIZE(fast);
2412 if (n != 2) {
2413 PyErr_Format(PyExc_ValueError,
2414 "dictionary update sequence element #%zd "
2415 "has length %zd; 2 is required",
2416 i, n);
2417 goto Fail;
2418 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002420 /* Update/merge with this (key, value) pair. */
2421 key = PySequence_Fast_GET_ITEM(fast, 0);
2422 value = PySequence_Fast_GET_ITEM(fast, 1);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002423 Py_INCREF(key);
2424 Py_INCREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002425 if (override || PyDict_GetItem(d, key) == NULL) {
2426 int status = PyDict_SetItem(d, key, value);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002427 if (status < 0) {
2428 Py_DECREF(key);
2429 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002430 goto Fail;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002431 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002433 Py_DECREF(key);
2434 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 Py_DECREF(fast);
2436 Py_DECREF(item);
2437 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 i = 0;
Victor Stinner611b0fa2016-09-14 15:02:01 +02002440 assert(_PyDict_CheckConsistency((PyDictObject *)d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002441 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002442Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002443 Py_XDECREF(item);
2444 Py_XDECREF(fast);
2445 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002446Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002447 Py_DECREF(it);
2448 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002449}
2450
doko@ubuntu.comc96df682016-10-11 08:04:02 +02002451static int
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002452dict_merge(PyObject *a, PyObject *b, int override)
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002453{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002454 PyDictObject *mp, *other;
2455 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -07002456 PyDictKeyEntry *entry, *ep0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002457
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002458 assert(0 <= override && override <= 2);
2459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002460 /* We accept for the argument either a concrete dictionary object,
2461 * or an abstract "mapping" object. For the former, we can do
2462 * things quite efficiently. For the latter, we only require that
2463 * PyMapping_Keys() and PyObject_GetItem() be supported.
2464 */
2465 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2466 PyErr_BadInternalCall();
2467 return -1;
2468 }
2469 mp = (PyDictObject*)a;
2470 if (PyDict_Check(b)) {
2471 other = (PyDictObject*)b;
2472 if (other == mp || other->ma_used == 0)
2473 /* a.update(a) or a.update({}); nothing to do */
2474 return 0;
2475 if (mp->ma_used == 0)
2476 /* Since the target dict is empty, PyDict_GetItem()
2477 * always returns NULL. Setting override to 1
2478 * skips the unnecessary test.
2479 */
2480 override = 1;
2481 /* Do one big resize at the start, rather than
2482 * incrementally resizing as we insert new items. Expect
2483 * that there will be no (or few) overlapping keys.
2484 */
INADA Naokib1152be2016-10-27 19:26:50 +09002485 if (USABLE_FRACTION(mp->ma_keys->dk_size) < other->ma_used) {
2486 if (dictresize(mp, ESTIMATE_SIZE(mp->ma_used + other->ma_used))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002487 return -1;
INADA Naokib1152be2016-10-27 19:26:50 +09002488 }
2489 }
Victor Stinner742da042016-09-07 17:40:12 -07002490 ep0 = DK_ENTRIES(other->ma_keys);
2491 for (i = 0, n = other->ma_keys->dk_nentries; i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002492 PyObject *key, *value;
2493 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002494 entry = &ep0[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002495 key = entry->me_key;
2496 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002497 if (other->ma_values)
2498 value = other->ma_values[i];
2499 else
2500 value = entry->me_value;
2501
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002502 if (value != NULL) {
2503 int err = 0;
2504 Py_INCREF(key);
2505 Py_INCREF(value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002506 if (override == 1)
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002507 err = insertdict(mp, key, hash, value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002508 else if (_PyDict_GetItem_KnownHash(a, key, hash) == NULL) {
2509 if (PyErr_Occurred()) {
2510 Py_DECREF(value);
2511 Py_DECREF(key);
2512 return -1;
2513 }
2514 err = insertdict(mp, key, hash, value);
2515 }
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002516 else if (override != 0) {
2517 _PyErr_SetKeyError(key);
2518 Py_DECREF(value);
2519 Py_DECREF(key);
2520 return -1;
2521 }
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002522 Py_DECREF(value);
2523 Py_DECREF(key);
2524 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002525 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002526
Victor Stinner742da042016-09-07 17:40:12 -07002527 if (n != other->ma_keys->dk_nentries) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002528 PyErr_SetString(PyExc_RuntimeError,
2529 "dict mutated during update");
2530 return -1;
2531 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002532 }
2533 }
2534 }
2535 else {
2536 /* Do it the generic, slower way */
2537 PyObject *keys = PyMapping_Keys(b);
2538 PyObject *iter;
2539 PyObject *key, *value;
2540 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002542 if (keys == NULL)
2543 /* Docstring says this is equivalent to E.keys() so
2544 * if E doesn't have a .keys() method we want
2545 * AttributeError to percolate up. Might as well
2546 * do the same for any other error.
2547 */
2548 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002550 iter = PyObject_GetIter(keys);
2551 Py_DECREF(keys);
2552 if (iter == NULL)
2553 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002555 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002556 if (override != 1 && PyDict_GetItem(a, key) != NULL) {
2557 if (override != 0) {
2558 _PyErr_SetKeyError(key);
2559 Py_DECREF(key);
2560 Py_DECREF(iter);
2561 return -1;
2562 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002563 Py_DECREF(key);
2564 continue;
2565 }
2566 value = PyObject_GetItem(b, key);
2567 if (value == NULL) {
2568 Py_DECREF(iter);
2569 Py_DECREF(key);
2570 return -1;
2571 }
2572 status = PyDict_SetItem(a, key, value);
2573 Py_DECREF(key);
2574 Py_DECREF(value);
2575 if (status < 0) {
2576 Py_DECREF(iter);
2577 return -1;
2578 }
2579 }
2580 Py_DECREF(iter);
2581 if (PyErr_Occurred())
2582 /* Iterator completed, via error */
2583 return -1;
2584 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02002585 assert(_PyDict_CheckConsistency((PyDictObject *)a));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002586 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002587}
2588
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002589int
2590PyDict_Update(PyObject *a, PyObject *b)
2591{
2592 return dict_merge(a, b, 1);
2593}
2594
2595int
2596PyDict_Merge(PyObject *a, PyObject *b, int override)
2597{
2598 /* XXX Deprecate override not in (0, 1). */
2599 return dict_merge(a, b, override != 0);
2600}
2601
2602int
2603_PyDict_MergeEx(PyObject *a, PyObject *b, int override)
2604{
2605 return dict_merge(a, b, override);
2606}
2607
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002608static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002609dict_copy(PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002610{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002611 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002612}
2613
2614PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002615PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002616{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002617 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002618 PyDictObject *mp;
2619 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002621 if (o == NULL || !PyDict_Check(o)) {
2622 PyErr_BadInternalCall();
2623 return NULL;
2624 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002625 mp = (PyDictObject *)o;
2626 if (_PyDict_HasSplitTable(mp)) {
2627 PyDictObject *split_copy;
Victor Stinner742da042016-09-07 17:40:12 -07002628 Py_ssize_t size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
2629 PyObject **newvalues;
2630 newvalues = new_values(size);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002631 if (newvalues == NULL)
2632 return PyErr_NoMemory();
2633 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2634 if (split_copy == NULL) {
2635 free_values(newvalues);
2636 return NULL;
2637 }
2638 split_copy->ma_values = newvalues;
2639 split_copy->ma_keys = mp->ma_keys;
2640 split_copy->ma_used = mp->ma_used;
2641 DK_INCREF(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002642 for (i = 0, n = size; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002643 PyObject *value = mp->ma_values[i];
2644 Py_XINCREF(value);
2645 split_copy->ma_values[i] = value;
2646 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002647 if (_PyObject_GC_IS_TRACKED(mp))
2648 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002649 return (PyObject *)split_copy;
2650 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002651 copy = PyDict_New();
2652 if (copy == NULL)
2653 return NULL;
2654 if (PyDict_Merge(copy, o, 1) == 0)
2655 return copy;
2656 Py_DECREF(copy);
2657 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002658}
2659
Martin v. Löwis18e16552006-02-15 17:27:45 +00002660Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002661PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002662{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002663 if (mp == NULL || !PyDict_Check(mp)) {
2664 PyErr_BadInternalCall();
2665 return -1;
2666 }
2667 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002668}
2669
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002670PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002671PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002672{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002673 if (mp == NULL || !PyDict_Check(mp)) {
2674 PyErr_BadInternalCall();
2675 return NULL;
2676 }
2677 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002678}
2679
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002680PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002681PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002682{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002683 if (mp == NULL || !PyDict_Check(mp)) {
2684 PyErr_BadInternalCall();
2685 return NULL;
2686 }
2687 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002688}
2689
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002690PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002691PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002692{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002693 if (mp == NULL || !PyDict_Check(mp)) {
2694 PyErr_BadInternalCall();
2695 return NULL;
2696 }
2697 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002698}
2699
Tim Peterse63415e2001-05-08 04:38:29 +00002700/* Return 1 if dicts equal, 0 if not, -1 if error.
2701 * Gets out as soon as any difference is detected.
2702 * Uses only Py_EQ comparison.
2703 */
2704static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002705dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002706{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002707 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002709 if (a->ma_used != b->ma_used)
2710 /* can't be equal if # of entries differ */
2711 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002712 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Victor Stinner742da042016-09-07 17:40:12 -07002713 for (i = 0; i < a->ma_keys->dk_nentries; i++) {
2714 PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002715 PyObject *aval;
2716 if (a->ma_values)
2717 aval = a->ma_values[i];
2718 else
2719 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002720 if (aval != NULL) {
2721 int cmp;
2722 PyObject *bval;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002723 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002724 /* temporarily bump aval's refcount to ensure it stays
2725 alive until we're done with it */
2726 Py_INCREF(aval);
2727 /* ditto for key */
2728 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002729 /* reuse the known hash value */
INADA Naokiba609772016-12-07 20:41:42 +09002730 b->ma_keys->dk_lookup(b, key, ep->me_hash, &bval, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002731 if (bval == NULL) {
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002732 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002733 Py_DECREF(aval);
2734 if (PyErr_Occurred())
2735 return -1;
2736 return 0;
2737 }
2738 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03002739 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002740 Py_DECREF(aval);
2741 if (cmp <= 0) /* error or not equal */
2742 return cmp;
2743 }
2744 }
2745 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002746}
Tim Peterse63415e2001-05-08 04:38:29 +00002747
2748static PyObject *
2749dict_richcompare(PyObject *v, PyObject *w, int op)
2750{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 int cmp;
2752 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002754 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2755 res = Py_NotImplemented;
2756 }
2757 else if (op == Py_EQ || op == Py_NE) {
2758 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2759 if (cmp < 0)
2760 return NULL;
2761 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2762 }
2763 else
2764 res = Py_NotImplemented;
2765 Py_INCREF(res);
2766 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002767}
Tim Peterse63415e2001-05-08 04:38:29 +00002768
Larry Hastings61272b72014-01-07 12:41:53 -08002769/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002770
2771@coexist
2772dict.__contains__
2773
2774 key: object
2775 /
2776
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002777True if the dictionary has the specified key, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002778[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002779
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002780static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002781dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka19d25972017-02-04 08:05:07 +02002782/*[clinic end generated code: output=a3d03db709ed6e6b input=fe1cb42ad831e820]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002783{
Larry Hastingsc2047262014-01-25 20:43:29 -08002784 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002785 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002786 Py_ssize_t ix;
INADA Naokiba609772016-12-07 20:41:42 +09002787 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002788
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002789 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002790 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002791 hash = PyObject_Hash(key);
2792 if (hash == -1)
2793 return NULL;
2794 }
INADA Naokiba609772016-12-07 20:41:42 +09002795 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value, NULL);
Victor Stinner742da042016-09-07 17:40:12 -07002796 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002797 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002798 if (ix == DKIX_EMPTY || value == NULL)
Victor Stinner742da042016-09-07 17:40:12 -07002799 Py_RETURN_FALSE;
2800 Py_RETURN_TRUE;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002801}
2802
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002803/*[clinic input]
2804dict.get
2805
2806 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002807 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002808 /
2809
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002810Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002811[clinic start generated code]*/
2812
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002813static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002814dict_get_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002815/*[clinic end generated code: output=bba707729dee05bf input=279ddb5790b6b107]*/
Barry Warsawc38c5da1997-10-06 17:49:20 +00002816{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002817 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002818 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002819 Py_ssize_t ix;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002821 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002822 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002823 hash = PyObject_Hash(key);
2824 if (hash == -1)
2825 return NULL;
2826 }
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002827 ix = (self->ma_keys->dk_lookup) (self, key, hash, &val, NULL);
Victor Stinner742da042016-09-07 17:40:12 -07002828 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002829 return NULL;
INADA Naokiba609772016-12-07 20:41:42 +09002830 if (ix == DKIX_EMPTY || val == NULL) {
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002831 val = default_value;
INADA Naokiba609772016-12-07 20:41:42 +09002832 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002833 Py_INCREF(val);
2834 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002835}
2836
Benjamin Peterson00e98862013-03-07 22:16:29 -05002837PyObject *
2838PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002839{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002840 PyDictObject *mp = (PyDictObject *)d;
INADA Naoki93f26f72016-11-02 18:45:16 +09002841 PyObject *value;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002842 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002843 Py_ssize_t hashpos, ix;
Guido van Rossum164452c2000-08-08 16:12:54 +00002844
Benjamin Peterson00e98862013-03-07 22:16:29 -05002845 if (!PyDict_Check(d)) {
2846 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002847 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002848 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002850 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002851 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002852 hash = PyObject_Hash(key);
2853 if (hash == -1)
2854 return NULL;
2855 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002856
2857 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
2858 if (insertion_resize(mp) < 0)
2859 return NULL;
2860 }
2861
INADA Naokiba609772016-12-07 20:41:42 +09002862 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value, &hashpos);
Victor Stinner742da042016-09-07 17:40:12 -07002863 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002864 return NULL;
INADA Naoki93f26f72016-11-02 18:45:16 +09002865
2866 if (_PyDict_HasSplitTable(mp) &&
INADA Naokiba609772016-12-07 20:41:42 +09002867 ((ix >= 0 && value == NULL && mp->ma_used != ix) ||
INADA Naoki93f26f72016-11-02 18:45:16 +09002868 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
2869 if (insertion_resize(mp) < 0) {
2870 return NULL;
2871 }
INADA Naokiba609772016-12-07 20:41:42 +09002872 hashpos = find_empty_slot(mp->ma_keys, key, hash);
INADA Naoki93f26f72016-11-02 18:45:16 +09002873 ix = DKIX_EMPTY;
2874 }
2875
2876 if (ix == DKIX_EMPTY) {
2877 PyDictKeyEntry *ep, *ep0;
2878 value = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002879 if (mp->ma_keys->dk_usable <= 0) {
Victor Stinner3c336c52016-09-12 14:17:40 +02002880 if (insertion_resize(mp) < 0) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002881 return NULL;
Victor Stinner3c336c52016-09-12 14:17:40 +02002882 }
INADA Naokiba609772016-12-07 20:41:42 +09002883 hashpos = find_empty_slot(mp->ma_keys, key, hash);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002884 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002885 ep0 = DK_ENTRIES(mp->ma_keys);
2886 ep = &ep0[mp->ma_keys->dk_nentries];
2887 dk_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002888 Py_INCREF(key);
INADA Naoki93f26f72016-11-02 18:45:16 +09002889 Py_INCREF(value);
2890 MAINTAIN_TRACKING(mp, key, value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002891 ep->me_key = key;
2892 ep->me_hash = hash;
INADA Naokiba609772016-12-07 20:41:42 +09002893 if (_PyDict_HasSplitTable(mp)) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002894 assert(mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
2895 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Victor Stinner742da042016-09-07 17:40:12 -07002896 }
2897 else {
INADA Naoki93f26f72016-11-02 18:45:16 +09002898 ep->me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07002899 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002900 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002901 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naoki93f26f72016-11-02 18:45:16 +09002902 mp->ma_keys->dk_usable--;
2903 mp->ma_keys->dk_nentries++;
2904 assert(mp->ma_keys->dk_usable >= 0);
2905 }
INADA Naokiba609772016-12-07 20:41:42 +09002906 else if (value == NULL) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002907 value = defaultobj;
2908 assert(_PyDict_HasSplitTable(mp));
2909 assert(ix == mp->ma_used);
2910 Py_INCREF(value);
2911 MAINTAIN_TRACKING(mp, key, value);
INADA Naokiba609772016-12-07 20:41:42 +09002912 mp->ma_values[ix] = value;
INADA Naoki93f26f72016-11-02 18:45:16 +09002913 mp->ma_used++;
2914 mp->ma_version_tag = DICT_NEXT_VERSION();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002915 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002916
2917 assert(_PyDict_CheckConsistency(mp));
2918 return value;
Guido van Rossum164452c2000-08-08 16:12:54 +00002919}
2920
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002921/*[clinic input]
2922dict.setdefault
2923
2924 key: object
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002925 default: object = None
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002926 /
2927
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002928Insert key with a value of default if key is not in the dictionary.
2929
2930Return the value for key if key is in the dictionary, else default.
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002931[clinic start generated code]*/
2932
Benjamin Peterson00e98862013-03-07 22:16:29 -05002933static PyObject *
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002934dict_setdefault_impl(PyDictObject *self, PyObject *key,
2935 PyObject *default_value)
Serhiy Storchaka78d9e582017-01-25 00:30:04 +02002936/*[clinic end generated code: output=f8c1101ebf69e220 input=0f063756e815fd9d]*/
Benjamin Peterson00e98862013-03-07 22:16:29 -05002937{
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01002938 PyObject *val;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002939
Serhiy Storchaka48088ee2017-01-19 19:00:30 +02002940 val = PyDict_SetDefault((PyObject *)self, key, default_value);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002941 Py_XINCREF(val);
2942 return val;
2943}
Guido van Rossum164452c2000-08-08 16:12:54 +00002944
2945static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002946dict_clear(PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002947{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002948 PyDict_Clear((PyObject *)mp);
2949 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002950}
2951
Guido van Rossumba6ab842000-12-12 22:02:18 +00002952static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002953dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002954{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002955 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00002956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002957 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2958 return NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06002959
Yury Selivanov684ef2c2016-10-28 19:01:21 -04002960 return _PyDict_Pop((PyObject*)mp, key, deflt);
Guido van Rossume027d982002-04-12 15:11:59 +00002961}
2962
2963static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002964dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002965{
Victor Stinner742da042016-09-07 17:40:12 -07002966 Py_ssize_t i, j;
2967 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002968 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002970 /* Allocate the result tuple before checking the size. Believe it
2971 * or not, this allocation could trigger a garbage collection which
2972 * could empty the dict, so if we checked the size first and that
2973 * happened, the result would be an infinite loop (searching for an
2974 * entry that no longer exists). Note that the usual popitem()
2975 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2976 * tuple away if the dict *is* empty isn't a significant
2977 * inefficiency -- possible, but unlikely in practice.
2978 */
2979 res = PyTuple_New(2);
2980 if (res == NULL)
2981 return NULL;
2982 if (mp->ma_used == 0) {
2983 Py_DECREF(res);
2984 PyErr_SetString(PyExc_KeyError,
2985 "popitem(): dictionary is empty");
2986 return NULL;
2987 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002988 /* Convert split table to combined table */
2989 if (mp->ma_keys->dk_lookup == lookdict_split) {
2990 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2991 Py_DECREF(res);
2992 return NULL;
2993 }
2994 }
2995 ENSURE_ALLOWS_DELETIONS(mp);
Victor Stinner742da042016-09-07 17:40:12 -07002996
2997 /* Pop last item */
2998 ep0 = DK_ENTRIES(mp->ma_keys);
2999 i = mp->ma_keys->dk_nentries - 1;
3000 while (i >= 0 && ep0[i].me_value == NULL) {
3001 i--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003002 }
Victor Stinner742da042016-09-07 17:40:12 -07003003 assert(i >= 0);
3004
3005 ep = &ep0[i];
3006 j = lookdict_index(mp->ma_keys, ep->me_hash, i);
3007 assert(j >= 0);
3008 assert(dk_get_index(mp->ma_keys, j) == i);
3009 dk_set_index(mp->ma_keys, j, DKIX_DUMMY);
3010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003011 PyTuple_SET_ITEM(res, 0, ep->me_key);
3012 PyTuple_SET_ITEM(res, 1, ep->me_value);
Victor Stinner742da042016-09-07 17:40:12 -07003013 ep->me_key = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003014 ep->me_value = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07003015 /* We can't dk_usable++ since there is DKIX_DUMMY in indices */
3016 mp->ma_keys->dk_nentries = i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003017 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003018 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02003019 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003020 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00003021}
3022
Jeremy Hylton8caad492000-06-23 14:18:11 +00003023static int
3024dict_traverse(PyObject *op, visitproc visit, void *arg)
3025{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003026 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson55f44522016-09-05 12:12:59 -07003027 PyDictKeysObject *keys = mp->ma_keys;
Serhiy Storchaka46825d22016-09-26 21:29:34 +03003028 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Victor Stinner742da042016-09-07 17:40:12 -07003029 Py_ssize_t i, n = keys->dk_nentries;
3030
Benjamin Peterson55f44522016-09-05 12:12:59 -07003031 if (keys->dk_lookup == lookdict) {
3032 for (i = 0; i < n; i++) {
3033 if (entries[i].me_value != NULL) {
3034 Py_VISIT(entries[i].me_value);
3035 Py_VISIT(entries[i].me_key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003036 }
3037 }
Victor Stinner742da042016-09-07 17:40:12 -07003038 }
3039 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003040 if (mp->ma_values != NULL) {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003041 for (i = 0; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003042 Py_VISIT(mp->ma_values[i]);
3043 }
3044 }
3045 else {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003046 for (i = 0; i < n; i++) {
3047 Py_VISIT(entries[i].me_value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003048 }
3049 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003050 }
3051 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003052}
3053
3054static int
3055dict_tp_clear(PyObject *op)
3056{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003057 PyDict_Clear(op);
3058 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003059}
3060
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003061static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003062
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003063Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003064_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003065{
Victor Stinner742da042016-09-07 17:40:12 -07003066 Py_ssize_t size, usable, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003067
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003068 size = DK_SIZE(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07003069 usable = USABLE_FRACTION(size);
3070
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02003071 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003072 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -07003073 res += usable * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003074 /* If the dictionary is split, the keys portion is accounted-for
3075 in the type object. */
3076 if (mp->ma_keys->dk_refcnt == 1)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003077 res += (sizeof(PyDictKeysObject)
3078 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
3079 + DK_IXSIZE(mp->ma_keys) * size
3080 + sizeof(PyDictKeyEntry) * usable);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003081 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003082}
3083
3084Py_ssize_t
3085_PyDict_KeysSize(PyDictKeysObject *keys)
3086{
Victor Stinner98ee9d52016-09-08 09:33:56 -07003087 return (sizeof(PyDictKeysObject)
3088 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
3089 + DK_IXSIZE(keys) * DK_SIZE(keys)
3090 + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003091}
3092
doko@ubuntu.com17210f52016-01-14 14:04:59 +01003093static PyObject *
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003094dict_sizeof(PyDictObject *mp)
3095{
3096 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
3097}
3098
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00003099PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
3100
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003101PyDoc_STRVAR(sizeof__doc__,
3102"D.__sizeof__() -> size of D in memory, in bytes");
3103
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003104PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00003105"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00003106If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00003107
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003108PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00003109"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000031102-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003111
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003112PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04003113"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
3114If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
3115If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
3116In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00003117
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003118PyDoc_STRVAR(clear__doc__,
3119"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003120
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003121PyDoc_STRVAR(copy__doc__,
3122"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00003123
Guido van Rossumb90c8482007-02-10 01:11:45 +00003124/* Forward */
3125static PyObject *dictkeys_new(PyObject *);
3126static PyObject *dictitems_new(PyObject *);
3127static PyObject *dictvalues_new(PyObject *);
3128
Guido van Rossum45c85d12007-07-27 16:31:40 +00003129PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003130 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003131PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003132 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003133PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003134 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00003135
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003136static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07003137 DICT___CONTAINS___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003138 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
3139 getitem__doc__},
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003140 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003141 sizeof__doc__},
Victor Stinner7dc6a5f2017-01-19 12:37:13 +01003142 DICT_GET_METHODDEF
3143 DICT_SETDEFAULT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003144 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
3145 pop__doc__},
3146 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
3147 popitem__doc__},
3148 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
3149 keys__doc__},
3150 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
3151 items__doc__},
3152 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
3153 values__doc__},
3154 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
3155 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08003156 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003157 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
3158 clear__doc__},
3159 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
3160 copy__doc__},
3161 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003162};
3163
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003164/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00003165int
3166PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003167{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003168 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07003169 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003170 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003171 PyObject *value;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003173 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003174 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003175 hash = PyObject_Hash(key);
3176 if (hash == -1)
3177 return -1;
3178 }
INADA Naokiba609772016-12-07 20:41:42 +09003179 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value, NULL);
Victor Stinner742da042016-09-07 17:40:12 -07003180 if (ix == DKIX_ERROR)
3181 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003182 return (ix != DKIX_EMPTY && value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003183}
3184
Thomas Wouterscf297e42007-02-23 15:07:44 +00003185/* Internal version of PyDict_Contains used when the hash value is already known */
3186int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003187_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00003188{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003189 PyDictObject *mp = (PyDictObject *)op;
INADA Naokiba609772016-12-07 20:41:42 +09003190 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07003191 Py_ssize_t ix;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003192
INADA Naokiba609772016-12-07 20:41:42 +09003193 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value, NULL);
Victor Stinner742da042016-09-07 17:40:12 -07003194 if (ix == DKIX_ERROR)
3195 return -1;
INADA Naokiba609772016-12-07 20:41:42 +09003196 return (ix != DKIX_EMPTY && value != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00003197}
3198
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003199/* Hack to implement "key in dict" */
3200static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003201 0, /* sq_length */
3202 0, /* sq_concat */
3203 0, /* sq_repeat */
3204 0, /* sq_item */
3205 0, /* sq_slice */
3206 0, /* sq_ass_item */
3207 0, /* sq_ass_slice */
3208 PyDict_Contains, /* sq_contains */
3209 0, /* sq_inplace_concat */
3210 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003211};
3212
Guido van Rossum09e563a2001-05-01 12:10:21 +00003213static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003214dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3215{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003216 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003217 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003219 assert(type != NULL && type->tp_alloc != NULL);
3220 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02003221 if (self == NULL)
3222 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003223 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003224
Victor Stinnera9f61a52013-07-16 22:17:26 +02003225 /* The object has been implicitly tracked by tp_alloc */
3226 if (type == &PyDict_Type)
3227 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003228
3229 d->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003230 d->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07003231 d->ma_keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003232 if (d->ma_keys == NULL) {
3233 Py_DECREF(self);
3234 return NULL;
3235 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02003236 assert(_PyDict_CheckConsistency(d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003237 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003238}
3239
Tim Peters25786c02001-09-02 08:22:48 +00003240static int
3241dict_init(PyObject *self, PyObject *args, PyObject *kwds)
3242{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003243 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00003244}
3245
Tim Peters6d6c1a32001-08-02 04:15:00 +00003246static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003247dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00003248{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003249 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003250}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003251
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003252PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00003253"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00003254"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003255" (key, value) pairs\n"
3256"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00003257" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003258" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00003259" d[k] = v\n"
3260"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
3261" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00003262
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003263PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003264 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3265 "dict",
3266 sizeof(PyDictObject),
3267 0,
3268 (destructor)dict_dealloc, /* tp_dealloc */
3269 0, /* tp_print */
3270 0, /* tp_getattr */
3271 0, /* tp_setattr */
3272 0, /* tp_reserved */
3273 (reprfunc)dict_repr, /* tp_repr */
3274 0, /* tp_as_number */
3275 &dict_as_sequence, /* tp_as_sequence */
3276 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003277 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003278 0, /* tp_call */
3279 0, /* tp_str */
3280 PyObject_GenericGetAttr, /* tp_getattro */
3281 0, /* tp_setattro */
3282 0, /* tp_as_buffer */
3283 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3284 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
3285 dictionary_doc, /* tp_doc */
3286 dict_traverse, /* tp_traverse */
3287 dict_tp_clear, /* tp_clear */
3288 dict_richcompare, /* tp_richcompare */
3289 0, /* tp_weaklistoffset */
3290 (getiterfunc)dict_iter, /* tp_iter */
3291 0, /* tp_iternext */
3292 mapp_methods, /* tp_methods */
3293 0, /* tp_members */
3294 0, /* tp_getset */
3295 0, /* tp_base */
3296 0, /* tp_dict */
3297 0, /* tp_descr_get */
3298 0, /* tp_descr_set */
3299 0, /* tp_dictoffset */
3300 dict_init, /* tp_init */
3301 PyType_GenericAlloc, /* tp_alloc */
3302 dict_new, /* tp_new */
3303 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003304};
3305
Victor Stinner3c1e4812012-03-26 22:10:51 +02003306PyObject *
3307_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
3308{
3309 PyObject *kv;
3310 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02003311 if (kv == NULL) {
3312 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003313 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02003314 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02003315 return PyDict_GetItem(dp, kv);
3316}
3317
Guido van Rossum3cca2451997-05-16 14:23:33 +00003318/* For backward compatibility with old dictionary interface */
3319
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003320PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003321PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003322{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003323 PyObject *kv, *rv;
3324 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003325 if (kv == NULL) {
3326 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003327 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003328 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003329 rv = PyDict_GetItem(v, kv);
3330 Py_DECREF(kv);
3331 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003332}
3333
3334int
Victor Stinner3c1e4812012-03-26 22:10:51 +02003335_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
3336{
3337 PyObject *kv;
3338 kv = _PyUnicode_FromId(key); /* borrowed */
3339 if (kv == NULL)
3340 return -1;
3341 return PyDict_SetItem(v, kv, item);
3342}
3343
3344int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003345PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003346{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003347 PyObject *kv;
3348 int err;
3349 kv = PyUnicode_FromString(key);
3350 if (kv == NULL)
3351 return -1;
3352 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
3353 err = PyDict_SetItem(v, kv, item);
3354 Py_DECREF(kv);
3355 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003356}
3357
3358int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01003359_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
3360{
3361 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3362 if (kv == NULL)
3363 return -1;
3364 return PyDict_DelItem(v, kv);
3365}
3366
3367int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003368PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003369{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003370 PyObject *kv;
3371 int err;
3372 kv = PyUnicode_FromString(key);
3373 if (kv == NULL)
3374 return -1;
3375 err = PyDict_DelItem(v, kv);
3376 Py_DECREF(kv);
3377 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003378}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003379
Raymond Hettinger019a1482004-03-18 02:41:19 +00003380/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003381
3382typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003383 PyObject_HEAD
3384 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
3385 Py_ssize_t di_used;
3386 Py_ssize_t di_pos;
3387 PyObject* di_result; /* reusable result tuple for iteritems */
3388 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003389} dictiterobject;
3390
3391static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003392dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003393{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003394 dictiterobject *di;
3395 di = PyObject_GC_New(dictiterobject, itertype);
3396 if (di == NULL)
3397 return NULL;
3398 Py_INCREF(dict);
3399 di->di_dict = dict;
3400 di->di_used = dict->ma_used;
3401 di->di_pos = 0;
3402 di->len = dict->ma_used;
3403 if (itertype == &PyDictIterItem_Type) {
3404 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
3405 if (di->di_result == NULL) {
3406 Py_DECREF(di);
3407 return NULL;
3408 }
3409 }
3410 else
3411 di->di_result = NULL;
3412 _PyObject_GC_TRACK(di);
3413 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003414}
3415
3416static void
3417dictiter_dealloc(dictiterobject *di)
3418{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003419 Py_XDECREF(di->di_dict);
3420 Py_XDECREF(di->di_result);
3421 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003422}
3423
3424static int
3425dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
3426{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003427 Py_VISIT(di->di_dict);
3428 Py_VISIT(di->di_result);
3429 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003430}
3431
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003432static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003433dictiter_len(dictiterobject *di)
3434{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003435 Py_ssize_t len = 0;
3436 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
3437 len = di->len;
3438 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003439}
3440
Guido van Rossumb90c8482007-02-10 01:11:45 +00003441PyDoc_STRVAR(length_hint_doc,
3442 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003443
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003444static PyObject *
3445dictiter_reduce(dictiterobject *di);
3446
3447PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3448
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003449static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003450 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
3451 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003452 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
3453 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003454 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003455};
3456
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003457static PyObject*
3458dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00003459{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003460 PyObject *key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003461 Py_ssize_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003462 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003463 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00003464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003465 if (d == NULL)
3466 return NULL;
3467 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00003468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003469 if (di->di_used != d->ma_used) {
3470 PyErr_SetString(PyExc_RuntimeError,
3471 "dictionary changed size during iteration");
3472 di->di_used = -1; /* Make this state sticky */
3473 return NULL;
3474 }
Guido van Rossum2147df72002-07-16 20:30:22 +00003475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003476 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003477 k = d->ma_keys;
INADA Naokica2d8be2016-11-04 16:59:10 +09003478 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003479 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003480 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003481 goto fail;
3482 key = DK_ENTRIES(k)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003483 assert(d->ma_values[i] != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003484 }
3485 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003486 Py_ssize_t n = k->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003487 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3488 while (i < n && entry_ptr->me_value == NULL) {
3489 entry_ptr++;
3490 i++;
3491 }
3492 if (i >= n)
3493 goto fail;
3494 key = entry_ptr->me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003495 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003496 di->di_pos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003497 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003498 Py_INCREF(key);
3499 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003500
3501fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003502 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003503 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003504 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003505}
3506
Raymond Hettinger019a1482004-03-18 02:41:19 +00003507PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003508 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3509 "dict_keyiterator", /* tp_name */
3510 sizeof(dictiterobject), /* tp_basicsize */
3511 0, /* tp_itemsize */
3512 /* methods */
3513 (destructor)dictiter_dealloc, /* tp_dealloc */
3514 0, /* tp_print */
3515 0, /* tp_getattr */
3516 0, /* tp_setattr */
3517 0, /* tp_reserved */
3518 0, /* tp_repr */
3519 0, /* tp_as_number */
3520 0, /* tp_as_sequence */
3521 0, /* tp_as_mapping */
3522 0, /* tp_hash */
3523 0, /* tp_call */
3524 0, /* tp_str */
3525 PyObject_GenericGetAttr, /* tp_getattro */
3526 0, /* tp_setattro */
3527 0, /* tp_as_buffer */
3528 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3529 0, /* tp_doc */
3530 (traverseproc)dictiter_traverse, /* tp_traverse */
3531 0, /* tp_clear */
3532 0, /* tp_richcompare */
3533 0, /* tp_weaklistoffset */
3534 PyObject_SelfIter, /* tp_iter */
3535 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3536 dictiter_methods, /* tp_methods */
3537 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003538};
3539
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003540static PyObject *
3541dictiter_iternextvalue(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003542{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003543 PyObject *value;
INADA Naokica2d8be2016-11-04 16:59:10 +09003544 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003545 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003547 if (d == NULL)
3548 return NULL;
3549 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003551 if (di->di_used != d->ma_used) {
3552 PyErr_SetString(PyExc_RuntimeError,
3553 "dictionary changed size during iteration");
3554 di->di_used = -1; /* Make this state sticky */
3555 return NULL;
3556 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003558 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003559 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003560 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003561 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003562 goto fail;
INADA Naokica2d8be2016-11-04 16:59:10 +09003563 value = d->ma_values[i];
3564 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003565 }
3566 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003567 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003568 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3569 while (i < n && entry_ptr->me_value == NULL) {
3570 entry_ptr++;
3571 i++;
3572 }
3573 if (i >= n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003574 goto fail;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003575 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003576 }
3577 di->di_pos = i+1;
3578 di->len--;
3579 Py_INCREF(value);
3580 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003581
3582fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003583 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003584 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003585 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003586}
3587
3588PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003589 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3590 "dict_valueiterator", /* tp_name */
3591 sizeof(dictiterobject), /* tp_basicsize */
3592 0, /* tp_itemsize */
3593 /* methods */
3594 (destructor)dictiter_dealloc, /* tp_dealloc */
3595 0, /* tp_print */
3596 0, /* tp_getattr */
3597 0, /* tp_setattr */
3598 0, /* tp_reserved */
3599 0, /* tp_repr */
3600 0, /* tp_as_number */
3601 0, /* tp_as_sequence */
3602 0, /* tp_as_mapping */
3603 0, /* tp_hash */
3604 0, /* tp_call */
3605 0, /* tp_str */
3606 PyObject_GenericGetAttr, /* tp_getattro */
3607 0, /* tp_setattro */
3608 0, /* tp_as_buffer */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003609 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003610 0, /* tp_doc */
3611 (traverseproc)dictiter_traverse, /* tp_traverse */
3612 0, /* tp_clear */
3613 0, /* tp_richcompare */
3614 0, /* tp_weaklistoffset */
3615 PyObject_SelfIter, /* tp_iter */
3616 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3617 dictiter_methods, /* tp_methods */
3618 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003619};
3620
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003621static PyObject *
3622dictiter_iternextitem(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003623{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003624 PyObject *key, *value, *result;
INADA Naokica2d8be2016-11-04 16:59:10 +09003625 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003626 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003628 if (d == NULL)
3629 return NULL;
3630 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003632 if (di->di_used != d->ma_used) {
3633 PyErr_SetString(PyExc_RuntimeError,
3634 "dictionary changed size during iteration");
3635 di->di_used = -1; /* Make this state sticky */
3636 return NULL;
3637 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003639 i = di->di_pos;
INADA Naokica2d8be2016-11-04 16:59:10 +09003640 assert(i >= 0);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003641 if (d->ma_values) {
INADA Naokica2d8be2016-11-04 16:59:10 +09003642 if (i >= d->ma_used)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003643 goto fail;
3644 key = DK_ENTRIES(d->ma_keys)[i].me_key;
INADA Naokica2d8be2016-11-04 16:59:10 +09003645 value = d->ma_values[i];
3646 assert(value != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003647 }
3648 else {
INADA Naokica2d8be2016-11-04 16:59:10 +09003649 Py_ssize_t n = d->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003650 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3651 while (i < n && entry_ptr->me_value == NULL) {
3652 entry_ptr++;
3653 i++;
3654 }
3655 if (i >= n)
3656 goto fail;
3657 key = entry_ptr->me_key;
3658 value = entry_ptr->me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003659 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003660 di->di_pos = i+1;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003661 di->len--;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003662 Py_INCREF(key);
3663 Py_INCREF(value);
3664 result = di->di_result;
3665 if (Py_REFCNT(result) == 1) {
3666 PyObject *oldkey = PyTuple_GET_ITEM(result, 0);
3667 PyObject *oldvalue = PyTuple_GET_ITEM(result, 1);
3668 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3669 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003670 Py_INCREF(result);
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003671 Py_DECREF(oldkey);
3672 Py_DECREF(oldvalue);
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003673 }
3674 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003675 result = PyTuple_New(2);
3676 if (result == NULL)
3677 return NULL;
Serhiy Storchaka753bca32017-05-20 12:30:02 +03003678 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3679 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003680 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003681 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003682
3683fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003684 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003685 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003686 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003687}
3688
3689PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003690 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3691 "dict_itemiterator", /* tp_name */
3692 sizeof(dictiterobject), /* tp_basicsize */
3693 0, /* tp_itemsize */
3694 /* methods */
3695 (destructor)dictiter_dealloc, /* tp_dealloc */
3696 0, /* tp_print */
3697 0, /* tp_getattr */
3698 0, /* tp_setattr */
3699 0, /* tp_reserved */
3700 0, /* tp_repr */
3701 0, /* tp_as_number */
3702 0, /* tp_as_sequence */
3703 0, /* tp_as_mapping */
3704 0, /* tp_hash */
3705 0, /* tp_call */
3706 0, /* tp_str */
3707 PyObject_GenericGetAttr, /* tp_getattro */
3708 0, /* tp_setattro */
3709 0, /* tp_as_buffer */
3710 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3711 0, /* tp_doc */
3712 (traverseproc)dictiter_traverse, /* tp_traverse */
3713 0, /* tp_clear */
3714 0, /* tp_richcompare */
3715 0, /* tp_weaklistoffset */
3716 PyObject_SelfIter, /* tp_iter */
3717 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3718 dictiter_methods, /* tp_methods */
3719 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003720};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003721
3722
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003723static PyObject *
3724dictiter_reduce(dictiterobject *di)
3725{
3726 PyObject *list;
3727 dictiterobject tmp;
3728
3729 list = PyList_New(0);
3730 if (!list)
3731 return NULL;
3732
3733 /* copy the itertor state */
3734 tmp = *di;
3735 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003736
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003737 /* iterate the temporary into a list */
3738 for(;;) {
3739 PyObject *element = 0;
3740 if (Py_TYPE(di) == &PyDictIterItem_Type)
3741 element = dictiter_iternextitem(&tmp);
3742 else if (Py_TYPE(di) == &PyDictIterKey_Type)
3743 element = dictiter_iternextkey(&tmp);
3744 else if (Py_TYPE(di) == &PyDictIterValue_Type)
3745 element = dictiter_iternextvalue(&tmp);
3746 else
3747 assert(0);
3748 if (element) {
3749 if (PyList_Append(list, element)) {
3750 Py_DECREF(element);
3751 Py_DECREF(list);
3752 Py_XDECREF(tmp.di_dict);
3753 return NULL;
3754 }
3755 Py_DECREF(element);
3756 } else
3757 break;
3758 }
3759 Py_XDECREF(tmp.di_dict);
3760 /* check for error */
3761 if (tmp.di_dict != NULL) {
3762 /* we have an error */
3763 Py_DECREF(list);
3764 return NULL;
3765 }
Antoine Pitroua7013882012-04-05 00:04:20 +02003766 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003767}
3768
Guido van Rossum3ac67412007-02-10 18:55:06 +00003769/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003770/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003771/***********************************************/
3772
Guido van Rossumb90c8482007-02-10 01:11:45 +00003773/* The instance lay-out is the same for all three; but the type differs. */
3774
Guido van Rossumb90c8482007-02-10 01:11:45 +00003775static void
Eric Snow96c6af92015-05-29 22:21:39 -06003776dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003777{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003778 Py_XDECREF(dv->dv_dict);
3779 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003780}
3781
3782static int
Eric Snow96c6af92015-05-29 22:21:39 -06003783dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003784{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003785 Py_VISIT(dv->dv_dict);
3786 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003787}
3788
Guido van Rossum83825ac2007-02-10 04:54:19 +00003789static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003790dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003791{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003792 Py_ssize_t len = 0;
3793 if (dv->dv_dict != NULL)
3794 len = dv->dv_dict->ma_used;
3795 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003796}
3797
Eric Snow96c6af92015-05-29 22:21:39 -06003798PyObject *
3799_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003800{
Eric Snow96c6af92015-05-29 22:21:39 -06003801 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003802 if (dict == NULL) {
3803 PyErr_BadInternalCall();
3804 return NULL;
3805 }
3806 if (!PyDict_Check(dict)) {
3807 /* XXX Get rid of this restriction later */
3808 PyErr_Format(PyExc_TypeError,
3809 "%s() requires a dict argument, not '%s'",
3810 type->tp_name, dict->ob_type->tp_name);
3811 return NULL;
3812 }
Eric Snow96c6af92015-05-29 22:21:39 -06003813 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003814 if (dv == NULL)
3815 return NULL;
3816 Py_INCREF(dict);
3817 dv->dv_dict = (PyDictObject *)dict;
3818 _PyObject_GC_TRACK(dv);
3819 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003820}
3821
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003822/* TODO(guido): The views objects are not complete:
3823
3824 * support more set operations
3825 * support arbitrary mappings?
3826 - either these should be static or exported in dictobject.h
3827 - if public then they should probably be in builtins
3828*/
3829
Guido van Rossumaac530c2007-08-24 22:33:45 +00003830/* Return 1 if self is a subset of other, iterating over self;
3831 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003832static int
3833all_contained_in(PyObject *self, PyObject *other)
3834{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003835 PyObject *iter = PyObject_GetIter(self);
3836 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003838 if (iter == NULL)
3839 return -1;
3840 for (;;) {
3841 PyObject *next = PyIter_Next(iter);
3842 if (next == NULL) {
3843 if (PyErr_Occurred())
3844 ok = -1;
3845 break;
3846 }
3847 ok = PySequence_Contains(other, next);
3848 Py_DECREF(next);
3849 if (ok <= 0)
3850 break;
3851 }
3852 Py_DECREF(iter);
3853 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003854}
3855
3856static PyObject *
3857dictview_richcompare(PyObject *self, PyObject *other, int op)
3858{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003859 Py_ssize_t len_self, len_other;
3860 int ok;
3861 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003863 assert(self != NULL);
3864 assert(PyDictViewSet_Check(self));
3865 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003866
Brian Curtindfc80e32011-08-10 20:28:54 -05003867 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3868 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003870 len_self = PyObject_Size(self);
3871 if (len_self < 0)
3872 return NULL;
3873 len_other = PyObject_Size(other);
3874 if (len_other < 0)
3875 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003876
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003877 ok = 0;
3878 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003880 case Py_NE:
3881 case Py_EQ:
3882 if (len_self == len_other)
3883 ok = all_contained_in(self, other);
3884 if (op == Py_NE && ok >= 0)
3885 ok = !ok;
3886 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003888 case Py_LT:
3889 if (len_self < len_other)
3890 ok = all_contained_in(self, other);
3891 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003893 case Py_LE:
3894 if (len_self <= len_other)
3895 ok = all_contained_in(self, other);
3896 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003898 case Py_GT:
3899 if (len_self > len_other)
3900 ok = all_contained_in(other, self);
3901 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003903 case Py_GE:
3904 if (len_self >= len_other)
3905 ok = all_contained_in(other, self);
3906 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003908 }
3909 if (ok < 0)
3910 return NULL;
3911 result = ok ? Py_True : Py_False;
3912 Py_INCREF(result);
3913 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003914}
3915
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003916static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003917dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003918{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003919 PyObject *seq;
3920 PyObject *result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003922 seq = PySequence_List((PyObject *)dv);
3923 if (seq == NULL)
3924 return NULL;
3925
3926 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
3927 Py_DECREF(seq);
3928 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003929}
3930
Guido van Rossum3ac67412007-02-10 18:55:06 +00003931/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003932
3933static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003934dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003935{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003936 if (dv->dv_dict == NULL) {
3937 Py_RETURN_NONE;
3938 }
3939 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003940}
3941
3942static int
Eric Snow96c6af92015-05-29 22:21:39 -06003943dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00003944{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003945 if (dv->dv_dict == NULL)
3946 return 0;
3947 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003948}
3949
Guido van Rossum83825ac2007-02-10 04:54:19 +00003950static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003951 (lenfunc)dictview_len, /* sq_length */
3952 0, /* sq_concat */
3953 0, /* sq_repeat */
3954 0, /* sq_item */
3955 0, /* sq_slice */
3956 0, /* sq_ass_item */
3957 0, /* sq_ass_slice */
3958 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003959};
3960
Guido van Rossum523259b2007-08-24 23:41:22 +00003961static PyObject*
3962dictviews_sub(PyObject* self, PyObject *other)
3963{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003964 PyObject *result = PySet_New(self);
3965 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003966 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003968 if (result == NULL)
3969 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003970
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003971 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003972 if (tmp == NULL) {
3973 Py_DECREF(result);
3974 return NULL;
3975 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003977 Py_DECREF(tmp);
3978 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003979}
3980
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003981PyObject*
3982_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00003983{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003984 PyObject *result = PySet_New(self);
3985 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003986 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003988 if (result == NULL)
3989 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003990
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003991 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_intersection_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003992 if (tmp == NULL) {
3993 Py_DECREF(result);
3994 return NULL;
3995 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003997 Py_DECREF(tmp);
3998 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003999}
4000
4001static PyObject*
4002dictviews_or(PyObject* self, PyObject *other)
4003{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004004 PyObject *result = PySet_New(self);
4005 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004006 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02004007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004008 if (result == NULL)
4009 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004010
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08004011 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004012 if (tmp == NULL) {
4013 Py_DECREF(result);
4014 return NULL;
4015 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004016
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004017 Py_DECREF(tmp);
4018 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004019}
4020
4021static PyObject*
4022dictviews_xor(PyObject* self, PyObject *other)
4023{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004024 PyObject *result = PySet_New(self);
4025 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004026 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004027
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004028 if (result == NULL)
4029 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004030
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08004031 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_symmetric_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004032 if (tmp == NULL) {
4033 Py_DECREF(result);
4034 return NULL;
4035 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004036
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004037 Py_DECREF(tmp);
4038 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004039}
4040
4041static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004042 0, /*nb_add*/
4043 (binaryfunc)dictviews_sub, /*nb_subtract*/
4044 0, /*nb_multiply*/
4045 0, /*nb_remainder*/
4046 0, /*nb_divmod*/
4047 0, /*nb_power*/
4048 0, /*nb_negative*/
4049 0, /*nb_positive*/
4050 0, /*nb_absolute*/
4051 0, /*nb_bool*/
4052 0, /*nb_invert*/
4053 0, /*nb_lshift*/
4054 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004055 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004056 (binaryfunc)dictviews_xor, /*nb_xor*/
4057 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00004058};
4059
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004060static PyObject*
4061dictviews_isdisjoint(PyObject *self, PyObject *other)
4062{
4063 PyObject *it;
4064 PyObject *item = NULL;
4065
4066 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06004067 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004068 Py_RETURN_TRUE;
4069 else
4070 Py_RETURN_FALSE;
4071 }
4072
4073 /* Iterate over the shorter object (only if other is a set,
4074 * because PySequence_Contains may be expensive otherwise): */
4075 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06004076 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004077 Py_ssize_t len_other = PyObject_Size(other);
4078 if (len_other == -1)
4079 return NULL;
4080
4081 if ((len_other > len_self)) {
4082 PyObject *tmp = other;
4083 other = self;
4084 self = tmp;
4085 }
4086 }
4087
4088 it = PyObject_GetIter(other);
4089 if (it == NULL)
4090 return NULL;
4091
4092 while ((item = PyIter_Next(it)) != NULL) {
4093 int contains = PySequence_Contains(self, item);
4094 Py_DECREF(item);
4095 if (contains == -1) {
4096 Py_DECREF(it);
4097 return NULL;
4098 }
4099
4100 if (contains) {
4101 Py_DECREF(it);
4102 Py_RETURN_FALSE;
4103 }
4104 }
4105 Py_DECREF(it);
4106 if (PyErr_Occurred())
4107 return NULL; /* PyIter_Next raised an exception. */
4108 Py_RETURN_TRUE;
4109}
4110
4111PyDoc_STRVAR(isdisjoint_doc,
4112"Return True if the view and the given iterable have a null intersection.");
4113
Guido van Rossumb90c8482007-02-10 01:11:45 +00004114static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004115 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4116 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004117 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004118};
4119
4120PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004121 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4122 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004123 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004124 0, /* tp_itemsize */
4125 /* methods */
4126 (destructor)dictview_dealloc, /* tp_dealloc */
4127 0, /* tp_print */
4128 0, /* tp_getattr */
4129 0, /* tp_setattr */
4130 0, /* tp_reserved */
4131 (reprfunc)dictview_repr, /* tp_repr */
4132 &dictviews_as_number, /* tp_as_number */
4133 &dictkeys_as_sequence, /* tp_as_sequence */
4134 0, /* tp_as_mapping */
4135 0, /* tp_hash */
4136 0, /* tp_call */
4137 0, /* tp_str */
4138 PyObject_GenericGetAttr, /* tp_getattro */
4139 0, /* tp_setattro */
4140 0, /* tp_as_buffer */
4141 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4142 0, /* tp_doc */
4143 (traverseproc)dictview_traverse, /* tp_traverse */
4144 0, /* tp_clear */
4145 dictview_richcompare, /* tp_richcompare */
4146 0, /* tp_weaklistoffset */
4147 (getiterfunc)dictkeys_iter, /* tp_iter */
4148 0, /* tp_iternext */
4149 dictkeys_methods, /* tp_methods */
4150 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004151};
4152
4153static PyObject *
4154dictkeys_new(PyObject *dict)
4155{
Eric Snow96c6af92015-05-29 22:21:39 -06004156 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004157}
4158
Guido van Rossum3ac67412007-02-10 18:55:06 +00004159/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004160
4161static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004162dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004163{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004164 if (dv->dv_dict == NULL) {
4165 Py_RETURN_NONE;
4166 }
4167 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004168}
4169
4170static int
Eric Snow96c6af92015-05-29 22:21:39 -06004171dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004172{
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004173 int result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004174 PyObject *key, *value, *found;
4175 if (dv->dv_dict == NULL)
4176 return 0;
4177 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
4178 return 0;
4179 key = PyTuple_GET_ITEM(obj, 0);
4180 value = PyTuple_GET_ITEM(obj, 1);
Raymond Hettinger6692f012016-09-18 21:46:08 -07004181 found = PyDict_GetItemWithError((PyObject *)dv->dv_dict, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004182 if (found == NULL) {
4183 if (PyErr_Occurred())
4184 return -1;
4185 return 0;
4186 }
Serhiy Storchaka753bca32017-05-20 12:30:02 +03004187 Py_INCREF(found);
4188 result = PyObject_RichCompareBool(value, found, Py_EQ);
4189 Py_DECREF(found);
4190 return result;
Guido van Rossumb90c8482007-02-10 01:11:45 +00004191}
4192
Guido van Rossum83825ac2007-02-10 04:54:19 +00004193static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004194 (lenfunc)dictview_len, /* sq_length */
4195 0, /* sq_concat */
4196 0, /* sq_repeat */
4197 0, /* sq_item */
4198 0, /* sq_slice */
4199 0, /* sq_ass_item */
4200 0, /* sq_ass_slice */
4201 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004202};
4203
Guido van Rossumb90c8482007-02-10 01:11:45 +00004204static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004205 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4206 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004207 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004208};
4209
4210PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004211 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4212 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004213 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004214 0, /* tp_itemsize */
4215 /* methods */
4216 (destructor)dictview_dealloc, /* tp_dealloc */
4217 0, /* tp_print */
4218 0, /* tp_getattr */
4219 0, /* tp_setattr */
4220 0, /* tp_reserved */
4221 (reprfunc)dictview_repr, /* tp_repr */
4222 &dictviews_as_number, /* tp_as_number */
4223 &dictitems_as_sequence, /* tp_as_sequence */
4224 0, /* tp_as_mapping */
4225 0, /* tp_hash */
4226 0, /* tp_call */
4227 0, /* tp_str */
4228 PyObject_GenericGetAttr, /* tp_getattro */
4229 0, /* tp_setattro */
4230 0, /* tp_as_buffer */
4231 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4232 0, /* tp_doc */
4233 (traverseproc)dictview_traverse, /* tp_traverse */
4234 0, /* tp_clear */
4235 dictview_richcompare, /* tp_richcompare */
4236 0, /* tp_weaklistoffset */
4237 (getiterfunc)dictitems_iter, /* tp_iter */
4238 0, /* tp_iternext */
4239 dictitems_methods, /* tp_methods */
4240 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004241};
4242
4243static PyObject *
4244dictitems_new(PyObject *dict)
4245{
Eric Snow96c6af92015-05-29 22:21:39 -06004246 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004247}
4248
Guido van Rossum3ac67412007-02-10 18:55:06 +00004249/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004250
4251static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004252dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004253{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004254 if (dv->dv_dict == NULL) {
4255 Py_RETURN_NONE;
4256 }
4257 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004258}
4259
Guido van Rossum83825ac2007-02-10 04:54:19 +00004260static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004261 (lenfunc)dictview_len, /* sq_length */
4262 0, /* sq_concat */
4263 0, /* sq_repeat */
4264 0, /* sq_item */
4265 0, /* sq_slice */
4266 0, /* sq_ass_item */
4267 0, /* sq_ass_slice */
4268 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004269};
4270
Guido van Rossumb90c8482007-02-10 01:11:45 +00004271static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004272 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004273};
4274
4275PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004276 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4277 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004278 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004279 0, /* tp_itemsize */
4280 /* methods */
4281 (destructor)dictview_dealloc, /* tp_dealloc */
4282 0, /* tp_print */
4283 0, /* tp_getattr */
4284 0, /* tp_setattr */
4285 0, /* tp_reserved */
4286 (reprfunc)dictview_repr, /* tp_repr */
4287 0, /* tp_as_number */
4288 &dictvalues_as_sequence, /* tp_as_sequence */
4289 0, /* tp_as_mapping */
4290 0, /* tp_hash */
4291 0, /* tp_call */
4292 0, /* tp_str */
4293 PyObject_GenericGetAttr, /* tp_getattro */
4294 0, /* tp_setattro */
4295 0, /* tp_as_buffer */
4296 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4297 0, /* tp_doc */
4298 (traverseproc)dictview_traverse, /* tp_traverse */
4299 0, /* tp_clear */
4300 0, /* tp_richcompare */
4301 0, /* tp_weaklistoffset */
4302 (getiterfunc)dictvalues_iter, /* tp_iter */
4303 0, /* tp_iternext */
4304 dictvalues_methods, /* tp_methods */
4305 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004306};
4307
4308static PyObject *
4309dictvalues_new(PyObject *dict)
4310{
Eric Snow96c6af92015-05-29 22:21:39 -06004311 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004312}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004313
4314/* Returns NULL if cannot allocate a new PyDictKeysObject,
4315 but does not set an error */
4316PyDictKeysObject *
4317_PyDict_NewKeysForClass(void)
4318{
Victor Stinner742da042016-09-07 17:40:12 -07004319 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004320 if (keys == NULL)
4321 PyErr_Clear();
4322 else
4323 keys->dk_lookup = lookdict_split;
4324 return keys;
4325}
4326
4327#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
4328
4329PyObject *
4330PyObject_GenericGetDict(PyObject *obj, void *context)
4331{
4332 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
4333 if (dictptr == NULL) {
4334 PyErr_SetString(PyExc_AttributeError,
4335 "This object has no __dict__");
4336 return NULL;
4337 }
4338 dict = *dictptr;
4339 if (dict == NULL) {
4340 PyTypeObject *tp = Py_TYPE(obj);
4341 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
4342 DK_INCREF(CACHED_KEYS(tp));
4343 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
4344 }
4345 else {
4346 *dictptr = dict = PyDict_New();
4347 }
4348 }
4349 Py_XINCREF(dict);
4350 return dict;
4351}
4352
4353int
4354_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
Victor Stinner742da042016-09-07 17:40:12 -07004355 PyObject *key, PyObject *value)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004356{
4357 PyObject *dict;
4358 int res;
4359 PyDictKeysObject *cached;
4360
4361 assert(dictptr != NULL);
4362 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
4363 assert(dictptr != NULL);
4364 dict = *dictptr;
4365 if (dict == NULL) {
4366 DK_INCREF(cached);
4367 dict = new_dict_with_shared_keys(cached);
4368 if (dict == NULL)
4369 return -1;
4370 *dictptr = dict;
4371 }
4372 if (value == NULL) {
4373 res = PyDict_DelItem(dict, key);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004374 // Since key sharing dict doesn't allow deletion, PyDict_DelItem()
4375 // always converts dict to combined form.
4376 if ((cached = CACHED_KEYS(tp)) != NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004377 CACHED_KEYS(tp) = NULL;
4378 DK_DECREF(cached);
4379 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01004380 }
4381 else {
INADA Naoki2294f3a2017-02-12 13:51:30 +09004382 int was_shared = (cached == ((PyDictObject *)dict)->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004383 res = PyDict_SetItem(dict, key, value);
INADA Naoki2294f3a2017-02-12 13:51:30 +09004384 if (was_shared &&
4385 (cached = CACHED_KEYS(tp)) != NULL &&
4386 cached != ((PyDictObject *)dict)->ma_keys) {
Victor Stinner3d3f2642016-12-15 17:21:23 +01004387 /* PyDict_SetItem() may call dictresize and convert split table
4388 * into combined table. In such case, convert it to split
4389 * table again and update type's shared key only when this is
4390 * the only dict sharing key with the type.
4391 *
4392 * This is to allow using shared key in class like this:
4393 *
4394 * class C:
4395 * def __init__(self):
4396 * # one dict resize happens
4397 * self.a, self.b, self.c = 1, 2, 3
4398 * self.d, self.e, self.f = 4, 5, 6
4399 * a = C()
4400 */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004401 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004402 CACHED_KEYS(tp) = make_keys_shared(dict);
Victor Stinner742da042016-09-07 17:40:12 -07004403 }
4404 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004405 CACHED_KEYS(tp) = NULL;
4406 }
4407 DK_DECREF(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004408 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
4409 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004410 }
4411 }
4412 } else {
4413 dict = *dictptr;
4414 if (dict == NULL) {
4415 dict = PyDict_New();
4416 if (dict == NULL)
4417 return -1;
4418 *dictptr = dict;
4419 }
4420 if (value == NULL) {
4421 res = PyDict_DelItem(dict, key);
4422 } else {
4423 res = PyDict_SetItem(dict, key, value);
4424 }
4425 }
4426 return res;
4427}
4428
4429void
4430_PyDictKeys_DecRef(PyDictKeysObject *keys)
4431{
4432 DK_DECREF(keys);
4433}