blob: a0c1977037b9e6bb78158587b21a0e3aa248d7d3 [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
Berker Peksag71c01d42016-09-09 03:57:23 +030013As of Python 3.6, this is compact and ordered. Basic idea is described here.
Benjamin Peterson003f0592016-09-08 10:14:31 -070014https://morepypy.blogspot.com/2015/01/faster-more-memory-efficient-and-more.html
Victor Stinner742da042016-09-07 17:40:12 -070015
16layout:
17
18+---------------+
19| dk_refcnt |
20| dk_size |
21| dk_lookup |
22| dk_usable |
23| dk_nentries |
24+---------------+
25| dk_indices |
26| |
27+---------------+
28| dk_entries |
29| |
30+---------------+
31
32dk_indices is actual hashtable. It holds index in entries, or DKIX_EMPTY(-1)
33or DKIX_DUMMY(-2).
34Size of indices is dk_size. Type of each index in indices is vary on dk_size:
35
36* int8 for dk_size <= 128
37* int16 for 256 <= dk_size <= 2**15
38* int32 for 2**16 <= dk_size <= 2**31
39* int64 for 2**32 <= dk_size
40
41dk_entries is array of PyDictKeyEntry. It's size is USABLE_FRACTION(dk_size).
42DK_ENTRIES(dk) can be used to get pointer to entries.
43
44NOTE: Since negative value is used for DKIX_EMPTY and DKIX_DUMMY, type of
45dk_indices entry is signed integer and int16 is used for table which
46dk_size == 256.
47*/
48
Benjamin Peterson7d95e402012-04-23 11:24:50 -040049
50/*
Benjamin Peterson7d95e402012-04-23 11:24:50 -040051The DictObject can be in one of two forms.
Victor Stinner742da042016-09-07 17:40:12 -070052
Benjamin Peterson7d95e402012-04-23 11:24:50 -040053Either:
54 A combined table:
55 ma_values == NULL, dk_refcnt == 1.
56 Values are stored in the me_value field of the PyDictKeysObject.
Benjamin Peterson7d95e402012-04-23 11:24:50 -040057Or:
58 A split table:
59 ma_values != NULL, dk_refcnt >= 1
60 Values are stored in the ma_values array.
Victor Stinner742da042016-09-07 17:40:12 -070061 Only string (unicode) keys are allowed.
62 All dicts sharing same key must have same insertion order.
Benjamin Peterson7d95e402012-04-23 11:24:50 -040063
Victor Stinner742da042016-09-07 17:40:12 -070064There are four kinds of slots in the table (slot is index, and
65DK_ENTRIES(keys)[index] if index >= 0):
66
671. Unused. index == DKIX_EMPTY
68 Does not hold an active (key, value) pair now and never did. Unused can
69 transition to Active upon key insertion. This is each slot's initial state.
70
712. Active. index >= 0, me_key != NULL and me_value != NULL
72 Holds an active (key, value) pair. Active can transition to Dummy or
73 Pending upon key deletion (for combined and split tables respectively).
74 This is the only case in which me_value != NULL.
75
763. Dummy. index == DKIX_DUMMY (combined only)
77 Previously held an active (key, value) pair, but that was deleted and an
78 active pair has not yet overwritten the slot. Dummy can transition to
79 Active upon key insertion. Dummy slots cannot be made Unused again
80 else the probe sequence in case of collision would have no way to know
81 they were once active.
82
834. Pending. index >= 0, key != NULL, and value == NULL (split only)
84 Not yet inserted in split-table.
Benjamin Peterson7d95e402012-04-23 11:24:50 -040085*/
86
Victor Stinner742da042016-09-07 17:40:12 -070087/*
88Preserving insertion order
Benjamin Peterson7d95e402012-04-23 11:24:50 -040089
Victor Stinner742da042016-09-07 17:40:12 -070090It's simple for combined table. Since dk_entries is mostly append only, we can
91get insertion order by just iterating dk_entries.
92
93One exception is .popitem(). It removes last item in dk_entries and decrement
94dk_nentries to achieve amortized O(1). Since there are DKIX_DUMMY remains in
95dk_indices, we can't increment dk_usable even though dk_nentries is
96decremented.
97
98In split table, inserting into pending entry is allowed only for dk_entries[ix]
99where ix == mp->ma_used. Inserting into other index and deleting item cause
100converting the dict to the combined table.
101*/
102
103/* PyDict_MINSIZE is the starting size for any new dict.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400104 * 8 allows dicts with no more than 5 active entries; experiments suggested
105 * this suffices for the majority of dicts (consisting mostly of usually-small
106 * dicts created to pass keyword arguments).
107 * Making this 8, rather than 4 reduces the number of resizes for most
108 * dictionaries, without any significant extra memory use.
109 */
Victor Stinner742da042016-09-07 17:40:12 -0700110#define PyDict_MINSIZE 8
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400111
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000112#include "Python.h"
Eric Snow96c6af92015-05-29 22:21:39 -0600113#include "dict-common.h"
Victor Stinner990397e2016-09-09 20:22:59 -0700114#include "stringlib/eq.h" /* to get unicode_eq() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000115
Larry Hastings61272b72014-01-07 12:41:53 -0800116/*[clinic input]
Larry Hastingsc2047262014-01-25 20:43:29 -0800117class dict "PyDictObject *" "&PyDict_Type"
Larry Hastings61272b72014-01-07 12:41:53 -0800118[clinic start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -0800119/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f157a5a0ce9589d6]*/
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800120
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400121
122/*
123To ensure the lookup algorithm terminates, there must be at least one Unused
124slot (NULL key) in the table.
125To avoid slowing down lookups on a near-full table, we resize the table when
126it's USABLE_FRACTION (currently two-thirds) full.
127*/
Guido van Rossum16e93a81997-01-28 00:00:11 +0000128
Tim Peterseb28ef22001-06-02 05:27:19 +0000129#define PERTURB_SHIFT 5
130
Guido van Rossum16e93a81997-01-28 00:00:11 +0000131/*
Tim Peterseb28ef22001-06-02 05:27:19 +0000132Major subtleties ahead: Most hash schemes depend on having a "good" hash
133function, in the sense of simulating randomness. Python doesn't: its most
R David Murray537ad7a2016-07-10 12:33:18 -0400134important hash functions (for ints) are very regular in common
Tim Peterseb28ef22001-06-02 05:27:19 +0000135cases:
Tim Peters15d49292001-05-27 07:39:22 +0000136
R David Murray537ad7a2016-07-10 12:33:18 -0400137 >>>[hash(i) for i in range(4)]
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000138 [0, 1, 2, 3]
Tim Peters15d49292001-05-27 07:39:22 +0000139
Tim Peterseb28ef22001-06-02 05:27:19 +0000140This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
141the low-order i bits as the initial table index is extremely fast, and there
R David Murray537ad7a2016-07-10 12:33:18 -0400142are no collisions at all for dicts indexed by a contiguous range of ints. So
143this gives better-than-random behavior in common cases, and that's very
144desirable.
Tim Peters15d49292001-05-27 07:39:22 +0000145
Tim Peterseb28ef22001-06-02 05:27:19 +0000146OTOH, when collisions occur, the tendency to fill contiguous slices of the
147hash table makes a good collision resolution strategy crucial. Taking only
148the last i bits of the hash code is also vulnerable: for example, consider
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000150their own hash codes, and this fits in a dict of size 2**15, the last 15 bits
151 of every hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +0000152
Tim Peterseb28ef22001-06-02 05:27:19 +0000153But catering to unusual cases should not slow the usual ones, so we just take
154the last i bits anyway. It's up to collision resolution to do the rest. If
155we *usually* find the key we're looking for on the first try (and, it turns
156out, we usually do -- the table load factor is kept under 2/3, so the odds
157are solidly in our favor), then it makes best sense to keep the initial index
158computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +0000159
Tim Peterseb28ef22001-06-02 05:27:19 +0000160The first half of collision resolution is to visit table indices via this
161recurrence:
Tim Peters15d49292001-05-27 07:39:22 +0000162
Tim Peterseb28ef22001-06-02 05:27:19 +0000163 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +0000164
Tim Peterseb28ef22001-06-02 05:27:19 +0000165For any initial j in range(2**i), repeating that 2**i times generates each
166int in range(2**i) exactly once (see any text on random-number generation for
167proof). By itself, this doesn't help much: like linear probing (setting
168j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
169order. This would be bad, except that's not the only thing we do, and it's
170actually *good* in the common cases where hash keys are consecutive. In an
171example that's really too small to make this entirely clear, for a table of
172size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +0000173
Tim Peterseb28ef22001-06-02 05:27:19 +0000174 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
175
176If two things come in at index 5, the first place we look after is index 2,
177not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
178Linear probing is deadly in this case because there the fixed probe order
179is the *same* as the order consecutive keys are likely to arrive. But it's
180extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
181and certain that consecutive hash codes do not.
182
183The other half of the strategy is to get the other bits of the hash code
184into play. This is done by initializing a (unsigned) vrbl "perturb" to the
185full hash code, and changing the recurrence to:
186
Tim Peterseb28ef22001-06-02 05:27:19 +0000187 perturb >>= PERTURB_SHIFT;
INADA Naoki267941c2016-10-06 15:19:07 +0900188 j = (5*j) + 1 + perturb;
Tim Peterseb28ef22001-06-02 05:27:19 +0000189 use j % 2**i as the next table index;
190
191Now the probe sequence depends (eventually) on every bit in the hash code,
192and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
193because it quickly magnifies small differences in the bits that didn't affect
194the initial index. Note that because perturb is unsigned, if the recurrence
195is executed often enough perturb eventually becomes and remains 0. At that
196point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
197that's certain to find an empty slot eventually (since it generates every int
198in range(2**i), and we make sure there's always at least one empty slot).
199
200Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
201small so that the high bits of the hash code continue to affect the probe
202sequence across iterations; but you want it large so that in really bad cases
203the high-order hash bits have an effect on early iterations. 5 was "the
204best" in minimizing total collisions across experiments Tim Peters ran (on
205both normal and pathological cases), but 4 and 6 weren't significantly worse.
206
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000207Historical: Reimer Behrends contributed the idea of using a polynomial-based
Tim Peterseb28ef22001-06-02 05:27:19 +0000208approach, using repeated multiplication by x in GF(2**n) where an irreducible
209polynomial for each table size was chosen such that x was a primitive root.
210Christian Tismer later extended that to use division by x instead, as an
211efficient way to get the high bits of the hash code into play. This scheme
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000212also gave excellent collision statistics, but was more expensive: two
213if-tests were required inside the loop; computing "the next" index took about
214the same number of operations but without as much potential parallelism
215(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
216above, and then shifting perturb can be done while the table index is being
217masked); and the PyDictObject struct required a member to hold the table's
218polynomial. In Tim's experiments the current scheme ran faster, produced
219equally good collision statistics, needed less code & used less memory.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000220
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000221*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000222
Fred Drake1bff34a2000-08-31 19:31:38 +0000223/* forward declarations */
Victor Stinner742da042016-09-07 17:40:12 -0700224static Py_ssize_t lookdict(PyDictObject *mp, PyObject *key,
225 Py_hash_t hash, PyObject ***value_addr,
226 Py_ssize_t *hashpos);
227static Py_ssize_t lookdict_unicode(PyDictObject *mp, PyObject *key,
228 Py_hash_t hash, PyObject ***value_addr,
229 Py_ssize_t *hashpos);
230static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400231lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
Victor Stinner742da042016-09-07 17:40:12 -0700232 Py_hash_t hash, PyObject ***value_addr,
233 Py_ssize_t *hashpos);
234static Py_ssize_t lookdict_split(PyDictObject *mp, PyObject *key,
235 Py_hash_t hash, PyObject ***value_addr,
236 Py_ssize_t *hashpos);
Fred Drake1bff34a2000-08-31 19:31:38 +0000237
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400238static int dictresize(PyDictObject *mp, Py_ssize_t minused);
Tim Petersdea48ec2001-05-22 20:40:22 +0000239
Benjamin Peterson3c569292016-09-08 13:16:41 -0700240/*Global counter used to set ma_version_tag field of dictionary.
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700241 * It is incremented each time that a dictionary is created and each
242 * time that a dictionary is modified. */
243static uint64_t pydict_global_version = 0;
244
245#define DICT_NEXT_VERSION() (++pydict_global_version)
246
Victor Stinner742da042016-09-07 17:40:12 -0700247/* Dictionary reuse scheme to save calls to malloc and free */
Christian Heimes2202f872008-02-06 14:31:34 +0000248#ifndef PyDict_MAXFREELIST
249#define PyDict_MAXFREELIST 80
250#endif
251static PyDictObject *free_list[PyDict_MAXFREELIST];
252static int numfree = 0;
Victor Stinner742da042016-09-07 17:40:12 -0700253static PyDictKeysObject *keys_free_list[PyDict_MAXFREELIST];
254static int numfreekeys = 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000255
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300256#include "clinic/dictobject.c.h"
257
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100258int
259PyDict_ClearFreeList(void)
Christian Heimes77c02eb2008-02-09 02:18:51 +0000260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 PyDictObject *op;
Victor Stinner742da042016-09-07 17:40:12 -0700262 int ret = numfree + numfreekeys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 while (numfree) {
264 op = free_list[--numfree];
265 assert(PyDict_CheckExact(op));
266 PyObject_GC_Del(op);
267 }
Victor Stinner742da042016-09-07 17:40:12 -0700268 while (numfreekeys) {
269 PyObject_FREE(keys_free_list[--numfreekeys]);
270 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100271 return ret;
272}
273
David Malcolm49526f42012-06-22 14:55:41 -0400274/* Print summary info about the state of the optimized allocator */
275void
276_PyDict_DebugMallocStats(FILE *out)
277{
278 _PyDebugAllocatorStats(out,
279 "free PyDictObject", numfree, sizeof(PyDictObject));
280}
281
282
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100283void
284PyDict_Fini(void)
285{
286 PyDict_ClearFreeList();
Christian Heimes77c02eb2008-02-09 02:18:51 +0000287}
288
Victor Stinner742da042016-09-07 17:40:12 -0700289#define DK_SIZE(dk) ((dk)->dk_size)
290#if SIZEOF_VOID_P > 4
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700291#define DK_IXSIZE(dk) \
292 (DK_SIZE(dk) <= 0xff ? \
293 1 : DK_SIZE(dk) <= 0xffff ? \
294 2 : DK_SIZE(dk) <= 0xffffffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700295 4 : sizeof(int64_t))
Victor Stinner742da042016-09-07 17:40:12 -0700296#else
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700297#define DK_IXSIZE(dk) \
298 (DK_SIZE(dk) <= 0xff ? \
299 1 : DK_SIZE(dk) <= 0xffff ? \
Benjamin Peterson3c569292016-09-08 13:16:41 -0700300 2 : sizeof(int32_t))
Victor Stinner742da042016-09-07 17:40:12 -0700301#endif
Victor Stinner58f7c5a2016-09-08 11:37:36 -0700302#define DK_ENTRIES(dk) \
Benjamin Peterson186122e2016-09-08 12:20:12 -0700303 ((PyDictKeyEntry*)(&(dk)->dk_indices.as_1[DK_SIZE(dk) * DK_IXSIZE(dk)]))
Victor Stinner742da042016-09-07 17:40:12 -0700304
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200305#define DK_DEBUG_INCREF _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA
306#define DK_DEBUG_DECREF _Py_DEC_REFTOTAL _Py_REF_DEBUG_COMMA
307
308#define DK_INCREF(dk) (DK_DEBUG_INCREF ++(dk)->dk_refcnt)
309#define DK_DECREF(dk) if (DK_DEBUG_DECREF (--(dk)->dk_refcnt) == 0) free_keys_object(dk)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400310#define DK_MASK(dk) (((dk)->dk_size)-1)
311#define IS_POWER_OF_2(x) (((x) & (x-1)) == 0)
312
Victor Stinner742da042016-09-07 17:40:12 -0700313/* lookup indices. returns DKIX_EMPTY, DKIX_DUMMY, or ix >=0 */
Benjamin Peterson73222252016-09-08 09:58:47 -0700314static inline Py_ssize_t
Victor Stinner742da042016-09-07 17:40:12 -0700315dk_get_index(PyDictKeysObject *keys, Py_ssize_t i)
316{
317 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700318 Py_ssize_t ix;
319
Victor Stinner742da042016-09-07 17:40:12 -0700320 if (s <= 0xff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700321 int8_t *indices = keys->dk_indices.as_1;
Victor Stinner208857e2016-09-08 11:35:46 -0700322 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700323 }
324 else if (s <= 0xffff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700325 int16_t *indices = keys->dk_indices.as_2;
Victor Stinner208857e2016-09-08 11:35:46 -0700326 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700327 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700328#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300329 else if (s > 0xffffffff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700330 int64_t *indices = keys->dk_indices.as_8;
Victor Stinner208857e2016-09-08 11:35:46 -0700331 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700332 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700333#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300334 else {
335 int32_t *indices = keys->dk_indices.as_4;
336 ix = indices[i];
337 }
Victor Stinner71211e32016-09-08 10:52:46 -0700338 assert(ix >= DKIX_DUMMY);
339 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700340}
341
342/* write to indices. */
Benjamin Peterson73222252016-09-08 09:58:47 -0700343static inline void
Victor Stinner742da042016-09-07 17:40:12 -0700344dk_set_index(PyDictKeysObject *keys, Py_ssize_t i, Py_ssize_t ix)
345{
346 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700347
348 assert(ix >= DKIX_DUMMY);
349
Victor Stinner742da042016-09-07 17:40:12 -0700350 if (s <= 0xff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700351 int8_t *indices = keys->dk_indices.as_1;
Victor Stinner71211e32016-09-08 10:52:46 -0700352 assert(ix <= 0x7f);
Victor Stinner208857e2016-09-08 11:35:46 -0700353 indices[i] = (char)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700354 }
355 else if (s <= 0xffff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700356 int16_t *indices = keys->dk_indices.as_2;
Victor Stinner71211e32016-09-08 10:52:46 -0700357 assert(ix <= 0x7fff);
Victor Stinner208857e2016-09-08 11:35:46 -0700358 indices[i] = (int16_t)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700359 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700360#if SIZEOF_VOID_P > 4
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300361 else if (s > 0xffffffff) {
Benjamin Peterson186122e2016-09-08 12:20:12 -0700362 int64_t *indices = keys->dk_indices.as_8;
Victor Stinner208857e2016-09-08 11:35:46 -0700363 indices[i] = ix;
Victor Stinner742da042016-09-07 17:40:12 -0700364 }
Benjamin Peterson3c569292016-09-08 13:16:41 -0700365#endif
Serhiy Storchaka473e0e42016-09-10 21:34:43 +0300366 else {
367 int32_t *indices = keys->dk_indices.as_4;
368 assert(ix <= 0x7fffffff);
369 indices[i] = (int32_t)ix;
370 }
Victor Stinner742da042016-09-07 17:40:12 -0700371}
372
373
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200374/* USABLE_FRACTION is the maximum dictionary load.
Victor Stinner742da042016-09-07 17:40:12 -0700375 * Increasing this ratio makes dictionaries more dense resulting in more
376 * collisions. Decreasing it improves sparseness at the expense of spreading
377 * indices over more cache lines and at the cost of total memory consumed.
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200378 *
379 * USABLE_FRACTION must obey the following:
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400380 * (0 < USABLE_FRACTION(n) < n) for all n >= 2
381 *
Victor Stinner742da042016-09-07 17:40:12 -0700382 * USABLE_FRACTION should be quick to calculate.
383 * Fractions around 1/2 to 2/3 seem to work well in practice.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400384 */
Victor Stinner742da042016-09-07 17:40:12 -0700385#define USABLE_FRACTION(n) (((n) << 1)/3)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400386
Victor Stinner742da042016-09-07 17:40:12 -0700387/* ESTIMATE_SIZE is reverse function of USABLE_FRACTION.
388 * This can be used to reserve enough size to insert n entries without
389 * resizing.
390 */
INADA Naoki2c5a8302016-12-07 18:34:44 +0900391#define ESTIMATE_SIZE(n) (((n)*3+1) >> 1)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400392
Victor Stinner742da042016-09-07 17:40:12 -0700393/* Alternative fraction that is otherwise close enough to 2n/3 to make
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400394 * little difference. 8 * 2/3 == 8 * 5/8 == 5. 16 * 2/3 == 16 * 5/8 == 10.
395 * 32 * 2/3 = 21, 32 * 5/8 = 20.
396 * Its advantage is that it is faster to compute on machines with slow division.
397 * #define USABLE_FRACTION(n) (((n) >> 1) + ((n) >> 2) - ((n) >> 3))
Victor Stinner742da042016-09-07 17:40:12 -0700398 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400399
Victor Stinnera9f61a52013-07-16 22:17:26 +0200400/* GROWTH_RATE. Growth rate upon hitting maximum load.
401 * Currently set to used*2 + capacity/2.
402 * This means that dicts double in size when growing without deletions,
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700403 * but have more head room when the number of deletions is on a par with the
404 * number of insertions.
405 * Raising this to used*4 doubles memory consumption depending on the size of
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200406 * the dictionary, but results in half the number of resizes, less effort to
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700407 * resize.
408 * GROWTH_RATE was set to used*4 up to version 3.2.
409 * GROWTH_RATE was set to used*2 in version 3.3.0
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200410 */
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700411#define GROWTH_RATE(d) (((d)->ma_used*2)+((d)->ma_keys->dk_size>>1))
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400412
413#define ENSURE_ALLOWS_DELETIONS(d) \
414 if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \
415 (d)->ma_keys->dk_lookup = lookdict_unicode; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400417
418/* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
419 * (which cannot fail and thus can do no allocation).
420 */
421static PyDictKeysObject empty_keys_struct = {
Serhiy Storchaka97932e42016-09-26 23:01:23 +0300422 1, /* dk_refcnt */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400423 1, /* dk_size */
424 lookdict_split, /* dk_lookup */
425 0, /* dk_usable (immutable) */
Victor Stinner742da042016-09-07 17:40:12 -0700426 0, /* dk_nentries */
Benjamin Peterson186122e2016-09-08 12:20:12 -0700427 .dk_indices = { .as_1 = {DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY,
428 DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY}},
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400429};
430
431static PyObject *empty_values[1] = { NULL };
432
433#define Py_EMPTY_KEYS &empty_keys_struct
434
Victor Stinner611b0fa2016-09-14 15:02:01 +0200435/* Uncomment to check the dict content in _PyDict_CheckConsistency() */
436/* #define DEBUG_PYDICT */
437
438
439#ifdef Py_DEBUG
440static int
441_PyDict_CheckConsistency(PyDictObject *mp)
442{
443 PyDictKeysObject *keys = mp->ma_keys;
444 int splitted = _PyDict_HasSplitTable(mp);
445 Py_ssize_t usable = USABLE_FRACTION(keys->dk_size);
446#ifdef DEBUG_PYDICT
447 PyDictKeyEntry *entries = DK_ENTRIES(keys);
448 Py_ssize_t i;
449#endif
450
451 assert(0 <= mp->ma_used && mp->ma_used <= usable);
452 assert(IS_POWER_OF_2(keys->dk_size));
453 assert(0 <= keys->dk_usable
454 && keys->dk_usable <= usable);
455 assert(0 <= keys->dk_nentries
456 && keys->dk_nentries <= usable);
457 assert(keys->dk_usable + keys->dk_nentries <= usable);
458
459 if (!splitted) {
460 /* combined table */
461 assert(keys->dk_refcnt == 1);
462 }
463
464#ifdef DEBUG_PYDICT
465 for (i=0; i < keys->dk_size; i++) {
466 Py_ssize_t ix = dk_get_index(keys, i);
467 assert(DKIX_DUMMY <= ix && ix <= usable);
468 }
469
470 for (i=0; i < usable; i++) {
471 PyDictKeyEntry *entry = &entries[i];
472 PyObject *key = entry->me_key;
473
474 if (key != NULL) {
475 if (PyUnicode_CheckExact(key)) {
476 Py_hash_t hash = ((PyASCIIObject *)key)->hash;
477 assert(hash != -1);
478 assert(entry->me_hash == hash);
479 }
480 else {
481 /* test_dict fails if PyObject_Hash() is called again */
482 assert(entry->me_hash != -1);
483 }
484 if (!splitted) {
485 assert(entry->me_value != NULL);
486 }
487 }
488
489 if (splitted) {
490 assert(entry->me_value == NULL);
491 }
492 }
493
494 if (splitted) {
495 /* splitted table */
496 for (i=0; i < mp->ma_used; i++) {
497 assert(mp->ma_values[i] != NULL);
498 }
499 }
500#endif
501
502 return 1;
503}
504#endif
505
506
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400507static PyDictKeysObject *new_keys_object(Py_ssize_t size)
508{
509 PyDictKeysObject *dk;
Victor Stinner742da042016-09-07 17:40:12 -0700510 Py_ssize_t es, usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400511
Victor Stinner742da042016-09-07 17:40:12 -0700512 assert(size >= PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400513 assert(IS_POWER_OF_2(size));
Victor Stinner742da042016-09-07 17:40:12 -0700514
515 usable = USABLE_FRACTION(size);
516 if (size <= 0xff) {
517 es = 1;
518 }
519 else if (size <= 0xffff) {
520 es = 2;
521 }
522#if SIZEOF_VOID_P > 4
523 else if (size <= 0xffffffff) {
524 es = 4;
525 }
526#endif
527 else {
528 es = sizeof(Py_ssize_t);
529 }
530
531 if (size == PyDict_MINSIZE && numfreekeys > 0) {
532 dk = keys_free_list[--numfreekeys];
533 }
534 else {
Victor Stinner98ee9d52016-09-08 09:33:56 -0700535 dk = PyObject_MALLOC(sizeof(PyDictKeysObject)
536 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
537 + es * size
538 + sizeof(PyDictKeyEntry) * usable);
Victor Stinner742da042016-09-07 17:40:12 -0700539 if (dk == NULL) {
540 PyErr_NoMemory();
541 return NULL;
542 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400543 }
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200544 DK_DEBUG_INCREF dk->dk_refcnt = 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400545 dk->dk_size = size;
Victor Stinner742da042016-09-07 17:40:12 -0700546 dk->dk_usable = usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400547 dk->dk_lookup = lookdict_unicode_nodummy;
Victor Stinner742da042016-09-07 17:40:12 -0700548 dk->dk_nentries = 0;
Benjamin Peterson186122e2016-09-08 12:20:12 -0700549 memset(&dk->dk_indices.as_1[0], 0xff, es * size);
Victor Stinner742da042016-09-07 17:40:12 -0700550 memset(DK_ENTRIES(dk), 0, sizeof(PyDictKeyEntry) * usable);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400551 return dk;
552}
553
554static void
555free_keys_object(PyDictKeysObject *keys)
556{
Victor Stinner742da042016-09-07 17:40:12 -0700557 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400558 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -0700559 for (i = 0, n = keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400560 Py_XDECREF(entries[i].me_key);
561 Py_XDECREF(entries[i].me_value);
562 }
Victor Stinner742da042016-09-07 17:40:12 -0700563 if (keys->dk_size == PyDict_MINSIZE && numfreekeys < PyDict_MAXFREELIST) {
564 keys_free_list[numfreekeys++] = keys;
565 return;
566 }
Raymond Hettingerce5179f2016-01-31 08:56:21 -0800567 PyObject_FREE(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400568}
569
570#define new_values(size) PyMem_NEW(PyObject *, size)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400571#define free_values(values) PyMem_FREE(values)
572
573/* Consumes a reference to the keys object */
574static PyObject *
575new_dict(PyDictKeysObject *keys, PyObject **values)
576{
577 PyDictObject *mp;
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200578 assert(keys != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 if (numfree) {
580 mp = free_list[--numfree];
581 assert (mp != NULL);
582 assert (Py_TYPE(mp) == &PyDict_Type);
583 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400585 else {
586 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
587 if (mp == NULL) {
588 DK_DECREF(keys);
589 free_values(values);
590 return NULL;
591 }
592 }
593 mp->ma_keys = keys;
594 mp->ma_values = values;
595 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -0700596 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +0200597 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000599}
600
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400601/* Consumes a reference to the keys object */
602static PyObject *
603new_dict_with_shared_keys(PyDictKeysObject *keys)
604{
605 PyObject **values;
606 Py_ssize_t i, size;
607
Victor Stinner742da042016-09-07 17:40:12 -0700608 size = USABLE_FRACTION(DK_SIZE(keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400609 values = new_values(size);
610 if (values == NULL) {
611 DK_DECREF(keys);
612 return PyErr_NoMemory();
613 }
614 for (i = 0; i < size; i++) {
615 values[i] = NULL;
616 }
617 return new_dict(keys, values);
618}
619
620PyObject *
621PyDict_New(void)
622{
Victor Stinner742da042016-09-07 17:40:12 -0700623 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200624 if (keys == NULL)
625 return NULL;
626 return new_dict(keys, NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400627}
628
Victor Stinner742da042016-09-07 17:40:12 -0700629/* Search index of hash table from offset of entry table */
630static Py_ssize_t
631lookdict_index(PyDictKeysObject *k, Py_hash_t hash, Py_ssize_t index)
632{
INADA Naoki267941c2016-10-06 15:19:07 +0900633 size_t i;
Victor Stinner742da042016-09-07 17:40:12 -0700634 size_t mask = DK_MASK(k);
635 Py_ssize_t ix;
636
637 i = (size_t)hash & mask;
638 ix = dk_get_index(k, i);
639 if (ix == index) {
640 return i;
641 }
642 if (ix == DKIX_EMPTY) {
643 return DKIX_EMPTY;
644 }
645
INADA Naoki267941c2016-10-06 15:19:07 +0900646 for (size_t perturb = hash;;) {
647 perturb >>= PERTURB_SHIFT;
Victor Stinner742da042016-09-07 17:40:12 -0700648 i = mask & ((i << 2) + i + perturb + 1);
649 ix = dk_get_index(k, i);
650 if (ix == index) {
651 return i;
652 }
653 if (ix == DKIX_EMPTY) {
654 return DKIX_EMPTY;
655 }
656 }
657 assert(0); /* NOT REACHED */
658 return DKIX_ERROR;
659}
660
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000661/*
662The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000663This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000664Open addressing is preferred over chaining since the link overhead for
665chaining would be substantial (100% with typical malloc overhead).
666
Tim Peterseb28ef22001-06-02 05:27:19 +0000667The initial probe index is computed as hash mod the table size. Subsequent
668probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000669
670All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000671
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000672The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000673contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000674Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000675
Victor Stinner742da042016-09-07 17:40:12 -0700676lookdict() is general-purpose, and may return DKIX_ERROR if (and only if) a
Victor Stinnera4348cc2016-09-08 12:01:25 -0700677comparison raises an exception.
Guido van Rossum89d8c602007-09-18 17:26:56 +0000678lookdict_unicode() below is specialized to string keys, comparison of which can
INADA Naokice552e22017-02-20 22:58:11 +0900679never raise an exception; that function can never return DKIX_ERROR when key
680is string. Otherwise, it falls back to lookdict().
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400681lookdict_unicode_nodummy is further specialized for string keys that cannot be
682the <dummy> value.
Victor Stinner742da042016-09-07 17:40:12 -0700683For both, when the key isn't found a DKIX_EMPTY is returned. hashpos returns
684where the key index should be inserted.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000685*/
Victor Stinner742da042016-09-07 17:40:12 -0700686static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400687lookdict(PyDictObject *mp, PyObject *key,
Victor Stinner742da042016-09-07 17:40:12 -0700688 Py_hash_t hash, PyObject ***value_addr, Py_ssize_t *hashpos)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000689{
INADA Naoki267941c2016-10-06 15:19:07 +0900690 size_t i, mask;
Victor Stinner742da042016-09-07 17:40:12 -0700691 Py_ssize_t ix, freeslot;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200692 int cmp;
Victor Stinner742da042016-09-07 17:40:12 -0700693 PyDictKeysObject *dk;
694 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000696
Antoine Pitrou9a234902012-05-13 20:48:01 +0200697top:
Victor Stinner742da042016-09-07 17:40:12 -0700698 dk = mp->ma_keys;
699 mask = DK_MASK(dk);
700 ep0 = DK_ENTRIES(dk);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700702
703 ix = dk_get_index(dk, i);
704 if (ix == DKIX_EMPTY) {
705 if (hashpos != NULL)
706 *hashpos = i;
707 *value_addr = NULL;
708 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400709 }
Victor Stinner742da042016-09-07 17:40:12 -0700710 if (ix == DKIX_DUMMY) {
711 freeslot = i;
712 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 else {
Victor Stinner742da042016-09-07 17:40:12 -0700714 ep = &ep0[ix];
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300715 assert(ep->me_key != NULL);
Victor Stinner742da042016-09-07 17:40:12 -0700716 if (ep->me_key == key) {
717 *value_addr = &ep->me_value;
718 if (hashpos != NULL)
719 *hashpos = i;
720 return ix;
721 }
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300722 if (ep->me_hash == hash) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 startkey = ep->me_key;
724 Py_INCREF(startkey);
725 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
726 Py_DECREF(startkey);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +0200727 if (cmp < 0) {
728 *value_addr = NULL;
Victor Stinner742da042016-09-07 17:40:12 -0700729 return DKIX_ERROR;
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +0200730 }
Victor Stinner742da042016-09-07 17:40:12 -0700731 if (dk == mp->ma_keys && ep->me_key == startkey) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400732 if (cmp > 0) {
733 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700734 if (hashpos != NULL)
735 *hashpos = i;
736 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400737 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 }
739 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200740 /* The dict was mutated, restart */
741 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 }
743 }
Victor Stinner742da042016-09-07 17:40:12 -0700744 freeslot = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 }
Tim Peters15d49292001-05-27 07:39:22 +0000746
INADA Naoki267941c2016-10-06 15:19:07 +0900747 for (size_t perturb = hash;;) {
748 perturb >>= PERTURB_SHIFT;
Victor Stinner742da042016-09-07 17:40:12 -0700749 i = ((i << 2) + i + perturb + 1) & mask;
750 ix = dk_get_index(dk, i);
751 if (ix == DKIX_EMPTY) {
752 if (hashpos != NULL) {
753 *hashpos = (freeslot == -1) ? (Py_ssize_t)i : freeslot;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400754 }
Victor Stinner742da042016-09-07 17:40:12 -0700755 *value_addr = NULL;
756 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400757 }
Victor Stinner742da042016-09-07 17:40:12 -0700758 if (ix == DKIX_DUMMY) {
759 if (freeslot == -1)
760 freeslot = i;
761 continue;
762 }
763 ep = &ep0[ix];
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300764 assert(ep->me_key != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400765 if (ep->me_key == key) {
Victor Stinner742da042016-09-07 17:40:12 -0700766 if (hashpos != NULL) {
767 *hashpos = i;
768 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400769 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700770 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400771 }
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300772 if (ep->me_hash == hash) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 startkey = ep->me_key;
774 Py_INCREF(startkey);
775 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
776 Py_DECREF(startkey);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400777 if (cmp < 0) {
778 *value_addr = NULL;
Victor Stinner742da042016-09-07 17:40:12 -0700779 return DKIX_ERROR;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400780 }
Victor Stinner742da042016-09-07 17:40:12 -0700781 if (dk == mp->ma_keys && ep->me_key == startkey) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400782 if (cmp > 0) {
Victor Stinner742da042016-09-07 17:40:12 -0700783 if (hashpos != NULL) {
784 *hashpos = i;
785 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400786 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700787 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400788 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 }
790 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200791 /* The dict was mutated, restart */
792 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 }
794 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 }
796 assert(0); /* NOT REACHED */
797 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000798}
799
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400800/* Specialized version for string-only keys */
Victor Stinner742da042016-09-07 17:40:12 -0700801static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400802lookdict_unicode(PyDictObject *mp, PyObject *key,
Victor Stinner742da042016-09-07 17:40:12 -0700803 Py_hash_t hash, PyObject ***value_addr, Py_ssize_t *hashpos)
Fred Drake1bff34a2000-08-31 19:31:38 +0000804{
INADA Naoki267941c2016-10-06 15:19:07 +0900805 size_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200806 size_t mask = DK_MASK(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -0700807 Py_ssize_t ix, freeslot;
808 PyDictKeyEntry *ep, *ep0 = DK_ENTRIES(mp->ma_keys);
Fred Drake1bff34a2000-08-31 19:31:38 +0000809
Victor Stinner742da042016-09-07 17:40:12 -0700810 assert(mp->ma_values == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 /* Make sure this function doesn't have to handle non-unicode keys,
812 including subclasses of str; e.g., one reason to subclass
813 unicodes is to override __eq__, and for speed we don't cater to
814 that here. */
815 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400816 mp->ma_keys->dk_lookup = lookdict;
Victor Stinner742da042016-09-07 17:40:12 -0700817 return lookdict(mp, key, hash, value_addr, hashpos);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 }
Mark Dickinson57e683e2011-09-24 18:18:40 +0100819 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700820 ix = dk_get_index(mp->ma_keys, i);
821 if (ix == DKIX_EMPTY) {
822 if (hashpos != NULL)
823 *hashpos = i;
824 *value_addr = NULL;
825 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400826 }
Victor Stinner742da042016-09-07 17:40:12 -0700827 if (ix == DKIX_DUMMY) {
828 freeslot = i;
829 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 else {
Victor Stinner742da042016-09-07 17:40:12 -0700831 ep = &ep0[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700832 assert(ep->me_key != NULL);
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300833 if (ep->me_key == key
834 || (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700835 if (hashpos != NULL)
836 *hashpos = i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400837 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700838 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400839 }
Victor Stinner742da042016-09-07 17:40:12 -0700840 freeslot = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 }
Tim Peters15d49292001-05-27 07:39:22 +0000842
INADA Naoki267941c2016-10-06 15:19:07 +0900843 for (size_t perturb = hash;;) {
844 perturb >>= PERTURB_SHIFT;
Victor Stinner742da042016-09-07 17:40:12 -0700845 i = mask & ((i << 2) + i + perturb + 1);
846 ix = dk_get_index(mp->ma_keys, i);
847 if (ix == DKIX_EMPTY) {
848 if (hashpos != NULL) {
849 *hashpos = (freeslot == -1) ? (Py_ssize_t)i : freeslot;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400850 }
Victor Stinner742da042016-09-07 17:40:12 -0700851 *value_addr = NULL;
852 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400853 }
Victor Stinner742da042016-09-07 17:40:12 -0700854 if (ix == DKIX_DUMMY) {
855 if (freeslot == -1)
856 freeslot = i;
857 continue;
858 }
859 ep = &ep0[ix];
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300860 assert(ep->me_key != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 if (ep->me_key == key
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300862 || (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400863 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700864 if (hashpos != NULL) {
865 *hashpos = i;
866 }
867 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400868 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 }
870 assert(0); /* NOT REACHED */
871 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000872}
873
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400874/* Faster version of lookdict_unicode when it is known that no <dummy> keys
875 * will be present. */
Victor Stinner742da042016-09-07 17:40:12 -0700876static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400877lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
Victor Stinner742da042016-09-07 17:40:12 -0700878 Py_hash_t hash, PyObject ***value_addr,
879 Py_ssize_t *hashpos)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400880{
INADA Naoki267941c2016-10-06 15:19:07 +0900881 size_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200882 size_t mask = DK_MASK(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -0700883 Py_ssize_t ix;
884 PyDictKeyEntry *ep, *ep0 = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400885
Victor Stinner742da042016-09-07 17:40:12 -0700886 assert(mp->ma_values == NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400887 /* Make sure this function doesn't have to handle non-unicode keys,
888 including subclasses of str; e.g., one reason to subclass
889 unicodes is to override __eq__, and for speed we don't cater to
890 that here. */
891 if (!PyUnicode_CheckExact(key)) {
892 mp->ma_keys->dk_lookup = lookdict;
Victor Stinner742da042016-09-07 17:40:12 -0700893 return lookdict(mp, key, hash, value_addr, hashpos);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400894 }
895 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700896 ix = dk_get_index(mp->ma_keys, i);
897 assert (ix != DKIX_DUMMY);
898 if (ix == DKIX_EMPTY) {
899 if (hashpos != NULL)
900 *hashpos = i;
901 *value_addr = NULL;
902 return DKIX_EMPTY;
903 }
904 ep = &ep0[ix];
Victor Stinnerdee6e252016-09-08 11:16:07 -0700905 assert(ep->me_key != NULL);
906 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700907 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400908 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700909 if (hashpos != NULL)
910 *hashpos = i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400911 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700912 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400913 }
INADA Naoki267941c2016-10-06 15:19:07 +0900914 for (size_t perturb = hash;;) {
915 perturb >>= PERTURB_SHIFT;
Victor Stinner742da042016-09-07 17:40:12 -0700916 i = mask & ((i << 2) + i + perturb + 1);
917 ix = dk_get_index(mp->ma_keys, i);
918 assert (ix != DKIX_DUMMY);
919 if (ix == DKIX_EMPTY) {
920 if (hashpos != NULL)
921 *hashpos = i;
922 *value_addr = NULL;
923 return DKIX_EMPTY;
924 }
925 ep = &ep0[ix];
926 assert(ep->me_key != NULL && PyUnicode_CheckExact(ep->me_key));
927 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400928 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700929 if (hashpos != NULL)
930 *hashpos = i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400931 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700932 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400933 }
934 }
935 assert(0); /* NOT REACHED */
936 return 0;
937}
938
939/* Version of lookdict for split tables.
940 * All split tables and only split tables use this lookup function.
941 * Split tables only contain unicode keys and no dummy keys,
942 * so algorithm is the same as lookdict_unicode_nodummy.
943 */
Victor Stinner742da042016-09-07 17:40:12 -0700944static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400945lookdict_split(PyDictObject *mp, PyObject *key,
Victor Stinner742da042016-09-07 17:40:12 -0700946 Py_hash_t hash, PyObject ***value_addr, Py_ssize_t *hashpos)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400947{
INADA Naoki267941c2016-10-06 15:19:07 +0900948 size_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200949 size_t mask = DK_MASK(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -0700950 Py_ssize_t ix;
951 PyDictKeyEntry *ep, *ep0 = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400952
Victor Stinner742da042016-09-07 17:40:12 -0700953 /* mp must split table */
954 assert(mp->ma_values != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400955 if (!PyUnicode_CheckExact(key)) {
Victor Stinner742da042016-09-07 17:40:12 -0700956 ix = lookdict(mp, key, hash, value_addr, hashpos);
957 if (ix >= 0) {
958 *value_addr = &mp->ma_values[ix];
959 }
960 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400961 }
Victor Stinner742da042016-09-07 17:40:12 -0700962
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400963 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700964 ix = dk_get_index(mp->ma_keys, i);
965 if (ix == DKIX_EMPTY) {
966 if (hashpos != NULL)
967 *hashpos = i;
968 *value_addr = NULL;
969 return DKIX_EMPTY;
970 }
971 assert(ix >= 0);
972 ep = &ep0[ix];
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300973 assert(ep->me_key != NULL && PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700974 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400975 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700976 if (hashpos != NULL)
977 *hashpos = i;
978 *value_addr = &mp->ma_values[ix];
979 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400980 }
INADA Naoki267941c2016-10-06 15:19:07 +0900981 for (size_t perturb = hash;;) {
982 perturb >>= PERTURB_SHIFT;
Victor Stinner742da042016-09-07 17:40:12 -0700983 i = mask & ((i << 2) + i + perturb + 1);
984 ix = dk_get_index(mp->ma_keys, i);
985 if (ix == DKIX_EMPTY) {
986 if (hashpos != NULL)
987 *hashpos = i;
988 *value_addr = NULL;
989 return DKIX_EMPTY;
990 }
991 assert(ix >= 0);
992 ep = &ep0[ix];
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300993 assert(ep->me_key != NULL && PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700994 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400995 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700996 if (hashpos != NULL)
997 *hashpos = i;
998 *value_addr = &mp->ma_values[ix];
999 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001000 }
1001 }
1002 assert(0); /* NOT REACHED */
1003 return 0;
1004}
1005
Benjamin Petersonfb886362010-04-24 18:21:17 +00001006int
1007_PyDict_HasOnlyStringKeys(PyObject *dict)
1008{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 Py_ssize_t pos = 0;
1010 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +00001011 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001013 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 return 1;
1015 while (PyDict_Next(dict, &pos, &key, &value))
1016 if (!PyUnicode_Check(key))
1017 return 0;
1018 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +00001019}
1020
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001021#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 do { \
1023 if (!_PyObject_GC_IS_TRACKED(mp)) { \
1024 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
1025 _PyObject_GC_MAY_BE_TRACKED(value)) { \
1026 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 } \
1028 } \
1029 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001030
1031void
1032_PyDict_MaybeUntrack(PyObject *op)
1033{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 PyDictObject *mp;
1035 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07001036 Py_ssize_t i, numentries;
1037 PyDictKeyEntry *ep0;
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001038
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
1040 return;
1041
1042 mp = (PyDictObject *) op;
Victor Stinner742da042016-09-07 17:40:12 -07001043 ep0 = DK_ENTRIES(mp->ma_keys);
1044 numentries = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001045 if (_PyDict_HasSplitTable(mp)) {
Victor Stinner742da042016-09-07 17:40:12 -07001046 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001047 if ((value = mp->ma_values[i]) == NULL)
1048 continue;
1049 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
Victor Stinner742da042016-09-07 17:40:12 -07001050 assert(!_PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001051 return;
1052 }
1053 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001055 else {
Victor Stinner742da042016-09-07 17:40:12 -07001056 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001057 if ((value = ep0[i].me_value) == NULL)
1058 continue;
1059 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
1060 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
1061 return;
1062 }
1063 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001065}
1066
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001067/* Internal function to find slot for an item from its hash
Victor Stinner3c336c52016-09-12 14:17:40 +02001068 when it is known that the key is not present in the dict.
1069
1070 The dict must be combined. */
Victor Stinner99264802016-09-13 09:38:29 +02001071static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001072find_empty_slot(PyDictObject *mp, PyObject *key, Py_hash_t hash,
Victor Stinner742da042016-09-07 17:40:12 -07001073 PyObject ***value_addr, Py_ssize_t *hashpos)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001074{
INADA Naoki267941c2016-10-06 15:19:07 +09001075 size_t i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001076 size_t mask = DK_MASK(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07001077 Py_ssize_t ix;
1078 PyDictKeyEntry *ep, *ep0 = DK_ENTRIES(mp->ma_keys);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001079
Victor Stinner3c336c52016-09-12 14:17:40 +02001080 assert(!_PyDict_HasSplitTable(mp));
Victor Stinner742da042016-09-07 17:40:12 -07001081 assert(hashpos != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001082 assert(key != NULL);
Victor Stinner3c336c52016-09-12 14:17:40 +02001083
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001084 if (!PyUnicode_CheckExact(key))
1085 mp->ma_keys->dk_lookup = lookdict;
1086 i = hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -07001087 ix = dk_get_index(mp->ma_keys, i);
INADA Naoki267941c2016-10-06 15:19:07 +09001088 for (size_t perturb = hash; ix != DKIX_EMPTY;) {
1089 perturb >>= PERTURB_SHIFT;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001090 i = (i << 2) + i + perturb + 1;
Victor Stinner742da042016-09-07 17:40:12 -07001091 ix = dk_get_index(mp->ma_keys, i & mask);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 }
Victor Stinner742da042016-09-07 17:40:12 -07001093 ep = &ep0[mp->ma_keys->dk_nentries];
1094 *hashpos = i & mask;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001095 assert(ep->me_value == NULL);
Victor Stinner99264802016-09-13 09:38:29 +02001096 *value_addr = &ep->me_value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001097}
1098
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001099static int
1100insertion_resize(PyDictObject *mp)
1101{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -07001102 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001103}
Antoine Pitroue965d972012-02-27 00:45:12 +01001104
1105/*
1106Internal routine to insert a new item into the table.
1107Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +01001108Returns -1 if an error occurred, or 0 on success.
1109*/
1110static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001111insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001112{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001113 PyObject *old_value;
1114 PyObject **value_addr;
Victor Stinner742da042016-09-07 17:40:12 -07001115 PyDictKeyEntry *ep, *ep0;
1116 Py_ssize_t hashpos, ix;
Antoine Pitroue965d972012-02-27 00:45:12 +01001117
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001118 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
1119 if (insertion_resize(mp) < 0)
1120 return -1;
1121 }
1122
Victor Stinner742da042016-09-07 17:40:12 -07001123 ix = mp->ma_keys->dk_lookup(mp, key, hash, &value_addr, &hashpos);
1124 if (ix == DKIX_ERROR) {
Antoine Pitroue965d972012-02-27 00:45:12 +01001125 return -1;
1126 }
Victor Stinner742da042016-09-07 17:40:12 -07001127
Antoine Pitroud6967322014-10-18 00:35:00 +02001128 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
Benjamin Petersona6f195e2012-04-30 10:23:40 -04001129 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001130 MAINTAIN_TRACKING(mp, key, value);
Victor Stinner742da042016-09-07 17:40:12 -07001131
1132 /* When insertion order is different from shared key, we can't share
1133 * the key anymore. Convert this instance to combine table.
1134 */
1135 if (_PyDict_HasSplitTable(mp) &&
1136 ((ix >= 0 && *value_addr == NULL && mp->ma_used != ix) ||
1137 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
1138 if (insertion_resize(mp) < 0) {
1139 Py_DECREF(value);
1140 return -1;
1141 }
1142 find_empty_slot(mp, key, hash, &value_addr, &hashpos);
1143 ix = DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001144 }
Victor Stinner742da042016-09-07 17:40:12 -07001145
1146 if (ix == DKIX_EMPTY) {
1147 /* Insert into new slot. */
1148 if (mp->ma_keys->dk_usable <= 0) {
1149 /* Need to resize. */
1150 if (insertion_resize(mp) < 0) {
1151 Py_DECREF(value);
1152 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001153 }
Victor Stinner742da042016-09-07 17:40:12 -07001154 find_empty_slot(mp, key, hash, &value_addr, &hashpos);
1155 }
1156 ep0 = DK_ENTRIES(mp->ma_keys);
1157 ep = &ep0[mp->ma_keys->dk_nentries];
1158 dk_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
1159 Py_INCREF(key);
1160 ep->me_key = key;
1161 ep->me_hash = hash;
1162 if (mp->ma_values) {
1163 assert (mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
1164 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001165 }
1166 else {
Victor Stinner742da042016-09-07 17:40:12 -07001167 ep->me_value = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001168 }
1169 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001170 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07001171 mp->ma_keys->dk_usable--;
1172 mp->ma_keys->dk_nentries++;
1173 assert(mp->ma_keys->dk_usable >= 0);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001174 assert(_PyDict_CheckConsistency(mp));
Victor Stinner742da042016-09-07 17:40:12 -07001175 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001176 }
Victor Stinner742da042016-09-07 17:40:12 -07001177
1178 assert(value_addr != NULL);
1179
1180 old_value = *value_addr;
1181 if (old_value != NULL) {
1182 *value_addr = value;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001183 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02001184 assert(_PyDict_CheckConsistency(mp));
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001185
Victor Stinner742da042016-09-07 17:40:12 -07001186 Py_DECREF(old_value); /* which **CAN** re-enter (see issue #22653) */
1187 return 0;
1188 }
1189
1190 /* pending state */
1191 assert(_PyDict_HasSplitTable(mp));
1192 assert(ix == mp->ma_used);
1193 *value_addr = value;
1194 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001195 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02001196 assert(_PyDict_CheckConsistency(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001197 return 0;
Antoine Pitroue965d972012-02-27 00:45:12 +01001198}
1199
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001200/*
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001201Internal routine used by dictresize() to insert an item which is
1202known to be absent from the dict. This routine also assumes that
1203the dict contains no deleted entries. Besides the performance benefit,
1204using insertdict() in dictresize() is dangerous (SF bug #1456209).
1205Note that no refcounts are changed by this routine; if needed, the caller
1206is responsible for incref'ing `key` and `value`.
1207Neither mp->ma_used nor k->dk_usable are modified by this routine; the caller
1208must set them correctly
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001209*/
1210static void
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001211insertdict_clean(PyDictObject *mp, PyObject *key, Py_hash_t hash,
1212 PyObject *value)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001213{
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001214 size_t i;
1215 PyDictKeysObject *k = mp->ma_keys;
1216 size_t mask = (size_t)DK_SIZE(k)-1;
1217 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
1218 PyDictKeyEntry *ep;
1219
1220 assert(k->dk_lookup != NULL);
1221 assert(value != NULL);
1222 assert(key != NULL);
1223 assert(PyUnicode_CheckExact(key) || k->dk_lookup == lookdict);
1224 i = hash & mask;
1225 for (size_t perturb = hash; dk_get_index(k, i) != DKIX_EMPTY;) {
1226 perturb >>= PERTURB_SHIFT;
1227 i = mask & ((i << 2) + i + perturb + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 }
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001229 ep = &ep0[k->dk_nentries];
1230 assert(ep->me_value == NULL);
1231 dk_set_index(k, i, k->dk_nentries);
1232 k->dk_nentries++;
1233 ep->me_key = key;
1234 ep->me_hash = hash;
1235 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001236}
1237
1238/*
1239Restructure the table by allocating a new table and reinserting all
1240items again. When entries have been deleted, the new table may
1241actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001242If a table is split (its keys and hashes are shared, its values are not),
1243then the values are temporarily copied into the table, it is resized as
1244a combined table, then the me_value slots in the old table are NULLed out.
1245After resizing a table is always combined,
1246but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001247*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001248static int
Victor Stinner3d3f2642016-12-15 17:21:23 +01001249dictresize(PyDictObject *mp, Py_ssize_t minsize)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001250{
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001251 Py_ssize_t i, newsize;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001252 PyDictKeysObject *oldkeys;
1253 PyObject **oldvalues;
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001254 PyDictKeyEntry *ep0;
Tim Peters91a364d2001-05-19 07:04:38 +00001255
Victor Stinner742da042016-09-07 17:40:12 -07001256 /* Find the smallest table size > minused. */
1257 for (newsize = PyDict_MINSIZE;
Victor Stinner3d3f2642016-12-15 17:21:23 +01001258 newsize < minsize && newsize > 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 newsize <<= 1)
1260 ;
1261 if (newsize <= 0) {
1262 PyErr_NoMemory();
1263 return -1;
1264 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001265 oldkeys = mp->ma_keys;
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001266 oldvalues = mp->ma_values;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001267 /* Allocate a new table. */
1268 mp->ma_keys = new_keys_object(newsize);
1269 if (mp->ma_keys == NULL) {
1270 mp->ma_keys = oldkeys;
1271 return -1;
1272 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01001273 // New table must be large enough.
1274 assert(mp->ma_keys->dk_usable >= mp->ma_used);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001275 if (oldkeys->dk_lookup == lookdict)
1276 mp->ma_keys->dk_lookup = lookdict;
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001277 mp->ma_values = NULL;
1278 ep0 = DK_ENTRIES(oldkeys);
1279 /* Main loop below assumes we can transfer refcount to new keys
1280 * and that value is stored in me_value.
1281 * Increment ref-counts and copy values here to compensate
1282 * This (resizing a split table) should be relatively rare */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001283 if (oldvalues != NULL) {
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001284 for (i = 0; i < oldkeys->dk_nentries; i++) {
1285 if (oldvalues[i] != NULL) {
1286 Py_INCREF(ep0[i].me_key);
1287 ep0[i].me_value = oldvalues[i];
1288 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 }
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001290 }
1291 /* Main loop */
1292 for (i = 0; i < oldkeys->dk_nentries; i++) {
1293 PyDictKeyEntry *ep = &ep0[i];
1294 if (ep->me_value != NULL) {
1295 insertdict_clean(mp, ep->me_key, ep->me_hash, ep->me_value);
1296 }
1297 }
1298 mp->ma_keys->dk_usable -= mp->ma_used;
1299 if (oldvalues != NULL) {
1300 /* NULL out me_value slot in oldkeys, in case it was shared */
1301 for (i = 0; i < oldkeys->dk_nentries; i++)
1302 ep0[i].me_value = NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001303 DK_DECREF(oldkeys);
Victor Stinner742da042016-09-07 17:40:12 -07001304 if (oldvalues != empty_values) {
1305 free_values(oldvalues);
1306 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001307 }
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001308 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001309 assert(oldkeys->dk_lookup != lookdict_split);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001310 assert(oldkeys->dk_refcnt == 1);
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001311 DK_DEBUG_DECREF PyObject_FREE(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001312 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001314}
1315
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001316/* Returns NULL if unable to split table.
1317 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001318static PyDictKeysObject *
1319make_keys_shared(PyObject *op)
1320{
1321 Py_ssize_t i;
1322 Py_ssize_t size;
1323 PyDictObject *mp = (PyDictObject *)op;
1324
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001325 if (!PyDict_CheckExact(op))
1326 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001327 if (!_PyDict_HasSplitTable(mp)) {
1328 PyDictKeyEntry *ep0;
1329 PyObject **values;
1330 assert(mp->ma_keys->dk_refcnt == 1);
1331 if (mp->ma_keys->dk_lookup == lookdict) {
1332 return NULL;
1333 }
1334 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1335 /* Remove dummy keys */
1336 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1337 return NULL;
1338 }
1339 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1340 /* Copy values into a new array */
Victor Stinner742da042016-09-07 17:40:12 -07001341 ep0 = DK_ENTRIES(mp->ma_keys);
1342 size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001343 values = new_values(size);
1344 if (values == NULL) {
1345 PyErr_SetString(PyExc_MemoryError,
1346 "Not enough memory to allocate new values array");
1347 return NULL;
1348 }
1349 for (i = 0; i < size; i++) {
1350 values[i] = ep0[i].me_value;
1351 ep0[i].me_value = NULL;
1352 }
1353 mp->ma_keys->dk_lookup = lookdict_split;
1354 mp->ma_values = values;
1355 }
1356 DK_INCREF(mp->ma_keys);
1357 return mp->ma_keys;
1358}
Christian Heimes99170a52007-12-19 02:07:34 +00001359
1360PyObject *
1361_PyDict_NewPresized(Py_ssize_t minused)
1362{
INADA Naoki2c5a8302016-12-07 18:34:44 +09001363 const Py_ssize_t max_presize = 128 * 1024;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001364 Py_ssize_t newsize;
1365 PyDictKeysObject *new_keys;
INADA Naoki2c5a8302016-12-07 18:34:44 +09001366
1367 /* There are no strict guarantee that returned dict can contain minused
1368 * items without resize. So we create medium size dict instead of very
1369 * large dict or MemoryError.
1370 */
1371 if (minused > USABLE_FRACTION(max_presize)) {
1372 newsize = max_presize;
1373 }
1374 else {
1375 Py_ssize_t minsize = ESTIMATE_SIZE(minused);
1376 newsize = PyDict_MINSIZE;
1377 while (newsize < minsize) {
1378 newsize <<= 1;
1379 }
1380 }
1381 assert(IS_POWER_OF_2(newsize));
1382
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001383 new_keys = new_keys_object(newsize);
1384 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001386 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001387}
1388
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001389/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1390 * that may occur (originally dicts supported only string keys, and exceptions
1391 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001392 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001393 * (suppressed) occurred while computing the key's hash, or that some error
1394 * (suppressed) occurred when comparing keys in the dict's internal probe
1395 * sequence. A nasty example of the latter is when a Python-coded comparison
1396 * function hits a stack-depth error, which can cause this to return NULL
1397 * even if the key is present.
1398 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001399PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001400PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001401{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001402 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07001403 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 PyDictObject *mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 PyThreadState *tstate;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001406 PyObject **value_addr;
1407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 if (!PyDict_Check(op))
1409 return NULL;
1410 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001411 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 {
1413 hash = PyObject_Hash(key);
1414 if (hash == -1) {
1415 PyErr_Clear();
1416 return NULL;
1417 }
1418 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 /* We can arrive here with a NULL tstate during initialization: try
1421 running "python -Wi" for an example related to string interning.
1422 Let's just hope that no exception occurs then... This must be
1423 _PyThreadState_Current and not PyThreadState_GET() because in debug
1424 mode, the latter complains if tstate is NULL. */
Victor Stinnerbfd316e2016-01-20 11:12:38 +01001425 tstate = _PyThreadState_UncheckedGet();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 if (tstate != NULL && tstate->curexc_type != NULL) {
1427 /* preserve the existing exception */
1428 PyObject *err_type, *err_value, *err_tb;
1429 PyErr_Fetch(&err_type, &err_value, &err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001430 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431 /* ignore errors */
1432 PyErr_Restore(err_type, err_value, err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001433 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 return NULL;
1435 }
1436 else {
Victor Stinner742da042016-09-07 17:40:12 -07001437 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
1438 if (ix < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 PyErr_Clear();
1440 return NULL;
1441 }
1442 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001443 return *value_addr;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001444}
1445
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001446/* Same as PyDict_GetItemWithError() but with hash supplied by caller.
1447 This returns NULL *with* an exception set if an exception occurred.
1448 It returns NULL *without* an exception set if the key wasn't present.
1449*/
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001450PyObject *
1451_PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1452{
Victor Stinner742da042016-09-07 17:40:12 -07001453 Py_ssize_t ix;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001454 PyDictObject *mp = (PyDictObject *)op;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001455 PyObject **value_addr;
1456
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001457 if (!PyDict_Check(op)) {
1458 PyErr_BadInternalCall();
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001459 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001460 }
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001461
1462 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
1463 if (ix < 0) {
1464 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001465 }
1466 return *value_addr;
1467}
1468
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001469/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1470 This returns NULL *with* an exception set if an exception occurred.
1471 It returns NULL *without* an exception set if the key wasn't present.
1472*/
1473PyObject *
1474PyDict_GetItemWithError(PyObject *op, PyObject *key)
1475{
Victor Stinner742da042016-09-07 17:40:12 -07001476 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001477 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 PyDictObject*mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001479 PyObject **value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 if (!PyDict_Check(op)) {
1482 PyErr_BadInternalCall();
1483 return NULL;
1484 }
1485 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001486 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 {
1488 hash = PyObject_Hash(key);
1489 if (hash == -1) {
1490 return NULL;
1491 }
1492 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001493
Victor Stinner742da042016-09-07 17:40:12 -07001494 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
1495 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001497 return *value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001498}
1499
Brett Cannonfd074152012-04-14 14:10:13 -04001500PyObject *
1501_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1502{
1503 PyObject *kv;
1504 kv = _PyUnicode_FromId(key); /* borrowed */
1505 if (kv == NULL)
1506 return NULL;
1507 return PyDict_GetItemWithError(dp, kv);
1508}
1509
Victor Stinnerb4efc962015-11-20 09:24:02 +01001510/* Fast version of global value lookup (LOAD_GLOBAL).
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001511 * Lookup in globals, then builtins.
Victor Stinnerb4efc962015-11-20 09:24:02 +01001512 *
1513 * Raise an exception and return NULL if an error occurred (ex: computing the
1514 * key hash failed, key comparison failed, ...). Return NULL if the key doesn't
1515 * exist. Return the value if the key exists.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001516 */
1517PyObject *
1518_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001519{
Victor Stinner742da042016-09-07 17:40:12 -07001520 Py_ssize_t ix;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001521 Py_hash_t hash;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001522 PyObject **value_addr;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001523
1524 if (!PyUnicode_CheckExact(key) ||
1525 (hash = ((PyASCIIObject *) key)->hash) == -1)
1526 {
1527 hash = PyObject_Hash(key);
1528 if (hash == -1)
1529 return NULL;
Antoine Pitroue965d972012-02-27 00:45:12 +01001530 }
Victor Stinnerb4efc962015-11-20 09:24:02 +01001531
1532 /* namespace 1: globals */
Victor Stinner742da042016-09-07 17:40:12 -07001533 ix = globals->ma_keys->dk_lookup(globals, key, hash, &value_addr, NULL);
1534 if (ix == DKIX_ERROR)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001535 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001536 if (ix != DKIX_EMPTY && *value_addr != NULL)
1537 return *value_addr;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001538
1539 /* namespace 2: builtins */
Victor Stinner742da042016-09-07 17:40:12 -07001540 ix = builtins->ma_keys->dk_lookup(builtins, key, hash, &value_addr, NULL);
1541 if (ix < 0)
Victor Stinnerb4efc962015-11-20 09:24:02 +01001542 return NULL;
1543 return *value_addr;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001544}
1545
Antoine Pitroue965d972012-02-27 00:45:12 +01001546/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1547 * dictionary if it's merely replacing the value for an existing key.
1548 * This means that it's safe to loop over a dictionary with PyDict_Next()
1549 * and occasionally replace a value -- but you can't insert new keys or
1550 * remove them.
1551 */
1552int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001553PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001554{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001555 PyDictObject *mp;
1556 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001557 if (!PyDict_Check(op)) {
1558 PyErr_BadInternalCall();
1559 return -1;
1560 }
1561 assert(key);
1562 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001563 mp = (PyDictObject *)op;
1564 if (!PyUnicode_CheckExact(key) ||
1565 (hash = ((PyASCIIObject *) key)->hash) == -1)
1566 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001567 hash = PyObject_Hash(key);
1568 if (hash == -1)
1569 return -1;
1570 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001571
1572 /* insertdict() handles any resizing that might be necessary */
1573 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001574}
1575
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001576int
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001577_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1578 Py_hash_t hash)
1579{
1580 PyDictObject *mp;
1581
1582 if (!PyDict_Check(op)) {
1583 PyErr_BadInternalCall();
1584 return -1;
1585 }
1586 assert(key);
1587 assert(value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001588 assert(hash != -1);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001589 mp = (PyDictObject *)op;
1590
1591 /* insertdict() handles any resizing that might be necessary */
1592 return insertdict(mp, key, hash, value);
1593}
1594
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001595static int
Antoine Pitroud741ed42016-12-27 14:23:43 +01001596delitem_common(PyDictObject *mp, Py_ssize_t hashpos, Py_ssize_t ix,
1597 PyObject **value_addr)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001598{
1599 PyObject *old_key, *old_value;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001600 PyDictKeyEntry *ep;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001601
1602 old_value = *value_addr;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001603 assert(old_value != NULL);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001604 *value_addr = NULL;
1605 mp->ma_used--;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001606 mp->ma_version_tag = DICT_NEXT_VERSION();
1607 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1608 dk_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
1609 ENSURE_ALLOWS_DELETIONS(mp);
1610 old_key = ep->me_key;
1611 ep->me_key = NULL;
1612 Py_DECREF(old_key);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001613 Py_DECREF(old_value);
Antoine Pitroud741ed42016-12-27 14:23:43 +01001614
1615 assert(_PyDict_CheckConsistency(mp));
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001616 return 0;
1617}
1618
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001619int
Tim Peters1f5871e2000-07-04 17:44:48 +00001620PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001621{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001622 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 assert(key);
1624 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001625 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 hash = PyObject_Hash(key);
1627 if (hash == -1)
1628 return -1;
1629 }
Victor Stinner742da042016-09-07 17:40:12 -07001630
1631 return _PyDict_DelItem_KnownHash(op, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001632}
1633
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001634int
1635_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1636{
Victor Stinner742da042016-09-07 17:40:12 -07001637 Py_ssize_t hashpos, ix;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001638 PyDictObject *mp;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001639 PyObject **value_addr;
1640
1641 if (!PyDict_Check(op)) {
1642 PyErr_BadInternalCall();
1643 return -1;
1644 }
1645 assert(key);
1646 assert(hash != -1);
1647 mp = (PyDictObject *)op;
Victor Stinner742da042016-09-07 17:40:12 -07001648 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
1649 if (ix == DKIX_ERROR)
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001650 return -1;
Victor Stinner742da042016-09-07 17:40:12 -07001651 if (ix == DKIX_EMPTY || *value_addr == NULL) {
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001652 _PyErr_SetKeyError(key);
1653 return -1;
1654 }
Victor Stinner742da042016-09-07 17:40:12 -07001655 assert(dk_get_index(mp->ma_keys, hashpos) == ix);
Victor Stinner78601a32016-09-09 19:28:36 -07001656
1657 // Split table doesn't allow deletion. Combine it.
1658 if (_PyDict_HasSplitTable(mp)) {
1659 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1660 return -1;
1661 }
1662 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
1663 assert(ix >= 0);
1664 }
Antoine Pitroud741ed42016-12-27 14:23:43 +01001665 return delitem_common(mp, hashpos, ix, value_addr);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001666}
1667
Antoine Pitroud741ed42016-12-27 14:23:43 +01001668/* This function promises that the predicate -> deletion sequence is atomic
1669 * (i.e. protected by the GIL), assuming the predicate itself doesn't
1670 * release the GIL.
1671 */
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001672int
1673_PyDict_DelItemIf(PyObject *op, PyObject *key,
1674 int (*predicate)(PyObject *value))
1675{
Antoine Pitroud741ed42016-12-27 14:23:43 +01001676 Py_ssize_t hashpos, ix;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001677 PyDictObject *mp;
1678 Py_hash_t hash;
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001679 PyObject **value_addr;
1680 int res;
1681
1682 if (!PyDict_Check(op)) {
1683 PyErr_BadInternalCall();
1684 return -1;
1685 }
1686 assert(key);
1687 hash = PyObject_Hash(key);
1688 if (hash == -1)
1689 return -1;
1690 mp = (PyDictObject *)op;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001691 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
1692 if (ix == DKIX_ERROR)
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001693 return -1;
Antoine Pitroud741ed42016-12-27 14:23:43 +01001694 if (ix == DKIX_EMPTY || *value_addr == NULL) {
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001695 _PyErr_SetKeyError(key);
1696 return -1;
1697 }
Antoine Pitroud741ed42016-12-27 14:23:43 +01001698 assert(dk_get_index(mp->ma_keys, hashpos) == ix);
1699
1700 // Split table doesn't allow deletion. Combine it.
1701 if (_PyDict_HasSplitTable(mp)) {
1702 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1703 return -1;
1704 }
1705 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
1706 assert(ix >= 0);
1707 }
1708
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001709 res = predicate(*value_addr);
1710 if (res == -1)
1711 return -1;
1712 if (res > 0)
Antoine Pitroud741ed42016-12-27 14:23:43 +01001713 return delitem_common(mp, hashpos, ix, value_addr);
Antoine Pitroue10ca3a2016-12-27 14:19:20 +01001714 else
1715 return 0;
1716}
1717
1718
Guido van Rossum25831651993-05-19 14:50:45 +00001719void
Tim Peters1f5871e2000-07-04 17:44:48 +00001720PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001721{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001723 PyDictKeysObject *oldkeys;
1724 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 if (!PyDict_Check(op))
1728 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001729 mp = ((PyDictObject *)op);
1730 oldkeys = mp->ma_keys;
1731 oldvalues = mp->ma_values;
1732 if (oldvalues == empty_values)
1733 return;
1734 /* Empty the dict... */
1735 DK_INCREF(Py_EMPTY_KEYS);
1736 mp->ma_keys = Py_EMPTY_KEYS;
1737 mp->ma_values = empty_values;
1738 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001739 mp->ma_version_tag = DICT_NEXT_VERSION();
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001740 /* ...then clear the keys and values */
1741 if (oldvalues != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001742 n = oldkeys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001743 for (i = 0; i < n; i++)
1744 Py_CLEAR(oldvalues[i]);
1745 free_values(oldvalues);
1746 DK_DECREF(oldkeys);
1747 }
1748 else {
1749 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001750 DK_DECREF(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001751 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02001752 assert(_PyDict_CheckConsistency(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001753}
1754
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001755/* Internal version of PyDict_Next that returns a hash value in addition
1756 * to the key and value.
1757 * Return 1 on success, return 0 when the reached the end of the dictionary
1758 * (or if op is not a dictionary)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001759 */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001760int
1761_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1762 PyObject **pvalue, Py_hash_t *phash)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001763{
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001764 Py_ssize_t i, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001765 PyDictObject *mp;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001766 PyDictKeyEntry *entry_ptr;
1767 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001768
1769 if (!PyDict_Check(op))
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001770 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 mp = (PyDictObject *)op;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001772 i = *ppos;
Victor Stinner742da042016-09-07 17:40:12 -07001773 n = mp->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001774 if ((size_t)i >= (size_t)n)
1775 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001776 if (mp->ma_values) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001777 PyObject **value_ptr = &mp->ma_values[i];
1778 while (i < n && *value_ptr == NULL) {
1779 value_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 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
1785 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001787 else {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001788 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
1789 while (i < n && entry_ptr->me_value == NULL) {
1790 entry_ptr++;
1791 i++;
Victor Stinner742da042016-09-07 17:40:12 -07001792 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001793 if (i >= n)
1794 return 0;
1795 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001796 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001797 *ppos = i+1;
1798 if (pkey)
1799 *pkey = entry_ptr->me_key;
1800 if (phash)
1801 *phash = entry_ptr->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001802 if (pvalue)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001803 *pvalue = value;
1804 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001805}
1806
Tim Peters080c88b2003-02-15 03:01:11 +00001807/*
1808 * Iterate over a dict. Use like so:
1809 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001810 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001811 * PyObject *key, *value;
1812 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001813 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001814 * Refer to borrowed references in key and value.
Tim Peters080c88b2003-02-15 03:01:11 +00001815 * }
1816 *
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001817 * Return 1 on success, return 0 when the reached the end of the dictionary
1818 * (or if op is not a dictionary)
1819 *
Tim Peters080c88b2003-02-15 03:01:11 +00001820 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001821 * mutates the dict. One exception: it is safe if the loop merely changes
1822 * the values associated with the keys (but doesn't insert new keys or
1823 * delete keys), via PyDict_SetItem().
1824 */
Guido van Rossum25831651993-05-19 14:50:45 +00001825int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001826PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001827{
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001828 return _PyDict_Next(op, ppos, pkey, pvalue, NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001829}
1830
Eric Snow96c6af92015-05-29 22:21:39 -06001831/* Internal version of dict.pop(). */
1832PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001833_PyDict_Pop_KnownHash(PyObject *dict, PyObject *key, Py_hash_t hash, PyObject *deflt)
Eric Snow96c6af92015-05-29 22:21:39 -06001834{
Victor Stinner742da042016-09-07 17:40:12 -07001835 Py_ssize_t ix, hashpos;
Eric Snow96c6af92015-05-29 22:21:39 -06001836 PyObject *old_value, *old_key;
1837 PyDictKeyEntry *ep;
1838 PyObject **value_addr;
Yury Selivanov684ef2c2016-10-28 19:01:21 -04001839 PyDictObject *mp;
1840
1841 assert(PyDict_Check(dict));
1842 mp = (PyDictObject *)dict;
Eric Snow96c6af92015-05-29 22:21:39 -06001843
1844 if (mp->ma_used == 0) {
1845 if (deflt) {
1846 Py_INCREF(deflt);
1847 return deflt;
1848 }
1849 _PyErr_SetKeyError(key);
1850 return NULL;
1851 }
Victor Stinner742da042016-09-07 17:40:12 -07001852 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
1853 if (ix == DKIX_ERROR)
Eric Snow96c6af92015-05-29 22:21:39 -06001854 return NULL;
Victor Stinnerd0ad11f2016-09-13 16:56:38 +02001855 if (ix == DKIX_EMPTY || *value_addr == NULL) {
Eric Snow96c6af92015-05-29 22:21:39 -06001856 if (deflt) {
1857 Py_INCREF(deflt);
1858 return deflt;
1859 }
1860 _PyErr_SetKeyError(key);
1861 return NULL;
1862 }
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001863
Victor Stinner78601a32016-09-09 19:28:36 -07001864 // Split table doesn't allow deletion. Combine it.
1865 if (_PyDict_HasSplitTable(mp)) {
1866 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1867 return NULL;
1868 }
1869 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
1870 assert(ix >= 0);
1871 }
1872
Victor Stinner742da042016-09-07 17:40:12 -07001873 old_value = *value_addr;
Victor Stinner78601a32016-09-09 19:28:36 -07001874 assert(old_value != NULL);
Eric Snow96c6af92015-05-29 22:21:39 -06001875 *value_addr = NULL;
1876 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001877 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner78601a32016-09-09 19:28:36 -07001878 dk_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
1879 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1880 ENSURE_ALLOWS_DELETIONS(mp);
1881 old_key = ep->me_key;
1882 ep->me_key = NULL;
1883 Py_DECREF(old_key);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001884
1885 assert(_PyDict_CheckConsistency(mp));
Eric Snow96c6af92015-05-29 22:21:39 -06001886 return old_value;
1887}
1888
Serhiy Storchaka67796522017-01-12 18:34:33 +02001889PyObject *
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001890_PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt)
Serhiy Storchaka67796522017-01-12 18:34:33 +02001891{
1892 Py_hash_t hash;
1893
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001894 if (((PyDictObject *)dict)->ma_used == 0) {
Serhiy Storchaka67796522017-01-12 18:34:33 +02001895 if (deflt) {
1896 Py_INCREF(deflt);
1897 return deflt;
1898 }
1899 _PyErr_SetKeyError(key);
1900 return NULL;
1901 }
1902 if (!PyUnicode_CheckExact(key) ||
1903 (hash = ((PyASCIIObject *) key)->hash) == -1) {
1904 hash = PyObject_Hash(key);
1905 if (hash == -1)
1906 return NULL;
1907 }
Serhiy Storchaka42e1ea92017-01-12 19:12:21 +02001908 return _PyDict_Pop_KnownHash(dict, key, hash, deflt);
Serhiy Storchaka67796522017-01-12 18:34:33 +02001909}
1910
Eric Snow96c6af92015-05-29 22:21:39 -06001911/* Internal version of dict.from_keys(). It is subclass-friendly. */
1912PyObject *
1913_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1914{
1915 PyObject *it; /* iter(iterable) */
1916 PyObject *key;
1917 PyObject *d;
1918 int status;
1919
1920 d = PyObject_CallObject(cls, NULL);
1921 if (d == NULL)
1922 return NULL;
1923
1924 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
1925 if (PyDict_CheckExact(iterable)) {
1926 PyDictObject *mp = (PyDictObject *)d;
1927 PyObject *oldvalue;
1928 Py_ssize_t pos = 0;
1929 PyObject *key;
1930 Py_hash_t hash;
1931
Victor Stinner742da042016-09-07 17:40:12 -07001932 if (dictresize(mp, ESTIMATE_SIZE(Py_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001933 Py_DECREF(d);
1934 return NULL;
1935 }
1936
1937 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
1938 if (insertdict(mp, key, hash, value)) {
1939 Py_DECREF(d);
1940 return NULL;
1941 }
1942 }
1943 return d;
1944 }
1945 if (PyAnySet_CheckExact(iterable)) {
1946 PyDictObject *mp = (PyDictObject *)d;
1947 Py_ssize_t pos = 0;
1948 PyObject *key;
1949 Py_hash_t hash;
1950
Victor Stinner742da042016-09-07 17:40:12 -07001951 if (dictresize(mp, ESTIMATE_SIZE(PySet_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001952 Py_DECREF(d);
1953 return NULL;
1954 }
1955
1956 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
1957 if (insertdict(mp, key, hash, value)) {
1958 Py_DECREF(d);
1959 return NULL;
1960 }
1961 }
1962 return d;
1963 }
1964 }
1965
1966 it = PyObject_GetIter(iterable);
1967 if (it == NULL){
1968 Py_DECREF(d);
1969 return NULL;
1970 }
1971
1972 if (PyDict_CheckExact(d)) {
1973 while ((key = PyIter_Next(it)) != NULL) {
1974 status = PyDict_SetItem(d, key, value);
1975 Py_DECREF(key);
1976 if (status < 0)
1977 goto Fail;
1978 }
1979 } else {
1980 while ((key = PyIter_Next(it)) != NULL) {
1981 status = PyObject_SetItem(d, key, value);
1982 Py_DECREF(key);
1983 if (status < 0)
1984 goto Fail;
1985 }
1986 }
1987
1988 if (PyErr_Occurred())
1989 goto Fail;
1990 Py_DECREF(it);
1991 return d;
1992
1993Fail:
1994 Py_DECREF(it);
1995 Py_DECREF(d);
1996 return NULL;
1997}
1998
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001999/* Methods */
2000
2001static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002002dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002003{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002004 PyObject **values = mp->ma_values;
2005 PyDictKeysObject *keys = mp->ma_keys;
2006 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002007 PyObject_GC_UnTrack(mp);
2008 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002009 if (values != NULL) {
2010 if (values != empty_values) {
Victor Stinner742da042016-09-07 17:40:12 -07002011 for (i = 0, n = mp->ma_keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002012 Py_XDECREF(values[i]);
2013 }
2014 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002016 DK_DECREF(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002017 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002018 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02002019 assert(keys->dk_refcnt == 1);
2020 DK_DECREF(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002021 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002022 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
2023 free_list[numfree++] = mp;
2024 else
2025 Py_TYPE(mp)->tp_free((PyObject *)mp);
2026 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002027}
2028
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002029
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002030static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002031dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002032{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002033 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01002034 PyObject *key = NULL, *value = NULL;
2035 _PyUnicodeWriter writer;
2036 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00002037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002038 i = Py_ReprEnter((PyObject *)mp);
2039 if (i != 0) {
2040 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
2041 }
Guido van Rossum255443b1998-04-10 22:47:14 +00002042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002043 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01002044 Py_ReprLeave((PyObject *)mp);
2045 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002046 }
Tim Petersa7259592001-06-16 05:11:17 +00002047
Victor Stinnerf91929b2013-11-19 13:07:38 +01002048 _PyUnicodeWriter_Init(&writer);
2049 writer.overallocate = 1;
2050 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
2051 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00002052
Victor Stinnerf91929b2013-11-19 13:07:38 +01002053 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
2054 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00002055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002056 /* Do repr() on each key+value pair, and insert ": " between them.
2057 Note that repr may mutate the dict. */
2058 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01002059 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002060 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01002061 PyObject *s;
2062 int res;
2063
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002064 /* Prevent repr from deleting key or value during key format. */
2065 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002066 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02002067
Victor Stinnerf91929b2013-11-19 13:07:38 +01002068 if (!first) {
2069 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
2070 goto error;
2071 }
2072 first = 0;
2073
2074 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002075 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01002076 goto error;
2077 res = _PyUnicodeWriter_WriteStr(&writer, s);
2078 Py_DECREF(s);
2079 if (res < 0)
2080 goto error;
2081
2082 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
2083 goto error;
2084
2085 s = PyObject_Repr(value);
2086 if (s == NULL)
2087 goto error;
2088 res = _PyUnicodeWriter_WriteStr(&writer, s);
2089 Py_DECREF(s);
2090 if (res < 0)
2091 goto error;
2092
2093 Py_CLEAR(key);
2094 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002095 }
Tim Petersa7259592001-06-16 05:11:17 +00002096
Victor Stinnerf91929b2013-11-19 13:07:38 +01002097 writer.overallocate = 0;
2098 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
2099 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00002100
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002101 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01002102
2103 return _PyUnicodeWriter_Finish(&writer);
2104
2105error:
2106 Py_ReprLeave((PyObject *)mp);
2107 _PyUnicodeWriter_Dealloc(&writer);
2108 Py_XDECREF(key);
2109 Py_XDECREF(value);
2110 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002111}
2112
Martin v. Löwis18e16552006-02-15 17:27:45 +00002113static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002114dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002115{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002116 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002117}
2118
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002119static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002120dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002121{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 PyObject *v;
Victor Stinner742da042016-09-07 17:40:12 -07002123 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002124 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002125 PyObject **value_addr;
2126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002127 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002128 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002129 hash = PyObject_Hash(key);
2130 if (hash == -1)
2131 return NULL;
2132 }
Victor Stinner742da042016-09-07 17:40:12 -07002133 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
2134 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002135 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002136 if (ix == DKIX_EMPTY || *value_addr == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002137 if (!PyDict_CheckExact(mp)) {
2138 /* Look up __missing__ method if we're a subclass. */
2139 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05002140 _Py_IDENTIFIER(__missing__);
2141 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002142 if (missing != NULL) {
2143 res = PyObject_CallFunctionObjArgs(missing,
2144 key, NULL);
2145 Py_DECREF(missing);
2146 return res;
2147 }
2148 else if (PyErr_Occurred())
2149 return NULL;
2150 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07002151 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002152 return NULL;
2153 }
Victor Stinner742da042016-09-07 17:40:12 -07002154 v = *value_addr;
2155 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002156 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002157}
2158
2159static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002160dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002161{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162 if (w == NULL)
2163 return PyDict_DelItem((PyObject *)mp, v);
2164 else
2165 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002166}
2167
Guido van Rossuma9e7a811997-05-13 21:02:11 +00002168static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169 (lenfunc)dict_length, /*mp_length*/
2170 (binaryfunc)dict_subscript, /*mp_subscript*/
2171 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002172};
2173
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002174static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002175dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002176{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002177 PyObject *v;
2178 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002179 PyDictKeyEntry *ep;
2180 Py_ssize_t size, n, offset;
2181 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002182
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002183 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002184 n = mp->ma_used;
2185 v = PyList_New(n);
2186 if (v == NULL)
2187 return NULL;
2188 if (n != mp->ma_used) {
2189 /* Durnit. The allocations caused the dict to resize.
2190 * Just start over, this shouldn't normally happen.
2191 */
2192 Py_DECREF(v);
2193 goto again;
2194 }
Victor Stinner742da042016-09-07 17:40:12 -07002195 ep = DK_ENTRIES(mp->ma_keys);
2196 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002197 if (mp->ma_values) {
2198 value_ptr = mp->ma_values;
2199 offset = sizeof(PyObject *);
2200 }
2201 else {
2202 value_ptr = &ep[0].me_value;
2203 offset = sizeof(PyDictKeyEntry);
2204 }
2205 for (i = 0, j = 0; i < size; i++) {
2206 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002207 PyObject *key = ep[i].me_key;
2208 Py_INCREF(key);
2209 PyList_SET_ITEM(v, j, key);
2210 j++;
2211 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002212 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213 }
2214 assert(j == n);
2215 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002216}
2217
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002218static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002219dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002220{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002221 PyObject *v;
2222 Py_ssize_t i, j;
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002223 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002224 Py_ssize_t size, n, offset;
2225 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002226
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002227 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 n = mp->ma_used;
2229 v = PyList_New(n);
2230 if (v == NULL)
2231 return NULL;
2232 if (n != mp->ma_used) {
2233 /* Durnit. The allocations caused the dict to resize.
2234 * Just start over, this shouldn't normally happen.
2235 */
2236 Py_DECREF(v);
2237 goto again;
2238 }
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002239 ep = DK_ENTRIES(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002240 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002241 if (mp->ma_values) {
2242 value_ptr = mp->ma_values;
2243 offset = sizeof(PyObject *);
2244 }
2245 else {
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002246 value_ptr = &ep[0].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002247 offset = sizeof(PyDictKeyEntry);
2248 }
2249 for (i = 0, j = 0; i < size; i++) {
2250 PyObject *value = *value_ptr;
2251 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2252 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002253 Py_INCREF(value);
2254 PyList_SET_ITEM(v, j, value);
2255 j++;
2256 }
2257 }
2258 assert(j == n);
2259 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002260}
2261
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002262static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002263dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002264{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002265 PyObject *v;
2266 Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002267 Py_ssize_t size, offset;
2268 PyObject *item, *key;
2269 PyDictKeyEntry *ep;
2270 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 /* Preallocate the list of tuples, to avoid allocations during
2273 * the loop over the items, which could trigger GC, which
2274 * could resize the dict. :-(
2275 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002276 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 n = mp->ma_used;
2278 v = PyList_New(n);
2279 if (v == NULL)
2280 return NULL;
2281 for (i = 0; i < n; i++) {
2282 item = PyTuple_New(2);
2283 if (item == NULL) {
2284 Py_DECREF(v);
2285 return NULL;
2286 }
2287 PyList_SET_ITEM(v, i, item);
2288 }
2289 if (n != mp->ma_used) {
2290 /* Durnit. The allocations caused the dict to resize.
2291 * Just start over, this shouldn't normally happen.
2292 */
2293 Py_DECREF(v);
2294 goto again;
2295 }
2296 /* Nothing we do below makes any function calls. */
Victor Stinner742da042016-09-07 17:40:12 -07002297 ep = DK_ENTRIES(mp->ma_keys);
2298 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002299 if (mp->ma_values) {
2300 value_ptr = mp->ma_values;
2301 offset = sizeof(PyObject *);
2302 }
2303 else {
2304 value_ptr = &ep[0].me_value;
2305 offset = sizeof(PyDictKeyEntry);
2306 }
2307 for (i = 0, j = 0; i < size; i++) {
2308 PyObject *value = *value_ptr;
2309 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2310 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 key = ep[i].me_key;
2312 item = PyList_GET_ITEM(v, j);
2313 Py_INCREF(key);
2314 PyTuple_SET_ITEM(item, 0, key);
2315 Py_INCREF(value);
2316 PyTuple_SET_ITEM(item, 1, value);
2317 j++;
2318 }
2319 }
2320 assert(j == n);
2321 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002322}
2323
Larry Hastings5c661892014-01-24 06:17:25 -08002324/*[clinic input]
2325@classmethod
2326dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08002327 iterable: object
2328 value: object=None
2329 /
2330
2331Returns a new dict with keys from iterable and values equal to value.
2332[clinic start generated code]*/
2333
Larry Hastings5c661892014-01-24 06:17:25 -08002334static PyObject *
2335dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002336/*[clinic end generated code: output=8fb98e4b10384999 input=b85a667f9bf4669d]*/
Larry Hastings5c661892014-01-24 06:17:25 -08002337{
Eric Snow96c6af92015-05-29 22:21:39 -06002338 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002339}
2340
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002341static int
Victor Stinner742da042016-09-07 17:40:12 -07002342dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
2343 const char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002344{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002345 PyObject *arg = NULL;
2346 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002348 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
2349 result = -1;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002351 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002352 _Py_IDENTIFIER(keys);
2353 if (_PyObject_HasAttrId(arg, &PyId_keys))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 result = PyDict_Merge(self, arg, 1);
2355 else
2356 result = PyDict_MergeFromSeq2(self, arg, 1);
2357 }
2358 if (result == 0 && kwds != NULL) {
2359 if (PyArg_ValidateKeywordArguments(kwds))
2360 result = PyDict_Merge(self, kwds, 1);
2361 else
2362 result = -1;
2363 }
2364 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002365}
2366
2367static PyObject *
2368dict_update(PyObject *self, PyObject *args, PyObject *kwds)
2369{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002370 if (dict_update_common(self, args, kwds, "update") != -1)
2371 Py_RETURN_NONE;
2372 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002373}
2374
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002375/* Update unconditionally replaces existing items.
2376 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00002377 otherwise it leaves existing items unchanged.
2378
2379 PyDict_{Update,Merge} update/merge from a mapping object.
2380
Tim Petersf582b822001-12-11 18:51:08 +00002381 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00002382 producing iterable objects of length 2.
2383*/
2384
Tim Petersf582b822001-12-11 18:51:08 +00002385int
Tim Peters1fc240e2001-10-26 05:06:50 +00002386PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
2387{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388 PyObject *it; /* iter(seq2) */
2389 Py_ssize_t i; /* index into seq2 of current element */
2390 PyObject *item; /* seq2[i] */
2391 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00002392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 assert(d != NULL);
2394 assert(PyDict_Check(d));
2395 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00002396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002397 it = PyObject_GetIter(seq2);
2398 if (it == NULL)
2399 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 for (i = 0; ; ++i) {
2402 PyObject *key, *value;
2403 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00002404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002405 fast = NULL;
2406 item = PyIter_Next(it);
2407 if (item == NULL) {
2408 if (PyErr_Occurred())
2409 goto Fail;
2410 break;
2411 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002413 /* Convert item to sequence, and verify length 2. */
2414 fast = PySequence_Fast(item, "");
2415 if (fast == NULL) {
2416 if (PyErr_ExceptionMatches(PyExc_TypeError))
2417 PyErr_Format(PyExc_TypeError,
2418 "cannot convert dictionary update "
2419 "sequence element #%zd to a sequence",
2420 i);
2421 goto Fail;
2422 }
2423 n = PySequence_Fast_GET_SIZE(fast);
2424 if (n != 2) {
2425 PyErr_Format(PyExc_ValueError,
2426 "dictionary update sequence element #%zd "
2427 "has length %zd; 2 is required",
2428 i, n);
2429 goto Fail;
2430 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 /* Update/merge with this (key, value) pair. */
2433 key = PySequence_Fast_GET_ITEM(fast, 0);
2434 value = PySequence_Fast_GET_ITEM(fast, 1);
2435 if (override || PyDict_GetItem(d, key) == NULL) {
2436 int status = PyDict_SetItem(d, key, value);
2437 if (status < 0)
2438 goto Fail;
2439 }
2440 Py_DECREF(fast);
2441 Py_DECREF(item);
2442 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002443
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002444 i = 0;
Victor Stinner611b0fa2016-09-14 15:02:01 +02002445 assert(_PyDict_CheckConsistency((PyDictObject *)d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002446 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002447Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002448 Py_XDECREF(item);
2449 Py_XDECREF(fast);
2450 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002451Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002452 Py_DECREF(it);
2453 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002454}
2455
doko@ubuntu.comde69ee72016-10-11 08:04:02 +02002456static int
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002457dict_merge(PyObject *a, PyObject *b, int override)
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002458{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002459 PyDictObject *mp, *other;
2460 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -07002461 PyDictKeyEntry *entry, *ep0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002462
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002463 assert(0 <= override && override <= 2);
2464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002465 /* We accept for the argument either a concrete dictionary object,
2466 * or an abstract "mapping" object. For the former, we can do
2467 * things quite efficiently. For the latter, we only require that
2468 * PyMapping_Keys() and PyObject_GetItem() be supported.
2469 */
2470 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2471 PyErr_BadInternalCall();
2472 return -1;
2473 }
2474 mp = (PyDictObject*)a;
2475 if (PyDict_Check(b)) {
2476 other = (PyDictObject*)b;
2477 if (other == mp || other->ma_used == 0)
2478 /* a.update(a) or a.update({}); nothing to do */
2479 return 0;
2480 if (mp->ma_used == 0)
2481 /* Since the target dict is empty, PyDict_GetItem()
2482 * always returns NULL. Setting override to 1
2483 * skips the unnecessary test.
2484 */
2485 override = 1;
2486 /* Do one big resize at the start, rather than
2487 * incrementally resizing as we insert new items. Expect
2488 * that there will be no (or few) overlapping keys.
2489 */
INADA Naokib1152be2016-10-27 19:26:50 +09002490 if (USABLE_FRACTION(mp->ma_keys->dk_size) < other->ma_used) {
2491 if (dictresize(mp, ESTIMATE_SIZE(mp->ma_used + other->ma_used))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002492 return -1;
INADA Naokib1152be2016-10-27 19:26:50 +09002493 }
2494 }
Victor Stinner742da042016-09-07 17:40:12 -07002495 ep0 = DK_ENTRIES(other->ma_keys);
2496 for (i = 0, n = other->ma_keys->dk_nentries; i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002497 PyObject *key, *value;
2498 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002499 entry = &ep0[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002500 key = entry->me_key;
2501 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002502 if (other->ma_values)
2503 value = other->ma_values[i];
2504 else
2505 value = entry->me_value;
2506
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002507 if (value != NULL) {
2508 int err = 0;
2509 Py_INCREF(key);
2510 Py_INCREF(value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002511 if (override == 1)
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002512 err = insertdict(mp, key, hash, value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002513 else if (_PyDict_GetItem_KnownHash(a, key, hash) == NULL) {
2514 if (PyErr_Occurred()) {
2515 Py_DECREF(value);
2516 Py_DECREF(key);
2517 return -1;
2518 }
2519 err = insertdict(mp, key, hash, value);
2520 }
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002521 else if (override != 0) {
2522 _PyErr_SetKeyError(key);
2523 Py_DECREF(value);
2524 Py_DECREF(key);
2525 return -1;
2526 }
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002527 Py_DECREF(value);
2528 Py_DECREF(key);
2529 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002530 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002531
Victor Stinner742da042016-09-07 17:40:12 -07002532 if (n != other->ma_keys->dk_nentries) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002533 PyErr_SetString(PyExc_RuntimeError,
2534 "dict mutated during update");
2535 return -1;
2536 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002537 }
2538 }
2539 }
2540 else {
2541 /* Do it the generic, slower way */
2542 PyObject *keys = PyMapping_Keys(b);
2543 PyObject *iter;
2544 PyObject *key, *value;
2545 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002547 if (keys == NULL)
2548 /* Docstring says this is equivalent to E.keys() so
2549 * if E doesn't have a .keys() method we want
2550 * AttributeError to percolate up. Might as well
2551 * do the same for any other error.
2552 */
2553 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002555 iter = PyObject_GetIter(keys);
2556 Py_DECREF(keys);
2557 if (iter == NULL)
2558 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002560 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002561 if (override != 1 && PyDict_GetItem(a, key) != NULL) {
2562 if (override != 0) {
2563 _PyErr_SetKeyError(key);
2564 Py_DECREF(key);
2565 Py_DECREF(iter);
2566 return -1;
2567 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002568 Py_DECREF(key);
2569 continue;
2570 }
2571 value = PyObject_GetItem(b, key);
2572 if (value == NULL) {
2573 Py_DECREF(iter);
2574 Py_DECREF(key);
2575 return -1;
2576 }
2577 status = PyDict_SetItem(a, key, value);
2578 Py_DECREF(key);
2579 Py_DECREF(value);
2580 if (status < 0) {
2581 Py_DECREF(iter);
2582 return -1;
2583 }
2584 }
2585 Py_DECREF(iter);
2586 if (PyErr_Occurred())
2587 /* Iterator completed, via error */
2588 return -1;
2589 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02002590 assert(_PyDict_CheckConsistency((PyDictObject *)a));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002591 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002592}
2593
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002594int
2595PyDict_Update(PyObject *a, PyObject *b)
2596{
2597 return dict_merge(a, b, 1);
2598}
2599
2600int
2601PyDict_Merge(PyObject *a, PyObject *b, int override)
2602{
2603 /* XXX Deprecate override not in (0, 1). */
2604 return dict_merge(a, b, override != 0);
2605}
2606
2607int
2608_PyDict_MergeEx(PyObject *a, PyObject *b, int override)
2609{
2610 return dict_merge(a, b, override);
2611}
2612
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002613static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002614dict_copy(PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002615{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002616 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002617}
2618
2619PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002620PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002621{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002622 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002623 PyDictObject *mp;
2624 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002625
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002626 if (o == NULL || !PyDict_Check(o)) {
2627 PyErr_BadInternalCall();
2628 return NULL;
2629 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002630 mp = (PyDictObject *)o;
2631 if (_PyDict_HasSplitTable(mp)) {
2632 PyDictObject *split_copy;
Victor Stinner742da042016-09-07 17:40:12 -07002633 Py_ssize_t size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
2634 PyObject **newvalues;
2635 newvalues = new_values(size);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002636 if (newvalues == NULL)
2637 return PyErr_NoMemory();
2638 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2639 if (split_copy == NULL) {
2640 free_values(newvalues);
2641 return NULL;
2642 }
2643 split_copy->ma_values = newvalues;
2644 split_copy->ma_keys = mp->ma_keys;
2645 split_copy->ma_used = mp->ma_used;
2646 DK_INCREF(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002647 for (i = 0, n = size; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002648 PyObject *value = mp->ma_values[i];
2649 Py_XINCREF(value);
2650 split_copy->ma_values[i] = value;
2651 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002652 if (_PyObject_GC_IS_TRACKED(mp))
2653 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002654 return (PyObject *)split_copy;
2655 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002656 copy = PyDict_New();
2657 if (copy == NULL)
2658 return NULL;
2659 if (PyDict_Merge(copy, o, 1) == 0)
2660 return copy;
2661 Py_DECREF(copy);
2662 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002663}
2664
Martin v. Löwis18e16552006-02-15 17:27:45 +00002665Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002666PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002667{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002668 if (mp == NULL || !PyDict_Check(mp)) {
2669 PyErr_BadInternalCall();
2670 return -1;
2671 }
2672 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002673}
2674
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002675PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002676PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002677{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002678 if (mp == NULL || !PyDict_Check(mp)) {
2679 PyErr_BadInternalCall();
2680 return NULL;
2681 }
2682 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002683}
2684
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002685PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002686PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002687{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002688 if (mp == NULL || !PyDict_Check(mp)) {
2689 PyErr_BadInternalCall();
2690 return NULL;
2691 }
2692 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002693}
2694
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002695PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002696PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002697{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002698 if (mp == NULL || !PyDict_Check(mp)) {
2699 PyErr_BadInternalCall();
2700 return NULL;
2701 }
2702 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002703}
2704
Tim Peterse63415e2001-05-08 04:38:29 +00002705/* Return 1 if dicts equal, 0 if not, -1 if error.
2706 * Gets out as soon as any difference is detected.
2707 * Uses only Py_EQ comparison.
2708 */
2709static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002710dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002711{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002712 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002714 if (a->ma_used != b->ma_used)
2715 /* can't be equal if # of entries differ */
2716 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002717 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Victor Stinner742da042016-09-07 17:40:12 -07002718 for (i = 0; i < a->ma_keys->dk_nentries; i++) {
2719 PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002720 PyObject *aval;
2721 if (a->ma_values)
2722 aval = a->ma_values[i];
2723 else
2724 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002725 if (aval != NULL) {
2726 int cmp;
2727 PyObject *bval;
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002728 PyObject **vaddr;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002729 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002730 /* temporarily bump aval's refcount to ensure it stays
2731 alive until we're done with it */
2732 Py_INCREF(aval);
2733 /* ditto for key */
2734 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002735 /* reuse the known hash value */
Victor Stinner742da042016-09-07 17:40:12 -07002736 if ((b->ma_keys->dk_lookup)(b, key, ep->me_hash, &vaddr, NULL) < 0)
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002737 bval = NULL;
2738 else
2739 bval = *vaddr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002740 Py_DECREF(key);
2741 if (bval == NULL) {
2742 Py_DECREF(aval);
2743 if (PyErr_Occurred())
2744 return -1;
2745 return 0;
2746 }
2747 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
2748 Py_DECREF(aval);
2749 if (cmp <= 0) /* error or not equal */
2750 return cmp;
2751 }
2752 }
2753 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002754}
Tim Peterse63415e2001-05-08 04:38:29 +00002755
2756static PyObject *
2757dict_richcompare(PyObject *v, PyObject *w, int op)
2758{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002759 int cmp;
2760 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002762 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2763 res = Py_NotImplemented;
2764 }
2765 else if (op == Py_EQ || op == Py_NE) {
2766 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2767 if (cmp < 0)
2768 return NULL;
2769 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2770 }
2771 else
2772 res = Py_NotImplemented;
2773 Py_INCREF(res);
2774 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002775}
Tim Peterse63415e2001-05-08 04:38:29 +00002776
Larry Hastings61272b72014-01-07 12:41:53 -08002777/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002778
2779@coexist
2780dict.__contains__
2781
2782 key: object
2783 /
2784
Meador Ingee02de8c2014-01-14 16:48:31 -06002785True if D has a key k, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002786[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002787
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002788static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002789dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002790/*[clinic end generated code: output=a3d03db709ed6e6b input=b852b2a19b51ab24]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002791{
Larry Hastingsc2047262014-01-25 20:43:29 -08002792 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002793 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002794 Py_ssize_t ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002795 PyObject **value_addr;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002797 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002798 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002799 hash = PyObject_Hash(key);
2800 if (hash == -1)
2801 return NULL;
2802 }
Victor Stinner742da042016-09-07 17:40:12 -07002803 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
2804 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002805 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002806 if (ix == DKIX_EMPTY || *value_addr == NULL)
2807 Py_RETURN_FALSE;
2808 Py_RETURN_TRUE;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002809}
2810
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002811static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002812dict_get(PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00002813{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002814 PyObject *key;
2815 PyObject *failobj = Py_None;
2816 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002817 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002818 Py_ssize_t ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002819 PyObject **value_addr;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002821 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
2822 return NULL;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002824 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002825 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002826 hash = PyObject_Hash(key);
2827 if (hash == -1)
2828 return NULL;
2829 }
Victor Stinner742da042016-09-07 17:40:12 -07002830 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
2831 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002833 if (ix == DKIX_EMPTY || *value_addr == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002834 val = failobj;
Victor Stinner742da042016-09-07 17:40:12 -07002835 else
2836 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002837 Py_INCREF(val);
2838 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002839}
2840
Benjamin Peterson00e98862013-03-07 22:16:29 -05002841PyObject *
2842PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002843{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002844 PyDictObject *mp = (PyDictObject *)d;
INADA Naoki93f26f72016-11-02 18:45:16 +09002845 PyObject *value;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002846 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002847 Py_ssize_t hashpos, ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002848 PyObject **value_addr;
Guido van Rossum164452c2000-08-08 16:12:54 +00002849
Benjamin Peterson00e98862013-03-07 22:16:29 -05002850 if (!PyDict_Check(d)) {
2851 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002852 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002853 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002854
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002855 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002856 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002857 hash = PyObject_Hash(key);
2858 if (hash == -1)
2859 return NULL;
2860 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002861
2862 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
2863 if (insertion_resize(mp) < 0)
2864 return NULL;
2865 }
2866
Victor Stinner742da042016-09-07 17:40:12 -07002867 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
2868 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002869 return NULL;
INADA Naoki93f26f72016-11-02 18:45:16 +09002870
2871 if (_PyDict_HasSplitTable(mp) &&
2872 ((ix >= 0 && *value_addr == NULL && mp->ma_used != ix) ||
2873 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
2874 if (insertion_resize(mp) < 0) {
2875 return NULL;
2876 }
2877 find_empty_slot(mp, key, hash, &value_addr, &hashpos);
2878 ix = DKIX_EMPTY;
2879 }
2880
2881 if (ix == DKIX_EMPTY) {
2882 PyDictKeyEntry *ep, *ep0;
2883 value = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002884 if (mp->ma_keys->dk_usable <= 0) {
Victor Stinner3c336c52016-09-12 14:17:40 +02002885 if (insertion_resize(mp) < 0) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002886 return NULL;
Victor Stinner3c336c52016-09-12 14:17:40 +02002887 }
Victor Stinner742da042016-09-07 17:40:12 -07002888 find_empty_slot(mp, key, hash, &value_addr, &hashpos);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002889 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002890 ep0 = DK_ENTRIES(mp->ma_keys);
2891 ep = &ep0[mp->ma_keys->dk_nentries];
2892 dk_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002893 Py_INCREF(key);
INADA Naoki93f26f72016-11-02 18:45:16 +09002894 Py_INCREF(value);
2895 MAINTAIN_TRACKING(mp, key, value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002896 ep->me_key = key;
2897 ep->me_hash = hash;
Victor Stinner742da042016-09-07 17:40:12 -07002898 if (mp->ma_values) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002899 assert(mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
2900 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Victor Stinner742da042016-09-07 17:40:12 -07002901 }
2902 else {
INADA Naoki93f26f72016-11-02 18:45:16 +09002903 ep->me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07002904 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002905 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002906 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naoki93f26f72016-11-02 18:45:16 +09002907 mp->ma_keys->dk_usable--;
2908 mp->ma_keys->dk_nentries++;
2909 assert(mp->ma_keys->dk_usable >= 0);
2910 }
2911 else if (*value_addr == NULL) {
2912 value = defaultobj;
2913 assert(_PyDict_HasSplitTable(mp));
2914 assert(ix == mp->ma_used);
2915 Py_INCREF(value);
2916 MAINTAIN_TRACKING(mp, key, value);
2917 *value_addr = value;
2918 mp->ma_used++;
2919 mp->ma_version_tag = DICT_NEXT_VERSION();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002920 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02002921 else {
INADA Naoki93f26f72016-11-02 18:45:16 +09002922 value = *value_addr;
Victor Stinner611b0fa2016-09-14 15:02:01 +02002923 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002924
2925 assert(_PyDict_CheckConsistency(mp));
2926 return value;
Guido van Rossum164452c2000-08-08 16:12:54 +00002927}
2928
Benjamin Peterson00e98862013-03-07 22:16:29 -05002929static PyObject *
2930dict_setdefault(PyDictObject *mp, PyObject *args)
2931{
2932 PyObject *key, *val;
2933 PyObject *defaultobj = Py_None;
2934
2935 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &defaultobj))
2936 return NULL;
2937
Benjamin Peterson55898502013-03-08 08:36:49 -05002938 val = PyDict_SetDefault((PyObject *)mp, key, defaultobj);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002939 Py_XINCREF(val);
2940 return val;
2941}
Guido van Rossum164452c2000-08-08 16:12:54 +00002942
2943static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002944dict_clear(PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002945{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 PyDict_Clear((PyObject *)mp);
2947 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002948}
2949
Guido van Rossumba6ab842000-12-12 22:02:18 +00002950static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002951dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002952{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002953 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00002954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002955 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2956 return NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06002957
Yury Selivanov684ef2c2016-10-28 19:01:21 -04002958 return _PyDict_Pop((PyObject*)mp, key, deflt);
Guido van Rossume027d982002-04-12 15:11:59 +00002959}
2960
2961static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002962dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002963{
Victor Stinner742da042016-09-07 17:40:12 -07002964 Py_ssize_t i, j;
2965 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002966 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002968 /* Allocate the result tuple before checking the size. Believe it
2969 * or not, this allocation could trigger a garbage collection which
2970 * could empty the dict, so if we checked the size first and that
2971 * happened, the result would be an infinite loop (searching for an
2972 * entry that no longer exists). Note that the usual popitem()
2973 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2974 * tuple away if the dict *is* empty isn't a significant
2975 * inefficiency -- possible, but unlikely in practice.
2976 */
2977 res = PyTuple_New(2);
2978 if (res == NULL)
2979 return NULL;
2980 if (mp->ma_used == 0) {
2981 Py_DECREF(res);
2982 PyErr_SetString(PyExc_KeyError,
2983 "popitem(): dictionary is empty");
2984 return NULL;
2985 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002986 /* Convert split table to combined table */
2987 if (mp->ma_keys->dk_lookup == lookdict_split) {
2988 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2989 Py_DECREF(res);
2990 return NULL;
2991 }
2992 }
2993 ENSURE_ALLOWS_DELETIONS(mp);
Victor Stinner742da042016-09-07 17:40:12 -07002994
2995 /* Pop last item */
2996 ep0 = DK_ENTRIES(mp->ma_keys);
2997 i = mp->ma_keys->dk_nentries - 1;
2998 while (i >= 0 && ep0[i].me_value == NULL) {
2999 i--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003000 }
Victor Stinner742da042016-09-07 17:40:12 -07003001 assert(i >= 0);
3002
3003 ep = &ep0[i];
3004 j = lookdict_index(mp->ma_keys, ep->me_hash, i);
3005 assert(j >= 0);
3006 assert(dk_get_index(mp->ma_keys, j) == i);
3007 dk_set_index(mp->ma_keys, j, DKIX_DUMMY);
3008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003009 PyTuple_SET_ITEM(res, 0, ep->me_key);
3010 PyTuple_SET_ITEM(res, 1, ep->me_value);
Victor Stinner742da042016-09-07 17:40:12 -07003011 ep->me_key = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003012 ep->me_value = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07003013 /* We can't dk_usable++ since there is DKIX_DUMMY in indices */
3014 mp->ma_keys->dk_nentries = i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003015 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003016 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02003017 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003018 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00003019}
3020
Jeremy Hylton8caad492000-06-23 14:18:11 +00003021static int
3022dict_traverse(PyObject *op, visitproc visit, void *arg)
3023{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003024 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson55f44522016-09-05 12:12:59 -07003025 PyDictKeysObject *keys = mp->ma_keys;
Serhiy Storchaka46825d22016-09-26 21:29:34 +03003026 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Victor Stinner742da042016-09-07 17:40:12 -07003027 Py_ssize_t i, n = keys->dk_nentries;
3028
Benjamin Peterson55f44522016-09-05 12:12:59 -07003029 if (keys->dk_lookup == lookdict) {
3030 for (i = 0; i < n; i++) {
3031 if (entries[i].me_value != NULL) {
3032 Py_VISIT(entries[i].me_value);
3033 Py_VISIT(entries[i].me_key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003034 }
3035 }
Victor Stinner742da042016-09-07 17:40:12 -07003036 }
3037 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003038 if (mp->ma_values != NULL) {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003039 for (i = 0; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003040 Py_VISIT(mp->ma_values[i]);
3041 }
3042 }
3043 else {
Benjamin Peterson55f44522016-09-05 12:12:59 -07003044 for (i = 0; i < n; i++) {
3045 Py_VISIT(entries[i].me_value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003046 }
3047 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003048 }
3049 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003050}
3051
3052static int
3053dict_tp_clear(PyObject *op)
3054{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003055 PyDict_Clear(op);
3056 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00003057}
3058
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003059static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003060
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003061Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003062_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003063{
Victor Stinner742da042016-09-07 17:40:12 -07003064 Py_ssize_t size, usable, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003065
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003066 size = DK_SIZE(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07003067 usable = USABLE_FRACTION(size);
3068
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02003069 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003070 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -07003071 res += usable * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003072 /* If the dictionary is split, the keys portion is accounted-for
3073 in the type object. */
3074 if (mp->ma_keys->dk_refcnt == 1)
Victor Stinner98ee9d52016-09-08 09:33:56 -07003075 res += (sizeof(PyDictKeysObject)
3076 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
3077 + DK_IXSIZE(mp->ma_keys) * size
3078 + sizeof(PyDictKeyEntry) * usable);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003079 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02003080}
3081
3082Py_ssize_t
3083_PyDict_KeysSize(PyDictKeysObject *keys)
3084{
Victor Stinner98ee9d52016-09-08 09:33:56 -07003085 return (sizeof(PyDictKeysObject)
3086 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
3087 + DK_IXSIZE(keys) * DK_SIZE(keys)
3088 + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003089}
3090
doko@ubuntu.com17210f52016-01-14 14:04:59 +01003091static PyObject *
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003092dict_sizeof(PyDictObject *mp)
3093{
3094 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
3095}
3096
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00003097PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
3098
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003099PyDoc_STRVAR(sizeof__doc__,
3100"D.__sizeof__() -> size of D in memory, in bytes");
3101
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003102PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00003103"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003104
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003105PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00003106"D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D");
Tim Petersf7f88b12000-12-13 23:18:45 +00003107
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003108PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00003109"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00003110If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00003111
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003112PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00003113"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000031142-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003115
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003116PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04003117"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
3118If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
3119If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
3120In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00003121
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003122PyDoc_STRVAR(clear__doc__,
3123"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003124
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003125PyDoc_STRVAR(copy__doc__,
3126"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00003127
Guido van Rossumb90c8482007-02-10 01:11:45 +00003128/* Forward */
3129static PyObject *dictkeys_new(PyObject *);
3130static PyObject *dictitems_new(PyObject *);
3131static PyObject *dictvalues_new(PyObject *);
3132
Guido van Rossum45c85d12007-07-27 16:31:40 +00003133PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003134 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003135PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003136 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003137PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003138 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00003139
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003140static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07003141 DICT___CONTAINS___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003142 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
3143 getitem__doc__},
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003144 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003145 sizeof__doc__},
3146 {"get", (PyCFunction)dict_get, METH_VARARGS,
3147 get__doc__},
3148 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
3149 setdefault_doc__},
3150 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
3151 pop__doc__},
3152 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
3153 popitem__doc__},
3154 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
3155 keys__doc__},
3156 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
3157 items__doc__},
3158 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
3159 values__doc__},
3160 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
3161 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08003162 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003163 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
3164 clear__doc__},
3165 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
3166 copy__doc__},
3167 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003168};
3169
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003170/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00003171int
3172PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003173{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003174 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07003175 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003176 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003177 PyObject **value_addr;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003179 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003180 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003181 hash = PyObject_Hash(key);
3182 if (hash == -1)
3183 return -1;
3184 }
Victor Stinner742da042016-09-07 17:40:12 -07003185 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
3186 if (ix == DKIX_ERROR)
3187 return -1;
3188 return (ix != DKIX_EMPTY && *value_addr != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003189}
3190
Thomas Wouterscf297e42007-02-23 15:07:44 +00003191/* Internal version of PyDict_Contains used when the hash value is already known */
3192int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003193_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00003194{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003195 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003196 PyObject **value_addr;
Victor Stinner742da042016-09-07 17:40:12 -07003197 Py_ssize_t ix;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003198
Victor Stinner742da042016-09-07 17:40:12 -07003199 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
3200 if (ix == DKIX_ERROR)
3201 return -1;
3202 return (ix != DKIX_EMPTY && *value_addr != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00003203}
3204
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003205/* Hack to implement "key in dict" */
3206static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003207 0, /* sq_length */
3208 0, /* sq_concat */
3209 0, /* sq_repeat */
3210 0, /* sq_item */
3211 0, /* sq_slice */
3212 0, /* sq_ass_item */
3213 0, /* sq_ass_slice */
3214 PyDict_Contains, /* sq_contains */
3215 0, /* sq_inplace_concat */
3216 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003217};
3218
Guido van Rossum09e563a2001-05-01 12:10:21 +00003219static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003220dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3221{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003222 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003223 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003225 assert(type != NULL && type->tp_alloc != NULL);
3226 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02003227 if (self == NULL)
3228 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003229 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003230
Victor Stinnera9f61a52013-07-16 22:17:26 +02003231 /* The object has been implicitly tracked by tp_alloc */
3232 if (type == &PyDict_Type)
3233 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003234
3235 d->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003236 d->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07003237 d->ma_keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003238 if (d->ma_keys == NULL) {
3239 Py_DECREF(self);
3240 return NULL;
3241 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02003242 assert(_PyDict_CheckConsistency(d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003243 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003244}
3245
Tim Peters25786c02001-09-02 08:22:48 +00003246static int
3247dict_init(PyObject *self, PyObject *args, PyObject *kwds)
3248{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003249 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00003250}
3251
Tim Peters6d6c1a32001-08-02 04:15:00 +00003252static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003253dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00003254{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003255 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003256}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003257
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003258PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00003259"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00003260"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003261" (key, value) pairs\n"
3262"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00003263" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003264" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00003265" d[k] = v\n"
3266"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
3267" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00003268
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003269PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003270 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3271 "dict",
3272 sizeof(PyDictObject),
3273 0,
3274 (destructor)dict_dealloc, /* tp_dealloc */
3275 0, /* tp_print */
3276 0, /* tp_getattr */
3277 0, /* tp_setattr */
3278 0, /* tp_reserved */
3279 (reprfunc)dict_repr, /* tp_repr */
3280 0, /* tp_as_number */
3281 &dict_as_sequence, /* tp_as_sequence */
3282 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003283 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003284 0, /* tp_call */
3285 0, /* tp_str */
3286 PyObject_GenericGetAttr, /* tp_getattro */
3287 0, /* tp_setattro */
3288 0, /* tp_as_buffer */
3289 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3290 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
3291 dictionary_doc, /* tp_doc */
3292 dict_traverse, /* tp_traverse */
3293 dict_tp_clear, /* tp_clear */
3294 dict_richcompare, /* tp_richcompare */
3295 0, /* tp_weaklistoffset */
3296 (getiterfunc)dict_iter, /* tp_iter */
3297 0, /* tp_iternext */
3298 mapp_methods, /* tp_methods */
3299 0, /* tp_members */
3300 0, /* tp_getset */
3301 0, /* tp_base */
3302 0, /* tp_dict */
3303 0, /* tp_descr_get */
3304 0, /* tp_descr_set */
3305 0, /* tp_dictoffset */
3306 dict_init, /* tp_init */
3307 PyType_GenericAlloc, /* tp_alloc */
3308 dict_new, /* tp_new */
3309 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003310};
3311
Victor Stinner3c1e4812012-03-26 22:10:51 +02003312PyObject *
3313_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
3314{
3315 PyObject *kv;
3316 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02003317 if (kv == NULL) {
3318 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003319 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02003320 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02003321 return PyDict_GetItem(dp, kv);
3322}
3323
Guido van Rossum3cca2451997-05-16 14:23:33 +00003324/* For backward compatibility with old dictionary interface */
3325
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003326PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003327PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003328{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003329 PyObject *kv, *rv;
3330 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003331 if (kv == NULL) {
3332 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003333 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003334 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003335 rv = PyDict_GetItem(v, kv);
3336 Py_DECREF(kv);
3337 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003338}
3339
3340int
Victor Stinner3c1e4812012-03-26 22:10:51 +02003341_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
3342{
3343 PyObject *kv;
3344 kv = _PyUnicode_FromId(key); /* borrowed */
3345 if (kv == NULL)
3346 return -1;
3347 return PyDict_SetItem(v, kv, item);
3348}
3349
3350int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003351PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003352{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003353 PyObject *kv;
3354 int err;
3355 kv = PyUnicode_FromString(key);
3356 if (kv == NULL)
3357 return -1;
3358 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
3359 err = PyDict_SetItem(v, kv, item);
3360 Py_DECREF(kv);
3361 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003362}
3363
3364int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01003365_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
3366{
3367 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3368 if (kv == NULL)
3369 return -1;
3370 return PyDict_DelItem(v, kv);
3371}
3372
3373int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003374PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003375{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003376 PyObject *kv;
3377 int err;
3378 kv = PyUnicode_FromString(key);
3379 if (kv == NULL)
3380 return -1;
3381 err = PyDict_DelItem(v, kv);
3382 Py_DECREF(kv);
3383 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003384}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003385
Raymond Hettinger019a1482004-03-18 02:41:19 +00003386/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003387
3388typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003389 PyObject_HEAD
3390 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
3391 Py_ssize_t di_used;
3392 Py_ssize_t di_pos;
3393 PyObject* di_result; /* reusable result tuple for iteritems */
3394 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003395} dictiterobject;
3396
3397static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003398dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003399{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003400 dictiterobject *di;
3401 di = PyObject_GC_New(dictiterobject, itertype);
3402 if (di == NULL)
3403 return NULL;
3404 Py_INCREF(dict);
3405 di->di_dict = dict;
3406 di->di_used = dict->ma_used;
3407 di->di_pos = 0;
3408 di->len = dict->ma_used;
3409 if (itertype == &PyDictIterItem_Type) {
3410 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
3411 if (di->di_result == NULL) {
3412 Py_DECREF(di);
3413 return NULL;
3414 }
3415 }
3416 else
3417 di->di_result = NULL;
3418 _PyObject_GC_TRACK(di);
3419 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003420}
3421
3422static void
3423dictiter_dealloc(dictiterobject *di)
3424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003425 Py_XDECREF(di->di_dict);
3426 Py_XDECREF(di->di_result);
3427 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003428}
3429
3430static int
3431dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
3432{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003433 Py_VISIT(di->di_dict);
3434 Py_VISIT(di->di_result);
3435 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003436}
3437
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003438static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003439dictiter_len(dictiterobject *di)
3440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003441 Py_ssize_t len = 0;
3442 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
3443 len = di->len;
3444 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003445}
3446
Guido van Rossumb90c8482007-02-10 01:11:45 +00003447PyDoc_STRVAR(length_hint_doc,
3448 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003449
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003450static PyObject *
3451dictiter_reduce(dictiterobject *di);
3452
3453PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3454
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003455static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003456 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
3457 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003458 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
3459 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003460 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003461};
3462
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003463static PyObject*
3464dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00003465{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003466 PyObject *key;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003467 Py_ssize_t i, n;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003468 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003469 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00003470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003471 if (d == NULL)
3472 return NULL;
3473 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00003474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003475 if (di->di_used != d->ma_used) {
3476 PyErr_SetString(PyExc_RuntimeError,
3477 "dictionary changed size during iteration");
3478 di->di_used = -1; /* Make this state sticky */
3479 return NULL;
3480 }
Guido van Rossum2147df72002-07-16 20:30:22 +00003481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003482 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003483 k = d->ma_keys;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003484 n = k->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003485 if (d->ma_values) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003486 PyObject **value_ptr = &d->ma_values[i];
3487 while (i < n && *value_ptr == NULL) {
3488 value_ptr++;
3489 i++;
3490 }
3491 if (i >= n)
3492 goto fail;
3493 key = DK_ENTRIES(k)[i].me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003494 }
3495 else {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003496 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3497 while (i < n && entry_ptr->me_value == NULL) {
3498 entry_ptr++;
3499 i++;
3500 }
3501 if (i >= n)
3502 goto fail;
3503 key = entry_ptr->me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003504 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003505 di->di_pos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003506 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003507 Py_INCREF(key);
3508 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003509
3510fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003511 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003512 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003513 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003514}
3515
Raymond Hettinger019a1482004-03-18 02:41:19 +00003516PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003517 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3518 "dict_keyiterator", /* tp_name */
3519 sizeof(dictiterobject), /* tp_basicsize */
3520 0, /* tp_itemsize */
3521 /* methods */
3522 (destructor)dictiter_dealloc, /* tp_dealloc */
3523 0, /* tp_print */
3524 0, /* tp_getattr */
3525 0, /* tp_setattr */
3526 0, /* tp_reserved */
3527 0, /* tp_repr */
3528 0, /* tp_as_number */
3529 0, /* tp_as_sequence */
3530 0, /* tp_as_mapping */
3531 0, /* tp_hash */
3532 0, /* tp_call */
3533 0, /* tp_str */
3534 PyObject_GenericGetAttr, /* tp_getattro */
3535 0, /* tp_setattro */
3536 0, /* tp_as_buffer */
3537 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3538 0, /* tp_doc */
3539 (traverseproc)dictiter_traverse, /* tp_traverse */
3540 0, /* tp_clear */
3541 0, /* tp_richcompare */
3542 0, /* tp_weaklistoffset */
3543 PyObject_SelfIter, /* tp_iter */
3544 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3545 dictiter_methods, /* tp_methods */
3546 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003547};
3548
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003549static PyObject *
3550dictiter_iternextvalue(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003551{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003552 PyObject *value;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003553 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003554 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003556 if (d == NULL)
3557 return NULL;
3558 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003560 if (di->di_used != d->ma_used) {
3561 PyErr_SetString(PyExc_RuntimeError,
3562 "dictionary changed size during iteration");
3563 di->di_used = -1; /* Make this state sticky */
3564 return NULL;
3565 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003567 i = di->di_pos;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003568 n = d->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003569 if (d->ma_values) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003570 PyObject **value_ptr = &d->ma_values[i];
3571 while (i < n && *value_ptr == NULL) {
3572 value_ptr++;
3573 i++;
3574 }
3575 if (i >= n)
3576 goto fail;
3577 value = *value_ptr;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003578 }
3579 else {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003580 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3581 while (i < n && entry_ptr->me_value == NULL) {
3582 entry_ptr++;
3583 i++;
3584 }
3585 if (i >= n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003586 goto fail;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003587 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003588 }
3589 di->di_pos = i+1;
3590 di->len--;
3591 Py_INCREF(value);
3592 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003593
3594fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003595 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003596 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003597 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003598}
3599
3600PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003601 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3602 "dict_valueiterator", /* tp_name */
3603 sizeof(dictiterobject), /* tp_basicsize */
3604 0, /* tp_itemsize */
3605 /* methods */
3606 (destructor)dictiter_dealloc, /* tp_dealloc */
3607 0, /* tp_print */
3608 0, /* tp_getattr */
3609 0, /* tp_setattr */
3610 0, /* tp_reserved */
3611 0, /* tp_repr */
3612 0, /* tp_as_number */
3613 0, /* tp_as_sequence */
3614 0, /* tp_as_mapping */
3615 0, /* tp_hash */
3616 0, /* tp_call */
3617 0, /* tp_str */
3618 PyObject_GenericGetAttr, /* tp_getattro */
3619 0, /* tp_setattro */
3620 0, /* tp_as_buffer */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003621 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003622 0, /* tp_doc */
3623 (traverseproc)dictiter_traverse, /* tp_traverse */
3624 0, /* tp_clear */
3625 0, /* tp_richcompare */
3626 0, /* tp_weaklistoffset */
3627 PyObject_SelfIter, /* tp_iter */
3628 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3629 dictiter_methods, /* tp_methods */
3630 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003631};
3632
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003633static PyObject *
3634dictiter_iternextitem(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003635{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003636 PyObject *key, *value, *result = di->di_result;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003637 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003638 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003640 if (d == NULL)
3641 return NULL;
3642 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003644 if (di->di_used != d->ma_used) {
3645 PyErr_SetString(PyExc_RuntimeError,
3646 "dictionary changed size during iteration");
3647 di->di_used = -1; /* Make this state sticky */
3648 return NULL;
3649 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003651 i = di->di_pos;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003652 n = d->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003653 if (d->ma_values) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003654 PyObject **value_ptr = &d->ma_values[i];
3655 while (i < n && *value_ptr == NULL) {
3656 value_ptr++;
3657 i++;
3658 }
3659 if (i >= n)
3660 goto fail;
3661 key = DK_ENTRIES(d->ma_keys)[i].me_key;
3662 value = *value_ptr;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003663 }
3664 else {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003665 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3666 while (i < n && entry_ptr->me_value == NULL) {
3667 entry_ptr++;
3668 i++;
3669 }
3670 if (i >= n)
3671 goto fail;
3672 key = entry_ptr->me_key;
3673 value = entry_ptr->me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003674 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003675 di->di_pos = i+1;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003676 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003677 if (result->ob_refcnt == 1) {
3678 Py_INCREF(result);
3679 Py_DECREF(PyTuple_GET_ITEM(result, 0));
3680 Py_DECREF(PyTuple_GET_ITEM(result, 1));
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003681 }
3682 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003683 result = PyTuple_New(2);
3684 if (result == NULL)
3685 return NULL;
3686 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003687 Py_INCREF(key);
3688 Py_INCREF(value);
Eric Snow96c6af92015-05-29 22:21:39 -06003689 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3690 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003691 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003692
3693fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003694 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003695 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003696 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003697}
3698
3699PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003700 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3701 "dict_itemiterator", /* tp_name */
3702 sizeof(dictiterobject), /* tp_basicsize */
3703 0, /* tp_itemsize */
3704 /* methods */
3705 (destructor)dictiter_dealloc, /* tp_dealloc */
3706 0, /* tp_print */
3707 0, /* tp_getattr */
3708 0, /* tp_setattr */
3709 0, /* tp_reserved */
3710 0, /* tp_repr */
3711 0, /* tp_as_number */
3712 0, /* tp_as_sequence */
3713 0, /* tp_as_mapping */
3714 0, /* tp_hash */
3715 0, /* tp_call */
3716 0, /* tp_str */
3717 PyObject_GenericGetAttr, /* tp_getattro */
3718 0, /* tp_setattro */
3719 0, /* tp_as_buffer */
3720 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3721 0, /* tp_doc */
3722 (traverseproc)dictiter_traverse, /* tp_traverse */
3723 0, /* tp_clear */
3724 0, /* tp_richcompare */
3725 0, /* tp_weaklistoffset */
3726 PyObject_SelfIter, /* tp_iter */
3727 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3728 dictiter_methods, /* tp_methods */
3729 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003730};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003731
3732
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003733static PyObject *
3734dictiter_reduce(dictiterobject *di)
3735{
3736 PyObject *list;
3737 dictiterobject tmp;
3738
3739 list = PyList_New(0);
3740 if (!list)
3741 return NULL;
3742
3743 /* copy the itertor state */
3744 tmp = *di;
3745 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003746
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003747 /* iterate the temporary into a list */
3748 for(;;) {
3749 PyObject *element = 0;
3750 if (Py_TYPE(di) == &PyDictIterItem_Type)
3751 element = dictiter_iternextitem(&tmp);
3752 else if (Py_TYPE(di) == &PyDictIterKey_Type)
3753 element = dictiter_iternextkey(&tmp);
3754 else if (Py_TYPE(di) == &PyDictIterValue_Type)
3755 element = dictiter_iternextvalue(&tmp);
3756 else
3757 assert(0);
3758 if (element) {
3759 if (PyList_Append(list, element)) {
3760 Py_DECREF(element);
3761 Py_DECREF(list);
3762 Py_XDECREF(tmp.di_dict);
3763 return NULL;
3764 }
3765 Py_DECREF(element);
3766 } else
3767 break;
3768 }
3769 Py_XDECREF(tmp.di_dict);
3770 /* check for error */
3771 if (tmp.di_dict != NULL) {
3772 /* we have an error */
3773 Py_DECREF(list);
3774 return NULL;
3775 }
Antoine Pitroua7013882012-04-05 00:04:20 +02003776 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003777}
3778
Guido van Rossum3ac67412007-02-10 18:55:06 +00003779/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003780/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003781/***********************************************/
3782
Guido van Rossumb90c8482007-02-10 01:11:45 +00003783/* The instance lay-out is the same for all three; but the type differs. */
3784
Guido van Rossumb90c8482007-02-10 01:11:45 +00003785static void
Eric Snow96c6af92015-05-29 22:21:39 -06003786dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003787{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003788 Py_XDECREF(dv->dv_dict);
3789 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003790}
3791
3792static int
Eric Snow96c6af92015-05-29 22:21:39 -06003793dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003794{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003795 Py_VISIT(dv->dv_dict);
3796 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003797}
3798
Guido van Rossum83825ac2007-02-10 04:54:19 +00003799static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003800dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003801{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003802 Py_ssize_t len = 0;
3803 if (dv->dv_dict != NULL)
3804 len = dv->dv_dict->ma_used;
3805 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003806}
3807
Eric Snow96c6af92015-05-29 22:21:39 -06003808PyObject *
3809_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003810{
Eric Snow96c6af92015-05-29 22:21:39 -06003811 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003812 if (dict == NULL) {
3813 PyErr_BadInternalCall();
3814 return NULL;
3815 }
3816 if (!PyDict_Check(dict)) {
3817 /* XXX Get rid of this restriction later */
3818 PyErr_Format(PyExc_TypeError,
3819 "%s() requires a dict argument, not '%s'",
3820 type->tp_name, dict->ob_type->tp_name);
3821 return NULL;
3822 }
Eric Snow96c6af92015-05-29 22:21:39 -06003823 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003824 if (dv == NULL)
3825 return NULL;
3826 Py_INCREF(dict);
3827 dv->dv_dict = (PyDictObject *)dict;
3828 _PyObject_GC_TRACK(dv);
3829 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003830}
3831
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003832/* TODO(guido): The views objects are not complete:
3833
3834 * support more set operations
3835 * support arbitrary mappings?
3836 - either these should be static or exported in dictobject.h
3837 - if public then they should probably be in builtins
3838*/
3839
Guido van Rossumaac530c2007-08-24 22:33:45 +00003840/* Return 1 if self is a subset of other, iterating over self;
3841 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003842static int
3843all_contained_in(PyObject *self, PyObject *other)
3844{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003845 PyObject *iter = PyObject_GetIter(self);
3846 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003848 if (iter == NULL)
3849 return -1;
3850 for (;;) {
3851 PyObject *next = PyIter_Next(iter);
3852 if (next == NULL) {
3853 if (PyErr_Occurred())
3854 ok = -1;
3855 break;
3856 }
3857 ok = PySequence_Contains(other, next);
3858 Py_DECREF(next);
3859 if (ok <= 0)
3860 break;
3861 }
3862 Py_DECREF(iter);
3863 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003864}
3865
3866static PyObject *
3867dictview_richcompare(PyObject *self, PyObject *other, int op)
3868{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003869 Py_ssize_t len_self, len_other;
3870 int ok;
3871 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003872
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003873 assert(self != NULL);
3874 assert(PyDictViewSet_Check(self));
3875 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003876
Brian Curtindfc80e32011-08-10 20:28:54 -05003877 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3878 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003880 len_self = PyObject_Size(self);
3881 if (len_self < 0)
3882 return NULL;
3883 len_other = PyObject_Size(other);
3884 if (len_other < 0)
3885 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003887 ok = 0;
3888 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003890 case Py_NE:
3891 case Py_EQ:
3892 if (len_self == len_other)
3893 ok = all_contained_in(self, other);
3894 if (op == Py_NE && ok >= 0)
3895 ok = !ok;
3896 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003898 case Py_LT:
3899 if (len_self < len_other)
3900 ok = all_contained_in(self, other);
3901 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003903 case Py_LE:
3904 if (len_self <= len_other)
3905 ok = all_contained_in(self, other);
3906 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003908 case Py_GT:
3909 if (len_self > len_other)
3910 ok = all_contained_in(other, self);
3911 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003913 case Py_GE:
3914 if (len_self >= len_other)
3915 ok = all_contained_in(other, self);
3916 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003918 }
3919 if (ok < 0)
3920 return NULL;
3921 result = ok ? Py_True : Py_False;
3922 Py_INCREF(result);
3923 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003924}
3925
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003926static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003927dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003928{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003929 PyObject *seq;
3930 PyObject *result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003932 seq = PySequence_List((PyObject *)dv);
3933 if (seq == NULL)
3934 return NULL;
3935
3936 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
3937 Py_DECREF(seq);
3938 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003939}
3940
Guido van Rossum3ac67412007-02-10 18:55:06 +00003941/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003942
3943static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003944dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003945{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003946 if (dv->dv_dict == NULL) {
3947 Py_RETURN_NONE;
3948 }
3949 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003950}
3951
3952static int
Eric Snow96c6af92015-05-29 22:21:39 -06003953dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00003954{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003955 if (dv->dv_dict == NULL)
3956 return 0;
3957 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003958}
3959
Guido van Rossum83825ac2007-02-10 04:54:19 +00003960static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003961 (lenfunc)dictview_len, /* sq_length */
3962 0, /* sq_concat */
3963 0, /* sq_repeat */
3964 0, /* sq_item */
3965 0, /* sq_slice */
3966 0, /* sq_ass_item */
3967 0, /* sq_ass_slice */
3968 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003969};
3970
Guido van Rossum523259b2007-08-24 23:41:22 +00003971static PyObject*
3972dictviews_sub(PyObject* self, PyObject *other)
3973{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003974 PyObject *result = PySet_New(self);
3975 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003976 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003978 if (result == NULL)
3979 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003980
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003981 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003982 if (tmp == NULL) {
3983 Py_DECREF(result);
3984 return NULL;
3985 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003987 Py_DECREF(tmp);
3988 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003989}
3990
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003991PyObject*
3992_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00003993{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003994 PyObject *result = PySet_New(self);
3995 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003996 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003998 if (result == NULL)
3999 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004000
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08004001 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_intersection_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004002 if (tmp == NULL) {
4003 Py_DECREF(result);
4004 return NULL;
4005 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004007 Py_DECREF(tmp);
4008 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004009}
4010
4011static PyObject*
4012dictviews_or(PyObject* self, PyObject *other)
4013{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004014 PyObject *result = PySet_New(self);
4015 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004016 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02004017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004018 if (result == NULL)
4019 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004020
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08004021 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004022 if (tmp == NULL) {
4023 Py_DECREF(result);
4024 return NULL;
4025 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004027 Py_DECREF(tmp);
4028 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004029}
4030
4031static PyObject*
4032dictviews_xor(PyObject* self, PyObject *other)
4033{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004034 PyObject *result = PySet_New(self);
4035 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004036 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004038 if (result == NULL)
4039 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00004040
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08004041 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_symmetric_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004042 if (tmp == NULL) {
4043 Py_DECREF(result);
4044 return NULL;
4045 }
Guido van Rossum523259b2007-08-24 23:41:22 +00004046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004047 Py_DECREF(tmp);
4048 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00004049}
4050
4051static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004052 0, /*nb_add*/
4053 (binaryfunc)dictviews_sub, /*nb_subtract*/
4054 0, /*nb_multiply*/
4055 0, /*nb_remainder*/
4056 0, /*nb_divmod*/
4057 0, /*nb_power*/
4058 0, /*nb_negative*/
4059 0, /*nb_positive*/
4060 0, /*nb_absolute*/
4061 0, /*nb_bool*/
4062 0, /*nb_invert*/
4063 0, /*nb_lshift*/
4064 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004065 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004066 (binaryfunc)dictviews_xor, /*nb_xor*/
4067 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00004068};
4069
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004070static PyObject*
4071dictviews_isdisjoint(PyObject *self, PyObject *other)
4072{
4073 PyObject *it;
4074 PyObject *item = NULL;
4075
4076 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06004077 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004078 Py_RETURN_TRUE;
4079 else
4080 Py_RETURN_FALSE;
4081 }
4082
4083 /* Iterate over the shorter object (only if other is a set,
4084 * because PySequence_Contains may be expensive otherwise): */
4085 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06004086 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004087 Py_ssize_t len_other = PyObject_Size(other);
4088 if (len_other == -1)
4089 return NULL;
4090
4091 if ((len_other > len_self)) {
4092 PyObject *tmp = other;
4093 other = self;
4094 self = tmp;
4095 }
4096 }
4097
4098 it = PyObject_GetIter(other);
4099 if (it == NULL)
4100 return NULL;
4101
4102 while ((item = PyIter_Next(it)) != NULL) {
4103 int contains = PySequence_Contains(self, item);
4104 Py_DECREF(item);
4105 if (contains == -1) {
4106 Py_DECREF(it);
4107 return NULL;
4108 }
4109
4110 if (contains) {
4111 Py_DECREF(it);
4112 Py_RETURN_FALSE;
4113 }
4114 }
4115 Py_DECREF(it);
4116 if (PyErr_Occurred())
4117 return NULL; /* PyIter_Next raised an exception. */
4118 Py_RETURN_TRUE;
4119}
4120
4121PyDoc_STRVAR(isdisjoint_doc,
4122"Return True if the view and the given iterable have a null intersection.");
4123
Guido van Rossumb90c8482007-02-10 01:11:45 +00004124static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004125 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4126 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004127 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004128};
4129
4130PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004131 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4132 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004133 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004134 0, /* tp_itemsize */
4135 /* methods */
4136 (destructor)dictview_dealloc, /* tp_dealloc */
4137 0, /* tp_print */
4138 0, /* tp_getattr */
4139 0, /* tp_setattr */
4140 0, /* tp_reserved */
4141 (reprfunc)dictview_repr, /* tp_repr */
4142 &dictviews_as_number, /* tp_as_number */
4143 &dictkeys_as_sequence, /* tp_as_sequence */
4144 0, /* tp_as_mapping */
4145 0, /* tp_hash */
4146 0, /* tp_call */
4147 0, /* tp_str */
4148 PyObject_GenericGetAttr, /* tp_getattro */
4149 0, /* tp_setattro */
4150 0, /* tp_as_buffer */
4151 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4152 0, /* tp_doc */
4153 (traverseproc)dictview_traverse, /* tp_traverse */
4154 0, /* tp_clear */
4155 dictview_richcompare, /* tp_richcompare */
4156 0, /* tp_weaklistoffset */
4157 (getiterfunc)dictkeys_iter, /* tp_iter */
4158 0, /* tp_iternext */
4159 dictkeys_methods, /* tp_methods */
4160 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004161};
4162
4163static PyObject *
4164dictkeys_new(PyObject *dict)
4165{
Eric Snow96c6af92015-05-29 22:21:39 -06004166 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004167}
4168
Guido van Rossum3ac67412007-02-10 18:55:06 +00004169/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004170
4171static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004172dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004173{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004174 if (dv->dv_dict == NULL) {
4175 Py_RETURN_NONE;
4176 }
4177 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004178}
4179
4180static int
Eric Snow96c6af92015-05-29 22:21:39 -06004181dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004182{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004183 PyObject *key, *value, *found;
4184 if (dv->dv_dict == NULL)
4185 return 0;
4186 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
4187 return 0;
4188 key = PyTuple_GET_ITEM(obj, 0);
4189 value = PyTuple_GET_ITEM(obj, 1);
Raymond Hettinger6692f012016-09-18 21:46:08 -07004190 found = PyDict_GetItemWithError((PyObject *)dv->dv_dict, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004191 if (found == NULL) {
4192 if (PyErr_Occurred())
4193 return -1;
4194 return 0;
4195 }
4196 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004197}
4198
Guido van Rossum83825ac2007-02-10 04:54:19 +00004199static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004200 (lenfunc)dictview_len, /* sq_length */
4201 0, /* sq_concat */
4202 0, /* sq_repeat */
4203 0, /* sq_item */
4204 0, /* sq_slice */
4205 0, /* sq_ass_item */
4206 0, /* sq_ass_slice */
4207 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004208};
4209
Guido van Rossumb90c8482007-02-10 01:11:45 +00004210static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004211 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4212 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004213 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004214};
4215
4216PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004217 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4218 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004219 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004220 0, /* tp_itemsize */
4221 /* methods */
4222 (destructor)dictview_dealloc, /* tp_dealloc */
4223 0, /* tp_print */
4224 0, /* tp_getattr */
4225 0, /* tp_setattr */
4226 0, /* tp_reserved */
4227 (reprfunc)dictview_repr, /* tp_repr */
4228 &dictviews_as_number, /* tp_as_number */
4229 &dictitems_as_sequence, /* tp_as_sequence */
4230 0, /* tp_as_mapping */
4231 0, /* tp_hash */
4232 0, /* tp_call */
4233 0, /* tp_str */
4234 PyObject_GenericGetAttr, /* tp_getattro */
4235 0, /* tp_setattro */
4236 0, /* tp_as_buffer */
4237 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4238 0, /* tp_doc */
4239 (traverseproc)dictview_traverse, /* tp_traverse */
4240 0, /* tp_clear */
4241 dictview_richcompare, /* tp_richcompare */
4242 0, /* tp_weaklistoffset */
4243 (getiterfunc)dictitems_iter, /* tp_iter */
4244 0, /* tp_iternext */
4245 dictitems_methods, /* tp_methods */
4246 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004247};
4248
4249static PyObject *
4250dictitems_new(PyObject *dict)
4251{
Eric Snow96c6af92015-05-29 22:21:39 -06004252 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004253}
4254
Guido van Rossum3ac67412007-02-10 18:55:06 +00004255/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004256
4257static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004258dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004259{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004260 if (dv->dv_dict == NULL) {
4261 Py_RETURN_NONE;
4262 }
4263 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004264}
4265
Guido van Rossum83825ac2007-02-10 04:54:19 +00004266static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004267 (lenfunc)dictview_len, /* sq_length */
4268 0, /* sq_concat */
4269 0, /* sq_repeat */
4270 0, /* sq_item */
4271 0, /* sq_slice */
4272 0, /* sq_ass_item */
4273 0, /* sq_ass_slice */
4274 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004275};
4276
Guido van Rossumb90c8482007-02-10 01:11:45 +00004277static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004278 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004279};
4280
4281PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004282 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4283 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004284 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004285 0, /* tp_itemsize */
4286 /* methods */
4287 (destructor)dictview_dealloc, /* tp_dealloc */
4288 0, /* tp_print */
4289 0, /* tp_getattr */
4290 0, /* tp_setattr */
4291 0, /* tp_reserved */
4292 (reprfunc)dictview_repr, /* tp_repr */
4293 0, /* tp_as_number */
4294 &dictvalues_as_sequence, /* tp_as_sequence */
4295 0, /* tp_as_mapping */
4296 0, /* tp_hash */
4297 0, /* tp_call */
4298 0, /* tp_str */
4299 PyObject_GenericGetAttr, /* tp_getattro */
4300 0, /* tp_setattro */
4301 0, /* tp_as_buffer */
4302 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4303 0, /* tp_doc */
4304 (traverseproc)dictview_traverse, /* tp_traverse */
4305 0, /* tp_clear */
4306 0, /* tp_richcompare */
4307 0, /* tp_weaklistoffset */
4308 (getiterfunc)dictvalues_iter, /* tp_iter */
4309 0, /* tp_iternext */
4310 dictvalues_methods, /* tp_methods */
4311 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004312};
4313
4314static PyObject *
4315dictvalues_new(PyObject *dict)
4316{
Eric Snow96c6af92015-05-29 22:21:39 -06004317 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004318}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004319
4320/* Returns NULL if cannot allocate a new PyDictKeysObject,
4321 but does not set an error */
4322PyDictKeysObject *
4323_PyDict_NewKeysForClass(void)
4324{
Victor Stinner742da042016-09-07 17:40:12 -07004325 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004326 if (keys == NULL)
4327 PyErr_Clear();
4328 else
4329 keys->dk_lookup = lookdict_split;
4330 return keys;
4331}
4332
4333#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
4334
4335PyObject *
4336PyObject_GenericGetDict(PyObject *obj, void *context)
4337{
4338 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
4339 if (dictptr == NULL) {
4340 PyErr_SetString(PyExc_AttributeError,
4341 "This object has no __dict__");
4342 return NULL;
4343 }
4344 dict = *dictptr;
4345 if (dict == NULL) {
4346 PyTypeObject *tp = Py_TYPE(obj);
4347 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
4348 DK_INCREF(CACHED_KEYS(tp));
4349 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
4350 }
4351 else {
4352 *dictptr = dict = PyDict_New();
4353 }
4354 }
4355 Py_XINCREF(dict);
4356 return dict;
4357}
4358
4359int
4360_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
Victor Stinner742da042016-09-07 17:40:12 -07004361 PyObject *key, PyObject *value)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004362{
4363 PyObject *dict;
4364 int res;
4365 PyDictKeysObject *cached;
4366
4367 assert(dictptr != NULL);
4368 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
4369 assert(dictptr != NULL);
4370 dict = *dictptr;
4371 if (dict == NULL) {
4372 DK_INCREF(cached);
4373 dict = new_dict_with_shared_keys(cached);
4374 if (dict == NULL)
4375 return -1;
4376 *dictptr = dict;
4377 }
4378 if (value == NULL) {
4379 res = PyDict_DelItem(dict, key);
INADA Naoki89ddffb2017-02-13 09:19:05 +09004380 // Since key sharing dict doesn't allow deletion, PyDict_DelItem()
4381 // always converts dict to combined form.
4382 if ((cached = CACHED_KEYS(tp)) != NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004383 CACHED_KEYS(tp) = NULL;
4384 DK_DECREF(cached);
4385 }
Victor Stinner3d3f2642016-12-15 17:21:23 +01004386 }
4387 else {
INADA Naoki89ddffb2017-02-13 09:19:05 +09004388 int was_shared = (cached == ((PyDictObject *)dict)->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004389 res = PyDict_SetItem(dict, key, value);
INADA Naoki89ddffb2017-02-13 09:19:05 +09004390 if (was_shared &&
4391 (cached = CACHED_KEYS(tp)) != NULL &&
4392 cached != ((PyDictObject *)dict)->ma_keys) {
Victor Stinner3d3f2642016-12-15 17:21:23 +01004393 /* PyDict_SetItem() may call dictresize and convert split table
4394 * into combined table. In such case, convert it to split
4395 * table again and update type's shared key only when this is
4396 * the only dict sharing key with the type.
4397 *
4398 * This is to allow using shared key in class like this:
4399 *
4400 * class C:
4401 * def __init__(self):
4402 * # one dict resize happens
4403 * self.a, self.b, self.c = 1, 2, 3
4404 * self.d, self.e, self.f = 4, 5, 6
4405 * a = C()
4406 */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004407 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004408 CACHED_KEYS(tp) = make_keys_shared(dict);
Victor Stinner742da042016-09-07 17:40:12 -07004409 }
4410 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004411 CACHED_KEYS(tp) = NULL;
4412 }
4413 DK_DECREF(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004414 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
4415 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004416 }
4417 }
4418 } else {
4419 dict = *dictptr;
4420 if (dict == NULL) {
4421 dict = PyDict_New();
4422 if (dict == NULL)
4423 return -1;
4424 *dictptr = dict;
4425 }
4426 if (value == NULL) {
4427 res = PyDict_DelItem(dict, key);
4428 } else {
4429 res = PyDict_SetItem(dict, key, value);
4430 }
4431 }
4432 return res;
4433}
4434
4435void
4436_PyDictKeys_DecRef(PyDictKeysObject *keys)
4437{
4438 DK_DECREF(keys);
4439}