blob: d8ab91fb12d6629db8524a8db8f153388ecc43b0 [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 */
391#define ESTIMATE_SIZE(n) (((n)*3) >> 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
Victor Stinner742da042016-09-07 17:40:12 -0700679never raise an exception; that function can never return DKIX_ERROR.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400680lookdict_unicode_nodummy is further specialized for string keys that cannot be
681the <dummy> value.
Victor Stinner742da042016-09-07 17:40:12 -0700682For both, when the key isn't found a DKIX_EMPTY is returned. hashpos returns
683where the key index should be inserted.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000684*/
Victor Stinner742da042016-09-07 17:40:12 -0700685static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400686lookdict(PyDictObject *mp, PyObject *key,
Victor Stinner742da042016-09-07 17:40:12 -0700687 Py_hash_t hash, PyObject ***value_addr, Py_ssize_t *hashpos)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000688{
INADA Naoki267941c2016-10-06 15:19:07 +0900689 size_t i, mask;
Victor Stinner742da042016-09-07 17:40:12 -0700690 Py_ssize_t ix, freeslot;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200691 int cmp;
Victor Stinner742da042016-09-07 17:40:12 -0700692 PyDictKeysObject *dk;
693 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000695
Antoine Pitrou9a234902012-05-13 20:48:01 +0200696top:
Victor Stinner742da042016-09-07 17:40:12 -0700697 dk = mp->ma_keys;
698 mask = DK_MASK(dk);
699 ep0 = DK_ENTRIES(dk);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700701
702 ix = dk_get_index(dk, i);
703 if (ix == DKIX_EMPTY) {
704 if (hashpos != NULL)
705 *hashpos = i;
706 *value_addr = NULL;
707 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400708 }
Victor Stinner742da042016-09-07 17:40:12 -0700709 if (ix == DKIX_DUMMY) {
710 freeslot = i;
711 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 else {
Victor Stinner742da042016-09-07 17:40:12 -0700713 ep = &ep0[ix];
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300714 assert(ep->me_key != NULL);
Victor Stinner742da042016-09-07 17:40:12 -0700715 if (ep->me_key == key) {
716 *value_addr = &ep->me_value;
717 if (hashpos != NULL)
718 *hashpos = i;
719 return ix;
720 }
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300721 if (ep->me_hash == hash) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 startkey = ep->me_key;
723 Py_INCREF(startkey);
724 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
725 Py_DECREF(startkey);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +0200726 if (cmp < 0) {
727 *value_addr = NULL;
Victor Stinner742da042016-09-07 17:40:12 -0700728 return DKIX_ERROR;
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +0200729 }
Victor Stinner742da042016-09-07 17:40:12 -0700730 if (dk == mp->ma_keys && ep->me_key == startkey) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400731 if (cmp > 0) {
732 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700733 if (hashpos != NULL)
734 *hashpos = i;
735 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400736 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 }
738 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200739 /* The dict was mutated, restart */
740 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 }
742 }
Victor Stinner742da042016-09-07 17:40:12 -0700743 freeslot = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 }
Tim Peters15d49292001-05-27 07:39:22 +0000745
INADA Naoki267941c2016-10-06 15:19:07 +0900746 for (size_t perturb = hash;;) {
747 perturb >>= PERTURB_SHIFT;
Victor Stinner742da042016-09-07 17:40:12 -0700748 i = ((i << 2) + i + perturb + 1) & mask;
749 ix = dk_get_index(dk, i);
750 if (ix == DKIX_EMPTY) {
751 if (hashpos != NULL) {
752 *hashpos = (freeslot == -1) ? (Py_ssize_t)i : freeslot;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400753 }
Victor Stinner742da042016-09-07 17:40:12 -0700754 *value_addr = NULL;
755 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400756 }
Victor Stinner742da042016-09-07 17:40:12 -0700757 if (ix == DKIX_DUMMY) {
758 if (freeslot == -1)
759 freeslot = i;
760 continue;
761 }
762 ep = &ep0[ix];
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300763 assert(ep->me_key != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400764 if (ep->me_key == key) {
Victor Stinner742da042016-09-07 17:40:12 -0700765 if (hashpos != NULL) {
766 *hashpos = i;
767 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400768 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700769 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400770 }
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300771 if (ep->me_hash == hash) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 startkey = ep->me_key;
773 Py_INCREF(startkey);
774 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
775 Py_DECREF(startkey);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400776 if (cmp < 0) {
777 *value_addr = NULL;
Victor Stinner742da042016-09-07 17:40:12 -0700778 return DKIX_ERROR;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400779 }
Victor Stinner742da042016-09-07 17:40:12 -0700780 if (dk == mp->ma_keys && ep->me_key == startkey) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400781 if (cmp > 0) {
Victor Stinner742da042016-09-07 17:40:12 -0700782 if (hashpos != NULL) {
783 *hashpos = i;
784 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400785 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700786 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400787 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 }
789 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200790 /* The dict was mutated, restart */
791 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 }
793 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 }
795 assert(0); /* NOT REACHED */
796 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000797}
798
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400799/* Specialized version for string-only keys */
Victor Stinner742da042016-09-07 17:40:12 -0700800static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400801lookdict_unicode(PyDictObject *mp, PyObject *key,
Victor Stinner742da042016-09-07 17:40:12 -0700802 Py_hash_t hash, PyObject ***value_addr, Py_ssize_t *hashpos)
Fred Drake1bff34a2000-08-31 19:31:38 +0000803{
INADA Naoki267941c2016-10-06 15:19:07 +0900804 size_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200805 size_t mask = DK_MASK(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -0700806 Py_ssize_t ix, freeslot;
807 PyDictKeyEntry *ep, *ep0 = DK_ENTRIES(mp->ma_keys);
Fred Drake1bff34a2000-08-31 19:31:38 +0000808
Victor Stinner742da042016-09-07 17:40:12 -0700809 assert(mp->ma_values == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 /* Make sure this function doesn't have to handle non-unicode keys,
811 including subclasses of str; e.g., one reason to subclass
812 unicodes is to override __eq__, and for speed we don't cater to
813 that here. */
814 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400815 mp->ma_keys->dk_lookup = lookdict;
Victor Stinner742da042016-09-07 17:40:12 -0700816 return lookdict(mp, key, hash, value_addr, hashpos);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 }
Mark Dickinson57e683e2011-09-24 18:18:40 +0100818 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700819 ix = dk_get_index(mp->ma_keys, i);
820 if (ix == DKIX_EMPTY) {
821 if (hashpos != NULL)
822 *hashpos = i;
823 *value_addr = NULL;
824 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400825 }
Victor Stinner742da042016-09-07 17:40:12 -0700826 if (ix == DKIX_DUMMY) {
827 freeslot = i;
828 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 else {
Victor Stinner742da042016-09-07 17:40:12 -0700830 ep = &ep0[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700831 assert(ep->me_key != NULL);
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300832 if (ep->me_key == key
833 || (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700834 if (hashpos != NULL)
835 *hashpos = i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400836 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700837 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400838 }
Victor Stinner742da042016-09-07 17:40:12 -0700839 freeslot = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 }
Tim Peters15d49292001-05-27 07:39:22 +0000841
INADA Naoki267941c2016-10-06 15:19:07 +0900842 for (size_t perturb = hash;;) {
843 perturb >>= PERTURB_SHIFT;
Victor Stinner742da042016-09-07 17:40:12 -0700844 i = mask & ((i << 2) + i + perturb + 1);
845 ix = dk_get_index(mp->ma_keys, i);
846 if (ix == DKIX_EMPTY) {
847 if (hashpos != NULL) {
848 *hashpos = (freeslot == -1) ? (Py_ssize_t)i : freeslot;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400849 }
Victor Stinner742da042016-09-07 17:40:12 -0700850 *value_addr = NULL;
851 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400852 }
Victor Stinner742da042016-09-07 17:40:12 -0700853 if (ix == DKIX_DUMMY) {
854 if (freeslot == -1)
855 freeslot = i;
856 continue;
857 }
858 ep = &ep0[ix];
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300859 assert(ep->me_key != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 if (ep->me_key == key
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300861 || (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400862 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700863 if (hashpos != NULL) {
864 *hashpos = i;
865 }
866 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400867 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 }
869 assert(0); /* NOT REACHED */
870 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000871}
872
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400873/* Faster version of lookdict_unicode when it is known that no <dummy> keys
874 * will be present. */
Victor Stinner742da042016-09-07 17:40:12 -0700875static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400876lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
Victor Stinner742da042016-09-07 17:40:12 -0700877 Py_hash_t hash, PyObject ***value_addr,
878 Py_ssize_t *hashpos)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400879{
INADA Naoki267941c2016-10-06 15:19:07 +0900880 size_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200881 size_t mask = DK_MASK(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -0700882 Py_ssize_t ix;
883 PyDictKeyEntry *ep, *ep0 = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400884
Victor Stinner742da042016-09-07 17:40:12 -0700885 assert(mp->ma_values == NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400886 /* Make sure this function doesn't have to handle non-unicode keys,
887 including subclasses of str; e.g., one reason to subclass
888 unicodes is to override __eq__, and for speed we don't cater to
889 that here. */
890 if (!PyUnicode_CheckExact(key)) {
891 mp->ma_keys->dk_lookup = lookdict;
Victor Stinner742da042016-09-07 17:40:12 -0700892 return lookdict(mp, key, hash, value_addr, hashpos);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400893 }
894 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700895 ix = dk_get_index(mp->ma_keys, i);
896 assert (ix != DKIX_DUMMY);
897 if (ix == DKIX_EMPTY) {
898 if (hashpos != NULL)
899 *hashpos = i;
900 *value_addr = NULL;
901 return DKIX_EMPTY;
902 }
903 ep = &ep0[ix];
Victor Stinnerdee6e252016-09-08 11:16:07 -0700904 assert(ep->me_key != NULL);
905 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700906 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400907 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700908 if (hashpos != NULL)
909 *hashpos = i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400910 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700911 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400912 }
INADA Naoki267941c2016-10-06 15:19:07 +0900913 for (size_t perturb = hash;;) {
914 perturb >>= PERTURB_SHIFT;
Victor Stinner742da042016-09-07 17:40:12 -0700915 i = mask & ((i << 2) + i + perturb + 1);
916 ix = dk_get_index(mp->ma_keys, i);
917 assert (ix != DKIX_DUMMY);
918 if (ix == DKIX_EMPTY) {
919 if (hashpos != NULL)
920 *hashpos = i;
921 *value_addr = NULL;
922 return DKIX_EMPTY;
923 }
924 ep = &ep0[ix];
925 assert(ep->me_key != NULL && PyUnicode_CheckExact(ep->me_key));
926 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400927 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700928 if (hashpos != NULL)
929 *hashpos = i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400930 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700931 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400932 }
933 }
934 assert(0); /* NOT REACHED */
935 return 0;
936}
937
938/* Version of lookdict for split tables.
939 * All split tables and only split tables use this lookup function.
940 * Split tables only contain unicode keys and no dummy keys,
941 * so algorithm is the same as lookdict_unicode_nodummy.
942 */
Victor Stinner742da042016-09-07 17:40:12 -0700943static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400944lookdict_split(PyDictObject *mp, PyObject *key,
Victor Stinner742da042016-09-07 17:40:12 -0700945 Py_hash_t hash, PyObject ***value_addr, Py_ssize_t *hashpos)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400946{
INADA Naoki267941c2016-10-06 15:19:07 +0900947 size_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200948 size_t mask = DK_MASK(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -0700949 Py_ssize_t ix;
950 PyDictKeyEntry *ep, *ep0 = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400951
Victor Stinner742da042016-09-07 17:40:12 -0700952 /* mp must split table */
953 assert(mp->ma_values != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400954 if (!PyUnicode_CheckExact(key)) {
Victor Stinner742da042016-09-07 17:40:12 -0700955 ix = lookdict(mp, key, hash, value_addr, hashpos);
956 if (ix >= 0) {
957 *value_addr = &mp->ma_values[ix];
958 }
959 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400960 }
Victor Stinner742da042016-09-07 17:40:12 -0700961
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400962 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700963 ix = dk_get_index(mp->ma_keys, i);
964 if (ix == DKIX_EMPTY) {
965 if (hashpos != NULL)
966 *hashpos = i;
967 *value_addr = NULL;
968 return DKIX_EMPTY;
969 }
970 assert(ix >= 0);
971 ep = &ep0[ix];
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300972 assert(ep->me_key != NULL && PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700973 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400974 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700975 if (hashpos != NULL)
976 *hashpos = i;
977 *value_addr = &mp->ma_values[ix];
978 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400979 }
INADA Naoki267941c2016-10-06 15:19:07 +0900980 for (size_t perturb = hash;;) {
981 perturb >>= PERTURB_SHIFT;
Victor Stinner742da042016-09-07 17:40:12 -0700982 i = mask & ((i << 2) + i + perturb + 1);
983 ix = dk_get_index(mp->ma_keys, i);
984 if (ix == DKIX_EMPTY) {
985 if (hashpos != NULL)
986 *hashpos = i;
987 *value_addr = NULL;
988 return DKIX_EMPTY;
989 }
990 assert(ix >= 0);
991 ep = &ep0[ix];
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300992 assert(ep->me_key != NULL && PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700993 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400994 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700995 if (hashpos != NULL)
996 *hashpos = i;
997 *value_addr = &mp->ma_values[ix];
998 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400999 }
1000 }
1001 assert(0); /* NOT REACHED */
1002 return 0;
1003}
1004
Benjamin Petersonfb886362010-04-24 18:21:17 +00001005int
1006_PyDict_HasOnlyStringKeys(PyObject *dict)
1007{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 Py_ssize_t pos = 0;
1009 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +00001010 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001012 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 return 1;
1014 while (PyDict_Next(dict, &pos, &key, &value))
1015 if (!PyUnicode_Check(key))
1016 return 0;
1017 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +00001018}
1019
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001020#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 do { \
1022 if (!_PyObject_GC_IS_TRACKED(mp)) { \
1023 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
1024 _PyObject_GC_MAY_BE_TRACKED(value)) { \
1025 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 } \
1027 } \
1028 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001029
1030void
1031_PyDict_MaybeUntrack(PyObject *op)
1032{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 PyDictObject *mp;
1034 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07001035 Py_ssize_t i, numentries;
1036 PyDictKeyEntry *ep0;
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
1039 return;
1040
1041 mp = (PyDictObject *) op;
Victor Stinner742da042016-09-07 17:40:12 -07001042 ep0 = DK_ENTRIES(mp->ma_keys);
1043 numentries = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001044 if (_PyDict_HasSplitTable(mp)) {
Victor Stinner742da042016-09-07 17:40:12 -07001045 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001046 if ((value = mp->ma_values[i]) == NULL)
1047 continue;
1048 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
Victor Stinner742da042016-09-07 17:40:12 -07001049 assert(!_PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001050 return;
1051 }
1052 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001054 else {
Victor Stinner742da042016-09-07 17:40:12 -07001055 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001056 if ((value = ep0[i].me_value) == NULL)
1057 continue;
1058 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
1059 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
1060 return;
1061 }
1062 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001064}
1065
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001066/* Internal function to find slot for an item from its hash
Victor Stinner3c336c52016-09-12 14:17:40 +02001067 when it is known that the key is not present in the dict.
1068
1069 The dict must be combined. */
Victor Stinner99264802016-09-13 09:38:29 +02001070static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001071find_empty_slot(PyDictObject *mp, PyObject *key, Py_hash_t hash,
Victor Stinner742da042016-09-07 17:40:12 -07001072 PyObject ***value_addr, Py_ssize_t *hashpos)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001073{
INADA Naoki267941c2016-10-06 15:19:07 +09001074 size_t i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001075 size_t mask = DK_MASK(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07001076 Py_ssize_t ix;
1077 PyDictKeyEntry *ep, *ep0 = DK_ENTRIES(mp->ma_keys);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001078
Victor Stinner3c336c52016-09-12 14:17:40 +02001079 assert(!_PyDict_HasSplitTable(mp));
Victor Stinner742da042016-09-07 17:40:12 -07001080 assert(hashpos != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001081 assert(key != NULL);
Victor Stinner3c336c52016-09-12 14:17:40 +02001082
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001083 if (!PyUnicode_CheckExact(key))
1084 mp->ma_keys->dk_lookup = lookdict;
1085 i = hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -07001086 ix = dk_get_index(mp->ma_keys, i);
INADA Naoki267941c2016-10-06 15:19:07 +09001087 for (size_t perturb = hash; ix != DKIX_EMPTY;) {
1088 perturb >>= PERTURB_SHIFT;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001089 i = (i << 2) + i + perturb + 1;
Victor Stinner742da042016-09-07 17:40:12 -07001090 ix = dk_get_index(mp->ma_keys, i & mask);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 }
Victor Stinner742da042016-09-07 17:40:12 -07001092 ep = &ep0[mp->ma_keys->dk_nentries];
1093 *hashpos = i & mask;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001094 assert(ep->me_value == NULL);
Victor Stinner99264802016-09-13 09:38:29 +02001095 *value_addr = &ep->me_value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001096}
1097
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001098static int
1099insertion_resize(PyDictObject *mp)
1100{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -07001101 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001102}
Antoine Pitroue965d972012-02-27 00:45:12 +01001103
1104/*
1105Internal routine to insert a new item into the table.
1106Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +01001107Returns -1 if an error occurred, or 0 on success.
1108*/
1109static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001110insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001111{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001112 PyObject *old_value;
1113 PyObject **value_addr;
Victor Stinner742da042016-09-07 17:40:12 -07001114 PyDictKeyEntry *ep, *ep0;
1115 Py_ssize_t hashpos, ix;
Antoine Pitroue965d972012-02-27 00:45:12 +01001116
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001117 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
1118 if (insertion_resize(mp) < 0)
1119 return -1;
1120 }
1121
Victor Stinner742da042016-09-07 17:40:12 -07001122 ix = mp->ma_keys->dk_lookup(mp, key, hash, &value_addr, &hashpos);
1123 if (ix == DKIX_ERROR) {
Antoine Pitroue965d972012-02-27 00:45:12 +01001124 return -1;
1125 }
Victor Stinner742da042016-09-07 17:40:12 -07001126
Antoine Pitroud6967322014-10-18 00:35:00 +02001127 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
Benjamin Petersona6f195e2012-04-30 10:23:40 -04001128 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001129 MAINTAIN_TRACKING(mp, key, value);
Victor Stinner742da042016-09-07 17:40:12 -07001130
1131 /* When insertion order is different from shared key, we can't share
1132 * the key anymore. Convert this instance to combine table.
1133 */
1134 if (_PyDict_HasSplitTable(mp) &&
1135 ((ix >= 0 && *value_addr == NULL && mp->ma_used != ix) ||
1136 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
1137 if (insertion_resize(mp) < 0) {
1138 Py_DECREF(value);
1139 return -1;
1140 }
1141 find_empty_slot(mp, key, hash, &value_addr, &hashpos);
1142 ix = DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001143 }
Victor Stinner742da042016-09-07 17:40:12 -07001144
1145 if (ix == DKIX_EMPTY) {
1146 /* Insert into new slot. */
1147 if (mp->ma_keys->dk_usable <= 0) {
1148 /* Need to resize. */
1149 if (insertion_resize(mp) < 0) {
1150 Py_DECREF(value);
1151 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001152 }
Victor Stinner742da042016-09-07 17:40:12 -07001153 find_empty_slot(mp, key, hash, &value_addr, &hashpos);
1154 }
1155 ep0 = DK_ENTRIES(mp->ma_keys);
1156 ep = &ep0[mp->ma_keys->dk_nentries];
1157 dk_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
1158 Py_INCREF(key);
1159 ep->me_key = key;
1160 ep->me_hash = hash;
1161 if (mp->ma_values) {
1162 assert (mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
1163 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001164 }
1165 else {
Victor Stinner742da042016-09-07 17:40:12 -07001166 ep->me_value = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001167 }
1168 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001169 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07001170 mp->ma_keys->dk_usable--;
1171 mp->ma_keys->dk_nentries++;
1172 assert(mp->ma_keys->dk_usable >= 0);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001173 assert(_PyDict_CheckConsistency(mp));
Victor Stinner742da042016-09-07 17:40:12 -07001174 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001175 }
Victor Stinner742da042016-09-07 17:40:12 -07001176
1177 assert(value_addr != NULL);
1178
1179 old_value = *value_addr;
1180 if (old_value != NULL) {
1181 *value_addr = value;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001182 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02001183 assert(_PyDict_CheckConsistency(mp));
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001184
Victor Stinner742da042016-09-07 17:40:12 -07001185 Py_DECREF(old_value); /* which **CAN** re-enter (see issue #22653) */
1186 return 0;
1187 }
1188
1189 /* pending state */
1190 assert(_PyDict_HasSplitTable(mp));
1191 assert(ix == mp->ma_used);
1192 *value_addr = value;
1193 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001194 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02001195 assert(_PyDict_CheckConsistency(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001196 return 0;
Antoine Pitroue965d972012-02-27 00:45:12 +01001197}
1198
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001199/*
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001200Internal routine used by dictresize() to insert an item which is
1201known to be absent from the dict. This routine also assumes that
1202the dict contains no deleted entries. Besides the performance benefit,
1203using insertdict() in dictresize() is dangerous (SF bug #1456209).
1204Note that no refcounts are changed by this routine; if needed, the caller
1205is responsible for incref'ing `key` and `value`.
1206Neither mp->ma_used nor k->dk_usable are modified by this routine; the caller
1207must set them correctly
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001208*/
1209static void
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001210insertdict_clean(PyDictObject *mp, PyObject *key, Py_hash_t hash,
1211 PyObject *value)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001212{
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001213 size_t i;
1214 PyDictKeysObject *k = mp->ma_keys;
1215 size_t mask = (size_t)DK_SIZE(k)-1;
1216 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
1217 PyDictKeyEntry *ep;
1218
1219 assert(k->dk_lookup != NULL);
1220 assert(value != NULL);
1221 assert(key != NULL);
1222 assert(PyUnicode_CheckExact(key) || k->dk_lookup == lookdict);
1223 i = hash & mask;
1224 for (size_t perturb = hash; dk_get_index(k, i) != DKIX_EMPTY;) {
1225 perturb >>= PERTURB_SHIFT;
1226 i = mask & ((i << 2) + i + perturb + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 }
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001228 ep = &ep0[k->dk_nentries];
1229 assert(ep->me_value == NULL);
1230 dk_set_index(k, i, k->dk_nentries);
1231 k->dk_nentries++;
1232 ep->me_key = key;
1233 ep->me_hash = hash;
1234 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001235}
1236
1237/*
1238Restructure the table by allocating a new table and reinserting all
1239items again. When entries have been deleted, the new table may
1240actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001241If a table is split (its keys and hashes are shared, its values are not),
1242then the values are temporarily copied into the table, it is resized as
1243a combined table, then the me_value slots in the old table are NULLed out.
1244After resizing a table is always combined,
1245but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001246*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001247static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001248dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001249{
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001250 Py_ssize_t i, newsize;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001251 PyDictKeysObject *oldkeys;
1252 PyObject **oldvalues;
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001253 PyDictKeyEntry *ep0;
Tim Peters91a364d2001-05-19 07:04:38 +00001254
Victor Stinner742da042016-09-07 17:40:12 -07001255 /* Find the smallest table size > minused. */
1256 for (newsize = PyDict_MINSIZE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 newsize <= minused && newsize > 0;
1258 newsize <<= 1)
1259 ;
1260 if (newsize <= 0) {
1261 PyErr_NoMemory();
1262 return -1;
1263 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001264 oldkeys = mp->ma_keys;
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001265 oldvalues = mp->ma_values;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001266 /* Allocate a new table. */
1267 mp->ma_keys = new_keys_object(newsize);
1268 if (mp->ma_keys == NULL) {
1269 mp->ma_keys = oldkeys;
1270 return -1;
1271 }
1272 if (oldkeys->dk_lookup == lookdict)
1273 mp->ma_keys->dk_lookup = lookdict;
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001274 mp->ma_values = NULL;
1275 ep0 = DK_ENTRIES(oldkeys);
1276 /* Main loop below assumes we can transfer refcount to new keys
1277 * and that value is stored in me_value.
1278 * Increment ref-counts and copy values here to compensate
1279 * This (resizing a split table) should be relatively rare */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001280 if (oldvalues != NULL) {
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001281 for (i = 0; i < oldkeys->dk_nentries; i++) {
1282 if (oldvalues[i] != NULL) {
1283 Py_INCREF(ep0[i].me_key);
1284 ep0[i].me_value = oldvalues[i];
1285 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 }
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001287 }
1288 /* Main loop */
1289 for (i = 0; i < oldkeys->dk_nentries; i++) {
1290 PyDictKeyEntry *ep = &ep0[i];
1291 if (ep->me_value != NULL) {
1292 insertdict_clean(mp, ep->me_key, ep->me_hash, ep->me_value);
1293 }
1294 }
1295 mp->ma_keys->dk_usable -= mp->ma_used;
1296 if (oldvalues != NULL) {
1297 /* NULL out me_value slot in oldkeys, in case it was shared */
1298 for (i = 0; i < oldkeys->dk_nentries; i++)
1299 ep0[i].me_value = NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001300 DK_DECREF(oldkeys);
Victor Stinner742da042016-09-07 17:40:12 -07001301 if (oldvalues != empty_values) {
1302 free_values(oldvalues);
1303 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001304 }
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001305 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001306 assert(oldkeys->dk_lookup != lookdict_split);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001307 assert(oldkeys->dk_refcnt == 1);
Serhiy Storchaka7f0514a2016-10-31 20:14:05 +02001308 DK_DEBUG_DECREF PyObject_FREE(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001309 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001311}
1312
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001313/* Returns NULL if unable to split table.
1314 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001315static PyDictKeysObject *
1316make_keys_shared(PyObject *op)
1317{
1318 Py_ssize_t i;
1319 Py_ssize_t size;
1320 PyDictObject *mp = (PyDictObject *)op;
1321
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001322 if (!PyDict_CheckExact(op))
1323 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001324 if (!_PyDict_HasSplitTable(mp)) {
1325 PyDictKeyEntry *ep0;
1326 PyObject **values;
1327 assert(mp->ma_keys->dk_refcnt == 1);
1328 if (mp->ma_keys->dk_lookup == lookdict) {
1329 return NULL;
1330 }
1331 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1332 /* Remove dummy keys */
1333 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1334 return NULL;
1335 }
1336 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1337 /* Copy values into a new array */
Victor Stinner742da042016-09-07 17:40:12 -07001338 ep0 = DK_ENTRIES(mp->ma_keys);
1339 size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001340 values = new_values(size);
1341 if (values == NULL) {
1342 PyErr_SetString(PyExc_MemoryError,
1343 "Not enough memory to allocate new values array");
1344 return NULL;
1345 }
1346 for (i = 0; i < size; i++) {
1347 values[i] = ep0[i].me_value;
1348 ep0[i].me_value = NULL;
1349 }
1350 mp->ma_keys->dk_lookup = lookdict_split;
1351 mp->ma_values = values;
1352 }
1353 DK_INCREF(mp->ma_keys);
1354 return mp->ma_keys;
1355}
Christian Heimes99170a52007-12-19 02:07:34 +00001356
1357PyObject *
1358_PyDict_NewPresized(Py_ssize_t minused)
1359{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001360 Py_ssize_t newsize;
1361 PyDictKeysObject *new_keys;
Victor Stinner742da042016-09-07 17:40:12 -07001362 for (newsize = PyDict_MINSIZE;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001363 newsize <= minused && newsize > 0;
1364 newsize <<= 1)
1365 ;
1366 new_keys = new_keys_object(newsize);
1367 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001369 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001370}
1371
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001372/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1373 * that may occur (originally dicts supported only string keys, and exceptions
1374 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001375 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001376 * (suppressed) occurred while computing the key's hash, or that some error
1377 * (suppressed) occurred when comparing keys in the dict's internal probe
1378 * sequence. A nasty example of the latter is when a Python-coded comparison
1379 * function hits a stack-depth error, which can cause this to return NULL
1380 * even if the key is present.
1381 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001382PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001383PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001384{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001385 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07001386 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 PyDictObject *mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 PyThreadState *tstate;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001389 PyObject **value_addr;
1390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 if (!PyDict_Check(op))
1392 return NULL;
1393 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001394 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 {
1396 hash = PyObject_Hash(key);
1397 if (hash == -1) {
1398 PyErr_Clear();
1399 return NULL;
1400 }
1401 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 /* We can arrive here with a NULL tstate during initialization: try
1404 running "python -Wi" for an example related to string interning.
1405 Let's just hope that no exception occurs then... This must be
1406 _PyThreadState_Current and not PyThreadState_GET() because in debug
1407 mode, the latter complains if tstate is NULL. */
Victor Stinnerbfd316e2016-01-20 11:12:38 +01001408 tstate = _PyThreadState_UncheckedGet();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 if (tstate != NULL && tstate->curexc_type != NULL) {
1410 /* preserve the existing exception */
1411 PyObject *err_type, *err_value, *err_tb;
1412 PyErr_Fetch(&err_type, &err_value, &err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001413 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001414 /* ignore errors */
1415 PyErr_Restore(err_type, err_value, err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001416 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 return NULL;
1418 }
1419 else {
Victor Stinner742da042016-09-07 17:40:12 -07001420 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
1421 if (ix < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 PyErr_Clear();
1423 return NULL;
1424 }
1425 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001426 return *value_addr;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001427}
1428
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001429/* Same as PyDict_GetItemWithError() but with hash supplied by caller.
1430 This returns NULL *with* an exception set if an exception occurred.
1431 It returns NULL *without* an exception set if the key wasn't present.
1432*/
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001433PyObject *
1434_PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1435{
Victor Stinner742da042016-09-07 17:40:12 -07001436 Py_ssize_t ix;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001437 PyDictObject *mp = (PyDictObject *)op;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001438 PyObject **value_addr;
1439
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001440 if (!PyDict_Check(op)) {
1441 PyErr_BadInternalCall();
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001442 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001443 }
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02001444
1445 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
1446 if (ix < 0) {
1447 return NULL;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001448 }
1449 return *value_addr;
1450}
1451
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001452/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1453 This returns NULL *with* an exception set if an exception occurred.
1454 It returns NULL *without* an exception set if the key wasn't present.
1455*/
1456PyObject *
1457PyDict_GetItemWithError(PyObject *op, PyObject *key)
1458{
Victor Stinner742da042016-09-07 17:40:12 -07001459 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001460 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 PyDictObject*mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001462 PyObject **value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 if (!PyDict_Check(op)) {
1465 PyErr_BadInternalCall();
1466 return NULL;
1467 }
1468 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001469 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 {
1471 hash = PyObject_Hash(key);
1472 if (hash == -1) {
1473 return NULL;
1474 }
1475 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001476
Victor Stinner742da042016-09-07 17:40:12 -07001477 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
1478 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001480 return *value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001481}
1482
Brett Cannonfd074152012-04-14 14:10:13 -04001483PyObject *
1484_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1485{
1486 PyObject *kv;
1487 kv = _PyUnicode_FromId(key); /* borrowed */
1488 if (kv == NULL)
1489 return NULL;
1490 return PyDict_GetItemWithError(dp, kv);
1491}
1492
Victor Stinnerb4efc962015-11-20 09:24:02 +01001493/* Fast version of global value lookup (LOAD_GLOBAL).
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001494 * Lookup in globals, then builtins.
Victor Stinnerb4efc962015-11-20 09:24:02 +01001495 *
1496 * Raise an exception and return NULL if an error occurred (ex: computing the
1497 * key hash failed, key comparison failed, ...). Return NULL if the key doesn't
1498 * exist. Return the value if the key exists.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001499 */
1500PyObject *
1501_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001502{
Victor Stinner742da042016-09-07 17:40:12 -07001503 Py_ssize_t ix;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001504 Py_hash_t hash;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001505 PyObject **value_addr;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001506
1507 if (!PyUnicode_CheckExact(key) ||
1508 (hash = ((PyASCIIObject *) key)->hash) == -1)
1509 {
1510 hash = PyObject_Hash(key);
1511 if (hash == -1)
1512 return NULL;
Antoine Pitroue965d972012-02-27 00:45:12 +01001513 }
Victor Stinnerb4efc962015-11-20 09:24:02 +01001514
1515 /* namespace 1: globals */
Victor Stinner742da042016-09-07 17:40:12 -07001516 ix = globals->ma_keys->dk_lookup(globals, key, hash, &value_addr, NULL);
1517 if (ix == DKIX_ERROR)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001518 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001519 if (ix != DKIX_EMPTY && *value_addr != NULL)
1520 return *value_addr;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001521
1522 /* namespace 2: builtins */
Victor Stinner742da042016-09-07 17:40:12 -07001523 ix = builtins->ma_keys->dk_lookup(builtins, key, hash, &value_addr, NULL);
1524 if (ix < 0)
Victor Stinnerb4efc962015-11-20 09:24:02 +01001525 return NULL;
1526 return *value_addr;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001527}
1528
Antoine Pitroue965d972012-02-27 00:45:12 +01001529/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1530 * dictionary if it's merely replacing the value for an existing key.
1531 * This means that it's safe to loop over a dictionary with PyDict_Next()
1532 * and occasionally replace a value -- but you can't insert new keys or
1533 * remove them.
1534 */
1535int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001536PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001537{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001538 PyDictObject *mp;
1539 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001540 if (!PyDict_Check(op)) {
1541 PyErr_BadInternalCall();
1542 return -1;
1543 }
1544 assert(key);
1545 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001546 mp = (PyDictObject *)op;
1547 if (!PyUnicode_CheckExact(key) ||
1548 (hash = ((PyASCIIObject *) key)->hash) == -1)
1549 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001550 hash = PyObject_Hash(key);
1551 if (hash == -1)
1552 return -1;
1553 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001554
1555 /* insertdict() handles any resizing that might be necessary */
1556 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001557}
1558
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001559int
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001560_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1561 Py_hash_t hash)
1562{
1563 PyDictObject *mp;
1564
1565 if (!PyDict_Check(op)) {
1566 PyErr_BadInternalCall();
1567 return -1;
1568 }
1569 assert(key);
1570 assert(value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001571 assert(hash != -1);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001572 mp = (PyDictObject *)op;
1573
1574 /* insertdict() handles any resizing that might be necessary */
1575 return insertdict(mp, key, hash, value);
1576}
1577
1578int
Tim Peters1f5871e2000-07-04 17:44:48 +00001579PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001580{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001581 Py_hash_t hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +00001582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 assert(key);
1584 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001585 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 hash = PyObject_Hash(key);
1587 if (hash == -1)
1588 return -1;
1589 }
Victor Stinner742da042016-09-07 17:40:12 -07001590
1591 return _PyDict_DelItem_KnownHash(op, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001592}
1593
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001594int
1595_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1596{
Victor Stinner742da042016-09-07 17:40:12 -07001597 Py_ssize_t hashpos, ix;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001598 PyDictObject *mp;
1599 PyDictKeyEntry *ep;
1600 PyObject *old_key, *old_value;
1601 PyObject **value_addr;
1602
1603 if (!PyDict_Check(op)) {
1604 PyErr_BadInternalCall();
1605 return -1;
1606 }
1607 assert(key);
1608 assert(hash != -1);
1609 mp = (PyDictObject *)op;
Victor Stinner742da042016-09-07 17:40:12 -07001610 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
1611 if (ix == DKIX_ERROR)
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001612 return -1;
Victor Stinner742da042016-09-07 17:40:12 -07001613 if (ix == DKIX_EMPTY || *value_addr == NULL) {
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001614 _PyErr_SetKeyError(key);
1615 return -1;
1616 }
Victor Stinner742da042016-09-07 17:40:12 -07001617 assert(dk_get_index(mp->ma_keys, hashpos) == ix);
Victor Stinner78601a32016-09-09 19:28:36 -07001618
1619 // Split table doesn't allow deletion. Combine it.
1620 if (_PyDict_HasSplitTable(mp)) {
1621 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1622 return -1;
1623 }
1624 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
1625 assert(ix >= 0);
1626 }
1627
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001628 old_value = *value_addr;
Victor Stinner78601a32016-09-09 19:28:36 -07001629 assert(old_value != NULL);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001630 *value_addr = NULL;
1631 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001632 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner78601a32016-09-09 19:28:36 -07001633 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1634 dk_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
1635 ENSURE_ALLOWS_DELETIONS(mp);
1636 old_key = ep->me_key;
1637 ep->me_key = NULL;
1638 Py_DECREF(old_key);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001639 Py_DECREF(old_value);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001640
1641 assert(_PyDict_CheckConsistency(mp));
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001642 return 0;
1643}
1644
Guido van Rossum25831651993-05-19 14:50:45 +00001645void
Tim Peters1f5871e2000-07-04 17:44:48 +00001646PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001647{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001649 PyDictKeysObject *oldkeys;
1650 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 if (!PyDict_Check(op))
1654 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001655 mp = ((PyDictObject *)op);
1656 oldkeys = mp->ma_keys;
1657 oldvalues = mp->ma_values;
1658 if (oldvalues == empty_values)
1659 return;
1660 /* Empty the dict... */
1661 DK_INCREF(Py_EMPTY_KEYS);
1662 mp->ma_keys = Py_EMPTY_KEYS;
1663 mp->ma_values = empty_values;
1664 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001665 mp->ma_version_tag = DICT_NEXT_VERSION();
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001666 /* ...then clear the keys and values */
1667 if (oldvalues != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001668 n = oldkeys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001669 for (i = 0; i < n; i++)
1670 Py_CLEAR(oldvalues[i]);
1671 free_values(oldvalues);
1672 DK_DECREF(oldkeys);
1673 }
1674 else {
1675 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001676 DK_DECREF(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001677 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02001678 assert(_PyDict_CheckConsistency(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001679}
1680
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001681/* Internal version of PyDict_Next that returns a hash value in addition
1682 * to the key and value.
1683 * Return 1 on success, return 0 when the reached the end of the dictionary
1684 * (or if op is not a dictionary)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001685 */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001686int
1687_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1688 PyObject **pvalue, Py_hash_t *phash)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001689{
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001690 Py_ssize_t i, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001691 PyDictObject *mp;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001692 PyDictKeyEntry *entry_ptr;
1693 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001694
1695 if (!PyDict_Check(op))
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001696 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 mp = (PyDictObject *)op;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001698 i = *ppos;
Victor Stinner742da042016-09-07 17:40:12 -07001699 n = mp->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001700 if ((size_t)i >= (size_t)n)
1701 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001702 if (mp->ma_values) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001703 PyObject **value_ptr = &mp->ma_values[i];
1704 while (i < n && *value_ptr == NULL) {
1705 value_ptr++;
1706 i++;
Victor Stinner742da042016-09-07 17:40:12 -07001707 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001708 if (i >= n)
1709 return 0;
1710 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
1711 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001713 else {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001714 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
1715 while (i < n && entry_ptr->me_value == NULL) {
1716 entry_ptr++;
1717 i++;
Victor Stinner742da042016-09-07 17:40:12 -07001718 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001719 if (i >= n)
1720 return 0;
1721 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001723 *ppos = i+1;
1724 if (pkey)
1725 *pkey = entry_ptr->me_key;
1726 if (phash)
1727 *phash = entry_ptr->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001728 if (pvalue)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001729 *pvalue = value;
1730 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001731}
1732
Tim Peters080c88b2003-02-15 03:01:11 +00001733/*
1734 * Iterate over a dict. Use like so:
1735 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001736 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001737 * PyObject *key, *value;
1738 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001739 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001740 * Refer to borrowed references in key and value.
Tim Peters080c88b2003-02-15 03:01:11 +00001741 * }
1742 *
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001743 * Return 1 on success, return 0 when the reached the end of the dictionary
1744 * (or if op is not a dictionary)
1745 *
Tim Peters080c88b2003-02-15 03:01:11 +00001746 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001747 * mutates the dict. One exception: it is safe if the loop merely changes
1748 * the values associated with the keys (but doesn't insert new keys or
1749 * delete keys), via PyDict_SetItem().
1750 */
Guido van Rossum25831651993-05-19 14:50:45 +00001751int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001752PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001753{
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001754 return _PyDict_Next(op, ppos, pkey, pvalue, NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001755}
1756
Eric Snow96c6af92015-05-29 22:21:39 -06001757/* Internal version of dict.pop(). */
1758PyObject *
Yury Selivanov684ef2c2016-10-28 19:01:21 -04001759_PyDict_Pop(PyObject *dict, PyObject *key, PyObject *deflt)
Eric Snow96c6af92015-05-29 22:21:39 -06001760{
1761 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07001762 Py_ssize_t ix, hashpos;
Eric Snow96c6af92015-05-29 22:21:39 -06001763 PyObject *old_value, *old_key;
1764 PyDictKeyEntry *ep;
1765 PyObject **value_addr;
Yury Selivanov684ef2c2016-10-28 19:01:21 -04001766 PyDictObject *mp;
1767
1768 assert(PyDict_Check(dict));
1769 mp = (PyDictObject *)dict;
Eric Snow96c6af92015-05-29 22:21:39 -06001770
1771 if (mp->ma_used == 0) {
1772 if (deflt) {
1773 Py_INCREF(deflt);
1774 return deflt;
1775 }
1776 _PyErr_SetKeyError(key);
1777 return NULL;
1778 }
1779 if (!PyUnicode_CheckExact(key) ||
1780 (hash = ((PyASCIIObject *) key)->hash) == -1) {
1781 hash = PyObject_Hash(key);
1782 if (hash == -1)
1783 return NULL;
1784 }
Victor Stinner742da042016-09-07 17:40:12 -07001785 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
1786 if (ix == DKIX_ERROR)
Eric Snow96c6af92015-05-29 22:21:39 -06001787 return NULL;
Victor Stinnerd0ad11f2016-09-13 16:56:38 +02001788 if (ix == DKIX_EMPTY || *value_addr == NULL) {
Eric Snow96c6af92015-05-29 22:21:39 -06001789 if (deflt) {
1790 Py_INCREF(deflt);
1791 return deflt;
1792 }
1793 _PyErr_SetKeyError(key);
1794 return NULL;
1795 }
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001796
Victor Stinner78601a32016-09-09 19:28:36 -07001797 // Split table doesn't allow deletion. Combine it.
1798 if (_PyDict_HasSplitTable(mp)) {
1799 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1800 return NULL;
1801 }
1802 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
1803 assert(ix >= 0);
1804 }
1805
Victor Stinner742da042016-09-07 17:40:12 -07001806 old_value = *value_addr;
Victor Stinner78601a32016-09-09 19:28:36 -07001807 assert(old_value != NULL);
Eric Snow96c6af92015-05-29 22:21:39 -06001808 *value_addr = NULL;
1809 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001810 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner78601a32016-09-09 19:28:36 -07001811 dk_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
1812 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1813 ENSURE_ALLOWS_DELETIONS(mp);
1814 old_key = ep->me_key;
1815 ep->me_key = NULL;
1816 Py_DECREF(old_key);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001817
1818 assert(_PyDict_CheckConsistency(mp));
Eric Snow96c6af92015-05-29 22:21:39 -06001819 return old_value;
1820}
1821
1822/* Internal version of dict.from_keys(). It is subclass-friendly. */
1823PyObject *
1824_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1825{
1826 PyObject *it; /* iter(iterable) */
1827 PyObject *key;
1828 PyObject *d;
1829 int status;
1830
1831 d = PyObject_CallObject(cls, NULL);
1832 if (d == NULL)
1833 return NULL;
1834
1835 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
1836 if (PyDict_CheckExact(iterable)) {
1837 PyDictObject *mp = (PyDictObject *)d;
1838 PyObject *oldvalue;
1839 Py_ssize_t pos = 0;
1840 PyObject *key;
1841 Py_hash_t hash;
1842
Victor Stinner742da042016-09-07 17:40:12 -07001843 if (dictresize(mp, ESTIMATE_SIZE(Py_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001844 Py_DECREF(d);
1845 return NULL;
1846 }
1847
1848 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
1849 if (insertdict(mp, key, hash, value)) {
1850 Py_DECREF(d);
1851 return NULL;
1852 }
1853 }
1854 return d;
1855 }
1856 if (PyAnySet_CheckExact(iterable)) {
1857 PyDictObject *mp = (PyDictObject *)d;
1858 Py_ssize_t pos = 0;
1859 PyObject *key;
1860 Py_hash_t hash;
1861
Victor Stinner742da042016-09-07 17:40:12 -07001862 if (dictresize(mp, ESTIMATE_SIZE(PySet_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001863 Py_DECREF(d);
1864 return NULL;
1865 }
1866
1867 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
1868 if (insertdict(mp, key, hash, value)) {
1869 Py_DECREF(d);
1870 return NULL;
1871 }
1872 }
1873 return d;
1874 }
1875 }
1876
1877 it = PyObject_GetIter(iterable);
1878 if (it == NULL){
1879 Py_DECREF(d);
1880 return NULL;
1881 }
1882
1883 if (PyDict_CheckExact(d)) {
1884 while ((key = PyIter_Next(it)) != NULL) {
1885 status = PyDict_SetItem(d, key, value);
1886 Py_DECREF(key);
1887 if (status < 0)
1888 goto Fail;
1889 }
1890 } else {
1891 while ((key = PyIter_Next(it)) != NULL) {
1892 status = PyObject_SetItem(d, key, value);
1893 Py_DECREF(key);
1894 if (status < 0)
1895 goto Fail;
1896 }
1897 }
1898
1899 if (PyErr_Occurred())
1900 goto Fail;
1901 Py_DECREF(it);
1902 return d;
1903
1904Fail:
1905 Py_DECREF(it);
1906 Py_DECREF(d);
1907 return NULL;
1908}
1909
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001910/* Methods */
1911
1912static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001913dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001914{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001915 PyObject **values = mp->ma_values;
1916 PyDictKeysObject *keys = mp->ma_keys;
1917 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 PyObject_GC_UnTrack(mp);
1919 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001920 if (values != NULL) {
1921 if (values != empty_values) {
Victor Stinner742da042016-09-07 17:40:12 -07001922 for (i = 0, n = mp->ma_keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001923 Py_XDECREF(values[i]);
1924 }
1925 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001927 DK_DECREF(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02001929 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001930 assert(keys->dk_refcnt == 1);
1931 DK_DECREF(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001932 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001933 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1934 free_list[numfree++] = mp;
1935 else
1936 Py_TYPE(mp)->tp_free((PyObject *)mp);
1937 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001938}
1939
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001940
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001941static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001942dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001943{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001944 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001945 PyObject *key = NULL, *value = NULL;
1946 _PyUnicodeWriter writer;
1947 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00001948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 i = Py_ReprEnter((PyObject *)mp);
1950 if (i != 0) {
1951 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1952 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001954 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001955 Py_ReprLeave((PyObject *)mp);
1956 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001957 }
Tim Petersa7259592001-06-16 05:11:17 +00001958
Victor Stinnerf91929b2013-11-19 13:07:38 +01001959 _PyUnicodeWriter_Init(&writer);
1960 writer.overallocate = 1;
1961 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
1962 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00001963
Victor Stinnerf91929b2013-11-19 13:07:38 +01001964 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
1965 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001966
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 /* Do repr() on each key+value pair, and insert ": " between them.
1968 Note that repr may mutate the dict. */
1969 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001970 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001971 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001972 PyObject *s;
1973 int res;
1974
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001975 /* Prevent repr from deleting key or value during key format. */
1976 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001977 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02001978
Victor Stinnerf91929b2013-11-19 13:07:38 +01001979 if (!first) {
1980 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
1981 goto error;
1982 }
1983 first = 0;
1984
1985 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001986 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01001987 goto error;
1988 res = _PyUnicodeWriter_WriteStr(&writer, s);
1989 Py_DECREF(s);
1990 if (res < 0)
1991 goto error;
1992
1993 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
1994 goto error;
1995
1996 s = PyObject_Repr(value);
1997 if (s == NULL)
1998 goto error;
1999 res = _PyUnicodeWriter_WriteStr(&writer, s);
2000 Py_DECREF(s);
2001 if (res < 0)
2002 goto error;
2003
2004 Py_CLEAR(key);
2005 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 }
Tim Petersa7259592001-06-16 05:11:17 +00002007
Victor Stinnerf91929b2013-11-19 13:07:38 +01002008 writer.overallocate = 0;
2009 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
2010 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00002011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002012 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01002013
2014 return _PyUnicodeWriter_Finish(&writer);
2015
2016error:
2017 Py_ReprLeave((PyObject *)mp);
2018 _PyUnicodeWriter_Dealloc(&writer);
2019 Py_XDECREF(key);
2020 Py_XDECREF(value);
2021 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002022}
2023
Martin v. Löwis18e16552006-02-15 17:27:45 +00002024static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002025dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002026{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002028}
2029
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002030static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002031dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002032{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002033 PyObject *v;
Victor Stinner742da042016-09-07 17:40:12 -07002034 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002035 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002036 PyObject **value_addr;
2037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002038 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002039 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002040 hash = PyObject_Hash(key);
2041 if (hash == -1)
2042 return NULL;
2043 }
Victor Stinner742da042016-09-07 17:40:12 -07002044 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
2045 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002046 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002047 if (ix == DKIX_EMPTY || *value_addr == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048 if (!PyDict_CheckExact(mp)) {
2049 /* Look up __missing__ method if we're a subclass. */
2050 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05002051 _Py_IDENTIFIER(__missing__);
2052 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 if (missing != NULL) {
2054 res = PyObject_CallFunctionObjArgs(missing,
2055 key, NULL);
2056 Py_DECREF(missing);
2057 return res;
2058 }
2059 else if (PyErr_Occurred())
2060 return NULL;
2061 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07002062 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002063 return NULL;
2064 }
Victor Stinner742da042016-09-07 17:40:12 -07002065 v = *value_addr;
2066 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002068}
2069
2070static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002071dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002072{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002073 if (w == NULL)
2074 return PyDict_DelItem((PyObject *)mp, v);
2075 else
2076 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002077}
2078
Guido van Rossuma9e7a811997-05-13 21:02:11 +00002079static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002080 (lenfunc)dict_length, /*mp_length*/
2081 (binaryfunc)dict_subscript, /*mp_subscript*/
2082 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002083};
2084
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002085static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002086dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002087{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002088 PyObject *v;
2089 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002090 PyDictKeyEntry *ep;
2091 Py_ssize_t size, n, offset;
2092 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002093
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002094 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002095 n = mp->ma_used;
2096 v = PyList_New(n);
2097 if (v == NULL)
2098 return NULL;
2099 if (n != mp->ma_used) {
2100 /* Durnit. The allocations caused the dict to resize.
2101 * Just start over, this shouldn't normally happen.
2102 */
2103 Py_DECREF(v);
2104 goto again;
2105 }
Victor Stinner742da042016-09-07 17:40:12 -07002106 ep = DK_ENTRIES(mp->ma_keys);
2107 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002108 if (mp->ma_values) {
2109 value_ptr = mp->ma_values;
2110 offset = sizeof(PyObject *);
2111 }
2112 else {
2113 value_ptr = &ep[0].me_value;
2114 offset = sizeof(PyDictKeyEntry);
2115 }
2116 for (i = 0, j = 0; i < size; i++) {
2117 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002118 PyObject *key = ep[i].me_key;
2119 Py_INCREF(key);
2120 PyList_SET_ITEM(v, j, key);
2121 j++;
2122 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002123 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002124 }
2125 assert(j == n);
2126 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002127}
2128
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002129static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002130dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002131{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002132 PyObject *v;
2133 Py_ssize_t i, j;
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002134 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002135 Py_ssize_t size, n, offset;
2136 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002137
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002138 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002139 n = mp->ma_used;
2140 v = PyList_New(n);
2141 if (v == NULL)
2142 return NULL;
2143 if (n != mp->ma_used) {
2144 /* Durnit. The allocations caused the dict to resize.
2145 * Just start over, this shouldn't normally happen.
2146 */
2147 Py_DECREF(v);
2148 goto again;
2149 }
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002150 ep = DK_ENTRIES(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002151 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002152 if (mp->ma_values) {
2153 value_ptr = mp->ma_values;
2154 offset = sizeof(PyObject *);
2155 }
2156 else {
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002157 value_ptr = &ep[0].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002158 offset = sizeof(PyDictKeyEntry);
2159 }
2160 for (i = 0, j = 0; i < size; i++) {
2161 PyObject *value = *value_ptr;
2162 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2163 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002164 Py_INCREF(value);
2165 PyList_SET_ITEM(v, j, value);
2166 j++;
2167 }
2168 }
2169 assert(j == n);
2170 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002171}
2172
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002173static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002174dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002175{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002176 PyObject *v;
2177 Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002178 Py_ssize_t size, offset;
2179 PyObject *item, *key;
2180 PyDictKeyEntry *ep;
2181 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002183 /* Preallocate the list of tuples, to avoid allocations during
2184 * the loop over the items, which could trigger GC, which
2185 * could resize the dict. :-(
2186 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002187 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002188 n = mp->ma_used;
2189 v = PyList_New(n);
2190 if (v == NULL)
2191 return NULL;
2192 for (i = 0; i < n; i++) {
2193 item = PyTuple_New(2);
2194 if (item == NULL) {
2195 Py_DECREF(v);
2196 return NULL;
2197 }
2198 PyList_SET_ITEM(v, i, item);
2199 }
2200 if (n != mp->ma_used) {
2201 /* Durnit. The allocations caused the dict to resize.
2202 * Just start over, this shouldn't normally happen.
2203 */
2204 Py_DECREF(v);
2205 goto again;
2206 }
2207 /* Nothing we do below makes any function calls. */
Victor Stinner742da042016-09-07 17:40:12 -07002208 ep = DK_ENTRIES(mp->ma_keys);
2209 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002210 if (mp->ma_values) {
2211 value_ptr = mp->ma_values;
2212 offset = sizeof(PyObject *);
2213 }
2214 else {
2215 value_ptr = &ep[0].me_value;
2216 offset = sizeof(PyDictKeyEntry);
2217 }
2218 for (i = 0, j = 0; i < size; i++) {
2219 PyObject *value = *value_ptr;
2220 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2221 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002222 key = ep[i].me_key;
2223 item = PyList_GET_ITEM(v, j);
2224 Py_INCREF(key);
2225 PyTuple_SET_ITEM(item, 0, key);
2226 Py_INCREF(value);
2227 PyTuple_SET_ITEM(item, 1, value);
2228 j++;
2229 }
2230 }
2231 assert(j == n);
2232 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002233}
2234
Larry Hastings5c661892014-01-24 06:17:25 -08002235/*[clinic input]
2236@classmethod
2237dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08002238 iterable: object
2239 value: object=None
2240 /
2241
2242Returns a new dict with keys from iterable and values equal to value.
2243[clinic start generated code]*/
2244
Larry Hastings5c661892014-01-24 06:17:25 -08002245static PyObject *
2246dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002247/*[clinic end generated code: output=8fb98e4b10384999 input=b85a667f9bf4669d]*/
Larry Hastings5c661892014-01-24 06:17:25 -08002248{
Eric Snow96c6af92015-05-29 22:21:39 -06002249 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002250}
2251
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002252static int
Victor Stinner742da042016-09-07 17:40:12 -07002253dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
2254 const char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002255{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002256 PyObject *arg = NULL;
2257 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002259 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
2260 result = -1;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002263 _Py_IDENTIFIER(keys);
2264 if (_PyObject_HasAttrId(arg, &PyId_keys))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265 result = PyDict_Merge(self, arg, 1);
2266 else
2267 result = PyDict_MergeFromSeq2(self, arg, 1);
2268 }
2269 if (result == 0 && kwds != NULL) {
2270 if (PyArg_ValidateKeywordArguments(kwds))
2271 result = PyDict_Merge(self, kwds, 1);
2272 else
2273 result = -1;
2274 }
2275 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002276}
2277
2278static PyObject *
2279dict_update(PyObject *self, PyObject *args, PyObject *kwds)
2280{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 if (dict_update_common(self, args, kwds, "update") != -1)
2282 Py_RETURN_NONE;
2283 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002284}
2285
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002286/* Update unconditionally replaces existing items.
2287 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00002288 otherwise it leaves existing items unchanged.
2289
2290 PyDict_{Update,Merge} update/merge from a mapping object.
2291
Tim Petersf582b822001-12-11 18:51:08 +00002292 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00002293 producing iterable objects of length 2.
2294*/
2295
Tim Petersf582b822001-12-11 18:51:08 +00002296int
Tim Peters1fc240e2001-10-26 05:06:50 +00002297PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
2298{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002299 PyObject *it; /* iter(seq2) */
2300 Py_ssize_t i; /* index into seq2 of current element */
2301 PyObject *item; /* seq2[i] */
2302 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00002303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002304 assert(d != NULL);
2305 assert(PyDict_Check(d));
2306 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00002307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002308 it = PyObject_GetIter(seq2);
2309 if (it == NULL)
2310 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002312 for (i = 0; ; ++i) {
2313 PyObject *key, *value;
2314 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00002315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002316 fast = NULL;
2317 item = PyIter_Next(it);
2318 if (item == NULL) {
2319 if (PyErr_Occurred())
2320 goto Fail;
2321 break;
2322 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002324 /* Convert item to sequence, and verify length 2. */
2325 fast = PySequence_Fast(item, "");
2326 if (fast == NULL) {
2327 if (PyErr_ExceptionMatches(PyExc_TypeError))
2328 PyErr_Format(PyExc_TypeError,
2329 "cannot convert dictionary update "
2330 "sequence element #%zd to a sequence",
2331 i);
2332 goto Fail;
2333 }
2334 n = PySequence_Fast_GET_SIZE(fast);
2335 if (n != 2) {
2336 PyErr_Format(PyExc_ValueError,
2337 "dictionary update sequence element #%zd "
2338 "has length %zd; 2 is required",
2339 i, n);
2340 goto Fail;
2341 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002343 /* Update/merge with this (key, value) pair. */
2344 key = PySequence_Fast_GET_ITEM(fast, 0);
2345 value = PySequence_Fast_GET_ITEM(fast, 1);
2346 if (override || PyDict_GetItem(d, key) == NULL) {
2347 int status = PyDict_SetItem(d, key, value);
2348 if (status < 0)
2349 goto Fail;
2350 }
2351 Py_DECREF(fast);
2352 Py_DECREF(item);
2353 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002355 i = 0;
Victor Stinner611b0fa2016-09-14 15:02:01 +02002356 assert(_PyDict_CheckConsistency((PyDictObject *)d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002358Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359 Py_XDECREF(item);
2360 Py_XDECREF(fast);
2361 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002362Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002363 Py_DECREF(it);
2364 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002365}
2366
doko@ubuntu.comde69ee72016-10-11 08:04:02 +02002367static int
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002368dict_merge(PyObject *a, PyObject *b, int override)
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002369{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002370 PyDictObject *mp, *other;
2371 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -07002372 PyDictKeyEntry *entry, *ep0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002373
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002374 assert(0 <= override && override <= 2);
2375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 /* We accept for the argument either a concrete dictionary object,
2377 * or an abstract "mapping" object. For the former, we can do
2378 * things quite efficiently. For the latter, we only require that
2379 * PyMapping_Keys() and PyObject_GetItem() be supported.
2380 */
2381 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2382 PyErr_BadInternalCall();
2383 return -1;
2384 }
2385 mp = (PyDictObject*)a;
2386 if (PyDict_Check(b)) {
2387 other = (PyDictObject*)b;
2388 if (other == mp || other->ma_used == 0)
2389 /* a.update(a) or a.update({}); nothing to do */
2390 return 0;
2391 if (mp->ma_used == 0)
2392 /* Since the target dict is empty, PyDict_GetItem()
2393 * always returns NULL. Setting override to 1
2394 * skips the unnecessary test.
2395 */
2396 override = 1;
2397 /* Do one big resize at the start, rather than
2398 * incrementally resizing as we insert new items. Expect
2399 * that there will be no (or few) overlapping keys.
2400 */
INADA Naokib1152be2016-10-27 19:26:50 +09002401 if (USABLE_FRACTION(mp->ma_keys->dk_size) < other->ma_used) {
2402 if (dictresize(mp, ESTIMATE_SIZE(mp->ma_used + other->ma_used))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002403 return -1;
INADA Naokib1152be2016-10-27 19:26:50 +09002404 }
2405 }
Victor Stinner742da042016-09-07 17:40:12 -07002406 ep0 = DK_ENTRIES(other->ma_keys);
2407 for (i = 0, n = other->ma_keys->dk_nentries; i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002408 PyObject *key, *value;
2409 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002410 entry = &ep0[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002411 key = entry->me_key;
2412 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002413 if (other->ma_values)
2414 value = other->ma_values[i];
2415 else
2416 value = entry->me_value;
2417
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002418 if (value != NULL) {
2419 int err = 0;
2420 Py_INCREF(key);
2421 Py_INCREF(value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002422 if (override == 1)
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002423 err = insertdict(mp, key, hash, value);
Serhiy Storchakaf0b311b2016-11-06 13:18:24 +02002424 else if (_PyDict_GetItem_KnownHash(a, key, hash) == NULL) {
2425 if (PyErr_Occurred()) {
2426 Py_DECREF(value);
2427 Py_DECREF(key);
2428 return -1;
2429 }
2430 err = insertdict(mp, key, hash, value);
2431 }
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002432 else if (override != 0) {
2433 _PyErr_SetKeyError(key);
2434 Py_DECREF(value);
2435 Py_DECREF(key);
2436 return -1;
2437 }
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002438 Py_DECREF(value);
2439 Py_DECREF(key);
2440 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002441 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002442
Victor Stinner742da042016-09-07 17:40:12 -07002443 if (n != other->ma_keys->dk_nentries) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002444 PyErr_SetString(PyExc_RuntimeError,
2445 "dict mutated during update");
2446 return -1;
2447 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002448 }
2449 }
2450 }
2451 else {
2452 /* Do it the generic, slower way */
2453 PyObject *keys = PyMapping_Keys(b);
2454 PyObject *iter;
2455 PyObject *key, *value;
2456 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002458 if (keys == NULL)
2459 /* Docstring says this is equivalent to E.keys() so
2460 * if E doesn't have a .keys() method we want
2461 * AttributeError to percolate up. Might as well
2462 * do the same for any other error.
2463 */
2464 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002466 iter = PyObject_GetIter(keys);
2467 Py_DECREF(keys);
2468 if (iter == NULL)
2469 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002471 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002472 if (override != 1 && PyDict_GetItem(a, key) != NULL) {
2473 if (override != 0) {
2474 _PyErr_SetKeyError(key);
2475 Py_DECREF(key);
2476 Py_DECREF(iter);
2477 return -1;
2478 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002479 Py_DECREF(key);
2480 continue;
2481 }
2482 value = PyObject_GetItem(b, key);
2483 if (value == NULL) {
2484 Py_DECREF(iter);
2485 Py_DECREF(key);
2486 return -1;
2487 }
2488 status = PyDict_SetItem(a, key, value);
2489 Py_DECREF(key);
2490 Py_DECREF(value);
2491 if (status < 0) {
2492 Py_DECREF(iter);
2493 return -1;
2494 }
2495 }
2496 Py_DECREF(iter);
2497 if (PyErr_Occurred())
2498 /* Iterator completed, via error */
2499 return -1;
2500 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02002501 assert(_PyDict_CheckConsistency((PyDictObject *)a));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002502 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002503}
2504
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002505int
2506PyDict_Update(PyObject *a, PyObject *b)
2507{
2508 return dict_merge(a, b, 1);
2509}
2510
2511int
2512PyDict_Merge(PyObject *a, PyObject *b, int override)
2513{
2514 /* XXX Deprecate override not in (0, 1). */
2515 return dict_merge(a, b, override != 0);
2516}
2517
2518int
2519_PyDict_MergeEx(PyObject *a, PyObject *b, int override)
2520{
2521 return dict_merge(a, b, override);
2522}
2523
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002524static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002525dict_copy(PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002526{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002527 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002528}
2529
2530PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002531PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002532{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002533 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002534 PyDictObject *mp;
2535 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002537 if (o == NULL || !PyDict_Check(o)) {
2538 PyErr_BadInternalCall();
2539 return NULL;
2540 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002541 mp = (PyDictObject *)o;
2542 if (_PyDict_HasSplitTable(mp)) {
2543 PyDictObject *split_copy;
Victor Stinner742da042016-09-07 17:40:12 -07002544 Py_ssize_t size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
2545 PyObject **newvalues;
2546 newvalues = new_values(size);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002547 if (newvalues == NULL)
2548 return PyErr_NoMemory();
2549 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2550 if (split_copy == NULL) {
2551 free_values(newvalues);
2552 return NULL;
2553 }
2554 split_copy->ma_values = newvalues;
2555 split_copy->ma_keys = mp->ma_keys;
2556 split_copy->ma_used = mp->ma_used;
2557 DK_INCREF(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002558 for (i = 0, n = size; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002559 PyObject *value = mp->ma_values[i];
2560 Py_XINCREF(value);
2561 split_copy->ma_values[i] = value;
2562 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002563 if (_PyObject_GC_IS_TRACKED(mp))
2564 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002565 return (PyObject *)split_copy;
2566 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002567 copy = PyDict_New();
2568 if (copy == NULL)
2569 return NULL;
2570 if (PyDict_Merge(copy, o, 1) == 0)
2571 return copy;
2572 Py_DECREF(copy);
2573 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002574}
2575
Martin v. Löwis18e16552006-02-15 17:27:45 +00002576Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002577PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002578{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002579 if (mp == NULL || !PyDict_Check(mp)) {
2580 PyErr_BadInternalCall();
2581 return -1;
2582 }
2583 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002584}
2585
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002586PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002587PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002588{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002589 if (mp == NULL || !PyDict_Check(mp)) {
2590 PyErr_BadInternalCall();
2591 return NULL;
2592 }
2593 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002594}
2595
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002596PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002597PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002598{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002599 if (mp == NULL || !PyDict_Check(mp)) {
2600 PyErr_BadInternalCall();
2601 return NULL;
2602 }
2603 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002604}
2605
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002606PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002607PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002608{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002609 if (mp == NULL || !PyDict_Check(mp)) {
2610 PyErr_BadInternalCall();
2611 return NULL;
2612 }
2613 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002614}
2615
Tim Peterse63415e2001-05-08 04:38:29 +00002616/* Return 1 if dicts equal, 0 if not, -1 if error.
2617 * Gets out as soon as any difference is detected.
2618 * Uses only Py_EQ comparison.
2619 */
2620static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002621dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002622{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002623 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002625 if (a->ma_used != b->ma_used)
2626 /* can't be equal if # of entries differ */
2627 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002628 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Victor Stinner742da042016-09-07 17:40:12 -07002629 for (i = 0; i < a->ma_keys->dk_nentries; i++) {
2630 PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002631 PyObject *aval;
2632 if (a->ma_values)
2633 aval = a->ma_values[i];
2634 else
2635 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002636 if (aval != NULL) {
2637 int cmp;
2638 PyObject *bval;
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002639 PyObject **vaddr;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002640 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002641 /* temporarily bump aval's refcount to ensure it stays
2642 alive until we're done with it */
2643 Py_INCREF(aval);
2644 /* ditto for key */
2645 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002646 /* reuse the known hash value */
Victor Stinner742da042016-09-07 17:40:12 -07002647 if ((b->ma_keys->dk_lookup)(b, key, ep->me_hash, &vaddr, NULL) < 0)
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002648 bval = NULL;
2649 else
2650 bval = *vaddr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002651 Py_DECREF(key);
2652 if (bval == NULL) {
2653 Py_DECREF(aval);
2654 if (PyErr_Occurred())
2655 return -1;
2656 return 0;
2657 }
2658 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
2659 Py_DECREF(aval);
2660 if (cmp <= 0) /* error or not equal */
2661 return cmp;
2662 }
2663 }
2664 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002665}
Tim Peterse63415e2001-05-08 04:38:29 +00002666
2667static PyObject *
2668dict_richcompare(PyObject *v, PyObject *w, int op)
2669{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002670 int cmp;
2671 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002673 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2674 res = Py_NotImplemented;
2675 }
2676 else if (op == Py_EQ || op == Py_NE) {
2677 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2678 if (cmp < 0)
2679 return NULL;
2680 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2681 }
2682 else
2683 res = Py_NotImplemented;
2684 Py_INCREF(res);
2685 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002686}
Tim Peterse63415e2001-05-08 04:38:29 +00002687
Larry Hastings61272b72014-01-07 12:41:53 -08002688/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002689
2690@coexist
2691dict.__contains__
2692
2693 key: object
2694 /
2695
Meador Ingee02de8c2014-01-14 16:48:31 -06002696True if D has a key k, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002697[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002698
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002699static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002700dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002701/*[clinic end generated code: output=a3d03db709ed6e6b input=b852b2a19b51ab24]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002702{
Larry Hastingsc2047262014-01-25 20:43:29 -08002703 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002704 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002705 Py_ssize_t ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002706 PyObject **value_addr;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002708 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002709 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002710 hash = PyObject_Hash(key);
2711 if (hash == -1)
2712 return NULL;
2713 }
Victor Stinner742da042016-09-07 17:40:12 -07002714 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
2715 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002716 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002717 if (ix == DKIX_EMPTY || *value_addr == NULL)
2718 Py_RETURN_FALSE;
2719 Py_RETURN_TRUE;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002720}
2721
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002722static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002723dict_get(PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00002724{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002725 PyObject *key;
2726 PyObject *failobj = Py_None;
2727 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002728 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002729 Py_ssize_t ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002730 PyObject **value_addr;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002732 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
2733 return NULL;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002734
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002735 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002736 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002737 hash = PyObject_Hash(key);
2738 if (hash == -1)
2739 return NULL;
2740 }
Victor Stinner742da042016-09-07 17:40:12 -07002741 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
2742 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002743 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002744 if (ix == DKIX_EMPTY || *value_addr == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002745 val = failobj;
Victor Stinner742da042016-09-07 17:40:12 -07002746 else
2747 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002748 Py_INCREF(val);
2749 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002750}
2751
Benjamin Peterson00e98862013-03-07 22:16:29 -05002752PyObject *
2753PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002754{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002755 PyDictObject *mp = (PyDictObject *)d;
INADA Naoki93f26f72016-11-02 18:45:16 +09002756 PyObject *value;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002757 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002758 Py_ssize_t hashpos, ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002759 PyObject **value_addr;
Guido van Rossum164452c2000-08-08 16:12:54 +00002760
Benjamin Peterson00e98862013-03-07 22:16:29 -05002761 if (!PyDict_Check(d)) {
2762 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002763 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002764 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002766 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002767 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002768 hash = PyObject_Hash(key);
2769 if (hash == -1)
2770 return NULL;
2771 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002772
2773 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
2774 if (insertion_resize(mp) < 0)
2775 return NULL;
2776 }
2777
Victor Stinner742da042016-09-07 17:40:12 -07002778 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
2779 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002780 return NULL;
INADA Naoki93f26f72016-11-02 18:45:16 +09002781
2782 if (_PyDict_HasSplitTable(mp) &&
2783 ((ix >= 0 && *value_addr == NULL && mp->ma_used != ix) ||
2784 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
2785 if (insertion_resize(mp) < 0) {
2786 return NULL;
2787 }
2788 find_empty_slot(mp, key, hash, &value_addr, &hashpos);
2789 ix = DKIX_EMPTY;
2790 }
2791
2792 if (ix == DKIX_EMPTY) {
2793 PyDictKeyEntry *ep, *ep0;
2794 value = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002795 if (mp->ma_keys->dk_usable <= 0) {
Victor Stinner3c336c52016-09-12 14:17:40 +02002796 if (insertion_resize(mp) < 0) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002797 return NULL;
Victor Stinner3c336c52016-09-12 14:17:40 +02002798 }
Victor Stinner742da042016-09-07 17:40:12 -07002799 find_empty_slot(mp, key, hash, &value_addr, &hashpos);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002800 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002801 ep0 = DK_ENTRIES(mp->ma_keys);
2802 ep = &ep0[mp->ma_keys->dk_nentries];
2803 dk_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002804 Py_INCREF(key);
INADA Naoki93f26f72016-11-02 18:45:16 +09002805 Py_INCREF(value);
2806 MAINTAIN_TRACKING(mp, key, value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002807 ep->me_key = key;
2808 ep->me_hash = hash;
Victor Stinner742da042016-09-07 17:40:12 -07002809 if (mp->ma_values) {
INADA Naoki93f26f72016-11-02 18:45:16 +09002810 assert(mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
2811 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Victor Stinner742da042016-09-07 17:40:12 -07002812 }
2813 else {
INADA Naoki93f26f72016-11-02 18:45:16 +09002814 ep->me_value = value;
Victor Stinner742da042016-09-07 17:40:12 -07002815 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002816 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002817 mp->ma_version_tag = DICT_NEXT_VERSION();
INADA Naoki93f26f72016-11-02 18:45:16 +09002818 mp->ma_keys->dk_usable--;
2819 mp->ma_keys->dk_nentries++;
2820 assert(mp->ma_keys->dk_usable >= 0);
2821 }
2822 else if (*value_addr == NULL) {
2823 value = defaultobj;
2824 assert(_PyDict_HasSplitTable(mp));
2825 assert(ix == mp->ma_used);
2826 Py_INCREF(value);
2827 MAINTAIN_TRACKING(mp, key, value);
2828 *value_addr = value;
2829 mp->ma_used++;
2830 mp->ma_version_tag = DICT_NEXT_VERSION();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002831 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02002832 else {
INADA Naoki93f26f72016-11-02 18:45:16 +09002833 value = *value_addr;
Victor Stinner611b0fa2016-09-14 15:02:01 +02002834 }
INADA Naoki93f26f72016-11-02 18:45:16 +09002835
2836 assert(_PyDict_CheckConsistency(mp));
2837 return value;
Guido van Rossum164452c2000-08-08 16:12:54 +00002838}
2839
Benjamin Peterson00e98862013-03-07 22:16:29 -05002840static PyObject *
2841dict_setdefault(PyDictObject *mp, PyObject *args)
2842{
2843 PyObject *key, *val;
2844 PyObject *defaultobj = Py_None;
2845
2846 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &defaultobj))
2847 return NULL;
2848
Benjamin Peterson55898502013-03-08 08:36:49 -05002849 val = PyDict_SetDefault((PyObject *)mp, key, defaultobj);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002850 Py_XINCREF(val);
2851 return val;
2852}
Guido van Rossum164452c2000-08-08 16:12:54 +00002853
2854static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002855dict_clear(PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002856{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002857 PyDict_Clear((PyObject *)mp);
2858 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002859}
2860
Guido van Rossumba6ab842000-12-12 22:02:18 +00002861static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002862dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002863{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002864 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00002865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002866 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2867 return NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06002868
Yury Selivanov684ef2c2016-10-28 19:01:21 -04002869 return _PyDict_Pop((PyObject*)mp, key, deflt);
Guido van Rossume027d982002-04-12 15:11:59 +00002870}
2871
2872static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002873dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002874{
Victor Stinner742da042016-09-07 17:40:12 -07002875 Py_ssize_t i, j;
2876 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002877 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 /* Allocate the result tuple before checking the size. Believe it
2880 * or not, this allocation could trigger a garbage collection which
2881 * could empty the dict, so if we checked the size first and that
2882 * happened, the result would be an infinite loop (searching for an
2883 * entry that no longer exists). Note that the usual popitem()
2884 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2885 * tuple away if the dict *is* empty isn't a significant
2886 * inefficiency -- possible, but unlikely in practice.
2887 */
2888 res = PyTuple_New(2);
2889 if (res == NULL)
2890 return NULL;
2891 if (mp->ma_used == 0) {
2892 Py_DECREF(res);
2893 PyErr_SetString(PyExc_KeyError,
2894 "popitem(): dictionary is empty");
2895 return NULL;
2896 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002897 /* Convert split table to combined table */
2898 if (mp->ma_keys->dk_lookup == lookdict_split) {
2899 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2900 Py_DECREF(res);
2901 return NULL;
2902 }
2903 }
2904 ENSURE_ALLOWS_DELETIONS(mp);
Victor Stinner742da042016-09-07 17:40:12 -07002905
2906 /* Pop last item */
2907 ep0 = DK_ENTRIES(mp->ma_keys);
2908 i = mp->ma_keys->dk_nentries - 1;
2909 while (i >= 0 && ep0[i].me_value == NULL) {
2910 i--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002911 }
Victor Stinner742da042016-09-07 17:40:12 -07002912 assert(i >= 0);
2913
2914 ep = &ep0[i];
2915 j = lookdict_index(mp->ma_keys, ep->me_hash, i);
2916 assert(j >= 0);
2917 assert(dk_get_index(mp->ma_keys, j) == i);
2918 dk_set_index(mp->ma_keys, j, DKIX_DUMMY);
2919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002920 PyTuple_SET_ITEM(res, 0, ep->me_key);
2921 PyTuple_SET_ITEM(res, 1, ep->me_value);
Victor Stinner742da042016-09-07 17:40:12 -07002922 ep->me_key = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002923 ep->me_value = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002924 /* We can't dk_usable++ since there is DKIX_DUMMY in indices */
2925 mp->ma_keys->dk_nentries = i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002926 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002927 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02002928 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002929 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002930}
2931
Jeremy Hylton8caad492000-06-23 14:18:11 +00002932static int
2933dict_traverse(PyObject *op, visitproc visit, void *arg)
2934{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002935 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson55f44522016-09-05 12:12:59 -07002936 PyDictKeysObject *keys = mp->ma_keys;
Serhiy Storchaka46825d22016-09-26 21:29:34 +03002937 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Victor Stinner742da042016-09-07 17:40:12 -07002938 Py_ssize_t i, n = keys->dk_nentries;
2939
Benjamin Peterson55f44522016-09-05 12:12:59 -07002940 if (keys->dk_lookup == lookdict) {
2941 for (i = 0; i < n; i++) {
2942 if (entries[i].me_value != NULL) {
2943 Py_VISIT(entries[i].me_value);
2944 Py_VISIT(entries[i].me_key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002945 }
2946 }
Victor Stinner742da042016-09-07 17:40:12 -07002947 }
2948 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002949 if (mp->ma_values != NULL) {
Benjamin Peterson55f44522016-09-05 12:12:59 -07002950 for (i = 0; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002951 Py_VISIT(mp->ma_values[i]);
2952 }
2953 }
2954 else {
Benjamin Peterson55f44522016-09-05 12:12:59 -07002955 for (i = 0; i < n; i++) {
2956 Py_VISIT(entries[i].me_value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002957 }
2958 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002959 }
2960 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002961}
2962
2963static int
2964dict_tp_clear(PyObject *op)
2965{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002966 PyDict_Clear(op);
2967 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002968}
2969
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002970static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002971
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002972Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06002973_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002974{
Victor Stinner742da042016-09-07 17:40:12 -07002975 Py_ssize_t size, usable, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002976
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002977 size = DK_SIZE(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002978 usable = USABLE_FRACTION(size);
2979
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002980 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002981 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -07002982 res += usable * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002983 /* If the dictionary is split, the keys portion is accounted-for
2984 in the type object. */
2985 if (mp->ma_keys->dk_refcnt == 1)
Victor Stinner98ee9d52016-09-08 09:33:56 -07002986 res += (sizeof(PyDictKeysObject)
2987 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
2988 + DK_IXSIZE(mp->ma_keys) * size
2989 + sizeof(PyDictKeyEntry) * usable);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002990 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002991}
2992
2993Py_ssize_t
2994_PyDict_KeysSize(PyDictKeysObject *keys)
2995{
Victor Stinner98ee9d52016-09-08 09:33:56 -07002996 return (sizeof(PyDictKeysObject)
2997 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
2998 + DK_IXSIZE(keys) * DK_SIZE(keys)
2999 + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003000}
3001
doko@ubuntu.com17210f52016-01-14 14:04:59 +01003002static PyObject *
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003003dict_sizeof(PyDictObject *mp)
3004{
3005 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
3006}
3007
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00003008PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
3009
Martin v. Löwis00709aa2008-06-04 14:18:43 +00003010PyDoc_STRVAR(sizeof__doc__,
3011"D.__sizeof__() -> size of D in memory, in bytes");
3012
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003013PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00003014"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003015
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003016PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00003017"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 +00003018
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003019PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00003020"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00003021If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00003022
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003023PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00003024"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000030252-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003026
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003027PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04003028"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
3029If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
3030If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
3031In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00003032
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003033PyDoc_STRVAR(clear__doc__,
3034"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003035
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003036PyDoc_STRVAR(copy__doc__,
3037"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00003038
Guido van Rossumb90c8482007-02-10 01:11:45 +00003039/* Forward */
3040static PyObject *dictkeys_new(PyObject *);
3041static PyObject *dictitems_new(PyObject *);
3042static PyObject *dictvalues_new(PyObject *);
3043
Guido van Rossum45c85d12007-07-27 16:31:40 +00003044PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003045 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003046PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003047 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003048PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003049 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00003050
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003051static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07003052 DICT___CONTAINS___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003053 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
3054 getitem__doc__},
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003055 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003056 sizeof__doc__},
3057 {"get", (PyCFunction)dict_get, METH_VARARGS,
3058 get__doc__},
3059 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
3060 setdefault_doc__},
3061 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
3062 pop__doc__},
3063 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
3064 popitem__doc__},
3065 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
3066 keys__doc__},
3067 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
3068 items__doc__},
3069 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
3070 values__doc__},
3071 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
3072 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08003073 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003074 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
3075 clear__doc__},
3076 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
3077 copy__doc__},
3078 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003079};
3080
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003081/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00003082int
3083PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003084{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003085 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07003086 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003087 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003088 PyObject **value_addr;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003090 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003091 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003092 hash = PyObject_Hash(key);
3093 if (hash == -1)
3094 return -1;
3095 }
Victor Stinner742da042016-09-07 17:40:12 -07003096 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
3097 if (ix == DKIX_ERROR)
3098 return -1;
3099 return (ix != DKIX_EMPTY && *value_addr != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003100}
3101
Thomas Wouterscf297e42007-02-23 15:07:44 +00003102/* Internal version of PyDict_Contains used when the hash value is already known */
3103int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003104_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00003105{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003106 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003107 PyObject **value_addr;
Victor Stinner742da042016-09-07 17:40:12 -07003108 Py_ssize_t ix;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003109
Victor Stinner742da042016-09-07 17:40:12 -07003110 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
3111 if (ix == DKIX_ERROR)
3112 return -1;
3113 return (ix != DKIX_EMPTY && *value_addr != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00003114}
3115
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003116/* Hack to implement "key in dict" */
3117static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003118 0, /* sq_length */
3119 0, /* sq_concat */
3120 0, /* sq_repeat */
3121 0, /* sq_item */
3122 0, /* sq_slice */
3123 0, /* sq_ass_item */
3124 0, /* sq_ass_slice */
3125 PyDict_Contains, /* sq_contains */
3126 0, /* sq_inplace_concat */
3127 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003128};
3129
Guido van Rossum09e563a2001-05-01 12:10:21 +00003130static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003131dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3132{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003133 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003134 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003136 assert(type != NULL && type->tp_alloc != NULL);
3137 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02003138 if (self == NULL)
3139 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003140 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003141
Victor Stinnera9f61a52013-07-16 22:17:26 +02003142 /* The object has been implicitly tracked by tp_alloc */
3143 if (type == &PyDict_Type)
3144 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003145
3146 d->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003147 d->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07003148 d->ma_keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003149 if (d->ma_keys == NULL) {
3150 Py_DECREF(self);
3151 return NULL;
3152 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02003153 assert(_PyDict_CheckConsistency(d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003154 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003155}
3156
Tim Peters25786c02001-09-02 08:22:48 +00003157static int
3158dict_init(PyObject *self, PyObject *args, PyObject *kwds)
3159{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003160 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00003161}
3162
Tim Peters6d6c1a32001-08-02 04:15:00 +00003163static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003164dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00003165{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003166 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003167}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003168
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003169PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00003170"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00003171"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003172" (key, value) pairs\n"
3173"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00003174" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003175" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00003176" d[k] = v\n"
3177"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
3178" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00003179
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003180PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003181 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3182 "dict",
3183 sizeof(PyDictObject),
3184 0,
3185 (destructor)dict_dealloc, /* tp_dealloc */
3186 0, /* tp_print */
3187 0, /* tp_getattr */
3188 0, /* tp_setattr */
3189 0, /* tp_reserved */
3190 (reprfunc)dict_repr, /* tp_repr */
3191 0, /* tp_as_number */
3192 &dict_as_sequence, /* tp_as_sequence */
3193 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003194 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003195 0, /* tp_call */
3196 0, /* tp_str */
3197 PyObject_GenericGetAttr, /* tp_getattro */
3198 0, /* tp_setattro */
3199 0, /* tp_as_buffer */
3200 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3201 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
3202 dictionary_doc, /* tp_doc */
3203 dict_traverse, /* tp_traverse */
3204 dict_tp_clear, /* tp_clear */
3205 dict_richcompare, /* tp_richcompare */
3206 0, /* tp_weaklistoffset */
3207 (getiterfunc)dict_iter, /* tp_iter */
3208 0, /* tp_iternext */
3209 mapp_methods, /* tp_methods */
3210 0, /* tp_members */
3211 0, /* tp_getset */
3212 0, /* tp_base */
3213 0, /* tp_dict */
3214 0, /* tp_descr_get */
3215 0, /* tp_descr_set */
3216 0, /* tp_dictoffset */
3217 dict_init, /* tp_init */
3218 PyType_GenericAlloc, /* tp_alloc */
3219 dict_new, /* tp_new */
3220 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003221};
3222
Victor Stinner3c1e4812012-03-26 22:10:51 +02003223PyObject *
3224_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
3225{
3226 PyObject *kv;
3227 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02003228 if (kv == NULL) {
3229 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003230 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02003231 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02003232 return PyDict_GetItem(dp, kv);
3233}
3234
Guido van Rossum3cca2451997-05-16 14:23:33 +00003235/* For backward compatibility with old dictionary interface */
3236
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003237PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003238PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003239{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003240 PyObject *kv, *rv;
3241 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003242 if (kv == NULL) {
3243 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003244 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003245 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003246 rv = PyDict_GetItem(v, kv);
3247 Py_DECREF(kv);
3248 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003249}
3250
3251int
Victor Stinner3c1e4812012-03-26 22:10:51 +02003252_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
3253{
3254 PyObject *kv;
3255 kv = _PyUnicode_FromId(key); /* borrowed */
3256 if (kv == NULL)
3257 return -1;
3258 return PyDict_SetItem(v, kv, item);
3259}
3260
3261int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003262PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003264 PyObject *kv;
3265 int err;
3266 kv = PyUnicode_FromString(key);
3267 if (kv == NULL)
3268 return -1;
3269 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
3270 err = PyDict_SetItem(v, kv, item);
3271 Py_DECREF(kv);
3272 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003273}
3274
3275int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01003276_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
3277{
3278 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3279 if (kv == NULL)
3280 return -1;
3281 return PyDict_DelItem(v, kv);
3282}
3283
3284int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003285PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003287 PyObject *kv;
3288 int err;
3289 kv = PyUnicode_FromString(key);
3290 if (kv == NULL)
3291 return -1;
3292 err = PyDict_DelItem(v, kv);
3293 Py_DECREF(kv);
3294 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003295}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003296
Raymond Hettinger019a1482004-03-18 02:41:19 +00003297/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003298
3299typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003300 PyObject_HEAD
3301 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
3302 Py_ssize_t di_used;
3303 Py_ssize_t di_pos;
3304 PyObject* di_result; /* reusable result tuple for iteritems */
3305 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003306} dictiterobject;
3307
3308static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003309dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003311 dictiterobject *di;
3312 di = PyObject_GC_New(dictiterobject, itertype);
3313 if (di == NULL)
3314 return NULL;
3315 Py_INCREF(dict);
3316 di->di_dict = dict;
3317 di->di_used = dict->ma_used;
3318 di->di_pos = 0;
3319 di->len = dict->ma_used;
3320 if (itertype == &PyDictIterItem_Type) {
3321 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
3322 if (di->di_result == NULL) {
3323 Py_DECREF(di);
3324 return NULL;
3325 }
3326 }
3327 else
3328 di->di_result = NULL;
3329 _PyObject_GC_TRACK(di);
3330 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003331}
3332
3333static void
3334dictiter_dealloc(dictiterobject *di)
3335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003336 Py_XDECREF(di->di_dict);
3337 Py_XDECREF(di->di_result);
3338 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003339}
3340
3341static int
3342dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
3343{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003344 Py_VISIT(di->di_dict);
3345 Py_VISIT(di->di_result);
3346 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003347}
3348
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003349static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003350dictiter_len(dictiterobject *di)
3351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003352 Py_ssize_t len = 0;
3353 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
3354 len = di->len;
3355 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003356}
3357
Guido van Rossumb90c8482007-02-10 01:11:45 +00003358PyDoc_STRVAR(length_hint_doc,
3359 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003360
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003361static PyObject *
3362dictiter_reduce(dictiterobject *di);
3363
3364PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3365
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003366static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003367 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
3368 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003369 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
3370 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003371 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003372};
3373
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003374static PyObject*
3375dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00003376{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003377 PyObject *key;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003378 Py_ssize_t i, n;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003379 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003380 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00003381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003382 if (d == NULL)
3383 return NULL;
3384 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00003385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003386 if (di->di_used != d->ma_used) {
3387 PyErr_SetString(PyExc_RuntimeError,
3388 "dictionary changed size during iteration");
3389 di->di_used = -1; /* Make this state sticky */
3390 return NULL;
3391 }
Guido van Rossum2147df72002-07-16 20:30:22 +00003392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003393 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003394 k = d->ma_keys;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003395 n = k->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003396 if (d->ma_values) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003397 PyObject **value_ptr = &d->ma_values[i];
3398 while (i < n && *value_ptr == NULL) {
3399 value_ptr++;
3400 i++;
3401 }
3402 if (i >= n)
3403 goto fail;
3404 key = DK_ENTRIES(k)[i].me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003405 }
3406 else {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003407 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3408 while (i < n && entry_ptr->me_value == NULL) {
3409 entry_ptr++;
3410 i++;
3411 }
3412 if (i >= n)
3413 goto fail;
3414 key = entry_ptr->me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003415 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003416 di->di_pos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003417 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003418 Py_INCREF(key);
3419 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003420
3421fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003422 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003423 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003424 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003425}
3426
Raymond Hettinger019a1482004-03-18 02:41:19 +00003427PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003428 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3429 "dict_keyiterator", /* tp_name */
3430 sizeof(dictiterobject), /* tp_basicsize */
3431 0, /* tp_itemsize */
3432 /* methods */
3433 (destructor)dictiter_dealloc, /* tp_dealloc */
3434 0, /* tp_print */
3435 0, /* tp_getattr */
3436 0, /* tp_setattr */
3437 0, /* tp_reserved */
3438 0, /* tp_repr */
3439 0, /* tp_as_number */
3440 0, /* tp_as_sequence */
3441 0, /* tp_as_mapping */
3442 0, /* tp_hash */
3443 0, /* tp_call */
3444 0, /* tp_str */
3445 PyObject_GenericGetAttr, /* tp_getattro */
3446 0, /* tp_setattro */
3447 0, /* tp_as_buffer */
3448 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3449 0, /* tp_doc */
3450 (traverseproc)dictiter_traverse, /* tp_traverse */
3451 0, /* tp_clear */
3452 0, /* tp_richcompare */
3453 0, /* tp_weaklistoffset */
3454 PyObject_SelfIter, /* tp_iter */
3455 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3456 dictiter_methods, /* tp_methods */
3457 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003458};
3459
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003460static PyObject *
3461dictiter_iternextvalue(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003462{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003463 PyObject *value;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003464 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003465 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003467 if (d == NULL)
3468 return NULL;
3469 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003471 if (di->di_used != d->ma_used) {
3472 PyErr_SetString(PyExc_RuntimeError,
3473 "dictionary changed size during iteration");
3474 di->di_used = -1; /* Make this state sticky */
3475 return NULL;
3476 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003478 i = di->di_pos;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003479 n = d->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003480 if (d->ma_values) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003481 PyObject **value_ptr = &d->ma_values[i];
3482 while (i < n && *value_ptr == NULL) {
3483 value_ptr++;
3484 i++;
3485 }
3486 if (i >= n)
3487 goto fail;
3488 value = *value_ptr;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003489 }
3490 else {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003491 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3492 while (i < n && entry_ptr->me_value == NULL) {
3493 entry_ptr++;
3494 i++;
3495 }
3496 if (i >= n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003497 goto fail;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003498 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003499 }
3500 di->di_pos = i+1;
3501 di->len--;
3502 Py_INCREF(value);
3503 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003504
3505fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003506 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003507 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003508 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003509}
3510
3511PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003512 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3513 "dict_valueiterator", /* tp_name */
3514 sizeof(dictiterobject), /* tp_basicsize */
3515 0, /* tp_itemsize */
3516 /* methods */
3517 (destructor)dictiter_dealloc, /* tp_dealloc */
3518 0, /* tp_print */
3519 0, /* tp_getattr */
3520 0, /* tp_setattr */
3521 0, /* tp_reserved */
3522 0, /* tp_repr */
3523 0, /* tp_as_number */
3524 0, /* tp_as_sequence */
3525 0, /* tp_as_mapping */
3526 0, /* tp_hash */
3527 0, /* tp_call */
3528 0, /* tp_str */
3529 PyObject_GenericGetAttr, /* tp_getattro */
3530 0, /* tp_setattro */
3531 0, /* tp_as_buffer */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003532 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003533 0, /* tp_doc */
3534 (traverseproc)dictiter_traverse, /* tp_traverse */
3535 0, /* tp_clear */
3536 0, /* tp_richcompare */
3537 0, /* tp_weaklistoffset */
3538 PyObject_SelfIter, /* tp_iter */
3539 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3540 dictiter_methods, /* tp_methods */
3541 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003542};
3543
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003544static PyObject *
3545dictiter_iternextitem(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003546{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003547 PyObject *key, *value, *result = di->di_result;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003548 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003549 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003551 if (d == NULL)
3552 return NULL;
3553 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003555 if (di->di_used != d->ma_used) {
3556 PyErr_SetString(PyExc_RuntimeError,
3557 "dictionary changed size during iteration");
3558 di->di_used = -1; /* Make this state sticky */
3559 return NULL;
3560 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003562 i = di->di_pos;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003563 n = d->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003564 if (d->ma_values) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003565 PyObject **value_ptr = &d->ma_values[i];
3566 while (i < n && *value_ptr == NULL) {
3567 value_ptr++;
3568 i++;
3569 }
3570 if (i >= n)
3571 goto fail;
3572 key = DK_ENTRIES(d->ma_keys)[i].me_key;
3573 value = *value_ptr;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003574 }
3575 else {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003576 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3577 while (i < n && entry_ptr->me_value == NULL) {
3578 entry_ptr++;
3579 i++;
3580 }
3581 if (i >= n)
3582 goto fail;
3583 key = entry_ptr->me_key;
3584 value = entry_ptr->me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003585 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003586 di->di_pos = i+1;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003587 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003588 if (result->ob_refcnt == 1) {
3589 Py_INCREF(result);
3590 Py_DECREF(PyTuple_GET_ITEM(result, 0));
3591 Py_DECREF(PyTuple_GET_ITEM(result, 1));
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003592 }
3593 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003594 result = PyTuple_New(2);
3595 if (result == NULL)
3596 return NULL;
3597 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003598 Py_INCREF(key);
3599 Py_INCREF(value);
Eric Snow96c6af92015-05-29 22:21:39 -06003600 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3601 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003602 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003603
3604fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003605 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003606 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003607 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003608}
3609
3610PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003611 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3612 "dict_itemiterator", /* tp_name */
3613 sizeof(dictiterobject), /* tp_basicsize */
3614 0, /* tp_itemsize */
3615 /* methods */
3616 (destructor)dictiter_dealloc, /* tp_dealloc */
3617 0, /* tp_print */
3618 0, /* tp_getattr */
3619 0, /* tp_setattr */
3620 0, /* tp_reserved */
3621 0, /* tp_repr */
3622 0, /* tp_as_number */
3623 0, /* tp_as_sequence */
3624 0, /* tp_as_mapping */
3625 0, /* tp_hash */
3626 0, /* tp_call */
3627 0, /* tp_str */
3628 PyObject_GenericGetAttr, /* tp_getattro */
3629 0, /* tp_setattro */
3630 0, /* tp_as_buffer */
3631 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3632 0, /* tp_doc */
3633 (traverseproc)dictiter_traverse, /* tp_traverse */
3634 0, /* tp_clear */
3635 0, /* tp_richcompare */
3636 0, /* tp_weaklistoffset */
3637 PyObject_SelfIter, /* tp_iter */
3638 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3639 dictiter_methods, /* tp_methods */
3640 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003641};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003642
3643
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003644static PyObject *
3645dictiter_reduce(dictiterobject *di)
3646{
3647 PyObject *list;
3648 dictiterobject tmp;
3649
3650 list = PyList_New(0);
3651 if (!list)
3652 return NULL;
3653
3654 /* copy the itertor state */
3655 tmp = *di;
3656 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003657
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003658 /* iterate the temporary into a list */
3659 for(;;) {
3660 PyObject *element = 0;
3661 if (Py_TYPE(di) == &PyDictIterItem_Type)
3662 element = dictiter_iternextitem(&tmp);
3663 else if (Py_TYPE(di) == &PyDictIterKey_Type)
3664 element = dictiter_iternextkey(&tmp);
3665 else if (Py_TYPE(di) == &PyDictIterValue_Type)
3666 element = dictiter_iternextvalue(&tmp);
3667 else
3668 assert(0);
3669 if (element) {
3670 if (PyList_Append(list, element)) {
3671 Py_DECREF(element);
3672 Py_DECREF(list);
3673 Py_XDECREF(tmp.di_dict);
3674 return NULL;
3675 }
3676 Py_DECREF(element);
3677 } else
3678 break;
3679 }
3680 Py_XDECREF(tmp.di_dict);
3681 /* check for error */
3682 if (tmp.di_dict != NULL) {
3683 /* we have an error */
3684 Py_DECREF(list);
3685 return NULL;
3686 }
Antoine Pitroua7013882012-04-05 00:04:20 +02003687 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003688}
3689
Guido van Rossum3ac67412007-02-10 18:55:06 +00003690/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003691/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003692/***********************************************/
3693
Guido van Rossumb90c8482007-02-10 01:11:45 +00003694/* The instance lay-out is the same for all three; but the type differs. */
3695
Guido van Rossumb90c8482007-02-10 01:11:45 +00003696static void
Eric Snow96c6af92015-05-29 22:21:39 -06003697dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003698{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003699 Py_XDECREF(dv->dv_dict);
3700 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003701}
3702
3703static int
Eric Snow96c6af92015-05-29 22:21:39 -06003704dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003705{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003706 Py_VISIT(dv->dv_dict);
3707 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003708}
3709
Guido van Rossum83825ac2007-02-10 04:54:19 +00003710static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003711dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003712{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003713 Py_ssize_t len = 0;
3714 if (dv->dv_dict != NULL)
3715 len = dv->dv_dict->ma_used;
3716 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003717}
3718
Eric Snow96c6af92015-05-29 22:21:39 -06003719PyObject *
3720_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003721{
Eric Snow96c6af92015-05-29 22:21:39 -06003722 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003723 if (dict == NULL) {
3724 PyErr_BadInternalCall();
3725 return NULL;
3726 }
3727 if (!PyDict_Check(dict)) {
3728 /* XXX Get rid of this restriction later */
3729 PyErr_Format(PyExc_TypeError,
3730 "%s() requires a dict argument, not '%s'",
3731 type->tp_name, dict->ob_type->tp_name);
3732 return NULL;
3733 }
Eric Snow96c6af92015-05-29 22:21:39 -06003734 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003735 if (dv == NULL)
3736 return NULL;
3737 Py_INCREF(dict);
3738 dv->dv_dict = (PyDictObject *)dict;
3739 _PyObject_GC_TRACK(dv);
3740 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003741}
3742
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003743/* TODO(guido): The views objects are not complete:
3744
3745 * support more set operations
3746 * support arbitrary mappings?
3747 - either these should be static or exported in dictobject.h
3748 - if public then they should probably be in builtins
3749*/
3750
Guido van Rossumaac530c2007-08-24 22:33:45 +00003751/* Return 1 if self is a subset of other, iterating over self;
3752 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003753static int
3754all_contained_in(PyObject *self, PyObject *other)
3755{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003756 PyObject *iter = PyObject_GetIter(self);
3757 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003759 if (iter == NULL)
3760 return -1;
3761 for (;;) {
3762 PyObject *next = PyIter_Next(iter);
3763 if (next == NULL) {
3764 if (PyErr_Occurred())
3765 ok = -1;
3766 break;
3767 }
3768 ok = PySequence_Contains(other, next);
3769 Py_DECREF(next);
3770 if (ok <= 0)
3771 break;
3772 }
3773 Py_DECREF(iter);
3774 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003775}
3776
3777static PyObject *
3778dictview_richcompare(PyObject *self, PyObject *other, int op)
3779{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003780 Py_ssize_t len_self, len_other;
3781 int ok;
3782 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003784 assert(self != NULL);
3785 assert(PyDictViewSet_Check(self));
3786 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003787
Brian Curtindfc80e32011-08-10 20:28:54 -05003788 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3789 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003791 len_self = PyObject_Size(self);
3792 if (len_self < 0)
3793 return NULL;
3794 len_other = PyObject_Size(other);
3795 if (len_other < 0)
3796 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003798 ok = 0;
3799 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003801 case Py_NE:
3802 case Py_EQ:
3803 if (len_self == len_other)
3804 ok = all_contained_in(self, other);
3805 if (op == Py_NE && ok >= 0)
3806 ok = !ok;
3807 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003808
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003809 case Py_LT:
3810 if (len_self < len_other)
3811 ok = all_contained_in(self, other);
3812 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003814 case Py_LE:
3815 if (len_self <= len_other)
3816 ok = all_contained_in(self, other);
3817 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003819 case Py_GT:
3820 if (len_self > len_other)
3821 ok = all_contained_in(other, self);
3822 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003824 case Py_GE:
3825 if (len_self >= len_other)
3826 ok = all_contained_in(other, self);
3827 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003829 }
3830 if (ok < 0)
3831 return NULL;
3832 result = ok ? Py_True : Py_False;
3833 Py_INCREF(result);
3834 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003835}
3836
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003837static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003838dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003839{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003840 PyObject *seq;
3841 PyObject *result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003842
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003843 seq = PySequence_List((PyObject *)dv);
3844 if (seq == NULL)
3845 return NULL;
3846
3847 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
3848 Py_DECREF(seq);
3849 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003850}
3851
Guido van Rossum3ac67412007-02-10 18:55:06 +00003852/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003853
3854static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003855dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003856{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003857 if (dv->dv_dict == NULL) {
3858 Py_RETURN_NONE;
3859 }
3860 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003861}
3862
3863static int
Eric Snow96c6af92015-05-29 22:21:39 -06003864dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00003865{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003866 if (dv->dv_dict == NULL)
3867 return 0;
3868 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003869}
3870
Guido van Rossum83825ac2007-02-10 04:54:19 +00003871static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003872 (lenfunc)dictview_len, /* sq_length */
3873 0, /* sq_concat */
3874 0, /* sq_repeat */
3875 0, /* sq_item */
3876 0, /* sq_slice */
3877 0, /* sq_ass_item */
3878 0, /* sq_ass_slice */
3879 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003880};
3881
Guido van Rossum523259b2007-08-24 23:41:22 +00003882static PyObject*
3883dictviews_sub(PyObject* self, PyObject *other)
3884{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003885 PyObject *result = PySet_New(self);
3886 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003887 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003889 if (result == NULL)
3890 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003891
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003892 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003893 if (tmp == NULL) {
3894 Py_DECREF(result);
3895 return NULL;
3896 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003898 Py_DECREF(tmp);
3899 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003900}
3901
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003902PyObject*
3903_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00003904{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003905 PyObject *result = PySet_New(self);
3906 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003907 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003909 if (result == NULL)
3910 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003911
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003912 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_intersection_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003913 if (tmp == NULL) {
3914 Py_DECREF(result);
3915 return NULL;
3916 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003918 Py_DECREF(tmp);
3919 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003920}
3921
3922static PyObject*
3923dictviews_or(PyObject* self, PyObject *other)
3924{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003925 PyObject *result = PySet_New(self);
3926 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003927 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003929 if (result == NULL)
3930 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003931
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003932 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003933 if (tmp == NULL) {
3934 Py_DECREF(result);
3935 return NULL;
3936 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003938 Py_DECREF(tmp);
3939 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003940}
3941
3942static PyObject*
3943dictviews_xor(PyObject* self, PyObject *other)
3944{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003945 PyObject *result = PySet_New(self);
3946 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003947 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003949 if (result == NULL)
3950 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003951
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003952 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_symmetric_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003953 if (tmp == NULL) {
3954 Py_DECREF(result);
3955 return NULL;
3956 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003958 Py_DECREF(tmp);
3959 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003960}
3961
3962static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003963 0, /*nb_add*/
3964 (binaryfunc)dictviews_sub, /*nb_subtract*/
3965 0, /*nb_multiply*/
3966 0, /*nb_remainder*/
3967 0, /*nb_divmod*/
3968 0, /*nb_power*/
3969 0, /*nb_negative*/
3970 0, /*nb_positive*/
3971 0, /*nb_absolute*/
3972 0, /*nb_bool*/
3973 0, /*nb_invert*/
3974 0, /*nb_lshift*/
3975 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003976 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003977 (binaryfunc)dictviews_xor, /*nb_xor*/
3978 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00003979};
3980
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003981static PyObject*
3982dictviews_isdisjoint(PyObject *self, PyObject *other)
3983{
3984 PyObject *it;
3985 PyObject *item = NULL;
3986
3987 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06003988 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003989 Py_RETURN_TRUE;
3990 else
3991 Py_RETURN_FALSE;
3992 }
3993
3994 /* Iterate over the shorter object (only if other is a set,
3995 * because PySequence_Contains may be expensive otherwise): */
3996 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06003997 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003998 Py_ssize_t len_other = PyObject_Size(other);
3999 if (len_other == -1)
4000 return NULL;
4001
4002 if ((len_other > len_self)) {
4003 PyObject *tmp = other;
4004 other = self;
4005 self = tmp;
4006 }
4007 }
4008
4009 it = PyObject_GetIter(other);
4010 if (it == NULL)
4011 return NULL;
4012
4013 while ((item = PyIter_Next(it)) != NULL) {
4014 int contains = PySequence_Contains(self, item);
4015 Py_DECREF(item);
4016 if (contains == -1) {
4017 Py_DECREF(it);
4018 return NULL;
4019 }
4020
4021 if (contains) {
4022 Py_DECREF(it);
4023 Py_RETURN_FALSE;
4024 }
4025 }
4026 Py_DECREF(it);
4027 if (PyErr_Occurred())
4028 return NULL; /* PyIter_Next raised an exception. */
4029 Py_RETURN_TRUE;
4030}
4031
4032PyDoc_STRVAR(isdisjoint_doc,
4033"Return True if the view and the given iterable have a null intersection.");
4034
Guido van Rossumb90c8482007-02-10 01:11:45 +00004035static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004036 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4037 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004038 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004039};
4040
4041PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004042 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4043 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004044 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004045 0, /* tp_itemsize */
4046 /* methods */
4047 (destructor)dictview_dealloc, /* tp_dealloc */
4048 0, /* tp_print */
4049 0, /* tp_getattr */
4050 0, /* tp_setattr */
4051 0, /* tp_reserved */
4052 (reprfunc)dictview_repr, /* tp_repr */
4053 &dictviews_as_number, /* tp_as_number */
4054 &dictkeys_as_sequence, /* tp_as_sequence */
4055 0, /* tp_as_mapping */
4056 0, /* tp_hash */
4057 0, /* tp_call */
4058 0, /* tp_str */
4059 PyObject_GenericGetAttr, /* tp_getattro */
4060 0, /* tp_setattro */
4061 0, /* tp_as_buffer */
4062 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4063 0, /* tp_doc */
4064 (traverseproc)dictview_traverse, /* tp_traverse */
4065 0, /* tp_clear */
4066 dictview_richcompare, /* tp_richcompare */
4067 0, /* tp_weaklistoffset */
4068 (getiterfunc)dictkeys_iter, /* tp_iter */
4069 0, /* tp_iternext */
4070 dictkeys_methods, /* tp_methods */
4071 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004072};
4073
4074static PyObject *
4075dictkeys_new(PyObject *dict)
4076{
Eric Snow96c6af92015-05-29 22:21:39 -06004077 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004078}
4079
Guido van Rossum3ac67412007-02-10 18:55:06 +00004080/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004081
4082static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004083dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004084{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004085 if (dv->dv_dict == NULL) {
4086 Py_RETURN_NONE;
4087 }
4088 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004089}
4090
4091static int
Eric Snow96c6af92015-05-29 22:21:39 -06004092dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004093{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004094 PyObject *key, *value, *found;
4095 if (dv->dv_dict == NULL)
4096 return 0;
4097 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
4098 return 0;
4099 key = PyTuple_GET_ITEM(obj, 0);
4100 value = PyTuple_GET_ITEM(obj, 1);
Raymond Hettinger6692f012016-09-18 21:46:08 -07004101 found = PyDict_GetItemWithError((PyObject *)dv->dv_dict, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004102 if (found == NULL) {
4103 if (PyErr_Occurred())
4104 return -1;
4105 return 0;
4106 }
4107 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004108}
4109
Guido van Rossum83825ac2007-02-10 04:54:19 +00004110static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004111 (lenfunc)dictview_len, /* sq_length */
4112 0, /* sq_concat */
4113 0, /* sq_repeat */
4114 0, /* sq_item */
4115 0, /* sq_slice */
4116 0, /* sq_ass_item */
4117 0, /* sq_ass_slice */
4118 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004119};
4120
Guido van Rossumb90c8482007-02-10 01:11:45 +00004121static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004122 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4123 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004124 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004125};
4126
4127PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004128 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4129 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004130 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004131 0, /* tp_itemsize */
4132 /* methods */
4133 (destructor)dictview_dealloc, /* tp_dealloc */
4134 0, /* tp_print */
4135 0, /* tp_getattr */
4136 0, /* tp_setattr */
4137 0, /* tp_reserved */
4138 (reprfunc)dictview_repr, /* tp_repr */
4139 &dictviews_as_number, /* tp_as_number */
4140 &dictitems_as_sequence, /* tp_as_sequence */
4141 0, /* tp_as_mapping */
4142 0, /* tp_hash */
4143 0, /* tp_call */
4144 0, /* tp_str */
4145 PyObject_GenericGetAttr, /* tp_getattro */
4146 0, /* tp_setattro */
4147 0, /* tp_as_buffer */
4148 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4149 0, /* tp_doc */
4150 (traverseproc)dictview_traverse, /* tp_traverse */
4151 0, /* tp_clear */
4152 dictview_richcompare, /* tp_richcompare */
4153 0, /* tp_weaklistoffset */
4154 (getiterfunc)dictitems_iter, /* tp_iter */
4155 0, /* tp_iternext */
4156 dictitems_methods, /* tp_methods */
4157 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004158};
4159
4160static PyObject *
4161dictitems_new(PyObject *dict)
4162{
Eric Snow96c6af92015-05-29 22:21:39 -06004163 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004164}
4165
Guido van Rossum3ac67412007-02-10 18:55:06 +00004166/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004167
4168static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004169dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004170{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004171 if (dv->dv_dict == NULL) {
4172 Py_RETURN_NONE;
4173 }
4174 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004175}
4176
Guido van Rossum83825ac2007-02-10 04:54:19 +00004177static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004178 (lenfunc)dictview_len, /* sq_length */
4179 0, /* sq_concat */
4180 0, /* sq_repeat */
4181 0, /* sq_item */
4182 0, /* sq_slice */
4183 0, /* sq_ass_item */
4184 0, /* sq_ass_slice */
4185 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004186};
4187
Guido van Rossumb90c8482007-02-10 01:11:45 +00004188static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004189 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004190};
4191
4192PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004193 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4194 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004195 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004196 0, /* tp_itemsize */
4197 /* methods */
4198 (destructor)dictview_dealloc, /* tp_dealloc */
4199 0, /* tp_print */
4200 0, /* tp_getattr */
4201 0, /* tp_setattr */
4202 0, /* tp_reserved */
4203 (reprfunc)dictview_repr, /* tp_repr */
4204 0, /* tp_as_number */
4205 &dictvalues_as_sequence, /* tp_as_sequence */
4206 0, /* tp_as_mapping */
4207 0, /* tp_hash */
4208 0, /* tp_call */
4209 0, /* tp_str */
4210 PyObject_GenericGetAttr, /* tp_getattro */
4211 0, /* tp_setattro */
4212 0, /* tp_as_buffer */
4213 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4214 0, /* tp_doc */
4215 (traverseproc)dictview_traverse, /* tp_traverse */
4216 0, /* tp_clear */
4217 0, /* tp_richcompare */
4218 0, /* tp_weaklistoffset */
4219 (getiterfunc)dictvalues_iter, /* tp_iter */
4220 0, /* tp_iternext */
4221 dictvalues_methods, /* tp_methods */
4222 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004223};
4224
4225static PyObject *
4226dictvalues_new(PyObject *dict)
4227{
Eric Snow96c6af92015-05-29 22:21:39 -06004228 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004229}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004230
4231/* Returns NULL if cannot allocate a new PyDictKeysObject,
4232 but does not set an error */
4233PyDictKeysObject *
4234_PyDict_NewKeysForClass(void)
4235{
Victor Stinner742da042016-09-07 17:40:12 -07004236 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004237 if (keys == NULL)
4238 PyErr_Clear();
4239 else
4240 keys->dk_lookup = lookdict_split;
4241 return keys;
4242}
4243
4244#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
4245
4246PyObject *
4247PyObject_GenericGetDict(PyObject *obj, void *context)
4248{
4249 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
4250 if (dictptr == NULL) {
4251 PyErr_SetString(PyExc_AttributeError,
4252 "This object has no __dict__");
4253 return NULL;
4254 }
4255 dict = *dictptr;
4256 if (dict == NULL) {
4257 PyTypeObject *tp = Py_TYPE(obj);
4258 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
4259 DK_INCREF(CACHED_KEYS(tp));
4260 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
4261 }
4262 else {
4263 *dictptr = dict = PyDict_New();
4264 }
4265 }
4266 Py_XINCREF(dict);
4267 return dict;
4268}
4269
4270int
4271_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
Victor Stinner742da042016-09-07 17:40:12 -07004272 PyObject *key, PyObject *value)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004273{
4274 PyObject *dict;
4275 int res;
4276 PyDictKeysObject *cached;
4277
4278 assert(dictptr != NULL);
4279 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
4280 assert(dictptr != NULL);
4281 dict = *dictptr;
4282 if (dict == NULL) {
4283 DK_INCREF(cached);
4284 dict = new_dict_with_shared_keys(cached);
4285 if (dict == NULL)
4286 return -1;
4287 *dictptr = dict;
4288 }
4289 if (value == NULL) {
4290 res = PyDict_DelItem(dict, key);
4291 if (cached != ((PyDictObject *)dict)->ma_keys) {
4292 CACHED_KEYS(tp) = NULL;
4293 DK_DECREF(cached);
4294 }
4295 } else {
4296 res = PyDict_SetItem(dict, key, value);
4297 if (cached != ((PyDictObject *)dict)->ma_keys) {
4298 /* Either update tp->ht_cached_keys or delete it */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004299 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004300 CACHED_KEYS(tp) = make_keys_shared(dict);
Victor Stinner742da042016-09-07 17:40:12 -07004301 }
4302 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004303 CACHED_KEYS(tp) = NULL;
4304 }
4305 DK_DECREF(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004306 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
4307 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004308 }
4309 }
4310 } else {
4311 dict = *dictptr;
4312 if (dict == NULL) {
4313 dict = PyDict_New();
4314 if (dict == NULL)
4315 return -1;
4316 *dictptr = dict;
4317 }
4318 if (value == NULL) {
4319 res = PyDict_DelItem(dict, key);
4320 } else {
4321 res = PyDict_SetItem(dict, key, value);
4322 }
4323 }
4324 return res;
4325}
4326
4327void
4328_PyDictKeys_DecRef(PyDictKeysObject *keys)
4329{
4330 DK_DECREF(keys);
4331}