blob: ff285bda892f1eb7b76b0871aa0e0c9278ba42a9 [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
13As of Python 3.6, this is compact and orderd. 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"
Christian Heimes0ded5b52007-12-10 15:50:56 +0000114#include "stringlib/eq.h"
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
187 j = (5*j) + 1 + perturb;
188 perturb >>= PERTURB_SHIFT;
189 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
Victor Stinner742da042016-09-07 17:40:12 -0700240/* Dictionary reuse scheme to save calls to malloc and free */
Christian Heimes2202f872008-02-06 14:31:34 +0000241#ifndef PyDict_MAXFREELIST
242#define PyDict_MAXFREELIST 80
243#endif
244static PyDictObject *free_list[PyDict_MAXFREELIST];
245static int numfree = 0;
Victor Stinner742da042016-09-07 17:40:12 -0700246static PyDictKeysObject *keys_free_list[PyDict_MAXFREELIST];
247static int numfreekeys = 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000248
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300249#include "clinic/dictobject.c.h"
250
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100251int
252PyDict_ClearFreeList(void)
Christian Heimes77c02eb2008-02-09 02:18:51 +0000253{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 PyDictObject *op;
Victor Stinner742da042016-09-07 17:40:12 -0700255 int ret = numfree + numfreekeys;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 while (numfree) {
257 op = free_list[--numfree];
258 assert(PyDict_CheckExact(op));
259 PyObject_GC_Del(op);
260 }
Victor Stinner742da042016-09-07 17:40:12 -0700261 while (numfreekeys) {
262 PyObject_FREE(keys_free_list[--numfreekeys]);
263 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100264 return ret;
265}
266
David Malcolm49526f42012-06-22 14:55:41 -0400267/* Print summary info about the state of the optimized allocator */
268void
269_PyDict_DebugMallocStats(FILE *out)
270{
271 _PyDebugAllocatorStats(out,
272 "free PyDictObject", numfree, sizeof(PyDictObject));
273}
274
275
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100276void
277PyDict_Fini(void)
278{
279 PyDict_ClearFreeList();
Christian Heimes77c02eb2008-02-09 02:18:51 +0000280}
281
Victor Stinner742da042016-09-07 17:40:12 -0700282#define DK_SIZE(dk) ((dk)->dk_size)
283#if SIZEOF_VOID_P > 4
284#define DK_IXSIZE(dk) (DK_SIZE(dk) <= 0xff ? 1 : DK_SIZE(dk) <= 0xffff ? 2 : \
285 DK_SIZE(dk) <= 0xffffffff ? 4 : sizeof(Py_ssize_t))
286#else
287#define DK_IXSIZE(dk) (DK_SIZE(dk) <= 0xff ? 1 : DK_SIZE(dk) <= 0xffff ? 2 : \
288 sizeof(Py_ssize_t))
289#endif
290#define DK_ENTRIES(dk) ((PyDictKeyEntry*)(&(dk)->dk_indices[DK_SIZE(dk) * \
291 DK_IXSIZE(dk)]))
292
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200293#define DK_DEBUG_INCREF _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA
294#define DK_DEBUG_DECREF _Py_DEC_REFTOTAL _Py_REF_DEBUG_COMMA
295
296#define DK_INCREF(dk) (DK_DEBUG_INCREF ++(dk)->dk_refcnt)
297#define DK_DECREF(dk) if (DK_DEBUG_DECREF (--(dk)->dk_refcnt) == 0) free_keys_object(dk)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400298#define DK_MASK(dk) (((dk)->dk_size)-1)
299#define IS_POWER_OF_2(x) (((x) & (x-1)) == 0)
300
Victor Stinner742da042016-09-07 17:40:12 -0700301
302/* lookup indices. returns DKIX_EMPTY, DKIX_DUMMY, or ix >=0 */
Benjamin Peterson73222252016-09-08 09:58:47 -0700303static inline Py_ssize_t
Victor Stinner742da042016-09-07 17:40:12 -0700304dk_get_index(PyDictKeysObject *keys, Py_ssize_t i)
305{
306 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700307 Py_ssize_t ix;
308
Victor Stinner742da042016-09-07 17:40:12 -0700309 if (s <= 0xff) {
Victor Stinner208857e2016-09-08 11:35:46 -0700310 char *indices = (char*)keys->dk_indices;
311 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700312 }
313 else if (s <= 0xffff) {
Victor Stinner208857e2016-09-08 11:35:46 -0700314 int16_t *indices = (int16_t*)keys->dk_indices;
315 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700316 }
317#if SIZEOF_VOID_P > 4
318 else if (s <= 0xffffffff) {
Victor Stinner208857e2016-09-08 11:35:46 -0700319 int32_t *indices = (int32_t*)keys->dk_indices;
320 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700321 }
322#endif
323 else {
Victor Stinner208857e2016-09-08 11:35:46 -0700324 Py_ssize_t *indices = (Py_ssize_t*)keys->dk_indices;
325 ix = indices[i];
Victor Stinner742da042016-09-07 17:40:12 -0700326 }
Victor Stinner71211e32016-09-08 10:52:46 -0700327 assert(ix >= DKIX_DUMMY);
328 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700329}
330
331/* write to indices. */
Benjamin Peterson73222252016-09-08 09:58:47 -0700332static inline void
Victor Stinner742da042016-09-07 17:40:12 -0700333dk_set_index(PyDictKeysObject *keys, Py_ssize_t i, Py_ssize_t ix)
334{
335 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700336
337 assert(ix >= DKIX_DUMMY);
338
Victor Stinner742da042016-09-07 17:40:12 -0700339 if (s <= 0xff) {
Victor Stinner208857e2016-09-08 11:35:46 -0700340 char *indices = (char*)keys->dk_indices;
Victor Stinner71211e32016-09-08 10:52:46 -0700341 assert(ix <= 0x7f);
Victor Stinner208857e2016-09-08 11:35:46 -0700342 indices[i] = (char)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700343 }
344 else if (s <= 0xffff) {
Victor Stinner208857e2016-09-08 11:35:46 -0700345 int16_t *indices = (int16_t*)keys->dk_indices;
Victor Stinner71211e32016-09-08 10:52:46 -0700346 assert(ix <= 0x7fff);
Victor Stinner208857e2016-09-08 11:35:46 -0700347 indices[i] = (int16_t)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700348 }
349#if SIZEOF_VOID_P > 4
350 else if (s <= 0xffffffff) {
Victor Stinner208857e2016-09-08 11:35:46 -0700351 int32_t *indices = (int32_t*)keys->dk_indices;
Victor Stinner71211e32016-09-08 10:52:46 -0700352 assert(ix <= 0x7fffffff);
Victor Stinner208857e2016-09-08 11:35:46 -0700353 indices[i] = (int32_t)ix;
Victor Stinner742da042016-09-07 17:40:12 -0700354 }
355#endif
356 else {
Victor Stinner208857e2016-09-08 11:35:46 -0700357 Py_ssize_t *indices = (Py_ssize_t*)keys->dk_indices;
358 indices[i] = ix;
Victor Stinner742da042016-09-07 17:40:12 -0700359 }
360}
361
362
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200363/* USABLE_FRACTION is the maximum dictionary load.
Victor Stinner742da042016-09-07 17:40:12 -0700364 * Increasing this ratio makes dictionaries more dense resulting in more
365 * collisions. Decreasing it improves sparseness at the expense of spreading
366 * indices over more cache lines and at the cost of total memory consumed.
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200367 *
368 * USABLE_FRACTION must obey the following:
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400369 * (0 < USABLE_FRACTION(n) < n) for all n >= 2
370 *
Victor Stinner742da042016-09-07 17:40:12 -0700371 * USABLE_FRACTION should be quick to calculate.
372 * Fractions around 1/2 to 2/3 seem to work well in practice.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400373 */
Victor Stinner742da042016-09-07 17:40:12 -0700374#define USABLE_FRACTION(n) (((n) << 1)/3)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400375
Victor Stinner742da042016-09-07 17:40:12 -0700376/* ESTIMATE_SIZE is reverse function of USABLE_FRACTION.
377 * This can be used to reserve enough size to insert n entries without
378 * resizing.
379 */
380#define ESTIMATE_SIZE(n) (((n)*3) >> 1)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400381
Victor Stinner742da042016-09-07 17:40:12 -0700382/* Alternative fraction that is otherwise close enough to 2n/3 to make
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400383 * little difference. 8 * 2/3 == 8 * 5/8 == 5. 16 * 2/3 == 16 * 5/8 == 10.
384 * 32 * 2/3 = 21, 32 * 5/8 = 20.
385 * Its advantage is that it is faster to compute on machines with slow division.
386 * #define USABLE_FRACTION(n) (((n) >> 1) + ((n) >> 2) - ((n) >> 3))
Victor Stinner742da042016-09-07 17:40:12 -0700387 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400388
Victor Stinnera9f61a52013-07-16 22:17:26 +0200389/* GROWTH_RATE. Growth rate upon hitting maximum load.
390 * Currently set to used*2 + capacity/2.
391 * This means that dicts double in size when growing without deletions,
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700392 * but have more head room when the number of deletions is on a par with the
393 * number of insertions.
394 * Raising this to used*4 doubles memory consumption depending on the size of
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200395 * the dictionary, but results in half the number of resizes, less effort to
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700396 * resize.
397 * GROWTH_RATE was set to used*4 up to version 3.2.
398 * GROWTH_RATE was set to used*2 in version 3.3.0
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200399 */
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700400#define GROWTH_RATE(d) (((d)->ma_used*2)+((d)->ma_keys->dk_size>>1))
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400401
402#define ENSURE_ALLOWS_DELETIONS(d) \
403 if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \
404 (d)->ma_keys->dk_lookup = lookdict_unicode; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400406
407/* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
408 * (which cannot fail and thus can do no allocation).
409 */
410static PyDictKeysObject empty_keys_struct = {
411 2, /* dk_refcnt 1 for this struct, 1 for dummy_struct */
412 1, /* dk_size */
413 lookdict_split, /* dk_lookup */
414 0, /* dk_usable (immutable) */
Victor Stinner742da042016-09-07 17:40:12 -0700415 0, /* dk_nentries */
416 {DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY,
417 DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY}, /* dk_indices */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400418};
419
420static PyObject *empty_values[1] = { NULL };
421
422#define Py_EMPTY_KEYS &empty_keys_struct
423
424static PyDictKeysObject *new_keys_object(Py_ssize_t size)
425{
426 PyDictKeysObject *dk;
Victor Stinner742da042016-09-07 17:40:12 -0700427 Py_ssize_t es, usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400428
Victor Stinner742da042016-09-07 17:40:12 -0700429 assert(size >= PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400430 assert(IS_POWER_OF_2(size));
Victor Stinner742da042016-09-07 17:40:12 -0700431
432 usable = USABLE_FRACTION(size);
433 if (size <= 0xff) {
434 es = 1;
435 }
436 else if (size <= 0xffff) {
437 es = 2;
438 }
439#if SIZEOF_VOID_P > 4
440 else if (size <= 0xffffffff) {
441 es = 4;
442 }
443#endif
444 else {
445 es = sizeof(Py_ssize_t);
446 }
447
448 if (size == PyDict_MINSIZE && numfreekeys > 0) {
449 dk = keys_free_list[--numfreekeys];
450 }
451 else {
Victor Stinner98ee9d52016-09-08 09:33:56 -0700452 dk = PyObject_MALLOC(sizeof(PyDictKeysObject)
453 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
454 + es * size
455 + sizeof(PyDictKeyEntry) * usable);
Victor Stinner742da042016-09-07 17:40:12 -0700456 if (dk == NULL) {
457 PyErr_NoMemory();
458 return NULL;
459 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400460 }
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200461 DK_DEBUG_INCREF dk->dk_refcnt = 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400462 dk->dk_size = size;
Victor Stinner742da042016-09-07 17:40:12 -0700463 dk->dk_usable = usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400464 dk->dk_lookup = lookdict_unicode_nodummy;
Victor Stinner742da042016-09-07 17:40:12 -0700465 dk->dk_nentries = 0;
466 memset(&dk->dk_indices[0], 0xff, es * size);
467 memset(DK_ENTRIES(dk), 0, sizeof(PyDictKeyEntry) * usable);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400468 return dk;
469}
470
471static void
472free_keys_object(PyDictKeysObject *keys)
473{
Victor Stinner742da042016-09-07 17:40:12 -0700474 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400475 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -0700476 for (i = 0, n = keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400477 Py_XDECREF(entries[i].me_key);
478 Py_XDECREF(entries[i].me_value);
479 }
Victor Stinner742da042016-09-07 17:40:12 -0700480 if (keys->dk_size == PyDict_MINSIZE && numfreekeys < PyDict_MAXFREELIST) {
481 keys_free_list[numfreekeys++] = keys;
482 return;
483 }
Raymond Hettingerce5179f2016-01-31 08:56:21 -0800484 PyObject_FREE(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400485}
486
487#define new_values(size) PyMem_NEW(PyObject *, size)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400488#define free_values(values) PyMem_FREE(values)
489
490/* Consumes a reference to the keys object */
491static PyObject *
492new_dict(PyDictKeysObject *keys, PyObject **values)
493{
494 PyDictObject *mp;
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200495 assert(keys != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 if (numfree) {
497 mp = free_list[--numfree];
498 assert (mp != NULL);
499 assert (Py_TYPE(mp) == &PyDict_Type);
500 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400502 else {
503 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
504 if (mp == NULL) {
505 DK_DECREF(keys);
506 free_values(values);
507 return NULL;
508 }
509 }
510 mp->ma_keys = keys;
511 mp->ma_values = values;
512 mp->ma_used = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000514}
515
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400516/* Consumes a reference to the keys object */
517static PyObject *
518new_dict_with_shared_keys(PyDictKeysObject *keys)
519{
520 PyObject **values;
521 Py_ssize_t i, size;
522
Victor Stinner742da042016-09-07 17:40:12 -0700523 size = USABLE_FRACTION(DK_SIZE(keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400524 values = new_values(size);
525 if (values == NULL) {
526 DK_DECREF(keys);
527 return PyErr_NoMemory();
528 }
529 for (i = 0; i < size; i++) {
530 values[i] = NULL;
531 }
532 return new_dict(keys, values);
533}
534
535PyObject *
536PyDict_New(void)
537{
Victor Stinner742da042016-09-07 17:40:12 -0700538 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200539 if (keys == NULL)
540 return NULL;
541 return new_dict(keys, NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400542}
543
Victor Stinner742da042016-09-07 17:40:12 -0700544/* Search index of hash table from offset of entry table */
545static Py_ssize_t
546lookdict_index(PyDictKeysObject *k, Py_hash_t hash, Py_ssize_t index)
547{
548 size_t i, perturb;
549 size_t mask = DK_MASK(k);
550 Py_ssize_t ix;
551
552 i = (size_t)hash & mask;
553 ix = dk_get_index(k, i);
554 if (ix == index) {
555 return i;
556 }
557 if (ix == DKIX_EMPTY) {
558 return DKIX_EMPTY;
559 }
560
561 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
562 i = mask & ((i << 2) + i + perturb + 1);
563 ix = dk_get_index(k, i);
564 if (ix == index) {
565 return i;
566 }
567 if (ix == DKIX_EMPTY) {
568 return DKIX_EMPTY;
569 }
570 }
571 assert(0); /* NOT REACHED */
572 return DKIX_ERROR;
573}
574
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000575/*
576The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000577This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000578Open addressing is preferred over chaining since the link overhead for
579chaining would be substantial (100% with typical malloc overhead).
580
Tim Peterseb28ef22001-06-02 05:27:19 +0000581The initial probe index is computed as hash mod the table size. Subsequent
582probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000583
584All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000585
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000586The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000587contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000588Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000589
Victor Stinner742da042016-09-07 17:40:12 -0700590lookdict() is general-purpose, and may return DKIX_ERROR if (and only if) a
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000591comparison raises an exception (this was new in Python 2.5).
Guido van Rossum89d8c602007-09-18 17:26:56 +0000592lookdict_unicode() below is specialized to string keys, comparison of which can
Victor Stinner742da042016-09-07 17:40:12 -0700593never raise an exception; that function can never return DKIX_ERROR.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400594lookdict_unicode_nodummy is further specialized for string keys that cannot be
595the <dummy> value.
Victor Stinner742da042016-09-07 17:40:12 -0700596For both, when the key isn't found a DKIX_EMPTY is returned. hashpos returns
597where the key index should be inserted.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000598*/
Victor Stinner742da042016-09-07 17:40:12 -0700599static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400600lookdict(PyDictObject *mp, PyObject *key,
Victor Stinner742da042016-09-07 17:40:12 -0700601 Py_hash_t hash, PyObject ***value_addr, Py_ssize_t *hashpos)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000602{
Victor Stinner742da042016-09-07 17:40:12 -0700603 size_t i, perturb, mask;
604 Py_ssize_t ix, freeslot;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200605 int cmp;
Victor Stinner742da042016-09-07 17:40:12 -0700606 PyDictKeysObject *dk;
607 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000609
Antoine Pitrou9a234902012-05-13 20:48:01 +0200610top:
Victor Stinner742da042016-09-07 17:40:12 -0700611 dk = mp->ma_keys;
612 mask = DK_MASK(dk);
613 ep0 = DK_ENTRIES(dk);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700615
616 ix = dk_get_index(dk, i);
617 if (ix == DKIX_EMPTY) {
618 if (hashpos != NULL)
619 *hashpos = i;
620 *value_addr = NULL;
621 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400622 }
Victor Stinner742da042016-09-07 17:40:12 -0700623 if (ix == DKIX_DUMMY) {
624 freeslot = i;
625 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 else {
Victor Stinner742da042016-09-07 17:40:12 -0700627 ep = &ep0[ix];
628 if (ep->me_key == key) {
629 *value_addr = &ep->me_value;
630 if (hashpos != NULL)
631 *hashpos = i;
632 return ix;
633 }
634 if (ep->me_key != NULL && ep->me_hash == hash) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 startkey = ep->me_key;
636 Py_INCREF(startkey);
637 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
638 Py_DECREF(startkey);
639 if (cmp < 0)
Victor Stinner742da042016-09-07 17:40:12 -0700640 return DKIX_ERROR;
641 if (dk == mp->ma_keys && ep->me_key == startkey) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400642 if (cmp > 0) {
643 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700644 if (hashpos != NULL)
645 *hashpos = i;
646 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400647 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 }
649 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200650 /* The dict was mutated, restart */
651 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 }
653 }
Victor Stinner742da042016-09-07 17:40:12 -0700654 freeslot = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 }
Tim Peters15d49292001-05-27 07:39:22 +0000656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
Victor Stinner742da042016-09-07 17:40:12 -0700658 i = ((i << 2) + i + perturb + 1) & mask;
659 ix = dk_get_index(dk, i);
660 if (ix == DKIX_EMPTY) {
661 if (hashpos != NULL) {
662 *hashpos = (freeslot == -1) ? (Py_ssize_t)i : freeslot;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400663 }
Victor Stinner742da042016-09-07 17:40:12 -0700664 *value_addr = NULL;
665 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400666 }
Victor Stinner742da042016-09-07 17:40:12 -0700667 if (ix == DKIX_DUMMY) {
668 if (freeslot == -1)
669 freeslot = i;
670 continue;
671 }
672 ep = &ep0[ix];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400673 if (ep->me_key == key) {
Victor Stinner742da042016-09-07 17:40:12 -0700674 if (hashpos != NULL) {
675 *hashpos = i;
676 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400677 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700678 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400679 }
Victor Stinner742da042016-09-07 17:40:12 -0700680 if (ep->me_hash == hash && ep->me_key != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 startkey = ep->me_key;
682 Py_INCREF(startkey);
683 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
684 Py_DECREF(startkey);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400685 if (cmp < 0) {
686 *value_addr = NULL;
Victor Stinner742da042016-09-07 17:40:12 -0700687 return DKIX_ERROR;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400688 }
Victor Stinner742da042016-09-07 17:40:12 -0700689 if (dk == mp->ma_keys && ep->me_key == startkey) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400690 if (cmp > 0) {
Victor Stinner742da042016-09-07 17:40:12 -0700691 if (hashpos != NULL) {
692 *hashpos = i;
693 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400694 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700695 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400696 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 }
698 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200699 /* The dict was mutated, restart */
700 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 }
702 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 }
704 assert(0); /* NOT REACHED */
705 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000706}
707
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400708/* Specialized version for string-only keys */
Victor Stinner742da042016-09-07 17:40:12 -0700709static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400710lookdict_unicode(PyDictObject *mp, PyObject *key,
Victor Stinner742da042016-09-07 17:40:12 -0700711 Py_hash_t hash, PyObject ***value_addr, Py_ssize_t *hashpos)
Fred Drake1bff34a2000-08-31 19:31:38 +0000712{
Victor Stinner742da042016-09-07 17:40:12 -0700713 size_t i, perturb;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200714 size_t mask = DK_MASK(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -0700715 Py_ssize_t ix, freeslot;
716 PyDictKeyEntry *ep, *ep0 = DK_ENTRIES(mp->ma_keys);
Fred Drake1bff34a2000-08-31 19:31:38 +0000717
Victor Stinner742da042016-09-07 17:40:12 -0700718 assert(mp->ma_values == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 /* Make sure this function doesn't have to handle non-unicode keys,
720 including subclasses of str; e.g., one reason to subclass
721 unicodes is to override __eq__, and for speed we don't cater to
722 that here. */
723 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400724 mp->ma_keys->dk_lookup = lookdict;
Victor Stinner742da042016-09-07 17:40:12 -0700725 return lookdict(mp, key, hash, value_addr, hashpos);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 }
Mark Dickinson57e683e2011-09-24 18:18:40 +0100727 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700728 ix = dk_get_index(mp->ma_keys, i);
729 if (ix == DKIX_EMPTY) {
730 if (hashpos != NULL)
731 *hashpos = i;
732 *value_addr = NULL;
733 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400734 }
Victor Stinner742da042016-09-07 17:40:12 -0700735 if (ix == DKIX_DUMMY) {
736 freeslot = i;
737 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 else {
Victor Stinner742da042016-09-07 17:40:12 -0700739 ep = &ep0[ix];
740 /* only split table can be ix != DKIX_DUMMY && me_key == NULL */
741 assert(ep->me_key != NULL);
742 if (ep->me_key == key || (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
743 if (hashpos != NULL)
744 *hashpos = i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400745 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700746 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400747 }
Victor Stinner742da042016-09-07 17:40:12 -0700748 freeslot = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 }
Tim Peters15d49292001-05-27 07:39:22 +0000750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
Victor Stinner742da042016-09-07 17:40:12 -0700752 i = mask & ((i << 2) + i + perturb + 1);
753 ix = dk_get_index(mp->ma_keys, i);
754 if (ix == DKIX_EMPTY) {
755 if (hashpos != NULL) {
756 *hashpos = (freeslot == -1) ? (Py_ssize_t)i : freeslot;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400757 }
Victor Stinner742da042016-09-07 17:40:12 -0700758 *value_addr = NULL;
759 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400760 }
Victor Stinner742da042016-09-07 17:40:12 -0700761 if (ix == DKIX_DUMMY) {
762 if (freeslot == -1)
763 freeslot = i;
764 continue;
765 }
766 ep = &ep0[ix];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 if (ep->me_key == key
768 || (ep->me_hash == hash
Victor Stinner742da042016-09-07 17:40:12 -0700769 && ep->me_key != NULL
770 && unicode_eq(ep->me_key, key))) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400771 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700772 if (hashpos != NULL) {
773 *hashpos = i;
774 }
775 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400776 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 }
778 assert(0); /* NOT REACHED */
779 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000780}
781
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400782/* Faster version of lookdict_unicode when it is known that no <dummy> keys
783 * will be present. */
Victor Stinner742da042016-09-07 17:40:12 -0700784static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400785lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
Victor Stinner742da042016-09-07 17:40:12 -0700786 Py_hash_t hash, PyObject ***value_addr,
787 Py_ssize_t *hashpos)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400788{
Victor Stinner742da042016-09-07 17:40:12 -0700789 size_t i, perturb;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200790 size_t mask = DK_MASK(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -0700791 Py_ssize_t ix;
792 PyDictKeyEntry *ep, *ep0 = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400793
Victor Stinner742da042016-09-07 17:40:12 -0700794 assert(mp->ma_values == NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400795 /* Make sure this function doesn't have to handle non-unicode keys,
796 including subclasses of str; e.g., one reason to subclass
797 unicodes is to override __eq__, and for speed we don't cater to
798 that here. */
799 if (!PyUnicode_CheckExact(key)) {
800 mp->ma_keys->dk_lookup = lookdict;
Victor Stinner742da042016-09-07 17:40:12 -0700801 return lookdict(mp, key, hash, value_addr, hashpos);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400802 }
803 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700804 ix = dk_get_index(mp->ma_keys, i);
805 assert (ix != DKIX_DUMMY);
806 if (ix == DKIX_EMPTY) {
807 if (hashpos != NULL)
808 *hashpos = i;
809 *value_addr = NULL;
810 return DKIX_EMPTY;
811 }
812 ep = &ep0[ix];
Victor Stinnerdee6e252016-09-08 11:16:07 -0700813 assert(ep->me_key != NULL);
814 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700815 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400816 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700817 if (hashpos != NULL)
818 *hashpos = i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400819 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700820 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400821 }
822 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
Victor Stinner742da042016-09-07 17:40:12 -0700823 i = mask & ((i << 2) + i + perturb + 1);
824 ix = dk_get_index(mp->ma_keys, i);
825 assert (ix != DKIX_DUMMY);
826 if (ix == DKIX_EMPTY) {
827 if (hashpos != NULL)
828 *hashpos = i;
829 *value_addr = NULL;
830 return DKIX_EMPTY;
831 }
832 ep = &ep0[ix];
833 assert(ep->me_key != NULL && PyUnicode_CheckExact(ep->me_key));
834 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400835 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700836 if (hashpos != NULL)
837 *hashpos = i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400838 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700839 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400840 }
841 }
842 assert(0); /* NOT REACHED */
843 return 0;
844}
845
846/* Version of lookdict for split tables.
847 * All split tables and only split tables use this lookup function.
848 * Split tables only contain unicode keys and no dummy keys,
849 * so algorithm is the same as lookdict_unicode_nodummy.
850 */
Victor Stinner742da042016-09-07 17:40:12 -0700851static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400852lookdict_split(PyDictObject *mp, PyObject *key,
Victor Stinner742da042016-09-07 17:40:12 -0700853 Py_hash_t hash, PyObject ***value_addr, Py_ssize_t *hashpos)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400854{
Victor Stinner742da042016-09-07 17:40:12 -0700855 size_t i, perturb;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200856 size_t mask = DK_MASK(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -0700857 Py_ssize_t ix;
858 PyDictKeyEntry *ep, *ep0 = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400859
Victor Stinner742da042016-09-07 17:40:12 -0700860 /* mp must split table */
861 assert(mp->ma_values != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400862 if (!PyUnicode_CheckExact(key)) {
Victor Stinner742da042016-09-07 17:40:12 -0700863 ix = lookdict(mp, key, hash, value_addr, hashpos);
864 if (ix >= 0) {
865 *value_addr = &mp->ma_values[ix];
866 }
867 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400868 }
Victor Stinner742da042016-09-07 17:40:12 -0700869
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400870 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700871 ix = dk_get_index(mp->ma_keys, i);
872 if (ix == DKIX_EMPTY) {
873 if (hashpos != NULL)
874 *hashpos = i;
875 *value_addr = NULL;
876 return DKIX_EMPTY;
877 }
878 assert(ix >= 0);
879 ep = &ep0[ix];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400880 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700881 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400882 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700883 if (hashpos != NULL)
884 *hashpos = i;
885 *value_addr = &mp->ma_values[ix];
886 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400887 }
888 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
Victor Stinner742da042016-09-07 17:40:12 -0700889 i = mask & ((i << 2) + i + perturb + 1);
890 ix = dk_get_index(mp->ma_keys, i);
891 if (ix == DKIX_EMPTY) {
892 if (hashpos != NULL)
893 *hashpos = i;
894 *value_addr = NULL;
895 return DKIX_EMPTY;
896 }
897 assert(ix >= 0);
898 ep = &ep0[ix];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400899 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700900 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400901 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700902 if (hashpos != NULL)
903 *hashpos = i;
904 *value_addr = &mp->ma_values[ix];
905 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400906 }
907 }
908 assert(0); /* NOT REACHED */
909 return 0;
910}
911
Benjamin Petersonfb886362010-04-24 18:21:17 +0000912int
913_PyDict_HasOnlyStringKeys(PyObject *dict)
914{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 Py_ssize_t pos = 0;
916 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000917 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400919 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 return 1;
921 while (PyDict_Next(dict, &pos, &key, &value))
922 if (!PyUnicode_Check(key))
923 return 0;
924 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000925}
926
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000927#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 do { \
929 if (!_PyObject_GC_IS_TRACKED(mp)) { \
930 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
931 _PyObject_GC_MAY_BE_TRACKED(value)) { \
932 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 } \
934 } \
935 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000936
937void
938_PyDict_MaybeUntrack(PyObject *op)
939{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 PyDictObject *mp;
941 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -0700942 Py_ssize_t i, numentries;
943 PyDictKeyEntry *ep0;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000944
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
946 return;
947
948 mp = (PyDictObject *) op;
Victor Stinner742da042016-09-07 17:40:12 -0700949 ep0 = DK_ENTRIES(mp->ma_keys);
950 numentries = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400951 if (_PyDict_HasSplitTable(mp)) {
Victor Stinner742da042016-09-07 17:40:12 -0700952 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400953 if ((value = mp->ma_values[i]) == NULL)
954 continue;
955 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
Victor Stinner742da042016-09-07 17:40:12 -0700956 assert(!_PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400957 return;
958 }
959 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400961 else {
Victor Stinner742da042016-09-07 17:40:12 -0700962 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400963 if ((value = ep0[i].me_value) == NULL)
964 continue;
965 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
966 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
967 return;
968 }
969 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000971}
972
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400973/* Internal function to find slot for an item from its hash
974 * when it is known that the key is not present in the dict.
975 */
Victor Stinner742da042016-09-07 17:40:12 -0700976static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400977find_empty_slot(PyDictObject *mp, PyObject *key, Py_hash_t hash,
Victor Stinner742da042016-09-07 17:40:12 -0700978 PyObject ***value_addr, Py_ssize_t *hashpos)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000979{
Victor Stinner742da042016-09-07 17:40:12 -0700980 size_t i, perturb;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400981 size_t mask = DK_MASK(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -0700982 Py_ssize_t ix;
983 PyDictKeyEntry *ep, *ep0 = DK_ENTRIES(mp->ma_keys);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000984
Victor Stinner742da042016-09-07 17:40:12 -0700985 assert(hashpos != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400986 assert(key != NULL);
987 if (!PyUnicode_CheckExact(key))
988 mp->ma_keys->dk_lookup = lookdict;
989 i = hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700990 ix = dk_get_index(mp->ma_keys, i);
991 for (perturb = hash; ix != DKIX_EMPTY; perturb >>= PERTURB_SHIFT) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400992 i = (i << 2) + i + perturb + 1;
Victor Stinner742da042016-09-07 17:40:12 -0700993 ix = dk_get_index(mp->ma_keys, i & mask);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 }
Victor Stinner742da042016-09-07 17:40:12 -0700995 ep = &ep0[mp->ma_keys->dk_nentries];
996 *hashpos = i & mask;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400997 assert(ep->me_value == NULL);
998 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -0700999 *value_addr = &mp->ma_values[ix];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001000 else
1001 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -07001002 return ix;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001003}
1004
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001005static int
1006insertion_resize(PyDictObject *mp)
1007{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -07001008 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001009}
Antoine Pitroue965d972012-02-27 00:45:12 +01001010
1011/*
1012Internal routine to insert a new item into the table.
1013Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +01001014Returns -1 if an error occurred, or 0 on success.
1015*/
1016static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001017insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001018{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001019 PyObject *old_value;
1020 PyObject **value_addr;
Victor Stinner742da042016-09-07 17:40:12 -07001021 PyDictKeyEntry *ep, *ep0;
1022 Py_ssize_t hashpos, ix;
Antoine Pitroue965d972012-02-27 00:45:12 +01001023
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001024 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
1025 if (insertion_resize(mp) < 0)
1026 return -1;
1027 }
1028
Victor Stinner742da042016-09-07 17:40:12 -07001029 ix = mp->ma_keys->dk_lookup(mp, key, hash, &value_addr, &hashpos);
1030 if (ix == DKIX_ERROR) {
Antoine Pitroue965d972012-02-27 00:45:12 +01001031 return -1;
1032 }
Victor Stinner742da042016-09-07 17:40:12 -07001033
Antoine Pitroud6967322014-10-18 00:35:00 +02001034 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
Benjamin Petersona6f195e2012-04-30 10:23:40 -04001035 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001036 MAINTAIN_TRACKING(mp, key, value);
Victor Stinner742da042016-09-07 17:40:12 -07001037
1038 /* When insertion order is different from shared key, we can't share
1039 * the key anymore. Convert this instance to combine table.
1040 */
1041 if (_PyDict_HasSplitTable(mp) &&
1042 ((ix >= 0 && *value_addr == NULL && mp->ma_used != ix) ||
1043 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
1044 if (insertion_resize(mp) < 0) {
1045 Py_DECREF(value);
1046 return -1;
1047 }
1048 find_empty_slot(mp, key, hash, &value_addr, &hashpos);
1049 ix = DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001050 }
Victor Stinner742da042016-09-07 17:40:12 -07001051
1052 if (ix == DKIX_EMPTY) {
1053 /* Insert into new slot. */
1054 if (mp->ma_keys->dk_usable <= 0) {
1055 /* Need to resize. */
1056 if (insertion_resize(mp) < 0) {
1057 Py_DECREF(value);
1058 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001059 }
Victor Stinner742da042016-09-07 17:40:12 -07001060 find_empty_slot(mp, key, hash, &value_addr, &hashpos);
1061 }
1062 ep0 = DK_ENTRIES(mp->ma_keys);
1063 ep = &ep0[mp->ma_keys->dk_nentries];
1064 dk_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
1065 Py_INCREF(key);
1066 ep->me_key = key;
1067 ep->me_hash = hash;
1068 if (mp->ma_values) {
1069 assert (mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
1070 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001071 }
1072 else {
Victor Stinner742da042016-09-07 17:40:12 -07001073 ep->me_value = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001074 }
1075 mp->ma_used++;
Victor Stinner742da042016-09-07 17:40:12 -07001076 mp->ma_keys->dk_usable--;
1077 mp->ma_keys->dk_nentries++;
1078 assert(mp->ma_keys->dk_usable >= 0);
1079 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001080 }
Victor Stinner742da042016-09-07 17:40:12 -07001081
1082 assert(value_addr != NULL);
1083
1084 old_value = *value_addr;
1085 if (old_value != NULL) {
1086 *value_addr = value;
1087 Py_DECREF(old_value); /* which **CAN** re-enter (see issue #22653) */
1088 return 0;
1089 }
1090
1091 /* pending state */
1092 assert(_PyDict_HasSplitTable(mp));
1093 assert(ix == mp->ma_used);
1094 *value_addr = value;
1095 mp->ma_used++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001096 return 0;
Antoine Pitroue965d972012-02-27 00:45:12 +01001097}
1098
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001099/*
1100Internal routine used by dictresize() to insert an item which is
1101known to be absent from the dict. This routine also assumes that
1102the dict contains no deleted entries. Besides the performance benefit,
1103using insertdict() in dictresize() is dangerous (SF bug #1456209).
1104Note that no refcounts are changed by this routine; if needed, the caller
1105is responsible for incref'ing `key` and `value`.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001106Neither mp->ma_used nor k->dk_usable are modified by this routine; the caller
1107must set them correctly
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001108*/
1109static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001110insertdict_clean(PyDictObject *mp, PyObject *key, Py_hash_t hash,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 PyObject *value)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001112{
Victor Stinner742da042016-09-07 17:40:12 -07001113 size_t i, perturb;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001114 PyDictKeysObject *k = mp->ma_keys;
1115 size_t mask = (size_t)DK_SIZE(k)-1;
Victor Stinner742da042016-09-07 17:40:12 -07001116 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001117 PyDictKeyEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001118
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001119 assert(k->dk_lookup != NULL);
1120 assert(value != NULL);
1121 assert(key != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001122 assert(PyUnicode_CheckExact(key) || k->dk_lookup == lookdict);
1123 i = hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -07001124 for (perturb = hash; dk_get_index(k, i) != DKIX_EMPTY;
1125 perturb >>= PERTURB_SHIFT) {
1126 i = mask & ((i << 2) + i + perturb + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 }
Victor Stinner742da042016-09-07 17:40:12 -07001128 ep = &ep0[k->dk_nentries];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 assert(ep->me_value == NULL);
Victor Stinner742da042016-09-07 17:40:12 -07001130 dk_set_index(k, i, k->dk_nentries);
1131 k->dk_nentries++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 ep->me_key = key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001133 ep->me_hash = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001135}
1136
1137/*
1138Restructure the table by allocating a new table and reinserting all
1139items again. When entries have been deleted, the new table may
1140actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001141If a table is split (its keys and hashes are shared, its values are not),
1142then the values are temporarily copied into the table, it is resized as
1143a combined table, then the me_value slots in the old table are NULLed out.
1144After resizing a table is always combined,
1145but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001146*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001147static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001148dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001149{
Victor Stinner742da042016-09-07 17:40:12 -07001150 Py_ssize_t i, newsize;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001151 PyDictKeysObject *oldkeys;
1152 PyObject **oldvalues;
Victor Stinner742da042016-09-07 17:40:12 -07001153 PyDictKeyEntry *ep0;
Tim Peters91a364d2001-05-19 07:04:38 +00001154
Victor Stinner742da042016-09-07 17:40:12 -07001155 /* Find the smallest table size > minused. */
1156 for (newsize = PyDict_MINSIZE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 newsize <= minused && newsize > 0;
1158 newsize <<= 1)
1159 ;
1160 if (newsize <= 0) {
1161 PyErr_NoMemory();
1162 return -1;
1163 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001164 oldkeys = mp->ma_keys;
1165 oldvalues = mp->ma_values;
1166 /* Allocate a new table. */
1167 mp->ma_keys = new_keys_object(newsize);
1168 if (mp->ma_keys == NULL) {
1169 mp->ma_keys = oldkeys;
1170 return -1;
1171 }
1172 if (oldkeys->dk_lookup == lookdict)
1173 mp->ma_keys->dk_lookup = lookdict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001174 mp->ma_values = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001175 ep0 = DK_ENTRIES(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001176 /* Main loop below assumes we can transfer refcount to new keys
1177 * and that value is stored in me_value.
1178 * Increment ref-counts and copy values here to compensate
1179 * This (resizing a split table) should be relatively rare */
1180 if (oldvalues != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001181 for (i = 0; i < oldkeys->dk_nentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001182 if (oldvalues[i] != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001183 Py_INCREF(ep0[i].me_key);
1184 ep0[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 }
1187 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001188 /* Main loop */
Victor Stinner742da042016-09-07 17:40:12 -07001189 for (i = 0; i < oldkeys->dk_nentries; i++) {
1190 PyDictKeyEntry *ep = &ep0[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001191 if (ep->me_value != NULL) {
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001192 insertdict_clean(mp, ep->me_key, ep->me_hash, ep->me_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001195 mp->ma_keys->dk_usable -= mp->ma_used;
1196 if (oldvalues != NULL) {
1197 /* NULL out me_value slot in oldkeys, in case it was shared */
Victor Stinner742da042016-09-07 17:40:12 -07001198 for (i = 0; i < oldkeys->dk_nentries; i++)
1199 ep0[i].me_value = NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001200 DK_DECREF(oldkeys);
Victor Stinner742da042016-09-07 17:40:12 -07001201 if (oldvalues != empty_values) {
1202 free_values(oldvalues);
1203 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001204 }
1205 else {
1206 assert(oldkeys->dk_lookup != lookdict_split);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001207 assert(oldkeys->dk_refcnt == 1);
Raymond Hettingerce5179f2016-01-31 08:56:21 -08001208 DK_DEBUG_DECREF PyObject_FREE(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001209 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001211}
1212
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001213/* Returns NULL if unable to split table.
1214 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001215static PyDictKeysObject *
1216make_keys_shared(PyObject *op)
1217{
1218 Py_ssize_t i;
1219 Py_ssize_t size;
1220 PyDictObject *mp = (PyDictObject *)op;
1221
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001222 if (!PyDict_CheckExact(op))
1223 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001224 if (!_PyDict_HasSplitTable(mp)) {
1225 PyDictKeyEntry *ep0;
1226 PyObject **values;
1227 assert(mp->ma_keys->dk_refcnt == 1);
1228 if (mp->ma_keys->dk_lookup == lookdict) {
1229 return NULL;
1230 }
1231 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1232 /* Remove dummy keys */
1233 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1234 return NULL;
1235 }
1236 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1237 /* Copy values into a new array */
Victor Stinner742da042016-09-07 17:40:12 -07001238 ep0 = DK_ENTRIES(mp->ma_keys);
1239 size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001240 values = new_values(size);
1241 if (values == NULL) {
1242 PyErr_SetString(PyExc_MemoryError,
1243 "Not enough memory to allocate new values array");
1244 return NULL;
1245 }
1246 for (i = 0; i < size; i++) {
1247 values[i] = ep0[i].me_value;
1248 ep0[i].me_value = NULL;
1249 }
1250 mp->ma_keys->dk_lookup = lookdict_split;
1251 mp->ma_values = values;
1252 }
1253 DK_INCREF(mp->ma_keys);
1254 return mp->ma_keys;
1255}
Christian Heimes99170a52007-12-19 02:07:34 +00001256
1257PyObject *
1258_PyDict_NewPresized(Py_ssize_t minused)
1259{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001260 Py_ssize_t newsize;
1261 PyDictKeysObject *new_keys;
Victor Stinner742da042016-09-07 17:40:12 -07001262 for (newsize = PyDict_MINSIZE;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001263 newsize <= minused && newsize > 0;
1264 newsize <<= 1)
1265 ;
1266 new_keys = new_keys_object(newsize);
1267 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001269 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001270}
1271
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001272/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1273 * that may occur (originally dicts supported only string keys, and exceptions
1274 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001275 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001276 * (suppressed) occurred while computing the key's hash, or that some error
1277 * (suppressed) occurred when comparing keys in the dict's internal probe
1278 * sequence. A nasty example of the latter is when a Python-coded comparison
1279 * function hits a stack-depth error, which can cause this to return NULL
1280 * even if the key is present.
1281 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001282PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001283PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001284{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001285 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07001286 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 PyDictObject *mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 PyThreadState *tstate;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001289 PyObject **value_addr;
1290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 if (!PyDict_Check(op))
1292 return NULL;
1293 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001294 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 {
1296 hash = PyObject_Hash(key);
1297 if (hash == -1) {
1298 PyErr_Clear();
1299 return NULL;
1300 }
1301 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 /* We can arrive here with a NULL tstate during initialization: try
1304 running "python -Wi" for an example related to string interning.
1305 Let's just hope that no exception occurs then... This must be
1306 _PyThreadState_Current and not PyThreadState_GET() because in debug
1307 mode, the latter complains if tstate is NULL. */
Victor Stinnerbfd316e2016-01-20 11:12:38 +01001308 tstate = _PyThreadState_UncheckedGet();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 if (tstate != NULL && tstate->curexc_type != NULL) {
1310 /* preserve the existing exception */
1311 PyObject *err_type, *err_value, *err_tb;
1312 PyErr_Fetch(&err_type, &err_value, &err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001313 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 /* ignore errors */
1315 PyErr_Restore(err_type, err_value, err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001316 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 return NULL;
1318 }
1319 else {
Victor Stinner742da042016-09-07 17:40:12 -07001320 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
1321 if (ix < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 PyErr_Clear();
1323 return NULL;
1324 }
1325 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001326 return *value_addr;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001327}
1328
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001329PyObject *
1330_PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1331{
Victor Stinner742da042016-09-07 17:40:12 -07001332 Py_ssize_t ix;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001333 PyDictObject *mp = (PyDictObject *)op;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001334 PyThreadState *tstate;
1335 PyObject **value_addr;
1336
1337 if (!PyDict_Check(op))
1338 return NULL;
1339
1340 /* We can arrive here with a NULL tstate during initialization: try
1341 running "python -Wi" for an example related to string interning.
1342 Let's just hope that no exception occurs then... This must be
1343 _PyThreadState_Current and not PyThreadState_GET() because in debug
1344 mode, the latter complains if tstate is NULL. */
Victor Stinnerbfd316e2016-01-20 11:12:38 +01001345 tstate = _PyThreadState_UncheckedGet();
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001346 if (tstate != NULL && tstate->curexc_type != NULL) {
1347 /* preserve the existing exception */
1348 PyObject *err_type, *err_value, *err_tb;
1349 PyErr_Fetch(&err_type, &err_value, &err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001350 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001351 /* ignore errors */
1352 PyErr_Restore(err_type, err_value, err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001353 if (ix == DKIX_EMPTY)
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001354 return NULL;
1355 }
1356 else {
Victor Stinner742da042016-09-07 17:40:12 -07001357 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
1358 if (ix == DKIX_EMPTY) {
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001359 PyErr_Clear();
1360 return NULL;
1361 }
1362 }
1363 return *value_addr;
1364}
1365
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001366/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1367 This returns NULL *with* an exception set if an exception occurred.
1368 It returns NULL *without* an exception set if the key wasn't present.
1369*/
1370PyObject *
1371PyDict_GetItemWithError(PyObject *op, PyObject *key)
1372{
Victor Stinner742da042016-09-07 17:40:12 -07001373 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001374 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 PyDictObject*mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001376 PyObject **value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 if (!PyDict_Check(op)) {
1379 PyErr_BadInternalCall();
1380 return NULL;
1381 }
1382 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001383 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 {
1385 hash = PyObject_Hash(key);
1386 if (hash == -1) {
1387 return NULL;
1388 }
1389 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001390
Victor Stinner742da042016-09-07 17:40:12 -07001391 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
1392 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001394 return *value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001395}
1396
Brett Cannonfd074152012-04-14 14:10:13 -04001397PyObject *
1398_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1399{
1400 PyObject *kv;
1401 kv = _PyUnicode_FromId(key); /* borrowed */
1402 if (kv == NULL)
1403 return NULL;
1404 return PyDict_GetItemWithError(dp, kv);
1405}
1406
Victor Stinnerb4efc962015-11-20 09:24:02 +01001407/* Fast version of global value lookup (LOAD_GLOBAL).
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001408 * Lookup in globals, then builtins.
Victor Stinnerb4efc962015-11-20 09:24:02 +01001409 *
1410 * Raise an exception and return NULL if an error occurred (ex: computing the
1411 * key hash failed, key comparison failed, ...). Return NULL if the key doesn't
1412 * exist. Return the value if the key exists.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001413 */
1414PyObject *
1415_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001416{
Victor Stinner742da042016-09-07 17:40:12 -07001417 Py_ssize_t ix;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001418 Py_hash_t hash;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001419 PyObject **value_addr;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001420
1421 if (!PyUnicode_CheckExact(key) ||
1422 (hash = ((PyASCIIObject *) key)->hash) == -1)
1423 {
1424 hash = PyObject_Hash(key);
1425 if (hash == -1)
1426 return NULL;
Antoine Pitroue965d972012-02-27 00:45:12 +01001427 }
Victor Stinnerb4efc962015-11-20 09:24:02 +01001428
1429 /* namespace 1: globals */
Victor Stinner742da042016-09-07 17:40:12 -07001430 ix = globals->ma_keys->dk_lookup(globals, key, hash, &value_addr, NULL);
1431 if (ix == DKIX_ERROR)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001432 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001433 if (ix != DKIX_EMPTY && *value_addr != NULL)
1434 return *value_addr;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001435
1436 /* namespace 2: builtins */
Victor Stinner742da042016-09-07 17:40:12 -07001437 ix = builtins->ma_keys->dk_lookup(builtins, key, hash, &value_addr, NULL);
1438 if (ix < 0)
Victor Stinnerb4efc962015-11-20 09:24:02 +01001439 return NULL;
1440 return *value_addr;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001441}
1442
Antoine Pitroue965d972012-02-27 00:45:12 +01001443/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1444 * dictionary if it's merely replacing the value for an existing key.
1445 * This means that it's safe to loop over a dictionary with PyDict_Next()
1446 * and occasionally replace a value -- but you can't insert new keys or
1447 * remove them.
1448 */
1449int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001450PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001451{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001452 PyDictObject *mp;
1453 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001454 if (!PyDict_Check(op)) {
1455 PyErr_BadInternalCall();
1456 return -1;
1457 }
1458 assert(key);
1459 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001460 mp = (PyDictObject *)op;
1461 if (!PyUnicode_CheckExact(key) ||
1462 (hash = ((PyASCIIObject *) key)->hash) == -1)
1463 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001464 hash = PyObject_Hash(key);
1465 if (hash == -1)
1466 return -1;
1467 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001468
1469 /* insertdict() handles any resizing that might be necessary */
1470 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001471}
1472
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001473int
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001474_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1475 Py_hash_t hash)
1476{
1477 PyDictObject *mp;
1478
1479 if (!PyDict_Check(op)) {
1480 PyErr_BadInternalCall();
1481 return -1;
1482 }
1483 assert(key);
1484 assert(value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001485 assert(hash != -1);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001486 mp = (PyDictObject *)op;
1487
1488 /* insertdict() handles any resizing that might be necessary */
1489 return insertdict(mp, key, hash, value);
1490}
1491
1492int
Tim Peters1f5871e2000-07-04 17:44:48 +00001493PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001494{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001495 Py_hash_t hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +00001496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 assert(key);
1498 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001499 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001500 hash = PyObject_Hash(key);
1501 if (hash == -1)
1502 return -1;
1503 }
Victor Stinner742da042016-09-07 17:40:12 -07001504
1505 return _PyDict_DelItem_KnownHash(op, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001506}
1507
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001508int
1509_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1510{
Victor Stinner742da042016-09-07 17:40:12 -07001511 Py_ssize_t hashpos, ix;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001512 PyDictObject *mp;
1513 PyDictKeyEntry *ep;
1514 PyObject *old_key, *old_value;
1515 PyObject **value_addr;
1516
1517 if (!PyDict_Check(op)) {
1518 PyErr_BadInternalCall();
1519 return -1;
1520 }
1521 assert(key);
1522 assert(hash != -1);
1523 mp = (PyDictObject *)op;
Victor Stinner742da042016-09-07 17:40:12 -07001524 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
1525 if (ix == DKIX_ERROR)
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001526 return -1;
Victor Stinner742da042016-09-07 17:40:12 -07001527 if (ix == DKIX_EMPTY || *value_addr == NULL) {
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001528 _PyErr_SetKeyError(key);
1529 return -1;
1530 }
Victor Stinner742da042016-09-07 17:40:12 -07001531 assert(dk_get_index(mp->ma_keys, hashpos) == ix);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001532 old_value = *value_addr;
1533 *value_addr = NULL;
1534 mp->ma_used--;
Victor Stinner742da042016-09-07 17:40:12 -07001535 if (_PyDict_HasSplitTable(mp)) {
1536 mp->ma_keys->dk_usable = 0;
1537 }
1538 else {
1539 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1540 dk_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001541 ENSURE_ALLOWS_DELETIONS(mp);
1542 old_key = ep->me_key;
Victor Stinner742da042016-09-07 17:40:12 -07001543 ep->me_key = NULL;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001544 Py_DECREF(old_key);
1545 }
1546 Py_DECREF(old_value);
1547 return 0;
1548}
1549
Guido van Rossum25831651993-05-19 14:50:45 +00001550void
Tim Peters1f5871e2000-07-04 17:44:48 +00001551PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001552{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001553 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001554 PyDictKeysObject *oldkeys;
1555 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 if (!PyDict_Check(op))
1559 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001560 mp = ((PyDictObject *)op);
1561 oldkeys = mp->ma_keys;
1562 oldvalues = mp->ma_values;
1563 if (oldvalues == empty_values)
1564 return;
1565 /* Empty the dict... */
1566 DK_INCREF(Py_EMPTY_KEYS);
1567 mp->ma_keys = Py_EMPTY_KEYS;
1568 mp->ma_values = empty_values;
1569 mp->ma_used = 0;
1570 /* ...then clear the keys and values */
1571 if (oldvalues != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001572 n = oldkeys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001573 for (i = 0; i < n; i++)
1574 Py_CLEAR(oldvalues[i]);
1575 free_values(oldvalues);
1576 DK_DECREF(oldkeys);
1577 }
1578 else {
1579 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001580 DK_DECREF(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001581 }
1582}
1583
1584/* Returns -1 if no more items (or op is not a dict),
1585 * index of item otherwise. Stores value in pvalue
1586 */
Benjamin Peterson73222252016-09-08 09:58:47 -07001587static inline Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001588dict_next(PyObject *op, Py_ssize_t i, PyObject **pvalue)
1589{
Victor Stinner742da042016-09-07 17:40:12 -07001590 Py_ssize_t n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001591 PyDictObject *mp;
Victor Stinner742da042016-09-07 17:40:12 -07001592 PyObject **value_ptr = NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001593
1594 if (!PyDict_Check(op))
1595 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001597 if (i < 0)
1598 return -1;
Victor Stinner742da042016-09-07 17:40:12 -07001599
1600 n = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001601 if (mp->ma_values) {
Victor Stinner742da042016-09-07 17:40:12 -07001602 for (; i < n; i++) {
1603 value_ptr = &mp->ma_values[i];
1604 if (*value_ptr != NULL)
1605 break;
1606 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001608 else {
Victor Stinner742da042016-09-07 17:40:12 -07001609 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
1610 for (; i < n; i++) {
1611 value_ptr = &ep0[i].me_value;
1612 if (*value_ptr != NULL)
1613 break;
1614 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 }
Victor Stinner742da042016-09-07 17:40:12 -07001616 if (i >= n)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001617 return -1;
1618 if (pvalue)
1619 *pvalue = *value_ptr;
1620 return i;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001621}
1622
Tim Peters080c88b2003-02-15 03:01:11 +00001623/*
1624 * Iterate over a dict. Use like so:
1625 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001626 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001627 * PyObject *key, *value;
1628 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001629 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +00001630 * Refer to borrowed references in key and value.
1631 * }
1632 *
1633 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001634 * mutates the dict. One exception: it is safe if the loop merely changes
1635 * the values associated with the keys (but doesn't insert new keys or
1636 * delete keys), via PyDict_SetItem().
1637 */
Guido van Rossum25831651993-05-19 14:50:45 +00001638int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001639PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001640{
Victor Stinner742da042016-09-07 17:40:12 -07001641 PyDictObject *mp = (PyDictObject*)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001642 Py_ssize_t i = dict_next(op, *ppos, pvalue);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001643 if (i < 0)
1644 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001645 mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 *ppos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 if (pkey)
Victor Stinner742da042016-09-07 17:40:12 -07001648 *pkey = DK_ENTRIES(mp->ma_keys)[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001650}
1651
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001652/* Internal version of PyDict_Next that returns a hash value in addition
1653 * to the key and value.
1654 */
Thomas Wouterscf297e42007-02-23 15:07:44 +00001655int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001656_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1657 PyObject **pvalue, Py_hash_t *phash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00001658{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001659 PyDictObject *mp;
Victor Stinner742da042016-09-07 17:40:12 -07001660 PyDictKeyEntry *ep0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001661 Py_ssize_t i = dict_next(op, *ppos, pvalue);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001662 if (i < 0)
1663 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001664 mp = (PyDictObject *)op;
Victor Stinner742da042016-09-07 17:40:12 -07001665 ep0 = DK_ENTRIES(mp->ma_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 *ppos = i+1;
Victor Stinner742da042016-09-07 17:40:12 -07001667 *phash = ep0[i].me_hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 if (pkey)
Victor Stinner742da042016-09-07 17:40:12 -07001669 *pkey = ep0[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 return 1;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001671}
1672
Eric Snow96c6af92015-05-29 22:21:39 -06001673/* Internal version of dict.pop(). */
1674PyObject *
1675_PyDict_Pop(PyDictObject *mp, PyObject *key, PyObject *deflt)
1676{
1677 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07001678 Py_ssize_t ix, hashpos;
Eric Snow96c6af92015-05-29 22:21:39 -06001679 PyObject *old_value, *old_key;
1680 PyDictKeyEntry *ep;
1681 PyObject **value_addr;
1682
1683 if (mp->ma_used == 0) {
1684 if (deflt) {
1685 Py_INCREF(deflt);
1686 return deflt;
1687 }
1688 _PyErr_SetKeyError(key);
1689 return NULL;
1690 }
1691 if (!PyUnicode_CheckExact(key) ||
1692 (hash = ((PyASCIIObject *) key)->hash) == -1) {
1693 hash = PyObject_Hash(key);
1694 if (hash == -1)
1695 return NULL;
1696 }
Victor Stinner742da042016-09-07 17:40:12 -07001697 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
1698 if (ix == DKIX_ERROR)
Eric Snow96c6af92015-05-29 22:21:39 -06001699 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001700 if (ix == DKIX_EMPTY) {
Eric Snow96c6af92015-05-29 22:21:39 -06001701 if (deflt) {
1702 Py_INCREF(deflt);
1703 return deflt;
1704 }
1705 _PyErr_SetKeyError(key);
1706 return NULL;
1707 }
Victor Stinner742da042016-09-07 17:40:12 -07001708 old_value = *value_addr;
Eric Snow96c6af92015-05-29 22:21:39 -06001709 *value_addr = NULL;
1710 mp->ma_used--;
1711 if (!_PyDict_HasSplitTable(mp)) {
Victor Stinner742da042016-09-07 17:40:12 -07001712 dk_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
1713 ep = &DK_ENTRIES(mp->ma_keys)[ix];
Eric Snow96c6af92015-05-29 22:21:39 -06001714 ENSURE_ALLOWS_DELETIONS(mp);
1715 old_key = ep->me_key;
Victor Stinner742da042016-09-07 17:40:12 -07001716 ep->me_key = NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06001717 Py_DECREF(old_key);
1718 }
1719 return old_value;
1720}
1721
1722/* Internal version of dict.from_keys(). It is subclass-friendly. */
1723PyObject *
1724_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1725{
1726 PyObject *it; /* iter(iterable) */
1727 PyObject *key;
1728 PyObject *d;
1729 int status;
1730
1731 d = PyObject_CallObject(cls, NULL);
1732 if (d == NULL)
1733 return NULL;
1734
1735 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
1736 if (PyDict_CheckExact(iterable)) {
1737 PyDictObject *mp = (PyDictObject *)d;
1738 PyObject *oldvalue;
1739 Py_ssize_t pos = 0;
1740 PyObject *key;
1741 Py_hash_t hash;
1742
Victor Stinner742da042016-09-07 17:40:12 -07001743 if (dictresize(mp, ESTIMATE_SIZE(Py_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001744 Py_DECREF(d);
1745 return NULL;
1746 }
1747
1748 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
1749 if (insertdict(mp, key, hash, value)) {
1750 Py_DECREF(d);
1751 return NULL;
1752 }
1753 }
1754 return d;
1755 }
1756 if (PyAnySet_CheckExact(iterable)) {
1757 PyDictObject *mp = (PyDictObject *)d;
1758 Py_ssize_t pos = 0;
1759 PyObject *key;
1760 Py_hash_t hash;
1761
Victor Stinner742da042016-09-07 17:40:12 -07001762 if (dictresize(mp, ESTIMATE_SIZE(PySet_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001763 Py_DECREF(d);
1764 return NULL;
1765 }
1766
1767 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
1768 if (insertdict(mp, key, hash, value)) {
1769 Py_DECREF(d);
1770 return NULL;
1771 }
1772 }
1773 return d;
1774 }
1775 }
1776
1777 it = PyObject_GetIter(iterable);
1778 if (it == NULL){
1779 Py_DECREF(d);
1780 return NULL;
1781 }
1782
1783 if (PyDict_CheckExact(d)) {
1784 while ((key = PyIter_Next(it)) != NULL) {
1785 status = PyDict_SetItem(d, key, value);
1786 Py_DECREF(key);
1787 if (status < 0)
1788 goto Fail;
1789 }
1790 } else {
1791 while ((key = PyIter_Next(it)) != NULL) {
1792 status = PyObject_SetItem(d, key, value);
1793 Py_DECREF(key);
1794 if (status < 0)
1795 goto Fail;
1796 }
1797 }
1798
1799 if (PyErr_Occurred())
1800 goto Fail;
1801 Py_DECREF(it);
1802 return d;
1803
1804Fail:
1805 Py_DECREF(it);
1806 Py_DECREF(d);
1807 return NULL;
1808}
1809
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001810/* Methods */
1811
1812static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001813dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001814{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001815 PyObject **values = mp->ma_values;
1816 PyDictKeysObject *keys = mp->ma_keys;
1817 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 PyObject_GC_UnTrack(mp);
1819 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001820 if (values != NULL) {
1821 if (values != empty_values) {
Victor Stinner742da042016-09-07 17:40:12 -07001822 for (i = 0, n = mp->ma_keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001823 Py_XDECREF(values[i]);
1824 }
1825 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001827 DK_DECREF(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001828 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02001829 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001830 assert(keys->dk_refcnt == 1);
1831 DK_DECREF(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001832 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001833 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1834 free_list[numfree++] = mp;
1835 else
1836 Py_TYPE(mp)->tp_free((PyObject *)mp);
1837 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001838}
1839
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001840
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001841static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001842dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001843{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001845 PyObject *key = NULL, *value = NULL;
1846 _PyUnicodeWriter writer;
1847 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00001848
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849 i = Py_ReprEnter((PyObject *)mp);
1850 if (i != 0) {
1851 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1852 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001855 Py_ReprLeave((PyObject *)mp);
1856 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 }
Tim Petersa7259592001-06-16 05:11:17 +00001858
Victor Stinnerf91929b2013-11-19 13:07:38 +01001859 _PyUnicodeWriter_Init(&writer);
1860 writer.overallocate = 1;
1861 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
1862 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00001863
Victor Stinnerf91929b2013-11-19 13:07:38 +01001864 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
1865 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001867 /* Do repr() on each key+value pair, and insert ": " between them.
1868 Note that repr may mutate the dict. */
1869 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001870 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001872 PyObject *s;
1873 int res;
1874
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001875 /* Prevent repr from deleting key or value during key format. */
1876 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001877 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02001878
Victor Stinnerf91929b2013-11-19 13:07:38 +01001879 if (!first) {
1880 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
1881 goto error;
1882 }
1883 first = 0;
1884
1885 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01001887 goto error;
1888 res = _PyUnicodeWriter_WriteStr(&writer, s);
1889 Py_DECREF(s);
1890 if (res < 0)
1891 goto error;
1892
1893 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
1894 goto error;
1895
1896 s = PyObject_Repr(value);
1897 if (s == NULL)
1898 goto error;
1899 res = _PyUnicodeWriter_WriteStr(&writer, s);
1900 Py_DECREF(s);
1901 if (res < 0)
1902 goto error;
1903
1904 Py_CLEAR(key);
1905 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 }
Tim Petersa7259592001-06-16 05:11:17 +00001907
Victor Stinnerf91929b2013-11-19 13:07:38 +01001908 writer.overallocate = 0;
1909 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
1910 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01001913
1914 return _PyUnicodeWriter_Finish(&writer);
1915
1916error:
1917 Py_ReprLeave((PyObject *)mp);
1918 _PyUnicodeWriter_Dealloc(&writer);
1919 Py_XDECREF(key);
1920 Py_XDECREF(value);
1921 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001922}
1923
Martin v. Löwis18e16552006-02-15 17:27:45 +00001924static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001925dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001926{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001927 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001928}
1929
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001930static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001931dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001932{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001933 PyObject *v;
Victor Stinner742da042016-09-07 17:40:12 -07001934 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001935 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001936 PyObject **value_addr;
1937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001939 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 hash = PyObject_Hash(key);
1941 if (hash == -1)
1942 return NULL;
1943 }
Victor Stinner742da042016-09-07 17:40:12 -07001944 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
1945 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001946 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001947 if (ix == DKIX_EMPTY || *value_addr == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 if (!PyDict_CheckExact(mp)) {
1949 /* Look up __missing__ method if we're a subclass. */
1950 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05001951 _Py_IDENTIFIER(__missing__);
1952 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953 if (missing != NULL) {
1954 res = PyObject_CallFunctionObjArgs(missing,
1955 key, NULL);
1956 Py_DECREF(missing);
1957 return res;
1958 }
1959 else if (PyErr_Occurred())
1960 return NULL;
1961 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07001962 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 return NULL;
1964 }
Victor Stinner742da042016-09-07 17:40:12 -07001965 v = *value_addr;
1966 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001968}
1969
1970static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001971dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001972{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001973 if (w == NULL)
1974 return PyDict_DelItem((PyObject *)mp, v);
1975 else
1976 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001977}
1978
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001979static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 (lenfunc)dict_length, /*mp_length*/
1981 (binaryfunc)dict_subscript, /*mp_subscript*/
1982 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001983};
1984
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001985static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001986dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001987{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001988 PyObject *v;
1989 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001990 PyDictKeyEntry *ep;
1991 Py_ssize_t size, n, offset;
1992 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001993
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001994 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995 n = mp->ma_used;
1996 v = PyList_New(n);
1997 if (v == NULL)
1998 return NULL;
1999 if (n != mp->ma_used) {
2000 /* Durnit. The allocations caused the dict to resize.
2001 * Just start over, this shouldn't normally happen.
2002 */
2003 Py_DECREF(v);
2004 goto again;
2005 }
Victor Stinner742da042016-09-07 17:40:12 -07002006 ep = DK_ENTRIES(mp->ma_keys);
2007 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002008 if (mp->ma_values) {
2009 value_ptr = mp->ma_values;
2010 offset = sizeof(PyObject *);
2011 }
2012 else {
2013 value_ptr = &ep[0].me_value;
2014 offset = sizeof(PyDictKeyEntry);
2015 }
2016 for (i = 0, j = 0; i < size; i++) {
2017 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002018 PyObject *key = ep[i].me_key;
2019 Py_INCREF(key);
2020 PyList_SET_ITEM(v, j, key);
2021 j++;
2022 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002023 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002024 }
2025 assert(j == n);
2026 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002027}
2028
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002029static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002030dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002031{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002032 PyObject *v;
2033 Py_ssize_t i, j;
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002034 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002035 Py_ssize_t size, n, offset;
2036 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002037
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002038 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002039 n = mp->ma_used;
2040 v = PyList_New(n);
2041 if (v == NULL)
2042 return NULL;
2043 if (n != mp->ma_used) {
2044 /* Durnit. The allocations caused the dict to resize.
2045 * Just start over, this shouldn't normally happen.
2046 */
2047 Py_DECREF(v);
2048 goto again;
2049 }
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002050 ep = DK_ENTRIES(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002051 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002052 if (mp->ma_values) {
2053 value_ptr = mp->ma_values;
2054 offset = sizeof(PyObject *);
2055 }
2056 else {
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002057 value_ptr = &ep[0].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002058 offset = sizeof(PyDictKeyEntry);
2059 }
2060 for (i = 0, j = 0; i < size; i++) {
2061 PyObject *value = *value_ptr;
2062 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2063 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002064 Py_INCREF(value);
2065 PyList_SET_ITEM(v, j, value);
2066 j++;
2067 }
2068 }
2069 assert(j == n);
2070 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002071}
2072
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002073static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002074dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002075{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002076 PyObject *v;
2077 Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002078 Py_ssize_t size, offset;
2079 PyObject *item, *key;
2080 PyDictKeyEntry *ep;
2081 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002083 /* Preallocate the list of tuples, to avoid allocations during
2084 * the loop over the items, which could trigger GC, which
2085 * could resize the dict. :-(
2086 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002087 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002088 n = mp->ma_used;
2089 v = PyList_New(n);
2090 if (v == NULL)
2091 return NULL;
2092 for (i = 0; i < n; i++) {
2093 item = PyTuple_New(2);
2094 if (item == NULL) {
2095 Py_DECREF(v);
2096 return NULL;
2097 }
2098 PyList_SET_ITEM(v, i, item);
2099 }
2100 if (n != mp->ma_used) {
2101 /* Durnit. The allocations caused the dict to resize.
2102 * Just start over, this shouldn't normally happen.
2103 */
2104 Py_DECREF(v);
2105 goto again;
2106 }
2107 /* Nothing we do below makes any function calls. */
Victor Stinner742da042016-09-07 17:40:12 -07002108 ep = DK_ENTRIES(mp->ma_keys);
2109 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002110 if (mp->ma_values) {
2111 value_ptr = mp->ma_values;
2112 offset = sizeof(PyObject *);
2113 }
2114 else {
2115 value_ptr = &ep[0].me_value;
2116 offset = sizeof(PyDictKeyEntry);
2117 }
2118 for (i = 0, j = 0; i < size; i++) {
2119 PyObject *value = *value_ptr;
2120 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2121 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 key = ep[i].me_key;
2123 item = PyList_GET_ITEM(v, j);
2124 Py_INCREF(key);
2125 PyTuple_SET_ITEM(item, 0, key);
2126 Py_INCREF(value);
2127 PyTuple_SET_ITEM(item, 1, value);
2128 j++;
2129 }
2130 }
2131 assert(j == n);
2132 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002133}
2134
Larry Hastings5c661892014-01-24 06:17:25 -08002135/*[clinic input]
2136@classmethod
2137dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08002138 iterable: object
2139 value: object=None
2140 /
2141
2142Returns a new dict with keys from iterable and values equal to value.
2143[clinic start generated code]*/
2144
Larry Hastings5c661892014-01-24 06:17:25 -08002145static PyObject *
2146dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002147/*[clinic end generated code: output=8fb98e4b10384999 input=b85a667f9bf4669d]*/
Larry Hastings5c661892014-01-24 06:17:25 -08002148{
Eric Snow96c6af92015-05-29 22:21:39 -06002149 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002150}
2151
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002152static int
Victor Stinner742da042016-09-07 17:40:12 -07002153dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
2154 const char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002155{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002156 PyObject *arg = NULL;
2157 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002159 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
2160 result = -1;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002163 _Py_IDENTIFIER(keys);
2164 if (_PyObject_HasAttrId(arg, &PyId_keys))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002165 result = PyDict_Merge(self, arg, 1);
2166 else
2167 result = PyDict_MergeFromSeq2(self, arg, 1);
2168 }
2169 if (result == 0 && kwds != NULL) {
2170 if (PyArg_ValidateKeywordArguments(kwds))
2171 result = PyDict_Merge(self, kwds, 1);
2172 else
2173 result = -1;
2174 }
2175 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002176}
2177
2178static PyObject *
2179dict_update(PyObject *self, PyObject *args, PyObject *kwds)
2180{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002181 if (dict_update_common(self, args, kwds, "update") != -1)
2182 Py_RETURN_NONE;
2183 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002184}
2185
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002186/* Update unconditionally replaces existing items.
2187 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00002188 otherwise it leaves existing items unchanged.
2189
2190 PyDict_{Update,Merge} update/merge from a mapping object.
2191
Tim Petersf582b822001-12-11 18:51:08 +00002192 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00002193 producing iterable objects of length 2.
2194*/
2195
Tim Petersf582b822001-12-11 18:51:08 +00002196int
Tim Peters1fc240e2001-10-26 05:06:50 +00002197PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
2198{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199 PyObject *it; /* iter(seq2) */
2200 Py_ssize_t i; /* index into seq2 of current element */
2201 PyObject *item; /* seq2[i] */
2202 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00002203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002204 assert(d != NULL);
2205 assert(PyDict_Check(d));
2206 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00002207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002208 it = PyObject_GetIter(seq2);
2209 if (it == NULL)
2210 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002212 for (i = 0; ; ++i) {
2213 PyObject *key, *value;
2214 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00002215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002216 fast = NULL;
2217 item = PyIter_Next(it);
2218 if (item == NULL) {
2219 if (PyErr_Occurred())
2220 goto Fail;
2221 break;
2222 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224 /* Convert item to sequence, and verify length 2. */
2225 fast = PySequence_Fast(item, "");
2226 if (fast == NULL) {
2227 if (PyErr_ExceptionMatches(PyExc_TypeError))
2228 PyErr_Format(PyExc_TypeError,
2229 "cannot convert dictionary update "
2230 "sequence element #%zd to a sequence",
2231 i);
2232 goto Fail;
2233 }
2234 n = PySequence_Fast_GET_SIZE(fast);
2235 if (n != 2) {
2236 PyErr_Format(PyExc_ValueError,
2237 "dictionary update sequence element #%zd "
2238 "has length %zd; 2 is required",
2239 i, n);
2240 goto Fail;
2241 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002243 /* Update/merge with this (key, value) pair. */
2244 key = PySequence_Fast_GET_ITEM(fast, 0);
2245 value = PySequence_Fast_GET_ITEM(fast, 1);
2246 if (override || PyDict_GetItem(d, key) == NULL) {
2247 int status = PyDict_SetItem(d, key, value);
2248 if (status < 0)
2249 goto Fail;
2250 }
2251 Py_DECREF(fast);
2252 Py_DECREF(item);
2253 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002255 i = 0;
2256 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002257Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002258 Py_XDECREF(item);
2259 Py_XDECREF(fast);
2260 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002261Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262 Py_DECREF(it);
2263 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002264}
2265
Tim Peters6d6c1a32001-08-02 04:15:00 +00002266int
2267PyDict_Update(PyObject *a, PyObject *b)
2268{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002269 return PyDict_Merge(a, b, 1);
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002270}
2271
2272int
2273PyDict_Merge(PyObject *a, PyObject *b, int override)
2274{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002275 PyDictObject *mp, *other;
2276 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -07002277 PyDictKeyEntry *entry, *ep0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002279 /* We accept for the argument either a concrete dictionary object,
2280 * or an abstract "mapping" object. For the former, we can do
2281 * things quite efficiently. For the latter, we only require that
2282 * PyMapping_Keys() and PyObject_GetItem() be supported.
2283 */
2284 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2285 PyErr_BadInternalCall();
2286 return -1;
2287 }
2288 mp = (PyDictObject*)a;
2289 if (PyDict_Check(b)) {
2290 other = (PyDictObject*)b;
2291 if (other == mp || other->ma_used == 0)
2292 /* a.update(a) or a.update({}); nothing to do */
2293 return 0;
2294 if (mp->ma_used == 0)
2295 /* Since the target dict is empty, PyDict_GetItem()
2296 * always returns NULL. Setting override to 1
2297 * skips the unnecessary test.
2298 */
2299 override = 1;
2300 /* Do one big resize at the start, rather than
2301 * incrementally resizing as we insert new items. Expect
2302 * that there will be no (or few) overlapping keys.
2303 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002304 if (mp->ma_keys->dk_usable * 3 < other->ma_used * 2)
2305 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002306 return -1;
Victor Stinner742da042016-09-07 17:40:12 -07002307 ep0 = DK_ENTRIES(other->ma_keys);
2308 for (i = 0, n = other->ma_keys->dk_nentries; i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002309 PyObject *key, *value;
2310 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002311 entry = &ep0[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002312 key = entry->me_key;
2313 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002314 if (other->ma_values)
2315 value = other->ma_values[i];
2316 else
2317 value = entry->me_value;
2318
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002319 if (value != NULL) {
2320 int err = 0;
2321 Py_INCREF(key);
2322 Py_INCREF(value);
2323 if (override || PyDict_GetItem(a, key) == NULL)
2324 err = insertdict(mp, key, hash, value);
2325 Py_DECREF(value);
2326 Py_DECREF(key);
2327 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002328 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002329
Victor Stinner742da042016-09-07 17:40:12 -07002330 if (n != other->ma_keys->dk_nentries) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002331 PyErr_SetString(PyExc_RuntimeError,
2332 "dict mutated during update");
2333 return -1;
2334 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002335 }
2336 }
2337 }
2338 else {
2339 /* Do it the generic, slower way */
2340 PyObject *keys = PyMapping_Keys(b);
2341 PyObject *iter;
2342 PyObject *key, *value;
2343 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002345 if (keys == NULL)
2346 /* Docstring says this is equivalent to E.keys() so
2347 * if E doesn't have a .keys() method we want
2348 * AttributeError to percolate up. Might as well
2349 * do the same for any other error.
2350 */
2351 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353 iter = PyObject_GetIter(keys);
2354 Py_DECREF(keys);
2355 if (iter == NULL)
2356 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
2359 if (!override && PyDict_GetItem(a, key) != NULL) {
2360 Py_DECREF(key);
2361 continue;
2362 }
2363 value = PyObject_GetItem(b, key);
2364 if (value == NULL) {
2365 Py_DECREF(iter);
2366 Py_DECREF(key);
2367 return -1;
2368 }
2369 status = PyDict_SetItem(a, key, value);
2370 Py_DECREF(key);
2371 Py_DECREF(value);
2372 if (status < 0) {
2373 Py_DECREF(iter);
2374 return -1;
2375 }
2376 }
2377 Py_DECREF(iter);
2378 if (PyErr_Occurred())
2379 /* Iterator completed, via error */
2380 return -1;
2381 }
2382 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002383}
2384
2385static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002386dict_copy(PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002387{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002389}
2390
2391PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002392PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002393{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002395 PyDictObject *mp;
2396 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 if (o == NULL || !PyDict_Check(o)) {
2399 PyErr_BadInternalCall();
2400 return NULL;
2401 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002402 mp = (PyDictObject *)o;
2403 if (_PyDict_HasSplitTable(mp)) {
2404 PyDictObject *split_copy;
Victor Stinner742da042016-09-07 17:40:12 -07002405 Py_ssize_t size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
2406 PyObject **newvalues;
2407 newvalues = new_values(size);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002408 if (newvalues == NULL)
2409 return PyErr_NoMemory();
2410 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2411 if (split_copy == NULL) {
2412 free_values(newvalues);
2413 return NULL;
2414 }
2415 split_copy->ma_values = newvalues;
2416 split_copy->ma_keys = mp->ma_keys;
2417 split_copy->ma_used = mp->ma_used;
2418 DK_INCREF(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002419 for (i = 0, n = size; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002420 PyObject *value = mp->ma_values[i];
2421 Py_XINCREF(value);
2422 split_copy->ma_values[i] = value;
2423 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002424 if (_PyObject_GC_IS_TRACKED(mp))
2425 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002426 return (PyObject *)split_copy;
2427 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002428 copy = PyDict_New();
2429 if (copy == NULL)
2430 return NULL;
2431 if (PyDict_Merge(copy, o, 1) == 0)
2432 return copy;
2433 Py_DECREF(copy);
2434 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002435}
2436
Martin v. Löwis18e16552006-02-15 17:27:45 +00002437Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002438PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002439{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002440 if (mp == NULL || !PyDict_Check(mp)) {
2441 PyErr_BadInternalCall();
2442 return -1;
2443 }
2444 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002445}
2446
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002447PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002448PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002449{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002450 if (mp == NULL || !PyDict_Check(mp)) {
2451 PyErr_BadInternalCall();
2452 return NULL;
2453 }
2454 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002455}
2456
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002457PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002458PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002459{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002460 if (mp == NULL || !PyDict_Check(mp)) {
2461 PyErr_BadInternalCall();
2462 return NULL;
2463 }
2464 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002465}
2466
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002467PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002468PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002469{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002470 if (mp == NULL || !PyDict_Check(mp)) {
2471 PyErr_BadInternalCall();
2472 return NULL;
2473 }
2474 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002475}
2476
Tim Peterse63415e2001-05-08 04:38:29 +00002477/* Return 1 if dicts equal, 0 if not, -1 if error.
2478 * Gets out as soon as any difference is detected.
2479 * Uses only Py_EQ comparison.
2480 */
2481static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002482dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002483{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002484 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 if (a->ma_used != b->ma_used)
2487 /* can't be equal if # of entries differ */
2488 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002489 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Victor Stinner742da042016-09-07 17:40:12 -07002490 for (i = 0; i < a->ma_keys->dk_nentries; i++) {
2491 PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002492 PyObject *aval;
2493 if (a->ma_values)
2494 aval = a->ma_values[i];
2495 else
2496 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002497 if (aval != NULL) {
2498 int cmp;
2499 PyObject *bval;
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002500 PyObject **vaddr;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002501 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002502 /* temporarily bump aval's refcount to ensure it stays
2503 alive until we're done with it */
2504 Py_INCREF(aval);
2505 /* ditto for key */
2506 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002507 /* reuse the known hash value */
Victor Stinner742da042016-09-07 17:40:12 -07002508 if ((b->ma_keys->dk_lookup)(b, key, ep->me_hash, &vaddr, NULL) < 0)
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002509 bval = NULL;
2510 else
2511 bval = *vaddr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002512 Py_DECREF(key);
2513 if (bval == NULL) {
2514 Py_DECREF(aval);
2515 if (PyErr_Occurred())
2516 return -1;
2517 return 0;
2518 }
2519 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
2520 Py_DECREF(aval);
2521 if (cmp <= 0) /* error or not equal */
2522 return cmp;
2523 }
2524 }
2525 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002526}
Tim Peterse63415e2001-05-08 04:38:29 +00002527
2528static PyObject *
2529dict_richcompare(PyObject *v, PyObject *w, int op)
2530{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002531 int cmp;
2532 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002534 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2535 res = Py_NotImplemented;
2536 }
2537 else if (op == Py_EQ || op == Py_NE) {
2538 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2539 if (cmp < 0)
2540 return NULL;
2541 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2542 }
2543 else
2544 res = Py_NotImplemented;
2545 Py_INCREF(res);
2546 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002547}
Tim Peterse63415e2001-05-08 04:38:29 +00002548
Larry Hastings61272b72014-01-07 12:41:53 -08002549/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002550
2551@coexist
2552dict.__contains__
2553
2554 key: object
2555 /
2556
Meador Ingee02de8c2014-01-14 16:48:31 -06002557True if D has a key k, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002558[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002559
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002560static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002561dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002562/*[clinic end generated code: output=a3d03db709ed6e6b input=b852b2a19b51ab24]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002563{
Larry Hastingsc2047262014-01-25 20:43:29 -08002564 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002565 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002566 Py_ssize_t ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002567 PyObject **value_addr;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002569 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002570 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002571 hash = PyObject_Hash(key);
2572 if (hash == -1)
2573 return NULL;
2574 }
Victor Stinner742da042016-09-07 17:40:12 -07002575 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
2576 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002577 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002578 if (ix == DKIX_EMPTY || *value_addr == NULL)
2579 Py_RETURN_FALSE;
2580 Py_RETURN_TRUE;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002581}
2582
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002583static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002584dict_get(PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00002585{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002586 PyObject *key;
2587 PyObject *failobj = Py_None;
2588 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002589 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002590 Py_ssize_t ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002591 PyObject **value_addr;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002593 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
2594 return NULL;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002596 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002597 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002598 hash = PyObject_Hash(key);
2599 if (hash == -1)
2600 return NULL;
2601 }
Victor Stinner742da042016-09-07 17:40:12 -07002602 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
2603 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002604 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002605 if (ix == DKIX_EMPTY || *value_addr == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002606 val = failobj;
Victor Stinner742da042016-09-07 17:40:12 -07002607 else
2608 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002609 Py_INCREF(val);
2610 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002611}
2612
Benjamin Peterson00e98862013-03-07 22:16:29 -05002613PyObject *
2614PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002615{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002616 PyDictObject *mp = (PyDictObject *)d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002617 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002618 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002619 Py_ssize_t hashpos, ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002620 PyDictKeyEntry *ep;
2621 PyObject **value_addr;
Guido van Rossum164452c2000-08-08 16:12:54 +00002622
Benjamin Peterson00e98862013-03-07 22:16:29 -05002623 if (!PyDict_Check(d)) {
2624 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002625 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002626 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002627 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002628 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002629 hash = PyObject_Hash(key);
2630 if (hash == -1)
2631 return NULL;
2632 }
Victor Stinner742da042016-09-07 17:40:12 -07002633 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
2634 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002635 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002636 if (ix == DKIX_EMPTY || *value_addr == NULL) {
2637 val = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002638 if (mp->ma_keys->dk_usable <= 0) {
2639 /* Need to resize. */
2640 if (insertion_resize(mp) < 0)
2641 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002642 find_empty_slot(mp, key, hash, &value_addr, &hashpos);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002643 }
Victor Stinner742da042016-09-07 17:40:12 -07002644 ix = mp->ma_keys->dk_nentries;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002645 Py_INCREF(defaultobj);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002646 Py_INCREF(key);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002647 MAINTAIN_TRACKING(mp, key, defaultobj);
Victor Stinner742da042016-09-07 17:40:12 -07002648 dk_set_index(mp->ma_keys, hashpos, ix);
2649 ep = &DK_ENTRIES(mp->ma_keys)[ix];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002650 ep->me_key = key;
2651 ep->me_hash = hash;
Victor Stinner742da042016-09-07 17:40:12 -07002652 if (mp->ma_values) {
2653 mp->ma_values[ix] = val;
2654 }
2655 else {
2656 ep->me_value = val;
2657 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002658 mp->ma_keys->dk_usable--;
Victor Stinner742da042016-09-07 17:40:12 -07002659 mp->ma_keys->dk_nentries++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002660 mp->ma_used++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002661 }
Victor Stinner742da042016-09-07 17:40:12 -07002662 else
2663 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002664 return val;
Guido van Rossum164452c2000-08-08 16:12:54 +00002665}
2666
Benjamin Peterson00e98862013-03-07 22:16:29 -05002667static PyObject *
2668dict_setdefault(PyDictObject *mp, PyObject *args)
2669{
2670 PyObject *key, *val;
2671 PyObject *defaultobj = Py_None;
2672
2673 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &defaultobj))
2674 return NULL;
2675
Benjamin Peterson55898502013-03-08 08:36:49 -05002676 val = PyDict_SetDefault((PyObject *)mp, key, defaultobj);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002677 Py_XINCREF(val);
2678 return val;
2679}
Guido van Rossum164452c2000-08-08 16:12:54 +00002680
2681static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002682dict_clear(PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002683{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002684 PyDict_Clear((PyObject *)mp);
2685 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002686}
2687
Guido van Rossumba6ab842000-12-12 22:02:18 +00002688static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002689dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002690{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002691 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00002692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002693 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2694 return NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06002695
2696 return _PyDict_Pop(mp, key, deflt);
Guido van Rossume027d982002-04-12 15:11:59 +00002697}
2698
2699static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002700dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002701{
Victor Stinner742da042016-09-07 17:40:12 -07002702 Py_ssize_t i, j;
2703 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002704 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002706 /* Allocate the result tuple before checking the size. Believe it
2707 * or not, this allocation could trigger a garbage collection which
2708 * could empty the dict, so if we checked the size first and that
2709 * happened, the result would be an infinite loop (searching for an
2710 * entry that no longer exists). Note that the usual popitem()
2711 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2712 * tuple away if the dict *is* empty isn't a significant
2713 * inefficiency -- possible, but unlikely in practice.
2714 */
2715 res = PyTuple_New(2);
2716 if (res == NULL)
2717 return NULL;
2718 if (mp->ma_used == 0) {
2719 Py_DECREF(res);
2720 PyErr_SetString(PyExc_KeyError,
2721 "popitem(): dictionary is empty");
2722 return NULL;
2723 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002724 /* Convert split table to combined table */
2725 if (mp->ma_keys->dk_lookup == lookdict_split) {
2726 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2727 Py_DECREF(res);
2728 return NULL;
2729 }
2730 }
2731 ENSURE_ALLOWS_DELETIONS(mp);
Victor Stinner742da042016-09-07 17:40:12 -07002732
2733 /* Pop last item */
2734 ep0 = DK_ENTRIES(mp->ma_keys);
2735 i = mp->ma_keys->dk_nentries - 1;
2736 while (i >= 0 && ep0[i].me_value == NULL) {
2737 i--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002738 }
Victor Stinner742da042016-09-07 17:40:12 -07002739 assert(i >= 0);
2740
2741 ep = &ep0[i];
2742 j = lookdict_index(mp->ma_keys, ep->me_hash, i);
2743 assert(j >= 0);
2744 assert(dk_get_index(mp->ma_keys, j) == i);
2745 dk_set_index(mp->ma_keys, j, DKIX_DUMMY);
2746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002747 PyTuple_SET_ITEM(res, 0, ep->me_key);
2748 PyTuple_SET_ITEM(res, 1, ep->me_value);
Victor Stinner742da042016-09-07 17:40:12 -07002749 ep->me_key = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002750 ep->me_value = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002751 /* We can't dk_usable++ since there is DKIX_DUMMY in indices */
2752 mp->ma_keys->dk_nentries = i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002753 mp->ma_used--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002754 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002755}
2756
Jeremy Hylton8caad492000-06-23 14:18:11 +00002757static int
2758dict_traverse(PyObject *op, visitproc visit, void *arg)
2759{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002760 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson55f44522016-09-05 12:12:59 -07002761 PyDictKeysObject *keys = mp->ma_keys;
Victor Stinner742da042016-09-07 17:40:12 -07002762 PyDictKeyEntry *entries = DK_ENTRIES(mp->ma_keys);
2763 Py_ssize_t i, n = keys->dk_nentries;
2764
Benjamin Peterson55f44522016-09-05 12:12:59 -07002765 if (keys->dk_lookup == lookdict) {
2766 for (i = 0; i < n; i++) {
2767 if (entries[i].me_value != NULL) {
2768 Py_VISIT(entries[i].me_value);
2769 Py_VISIT(entries[i].me_key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002770 }
2771 }
Victor Stinner742da042016-09-07 17:40:12 -07002772 }
2773 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002774 if (mp->ma_values != NULL) {
Benjamin Peterson55f44522016-09-05 12:12:59 -07002775 for (i = 0; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002776 Py_VISIT(mp->ma_values[i]);
2777 }
2778 }
2779 else {
Benjamin Peterson55f44522016-09-05 12:12:59 -07002780 for (i = 0; i < n; i++) {
2781 Py_VISIT(entries[i].me_value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002782 }
2783 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002784 }
2785 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002786}
2787
2788static int
2789dict_tp_clear(PyObject *op)
2790{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002791 PyDict_Clear(op);
2792 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002793}
2794
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002795static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002796
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002797Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06002798_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002799{
Victor Stinner742da042016-09-07 17:40:12 -07002800 Py_ssize_t size, usable, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002801
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002802 size = DK_SIZE(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002803 usable = USABLE_FRACTION(size);
2804
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002805 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002806 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -07002807 res += usable * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002808 /* If the dictionary is split, the keys portion is accounted-for
2809 in the type object. */
2810 if (mp->ma_keys->dk_refcnt == 1)
Victor Stinner98ee9d52016-09-08 09:33:56 -07002811 res += (sizeof(PyDictKeysObject)
2812 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
2813 + DK_IXSIZE(mp->ma_keys) * size
2814 + sizeof(PyDictKeyEntry) * usable);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002815 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002816}
2817
2818Py_ssize_t
2819_PyDict_KeysSize(PyDictKeysObject *keys)
2820{
Victor Stinner98ee9d52016-09-08 09:33:56 -07002821 return (sizeof(PyDictKeysObject)
2822 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
2823 + DK_IXSIZE(keys) * DK_SIZE(keys)
2824 + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002825}
2826
doko@ubuntu.com17210f52016-01-14 14:04:59 +01002827static PyObject *
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002828dict_sizeof(PyDictObject *mp)
2829{
2830 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
2831}
2832
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002833PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
2834
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002835PyDoc_STRVAR(sizeof__doc__,
2836"D.__sizeof__() -> size of D in memory, in bytes");
2837
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002838PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002839"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002840
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002841PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002842"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 +00002843
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002844PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002845"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002846If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00002847
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002848PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00002849"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000028502-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002851
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002852PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04002853"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
2854If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
2855If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
2856In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00002857
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002858PyDoc_STRVAR(clear__doc__,
2859"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002860
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002861PyDoc_STRVAR(copy__doc__,
2862"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00002863
Guido van Rossumb90c8482007-02-10 01:11:45 +00002864/* Forward */
2865static PyObject *dictkeys_new(PyObject *);
2866static PyObject *dictitems_new(PyObject *);
2867static PyObject *dictvalues_new(PyObject *);
2868
Guido van Rossum45c85d12007-07-27 16:31:40 +00002869PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002871PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002873PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00002875
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002876static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07002877 DICT___CONTAINS___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002878 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
2879 getitem__doc__},
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002880 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 sizeof__doc__},
2882 {"get", (PyCFunction)dict_get, METH_VARARGS,
2883 get__doc__},
2884 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
2885 setdefault_doc__},
2886 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
2887 pop__doc__},
2888 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
2889 popitem__doc__},
2890 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
2891 keys__doc__},
2892 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
2893 items__doc__},
2894 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
2895 values__doc__},
2896 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
2897 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08002898 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002899 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
2900 clear__doc__},
2901 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
2902 copy__doc__},
2903 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002904};
2905
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002906/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002907int
2908PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002909{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002910 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002911 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002912 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002913 PyObject **value_addr;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002915 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002916 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 hash = PyObject_Hash(key);
2918 if (hash == -1)
2919 return -1;
2920 }
Victor Stinner742da042016-09-07 17:40:12 -07002921 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
2922 if (ix == DKIX_ERROR)
2923 return -1;
2924 return (ix != DKIX_EMPTY && *value_addr != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002925}
2926
Thomas Wouterscf297e42007-02-23 15:07:44 +00002927/* Internal version of PyDict_Contains used when the hash value is already known */
2928int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002929_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00002930{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002931 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002932 PyObject **value_addr;
Victor Stinner742da042016-09-07 17:40:12 -07002933 Py_ssize_t ix;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002934
Victor Stinner742da042016-09-07 17:40:12 -07002935 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
2936 if (ix == DKIX_ERROR)
2937 return -1;
2938 return (ix != DKIX_EMPTY && *value_addr != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00002939}
2940
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002941/* Hack to implement "key in dict" */
2942static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002943 0, /* sq_length */
2944 0, /* sq_concat */
2945 0, /* sq_repeat */
2946 0, /* sq_item */
2947 0, /* sq_slice */
2948 0, /* sq_ass_item */
2949 0, /* sq_ass_slice */
2950 PyDict_Contains, /* sq_contains */
2951 0, /* sq_inplace_concat */
2952 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002953};
2954
Guido van Rossum09e563a2001-05-01 12:10:21 +00002955static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002956dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2957{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002958 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02002959 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002961 assert(type != NULL && type->tp_alloc != NULL);
2962 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02002963 if (self == NULL)
2964 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02002965 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002966
Victor Stinnera9f61a52013-07-16 22:17:26 +02002967 /* The object has been implicitly tracked by tp_alloc */
2968 if (type == &PyDict_Type)
2969 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002970
2971 d->ma_used = 0;
Victor Stinner742da042016-09-07 17:40:12 -07002972 d->ma_keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002973 if (d->ma_keys == NULL) {
2974 Py_DECREF(self);
2975 return NULL;
2976 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002977 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002978}
2979
Tim Peters25786c02001-09-02 08:22:48 +00002980static int
2981dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2982{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002983 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002984}
2985
Tim Peters6d6c1a32001-08-02 04:15:00 +00002986static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002987dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002988{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002989 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002990}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002991
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002992PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002993"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002994"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002995" (key, value) pairs\n"
2996"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002997" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002998" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002999" d[k] = v\n"
3000"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
3001" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00003002
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003003PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003004 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3005 "dict",
3006 sizeof(PyDictObject),
3007 0,
3008 (destructor)dict_dealloc, /* tp_dealloc */
3009 0, /* tp_print */
3010 0, /* tp_getattr */
3011 0, /* tp_setattr */
3012 0, /* tp_reserved */
3013 (reprfunc)dict_repr, /* tp_repr */
3014 0, /* tp_as_number */
3015 &dict_as_sequence, /* tp_as_sequence */
3016 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003017 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003018 0, /* tp_call */
3019 0, /* tp_str */
3020 PyObject_GenericGetAttr, /* tp_getattro */
3021 0, /* tp_setattro */
3022 0, /* tp_as_buffer */
3023 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3024 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
3025 dictionary_doc, /* tp_doc */
3026 dict_traverse, /* tp_traverse */
3027 dict_tp_clear, /* tp_clear */
3028 dict_richcompare, /* tp_richcompare */
3029 0, /* tp_weaklistoffset */
3030 (getiterfunc)dict_iter, /* tp_iter */
3031 0, /* tp_iternext */
3032 mapp_methods, /* tp_methods */
3033 0, /* tp_members */
3034 0, /* tp_getset */
3035 0, /* tp_base */
3036 0, /* tp_dict */
3037 0, /* tp_descr_get */
3038 0, /* tp_descr_set */
3039 0, /* tp_dictoffset */
3040 dict_init, /* tp_init */
3041 PyType_GenericAlloc, /* tp_alloc */
3042 dict_new, /* tp_new */
3043 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003044};
3045
Victor Stinner3c1e4812012-03-26 22:10:51 +02003046PyObject *
3047_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
3048{
3049 PyObject *kv;
3050 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02003051 if (kv == NULL) {
3052 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003053 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02003054 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02003055 return PyDict_GetItem(dp, kv);
3056}
3057
Guido van Rossum3cca2451997-05-16 14:23:33 +00003058/* For backward compatibility with old dictionary interface */
3059
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003060PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003061PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003062{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003063 PyObject *kv, *rv;
3064 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003065 if (kv == NULL) {
3066 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003067 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003068 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003069 rv = PyDict_GetItem(v, kv);
3070 Py_DECREF(kv);
3071 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003072}
3073
3074int
Victor Stinner3c1e4812012-03-26 22:10:51 +02003075_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
3076{
3077 PyObject *kv;
3078 kv = _PyUnicode_FromId(key); /* borrowed */
3079 if (kv == NULL)
3080 return -1;
3081 return PyDict_SetItem(v, kv, item);
3082}
3083
3084int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003085PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003086{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003087 PyObject *kv;
3088 int err;
3089 kv = PyUnicode_FromString(key);
3090 if (kv == NULL)
3091 return -1;
3092 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
3093 err = PyDict_SetItem(v, kv, item);
3094 Py_DECREF(kv);
3095 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003096}
3097
3098int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01003099_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
3100{
3101 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3102 if (kv == NULL)
3103 return -1;
3104 return PyDict_DelItem(v, kv);
3105}
3106
3107int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003108PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003109{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003110 PyObject *kv;
3111 int err;
3112 kv = PyUnicode_FromString(key);
3113 if (kv == NULL)
3114 return -1;
3115 err = PyDict_DelItem(v, kv);
3116 Py_DECREF(kv);
3117 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003118}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003119
Raymond Hettinger019a1482004-03-18 02:41:19 +00003120/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003121
3122typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003123 PyObject_HEAD
3124 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
3125 Py_ssize_t di_used;
3126 Py_ssize_t di_pos;
3127 PyObject* di_result; /* reusable result tuple for iteritems */
3128 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003129} dictiterobject;
3130
3131static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003132dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003133{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003134 dictiterobject *di;
3135 di = PyObject_GC_New(dictiterobject, itertype);
3136 if (di == NULL)
3137 return NULL;
3138 Py_INCREF(dict);
3139 di->di_dict = dict;
3140 di->di_used = dict->ma_used;
3141 di->di_pos = 0;
3142 di->len = dict->ma_used;
3143 if (itertype == &PyDictIterItem_Type) {
3144 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
3145 if (di->di_result == NULL) {
3146 Py_DECREF(di);
3147 return NULL;
3148 }
3149 }
3150 else
3151 di->di_result = NULL;
3152 _PyObject_GC_TRACK(di);
3153 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003154}
3155
3156static void
3157dictiter_dealloc(dictiterobject *di)
3158{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003159 Py_XDECREF(di->di_dict);
3160 Py_XDECREF(di->di_result);
3161 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003162}
3163
3164static int
3165dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
3166{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003167 Py_VISIT(di->di_dict);
3168 Py_VISIT(di->di_result);
3169 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003170}
3171
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003172static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003173dictiter_len(dictiterobject *di)
3174{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003175 Py_ssize_t len = 0;
3176 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
3177 len = di->len;
3178 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003179}
3180
Guido van Rossumb90c8482007-02-10 01:11:45 +00003181PyDoc_STRVAR(length_hint_doc,
3182 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003183
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003184static PyObject *
3185dictiter_reduce(dictiterobject *di);
3186
3187PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3188
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003189static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003190 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
3191 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003192 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
3193 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003194 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003195};
3196
Raymond Hettinger019a1482004-03-18 02:41:19 +00003197static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00003198{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003199 PyObject *key;
Victor Stinner742da042016-09-07 17:40:12 -07003200 Py_ssize_t i, n, offset;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003201 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003202 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003203 PyObject **value_ptr;
Guido van Rossum213c7a62001-04-23 14:08:49 +00003204
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003205 if (d == NULL)
3206 return NULL;
3207 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00003208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003209 if (di->di_used != d->ma_used) {
3210 PyErr_SetString(PyExc_RuntimeError,
3211 "dictionary changed size during iteration");
3212 di->di_used = -1; /* Make this state sticky */
3213 return NULL;
3214 }
Guido van Rossum2147df72002-07-16 20:30:22 +00003215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003216 i = di->di_pos;
3217 if (i < 0)
3218 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003219 k = d->ma_keys;
3220 if (d->ma_values) {
3221 value_ptr = &d->ma_values[i];
3222 offset = sizeof(PyObject *);
3223 }
3224 else {
Victor Stinner742da042016-09-07 17:40:12 -07003225 value_ptr = &DK_ENTRIES(k)[i].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003226 offset = sizeof(PyDictKeyEntry);
3227 }
Victor Stinner742da042016-09-07 17:40:12 -07003228 n = k->dk_nentries - 1;
3229 while (i <= n && *value_ptr == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003230 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003231 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003232 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003233 di->di_pos = i+1;
Victor Stinner742da042016-09-07 17:40:12 -07003234 if (i > n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003235 goto fail;
3236 di->len--;
Victor Stinner742da042016-09-07 17:40:12 -07003237 key = DK_ENTRIES(k)[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003238 Py_INCREF(key);
3239 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003240
3241fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003242 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003243 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003244 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003245}
3246
Raymond Hettinger019a1482004-03-18 02:41:19 +00003247PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003248 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3249 "dict_keyiterator", /* tp_name */
3250 sizeof(dictiterobject), /* tp_basicsize */
3251 0, /* tp_itemsize */
3252 /* methods */
3253 (destructor)dictiter_dealloc, /* tp_dealloc */
3254 0, /* tp_print */
3255 0, /* tp_getattr */
3256 0, /* tp_setattr */
3257 0, /* tp_reserved */
3258 0, /* tp_repr */
3259 0, /* tp_as_number */
3260 0, /* tp_as_sequence */
3261 0, /* tp_as_mapping */
3262 0, /* tp_hash */
3263 0, /* tp_call */
3264 0, /* tp_str */
3265 PyObject_GenericGetAttr, /* tp_getattro */
3266 0, /* tp_setattro */
3267 0, /* tp_as_buffer */
3268 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3269 0, /* tp_doc */
3270 (traverseproc)dictiter_traverse, /* tp_traverse */
3271 0, /* tp_clear */
3272 0, /* tp_richcompare */
3273 0, /* tp_weaklistoffset */
3274 PyObject_SelfIter, /* tp_iter */
3275 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3276 dictiter_methods, /* tp_methods */
3277 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003278};
3279
3280static PyObject *dictiter_iternextvalue(dictiterobject *di)
3281{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003282 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07003283 Py_ssize_t i, n, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003284 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003285 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003287 if (d == NULL)
3288 return NULL;
3289 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003291 if (di->di_used != d->ma_used) {
3292 PyErr_SetString(PyExc_RuntimeError,
3293 "dictionary changed size during iteration");
3294 di->di_used = -1; /* Make this state sticky */
3295 return NULL;
3296 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003298 i = di->di_pos;
Victor Stinner742da042016-09-07 17:40:12 -07003299 n = d->ma_keys->dk_nentries - 1;
3300 if (i < 0 || i > n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003301 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003302 if (d->ma_values) {
3303 value_ptr = &d->ma_values[i];
3304 offset = sizeof(PyObject *);
3305 }
3306 else {
Victor Stinner742da042016-09-07 17:40:12 -07003307 value_ptr = &DK_ENTRIES(d->ma_keys)[i].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003308 offset = sizeof(PyDictKeyEntry);
3309 }
Victor Stinner742da042016-09-07 17:40:12 -07003310 while (i <= n && *value_ptr == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003311 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003312 i++;
Victor Stinner742da042016-09-07 17:40:12 -07003313 if (i > n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003314 goto fail;
3315 }
3316 di->di_pos = i+1;
3317 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003318 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003319 Py_INCREF(value);
3320 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003321
3322fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003323 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003324 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003325 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003326}
3327
3328PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003329 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3330 "dict_valueiterator", /* tp_name */
3331 sizeof(dictiterobject), /* tp_basicsize */
3332 0, /* tp_itemsize */
3333 /* methods */
3334 (destructor)dictiter_dealloc, /* tp_dealloc */
3335 0, /* tp_print */
3336 0, /* tp_getattr */
3337 0, /* tp_setattr */
3338 0, /* tp_reserved */
3339 0, /* tp_repr */
3340 0, /* tp_as_number */
3341 0, /* tp_as_sequence */
3342 0, /* tp_as_mapping */
3343 0, /* tp_hash */
3344 0, /* tp_call */
3345 0, /* tp_str */
3346 PyObject_GenericGetAttr, /* tp_getattro */
3347 0, /* tp_setattro */
3348 0, /* tp_as_buffer */
3349 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3350 0, /* tp_doc */
3351 (traverseproc)dictiter_traverse, /* tp_traverse */
3352 0, /* tp_clear */
3353 0, /* tp_richcompare */
3354 0, /* tp_weaklistoffset */
3355 PyObject_SelfIter, /* tp_iter */
3356 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3357 dictiter_methods, /* tp_methods */
3358 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003359};
3360
3361static PyObject *dictiter_iternextitem(dictiterobject *di)
3362{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003363 PyObject *key, *value, *result = di->di_result;
Victor Stinner742da042016-09-07 17:40:12 -07003364 Py_ssize_t i, n, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003365 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003366 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003368 if (d == NULL)
3369 return NULL;
3370 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003372 if (di->di_used != d->ma_used) {
3373 PyErr_SetString(PyExc_RuntimeError,
3374 "dictionary changed size during iteration");
3375 di->di_used = -1; /* Make this state sticky */
3376 return NULL;
3377 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003379 i = di->di_pos;
3380 if (i < 0)
3381 goto fail;
Victor Stinner742da042016-09-07 17:40:12 -07003382 n = d->ma_keys->dk_nentries - 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003383 if (d->ma_values) {
3384 value_ptr = &d->ma_values[i];
3385 offset = sizeof(PyObject *);
3386 }
3387 else {
Victor Stinner742da042016-09-07 17:40:12 -07003388 value_ptr = &DK_ENTRIES(d->ma_keys)[i].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003389 offset = sizeof(PyDictKeyEntry);
3390 }
Victor Stinner742da042016-09-07 17:40:12 -07003391 while (i <= n && *value_ptr == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003392 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003393 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003394 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003395 di->di_pos = i+1;
Victor Stinner742da042016-09-07 17:40:12 -07003396 if (i > n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003397 goto fail;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003399 if (result->ob_refcnt == 1) {
3400 Py_INCREF(result);
3401 Py_DECREF(PyTuple_GET_ITEM(result, 0));
3402 Py_DECREF(PyTuple_GET_ITEM(result, 1));
3403 } else {
3404 result = PyTuple_New(2);
3405 if (result == NULL)
3406 return NULL;
3407 }
3408 di->len--;
Victor Stinner742da042016-09-07 17:40:12 -07003409 key = DK_ENTRIES(d->ma_keys)[i].me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003410 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003411 Py_INCREF(key);
3412 Py_INCREF(value);
Eric Snow96c6af92015-05-29 22:21:39 -06003413 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3414 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003415 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003416
3417fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003418 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003419 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003420 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003421}
3422
3423PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003424 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3425 "dict_itemiterator", /* tp_name */
3426 sizeof(dictiterobject), /* tp_basicsize */
3427 0, /* tp_itemsize */
3428 /* methods */
3429 (destructor)dictiter_dealloc, /* tp_dealloc */
3430 0, /* tp_print */
3431 0, /* tp_getattr */
3432 0, /* tp_setattr */
3433 0, /* tp_reserved */
3434 0, /* tp_repr */
3435 0, /* tp_as_number */
3436 0, /* tp_as_sequence */
3437 0, /* tp_as_mapping */
3438 0, /* tp_hash */
3439 0, /* tp_call */
3440 0, /* tp_str */
3441 PyObject_GenericGetAttr, /* tp_getattro */
3442 0, /* tp_setattro */
3443 0, /* tp_as_buffer */
3444 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3445 0, /* tp_doc */
3446 (traverseproc)dictiter_traverse, /* tp_traverse */
3447 0, /* tp_clear */
3448 0, /* tp_richcompare */
3449 0, /* tp_weaklistoffset */
3450 PyObject_SelfIter, /* tp_iter */
3451 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3452 dictiter_methods, /* tp_methods */
3453 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003454};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003455
3456
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003457static PyObject *
3458dictiter_reduce(dictiterobject *di)
3459{
3460 PyObject *list;
3461 dictiterobject tmp;
3462
3463 list = PyList_New(0);
3464 if (!list)
3465 return NULL;
3466
3467 /* copy the itertor state */
3468 tmp = *di;
3469 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003470
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003471 /* iterate the temporary into a list */
3472 for(;;) {
3473 PyObject *element = 0;
3474 if (Py_TYPE(di) == &PyDictIterItem_Type)
3475 element = dictiter_iternextitem(&tmp);
3476 else if (Py_TYPE(di) == &PyDictIterKey_Type)
3477 element = dictiter_iternextkey(&tmp);
3478 else if (Py_TYPE(di) == &PyDictIterValue_Type)
3479 element = dictiter_iternextvalue(&tmp);
3480 else
3481 assert(0);
3482 if (element) {
3483 if (PyList_Append(list, element)) {
3484 Py_DECREF(element);
3485 Py_DECREF(list);
3486 Py_XDECREF(tmp.di_dict);
3487 return NULL;
3488 }
3489 Py_DECREF(element);
3490 } else
3491 break;
3492 }
3493 Py_XDECREF(tmp.di_dict);
3494 /* check for error */
3495 if (tmp.di_dict != NULL) {
3496 /* we have an error */
3497 Py_DECREF(list);
3498 return NULL;
3499 }
Antoine Pitroua7013882012-04-05 00:04:20 +02003500 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003501}
3502
Guido van Rossum3ac67412007-02-10 18:55:06 +00003503/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003504/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003505/***********************************************/
3506
Guido van Rossumb90c8482007-02-10 01:11:45 +00003507/* The instance lay-out is the same for all three; but the type differs. */
3508
Guido van Rossumb90c8482007-02-10 01:11:45 +00003509static void
Eric Snow96c6af92015-05-29 22:21:39 -06003510dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003511{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003512 Py_XDECREF(dv->dv_dict);
3513 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003514}
3515
3516static int
Eric Snow96c6af92015-05-29 22:21:39 -06003517dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003518{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003519 Py_VISIT(dv->dv_dict);
3520 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003521}
3522
Guido van Rossum83825ac2007-02-10 04:54:19 +00003523static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003524dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003525{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003526 Py_ssize_t len = 0;
3527 if (dv->dv_dict != NULL)
3528 len = dv->dv_dict->ma_used;
3529 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003530}
3531
Eric Snow96c6af92015-05-29 22:21:39 -06003532PyObject *
3533_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003534{
Eric Snow96c6af92015-05-29 22:21:39 -06003535 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003536 if (dict == NULL) {
3537 PyErr_BadInternalCall();
3538 return NULL;
3539 }
3540 if (!PyDict_Check(dict)) {
3541 /* XXX Get rid of this restriction later */
3542 PyErr_Format(PyExc_TypeError,
3543 "%s() requires a dict argument, not '%s'",
3544 type->tp_name, dict->ob_type->tp_name);
3545 return NULL;
3546 }
Eric Snow96c6af92015-05-29 22:21:39 -06003547 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003548 if (dv == NULL)
3549 return NULL;
3550 Py_INCREF(dict);
3551 dv->dv_dict = (PyDictObject *)dict;
3552 _PyObject_GC_TRACK(dv);
3553 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003554}
3555
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003556/* TODO(guido): The views objects are not complete:
3557
3558 * support more set operations
3559 * support arbitrary mappings?
3560 - either these should be static or exported in dictobject.h
3561 - if public then they should probably be in builtins
3562*/
3563
Guido van Rossumaac530c2007-08-24 22:33:45 +00003564/* Return 1 if self is a subset of other, iterating over self;
3565 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003566static int
3567all_contained_in(PyObject *self, PyObject *other)
3568{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003569 PyObject *iter = PyObject_GetIter(self);
3570 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003572 if (iter == NULL)
3573 return -1;
3574 for (;;) {
3575 PyObject *next = PyIter_Next(iter);
3576 if (next == NULL) {
3577 if (PyErr_Occurred())
3578 ok = -1;
3579 break;
3580 }
3581 ok = PySequence_Contains(other, next);
3582 Py_DECREF(next);
3583 if (ok <= 0)
3584 break;
3585 }
3586 Py_DECREF(iter);
3587 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003588}
3589
3590static PyObject *
3591dictview_richcompare(PyObject *self, PyObject *other, int op)
3592{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003593 Py_ssize_t len_self, len_other;
3594 int ok;
3595 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003597 assert(self != NULL);
3598 assert(PyDictViewSet_Check(self));
3599 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003600
Brian Curtindfc80e32011-08-10 20:28:54 -05003601 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3602 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003604 len_self = PyObject_Size(self);
3605 if (len_self < 0)
3606 return NULL;
3607 len_other = PyObject_Size(other);
3608 if (len_other < 0)
3609 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003611 ok = 0;
3612 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003614 case Py_NE:
3615 case Py_EQ:
3616 if (len_self == len_other)
3617 ok = all_contained_in(self, other);
3618 if (op == Py_NE && ok >= 0)
3619 ok = !ok;
3620 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003622 case Py_LT:
3623 if (len_self < len_other)
3624 ok = all_contained_in(self, other);
3625 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003627 case Py_LE:
3628 if (len_self <= len_other)
3629 ok = all_contained_in(self, other);
3630 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003632 case Py_GT:
3633 if (len_self > len_other)
3634 ok = all_contained_in(other, self);
3635 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003637 case Py_GE:
3638 if (len_self >= len_other)
3639 ok = all_contained_in(other, self);
3640 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003642 }
3643 if (ok < 0)
3644 return NULL;
3645 result = ok ? Py_True : Py_False;
3646 Py_INCREF(result);
3647 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003648}
3649
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003650static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003651dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003652{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003653 PyObject *seq;
3654 PyObject *result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003656 seq = PySequence_List((PyObject *)dv);
3657 if (seq == NULL)
3658 return NULL;
3659
3660 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
3661 Py_DECREF(seq);
3662 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003663}
3664
Guido van Rossum3ac67412007-02-10 18:55:06 +00003665/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003666
3667static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003668dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003669{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003670 if (dv->dv_dict == NULL) {
3671 Py_RETURN_NONE;
3672 }
3673 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003674}
3675
3676static int
Eric Snow96c6af92015-05-29 22:21:39 -06003677dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00003678{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003679 if (dv->dv_dict == NULL)
3680 return 0;
3681 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003682}
3683
Guido van Rossum83825ac2007-02-10 04:54:19 +00003684static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003685 (lenfunc)dictview_len, /* sq_length */
3686 0, /* sq_concat */
3687 0, /* sq_repeat */
3688 0, /* sq_item */
3689 0, /* sq_slice */
3690 0, /* sq_ass_item */
3691 0, /* sq_ass_slice */
3692 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003693};
3694
Guido van Rossum523259b2007-08-24 23:41:22 +00003695static PyObject*
3696dictviews_sub(PyObject* self, PyObject *other)
3697{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003698 PyObject *result = PySet_New(self);
3699 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003700 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003702 if (result == NULL)
3703 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003704
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003705 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003706 if (tmp == NULL) {
3707 Py_DECREF(result);
3708 return NULL;
3709 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003711 Py_DECREF(tmp);
3712 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003713}
3714
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003715PyObject*
3716_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00003717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003718 PyObject *result = PySet_New(self);
3719 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003720 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003722 if (result == NULL)
3723 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003724
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003725 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_intersection_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003726 if (tmp == NULL) {
3727 Py_DECREF(result);
3728 return NULL;
3729 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003730
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003731 Py_DECREF(tmp);
3732 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003733}
3734
3735static PyObject*
3736dictviews_or(PyObject* self, PyObject *other)
3737{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003738 PyObject *result = PySet_New(self);
3739 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003740 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003742 if (result == NULL)
3743 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003744
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003745 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003746 if (tmp == NULL) {
3747 Py_DECREF(result);
3748 return NULL;
3749 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003751 Py_DECREF(tmp);
3752 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003753}
3754
3755static PyObject*
3756dictviews_xor(PyObject* self, PyObject *other)
3757{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003758 PyObject *result = PySet_New(self);
3759 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003760 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003762 if (result == NULL)
3763 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003764
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003765 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_symmetric_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003766 if (tmp == NULL) {
3767 Py_DECREF(result);
3768 return NULL;
3769 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003771 Py_DECREF(tmp);
3772 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003773}
3774
3775static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003776 0, /*nb_add*/
3777 (binaryfunc)dictviews_sub, /*nb_subtract*/
3778 0, /*nb_multiply*/
3779 0, /*nb_remainder*/
3780 0, /*nb_divmod*/
3781 0, /*nb_power*/
3782 0, /*nb_negative*/
3783 0, /*nb_positive*/
3784 0, /*nb_absolute*/
3785 0, /*nb_bool*/
3786 0, /*nb_invert*/
3787 0, /*nb_lshift*/
3788 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003789 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003790 (binaryfunc)dictviews_xor, /*nb_xor*/
3791 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00003792};
3793
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003794static PyObject*
3795dictviews_isdisjoint(PyObject *self, PyObject *other)
3796{
3797 PyObject *it;
3798 PyObject *item = NULL;
3799
3800 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06003801 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003802 Py_RETURN_TRUE;
3803 else
3804 Py_RETURN_FALSE;
3805 }
3806
3807 /* Iterate over the shorter object (only if other is a set,
3808 * because PySequence_Contains may be expensive otherwise): */
3809 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06003810 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003811 Py_ssize_t len_other = PyObject_Size(other);
3812 if (len_other == -1)
3813 return NULL;
3814
3815 if ((len_other > len_self)) {
3816 PyObject *tmp = other;
3817 other = self;
3818 self = tmp;
3819 }
3820 }
3821
3822 it = PyObject_GetIter(other);
3823 if (it == NULL)
3824 return NULL;
3825
3826 while ((item = PyIter_Next(it)) != NULL) {
3827 int contains = PySequence_Contains(self, item);
3828 Py_DECREF(item);
3829 if (contains == -1) {
3830 Py_DECREF(it);
3831 return NULL;
3832 }
3833
3834 if (contains) {
3835 Py_DECREF(it);
3836 Py_RETURN_FALSE;
3837 }
3838 }
3839 Py_DECREF(it);
3840 if (PyErr_Occurred())
3841 return NULL; /* PyIter_Next raised an exception. */
3842 Py_RETURN_TRUE;
3843}
3844
3845PyDoc_STRVAR(isdisjoint_doc,
3846"Return True if the view and the given iterable have a null intersection.");
3847
Guido van Rossumb90c8482007-02-10 01:11:45 +00003848static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003849 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3850 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003851 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003852};
3853
3854PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003855 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3856 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06003857 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003858 0, /* tp_itemsize */
3859 /* methods */
3860 (destructor)dictview_dealloc, /* tp_dealloc */
3861 0, /* tp_print */
3862 0, /* tp_getattr */
3863 0, /* tp_setattr */
3864 0, /* tp_reserved */
3865 (reprfunc)dictview_repr, /* tp_repr */
3866 &dictviews_as_number, /* tp_as_number */
3867 &dictkeys_as_sequence, /* tp_as_sequence */
3868 0, /* tp_as_mapping */
3869 0, /* tp_hash */
3870 0, /* tp_call */
3871 0, /* tp_str */
3872 PyObject_GenericGetAttr, /* tp_getattro */
3873 0, /* tp_setattro */
3874 0, /* tp_as_buffer */
3875 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3876 0, /* tp_doc */
3877 (traverseproc)dictview_traverse, /* tp_traverse */
3878 0, /* tp_clear */
3879 dictview_richcompare, /* tp_richcompare */
3880 0, /* tp_weaklistoffset */
3881 (getiterfunc)dictkeys_iter, /* tp_iter */
3882 0, /* tp_iternext */
3883 dictkeys_methods, /* tp_methods */
3884 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003885};
3886
3887static PyObject *
3888dictkeys_new(PyObject *dict)
3889{
Eric Snow96c6af92015-05-29 22:21:39 -06003890 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003891}
3892
Guido van Rossum3ac67412007-02-10 18:55:06 +00003893/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003894
3895static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003896dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003897{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003898 if (dv->dv_dict == NULL) {
3899 Py_RETURN_NONE;
3900 }
3901 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003902}
3903
3904static int
Eric Snow96c6af92015-05-29 22:21:39 -06003905dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00003906{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003907 PyObject *key, *value, *found;
3908 if (dv->dv_dict == NULL)
3909 return 0;
3910 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
3911 return 0;
3912 key = PyTuple_GET_ITEM(obj, 0);
3913 value = PyTuple_GET_ITEM(obj, 1);
3914 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
3915 if (found == NULL) {
3916 if (PyErr_Occurred())
3917 return -1;
3918 return 0;
3919 }
3920 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003921}
3922
Guido van Rossum83825ac2007-02-10 04:54:19 +00003923static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003924 (lenfunc)dictview_len, /* sq_length */
3925 0, /* sq_concat */
3926 0, /* sq_repeat */
3927 0, /* sq_item */
3928 0, /* sq_slice */
3929 0, /* sq_ass_item */
3930 0, /* sq_ass_slice */
3931 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003932};
3933
Guido van Rossumb90c8482007-02-10 01:11:45 +00003934static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003935 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3936 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003937 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003938};
3939
3940PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003941 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3942 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06003943 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003944 0, /* tp_itemsize */
3945 /* methods */
3946 (destructor)dictview_dealloc, /* tp_dealloc */
3947 0, /* tp_print */
3948 0, /* tp_getattr */
3949 0, /* tp_setattr */
3950 0, /* tp_reserved */
3951 (reprfunc)dictview_repr, /* tp_repr */
3952 &dictviews_as_number, /* tp_as_number */
3953 &dictitems_as_sequence, /* tp_as_sequence */
3954 0, /* tp_as_mapping */
3955 0, /* tp_hash */
3956 0, /* tp_call */
3957 0, /* tp_str */
3958 PyObject_GenericGetAttr, /* tp_getattro */
3959 0, /* tp_setattro */
3960 0, /* tp_as_buffer */
3961 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3962 0, /* tp_doc */
3963 (traverseproc)dictview_traverse, /* tp_traverse */
3964 0, /* tp_clear */
3965 dictview_richcompare, /* tp_richcompare */
3966 0, /* tp_weaklistoffset */
3967 (getiterfunc)dictitems_iter, /* tp_iter */
3968 0, /* tp_iternext */
3969 dictitems_methods, /* tp_methods */
3970 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003971};
3972
3973static PyObject *
3974dictitems_new(PyObject *dict)
3975{
Eric Snow96c6af92015-05-29 22:21:39 -06003976 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003977}
3978
Guido van Rossum3ac67412007-02-10 18:55:06 +00003979/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003980
3981static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003982dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003983{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003984 if (dv->dv_dict == NULL) {
3985 Py_RETURN_NONE;
3986 }
3987 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003988}
3989
Guido van Rossum83825ac2007-02-10 04:54:19 +00003990static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003991 (lenfunc)dictview_len, /* sq_length */
3992 0, /* sq_concat */
3993 0, /* sq_repeat */
3994 0, /* sq_item */
3995 0, /* sq_slice */
3996 0, /* sq_ass_item */
3997 0, /* sq_ass_slice */
3998 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003999};
4000
Guido van Rossumb90c8482007-02-10 01:11:45 +00004001static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004002 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00004003};
4004
4005PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004006 PyVarObject_HEAD_INIT(&PyType_Type, 0)
4007 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004008 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004009 0, /* tp_itemsize */
4010 /* methods */
4011 (destructor)dictview_dealloc, /* tp_dealloc */
4012 0, /* tp_print */
4013 0, /* tp_getattr */
4014 0, /* tp_setattr */
4015 0, /* tp_reserved */
4016 (reprfunc)dictview_repr, /* tp_repr */
4017 0, /* tp_as_number */
4018 &dictvalues_as_sequence, /* tp_as_sequence */
4019 0, /* tp_as_mapping */
4020 0, /* tp_hash */
4021 0, /* tp_call */
4022 0, /* tp_str */
4023 PyObject_GenericGetAttr, /* tp_getattro */
4024 0, /* tp_setattro */
4025 0, /* tp_as_buffer */
4026 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4027 0, /* tp_doc */
4028 (traverseproc)dictview_traverse, /* tp_traverse */
4029 0, /* tp_clear */
4030 0, /* tp_richcompare */
4031 0, /* tp_weaklistoffset */
4032 (getiterfunc)dictvalues_iter, /* tp_iter */
4033 0, /* tp_iternext */
4034 dictvalues_methods, /* tp_methods */
4035 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004036};
4037
4038static PyObject *
4039dictvalues_new(PyObject *dict)
4040{
Eric Snow96c6af92015-05-29 22:21:39 -06004041 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004042}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004043
4044/* Returns NULL if cannot allocate a new PyDictKeysObject,
4045 but does not set an error */
4046PyDictKeysObject *
4047_PyDict_NewKeysForClass(void)
4048{
Victor Stinner742da042016-09-07 17:40:12 -07004049 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004050 if (keys == NULL)
4051 PyErr_Clear();
4052 else
4053 keys->dk_lookup = lookdict_split;
4054 return keys;
4055}
4056
4057#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
4058
4059PyObject *
4060PyObject_GenericGetDict(PyObject *obj, void *context)
4061{
4062 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
4063 if (dictptr == NULL) {
4064 PyErr_SetString(PyExc_AttributeError,
4065 "This object has no __dict__");
4066 return NULL;
4067 }
4068 dict = *dictptr;
4069 if (dict == NULL) {
4070 PyTypeObject *tp = Py_TYPE(obj);
4071 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
4072 DK_INCREF(CACHED_KEYS(tp));
4073 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
4074 }
4075 else {
4076 *dictptr = dict = PyDict_New();
4077 }
4078 }
4079 Py_XINCREF(dict);
4080 return dict;
4081}
4082
4083int
4084_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
Victor Stinner742da042016-09-07 17:40:12 -07004085 PyObject *key, PyObject *value)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004086{
4087 PyObject *dict;
4088 int res;
4089 PyDictKeysObject *cached;
4090
4091 assert(dictptr != NULL);
4092 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
4093 assert(dictptr != NULL);
4094 dict = *dictptr;
4095 if (dict == NULL) {
4096 DK_INCREF(cached);
4097 dict = new_dict_with_shared_keys(cached);
4098 if (dict == NULL)
4099 return -1;
4100 *dictptr = dict;
4101 }
4102 if (value == NULL) {
4103 res = PyDict_DelItem(dict, key);
4104 if (cached != ((PyDictObject *)dict)->ma_keys) {
4105 CACHED_KEYS(tp) = NULL;
4106 DK_DECREF(cached);
4107 }
4108 } else {
4109 res = PyDict_SetItem(dict, key, value);
4110 if (cached != ((PyDictObject *)dict)->ma_keys) {
4111 /* Either update tp->ht_cached_keys or delete it */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004112 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004113 CACHED_KEYS(tp) = make_keys_shared(dict);
Victor Stinner742da042016-09-07 17:40:12 -07004114 }
4115 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004116 CACHED_KEYS(tp) = NULL;
4117 }
4118 DK_DECREF(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004119 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
4120 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004121 }
4122 }
4123 } else {
4124 dict = *dictptr;
4125 if (dict == NULL) {
4126 dict = PyDict_New();
4127 if (dict == NULL)
4128 return -1;
4129 *dictptr = dict;
4130 }
4131 if (value == NULL) {
4132 res = PyDict_DelItem(dict, key);
4133 } else {
4134 res = PyDict_SetItem(dict, key, value);
4135 }
4136 }
4137 return res;
4138}
4139
4140void
4141_PyDictKeys_DecRef(PyDictKeysObject *keys)
4142{
4143 DK_DECREF(keys);
4144}