blob: 03c973be636adc05cba13d469203a5a64a67c35d [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);
726 if (cmp < 0)
Victor Stinner742da042016-09-07 17:40:12 -0700727 return DKIX_ERROR;
728 if (dk == mp->ma_keys && ep->me_key == startkey) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400729 if (cmp > 0) {
730 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700731 if (hashpos != NULL)
732 *hashpos = i;
733 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400734 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 }
736 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200737 /* The dict was mutated, restart */
738 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 }
740 }
Victor Stinner742da042016-09-07 17:40:12 -0700741 freeslot = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 }
Tim Peters15d49292001-05-27 07:39:22 +0000743
INADA Naoki267941c2016-10-06 15:19:07 +0900744 for (size_t perturb = hash;;) {
745 perturb >>= PERTURB_SHIFT;
Victor Stinner742da042016-09-07 17:40:12 -0700746 i = ((i << 2) + i + perturb + 1) & mask;
747 ix = dk_get_index(dk, i);
748 if (ix == DKIX_EMPTY) {
749 if (hashpos != NULL) {
750 *hashpos = (freeslot == -1) ? (Py_ssize_t)i : freeslot;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400751 }
Victor Stinner742da042016-09-07 17:40:12 -0700752 *value_addr = NULL;
753 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400754 }
Victor Stinner742da042016-09-07 17:40:12 -0700755 if (ix == DKIX_DUMMY) {
756 if (freeslot == -1)
757 freeslot = i;
758 continue;
759 }
760 ep = &ep0[ix];
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300761 assert(ep->me_key != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400762 if (ep->me_key == key) {
Victor Stinner742da042016-09-07 17:40:12 -0700763 if (hashpos != NULL) {
764 *hashpos = i;
765 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400766 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700767 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400768 }
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300769 if (ep->me_hash == hash) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 startkey = ep->me_key;
771 Py_INCREF(startkey);
772 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
773 Py_DECREF(startkey);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400774 if (cmp < 0) {
775 *value_addr = NULL;
Victor Stinner742da042016-09-07 17:40:12 -0700776 return DKIX_ERROR;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400777 }
Victor Stinner742da042016-09-07 17:40:12 -0700778 if (dk == mp->ma_keys && ep->me_key == startkey) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400779 if (cmp > 0) {
Victor Stinner742da042016-09-07 17:40:12 -0700780 if (hashpos != NULL) {
781 *hashpos = i;
782 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400783 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700784 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400785 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 }
787 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200788 /* The dict was mutated, restart */
789 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 }
791 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 }
793 assert(0); /* NOT REACHED */
794 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000795}
796
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400797/* Specialized version for string-only keys */
Victor Stinner742da042016-09-07 17:40:12 -0700798static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400799lookdict_unicode(PyDictObject *mp, PyObject *key,
Victor Stinner742da042016-09-07 17:40:12 -0700800 Py_hash_t hash, PyObject ***value_addr, Py_ssize_t *hashpos)
Fred Drake1bff34a2000-08-31 19:31:38 +0000801{
INADA Naoki267941c2016-10-06 15:19:07 +0900802 size_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200803 size_t mask = DK_MASK(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -0700804 Py_ssize_t ix, freeslot;
805 PyDictKeyEntry *ep, *ep0 = DK_ENTRIES(mp->ma_keys);
Fred Drake1bff34a2000-08-31 19:31:38 +0000806
Victor Stinner742da042016-09-07 17:40:12 -0700807 assert(mp->ma_values == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 /* Make sure this function doesn't have to handle non-unicode keys,
809 including subclasses of str; e.g., one reason to subclass
810 unicodes is to override __eq__, and for speed we don't cater to
811 that here. */
812 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400813 mp->ma_keys->dk_lookup = lookdict;
Victor Stinner742da042016-09-07 17:40:12 -0700814 return lookdict(mp, key, hash, value_addr, hashpos);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 }
Mark Dickinson57e683e2011-09-24 18:18:40 +0100816 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700817 ix = dk_get_index(mp->ma_keys, i);
818 if (ix == DKIX_EMPTY) {
819 if (hashpos != NULL)
820 *hashpos = i;
821 *value_addr = NULL;
822 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400823 }
Victor Stinner742da042016-09-07 17:40:12 -0700824 if (ix == DKIX_DUMMY) {
825 freeslot = i;
826 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 else {
Victor Stinner742da042016-09-07 17:40:12 -0700828 ep = &ep0[ix];
Victor Stinner742da042016-09-07 17:40:12 -0700829 assert(ep->me_key != NULL);
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300830 if (ep->me_key == key
831 || (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700832 if (hashpos != NULL)
833 *hashpos = i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400834 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700835 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400836 }
Victor Stinner742da042016-09-07 17:40:12 -0700837 freeslot = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 }
Tim Peters15d49292001-05-27 07:39:22 +0000839
INADA Naoki267941c2016-10-06 15:19:07 +0900840 for (size_t perturb = hash;;) {
841 perturb >>= PERTURB_SHIFT;
Victor Stinner742da042016-09-07 17:40:12 -0700842 i = mask & ((i << 2) + i + perturb + 1);
843 ix = dk_get_index(mp->ma_keys, i);
844 if (ix == DKIX_EMPTY) {
845 if (hashpos != NULL) {
846 *hashpos = (freeslot == -1) ? (Py_ssize_t)i : freeslot;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400847 }
Victor Stinner742da042016-09-07 17:40:12 -0700848 *value_addr = NULL;
849 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400850 }
Victor Stinner742da042016-09-07 17:40:12 -0700851 if (ix == DKIX_DUMMY) {
852 if (freeslot == -1)
853 freeslot = i;
854 continue;
855 }
856 ep = &ep0[ix];
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300857 assert(ep->me_key != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 if (ep->me_key == key
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300859 || (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400860 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700861 if (hashpos != NULL) {
862 *hashpos = i;
863 }
864 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400865 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 }
867 assert(0); /* NOT REACHED */
868 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000869}
870
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400871/* Faster version of lookdict_unicode when it is known that no <dummy> keys
872 * will be present. */
Victor Stinner742da042016-09-07 17:40:12 -0700873static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400874lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
Victor Stinner742da042016-09-07 17:40:12 -0700875 Py_hash_t hash, PyObject ***value_addr,
876 Py_ssize_t *hashpos)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400877{
INADA Naoki267941c2016-10-06 15:19:07 +0900878 size_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200879 size_t mask = DK_MASK(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -0700880 Py_ssize_t ix;
881 PyDictKeyEntry *ep, *ep0 = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400882
Victor Stinner742da042016-09-07 17:40:12 -0700883 assert(mp->ma_values == NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400884 /* Make sure this function doesn't have to handle non-unicode keys,
885 including subclasses of str; e.g., one reason to subclass
886 unicodes is to override __eq__, and for speed we don't cater to
887 that here. */
888 if (!PyUnicode_CheckExact(key)) {
889 mp->ma_keys->dk_lookup = lookdict;
Victor Stinner742da042016-09-07 17:40:12 -0700890 return lookdict(mp, key, hash, value_addr, hashpos);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400891 }
892 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700893 ix = dk_get_index(mp->ma_keys, i);
894 assert (ix != DKIX_DUMMY);
895 if (ix == DKIX_EMPTY) {
896 if (hashpos != NULL)
897 *hashpos = i;
898 *value_addr = NULL;
899 return DKIX_EMPTY;
900 }
901 ep = &ep0[ix];
Victor Stinnerdee6e252016-09-08 11:16:07 -0700902 assert(ep->me_key != NULL);
903 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700904 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400905 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700906 if (hashpos != NULL)
907 *hashpos = i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400908 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700909 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400910 }
INADA Naoki267941c2016-10-06 15:19:07 +0900911 for (size_t perturb = hash;;) {
912 perturb >>= PERTURB_SHIFT;
Victor Stinner742da042016-09-07 17:40:12 -0700913 i = mask & ((i << 2) + i + perturb + 1);
914 ix = dk_get_index(mp->ma_keys, i);
915 assert (ix != DKIX_DUMMY);
916 if (ix == DKIX_EMPTY) {
917 if (hashpos != NULL)
918 *hashpos = i;
919 *value_addr = NULL;
920 return DKIX_EMPTY;
921 }
922 ep = &ep0[ix];
923 assert(ep->me_key != NULL && PyUnicode_CheckExact(ep->me_key));
924 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400925 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700926 if (hashpos != NULL)
927 *hashpos = i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400928 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700929 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400930 }
931 }
932 assert(0); /* NOT REACHED */
933 return 0;
934}
935
936/* Version of lookdict for split tables.
937 * All split tables and only split tables use this lookup function.
938 * Split tables only contain unicode keys and no dummy keys,
939 * so algorithm is the same as lookdict_unicode_nodummy.
940 */
Victor Stinner742da042016-09-07 17:40:12 -0700941static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400942lookdict_split(PyDictObject *mp, PyObject *key,
Victor Stinner742da042016-09-07 17:40:12 -0700943 Py_hash_t hash, PyObject ***value_addr, Py_ssize_t *hashpos)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400944{
INADA Naoki267941c2016-10-06 15:19:07 +0900945 size_t i;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200946 size_t mask = DK_MASK(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -0700947 Py_ssize_t ix;
948 PyDictKeyEntry *ep, *ep0 = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400949
Victor Stinner742da042016-09-07 17:40:12 -0700950 /* mp must split table */
951 assert(mp->ma_values != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400952 if (!PyUnicode_CheckExact(key)) {
Victor Stinner742da042016-09-07 17:40:12 -0700953 ix = lookdict(mp, key, hash, value_addr, hashpos);
954 if (ix >= 0) {
955 *value_addr = &mp->ma_values[ix];
956 }
957 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400958 }
Victor Stinner742da042016-09-07 17:40:12 -0700959
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400960 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700961 ix = dk_get_index(mp->ma_keys, i);
962 if (ix == DKIX_EMPTY) {
963 if (hashpos != NULL)
964 *hashpos = i;
965 *value_addr = NULL;
966 return DKIX_EMPTY;
967 }
968 assert(ix >= 0);
969 ep = &ep0[ix];
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300970 assert(ep->me_key != NULL && PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700971 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400972 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700973 if (hashpos != NULL)
974 *hashpos = i;
975 *value_addr = &mp->ma_values[ix];
976 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400977 }
INADA Naoki267941c2016-10-06 15:19:07 +0900978 for (size_t perturb = hash;;) {
979 perturb >>= PERTURB_SHIFT;
Victor Stinner742da042016-09-07 17:40:12 -0700980 i = mask & ((i << 2) + i + perturb + 1);
981 ix = dk_get_index(mp->ma_keys, i);
982 if (ix == DKIX_EMPTY) {
983 if (hashpos != NULL)
984 *hashpos = i;
985 *value_addr = NULL;
986 return DKIX_EMPTY;
987 }
988 assert(ix >= 0);
989 ep = &ep0[ix];
Serhiy Storchaka46825d22016-09-26 21:29:34 +0300990 assert(ep->me_key != NULL && PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700991 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400992 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700993 if (hashpos != NULL)
994 *hashpos = i;
995 *value_addr = &mp->ma_values[ix];
996 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400997 }
998 }
999 assert(0); /* NOT REACHED */
1000 return 0;
1001}
1002
Benjamin Petersonfb886362010-04-24 18:21:17 +00001003int
1004_PyDict_HasOnlyStringKeys(PyObject *dict)
1005{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 Py_ssize_t pos = 0;
1007 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +00001008 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001010 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 return 1;
1012 while (PyDict_Next(dict, &pos, &key, &value))
1013 if (!PyUnicode_Check(key))
1014 return 0;
1015 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +00001016}
1017
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001018#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 do { \
1020 if (!_PyObject_GC_IS_TRACKED(mp)) { \
1021 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
1022 _PyObject_GC_MAY_BE_TRACKED(value)) { \
1023 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 } \
1025 } \
1026 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001027
1028void
1029_PyDict_MaybeUntrack(PyObject *op)
1030{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 PyDictObject *mp;
1032 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07001033 Py_ssize_t i, numentries;
1034 PyDictKeyEntry *ep0;
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
1037 return;
1038
1039 mp = (PyDictObject *) op;
Victor Stinner742da042016-09-07 17:40:12 -07001040 ep0 = DK_ENTRIES(mp->ma_keys);
1041 numentries = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001042 if (_PyDict_HasSplitTable(mp)) {
Victor Stinner742da042016-09-07 17:40:12 -07001043 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001044 if ((value = mp->ma_values[i]) == NULL)
1045 continue;
1046 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
Victor Stinner742da042016-09-07 17:40:12 -07001047 assert(!_PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001048 return;
1049 }
1050 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001052 else {
Victor Stinner742da042016-09-07 17:40:12 -07001053 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001054 if ((value = ep0[i].me_value) == NULL)
1055 continue;
1056 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
1057 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
1058 return;
1059 }
1060 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +00001062}
1063
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001064/* Internal function to find slot for an item from its hash
Victor Stinner3c336c52016-09-12 14:17:40 +02001065 when it is known that the key is not present in the dict.
1066
1067 The dict must be combined. */
Victor Stinner99264802016-09-13 09:38:29 +02001068static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001069find_empty_slot(PyDictObject *mp, PyObject *key, Py_hash_t hash,
Victor Stinner742da042016-09-07 17:40:12 -07001070 PyObject ***value_addr, Py_ssize_t *hashpos)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001071{
INADA Naoki267941c2016-10-06 15:19:07 +09001072 size_t i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001073 size_t mask = DK_MASK(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07001074 Py_ssize_t ix;
1075 PyDictKeyEntry *ep, *ep0 = DK_ENTRIES(mp->ma_keys);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001076
Victor Stinner3c336c52016-09-12 14:17:40 +02001077 assert(!_PyDict_HasSplitTable(mp));
Victor Stinner742da042016-09-07 17:40:12 -07001078 assert(hashpos != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001079 assert(key != NULL);
Victor Stinner3c336c52016-09-12 14:17:40 +02001080
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001081 if (!PyUnicode_CheckExact(key))
1082 mp->ma_keys->dk_lookup = lookdict;
1083 i = hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -07001084 ix = dk_get_index(mp->ma_keys, i);
INADA Naoki267941c2016-10-06 15:19:07 +09001085 for (size_t perturb = hash; ix != DKIX_EMPTY;) {
1086 perturb >>= PERTURB_SHIFT;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001087 i = (i << 2) + i + perturb + 1;
Victor Stinner742da042016-09-07 17:40:12 -07001088 ix = dk_get_index(mp->ma_keys, i & mask);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 }
Victor Stinner742da042016-09-07 17:40:12 -07001090 ep = &ep0[mp->ma_keys->dk_nentries];
1091 *hashpos = i & mask;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001092 assert(ep->me_value == NULL);
Victor Stinner99264802016-09-13 09:38:29 +02001093 *value_addr = &ep->me_value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001094}
1095
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001096static int
1097insertion_resize(PyDictObject *mp)
1098{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -07001099 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001100}
Antoine Pitroue965d972012-02-27 00:45:12 +01001101
1102/*
1103Internal routine to insert a new item into the table.
1104Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +01001105Returns -1 if an error occurred, or 0 on success.
1106*/
1107static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001108insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001109{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001110 PyObject *old_value;
1111 PyObject **value_addr;
Victor Stinner742da042016-09-07 17:40:12 -07001112 PyDictKeyEntry *ep, *ep0;
1113 Py_ssize_t hashpos, ix;
Antoine Pitroue965d972012-02-27 00:45:12 +01001114
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001115 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
1116 if (insertion_resize(mp) < 0)
1117 return -1;
1118 }
1119
Victor Stinner742da042016-09-07 17:40:12 -07001120 ix = mp->ma_keys->dk_lookup(mp, key, hash, &value_addr, &hashpos);
1121 if (ix == DKIX_ERROR) {
Antoine Pitroue965d972012-02-27 00:45:12 +01001122 return -1;
1123 }
Victor Stinner742da042016-09-07 17:40:12 -07001124
Antoine Pitroud6967322014-10-18 00:35:00 +02001125 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
Benjamin Petersona6f195e2012-04-30 10:23:40 -04001126 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001127 MAINTAIN_TRACKING(mp, key, value);
Victor Stinner742da042016-09-07 17:40:12 -07001128
1129 /* When insertion order is different from shared key, we can't share
1130 * the key anymore. Convert this instance to combine table.
1131 */
1132 if (_PyDict_HasSplitTable(mp) &&
1133 ((ix >= 0 && *value_addr == NULL && mp->ma_used != ix) ||
1134 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
1135 if (insertion_resize(mp) < 0) {
1136 Py_DECREF(value);
1137 return -1;
1138 }
1139 find_empty_slot(mp, key, hash, &value_addr, &hashpos);
1140 ix = DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001141 }
Victor Stinner742da042016-09-07 17:40:12 -07001142
1143 if (ix == DKIX_EMPTY) {
1144 /* Insert into new slot. */
1145 if (mp->ma_keys->dk_usable <= 0) {
1146 /* Need to resize. */
1147 if (insertion_resize(mp) < 0) {
1148 Py_DECREF(value);
1149 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001150 }
Victor Stinner742da042016-09-07 17:40:12 -07001151 find_empty_slot(mp, key, hash, &value_addr, &hashpos);
1152 }
1153 ep0 = DK_ENTRIES(mp->ma_keys);
1154 ep = &ep0[mp->ma_keys->dk_nentries];
1155 dk_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
1156 Py_INCREF(key);
1157 ep->me_key = key;
1158 ep->me_hash = hash;
1159 if (mp->ma_values) {
1160 assert (mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
1161 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001162 }
1163 else {
Victor Stinner742da042016-09-07 17:40:12 -07001164 ep->me_value = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001165 }
1166 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001167 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07001168 mp->ma_keys->dk_usable--;
1169 mp->ma_keys->dk_nentries++;
1170 assert(mp->ma_keys->dk_usable >= 0);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001171 assert(_PyDict_CheckConsistency(mp));
Victor Stinner742da042016-09-07 17:40:12 -07001172 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001173 }
Victor Stinner742da042016-09-07 17:40:12 -07001174
1175 assert(value_addr != NULL);
1176
1177 old_value = *value_addr;
1178 if (old_value != NULL) {
1179 *value_addr = value;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001180 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02001181 assert(_PyDict_CheckConsistency(mp));
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001182
Victor Stinner742da042016-09-07 17:40:12 -07001183 Py_DECREF(old_value); /* which **CAN** re-enter (see issue #22653) */
1184 return 0;
1185 }
1186
1187 /* pending state */
1188 assert(_PyDict_HasSplitTable(mp));
1189 assert(ix == mp->ma_used);
1190 *value_addr = value;
1191 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001192 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02001193 assert(_PyDict_CheckConsistency(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001194 return 0;
Antoine Pitroue965d972012-02-27 00:45:12 +01001195}
1196
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001197/*
1198Internal routine used by dictresize() to insert an item which is
1199known to be absent from the dict. This routine also assumes that
1200the dict contains no deleted entries. Besides the performance benefit,
1201using insertdict() in dictresize() is dangerous (SF bug #1456209).
1202Note that no refcounts are changed by this routine; if needed, the caller
1203is responsible for incref'ing `key` and `value`.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001204Neither mp->ma_used nor k->dk_usable are modified by this routine; the caller
1205must set them correctly
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001206*/
1207static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001208insertdict_clean(PyDictObject *mp, PyObject *key, Py_hash_t hash,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 PyObject *value)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001210{
INADA Naoki267941c2016-10-06 15:19:07 +09001211 size_t i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001212 PyDictKeysObject *k = mp->ma_keys;
1213 size_t mask = (size_t)DK_SIZE(k)-1;
Victor Stinner742da042016-09-07 17:40:12 -07001214 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001215 PyDictKeyEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001216
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001217 assert(k->dk_lookup != NULL);
1218 assert(value != NULL);
1219 assert(key != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001220 assert(PyUnicode_CheckExact(key) || k->dk_lookup == lookdict);
1221 i = hash & mask;
INADA Naoki267941c2016-10-06 15:19:07 +09001222 for (size_t perturb = hash; dk_get_index(k, i) != DKIX_EMPTY;) {
1223 perturb >>= PERTURB_SHIFT;
Victor Stinner742da042016-09-07 17:40:12 -07001224 i = mask & ((i << 2) + i + perturb + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 }
Victor Stinner742da042016-09-07 17:40:12 -07001226 ep = &ep0[k->dk_nentries];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 assert(ep->me_value == NULL);
Victor Stinner742da042016-09-07 17:40:12 -07001228 dk_set_index(k, i, k->dk_nentries);
1229 k->dk_nentries++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 ep->me_key = key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001231 ep->me_hash = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001233}
1234
1235/*
1236Restructure the table by allocating a new table and reinserting all
1237items again. When entries have been deleted, the new table may
1238actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001239If a table is split (its keys and hashes are shared, its values are not),
1240then the values are temporarily copied into the table, it is resized as
1241a combined table, then the me_value slots in the old table are NULLed out.
1242After resizing a table is always combined,
1243but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001244*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001245static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001246dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001247{
Victor Stinner742da042016-09-07 17:40:12 -07001248 Py_ssize_t i, newsize;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001249 PyDictKeysObject *oldkeys;
1250 PyObject **oldvalues;
Victor Stinner742da042016-09-07 17:40:12 -07001251 PyDictKeyEntry *ep0;
Tim Peters91a364d2001-05-19 07:04:38 +00001252
Victor Stinner742da042016-09-07 17:40:12 -07001253 /* Find the smallest table size > minused. */
1254 for (newsize = PyDict_MINSIZE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 newsize <= minused && newsize > 0;
1256 newsize <<= 1)
1257 ;
1258 if (newsize <= 0) {
1259 PyErr_NoMemory();
1260 return -1;
1261 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001262 oldkeys = mp->ma_keys;
1263 oldvalues = mp->ma_values;
1264 /* Allocate a new table. */
1265 mp->ma_keys = new_keys_object(newsize);
1266 if (mp->ma_keys == NULL) {
1267 mp->ma_keys = oldkeys;
1268 return -1;
1269 }
1270 if (oldkeys->dk_lookup == lookdict)
1271 mp->ma_keys->dk_lookup = lookdict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001272 mp->ma_values = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001273 ep0 = DK_ENTRIES(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001274 /* Main loop below assumes we can transfer refcount to new keys
1275 * and that value is stored in me_value.
1276 * Increment ref-counts and copy values here to compensate
1277 * This (resizing a split table) should be relatively rare */
1278 if (oldvalues != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001279 for (i = 0; i < oldkeys->dk_nentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001280 if (oldvalues[i] != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001281 Py_INCREF(ep0[i].me_key);
1282 ep0[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 }
1285 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001286 /* Main loop */
Victor Stinner742da042016-09-07 17:40:12 -07001287 for (i = 0; i < oldkeys->dk_nentries; i++) {
1288 PyDictKeyEntry *ep = &ep0[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001289 if (ep->me_value != NULL) {
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001290 insertdict_clean(mp, ep->me_key, ep->me_hash, ep->me_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001293 mp->ma_keys->dk_usable -= mp->ma_used;
1294 if (oldvalues != NULL) {
1295 /* NULL out me_value slot in oldkeys, in case it was shared */
Victor Stinner742da042016-09-07 17:40:12 -07001296 for (i = 0; i < oldkeys->dk_nentries; i++)
1297 ep0[i].me_value = NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001298 DK_DECREF(oldkeys);
Victor Stinner742da042016-09-07 17:40:12 -07001299 if (oldvalues != empty_values) {
1300 free_values(oldvalues);
1301 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001302 }
1303 else {
1304 assert(oldkeys->dk_lookup != lookdict_split);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001305 assert(oldkeys->dk_refcnt == 1);
Raymond Hettingerce5179f2016-01-31 08:56:21 -08001306 DK_DEBUG_DECREF PyObject_FREE(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001307 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001309}
1310
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001311/* Returns NULL if unable to split table.
1312 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001313static PyDictKeysObject *
1314make_keys_shared(PyObject *op)
1315{
1316 Py_ssize_t i;
1317 Py_ssize_t size;
1318 PyDictObject *mp = (PyDictObject *)op;
1319
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001320 if (!PyDict_CheckExact(op))
1321 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001322 if (!_PyDict_HasSplitTable(mp)) {
1323 PyDictKeyEntry *ep0;
1324 PyObject **values;
1325 assert(mp->ma_keys->dk_refcnt == 1);
1326 if (mp->ma_keys->dk_lookup == lookdict) {
1327 return NULL;
1328 }
1329 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1330 /* Remove dummy keys */
1331 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1332 return NULL;
1333 }
1334 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1335 /* Copy values into a new array */
Victor Stinner742da042016-09-07 17:40:12 -07001336 ep0 = DK_ENTRIES(mp->ma_keys);
1337 size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001338 values = new_values(size);
1339 if (values == NULL) {
1340 PyErr_SetString(PyExc_MemoryError,
1341 "Not enough memory to allocate new values array");
1342 return NULL;
1343 }
1344 for (i = 0; i < size; i++) {
1345 values[i] = ep0[i].me_value;
1346 ep0[i].me_value = NULL;
1347 }
1348 mp->ma_keys->dk_lookup = lookdict_split;
1349 mp->ma_values = values;
1350 }
1351 DK_INCREF(mp->ma_keys);
1352 return mp->ma_keys;
1353}
Christian Heimes99170a52007-12-19 02:07:34 +00001354
1355PyObject *
1356_PyDict_NewPresized(Py_ssize_t minused)
1357{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001358 Py_ssize_t newsize;
1359 PyDictKeysObject *new_keys;
Victor Stinner742da042016-09-07 17:40:12 -07001360 for (newsize = PyDict_MINSIZE;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001361 newsize <= minused && newsize > 0;
1362 newsize <<= 1)
1363 ;
1364 new_keys = new_keys_object(newsize);
1365 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001367 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001368}
1369
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001370/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1371 * that may occur (originally dicts supported only string keys, and exceptions
1372 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001373 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001374 * (suppressed) occurred while computing the key's hash, or that some error
1375 * (suppressed) occurred when comparing keys in the dict's internal probe
1376 * sequence. A nasty example of the latter is when a Python-coded comparison
1377 * function hits a stack-depth error, which can cause this to return NULL
1378 * even if the key is present.
1379 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001380PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001381PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001382{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001383 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07001384 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 PyDictObject *mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 PyThreadState *tstate;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001387 PyObject **value_addr;
1388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 if (!PyDict_Check(op))
1390 return NULL;
1391 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001392 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 {
1394 hash = PyObject_Hash(key);
1395 if (hash == -1) {
1396 PyErr_Clear();
1397 return NULL;
1398 }
1399 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 /* We can arrive here with a NULL tstate during initialization: try
1402 running "python -Wi" for an example related to string interning.
1403 Let's just hope that no exception occurs then... This must be
1404 _PyThreadState_Current and not PyThreadState_GET() because in debug
1405 mode, the latter complains if tstate is NULL. */
Victor Stinnerbfd316e2016-01-20 11:12:38 +01001406 tstate = _PyThreadState_UncheckedGet();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 if (tstate != NULL && tstate->curexc_type != NULL) {
1408 /* preserve the existing exception */
1409 PyObject *err_type, *err_value, *err_tb;
1410 PyErr_Fetch(&err_type, &err_value, &err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001411 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 /* ignore errors */
1413 PyErr_Restore(err_type, err_value, err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001414 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 return NULL;
1416 }
1417 else {
Victor Stinner742da042016-09-07 17:40:12 -07001418 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
1419 if (ix < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 PyErr_Clear();
1421 return NULL;
1422 }
1423 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001424 return *value_addr;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001425}
1426
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001427PyObject *
1428_PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1429{
Victor Stinner742da042016-09-07 17:40:12 -07001430 Py_ssize_t ix;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001431 PyDictObject *mp = (PyDictObject *)op;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001432 PyThreadState *tstate;
1433 PyObject **value_addr;
1434
1435 if (!PyDict_Check(op))
1436 return NULL;
1437
1438 /* We can arrive here with a NULL tstate during initialization: try
1439 running "python -Wi" for an example related to string interning.
1440 Let's just hope that no exception occurs then... This must be
1441 _PyThreadState_Current and not PyThreadState_GET() because in debug
1442 mode, the latter complains if tstate is NULL. */
Victor Stinnerbfd316e2016-01-20 11:12:38 +01001443 tstate = _PyThreadState_UncheckedGet();
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001444 if (tstate != NULL && tstate->curexc_type != NULL) {
1445 /* preserve the existing exception */
1446 PyObject *err_type, *err_value, *err_tb;
1447 PyErr_Fetch(&err_type, &err_value, &err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001448 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001449 /* ignore errors */
1450 PyErr_Restore(err_type, err_value, err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001451 if (ix == DKIX_EMPTY)
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001452 return NULL;
1453 }
1454 else {
Victor Stinner742da042016-09-07 17:40:12 -07001455 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
1456 if (ix == DKIX_EMPTY) {
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001457 PyErr_Clear();
1458 return NULL;
1459 }
1460 }
1461 return *value_addr;
1462}
1463
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001464/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1465 This returns NULL *with* an exception set if an exception occurred.
1466 It returns NULL *without* an exception set if the key wasn't present.
1467*/
1468PyObject *
1469PyDict_GetItemWithError(PyObject *op, PyObject *key)
1470{
Victor Stinner742da042016-09-07 17:40:12 -07001471 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001472 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 PyDictObject*mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001474 PyObject **value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 if (!PyDict_Check(op)) {
1477 PyErr_BadInternalCall();
1478 return NULL;
1479 }
1480 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001481 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 {
1483 hash = PyObject_Hash(key);
1484 if (hash == -1) {
1485 return NULL;
1486 }
1487 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001488
Victor Stinner742da042016-09-07 17:40:12 -07001489 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
1490 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001492 return *value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001493}
1494
Brett Cannonfd074152012-04-14 14:10:13 -04001495PyObject *
1496_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1497{
1498 PyObject *kv;
1499 kv = _PyUnicode_FromId(key); /* borrowed */
1500 if (kv == NULL)
1501 return NULL;
1502 return PyDict_GetItemWithError(dp, kv);
1503}
1504
Victor Stinnerb4efc962015-11-20 09:24:02 +01001505/* Fast version of global value lookup (LOAD_GLOBAL).
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001506 * Lookup in globals, then builtins.
Victor Stinnerb4efc962015-11-20 09:24:02 +01001507 *
1508 * Raise an exception and return NULL if an error occurred (ex: computing the
1509 * key hash failed, key comparison failed, ...). Return NULL if the key doesn't
1510 * exist. Return the value if the key exists.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001511 */
1512PyObject *
1513_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001514{
Victor Stinner742da042016-09-07 17:40:12 -07001515 Py_ssize_t ix;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001516 Py_hash_t hash;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001517 PyObject **value_addr;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001518
1519 if (!PyUnicode_CheckExact(key) ||
1520 (hash = ((PyASCIIObject *) key)->hash) == -1)
1521 {
1522 hash = PyObject_Hash(key);
1523 if (hash == -1)
1524 return NULL;
Antoine Pitroue965d972012-02-27 00:45:12 +01001525 }
Victor Stinnerb4efc962015-11-20 09:24:02 +01001526
1527 /* namespace 1: globals */
Victor Stinner742da042016-09-07 17:40:12 -07001528 ix = globals->ma_keys->dk_lookup(globals, key, hash, &value_addr, NULL);
1529 if (ix == DKIX_ERROR)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001530 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001531 if (ix != DKIX_EMPTY && *value_addr != NULL)
1532 return *value_addr;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001533
1534 /* namespace 2: builtins */
Victor Stinner742da042016-09-07 17:40:12 -07001535 ix = builtins->ma_keys->dk_lookup(builtins, key, hash, &value_addr, NULL);
1536 if (ix < 0)
Victor Stinnerb4efc962015-11-20 09:24:02 +01001537 return NULL;
1538 return *value_addr;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001539}
1540
Antoine Pitroue965d972012-02-27 00:45:12 +01001541/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1542 * dictionary if it's merely replacing the value for an existing key.
1543 * This means that it's safe to loop over a dictionary with PyDict_Next()
1544 * and occasionally replace a value -- but you can't insert new keys or
1545 * remove them.
1546 */
1547int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001548PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001549{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001550 PyDictObject *mp;
1551 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001552 if (!PyDict_Check(op)) {
1553 PyErr_BadInternalCall();
1554 return -1;
1555 }
1556 assert(key);
1557 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001558 mp = (PyDictObject *)op;
1559 if (!PyUnicode_CheckExact(key) ||
1560 (hash = ((PyASCIIObject *) key)->hash) == -1)
1561 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001562 hash = PyObject_Hash(key);
1563 if (hash == -1)
1564 return -1;
1565 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001566
1567 /* insertdict() handles any resizing that might be necessary */
1568 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001569}
1570
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001571int
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001572_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1573 Py_hash_t hash)
1574{
1575 PyDictObject *mp;
1576
1577 if (!PyDict_Check(op)) {
1578 PyErr_BadInternalCall();
1579 return -1;
1580 }
1581 assert(key);
1582 assert(value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001583 assert(hash != -1);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001584 mp = (PyDictObject *)op;
1585
1586 /* insertdict() handles any resizing that might be necessary */
1587 return insertdict(mp, key, hash, value);
1588}
1589
1590int
Tim Peters1f5871e2000-07-04 17:44:48 +00001591PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001592{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001593 Py_hash_t hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +00001594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 assert(key);
1596 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001597 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 hash = PyObject_Hash(key);
1599 if (hash == -1)
1600 return -1;
1601 }
Victor Stinner742da042016-09-07 17:40:12 -07001602
1603 return _PyDict_DelItem_KnownHash(op, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001604}
1605
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001606int
1607_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1608{
Victor Stinner742da042016-09-07 17:40:12 -07001609 Py_ssize_t hashpos, ix;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001610 PyDictObject *mp;
1611 PyDictKeyEntry *ep;
1612 PyObject *old_key, *old_value;
1613 PyObject **value_addr;
1614
1615 if (!PyDict_Check(op)) {
1616 PyErr_BadInternalCall();
1617 return -1;
1618 }
1619 assert(key);
1620 assert(hash != -1);
1621 mp = (PyDictObject *)op;
Victor Stinner742da042016-09-07 17:40:12 -07001622 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
1623 if (ix == DKIX_ERROR)
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001624 return -1;
Victor Stinner742da042016-09-07 17:40:12 -07001625 if (ix == DKIX_EMPTY || *value_addr == NULL) {
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001626 _PyErr_SetKeyError(key);
1627 return -1;
1628 }
Victor Stinner742da042016-09-07 17:40:12 -07001629 assert(dk_get_index(mp->ma_keys, hashpos) == ix);
Victor Stinner78601a32016-09-09 19:28:36 -07001630
1631 // Split table doesn't allow deletion. Combine it.
1632 if (_PyDict_HasSplitTable(mp)) {
1633 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1634 return -1;
1635 }
1636 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
1637 assert(ix >= 0);
1638 }
1639
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001640 old_value = *value_addr;
Victor Stinner78601a32016-09-09 19:28:36 -07001641 assert(old_value != NULL);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001642 *value_addr = NULL;
1643 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001644 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner78601a32016-09-09 19:28:36 -07001645 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1646 dk_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
1647 ENSURE_ALLOWS_DELETIONS(mp);
1648 old_key = ep->me_key;
1649 ep->me_key = NULL;
1650 Py_DECREF(old_key);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001651 Py_DECREF(old_value);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001652
1653 assert(_PyDict_CheckConsistency(mp));
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001654 return 0;
1655}
1656
Guido van Rossum25831651993-05-19 14:50:45 +00001657void
Tim Peters1f5871e2000-07-04 17:44:48 +00001658PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001659{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001661 PyDictKeysObject *oldkeys;
1662 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 if (!PyDict_Check(op))
1666 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001667 mp = ((PyDictObject *)op);
1668 oldkeys = mp->ma_keys;
1669 oldvalues = mp->ma_values;
1670 if (oldvalues == empty_values)
1671 return;
1672 /* Empty the dict... */
1673 DK_INCREF(Py_EMPTY_KEYS);
1674 mp->ma_keys = Py_EMPTY_KEYS;
1675 mp->ma_values = empty_values;
1676 mp->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001677 mp->ma_version_tag = DICT_NEXT_VERSION();
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001678 /* ...then clear the keys and values */
1679 if (oldvalues != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001680 n = oldkeys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001681 for (i = 0; i < n; i++)
1682 Py_CLEAR(oldvalues[i]);
1683 free_values(oldvalues);
1684 DK_DECREF(oldkeys);
1685 }
1686 else {
1687 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001688 DK_DECREF(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001689 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02001690 assert(_PyDict_CheckConsistency(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001691}
1692
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001693/* Internal version of PyDict_Next that returns a hash value in addition
1694 * to the key and value.
1695 * Return 1 on success, return 0 when the reached the end of the dictionary
1696 * (or if op is not a dictionary)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001697 */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001698int
1699_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1700 PyObject **pvalue, Py_hash_t *phash)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001701{
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001702 Py_ssize_t i, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001703 PyDictObject *mp;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001704 PyDictKeyEntry *entry_ptr;
1705 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001706
1707 if (!PyDict_Check(op))
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001708 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 mp = (PyDictObject *)op;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001710 i = *ppos;
Victor Stinner742da042016-09-07 17:40:12 -07001711 n = mp->ma_keys->dk_nentries;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001712 if ((size_t)i >= (size_t)n)
1713 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001714 if (mp->ma_values) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001715 PyObject **value_ptr = &mp->ma_values[i];
1716 while (i < n && *value_ptr == NULL) {
1717 value_ptr++;
1718 i++;
Victor Stinner742da042016-09-07 17:40:12 -07001719 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001720 if (i >= n)
1721 return 0;
1722 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
1723 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001725 else {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001726 entry_ptr = &DK_ENTRIES(mp->ma_keys)[i];
1727 while (i < n && entry_ptr->me_value == NULL) {
1728 entry_ptr++;
1729 i++;
Victor Stinner742da042016-09-07 17:40:12 -07001730 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001731 if (i >= n)
1732 return 0;
1733 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001734 }
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001735 *ppos = i+1;
1736 if (pkey)
1737 *pkey = entry_ptr->me_key;
1738 if (phash)
1739 *phash = entry_ptr->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001740 if (pvalue)
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001741 *pvalue = value;
1742 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001743}
1744
Tim Peters080c88b2003-02-15 03:01:11 +00001745/*
1746 * Iterate over a dict. Use like so:
1747 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001748 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001749 * PyObject *key, *value;
1750 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001751 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001752 * Refer to borrowed references in key and value.
Tim Peters080c88b2003-02-15 03:01:11 +00001753 * }
1754 *
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001755 * Return 1 on success, return 0 when the reached the end of the dictionary
1756 * (or if op is not a dictionary)
1757 *
Tim Peters080c88b2003-02-15 03:01:11 +00001758 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001759 * mutates the dict. One exception: it is safe if the loop merely changes
1760 * the values associated with the keys (but doesn't insert new keys or
1761 * delete keys), via PyDict_SetItem().
1762 */
Guido van Rossum25831651993-05-19 14:50:45 +00001763int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001764PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001765{
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03001766 return _PyDict_Next(op, ppos, pkey, pvalue, NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00001767}
1768
Eric Snow96c6af92015-05-29 22:21:39 -06001769/* Internal version of dict.pop(). */
1770PyObject *
1771_PyDict_Pop(PyDictObject *mp, PyObject *key, PyObject *deflt)
1772{
1773 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07001774 Py_ssize_t ix, hashpos;
Eric Snow96c6af92015-05-29 22:21:39 -06001775 PyObject *old_value, *old_key;
1776 PyDictKeyEntry *ep;
1777 PyObject **value_addr;
1778
1779 if (mp->ma_used == 0) {
1780 if (deflt) {
1781 Py_INCREF(deflt);
1782 return deflt;
1783 }
1784 _PyErr_SetKeyError(key);
1785 return NULL;
1786 }
1787 if (!PyUnicode_CheckExact(key) ||
1788 (hash = ((PyASCIIObject *) key)->hash) == -1) {
1789 hash = PyObject_Hash(key);
1790 if (hash == -1)
1791 return NULL;
1792 }
Victor Stinner742da042016-09-07 17:40:12 -07001793 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
1794 if (ix == DKIX_ERROR)
Eric Snow96c6af92015-05-29 22:21:39 -06001795 return NULL;
Victor Stinnerd0ad11f2016-09-13 16:56:38 +02001796 if (ix == DKIX_EMPTY || *value_addr == NULL) {
Eric Snow96c6af92015-05-29 22:21:39 -06001797 if (deflt) {
1798 Py_INCREF(deflt);
1799 return deflt;
1800 }
1801 _PyErr_SetKeyError(key);
1802 return NULL;
1803 }
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001804
Victor Stinner78601a32016-09-09 19:28:36 -07001805 // Split table doesn't allow deletion. Combine it.
1806 if (_PyDict_HasSplitTable(mp)) {
1807 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
1808 return NULL;
1809 }
1810 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
1811 assert(ix >= 0);
1812 }
1813
Victor Stinner742da042016-09-07 17:40:12 -07001814 old_value = *value_addr;
Victor Stinner78601a32016-09-09 19:28:36 -07001815 assert(old_value != NULL);
Eric Snow96c6af92015-05-29 22:21:39 -06001816 *value_addr = NULL;
1817 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07001818 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner78601a32016-09-09 19:28:36 -07001819 dk_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
1820 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1821 ENSURE_ALLOWS_DELETIONS(mp);
1822 old_key = ep->me_key;
1823 ep->me_key = NULL;
1824 Py_DECREF(old_key);
Victor Stinner611b0fa2016-09-14 15:02:01 +02001825
1826 assert(_PyDict_CheckConsistency(mp));
Eric Snow96c6af92015-05-29 22:21:39 -06001827 return old_value;
1828}
1829
1830/* Internal version of dict.from_keys(). It is subclass-friendly. */
1831PyObject *
1832_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1833{
1834 PyObject *it; /* iter(iterable) */
1835 PyObject *key;
1836 PyObject *d;
1837 int status;
1838
1839 d = PyObject_CallObject(cls, NULL);
1840 if (d == NULL)
1841 return NULL;
1842
1843 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
1844 if (PyDict_CheckExact(iterable)) {
1845 PyDictObject *mp = (PyDictObject *)d;
1846 PyObject *oldvalue;
1847 Py_ssize_t pos = 0;
1848 PyObject *key;
1849 Py_hash_t hash;
1850
Victor Stinner742da042016-09-07 17:40:12 -07001851 if (dictresize(mp, ESTIMATE_SIZE(Py_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001852 Py_DECREF(d);
1853 return NULL;
1854 }
1855
1856 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
1857 if (insertdict(mp, key, hash, value)) {
1858 Py_DECREF(d);
1859 return NULL;
1860 }
1861 }
1862 return d;
1863 }
1864 if (PyAnySet_CheckExact(iterable)) {
1865 PyDictObject *mp = (PyDictObject *)d;
1866 Py_ssize_t pos = 0;
1867 PyObject *key;
1868 Py_hash_t hash;
1869
Victor Stinner742da042016-09-07 17:40:12 -07001870 if (dictresize(mp, ESTIMATE_SIZE(PySet_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001871 Py_DECREF(d);
1872 return NULL;
1873 }
1874
1875 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
1876 if (insertdict(mp, key, hash, value)) {
1877 Py_DECREF(d);
1878 return NULL;
1879 }
1880 }
1881 return d;
1882 }
1883 }
1884
1885 it = PyObject_GetIter(iterable);
1886 if (it == NULL){
1887 Py_DECREF(d);
1888 return NULL;
1889 }
1890
1891 if (PyDict_CheckExact(d)) {
1892 while ((key = PyIter_Next(it)) != NULL) {
1893 status = PyDict_SetItem(d, key, value);
1894 Py_DECREF(key);
1895 if (status < 0)
1896 goto Fail;
1897 }
1898 } else {
1899 while ((key = PyIter_Next(it)) != NULL) {
1900 status = PyObject_SetItem(d, key, value);
1901 Py_DECREF(key);
1902 if (status < 0)
1903 goto Fail;
1904 }
1905 }
1906
1907 if (PyErr_Occurred())
1908 goto Fail;
1909 Py_DECREF(it);
1910 return d;
1911
1912Fail:
1913 Py_DECREF(it);
1914 Py_DECREF(d);
1915 return NULL;
1916}
1917
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001918/* Methods */
1919
1920static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001921dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001922{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001923 PyObject **values = mp->ma_values;
1924 PyDictKeysObject *keys = mp->ma_keys;
1925 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926 PyObject_GC_UnTrack(mp);
1927 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001928 if (values != NULL) {
1929 if (values != empty_values) {
Victor Stinner742da042016-09-07 17:40:12 -07001930 for (i = 0, n = mp->ma_keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001931 Py_XDECREF(values[i]);
1932 }
1933 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001935 DK_DECREF(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02001937 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001938 assert(keys->dk_refcnt == 1);
1939 DK_DECREF(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001940 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1942 free_list[numfree++] = mp;
1943 else
1944 Py_TYPE(mp)->tp_free((PyObject *)mp);
1945 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001946}
1947
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001948
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001949static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001950dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001951{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001953 PyObject *key = NULL, *value = NULL;
1954 _PyUnicodeWriter writer;
1955 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00001956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001957 i = Py_ReprEnter((PyObject *)mp);
1958 if (i != 0) {
1959 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1960 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001962 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001963 Py_ReprLeave((PyObject *)mp);
1964 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001965 }
Tim Petersa7259592001-06-16 05:11:17 +00001966
Victor Stinnerf91929b2013-11-19 13:07:38 +01001967 _PyUnicodeWriter_Init(&writer);
1968 writer.overallocate = 1;
1969 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
1970 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00001971
Victor Stinnerf91929b2013-11-19 13:07:38 +01001972 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
1973 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 /* Do repr() on each key+value pair, and insert ": " between them.
1976 Note that repr may mutate the dict. */
1977 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001978 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001980 PyObject *s;
1981 int res;
1982
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001983 /* Prevent repr from deleting key or value during key format. */
1984 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001985 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02001986
Victor Stinnerf91929b2013-11-19 13:07:38 +01001987 if (!first) {
1988 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
1989 goto error;
1990 }
1991 first = 0;
1992
1993 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001994 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01001995 goto error;
1996 res = _PyUnicodeWriter_WriteStr(&writer, s);
1997 Py_DECREF(s);
1998 if (res < 0)
1999 goto error;
2000
2001 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
2002 goto error;
2003
2004 s = PyObject_Repr(value);
2005 if (s == NULL)
2006 goto error;
2007 res = _PyUnicodeWriter_WriteStr(&writer, s);
2008 Py_DECREF(s);
2009 if (res < 0)
2010 goto error;
2011
2012 Py_CLEAR(key);
2013 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002014 }
Tim Petersa7259592001-06-16 05:11:17 +00002015
Victor Stinnerf91929b2013-11-19 13:07:38 +01002016 writer.overallocate = 0;
2017 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
2018 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00002019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002020 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01002021
2022 return _PyUnicodeWriter_Finish(&writer);
2023
2024error:
2025 Py_ReprLeave((PyObject *)mp);
2026 _PyUnicodeWriter_Dealloc(&writer);
2027 Py_XDECREF(key);
2028 Py_XDECREF(value);
2029 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002030}
2031
Martin v. Löwis18e16552006-02-15 17:27:45 +00002032static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002033dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002034{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002035 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002036}
2037
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002038static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002039dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002040{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002041 PyObject *v;
Victor Stinner742da042016-09-07 17:40:12 -07002042 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002043 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002044 PyObject **value_addr;
2045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002046 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002047 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048 hash = PyObject_Hash(key);
2049 if (hash == -1)
2050 return NULL;
2051 }
Victor Stinner742da042016-09-07 17:40:12 -07002052 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
2053 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002054 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002055 if (ix == DKIX_EMPTY || *value_addr == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002056 if (!PyDict_CheckExact(mp)) {
2057 /* Look up __missing__ method if we're a subclass. */
2058 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05002059 _Py_IDENTIFIER(__missing__);
2060 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 if (missing != NULL) {
2062 res = PyObject_CallFunctionObjArgs(missing,
2063 key, NULL);
2064 Py_DECREF(missing);
2065 return res;
2066 }
2067 else if (PyErr_Occurred())
2068 return NULL;
2069 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07002070 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071 return NULL;
2072 }
Victor Stinner742da042016-09-07 17:40:12 -07002073 v = *value_addr;
2074 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002075 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002076}
2077
2078static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002079dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002080{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002081 if (w == NULL)
2082 return PyDict_DelItem((PyObject *)mp, v);
2083 else
2084 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002085}
2086
Guido van Rossuma9e7a811997-05-13 21:02:11 +00002087static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002088 (lenfunc)dict_length, /*mp_length*/
2089 (binaryfunc)dict_subscript, /*mp_subscript*/
2090 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002091};
2092
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002093static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002094dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002095{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002096 PyObject *v;
2097 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002098 PyDictKeyEntry *ep;
2099 Py_ssize_t size, n, offset;
2100 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002101
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002102 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002103 n = mp->ma_used;
2104 v = PyList_New(n);
2105 if (v == NULL)
2106 return NULL;
2107 if (n != mp->ma_used) {
2108 /* Durnit. The allocations caused the dict to resize.
2109 * Just start over, this shouldn't normally happen.
2110 */
2111 Py_DECREF(v);
2112 goto again;
2113 }
Victor Stinner742da042016-09-07 17:40:12 -07002114 ep = DK_ENTRIES(mp->ma_keys);
2115 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002116 if (mp->ma_values) {
2117 value_ptr = mp->ma_values;
2118 offset = sizeof(PyObject *);
2119 }
2120 else {
2121 value_ptr = &ep[0].me_value;
2122 offset = sizeof(PyDictKeyEntry);
2123 }
2124 for (i = 0, j = 0; i < size; i++) {
2125 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002126 PyObject *key = ep[i].me_key;
2127 Py_INCREF(key);
2128 PyList_SET_ITEM(v, j, key);
2129 j++;
2130 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002131 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002132 }
2133 assert(j == n);
2134 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002135}
2136
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002137static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002138dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002139{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002140 PyObject *v;
2141 Py_ssize_t i, j;
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002142 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002143 Py_ssize_t size, n, offset;
2144 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002145
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002146 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002147 n = mp->ma_used;
2148 v = PyList_New(n);
2149 if (v == NULL)
2150 return NULL;
2151 if (n != mp->ma_used) {
2152 /* Durnit. The allocations caused the dict to resize.
2153 * Just start over, this shouldn't normally happen.
2154 */
2155 Py_DECREF(v);
2156 goto again;
2157 }
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002158 ep = DK_ENTRIES(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002159 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002160 if (mp->ma_values) {
2161 value_ptr = mp->ma_values;
2162 offset = sizeof(PyObject *);
2163 }
2164 else {
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002165 value_ptr = &ep[0].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002166 offset = sizeof(PyDictKeyEntry);
2167 }
2168 for (i = 0, j = 0; i < size; i++) {
2169 PyObject *value = *value_ptr;
2170 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2171 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002172 Py_INCREF(value);
2173 PyList_SET_ITEM(v, j, value);
2174 j++;
2175 }
2176 }
2177 assert(j == n);
2178 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002179}
2180
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002181static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002182dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002183{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002184 PyObject *v;
2185 Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002186 Py_ssize_t size, offset;
2187 PyObject *item, *key;
2188 PyDictKeyEntry *ep;
2189 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 /* Preallocate the list of tuples, to avoid allocations during
2192 * the loop over the items, which could trigger GC, which
2193 * could resize the dict. :-(
2194 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002195 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002196 n = mp->ma_used;
2197 v = PyList_New(n);
2198 if (v == NULL)
2199 return NULL;
2200 for (i = 0; i < n; i++) {
2201 item = PyTuple_New(2);
2202 if (item == NULL) {
2203 Py_DECREF(v);
2204 return NULL;
2205 }
2206 PyList_SET_ITEM(v, i, item);
2207 }
2208 if (n != mp->ma_used) {
2209 /* Durnit. The allocations caused the dict to resize.
2210 * Just start over, this shouldn't normally happen.
2211 */
2212 Py_DECREF(v);
2213 goto again;
2214 }
2215 /* Nothing we do below makes any function calls. */
Victor Stinner742da042016-09-07 17:40:12 -07002216 ep = DK_ENTRIES(mp->ma_keys);
2217 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002218 if (mp->ma_values) {
2219 value_ptr = mp->ma_values;
2220 offset = sizeof(PyObject *);
2221 }
2222 else {
2223 value_ptr = &ep[0].me_value;
2224 offset = sizeof(PyDictKeyEntry);
2225 }
2226 for (i = 0, j = 0; i < size; i++) {
2227 PyObject *value = *value_ptr;
2228 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2229 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 key = ep[i].me_key;
2231 item = PyList_GET_ITEM(v, j);
2232 Py_INCREF(key);
2233 PyTuple_SET_ITEM(item, 0, key);
2234 Py_INCREF(value);
2235 PyTuple_SET_ITEM(item, 1, value);
2236 j++;
2237 }
2238 }
2239 assert(j == n);
2240 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002241}
2242
Larry Hastings5c661892014-01-24 06:17:25 -08002243/*[clinic input]
2244@classmethod
2245dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08002246 iterable: object
2247 value: object=None
2248 /
2249
2250Returns a new dict with keys from iterable and values equal to value.
2251[clinic start generated code]*/
2252
Larry Hastings5c661892014-01-24 06:17:25 -08002253static PyObject *
2254dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002255/*[clinic end generated code: output=8fb98e4b10384999 input=b85a667f9bf4669d]*/
Larry Hastings5c661892014-01-24 06:17:25 -08002256{
Eric Snow96c6af92015-05-29 22:21:39 -06002257 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002258}
2259
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002260static int
Victor Stinner742da042016-09-07 17:40:12 -07002261dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
2262 const char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002264 PyObject *arg = NULL;
2265 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002267 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
2268 result = -1;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002271 _Py_IDENTIFIER(keys);
2272 if (_PyObject_HasAttrId(arg, &PyId_keys))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002273 result = PyDict_Merge(self, arg, 1);
2274 else
2275 result = PyDict_MergeFromSeq2(self, arg, 1);
2276 }
2277 if (result == 0 && kwds != NULL) {
2278 if (PyArg_ValidateKeywordArguments(kwds))
2279 result = PyDict_Merge(self, kwds, 1);
2280 else
2281 result = -1;
2282 }
2283 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002284}
2285
2286static PyObject *
2287dict_update(PyObject *self, PyObject *args, PyObject *kwds)
2288{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002289 if (dict_update_common(self, args, kwds, "update") != -1)
2290 Py_RETURN_NONE;
2291 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002292}
2293
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002294/* Update unconditionally replaces existing items.
2295 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00002296 otherwise it leaves existing items unchanged.
2297
2298 PyDict_{Update,Merge} update/merge from a mapping object.
2299
Tim Petersf582b822001-12-11 18:51:08 +00002300 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00002301 producing iterable objects of length 2.
2302*/
2303
Tim Petersf582b822001-12-11 18:51:08 +00002304int
Tim Peters1fc240e2001-10-26 05:06:50 +00002305PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
2306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 PyObject *it; /* iter(seq2) */
2308 Py_ssize_t i; /* index into seq2 of current element */
2309 PyObject *item; /* seq2[i] */
2310 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00002311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002312 assert(d != NULL);
2313 assert(PyDict_Check(d));
2314 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00002315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002316 it = PyObject_GetIter(seq2);
2317 if (it == NULL)
2318 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002320 for (i = 0; ; ++i) {
2321 PyObject *key, *value;
2322 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00002323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002324 fast = NULL;
2325 item = PyIter_Next(it);
2326 if (item == NULL) {
2327 if (PyErr_Occurred())
2328 goto Fail;
2329 break;
2330 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002332 /* Convert item to sequence, and verify length 2. */
2333 fast = PySequence_Fast(item, "");
2334 if (fast == NULL) {
2335 if (PyErr_ExceptionMatches(PyExc_TypeError))
2336 PyErr_Format(PyExc_TypeError,
2337 "cannot convert dictionary update "
2338 "sequence element #%zd to a sequence",
2339 i);
2340 goto Fail;
2341 }
2342 n = PySequence_Fast_GET_SIZE(fast);
2343 if (n != 2) {
2344 PyErr_Format(PyExc_ValueError,
2345 "dictionary update sequence element #%zd "
2346 "has length %zd; 2 is required",
2347 i, n);
2348 goto Fail;
2349 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002351 /* Update/merge with this (key, value) pair. */
2352 key = PySequence_Fast_GET_ITEM(fast, 0);
2353 value = PySequence_Fast_GET_ITEM(fast, 1);
2354 if (override || PyDict_GetItem(d, key) == NULL) {
2355 int status = PyDict_SetItem(d, key, value);
2356 if (status < 0)
2357 goto Fail;
2358 }
2359 Py_DECREF(fast);
2360 Py_DECREF(item);
2361 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002363 i = 0;
Victor Stinner611b0fa2016-09-14 15:02:01 +02002364 assert(_PyDict_CheckConsistency((PyDictObject *)d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002366Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 Py_XDECREF(item);
2368 Py_XDECREF(fast);
2369 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002370Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371 Py_DECREF(it);
2372 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002373}
2374
doko@ubuntu.comde69ee72016-10-11 08:04:02 +02002375static int
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002376dict_merge(PyObject *a, PyObject *b, int override)
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002377{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002378 PyDictObject *mp, *other;
2379 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -07002380 PyDictKeyEntry *entry, *ep0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002381
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002382 assert(0 <= override && override <= 2);
2383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002384 /* We accept for the argument either a concrete dictionary object,
2385 * or an abstract "mapping" object. For the former, we can do
2386 * things quite efficiently. For the latter, we only require that
2387 * PyMapping_Keys() and PyObject_GetItem() be supported.
2388 */
2389 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2390 PyErr_BadInternalCall();
2391 return -1;
2392 }
2393 mp = (PyDictObject*)a;
2394 if (PyDict_Check(b)) {
2395 other = (PyDictObject*)b;
2396 if (other == mp || other->ma_used == 0)
2397 /* a.update(a) or a.update({}); nothing to do */
2398 return 0;
2399 if (mp->ma_used == 0)
2400 /* Since the target dict is empty, PyDict_GetItem()
2401 * always returns NULL. Setting override to 1
2402 * skips the unnecessary test.
2403 */
2404 override = 1;
2405 /* Do one big resize at the start, rather than
2406 * incrementally resizing as we insert new items. Expect
2407 * that there will be no (or few) overlapping keys.
2408 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002409 if (mp->ma_keys->dk_usable * 3 < other->ma_used * 2)
2410 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002411 return -1;
Victor Stinner742da042016-09-07 17:40:12 -07002412 ep0 = DK_ENTRIES(other->ma_keys);
2413 for (i = 0, n = other->ma_keys->dk_nentries; i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002414 PyObject *key, *value;
2415 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002416 entry = &ep0[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002417 key = entry->me_key;
2418 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002419 if (other->ma_values)
2420 value = other->ma_values[i];
2421 else
2422 value = entry->me_value;
2423
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002424 if (value != NULL) {
2425 int err = 0;
2426 Py_INCREF(key);
2427 Py_INCREF(value);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002428 if (override == 1 || _PyDict_GetItem_KnownHash(a, key, hash) == NULL)
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002429 err = insertdict(mp, key, hash, value);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002430 else if (override != 0) {
2431 _PyErr_SetKeyError(key);
2432 Py_DECREF(value);
2433 Py_DECREF(key);
2434 return -1;
2435 }
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002436 Py_DECREF(value);
2437 Py_DECREF(key);
2438 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002440
Victor Stinner742da042016-09-07 17:40:12 -07002441 if (n != other->ma_keys->dk_nentries) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002442 PyErr_SetString(PyExc_RuntimeError,
2443 "dict mutated during update");
2444 return -1;
2445 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002446 }
2447 }
2448 }
2449 else {
2450 /* Do it the generic, slower way */
2451 PyObject *keys = PyMapping_Keys(b);
2452 PyObject *iter;
2453 PyObject *key, *value;
2454 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002456 if (keys == NULL)
2457 /* Docstring says this is equivalent to E.keys() so
2458 * if E doesn't have a .keys() method we want
2459 * AttributeError to percolate up. Might as well
2460 * do the same for any other error.
2461 */
2462 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002464 iter = PyObject_GetIter(keys);
2465 Py_DECREF(keys);
2466 if (iter == NULL)
2467 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002469 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002470 if (override != 1 && PyDict_GetItem(a, key) != NULL) {
2471 if (override != 0) {
2472 _PyErr_SetKeyError(key);
2473 Py_DECREF(key);
2474 Py_DECREF(iter);
2475 return -1;
2476 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002477 Py_DECREF(key);
2478 continue;
2479 }
2480 value = PyObject_GetItem(b, key);
2481 if (value == NULL) {
2482 Py_DECREF(iter);
2483 Py_DECREF(key);
2484 return -1;
2485 }
2486 status = PyDict_SetItem(a, key, value);
2487 Py_DECREF(key);
2488 Py_DECREF(value);
2489 if (status < 0) {
2490 Py_DECREF(iter);
2491 return -1;
2492 }
2493 }
2494 Py_DECREF(iter);
2495 if (PyErr_Occurred())
2496 /* Iterator completed, via error */
2497 return -1;
2498 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02002499 assert(_PyDict_CheckConsistency((PyDictObject *)a));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002500 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002501}
2502
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002503int
2504PyDict_Update(PyObject *a, PyObject *b)
2505{
2506 return dict_merge(a, b, 1);
2507}
2508
2509int
2510PyDict_Merge(PyObject *a, PyObject *b, int override)
2511{
2512 /* XXX Deprecate override not in (0, 1). */
2513 return dict_merge(a, b, override != 0);
2514}
2515
2516int
2517_PyDict_MergeEx(PyObject *a, PyObject *b, int override)
2518{
2519 return dict_merge(a, b, override);
2520}
2521
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002522static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002523dict_copy(PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002524{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002525 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002526}
2527
2528PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002529PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002530{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002531 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002532 PyDictObject *mp;
2533 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002535 if (o == NULL || !PyDict_Check(o)) {
2536 PyErr_BadInternalCall();
2537 return NULL;
2538 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002539 mp = (PyDictObject *)o;
2540 if (_PyDict_HasSplitTable(mp)) {
2541 PyDictObject *split_copy;
Victor Stinner742da042016-09-07 17:40:12 -07002542 Py_ssize_t size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
2543 PyObject **newvalues;
2544 newvalues = new_values(size);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002545 if (newvalues == NULL)
2546 return PyErr_NoMemory();
2547 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2548 if (split_copy == NULL) {
2549 free_values(newvalues);
2550 return NULL;
2551 }
2552 split_copy->ma_values = newvalues;
2553 split_copy->ma_keys = mp->ma_keys;
2554 split_copy->ma_used = mp->ma_used;
2555 DK_INCREF(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002556 for (i = 0, n = size; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002557 PyObject *value = mp->ma_values[i];
2558 Py_XINCREF(value);
2559 split_copy->ma_values[i] = value;
2560 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002561 if (_PyObject_GC_IS_TRACKED(mp))
2562 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002563 return (PyObject *)split_copy;
2564 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002565 copy = PyDict_New();
2566 if (copy == NULL)
2567 return NULL;
2568 if (PyDict_Merge(copy, o, 1) == 0)
2569 return copy;
2570 Py_DECREF(copy);
2571 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002572}
2573
Martin v. Löwis18e16552006-02-15 17:27:45 +00002574Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002575PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002576{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002577 if (mp == NULL || !PyDict_Check(mp)) {
2578 PyErr_BadInternalCall();
2579 return -1;
2580 }
2581 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002582}
2583
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002584PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002585PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002586{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002587 if (mp == NULL || !PyDict_Check(mp)) {
2588 PyErr_BadInternalCall();
2589 return NULL;
2590 }
2591 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002592}
2593
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002594PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002595PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002596{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002597 if (mp == NULL || !PyDict_Check(mp)) {
2598 PyErr_BadInternalCall();
2599 return NULL;
2600 }
2601 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002602}
2603
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002604PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002605PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002606{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002607 if (mp == NULL || !PyDict_Check(mp)) {
2608 PyErr_BadInternalCall();
2609 return NULL;
2610 }
2611 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002612}
2613
Tim Peterse63415e2001-05-08 04:38:29 +00002614/* Return 1 if dicts equal, 0 if not, -1 if error.
2615 * Gets out as soon as any difference is detected.
2616 * Uses only Py_EQ comparison.
2617 */
2618static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002619dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002620{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002621 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002623 if (a->ma_used != b->ma_used)
2624 /* can't be equal if # of entries differ */
2625 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002626 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Victor Stinner742da042016-09-07 17:40:12 -07002627 for (i = 0; i < a->ma_keys->dk_nentries; i++) {
2628 PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002629 PyObject *aval;
2630 if (a->ma_values)
2631 aval = a->ma_values[i];
2632 else
2633 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002634 if (aval != NULL) {
2635 int cmp;
2636 PyObject *bval;
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002637 PyObject **vaddr;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002638 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002639 /* temporarily bump aval's refcount to ensure it stays
2640 alive until we're done with it */
2641 Py_INCREF(aval);
2642 /* ditto for key */
2643 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002644 /* reuse the known hash value */
Victor Stinner742da042016-09-07 17:40:12 -07002645 if ((b->ma_keys->dk_lookup)(b, key, ep->me_hash, &vaddr, NULL) < 0)
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002646 bval = NULL;
2647 else
2648 bval = *vaddr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002649 Py_DECREF(key);
2650 if (bval == NULL) {
2651 Py_DECREF(aval);
2652 if (PyErr_Occurred())
2653 return -1;
2654 return 0;
2655 }
2656 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
2657 Py_DECREF(aval);
2658 if (cmp <= 0) /* error or not equal */
2659 return cmp;
2660 }
2661 }
2662 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002663}
Tim Peterse63415e2001-05-08 04:38:29 +00002664
2665static PyObject *
2666dict_richcompare(PyObject *v, PyObject *w, int op)
2667{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002668 int cmp;
2669 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002671 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2672 res = Py_NotImplemented;
2673 }
2674 else if (op == Py_EQ || op == Py_NE) {
2675 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2676 if (cmp < 0)
2677 return NULL;
2678 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2679 }
2680 else
2681 res = Py_NotImplemented;
2682 Py_INCREF(res);
2683 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002684}
Tim Peterse63415e2001-05-08 04:38:29 +00002685
Larry Hastings61272b72014-01-07 12:41:53 -08002686/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002687
2688@coexist
2689dict.__contains__
2690
2691 key: object
2692 /
2693
Meador Ingee02de8c2014-01-14 16:48:31 -06002694True if D has a key k, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002695[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002696
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002697static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002698dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002699/*[clinic end generated code: output=a3d03db709ed6e6b input=b852b2a19b51ab24]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002700{
Larry Hastingsc2047262014-01-25 20:43:29 -08002701 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002702 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002703 Py_ssize_t ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002704 PyObject **value_addr;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002706 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002707 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002708 hash = PyObject_Hash(key);
2709 if (hash == -1)
2710 return NULL;
2711 }
Victor Stinner742da042016-09-07 17:40:12 -07002712 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
2713 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002714 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002715 if (ix == DKIX_EMPTY || *value_addr == NULL)
2716 Py_RETURN_FALSE;
2717 Py_RETURN_TRUE;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002718}
2719
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002720static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002721dict_get(PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00002722{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002723 PyObject *key;
2724 PyObject *failobj = Py_None;
2725 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002726 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002727 Py_ssize_t ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002728 PyObject **value_addr;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002730 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
2731 return NULL;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002733 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002734 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002735 hash = PyObject_Hash(key);
2736 if (hash == -1)
2737 return NULL;
2738 }
Victor Stinner742da042016-09-07 17:40:12 -07002739 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
2740 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002741 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002742 if (ix == DKIX_EMPTY || *value_addr == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002743 val = failobj;
Victor Stinner742da042016-09-07 17:40:12 -07002744 else
2745 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002746 Py_INCREF(val);
2747 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002748}
2749
Benjamin Peterson00e98862013-03-07 22:16:29 -05002750PyObject *
2751PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002752{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002753 PyDictObject *mp = (PyDictObject *)d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002754 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002755 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002756 Py_ssize_t hashpos, ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002757 PyDictKeyEntry *ep;
2758 PyObject **value_addr;
Guido van Rossum164452c2000-08-08 16:12:54 +00002759
Benjamin Peterson00e98862013-03-07 22:16:29 -05002760 if (!PyDict_Check(d)) {
2761 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002762 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002763 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002764 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002765 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002766 hash = PyObject_Hash(key);
2767 if (hash == -1)
2768 return NULL;
2769 }
Victor Stinner742da042016-09-07 17:40:12 -07002770 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
2771 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002772 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002773 if (ix == DKIX_EMPTY || *value_addr == NULL) {
2774 val = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002775 if (mp->ma_keys->dk_usable <= 0) {
2776 /* Need to resize. */
Victor Stinner3c336c52016-09-12 14:17:40 +02002777 if (insertion_resize(mp) < 0) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002778 return NULL;
Victor Stinner3c336c52016-09-12 14:17:40 +02002779 }
Victor Stinner742da042016-09-07 17:40:12 -07002780 find_empty_slot(mp, key, hash, &value_addr, &hashpos);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002781 }
Victor Stinner742da042016-09-07 17:40:12 -07002782 ix = mp->ma_keys->dk_nentries;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002783 Py_INCREF(defaultobj);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002784 Py_INCREF(key);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002785 MAINTAIN_TRACKING(mp, key, defaultobj);
Victor Stinner742da042016-09-07 17:40:12 -07002786 dk_set_index(mp->ma_keys, hashpos, ix);
2787 ep = &DK_ENTRIES(mp->ma_keys)[ix];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002788 ep->me_key = key;
2789 ep->me_hash = hash;
Victor Stinner742da042016-09-07 17:40:12 -07002790 if (mp->ma_values) {
2791 mp->ma_values[ix] = val;
2792 }
2793 else {
2794 ep->me_value = val;
2795 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002796 mp->ma_keys->dk_usable--;
Victor Stinner742da042016-09-07 17:40:12 -07002797 mp->ma_keys->dk_nentries++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002798 mp->ma_used++;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002799 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02002800 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002801 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02002802 else {
Victor Stinner742da042016-09-07 17:40:12 -07002803 val = *value_addr;
Victor Stinner611b0fa2016-09-14 15:02:01 +02002804 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002805 return val;
Guido van Rossum164452c2000-08-08 16:12:54 +00002806}
2807
Benjamin Peterson00e98862013-03-07 22:16:29 -05002808static PyObject *
2809dict_setdefault(PyDictObject *mp, PyObject *args)
2810{
2811 PyObject *key, *val;
2812 PyObject *defaultobj = Py_None;
2813
2814 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &defaultobj))
2815 return NULL;
2816
Benjamin Peterson55898502013-03-08 08:36:49 -05002817 val = PyDict_SetDefault((PyObject *)mp, key, defaultobj);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002818 Py_XINCREF(val);
2819 return val;
2820}
Guido van Rossum164452c2000-08-08 16:12:54 +00002821
2822static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002823dict_clear(PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002824{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002825 PyDict_Clear((PyObject *)mp);
2826 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002827}
2828
Guido van Rossumba6ab842000-12-12 22:02:18 +00002829static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002830dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002831{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00002833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002834 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2835 return NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06002836
2837 return _PyDict_Pop(mp, key, deflt);
Guido van Rossume027d982002-04-12 15:11:59 +00002838}
2839
2840static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002841dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002842{
Victor Stinner742da042016-09-07 17:40:12 -07002843 Py_ssize_t i, j;
2844 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002845 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002847 /* Allocate the result tuple before checking the size. Believe it
2848 * or not, this allocation could trigger a garbage collection which
2849 * could empty the dict, so if we checked the size first and that
2850 * happened, the result would be an infinite loop (searching for an
2851 * entry that no longer exists). Note that the usual popitem()
2852 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2853 * tuple away if the dict *is* empty isn't a significant
2854 * inefficiency -- possible, but unlikely in practice.
2855 */
2856 res = PyTuple_New(2);
2857 if (res == NULL)
2858 return NULL;
2859 if (mp->ma_used == 0) {
2860 Py_DECREF(res);
2861 PyErr_SetString(PyExc_KeyError,
2862 "popitem(): dictionary is empty");
2863 return NULL;
2864 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002865 /* Convert split table to combined table */
2866 if (mp->ma_keys->dk_lookup == lookdict_split) {
2867 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2868 Py_DECREF(res);
2869 return NULL;
2870 }
2871 }
2872 ENSURE_ALLOWS_DELETIONS(mp);
Victor Stinner742da042016-09-07 17:40:12 -07002873
2874 /* Pop last item */
2875 ep0 = DK_ENTRIES(mp->ma_keys);
2876 i = mp->ma_keys->dk_nentries - 1;
2877 while (i >= 0 && ep0[i].me_value == NULL) {
2878 i--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 }
Victor Stinner742da042016-09-07 17:40:12 -07002880 assert(i >= 0);
2881
2882 ep = &ep0[i];
2883 j = lookdict_index(mp->ma_keys, ep->me_hash, i);
2884 assert(j >= 0);
2885 assert(dk_get_index(mp->ma_keys, j) == i);
2886 dk_set_index(mp->ma_keys, j, DKIX_DUMMY);
2887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002888 PyTuple_SET_ITEM(res, 0, ep->me_key);
2889 PyTuple_SET_ITEM(res, 1, ep->me_value);
Victor Stinner742da042016-09-07 17:40:12 -07002890 ep->me_key = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002891 ep->me_value = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002892 /* We can't dk_usable++ since there is DKIX_DUMMY in indices */
2893 mp->ma_keys->dk_nentries = i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002894 mp->ma_used--;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07002895 mp->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner611b0fa2016-09-14 15:02:01 +02002896 assert(_PyDict_CheckConsistency(mp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002897 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002898}
2899
Jeremy Hylton8caad492000-06-23 14:18:11 +00002900static int
2901dict_traverse(PyObject *op, visitproc visit, void *arg)
2902{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002903 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson55f44522016-09-05 12:12:59 -07002904 PyDictKeysObject *keys = mp->ma_keys;
Serhiy Storchaka46825d22016-09-26 21:29:34 +03002905 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Victor Stinner742da042016-09-07 17:40:12 -07002906 Py_ssize_t i, n = keys->dk_nentries;
2907
Benjamin Peterson55f44522016-09-05 12:12:59 -07002908 if (keys->dk_lookup == lookdict) {
2909 for (i = 0; i < n; i++) {
2910 if (entries[i].me_value != NULL) {
2911 Py_VISIT(entries[i].me_value);
2912 Py_VISIT(entries[i].me_key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002913 }
2914 }
Victor Stinner742da042016-09-07 17:40:12 -07002915 }
2916 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002917 if (mp->ma_values != NULL) {
Benjamin Peterson55f44522016-09-05 12:12:59 -07002918 for (i = 0; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002919 Py_VISIT(mp->ma_values[i]);
2920 }
2921 }
2922 else {
Benjamin Peterson55f44522016-09-05 12:12:59 -07002923 for (i = 0; i < n; i++) {
2924 Py_VISIT(entries[i].me_value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002925 }
2926 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002927 }
2928 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002929}
2930
2931static int
2932dict_tp_clear(PyObject *op)
2933{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002934 PyDict_Clear(op);
2935 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002936}
2937
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002938static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002939
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002940Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06002941_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002942{
Victor Stinner742da042016-09-07 17:40:12 -07002943 Py_ssize_t size, usable, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002944
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002945 size = DK_SIZE(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002946 usable = USABLE_FRACTION(size);
2947
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002948 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002949 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -07002950 res += usable * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002951 /* If the dictionary is split, the keys portion is accounted-for
2952 in the type object. */
2953 if (mp->ma_keys->dk_refcnt == 1)
Victor Stinner98ee9d52016-09-08 09:33:56 -07002954 res += (sizeof(PyDictKeysObject)
2955 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
2956 + DK_IXSIZE(mp->ma_keys) * size
2957 + sizeof(PyDictKeyEntry) * usable);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002958 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002959}
2960
2961Py_ssize_t
2962_PyDict_KeysSize(PyDictKeysObject *keys)
2963{
Victor Stinner98ee9d52016-09-08 09:33:56 -07002964 return (sizeof(PyDictKeysObject)
2965 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
2966 + DK_IXSIZE(keys) * DK_SIZE(keys)
2967 + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002968}
2969
doko@ubuntu.com17210f52016-01-14 14:04:59 +01002970static PyObject *
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002971dict_sizeof(PyDictObject *mp)
2972{
2973 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
2974}
2975
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002976PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
2977
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002978PyDoc_STRVAR(sizeof__doc__,
2979"D.__sizeof__() -> size of D in memory, in bytes");
2980
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002981PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002982"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002983
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002984PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002985"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 +00002986
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002987PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002988"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002989If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00002990
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002991PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00002992"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000029932-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002994
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002995PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04002996"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
2997If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
2998If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
2999In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00003000
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003001PyDoc_STRVAR(clear__doc__,
3002"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00003003
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003004PyDoc_STRVAR(copy__doc__,
3005"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00003006
Guido van Rossumb90c8482007-02-10 01:11:45 +00003007/* Forward */
3008static PyObject *dictkeys_new(PyObject *);
3009static PyObject *dictitems_new(PyObject *);
3010static PyObject *dictvalues_new(PyObject *);
3011
Guido van Rossum45c85d12007-07-27 16:31:40 +00003012PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003013 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003014PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003015 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00003016PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003017 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00003018
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003019static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07003020 DICT___CONTAINS___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003021 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
3022 getitem__doc__},
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02003023 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003024 sizeof__doc__},
3025 {"get", (PyCFunction)dict_get, METH_VARARGS,
3026 get__doc__},
3027 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
3028 setdefault_doc__},
3029 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
3030 pop__doc__},
3031 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
3032 popitem__doc__},
3033 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
3034 keys__doc__},
3035 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
3036 items__doc__},
3037 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
3038 values__doc__},
3039 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
3040 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08003041 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003042 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
3043 clear__doc__},
3044 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
3045 copy__doc__},
3046 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003047};
3048
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00003049/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00003050int
3051PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003052{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003053 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07003054 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003055 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003056 PyObject **value_addr;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003058 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003059 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003060 hash = PyObject_Hash(key);
3061 if (hash == -1)
3062 return -1;
3063 }
Victor Stinner742da042016-09-07 17:40:12 -07003064 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
3065 if (ix == DKIX_ERROR)
3066 return -1;
3067 return (ix != DKIX_EMPTY && *value_addr != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003068}
3069
Thomas Wouterscf297e42007-02-23 15:07:44 +00003070/* Internal version of PyDict_Contains used when the hash value is already known */
3071int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003072_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00003073{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003074 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003075 PyObject **value_addr;
Victor Stinner742da042016-09-07 17:40:12 -07003076 Py_ssize_t ix;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003077
Victor Stinner742da042016-09-07 17:40:12 -07003078 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
3079 if (ix == DKIX_ERROR)
3080 return -1;
3081 return (ix != DKIX_EMPTY && *value_addr != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00003082}
3083
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003084/* Hack to implement "key in dict" */
3085static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003086 0, /* sq_length */
3087 0, /* sq_concat */
3088 0, /* sq_repeat */
3089 0, /* sq_item */
3090 0, /* sq_slice */
3091 0, /* sq_ass_item */
3092 0, /* sq_ass_slice */
3093 PyDict_Contains, /* sq_contains */
3094 0, /* sq_inplace_concat */
3095 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00003096};
3097
Guido van Rossum09e563a2001-05-01 12:10:21 +00003098static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003099dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3100{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003101 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003102 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003104 assert(type != NULL && type->tp_alloc != NULL);
3105 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02003106 if (self == NULL)
3107 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02003108 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003109
Victor Stinnera9f61a52013-07-16 22:17:26 +02003110 /* The object has been implicitly tracked by tp_alloc */
3111 if (type == &PyDict_Type)
3112 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003113
3114 d->ma_used = 0;
Victor Stinner3b6a6b42016-09-08 12:51:24 -07003115 d->ma_version_tag = DICT_NEXT_VERSION();
Victor Stinner742da042016-09-07 17:40:12 -07003116 d->ma_keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02003117 if (d->ma_keys == NULL) {
3118 Py_DECREF(self);
3119 return NULL;
3120 }
Victor Stinner611b0fa2016-09-14 15:02:01 +02003121 assert(_PyDict_CheckConsistency(d));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003122 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003123}
3124
Tim Peters25786c02001-09-02 08:22:48 +00003125static int
3126dict_init(PyObject *self, PyObject *args, PyObject *kwds)
3127{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003128 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00003129}
3130
Tim Peters6d6c1a32001-08-02 04:15:00 +00003131static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003132dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00003133{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003134 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00003135}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003136
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003137PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00003138"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00003139"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003140" (key, value) pairs\n"
3141"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00003142" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00003143" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00003144" d[k] = v\n"
3145"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
3146" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00003147
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003148PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003149 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3150 "dict",
3151 sizeof(PyDictObject),
3152 0,
3153 (destructor)dict_dealloc, /* tp_dealloc */
3154 0, /* tp_print */
3155 0, /* tp_getattr */
3156 0, /* tp_setattr */
3157 0, /* tp_reserved */
3158 (reprfunc)dict_repr, /* tp_repr */
3159 0, /* tp_as_number */
3160 &dict_as_sequence, /* tp_as_sequence */
3161 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003162 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003163 0, /* tp_call */
3164 0, /* tp_str */
3165 PyObject_GenericGetAttr, /* tp_getattro */
3166 0, /* tp_setattro */
3167 0, /* tp_as_buffer */
3168 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3169 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
3170 dictionary_doc, /* tp_doc */
3171 dict_traverse, /* tp_traverse */
3172 dict_tp_clear, /* tp_clear */
3173 dict_richcompare, /* tp_richcompare */
3174 0, /* tp_weaklistoffset */
3175 (getiterfunc)dict_iter, /* tp_iter */
3176 0, /* tp_iternext */
3177 mapp_methods, /* tp_methods */
3178 0, /* tp_members */
3179 0, /* tp_getset */
3180 0, /* tp_base */
3181 0, /* tp_dict */
3182 0, /* tp_descr_get */
3183 0, /* tp_descr_set */
3184 0, /* tp_dictoffset */
3185 dict_init, /* tp_init */
3186 PyType_GenericAlloc, /* tp_alloc */
3187 dict_new, /* tp_new */
3188 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003189};
3190
Victor Stinner3c1e4812012-03-26 22:10:51 +02003191PyObject *
3192_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
3193{
3194 PyObject *kv;
3195 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02003196 if (kv == NULL) {
3197 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003198 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02003199 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02003200 return PyDict_GetItem(dp, kv);
3201}
3202
Guido van Rossum3cca2451997-05-16 14:23:33 +00003203/* For backward compatibility with old dictionary interface */
3204
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003205PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003206PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003207{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003208 PyObject *kv, *rv;
3209 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003210 if (kv == NULL) {
3211 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003212 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003213 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003214 rv = PyDict_GetItem(v, kv);
3215 Py_DECREF(kv);
3216 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003217}
3218
3219int
Victor Stinner3c1e4812012-03-26 22:10:51 +02003220_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
3221{
3222 PyObject *kv;
3223 kv = _PyUnicode_FromId(key); /* borrowed */
3224 if (kv == NULL)
3225 return -1;
3226 return PyDict_SetItem(v, kv, item);
3227}
3228
3229int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003230PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003231{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003232 PyObject *kv;
3233 int err;
3234 kv = PyUnicode_FromString(key);
3235 if (kv == NULL)
3236 return -1;
3237 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
3238 err = PyDict_SetItem(v, kv, item);
3239 Py_DECREF(kv);
3240 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003241}
3242
3243int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01003244_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
3245{
3246 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3247 if (kv == NULL)
3248 return -1;
3249 return PyDict_DelItem(v, kv);
3250}
3251
3252int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003253PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003254{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003255 PyObject *kv;
3256 int err;
3257 kv = PyUnicode_FromString(key);
3258 if (kv == NULL)
3259 return -1;
3260 err = PyDict_DelItem(v, kv);
3261 Py_DECREF(kv);
3262 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003263}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003264
Raymond Hettinger019a1482004-03-18 02:41:19 +00003265/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003266
3267typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003268 PyObject_HEAD
3269 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
3270 Py_ssize_t di_used;
3271 Py_ssize_t di_pos;
3272 PyObject* di_result; /* reusable result tuple for iteritems */
3273 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003274} dictiterobject;
3275
3276static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003277dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003278{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003279 dictiterobject *di;
3280 di = PyObject_GC_New(dictiterobject, itertype);
3281 if (di == NULL)
3282 return NULL;
3283 Py_INCREF(dict);
3284 di->di_dict = dict;
3285 di->di_used = dict->ma_used;
3286 di->di_pos = 0;
3287 di->len = dict->ma_used;
3288 if (itertype == &PyDictIterItem_Type) {
3289 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
3290 if (di->di_result == NULL) {
3291 Py_DECREF(di);
3292 return NULL;
3293 }
3294 }
3295 else
3296 di->di_result = NULL;
3297 _PyObject_GC_TRACK(di);
3298 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003299}
3300
3301static void
3302dictiter_dealloc(dictiterobject *di)
3303{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003304 Py_XDECREF(di->di_dict);
3305 Py_XDECREF(di->di_result);
3306 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003307}
3308
3309static int
3310dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
3311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003312 Py_VISIT(di->di_dict);
3313 Py_VISIT(di->di_result);
3314 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003315}
3316
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003317static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003318dictiter_len(dictiterobject *di)
3319{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003320 Py_ssize_t len = 0;
3321 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
3322 len = di->len;
3323 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003324}
3325
Guido van Rossumb90c8482007-02-10 01:11:45 +00003326PyDoc_STRVAR(length_hint_doc,
3327 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003328
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003329static PyObject *
3330dictiter_reduce(dictiterobject *di);
3331
3332PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3333
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003334static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003335 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
3336 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003337 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
3338 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003339 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003340};
3341
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003342static PyObject*
3343dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00003344{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003345 PyObject *key;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003346 Py_ssize_t i, n;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003347 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003348 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00003349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003350 if (d == NULL)
3351 return NULL;
3352 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00003353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003354 if (di->di_used != d->ma_used) {
3355 PyErr_SetString(PyExc_RuntimeError,
3356 "dictionary changed size during iteration");
3357 di->di_used = -1; /* Make this state sticky */
3358 return NULL;
3359 }
Guido van Rossum2147df72002-07-16 20:30:22 +00003360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003361 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003362 k = d->ma_keys;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003363 n = k->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003364 if (d->ma_values) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003365 PyObject **value_ptr = &d->ma_values[i];
3366 while (i < n && *value_ptr == NULL) {
3367 value_ptr++;
3368 i++;
3369 }
3370 if (i >= n)
3371 goto fail;
3372 key = DK_ENTRIES(k)[i].me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003373 }
3374 else {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003375 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(k)[i];
3376 while (i < n && entry_ptr->me_value == NULL) {
3377 entry_ptr++;
3378 i++;
3379 }
3380 if (i >= n)
3381 goto fail;
3382 key = entry_ptr->me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003383 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003384 di->di_pos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003385 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003386 Py_INCREF(key);
3387 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003388
3389fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003390 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003391 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003392 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003393}
3394
Raymond Hettinger019a1482004-03-18 02:41:19 +00003395PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003396 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3397 "dict_keyiterator", /* tp_name */
3398 sizeof(dictiterobject), /* tp_basicsize */
3399 0, /* tp_itemsize */
3400 /* methods */
3401 (destructor)dictiter_dealloc, /* tp_dealloc */
3402 0, /* tp_print */
3403 0, /* tp_getattr */
3404 0, /* tp_setattr */
3405 0, /* tp_reserved */
3406 0, /* tp_repr */
3407 0, /* tp_as_number */
3408 0, /* tp_as_sequence */
3409 0, /* tp_as_mapping */
3410 0, /* tp_hash */
3411 0, /* tp_call */
3412 0, /* tp_str */
3413 PyObject_GenericGetAttr, /* tp_getattro */
3414 0, /* tp_setattro */
3415 0, /* tp_as_buffer */
3416 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3417 0, /* tp_doc */
3418 (traverseproc)dictiter_traverse, /* tp_traverse */
3419 0, /* tp_clear */
3420 0, /* tp_richcompare */
3421 0, /* tp_weaklistoffset */
3422 PyObject_SelfIter, /* tp_iter */
3423 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3424 dictiter_methods, /* tp_methods */
3425 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003426};
3427
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003428static PyObject *
3429dictiter_iternextvalue(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003430{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003431 PyObject *value;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003432 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003433 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003435 if (d == NULL)
3436 return NULL;
3437 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003439 if (di->di_used != d->ma_used) {
3440 PyErr_SetString(PyExc_RuntimeError,
3441 "dictionary changed size during iteration");
3442 di->di_used = -1; /* Make this state sticky */
3443 return NULL;
3444 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003446 i = di->di_pos;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003447 n = d->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003448 if (d->ma_values) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003449 PyObject **value_ptr = &d->ma_values[i];
3450 while (i < n && *value_ptr == NULL) {
3451 value_ptr++;
3452 i++;
3453 }
3454 if (i >= n)
3455 goto fail;
3456 value = *value_ptr;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003457 }
3458 else {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003459 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3460 while (i < n && entry_ptr->me_value == NULL) {
3461 entry_ptr++;
3462 i++;
3463 }
3464 if (i >= n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003465 goto fail;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003466 value = entry_ptr->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003467 }
3468 di->di_pos = i+1;
3469 di->len--;
3470 Py_INCREF(value);
3471 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003472
3473fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003474 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003475 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003476 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003477}
3478
3479PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003480 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3481 "dict_valueiterator", /* tp_name */
3482 sizeof(dictiterobject), /* tp_basicsize */
3483 0, /* tp_itemsize */
3484 /* methods */
3485 (destructor)dictiter_dealloc, /* tp_dealloc */
3486 0, /* tp_print */
3487 0, /* tp_getattr */
3488 0, /* tp_setattr */
3489 0, /* tp_reserved */
3490 0, /* tp_repr */
3491 0, /* tp_as_number */
3492 0, /* tp_as_sequence */
3493 0, /* tp_as_mapping */
3494 0, /* tp_hash */
3495 0, /* tp_call */
3496 0, /* tp_str */
3497 PyObject_GenericGetAttr, /* tp_getattro */
3498 0, /* tp_setattro */
3499 0, /* tp_as_buffer */
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003500 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003501 0, /* tp_doc */
3502 (traverseproc)dictiter_traverse, /* tp_traverse */
3503 0, /* tp_clear */
3504 0, /* tp_richcompare */
3505 0, /* tp_weaklistoffset */
3506 PyObject_SelfIter, /* tp_iter */
3507 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3508 dictiter_methods, /* tp_methods */
3509 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003510};
3511
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003512static PyObject *
3513dictiter_iternextitem(dictiterobject *di)
Raymond Hettinger019a1482004-03-18 02:41:19 +00003514{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003515 PyObject *key, *value, *result = di->di_result;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003516 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003517 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003519 if (d == NULL)
3520 return NULL;
3521 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003523 if (di->di_used != d->ma_used) {
3524 PyErr_SetString(PyExc_RuntimeError,
3525 "dictionary changed size during iteration");
3526 di->di_used = -1; /* Make this state sticky */
3527 return NULL;
3528 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003530 i = di->di_pos;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003531 n = d->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003532 if (d->ma_values) {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003533 PyObject **value_ptr = &d->ma_values[i];
3534 while (i < n && *value_ptr == NULL) {
3535 value_ptr++;
3536 i++;
3537 }
3538 if (i >= n)
3539 goto fail;
3540 key = DK_ENTRIES(d->ma_keys)[i].me_key;
3541 value = *value_ptr;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003542 }
3543 else {
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003544 PyDictKeyEntry *entry_ptr = &DK_ENTRIES(d->ma_keys)[i];
3545 while (i < n && entry_ptr->me_value == NULL) {
3546 entry_ptr++;
3547 i++;
3548 }
3549 if (i >= n)
3550 goto fail;
3551 key = entry_ptr->me_key;
3552 value = entry_ptr->me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003553 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003554 di->di_pos = i+1;
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003555 di->len--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003556 if (result->ob_refcnt == 1) {
3557 Py_INCREF(result);
3558 Py_DECREF(PyTuple_GET_ITEM(result, 0));
3559 Py_DECREF(PyTuple_GET_ITEM(result, 1));
Serhiy Storchaka49f5cdd2016-10-09 23:08:05 +03003560 }
3561 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003562 result = PyTuple_New(2);
3563 if (result == NULL)
3564 return NULL;
3565 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003566 Py_INCREF(key);
3567 Py_INCREF(value);
Eric Snow96c6af92015-05-29 22:21:39 -06003568 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3569 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003570 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003571
3572fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003573 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003574 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003575 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003576}
3577
3578PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003579 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3580 "dict_itemiterator", /* tp_name */
3581 sizeof(dictiterobject), /* tp_basicsize */
3582 0, /* tp_itemsize */
3583 /* methods */
3584 (destructor)dictiter_dealloc, /* tp_dealloc */
3585 0, /* tp_print */
3586 0, /* tp_getattr */
3587 0, /* tp_setattr */
3588 0, /* tp_reserved */
3589 0, /* tp_repr */
3590 0, /* tp_as_number */
3591 0, /* tp_as_sequence */
3592 0, /* tp_as_mapping */
3593 0, /* tp_hash */
3594 0, /* tp_call */
3595 0, /* tp_str */
3596 PyObject_GenericGetAttr, /* tp_getattro */
3597 0, /* tp_setattro */
3598 0, /* tp_as_buffer */
3599 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3600 0, /* tp_doc */
3601 (traverseproc)dictiter_traverse, /* tp_traverse */
3602 0, /* tp_clear */
3603 0, /* tp_richcompare */
3604 0, /* tp_weaklistoffset */
3605 PyObject_SelfIter, /* tp_iter */
3606 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3607 dictiter_methods, /* tp_methods */
3608 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003609};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003610
3611
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003612static PyObject *
3613dictiter_reduce(dictiterobject *di)
3614{
3615 PyObject *list;
3616 dictiterobject tmp;
3617
3618 list = PyList_New(0);
3619 if (!list)
3620 return NULL;
3621
3622 /* copy the itertor state */
3623 tmp = *di;
3624 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003625
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003626 /* iterate the temporary into a list */
3627 for(;;) {
3628 PyObject *element = 0;
3629 if (Py_TYPE(di) == &PyDictIterItem_Type)
3630 element = dictiter_iternextitem(&tmp);
3631 else if (Py_TYPE(di) == &PyDictIterKey_Type)
3632 element = dictiter_iternextkey(&tmp);
3633 else if (Py_TYPE(di) == &PyDictIterValue_Type)
3634 element = dictiter_iternextvalue(&tmp);
3635 else
3636 assert(0);
3637 if (element) {
3638 if (PyList_Append(list, element)) {
3639 Py_DECREF(element);
3640 Py_DECREF(list);
3641 Py_XDECREF(tmp.di_dict);
3642 return NULL;
3643 }
3644 Py_DECREF(element);
3645 } else
3646 break;
3647 }
3648 Py_XDECREF(tmp.di_dict);
3649 /* check for error */
3650 if (tmp.di_dict != NULL) {
3651 /* we have an error */
3652 Py_DECREF(list);
3653 return NULL;
3654 }
Antoine Pitroua7013882012-04-05 00:04:20 +02003655 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003656}
3657
Guido van Rossum3ac67412007-02-10 18:55:06 +00003658/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003659/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003660/***********************************************/
3661
Guido van Rossumb90c8482007-02-10 01:11:45 +00003662/* The instance lay-out is the same for all three; but the type differs. */
3663
Guido van Rossumb90c8482007-02-10 01:11:45 +00003664static void
Eric Snow96c6af92015-05-29 22:21:39 -06003665dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003666{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003667 Py_XDECREF(dv->dv_dict);
3668 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003669}
3670
3671static int
Eric Snow96c6af92015-05-29 22:21:39 -06003672dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003673{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003674 Py_VISIT(dv->dv_dict);
3675 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003676}
3677
Guido van Rossum83825ac2007-02-10 04:54:19 +00003678static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003679dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003680{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003681 Py_ssize_t len = 0;
3682 if (dv->dv_dict != NULL)
3683 len = dv->dv_dict->ma_used;
3684 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003685}
3686
Eric Snow96c6af92015-05-29 22:21:39 -06003687PyObject *
3688_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003689{
Eric Snow96c6af92015-05-29 22:21:39 -06003690 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003691 if (dict == NULL) {
3692 PyErr_BadInternalCall();
3693 return NULL;
3694 }
3695 if (!PyDict_Check(dict)) {
3696 /* XXX Get rid of this restriction later */
3697 PyErr_Format(PyExc_TypeError,
3698 "%s() requires a dict argument, not '%s'",
3699 type->tp_name, dict->ob_type->tp_name);
3700 return NULL;
3701 }
Eric Snow96c6af92015-05-29 22:21:39 -06003702 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003703 if (dv == NULL)
3704 return NULL;
3705 Py_INCREF(dict);
3706 dv->dv_dict = (PyDictObject *)dict;
3707 _PyObject_GC_TRACK(dv);
3708 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003709}
3710
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003711/* TODO(guido): The views objects are not complete:
3712
3713 * support more set operations
3714 * support arbitrary mappings?
3715 - either these should be static or exported in dictobject.h
3716 - if public then they should probably be in builtins
3717*/
3718
Guido van Rossumaac530c2007-08-24 22:33:45 +00003719/* Return 1 if self is a subset of other, iterating over self;
3720 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003721static int
3722all_contained_in(PyObject *self, PyObject *other)
3723{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003724 PyObject *iter = PyObject_GetIter(self);
3725 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003727 if (iter == NULL)
3728 return -1;
3729 for (;;) {
3730 PyObject *next = PyIter_Next(iter);
3731 if (next == NULL) {
3732 if (PyErr_Occurred())
3733 ok = -1;
3734 break;
3735 }
3736 ok = PySequence_Contains(other, next);
3737 Py_DECREF(next);
3738 if (ok <= 0)
3739 break;
3740 }
3741 Py_DECREF(iter);
3742 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003743}
3744
3745static PyObject *
3746dictview_richcompare(PyObject *self, PyObject *other, int op)
3747{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003748 Py_ssize_t len_self, len_other;
3749 int ok;
3750 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003752 assert(self != NULL);
3753 assert(PyDictViewSet_Check(self));
3754 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003755
Brian Curtindfc80e32011-08-10 20:28:54 -05003756 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3757 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003759 len_self = PyObject_Size(self);
3760 if (len_self < 0)
3761 return NULL;
3762 len_other = PyObject_Size(other);
3763 if (len_other < 0)
3764 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003766 ok = 0;
3767 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003769 case Py_NE:
3770 case Py_EQ:
3771 if (len_self == len_other)
3772 ok = all_contained_in(self, other);
3773 if (op == Py_NE && ok >= 0)
3774 ok = !ok;
3775 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003777 case Py_LT:
3778 if (len_self < len_other)
3779 ok = all_contained_in(self, other);
3780 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003781
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003782 case Py_LE:
3783 if (len_self <= len_other)
3784 ok = all_contained_in(self, other);
3785 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003787 case Py_GT:
3788 if (len_self > len_other)
3789 ok = all_contained_in(other, self);
3790 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003792 case Py_GE:
3793 if (len_self >= len_other)
3794 ok = all_contained_in(other, self);
3795 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003797 }
3798 if (ok < 0)
3799 return NULL;
3800 result = ok ? Py_True : Py_False;
3801 Py_INCREF(result);
3802 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003803}
3804
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003805static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003806dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003807{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003808 PyObject *seq;
3809 PyObject *result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003810
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003811 seq = PySequence_List((PyObject *)dv);
3812 if (seq == NULL)
3813 return NULL;
3814
3815 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
3816 Py_DECREF(seq);
3817 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003818}
3819
Guido van Rossum3ac67412007-02-10 18:55:06 +00003820/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003821
3822static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003823dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003824{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003825 if (dv->dv_dict == NULL) {
3826 Py_RETURN_NONE;
3827 }
3828 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003829}
3830
3831static int
Eric Snow96c6af92015-05-29 22:21:39 -06003832dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00003833{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003834 if (dv->dv_dict == NULL)
3835 return 0;
3836 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003837}
3838
Guido van Rossum83825ac2007-02-10 04:54:19 +00003839static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003840 (lenfunc)dictview_len, /* sq_length */
3841 0, /* sq_concat */
3842 0, /* sq_repeat */
3843 0, /* sq_item */
3844 0, /* sq_slice */
3845 0, /* sq_ass_item */
3846 0, /* sq_ass_slice */
3847 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003848};
3849
Guido van Rossum523259b2007-08-24 23:41:22 +00003850static PyObject*
3851dictviews_sub(PyObject* self, PyObject *other)
3852{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003853 PyObject *result = PySet_New(self);
3854 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003855 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003857 if (result == NULL)
3858 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003859
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003860 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003861 if (tmp == NULL) {
3862 Py_DECREF(result);
3863 return NULL;
3864 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003866 Py_DECREF(tmp);
3867 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003868}
3869
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003870PyObject*
3871_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00003872{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003873 PyObject *result = PySet_New(self);
3874 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003875 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003876
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003877 if (result == NULL)
3878 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003879
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003880 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_intersection_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003881 if (tmp == NULL) {
3882 Py_DECREF(result);
3883 return NULL;
3884 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003886 Py_DECREF(tmp);
3887 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003888}
3889
3890static PyObject*
3891dictviews_or(PyObject* self, PyObject *other)
3892{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003893 PyObject *result = PySet_New(self);
3894 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003895 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003896
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003897 if (result == NULL)
3898 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003899
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003900 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003901 if (tmp == NULL) {
3902 Py_DECREF(result);
3903 return NULL;
3904 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003906 Py_DECREF(tmp);
3907 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003908}
3909
3910static PyObject*
3911dictviews_xor(PyObject* self, PyObject *other)
3912{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003913 PyObject *result = PySet_New(self);
3914 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003915 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003916
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003917 if (result == NULL)
3918 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003919
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003920 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_symmetric_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003921 if (tmp == NULL) {
3922 Py_DECREF(result);
3923 return NULL;
3924 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003926 Py_DECREF(tmp);
3927 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003928}
3929
3930static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003931 0, /*nb_add*/
3932 (binaryfunc)dictviews_sub, /*nb_subtract*/
3933 0, /*nb_multiply*/
3934 0, /*nb_remainder*/
3935 0, /*nb_divmod*/
3936 0, /*nb_power*/
3937 0, /*nb_negative*/
3938 0, /*nb_positive*/
3939 0, /*nb_absolute*/
3940 0, /*nb_bool*/
3941 0, /*nb_invert*/
3942 0, /*nb_lshift*/
3943 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003944 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003945 (binaryfunc)dictviews_xor, /*nb_xor*/
3946 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00003947};
3948
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003949static PyObject*
3950dictviews_isdisjoint(PyObject *self, PyObject *other)
3951{
3952 PyObject *it;
3953 PyObject *item = NULL;
3954
3955 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06003956 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003957 Py_RETURN_TRUE;
3958 else
3959 Py_RETURN_FALSE;
3960 }
3961
3962 /* Iterate over the shorter object (only if other is a set,
3963 * because PySequence_Contains may be expensive otherwise): */
3964 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06003965 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003966 Py_ssize_t len_other = PyObject_Size(other);
3967 if (len_other == -1)
3968 return NULL;
3969
3970 if ((len_other > len_self)) {
3971 PyObject *tmp = other;
3972 other = self;
3973 self = tmp;
3974 }
3975 }
3976
3977 it = PyObject_GetIter(other);
3978 if (it == NULL)
3979 return NULL;
3980
3981 while ((item = PyIter_Next(it)) != NULL) {
3982 int contains = PySequence_Contains(self, item);
3983 Py_DECREF(item);
3984 if (contains == -1) {
3985 Py_DECREF(it);
3986 return NULL;
3987 }
3988
3989 if (contains) {
3990 Py_DECREF(it);
3991 Py_RETURN_FALSE;
3992 }
3993 }
3994 Py_DECREF(it);
3995 if (PyErr_Occurred())
3996 return NULL; /* PyIter_Next raised an exception. */
3997 Py_RETURN_TRUE;
3998}
3999
4000PyDoc_STRVAR(isdisjoint_doc,
4001"Return True if the view and the given iterable have a null intersection.");
4002
Guido van Rossumb90c8482007-02-10 01:11:45 +00004003static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004004 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4005 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004006 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004007};
4008
4009PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004010 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4011 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004012 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004013 0, /* tp_itemsize */
4014 /* methods */
4015 (destructor)dictview_dealloc, /* tp_dealloc */
4016 0, /* tp_print */
4017 0, /* tp_getattr */
4018 0, /* tp_setattr */
4019 0, /* tp_reserved */
4020 (reprfunc)dictview_repr, /* tp_repr */
4021 &dictviews_as_number, /* tp_as_number */
4022 &dictkeys_as_sequence, /* tp_as_sequence */
4023 0, /* tp_as_mapping */
4024 0, /* tp_hash */
4025 0, /* tp_call */
4026 0, /* tp_str */
4027 PyObject_GenericGetAttr, /* tp_getattro */
4028 0, /* tp_setattro */
4029 0, /* tp_as_buffer */
4030 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4031 0, /* tp_doc */
4032 (traverseproc)dictview_traverse, /* tp_traverse */
4033 0, /* tp_clear */
4034 dictview_richcompare, /* tp_richcompare */
4035 0, /* tp_weaklistoffset */
4036 (getiterfunc)dictkeys_iter, /* tp_iter */
4037 0, /* tp_iternext */
4038 dictkeys_methods, /* tp_methods */
4039 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004040};
4041
4042static PyObject *
4043dictkeys_new(PyObject *dict)
4044{
Eric Snow96c6af92015-05-29 22:21:39 -06004045 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004046}
4047
Guido van Rossum3ac67412007-02-10 18:55:06 +00004048/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004049
4050static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004051dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004052{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004053 if (dv->dv_dict == NULL) {
4054 Py_RETURN_NONE;
4055 }
4056 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00004057}
4058
4059static int
Eric Snow96c6af92015-05-29 22:21:39 -06004060dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00004061{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004062 PyObject *key, *value, *found;
4063 if (dv->dv_dict == NULL)
4064 return 0;
4065 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
4066 return 0;
4067 key = PyTuple_GET_ITEM(obj, 0);
4068 value = PyTuple_GET_ITEM(obj, 1);
Raymond Hettinger6692f012016-09-18 21:46:08 -07004069 found = PyDict_GetItemWithError((PyObject *)dv->dv_dict, key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004070 if (found == NULL) {
4071 if (PyErr_Occurred())
4072 return -1;
4073 return 0;
4074 }
4075 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004076}
4077
Guido van Rossum83825ac2007-02-10 04:54:19 +00004078static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004079 (lenfunc)dictview_len, /* sq_length */
4080 0, /* sq_concat */
4081 0, /* sq_repeat */
4082 0, /* sq_item */
4083 0, /* sq_slice */
4084 0, /* sq_ass_item */
4085 0, /* sq_ass_slice */
4086 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004087};
4088
Guido van Rossumb90c8482007-02-10 01:11:45 +00004089static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00004090 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
4091 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004092 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004093};
4094
4095PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004096 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4097 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004098 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004099 0, /* tp_itemsize */
4100 /* methods */
4101 (destructor)dictview_dealloc, /* tp_dealloc */
4102 0, /* tp_print */
4103 0, /* tp_getattr */
4104 0, /* tp_setattr */
4105 0, /* tp_reserved */
4106 (reprfunc)dictview_repr, /* tp_repr */
4107 &dictviews_as_number, /* tp_as_number */
4108 &dictitems_as_sequence, /* tp_as_sequence */
4109 0, /* tp_as_mapping */
4110 0, /* tp_hash */
4111 0, /* tp_call */
4112 0, /* tp_str */
4113 PyObject_GenericGetAttr, /* tp_getattro */
4114 0, /* tp_setattro */
4115 0, /* tp_as_buffer */
4116 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4117 0, /* tp_doc */
4118 (traverseproc)dictview_traverse, /* tp_traverse */
4119 0, /* tp_clear */
4120 dictview_richcompare, /* tp_richcompare */
4121 0, /* tp_weaklistoffset */
4122 (getiterfunc)dictitems_iter, /* tp_iter */
4123 0, /* tp_iternext */
4124 dictitems_methods, /* tp_methods */
4125 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004126};
4127
4128static PyObject *
4129dictitems_new(PyObject *dict)
4130{
Eric Snow96c6af92015-05-29 22:21:39 -06004131 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004132}
4133
Guido van Rossum3ac67412007-02-10 18:55:06 +00004134/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00004135
4136static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06004137dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00004138{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004139 if (dv->dv_dict == NULL) {
4140 Py_RETURN_NONE;
4141 }
4142 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004143}
4144
Guido van Rossum83825ac2007-02-10 04:54:19 +00004145static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004146 (lenfunc)dictview_len, /* sq_length */
4147 0, /* sq_concat */
4148 0, /* sq_repeat */
4149 0, /* sq_item */
4150 0, /* sq_slice */
4151 0, /* sq_ass_item */
4152 0, /* sq_ass_slice */
4153 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00004154};
4155
Guido van Rossumb90c8482007-02-10 01:11:45 +00004156static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004157 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004158};
4159
4160PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004161 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4162 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004163 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004164 0, /* tp_itemsize */
4165 /* methods */
4166 (destructor)dictview_dealloc, /* tp_dealloc */
4167 0, /* tp_print */
4168 0, /* tp_getattr */
4169 0, /* tp_setattr */
4170 0, /* tp_reserved */
4171 (reprfunc)dictview_repr, /* tp_repr */
4172 0, /* tp_as_number */
4173 &dictvalues_as_sequence, /* tp_as_sequence */
4174 0, /* tp_as_mapping */
4175 0, /* tp_hash */
4176 0, /* tp_call */
4177 0, /* tp_str */
4178 PyObject_GenericGetAttr, /* tp_getattro */
4179 0, /* tp_setattro */
4180 0, /* tp_as_buffer */
4181 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4182 0, /* tp_doc */
4183 (traverseproc)dictview_traverse, /* tp_traverse */
4184 0, /* tp_clear */
4185 0, /* tp_richcompare */
4186 0, /* tp_weaklistoffset */
4187 (getiterfunc)dictvalues_iter, /* tp_iter */
4188 0, /* tp_iternext */
4189 dictvalues_methods, /* tp_methods */
4190 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004191};
4192
4193static PyObject *
4194dictvalues_new(PyObject *dict)
4195{
Eric Snow96c6af92015-05-29 22:21:39 -06004196 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004197}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004198
4199/* Returns NULL if cannot allocate a new PyDictKeysObject,
4200 but does not set an error */
4201PyDictKeysObject *
4202_PyDict_NewKeysForClass(void)
4203{
Victor Stinner742da042016-09-07 17:40:12 -07004204 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004205 if (keys == NULL)
4206 PyErr_Clear();
4207 else
4208 keys->dk_lookup = lookdict_split;
4209 return keys;
4210}
4211
4212#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
4213
4214PyObject *
4215PyObject_GenericGetDict(PyObject *obj, void *context)
4216{
4217 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
4218 if (dictptr == NULL) {
4219 PyErr_SetString(PyExc_AttributeError,
4220 "This object has no __dict__");
4221 return NULL;
4222 }
4223 dict = *dictptr;
4224 if (dict == NULL) {
4225 PyTypeObject *tp = Py_TYPE(obj);
4226 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
4227 DK_INCREF(CACHED_KEYS(tp));
4228 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
4229 }
4230 else {
4231 *dictptr = dict = PyDict_New();
4232 }
4233 }
4234 Py_XINCREF(dict);
4235 return dict;
4236}
4237
4238int
4239_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
Victor Stinner742da042016-09-07 17:40:12 -07004240 PyObject *key, PyObject *value)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004241{
4242 PyObject *dict;
4243 int res;
4244 PyDictKeysObject *cached;
4245
4246 assert(dictptr != NULL);
4247 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
4248 assert(dictptr != NULL);
4249 dict = *dictptr;
4250 if (dict == NULL) {
4251 DK_INCREF(cached);
4252 dict = new_dict_with_shared_keys(cached);
4253 if (dict == NULL)
4254 return -1;
4255 *dictptr = dict;
4256 }
4257 if (value == NULL) {
4258 res = PyDict_DelItem(dict, key);
4259 if (cached != ((PyDictObject *)dict)->ma_keys) {
4260 CACHED_KEYS(tp) = NULL;
4261 DK_DECREF(cached);
4262 }
4263 } else {
4264 res = PyDict_SetItem(dict, key, value);
4265 if (cached != ((PyDictObject *)dict)->ma_keys) {
4266 /* Either update tp->ht_cached_keys or delete it */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004267 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004268 CACHED_KEYS(tp) = make_keys_shared(dict);
Victor Stinner742da042016-09-07 17:40:12 -07004269 }
4270 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004271 CACHED_KEYS(tp) = NULL;
4272 }
4273 DK_DECREF(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004274 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
4275 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004276 }
4277 }
4278 } else {
4279 dict = *dictptr;
4280 if (dict == NULL) {
4281 dict = PyDict_New();
4282 if (dict == NULL)
4283 return -1;
4284 *dictptr = dict;
4285 }
4286 if (value == NULL) {
4287 res = PyDict_DelItem(dict, key);
4288 } else {
4289 res = PyDict_SetItem(dict, key, value);
4290 }
4291 }
4292 return res;
4293}
4294
4295void
4296_PyDictKeys_DecRef(PyDictKeysObject *keys)
4297{
4298 DK_DECREF(keys);
4299}