blob: cd258034df8593f3e0d2e3b344ee14272769e634 [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 Stinner71211e32016-09-08 10:52:46 -0700310 ix = ((char*) &keys->dk_indices[0])[i];
Victor Stinner742da042016-09-07 17:40:12 -0700311 }
312 else if (s <= 0xffff) {
Victor Stinner71211e32016-09-08 10:52:46 -0700313 ix = ((int16_t*)&keys->dk_indices[0])[i];
Victor Stinner742da042016-09-07 17:40:12 -0700314 }
315#if SIZEOF_VOID_P > 4
316 else if (s <= 0xffffffff) {
Victor Stinner71211e32016-09-08 10:52:46 -0700317 ix = ((int32_t*)&keys->dk_indices[0])[i];
Victor Stinner742da042016-09-07 17:40:12 -0700318 }
319#endif
320 else {
Victor Stinner71211e32016-09-08 10:52:46 -0700321 ix = ((Py_ssize_t*)&keys->dk_indices[0])[i];
Victor Stinner742da042016-09-07 17:40:12 -0700322 }
Victor Stinner71211e32016-09-08 10:52:46 -0700323 assert(ix >= DKIX_DUMMY);
324 return ix;
Victor Stinner742da042016-09-07 17:40:12 -0700325}
326
327/* write to indices. */
Benjamin Peterson73222252016-09-08 09:58:47 -0700328static inline void
Victor Stinner742da042016-09-07 17:40:12 -0700329dk_set_index(PyDictKeysObject *keys, Py_ssize_t i, Py_ssize_t ix)
330{
331 Py_ssize_t s = DK_SIZE(keys);
Victor Stinner71211e32016-09-08 10:52:46 -0700332
333 assert(ix >= DKIX_DUMMY);
334
Victor Stinner742da042016-09-07 17:40:12 -0700335 if (s <= 0xff) {
Victor Stinner71211e32016-09-08 10:52:46 -0700336 assert(ix <= 0x7f);
Victor Stinner742da042016-09-07 17:40:12 -0700337 ((char*) &keys->dk_indices[0])[i] = (char)ix;
338 }
339 else if (s <= 0xffff) {
Victor Stinner71211e32016-09-08 10:52:46 -0700340 assert(ix <= 0x7fff);
Victor Stinner742da042016-09-07 17:40:12 -0700341 ((int16_t*) &keys->dk_indices[0])[i] = (int16_t)ix;
342 }
343#if SIZEOF_VOID_P > 4
344 else if (s <= 0xffffffff) {
Victor Stinner71211e32016-09-08 10:52:46 -0700345 assert(ix <= 0x7fffffff);
Victor Stinner742da042016-09-07 17:40:12 -0700346 ((int32_t*) &keys->dk_indices[0])[i] = (int32_t)ix;
347 }
348#endif
349 else {
350 ((Py_ssize_t*) &keys->dk_indices[0])[i] = ix;
351 }
352}
353
354
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200355/* USABLE_FRACTION is the maximum dictionary load.
Victor Stinner742da042016-09-07 17:40:12 -0700356 * Increasing this ratio makes dictionaries more dense resulting in more
357 * collisions. Decreasing it improves sparseness at the expense of spreading
358 * indices over more cache lines and at the cost of total memory consumed.
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200359 *
360 * USABLE_FRACTION must obey the following:
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400361 * (0 < USABLE_FRACTION(n) < n) for all n >= 2
362 *
Victor Stinner742da042016-09-07 17:40:12 -0700363 * USABLE_FRACTION should be quick to calculate.
364 * Fractions around 1/2 to 2/3 seem to work well in practice.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400365 */
Victor Stinner742da042016-09-07 17:40:12 -0700366#define USABLE_FRACTION(n) (((n) << 1)/3)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400367
Victor Stinner742da042016-09-07 17:40:12 -0700368/* ESTIMATE_SIZE is reverse function of USABLE_FRACTION.
369 * This can be used to reserve enough size to insert n entries without
370 * resizing.
371 */
372#define ESTIMATE_SIZE(n) (((n)*3) >> 1)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400373
Victor Stinner742da042016-09-07 17:40:12 -0700374/* Alternative fraction that is otherwise close enough to 2n/3 to make
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400375 * little difference. 8 * 2/3 == 8 * 5/8 == 5. 16 * 2/3 == 16 * 5/8 == 10.
376 * 32 * 2/3 = 21, 32 * 5/8 = 20.
377 * Its advantage is that it is faster to compute on machines with slow division.
378 * #define USABLE_FRACTION(n) (((n) >> 1) + ((n) >> 2) - ((n) >> 3))
Victor Stinner742da042016-09-07 17:40:12 -0700379 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400380
Victor Stinnera9f61a52013-07-16 22:17:26 +0200381/* GROWTH_RATE. Growth rate upon hitting maximum load.
382 * Currently set to used*2 + capacity/2.
383 * This means that dicts double in size when growing without deletions,
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700384 * but have more head room when the number of deletions is on a par with the
385 * number of insertions.
386 * Raising this to used*4 doubles memory consumption depending on the size of
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200387 * the dictionary, but results in half the number of resizes, less effort to
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700388 * resize.
389 * GROWTH_RATE was set to used*4 up to version 3.2.
390 * GROWTH_RATE was set to used*2 in version 3.3.0
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200391 */
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700392#define GROWTH_RATE(d) (((d)->ma_used*2)+((d)->ma_keys->dk_size>>1))
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400393
394#define ENSURE_ALLOWS_DELETIONS(d) \
395 if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \
396 (d)->ma_keys->dk_lookup = lookdict_unicode; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400398
399/* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
400 * (which cannot fail and thus can do no allocation).
401 */
402static PyDictKeysObject empty_keys_struct = {
403 2, /* dk_refcnt 1 for this struct, 1 for dummy_struct */
404 1, /* dk_size */
405 lookdict_split, /* dk_lookup */
406 0, /* dk_usable (immutable) */
Victor Stinner742da042016-09-07 17:40:12 -0700407 0, /* dk_nentries */
408 {DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY,
409 DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY, DKIX_EMPTY}, /* dk_indices */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400410};
411
412static PyObject *empty_values[1] = { NULL };
413
414#define Py_EMPTY_KEYS &empty_keys_struct
415
416static PyDictKeysObject *new_keys_object(Py_ssize_t size)
417{
418 PyDictKeysObject *dk;
Victor Stinner742da042016-09-07 17:40:12 -0700419 Py_ssize_t es, usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400420
Victor Stinner742da042016-09-07 17:40:12 -0700421 assert(size >= PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400422 assert(IS_POWER_OF_2(size));
Victor Stinner742da042016-09-07 17:40:12 -0700423
424 usable = USABLE_FRACTION(size);
425 if (size <= 0xff) {
426 es = 1;
427 }
428 else if (size <= 0xffff) {
429 es = 2;
430 }
431#if SIZEOF_VOID_P > 4
432 else if (size <= 0xffffffff) {
433 es = 4;
434 }
435#endif
436 else {
437 es = sizeof(Py_ssize_t);
438 }
439
440 if (size == PyDict_MINSIZE && numfreekeys > 0) {
441 dk = keys_free_list[--numfreekeys];
442 }
443 else {
Victor Stinner98ee9d52016-09-08 09:33:56 -0700444 dk = PyObject_MALLOC(sizeof(PyDictKeysObject)
445 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
446 + es * size
447 + sizeof(PyDictKeyEntry) * usable);
Victor Stinner742da042016-09-07 17:40:12 -0700448 if (dk == NULL) {
449 PyErr_NoMemory();
450 return NULL;
451 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400452 }
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200453 DK_DEBUG_INCREF dk->dk_refcnt = 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400454 dk->dk_size = size;
Victor Stinner742da042016-09-07 17:40:12 -0700455 dk->dk_usable = usable;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400456 dk->dk_lookup = lookdict_unicode_nodummy;
Victor Stinner742da042016-09-07 17:40:12 -0700457 dk->dk_nentries = 0;
458 memset(&dk->dk_indices[0], 0xff, es * size);
459 memset(DK_ENTRIES(dk), 0, sizeof(PyDictKeyEntry) * usable);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400460 return dk;
461}
462
463static void
464free_keys_object(PyDictKeysObject *keys)
465{
Victor Stinner742da042016-09-07 17:40:12 -0700466 PyDictKeyEntry *entries = DK_ENTRIES(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400467 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -0700468 for (i = 0, n = keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400469 Py_XDECREF(entries[i].me_key);
470 Py_XDECREF(entries[i].me_value);
471 }
Victor Stinner742da042016-09-07 17:40:12 -0700472 if (keys->dk_size == PyDict_MINSIZE && numfreekeys < PyDict_MAXFREELIST) {
473 keys_free_list[numfreekeys++] = keys;
474 return;
475 }
Raymond Hettingerce5179f2016-01-31 08:56:21 -0800476 PyObject_FREE(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400477}
478
479#define new_values(size) PyMem_NEW(PyObject *, size)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400480#define free_values(values) PyMem_FREE(values)
481
482/* Consumes a reference to the keys object */
483static PyObject *
484new_dict(PyDictKeysObject *keys, PyObject **values)
485{
486 PyDictObject *mp;
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200487 assert(keys != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 if (numfree) {
489 mp = free_list[--numfree];
490 assert (mp != NULL);
491 assert (Py_TYPE(mp) == &PyDict_Type);
492 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400494 else {
495 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
496 if (mp == NULL) {
497 DK_DECREF(keys);
498 free_values(values);
499 return NULL;
500 }
501 }
502 mp->ma_keys = keys;
503 mp->ma_values = values;
504 mp->ma_used = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000506}
507
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400508/* Consumes a reference to the keys object */
509static PyObject *
510new_dict_with_shared_keys(PyDictKeysObject *keys)
511{
512 PyObject **values;
513 Py_ssize_t i, size;
514
Victor Stinner742da042016-09-07 17:40:12 -0700515 size = USABLE_FRACTION(DK_SIZE(keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400516 values = new_values(size);
517 if (values == NULL) {
518 DK_DECREF(keys);
519 return PyErr_NoMemory();
520 }
521 for (i = 0; i < size; i++) {
522 values[i] = NULL;
523 }
524 return new_dict(keys, values);
525}
526
527PyObject *
528PyDict_New(void)
529{
Victor Stinner742da042016-09-07 17:40:12 -0700530 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200531 if (keys == NULL)
532 return NULL;
533 return new_dict(keys, NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400534}
535
Victor Stinner742da042016-09-07 17:40:12 -0700536/* Search index of hash table from offset of entry table */
537static Py_ssize_t
538lookdict_index(PyDictKeysObject *k, Py_hash_t hash, Py_ssize_t index)
539{
540 size_t i, perturb;
541 size_t mask = DK_MASK(k);
542 Py_ssize_t ix;
543
544 i = (size_t)hash & mask;
545 ix = dk_get_index(k, i);
546 if (ix == index) {
547 return i;
548 }
549 if (ix == DKIX_EMPTY) {
550 return DKIX_EMPTY;
551 }
552
553 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
554 i = mask & ((i << 2) + i + perturb + 1);
555 ix = dk_get_index(k, i);
556 if (ix == index) {
557 return i;
558 }
559 if (ix == DKIX_EMPTY) {
560 return DKIX_EMPTY;
561 }
562 }
563 assert(0); /* NOT REACHED */
564 return DKIX_ERROR;
565}
566
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000567/*
568The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000569This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000570Open addressing is preferred over chaining since the link overhead for
571chaining would be substantial (100% with typical malloc overhead).
572
Tim Peterseb28ef22001-06-02 05:27:19 +0000573The initial probe index is computed as hash mod the table size. Subsequent
574probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000575
576All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000577
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000578The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000579contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000580Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000581
Victor Stinner742da042016-09-07 17:40:12 -0700582lookdict() is general-purpose, and may return DKIX_ERROR if (and only if) a
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000583comparison raises an exception (this was new in Python 2.5).
Guido van Rossum89d8c602007-09-18 17:26:56 +0000584lookdict_unicode() below is specialized to string keys, comparison of which can
Victor Stinner742da042016-09-07 17:40:12 -0700585never raise an exception; that function can never return DKIX_ERROR.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400586lookdict_unicode_nodummy is further specialized for string keys that cannot be
587the <dummy> value.
Victor Stinner742da042016-09-07 17:40:12 -0700588For both, when the key isn't found a DKIX_EMPTY is returned. hashpos returns
589where the key index should be inserted.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000590*/
Victor Stinner742da042016-09-07 17:40:12 -0700591static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400592lookdict(PyDictObject *mp, PyObject *key,
Victor Stinner742da042016-09-07 17:40:12 -0700593 Py_hash_t hash, PyObject ***value_addr, Py_ssize_t *hashpos)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000594{
Victor Stinner742da042016-09-07 17:40:12 -0700595 size_t i, perturb, mask;
596 Py_ssize_t ix, freeslot;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200597 int cmp;
Victor Stinner742da042016-09-07 17:40:12 -0700598 PyDictKeysObject *dk;
599 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000601
Antoine Pitrou9a234902012-05-13 20:48:01 +0200602top:
Victor Stinner742da042016-09-07 17:40:12 -0700603 dk = mp->ma_keys;
604 mask = DK_MASK(dk);
605 ep0 = DK_ENTRIES(dk);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700607
608 ix = dk_get_index(dk, i);
609 if (ix == DKIX_EMPTY) {
610 if (hashpos != NULL)
611 *hashpos = i;
612 *value_addr = NULL;
613 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400614 }
Victor Stinner742da042016-09-07 17:40:12 -0700615 if (ix == DKIX_DUMMY) {
616 freeslot = i;
617 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 else {
Victor Stinner742da042016-09-07 17:40:12 -0700619 ep = &ep0[ix];
620 if (ep->me_key == key) {
621 *value_addr = &ep->me_value;
622 if (hashpos != NULL)
623 *hashpos = i;
624 return ix;
625 }
626 if (ep->me_key != NULL && ep->me_hash == hash) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 startkey = ep->me_key;
628 Py_INCREF(startkey);
629 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
630 Py_DECREF(startkey);
631 if (cmp < 0)
Victor Stinner742da042016-09-07 17:40:12 -0700632 return DKIX_ERROR;
633 if (dk == mp->ma_keys && ep->me_key == startkey) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400634 if (cmp > 0) {
635 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700636 if (hashpos != NULL)
637 *hashpos = i;
638 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400639 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 }
641 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200642 /* The dict was mutated, restart */
643 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 }
645 }
Victor Stinner742da042016-09-07 17:40:12 -0700646 freeslot = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 }
Tim Peters15d49292001-05-27 07:39:22 +0000648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
Victor Stinner742da042016-09-07 17:40:12 -0700650 i = ((i << 2) + i + perturb + 1) & mask;
651 ix = dk_get_index(dk, i);
652 if (ix == DKIX_EMPTY) {
653 if (hashpos != NULL) {
654 *hashpos = (freeslot == -1) ? (Py_ssize_t)i : freeslot;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400655 }
Victor Stinner742da042016-09-07 17:40:12 -0700656 *value_addr = NULL;
657 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400658 }
Victor Stinner742da042016-09-07 17:40:12 -0700659 if (ix == DKIX_DUMMY) {
660 if (freeslot == -1)
661 freeslot = i;
662 continue;
663 }
664 ep = &ep0[ix];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400665 if (ep->me_key == key) {
Victor Stinner742da042016-09-07 17:40:12 -0700666 if (hashpos != NULL) {
667 *hashpos = i;
668 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400669 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700670 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400671 }
Victor Stinner742da042016-09-07 17:40:12 -0700672 if (ep->me_hash == hash && ep->me_key != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 startkey = ep->me_key;
674 Py_INCREF(startkey);
675 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
676 Py_DECREF(startkey);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400677 if (cmp < 0) {
678 *value_addr = NULL;
Victor Stinner742da042016-09-07 17:40:12 -0700679 return DKIX_ERROR;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400680 }
Victor Stinner742da042016-09-07 17:40:12 -0700681 if (dk == mp->ma_keys && ep->me_key == startkey) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400682 if (cmp > 0) {
Victor Stinner742da042016-09-07 17:40:12 -0700683 if (hashpos != NULL) {
684 *hashpos = i;
685 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400686 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700687 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400688 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 }
690 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200691 /* The dict was mutated, restart */
692 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 }
694 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 }
696 assert(0); /* NOT REACHED */
697 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000698}
699
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400700/* Specialized version for string-only keys */
Victor Stinner742da042016-09-07 17:40:12 -0700701static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400702lookdict_unicode(PyDictObject *mp, PyObject *key,
Victor Stinner742da042016-09-07 17:40:12 -0700703 Py_hash_t hash, PyObject ***value_addr, Py_ssize_t *hashpos)
Fred Drake1bff34a2000-08-31 19:31:38 +0000704{
Victor Stinner742da042016-09-07 17:40:12 -0700705 size_t i, perturb;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200706 size_t mask = DK_MASK(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -0700707 Py_ssize_t ix, freeslot;
708 PyDictKeyEntry *ep, *ep0 = DK_ENTRIES(mp->ma_keys);
Fred Drake1bff34a2000-08-31 19:31:38 +0000709
Victor Stinner742da042016-09-07 17:40:12 -0700710 assert(mp->ma_values == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 /* Make sure this function doesn't have to handle non-unicode keys,
712 including subclasses of str; e.g., one reason to subclass
713 unicodes is to override __eq__, and for speed we don't cater to
714 that here. */
715 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400716 mp->ma_keys->dk_lookup = lookdict;
Victor Stinner742da042016-09-07 17:40:12 -0700717 return lookdict(mp, key, hash, value_addr, hashpos);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 }
Mark Dickinson57e683e2011-09-24 18:18:40 +0100719 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700720 ix = dk_get_index(mp->ma_keys, i);
721 if (ix == DKIX_EMPTY) {
722 if (hashpos != NULL)
723 *hashpos = i;
724 *value_addr = NULL;
725 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400726 }
Victor Stinner742da042016-09-07 17:40:12 -0700727 if (ix == DKIX_DUMMY) {
728 freeslot = i;
729 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 else {
Victor Stinner742da042016-09-07 17:40:12 -0700731 ep = &ep0[ix];
732 /* only split table can be ix != DKIX_DUMMY && me_key == NULL */
733 assert(ep->me_key != NULL);
734 if (ep->me_key == key || (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
735 if (hashpos != NULL)
736 *hashpos = i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400737 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700738 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400739 }
Victor Stinner742da042016-09-07 17:40:12 -0700740 freeslot = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 }
Tim Peters15d49292001-05-27 07:39:22 +0000742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
Victor Stinner742da042016-09-07 17:40:12 -0700744 i = mask & ((i << 2) + i + perturb + 1);
745 ix = dk_get_index(mp->ma_keys, i);
746 if (ix == DKIX_EMPTY) {
747 if (hashpos != NULL) {
748 *hashpos = (freeslot == -1) ? (Py_ssize_t)i : freeslot;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400749 }
Victor Stinner742da042016-09-07 17:40:12 -0700750 *value_addr = NULL;
751 return DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400752 }
Victor Stinner742da042016-09-07 17:40:12 -0700753 if (ix == DKIX_DUMMY) {
754 if (freeslot == -1)
755 freeslot = i;
756 continue;
757 }
758 ep = &ep0[ix];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 if (ep->me_key == key
760 || (ep->me_hash == hash
Victor Stinner742da042016-09-07 17:40:12 -0700761 && ep->me_key != NULL
762 && unicode_eq(ep->me_key, key))) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400763 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700764 if (hashpos != NULL) {
765 *hashpos = i;
766 }
767 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400768 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 }
770 assert(0); /* NOT REACHED */
771 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000772}
773
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400774/* Faster version of lookdict_unicode when it is known that no <dummy> keys
775 * will be present. */
Victor Stinner742da042016-09-07 17:40:12 -0700776static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400777lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
Victor Stinner742da042016-09-07 17:40:12 -0700778 Py_hash_t hash, PyObject ***value_addr,
779 Py_ssize_t *hashpos)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400780{
Victor Stinner742da042016-09-07 17:40:12 -0700781 size_t i, perturb;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200782 size_t mask = DK_MASK(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -0700783 Py_ssize_t ix;
784 PyDictKeyEntry *ep, *ep0 = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400785
Victor Stinner742da042016-09-07 17:40:12 -0700786 assert(mp->ma_values == NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400787 /* Make sure this function doesn't have to handle non-unicode keys,
788 including subclasses of str; e.g., one reason to subclass
789 unicodes is to override __eq__, and for speed we don't cater to
790 that here. */
791 if (!PyUnicode_CheckExact(key)) {
792 mp->ma_keys->dk_lookup = lookdict;
Victor Stinner742da042016-09-07 17:40:12 -0700793 return lookdict(mp, key, hash, value_addr, hashpos);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400794 }
795 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700796 ix = dk_get_index(mp->ma_keys, i);
797 assert (ix != DKIX_DUMMY);
798 if (ix == DKIX_EMPTY) {
799 if (hashpos != NULL)
800 *hashpos = i;
801 *value_addr = NULL;
802 return DKIX_EMPTY;
803 }
804 ep = &ep0[ix];
Victor Stinnerdee6e252016-09-08 11:16:07 -0700805 assert(ep->me_key != NULL);
806 assert(PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700807 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400808 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700809 if (hashpos != NULL)
810 *hashpos = i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400811 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700812 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400813 }
814 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
Victor Stinner742da042016-09-07 17:40:12 -0700815 i = mask & ((i << 2) + i + perturb + 1);
816 ix = dk_get_index(mp->ma_keys, i);
817 assert (ix != DKIX_DUMMY);
818 if (ix == DKIX_EMPTY) {
819 if (hashpos != NULL)
820 *hashpos = i;
821 *value_addr = NULL;
822 return DKIX_EMPTY;
823 }
824 ep = &ep0[ix];
825 assert(ep->me_key != NULL && PyUnicode_CheckExact(ep->me_key));
826 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400827 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700828 if (hashpos != NULL)
829 *hashpos = i;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400830 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700831 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400832 }
833 }
834 assert(0); /* NOT REACHED */
835 return 0;
836}
837
838/* Version of lookdict for split tables.
839 * All split tables and only split tables use this lookup function.
840 * Split tables only contain unicode keys and no dummy keys,
841 * so algorithm is the same as lookdict_unicode_nodummy.
842 */
Victor Stinner742da042016-09-07 17:40:12 -0700843static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400844lookdict_split(PyDictObject *mp, PyObject *key,
Victor Stinner742da042016-09-07 17:40:12 -0700845 Py_hash_t hash, PyObject ***value_addr, Py_ssize_t *hashpos)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400846{
Victor Stinner742da042016-09-07 17:40:12 -0700847 size_t i, perturb;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200848 size_t mask = DK_MASK(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -0700849 Py_ssize_t ix;
850 PyDictKeyEntry *ep, *ep0 = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400851
Victor Stinner742da042016-09-07 17:40:12 -0700852 /* mp must split table */
853 assert(mp->ma_values != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400854 if (!PyUnicode_CheckExact(key)) {
Victor Stinner742da042016-09-07 17:40:12 -0700855 ix = lookdict(mp, key, hash, value_addr, hashpos);
856 if (ix >= 0) {
857 *value_addr = &mp->ma_values[ix];
858 }
859 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400860 }
Victor Stinner742da042016-09-07 17:40:12 -0700861
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400862 i = (size_t)hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700863 ix = dk_get_index(mp->ma_keys, i);
864 if (ix == DKIX_EMPTY) {
865 if (hashpos != NULL)
866 *hashpos = i;
867 *value_addr = NULL;
868 return DKIX_EMPTY;
869 }
870 assert(ix >= 0);
871 ep = &ep0[ix];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400872 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700873 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400874 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700875 if (hashpos != NULL)
876 *hashpos = i;
877 *value_addr = &mp->ma_values[ix];
878 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400879 }
880 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
Victor Stinner742da042016-09-07 17:40:12 -0700881 i = mask & ((i << 2) + i + perturb + 1);
882 ix = dk_get_index(mp->ma_keys, i);
883 if (ix == DKIX_EMPTY) {
884 if (hashpos != NULL)
885 *hashpos = i;
886 *value_addr = NULL;
887 return DKIX_EMPTY;
888 }
889 assert(ix >= 0);
890 ep = &ep0[ix];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400891 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
Victor Stinner742da042016-09-07 17:40:12 -0700892 if (ep->me_key == key ||
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400893 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
Victor Stinner742da042016-09-07 17:40:12 -0700894 if (hashpos != NULL)
895 *hashpos = i;
896 *value_addr = &mp->ma_values[ix];
897 return ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400898 }
899 }
900 assert(0); /* NOT REACHED */
901 return 0;
902}
903
Benjamin Petersonfb886362010-04-24 18:21:17 +0000904int
905_PyDict_HasOnlyStringKeys(PyObject *dict)
906{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 Py_ssize_t pos = 0;
908 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000909 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400911 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 return 1;
913 while (PyDict_Next(dict, &pos, &key, &value))
914 if (!PyUnicode_Check(key))
915 return 0;
916 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000917}
918
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000919#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 do { \
921 if (!_PyObject_GC_IS_TRACKED(mp)) { \
922 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
923 _PyObject_GC_MAY_BE_TRACKED(value)) { \
924 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 } \
926 } \
927 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000928
929void
930_PyDict_MaybeUntrack(PyObject *op)
931{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 PyDictObject *mp;
933 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -0700934 Py_ssize_t i, numentries;
935 PyDictKeyEntry *ep0;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
938 return;
939
940 mp = (PyDictObject *) op;
Victor Stinner742da042016-09-07 17:40:12 -0700941 ep0 = DK_ENTRIES(mp->ma_keys);
942 numentries = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400943 if (_PyDict_HasSplitTable(mp)) {
Victor Stinner742da042016-09-07 17:40:12 -0700944 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400945 if ((value = mp->ma_values[i]) == NULL)
946 continue;
947 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
Victor Stinner742da042016-09-07 17:40:12 -0700948 assert(!_PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400949 return;
950 }
951 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400953 else {
Victor Stinner742da042016-09-07 17:40:12 -0700954 for (i = 0; i < numentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400955 if ((value = ep0[i].me_value) == NULL)
956 continue;
957 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
958 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
959 return;
960 }
961 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000963}
964
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400965/* Internal function to find slot for an item from its hash
966 * when it is known that the key is not present in the dict.
967 */
Victor Stinner742da042016-09-07 17:40:12 -0700968static Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400969find_empty_slot(PyDictObject *mp, PyObject *key, Py_hash_t hash,
Victor Stinner742da042016-09-07 17:40:12 -0700970 PyObject ***value_addr, Py_ssize_t *hashpos)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000971{
Victor Stinner742da042016-09-07 17:40:12 -0700972 size_t i, perturb;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400973 size_t mask = DK_MASK(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -0700974 Py_ssize_t ix;
975 PyDictKeyEntry *ep, *ep0 = DK_ENTRIES(mp->ma_keys);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000976
Victor Stinner742da042016-09-07 17:40:12 -0700977 assert(hashpos != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400978 assert(key != NULL);
979 if (!PyUnicode_CheckExact(key))
980 mp->ma_keys->dk_lookup = lookdict;
981 i = hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -0700982 ix = dk_get_index(mp->ma_keys, i);
983 for (perturb = hash; ix != DKIX_EMPTY; perturb >>= PERTURB_SHIFT) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400984 i = (i << 2) + i + perturb + 1;
Victor Stinner742da042016-09-07 17:40:12 -0700985 ix = dk_get_index(mp->ma_keys, i & mask);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 }
Victor Stinner742da042016-09-07 17:40:12 -0700987 ep = &ep0[mp->ma_keys->dk_nentries];
988 *hashpos = i & mask;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400989 assert(ep->me_value == NULL);
990 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -0700991 *value_addr = &mp->ma_values[ix];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400992 else
993 *value_addr = &ep->me_value;
Victor Stinner742da042016-09-07 17:40:12 -0700994 return ix;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000995}
996
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400997static int
998insertion_resize(PyDictObject *mp)
999{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -07001000 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001001}
Antoine Pitroue965d972012-02-27 00:45:12 +01001002
1003/*
1004Internal routine to insert a new item into the table.
1005Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +01001006Returns -1 if an error occurred, or 0 on success.
1007*/
1008static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001009insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001010{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001011 PyObject *old_value;
1012 PyObject **value_addr;
Victor Stinner742da042016-09-07 17:40:12 -07001013 PyDictKeyEntry *ep, *ep0;
1014 Py_ssize_t hashpos, ix;
Antoine Pitroue965d972012-02-27 00:45:12 +01001015
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001016 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
1017 if (insertion_resize(mp) < 0)
1018 return -1;
1019 }
1020
Victor Stinner742da042016-09-07 17:40:12 -07001021 ix = mp->ma_keys->dk_lookup(mp, key, hash, &value_addr, &hashpos);
1022 if (ix == DKIX_ERROR) {
Antoine Pitroue965d972012-02-27 00:45:12 +01001023 return -1;
1024 }
Victor Stinner742da042016-09-07 17:40:12 -07001025
Antoine Pitroud6967322014-10-18 00:35:00 +02001026 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
Benjamin Petersona6f195e2012-04-30 10:23:40 -04001027 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001028 MAINTAIN_TRACKING(mp, key, value);
Victor Stinner742da042016-09-07 17:40:12 -07001029
1030 /* When insertion order is different from shared key, we can't share
1031 * the key anymore. Convert this instance to combine table.
1032 */
1033 if (_PyDict_HasSplitTable(mp) &&
1034 ((ix >= 0 && *value_addr == NULL && mp->ma_used != ix) ||
1035 (ix == DKIX_EMPTY && mp->ma_used != mp->ma_keys->dk_nentries))) {
1036 if (insertion_resize(mp) < 0) {
1037 Py_DECREF(value);
1038 return -1;
1039 }
1040 find_empty_slot(mp, key, hash, &value_addr, &hashpos);
1041 ix = DKIX_EMPTY;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001042 }
Victor Stinner742da042016-09-07 17:40:12 -07001043
1044 if (ix == DKIX_EMPTY) {
1045 /* Insert into new slot. */
1046 if (mp->ma_keys->dk_usable <= 0) {
1047 /* Need to resize. */
1048 if (insertion_resize(mp) < 0) {
1049 Py_DECREF(value);
1050 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001051 }
Victor Stinner742da042016-09-07 17:40:12 -07001052 find_empty_slot(mp, key, hash, &value_addr, &hashpos);
1053 }
1054 ep0 = DK_ENTRIES(mp->ma_keys);
1055 ep = &ep0[mp->ma_keys->dk_nentries];
1056 dk_set_index(mp->ma_keys, hashpos, mp->ma_keys->dk_nentries);
1057 Py_INCREF(key);
1058 ep->me_key = key;
1059 ep->me_hash = hash;
1060 if (mp->ma_values) {
1061 assert (mp->ma_values[mp->ma_keys->dk_nentries] == NULL);
1062 mp->ma_values[mp->ma_keys->dk_nentries] = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001063 }
1064 else {
Victor Stinner742da042016-09-07 17:40:12 -07001065 ep->me_value = value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001066 }
1067 mp->ma_used++;
Victor Stinner742da042016-09-07 17:40:12 -07001068 mp->ma_keys->dk_usable--;
1069 mp->ma_keys->dk_nentries++;
1070 assert(mp->ma_keys->dk_usable >= 0);
1071 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001072 }
Victor Stinner742da042016-09-07 17:40:12 -07001073
1074 assert(value_addr != NULL);
1075
1076 old_value = *value_addr;
1077 if (old_value != NULL) {
1078 *value_addr = value;
1079 Py_DECREF(old_value); /* which **CAN** re-enter (see issue #22653) */
1080 return 0;
1081 }
1082
1083 /* pending state */
1084 assert(_PyDict_HasSplitTable(mp));
1085 assert(ix == mp->ma_used);
1086 *value_addr = value;
1087 mp->ma_used++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001088 return 0;
Antoine Pitroue965d972012-02-27 00:45:12 +01001089}
1090
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001091/*
1092Internal routine used by dictresize() to insert an item which is
1093known to be absent from the dict. This routine also assumes that
1094the dict contains no deleted entries. Besides the performance benefit,
1095using insertdict() in dictresize() is dangerous (SF bug #1456209).
1096Note that no refcounts are changed by this routine; if needed, the caller
1097is responsible for incref'ing `key` and `value`.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001098Neither mp->ma_used nor k->dk_usable are modified by this routine; the caller
1099must set them correctly
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001100*/
1101static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001102insertdict_clean(PyDictObject *mp, PyObject *key, Py_hash_t hash,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 PyObject *value)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001104{
Victor Stinner742da042016-09-07 17:40:12 -07001105 size_t i, perturb;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001106 PyDictKeysObject *k = mp->ma_keys;
1107 size_t mask = (size_t)DK_SIZE(k)-1;
Victor Stinner742da042016-09-07 17:40:12 -07001108 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001109 PyDictKeyEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001110
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001111 assert(k->dk_lookup != NULL);
1112 assert(value != NULL);
1113 assert(key != NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001114 assert(PyUnicode_CheckExact(key) || k->dk_lookup == lookdict);
1115 i = hash & mask;
Victor Stinner742da042016-09-07 17:40:12 -07001116 for (perturb = hash; dk_get_index(k, i) != DKIX_EMPTY;
1117 perturb >>= PERTURB_SHIFT) {
1118 i = mask & ((i << 2) + i + perturb + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 }
Victor Stinner742da042016-09-07 17:40:12 -07001120 ep = &ep0[k->dk_nentries];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 assert(ep->me_value == NULL);
Victor Stinner742da042016-09-07 17:40:12 -07001122 dk_set_index(k, i, k->dk_nentries);
1123 k->dk_nentries++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 ep->me_key = key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001125 ep->me_hash = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001127}
1128
1129/*
1130Restructure the table by allocating a new table and reinserting all
1131items again. When entries have been deleted, the new table may
1132actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001133If a table is split (its keys and hashes are shared, its values are not),
1134then the values are temporarily copied into the table, it is resized as
1135a combined table, then the me_value slots in the old table are NULLed out.
1136After resizing a table is always combined,
1137but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001138*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001139static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001140dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001141{
Victor Stinner742da042016-09-07 17:40:12 -07001142 Py_ssize_t i, newsize;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001143 PyDictKeysObject *oldkeys;
1144 PyObject **oldvalues;
Victor Stinner742da042016-09-07 17:40:12 -07001145 PyDictKeyEntry *ep0;
Tim Peters91a364d2001-05-19 07:04:38 +00001146
Victor Stinner742da042016-09-07 17:40:12 -07001147 /* Find the smallest table size > minused. */
1148 for (newsize = PyDict_MINSIZE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 newsize <= minused && newsize > 0;
1150 newsize <<= 1)
1151 ;
1152 if (newsize <= 0) {
1153 PyErr_NoMemory();
1154 return -1;
1155 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001156 oldkeys = mp->ma_keys;
1157 oldvalues = mp->ma_values;
1158 /* Allocate a new table. */
1159 mp->ma_keys = new_keys_object(newsize);
1160 if (mp->ma_keys == NULL) {
1161 mp->ma_keys = oldkeys;
1162 return -1;
1163 }
1164 if (oldkeys->dk_lookup == lookdict)
1165 mp->ma_keys->dk_lookup = lookdict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001166 mp->ma_values = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001167 ep0 = DK_ENTRIES(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001168 /* Main loop below assumes we can transfer refcount to new keys
1169 * and that value is stored in me_value.
1170 * Increment ref-counts and copy values here to compensate
1171 * This (resizing a split table) should be relatively rare */
1172 if (oldvalues != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001173 for (i = 0; i < oldkeys->dk_nentries; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001174 if (oldvalues[i] != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001175 Py_INCREF(ep0[i].me_key);
1176 ep0[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 }
1179 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001180 /* Main loop */
Victor Stinner742da042016-09-07 17:40:12 -07001181 for (i = 0; i < oldkeys->dk_nentries; i++) {
1182 PyDictKeyEntry *ep = &ep0[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001183 if (ep->me_value != NULL) {
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001184 insertdict_clean(mp, ep->me_key, ep->me_hash, ep->me_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001187 mp->ma_keys->dk_usable -= mp->ma_used;
1188 if (oldvalues != NULL) {
1189 /* NULL out me_value slot in oldkeys, in case it was shared */
Victor Stinner742da042016-09-07 17:40:12 -07001190 for (i = 0; i < oldkeys->dk_nentries; i++)
1191 ep0[i].me_value = NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001192 DK_DECREF(oldkeys);
Victor Stinner742da042016-09-07 17:40:12 -07001193 if (oldvalues != empty_values) {
1194 free_values(oldvalues);
1195 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001196 }
1197 else {
1198 assert(oldkeys->dk_lookup != lookdict_split);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001199 assert(oldkeys->dk_refcnt == 1);
Raymond Hettingerce5179f2016-01-31 08:56:21 -08001200 DK_DEBUG_DECREF PyObject_FREE(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001201 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001203}
1204
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001205/* Returns NULL if unable to split table.
1206 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001207static PyDictKeysObject *
1208make_keys_shared(PyObject *op)
1209{
1210 Py_ssize_t i;
1211 Py_ssize_t size;
1212 PyDictObject *mp = (PyDictObject *)op;
1213
Benjamin Peterson15ee8212012-04-24 14:44:18 -04001214 if (!PyDict_CheckExact(op))
1215 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001216 if (!_PyDict_HasSplitTable(mp)) {
1217 PyDictKeyEntry *ep0;
1218 PyObject **values;
1219 assert(mp->ma_keys->dk_refcnt == 1);
1220 if (mp->ma_keys->dk_lookup == lookdict) {
1221 return NULL;
1222 }
1223 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
1224 /* Remove dummy keys */
1225 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
1226 return NULL;
1227 }
1228 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
1229 /* Copy values into a new array */
Victor Stinner742da042016-09-07 17:40:12 -07001230 ep0 = DK_ENTRIES(mp->ma_keys);
1231 size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001232 values = new_values(size);
1233 if (values == NULL) {
1234 PyErr_SetString(PyExc_MemoryError,
1235 "Not enough memory to allocate new values array");
1236 return NULL;
1237 }
1238 for (i = 0; i < size; i++) {
1239 values[i] = ep0[i].me_value;
1240 ep0[i].me_value = NULL;
1241 }
1242 mp->ma_keys->dk_lookup = lookdict_split;
1243 mp->ma_values = values;
1244 }
1245 DK_INCREF(mp->ma_keys);
1246 return mp->ma_keys;
1247}
Christian Heimes99170a52007-12-19 02:07:34 +00001248
1249PyObject *
1250_PyDict_NewPresized(Py_ssize_t minused)
1251{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001252 Py_ssize_t newsize;
1253 PyDictKeysObject *new_keys;
Victor Stinner742da042016-09-07 17:40:12 -07001254 for (newsize = PyDict_MINSIZE;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001255 newsize <= minused && newsize > 0;
1256 newsize <<= 1)
1257 ;
1258 new_keys = new_keys_object(newsize);
1259 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001261 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001262}
1263
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001264/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1265 * that may occur (originally dicts supported only string keys, and exceptions
1266 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001267 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001268 * (suppressed) occurred while computing the key's hash, or that some error
1269 * (suppressed) occurred when comparing keys in the dict's internal probe
1270 * sequence. A nasty example of the latter is when a Python-coded comparison
1271 * function hits a stack-depth error, which can cause this to return NULL
1272 * even if the key is present.
1273 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001274PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001275PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001276{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001277 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07001278 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 PyDictObject *mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 PyThreadState *tstate;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001281 PyObject **value_addr;
1282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 if (!PyDict_Check(op))
1284 return NULL;
1285 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001286 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 {
1288 hash = PyObject_Hash(key);
1289 if (hash == -1) {
1290 PyErr_Clear();
1291 return NULL;
1292 }
1293 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 /* We can arrive here with a NULL tstate during initialization: try
1296 running "python -Wi" for an example related to string interning.
1297 Let's just hope that no exception occurs then... This must be
1298 _PyThreadState_Current and not PyThreadState_GET() because in debug
1299 mode, the latter complains if tstate is NULL. */
Victor Stinnerbfd316e2016-01-20 11:12:38 +01001300 tstate = _PyThreadState_UncheckedGet();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 if (tstate != NULL && tstate->curexc_type != NULL) {
1302 /* preserve the existing exception */
1303 PyObject *err_type, *err_value, *err_tb;
1304 PyErr_Fetch(&err_type, &err_value, &err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001305 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 /* ignore errors */
1307 PyErr_Restore(err_type, err_value, err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001308 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 return NULL;
1310 }
1311 else {
Victor Stinner742da042016-09-07 17:40:12 -07001312 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
1313 if (ix < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 PyErr_Clear();
1315 return NULL;
1316 }
1317 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001318 return *value_addr;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001319}
1320
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001321PyObject *
1322_PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1323{
Victor Stinner742da042016-09-07 17:40:12 -07001324 Py_ssize_t ix;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001325 PyDictObject *mp = (PyDictObject *)op;
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001326 PyThreadState *tstate;
1327 PyObject **value_addr;
1328
1329 if (!PyDict_Check(op))
1330 return NULL;
1331
1332 /* We can arrive here with a NULL tstate during initialization: try
1333 running "python -Wi" for an example related to string interning.
1334 Let's just hope that no exception occurs then... This must be
1335 _PyThreadState_Current and not PyThreadState_GET() because in debug
1336 mode, the latter complains if tstate is NULL. */
Victor Stinnerbfd316e2016-01-20 11:12:38 +01001337 tstate = _PyThreadState_UncheckedGet();
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001338 if (tstate != NULL && tstate->curexc_type != NULL) {
1339 /* preserve the existing exception */
1340 PyObject *err_type, *err_value, *err_tb;
1341 PyErr_Fetch(&err_type, &err_value, &err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001342 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001343 /* ignore errors */
1344 PyErr_Restore(err_type, err_value, err_tb);
Victor Stinner742da042016-09-07 17:40:12 -07001345 if (ix == DKIX_EMPTY)
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001346 return NULL;
1347 }
1348 else {
Victor Stinner742da042016-09-07 17:40:12 -07001349 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
1350 if (ix == DKIX_EMPTY) {
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001351 PyErr_Clear();
1352 return NULL;
1353 }
1354 }
1355 return *value_addr;
1356}
1357
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001358/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1359 This returns NULL *with* an exception set if an exception occurred.
1360 It returns NULL *without* an exception set if the key wasn't present.
1361*/
1362PyObject *
1363PyDict_GetItemWithError(PyObject *op, PyObject *key)
1364{
Victor Stinner742da042016-09-07 17:40:12 -07001365 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001366 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 PyDictObject*mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001368 PyObject **value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 if (!PyDict_Check(op)) {
1371 PyErr_BadInternalCall();
1372 return NULL;
1373 }
1374 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001375 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 {
1377 hash = PyObject_Hash(key);
1378 if (hash == -1) {
1379 return NULL;
1380 }
1381 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001382
Victor Stinner742da042016-09-07 17:40:12 -07001383 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
1384 if (ix < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001386 return *value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001387}
1388
Brett Cannonfd074152012-04-14 14:10:13 -04001389PyObject *
1390_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1391{
1392 PyObject *kv;
1393 kv = _PyUnicode_FromId(key); /* borrowed */
1394 if (kv == NULL)
1395 return NULL;
1396 return PyDict_GetItemWithError(dp, kv);
1397}
1398
Victor Stinnerb4efc962015-11-20 09:24:02 +01001399/* Fast version of global value lookup (LOAD_GLOBAL).
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001400 * Lookup in globals, then builtins.
Victor Stinnerb4efc962015-11-20 09:24:02 +01001401 *
1402 * Raise an exception and return NULL if an error occurred (ex: computing the
1403 * key hash failed, key comparison failed, ...). Return NULL if the key doesn't
1404 * exist. Return the value if the key exists.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001405 */
1406PyObject *
1407_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001408{
Victor Stinner742da042016-09-07 17:40:12 -07001409 Py_ssize_t ix;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001410 Py_hash_t hash;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001411 PyObject **value_addr;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001412
1413 if (!PyUnicode_CheckExact(key) ||
1414 (hash = ((PyASCIIObject *) key)->hash) == -1)
1415 {
1416 hash = PyObject_Hash(key);
1417 if (hash == -1)
1418 return NULL;
Antoine Pitroue965d972012-02-27 00:45:12 +01001419 }
Victor Stinnerb4efc962015-11-20 09:24:02 +01001420
1421 /* namespace 1: globals */
Victor Stinner742da042016-09-07 17:40:12 -07001422 ix = globals->ma_keys->dk_lookup(globals, key, hash, &value_addr, NULL);
1423 if (ix == DKIX_ERROR)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001424 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001425 if (ix != DKIX_EMPTY && *value_addr != NULL)
1426 return *value_addr;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001427
1428 /* namespace 2: builtins */
Victor Stinner742da042016-09-07 17:40:12 -07001429 ix = builtins->ma_keys->dk_lookup(builtins, key, hash, &value_addr, NULL);
1430 if (ix < 0)
Victor Stinnerb4efc962015-11-20 09:24:02 +01001431 return NULL;
1432 return *value_addr;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001433}
1434
Antoine Pitroue965d972012-02-27 00:45:12 +01001435/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1436 * dictionary if it's merely replacing the value for an existing key.
1437 * This means that it's safe to loop over a dictionary with PyDict_Next()
1438 * and occasionally replace a value -- but you can't insert new keys or
1439 * remove them.
1440 */
1441int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001442PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001443{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001444 PyDictObject *mp;
1445 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001446 if (!PyDict_Check(op)) {
1447 PyErr_BadInternalCall();
1448 return -1;
1449 }
1450 assert(key);
1451 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001452 mp = (PyDictObject *)op;
1453 if (!PyUnicode_CheckExact(key) ||
1454 (hash = ((PyASCIIObject *) key)->hash) == -1)
1455 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001456 hash = PyObject_Hash(key);
1457 if (hash == -1)
1458 return -1;
1459 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001460
1461 /* insertdict() handles any resizing that might be necessary */
1462 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001463}
1464
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001465int
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001466_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1467 Py_hash_t hash)
1468{
1469 PyDictObject *mp;
1470
1471 if (!PyDict_Check(op)) {
1472 PyErr_BadInternalCall();
1473 return -1;
1474 }
1475 assert(key);
1476 assert(value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001477 assert(hash != -1);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001478 mp = (PyDictObject *)op;
1479
1480 /* insertdict() handles any resizing that might be necessary */
1481 return insertdict(mp, key, hash, value);
1482}
1483
1484int
Tim Peters1f5871e2000-07-04 17:44:48 +00001485PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001486{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001487 Py_hash_t hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +00001488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 assert(key);
1490 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001491 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 hash = PyObject_Hash(key);
1493 if (hash == -1)
1494 return -1;
1495 }
Victor Stinner742da042016-09-07 17:40:12 -07001496
1497 return _PyDict_DelItem_KnownHash(op, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001498}
1499
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001500int
1501_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1502{
Victor Stinner742da042016-09-07 17:40:12 -07001503 Py_ssize_t hashpos, ix;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001504 PyDictObject *mp;
1505 PyDictKeyEntry *ep;
1506 PyObject *old_key, *old_value;
1507 PyObject **value_addr;
1508
1509 if (!PyDict_Check(op)) {
1510 PyErr_BadInternalCall();
1511 return -1;
1512 }
1513 assert(key);
1514 assert(hash != -1);
1515 mp = (PyDictObject *)op;
Victor Stinner742da042016-09-07 17:40:12 -07001516 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
1517 if (ix == DKIX_ERROR)
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001518 return -1;
Victor Stinner742da042016-09-07 17:40:12 -07001519 if (ix == DKIX_EMPTY || *value_addr == NULL) {
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001520 _PyErr_SetKeyError(key);
1521 return -1;
1522 }
Victor Stinner742da042016-09-07 17:40:12 -07001523 assert(dk_get_index(mp->ma_keys, hashpos) == ix);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001524 old_value = *value_addr;
1525 *value_addr = NULL;
1526 mp->ma_used--;
Victor Stinner742da042016-09-07 17:40:12 -07001527 if (_PyDict_HasSplitTable(mp)) {
1528 mp->ma_keys->dk_usable = 0;
1529 }
1530 else {
1531 ep = &DK_ENTRIES(mp->ma_keys)[ix];
1532 dk_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001533 ENSURE_ALLOWS_DELETIONS(mp);
1534 old_key = ep->me_key;
Victor Stinner742da042016-09-07 17:40:12 -07001535 ep->me_key = NULL;
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001536 Py_DECREF(old_key);
1537 }
1538 Py_DECREF(old_value);
1539 return 0;
1540}
1541
Guido van Rossum25831651993-05-19 14:50:45 +00001542void
Tim Peters1f5871e2000-07-04 17:44:48 +00001543PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001544{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001545 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001546 PyDictKeysObject *oldkeys;
1547 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 if (!PyDict_Check(op))
1551 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001552 mp = ((PyDictObject *)op);
1553 oldkeys = mp->ma_keys;
1554 oldvalues = mp->ma_values;
1555 if (oldvalues == empty_values)
1556 return;
1557 /* Empty the dict... */
1558 DK_INCREF(Py_EMPTY_KEYS);
1559 mp->ma_keys = Py_EMPTY_KEYS;
1560 mp->ma_values = empty_values;
1561 mp->ma_used = 0;
1562 /* ...then clear the keys and values */
1563 if (oldvalues != NULL) {
Victor Stinner742da042016-09-07 17:40:12 -07001564 n = oldkeys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001565 for (i = 0; i < n; i++)
1566 Py_CLEAR(oldvalues[i]);
1567 free_values(oldvalues);
1568 DK_DECREF(oldkeys);
1569 }
1570 else {
1571 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001572 DK_DECREF(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001573 }
1574}
1575
1576/* Returns -1 if no more items (or op is not a dict),
1577 * index of item otherwise. Stores value in pvalue
1578 */
Benjamin Peterson73222252016-09-08 09:58:47 -07001579static inline Py_ssize_t
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001580dict_next(PyObject *op, Py_ssize_t i, PyObject **pvalue)
1581{
Victor Stinner742da042016-09-07 17:40:12 -07001582 Py_ssize_t n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001583 PyDictObject *mp;
Victor Stinner742da042016-09-07 17:40:12 -07001584 PyObject **value_ptr = NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001585
1586 if (!PyDict_Check(op))
1587 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001589 if (i < 0)
1590 return -1;
Victor Stinner742da042016-09-07 17:40:12 -07001591
1592 n = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001593 if (mp->ma_values) {
Victor Stinner742da042016-09-07 17:40:12 -07001594 for (; i < n; i++) {
1595 value_ptr = &mp->ma_values[i];
1596 if (*value_ptr != NULL)
1597 break;
1598 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001600 else {
Victor Stinner742da042016-09-07 17:40:12 -07001601 PyDictKeyEntry *ep0 = DK_ENTRIES(mp->ma_keys);
1602 for (; i < n; i++) {
1603 value_ptr = &ep0[i].me_value;
1604 if (*value_ptr != NULL)
1605 break;
1606 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 }
Victor Stinner742da042016-09-07 17:40:12 -07001608 if (i >= n)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001609 return -1;
1610 if (pvalue)
1611 *pvalue = *value_ptr;
1612 return i;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001613}
1614
Tim Peters080c88b2003-02-15 03:01:11 +00001615/*
1616 * Iterate over a dict. Use like so:
1617 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001618 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001619 * PyObject *key, *value;
1620 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001621 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +00001622 * Refer to borrowed references in key and value.
1623 * }
1624 *
1625 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001626 * mutates the dict. One exception: it is safe if the loop merely changes
1627 * the values associated with the keys (but doesn't insert new keys or
1628 * delete keys), via PyDict_SetItem().
1629 */
Guido van Rossum25831651993-05-19 14:50:45 +00001630int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001631PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001632{
Victor Stinner742da042016-09-07 17:40:12 -07001633 PyDictObject *mp = (PyDictObject*)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001634 Py_ssize_t i = dict_next(op, *ppos, pvalue);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001635 if (i < 0)
1636 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001637 mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 *ppos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 if (pkey)
Victor Stinner742da042016-09-07 17:40:12 -07001640 *pkey = DK_ENTRIES(mp->ma_keys)[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001642}
1643
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001644/* Internal version of PyDict_Next that returns a hash value in addition
1645 * to the key and value.
1646 */
Thomas Wouterscf297e42007-02-23 15:07:44 +00001647int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001648_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1649 PyObject **pvalue, Py_hash_t *phash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00001650{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001651 PyDictObject *mp;
Victor Stinner742da042016-09-07 17:40:12 -07001652 PyDictKeyEntry *ep0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001653 Py_ssize_t i = dict_next(op, *ppos, pvalue);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 if (i < 0)
1655 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001656 mp = (PyDictObject *)op;
Victor Stinner742da042016-09-07 17:40:12 -07001657 ep0 = DK_ENTRIES(mp->ma_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 *ppos = i+1;
Victor Stinner742da042016-09-07 17:40:12 -07001659 *phash = ep0[i].me_hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 if (pkey)
Victor Stinner742da042016-09-07 17:40:12 -07001661 *pkey = ep0[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001662 return 1;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001663}
1664
Eric Snow96c6af92015-05-29 22:21:39 -06001665/* Internal version of dict.pop(). */
1666PyObject *
1667_PyDict_Pop(PyDictObject *mp, PyObject *key, PyObject *deflt)
1668{
1669 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07001670 Py_ssize_t ix, hashpos;
Eric Snow96c6af92015-05-29 22:21:39 -06001671 PyObject *old_value, *old_key;
1672 PyDictKeyEntry *ep;
1673 PyObject **value_addr;
1674
1675 if (mp->ma_used == 0) {
1676 if (deflt) {
1677 Py_INCREF(deflt);
1678 return deflt;
1679 }
1680 _PyErr_SetKeyError(key);
1681 return NULL;
1682 }
1683 if (!PyUnicode_CheckExact(key) ||
1684 (hash = ((PyASCIIObject *) key)->hash) == -1) {
1685 hash = PyObject_Hash(key);
1686 if (hash == -1)
1687 return NULL;
1688 }
Victor Stinner742da042016-09-07 17:40:12 -07001689 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
1690 if (ix == DKIX_ERROR)
Eric Snow96c6af92015-05-29 22:21:39 -06001691 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001692 if (ix == DKIX_EMPTY) {
Eric Snow96c6af92015-05-29 22:21:39 -06001693 if (deflt) {
1694 Py_INCREF(deflt);
1695 return deflt;
1696 }
1697 _PyErr_SetKeyError(key);
1698 return NULL;
1699 }
Victor Stinner742da042016-09-07 17:40:12 -07001700 old_value = *value_addr;
Eric Snow96c6af92015-05-29 22:21:39 -06001701 *value_addr = NULL;
1702 mp->ma_used--;
1703 if (!_PyDict_HasSplitTable(mp)) {
Victor Stinner742da042016-09-07 17:40:12 -07001704 dk_set_index(mp->ma_keys, hashpos, DKIX_DUMMY);
1705 ep = &DK_ENTRIES(mp->ma_keys)[ix];
Eric Snow96c6af92015-05-29 22:21:39 -06001706 ENSURE_ALLOWS_DELETIONS(mp);
1707 old_key = ep->me_key;
Victor Stinner742da042016-09-07 17:40:12 -07001708 ep->me_key = NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06001709 Py_DECREF(old_key);
1710 }
1711 return old_value;
1712}
1713
1714/* Internal version of dict.from_keys(). It is subclass-friendly. */
1715PyObject *
1716_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1717{
1718 PyObject *it; /* iter(iterable) */
1719 PyObject *key;
1720 PyObject *d;
1721 int status;
1722
1723 d = PyObject_CallObject(cls, NULL);
1724 if (d == NULL)
1725 return NULL;
1726
1727 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
1728 if (PyDict_CheckExact(iterable)) {
1729 PyDictObject *mp = (PyDictObject *)d;
1730 PyObject *oldvalue;
1731 Py_ssize_t pos = 0;
1732 PyObject *key;
1733 Py_hash_t hash;
1734
Victor Stinner742da042016-09-07 17:40:12 -07001735 if (dictresize(mp, ESTIMATE_SIZE(Py_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001736 Py_DECREF(d);
1737 return NULL;
1738 }
1739
1740 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
1741 if (insertdict(mp, key, hash, value)) {
1742 Py_DECREF(d);
1743 return NULL;
1744 }
1745 }
1746 return d;
1747 }
1748 if (PyAnySet_CheckExact(iterable)) {
1749 PyDictObject *mp = (PyDictObject *)d;
1750 Py_ssize_t pos = 0;
1751 PyObject *key;
1752 Py_hash_t hash;
1753
Victor Stinner742da042016-09-07 17:40:12 -07001754 if (dictresize(mp, ESTIMATE_SIZE(PySet_GET_SIZE(iterable)))) {
Eric Snow96c6af92015-05-29 22:21:39 -06001755 Py_DECREF(d);
1756 return NULL;
1757 }
1758
1759 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
1760 if (insertdict(mp, key, hash, value)) {
1761 Py_DECREF(d);
1762 return NULL;
1763 }
1764 }
1765 return d;
1766 }
1767 }
1768
1769 it = PyObject_GetIter(iterable);
1770 if (it == NULL){
1771 Py_DECREF(d);
1772 return NULL;
1773 }
1774
1775 if (PyDict_CheckExact(d)) {
1776 while ((key = PyIter_Next(it)) != NULL) {
1777 status = PyDict_SetItem(d, key, value);
1778 Py_DECREF(key);
1779 if (status < 0)
1780 goto Fail;
1781 }
1782 } else {
1783 while ((key = PyIter_Next(it)) != NULL) {
1784 status = PyObject_SetItem(d, key, value);
1785 Py_DECREF(key);
1786 if (status < 0)
1787 goto Fail;
1788 }
1789 }
1790
1791 if (PyErr_Occurred())
1792 goto Fail;
1793 Py_DECREF(it);
1794 return d;
1795
1796Fail:
1797 Py_DECREF(it);
1798 Py_DECREF(d);
1799 return NULL;
1800}
1801
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001802/* Methods */
1803
1804static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001805dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001806{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001807 PyObject **values = mp->ma_values;
1808 PyDictKeysObject *keys = mp->ma_keys;
1809 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001810 PyObject_GC_UnTrack(mp);
1811 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001812 if (values != NULL) {
1813 if (values != empty_values) {
Victor Stinner742da042016-09-07 17:40:12 -07001814 for (i = 0, n = mp->ma_keys->dk_nentries; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001815 Py_XDECREF(values[i]);
1816 }
1817 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001819 DK_DECREF(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001820 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02001821 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001822 assert(keys->dk_refcnt == 1);
1823 DK_DECREF(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001824 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1826 free_list[numfree++] = mp;
1827 else
1828 Py_TYPE(mp)->tp_free((PyObject *)mp);
1829 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001830}
1831
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001832
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001833static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001834dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001835{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001837 PyObject *key = NULL, *value = NULL;
1838 _PyUnicodeWriter writer;
1839 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00001840
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001841 i = Py_ReprEnter((PyObject *)mp);
1842 if (i != 0) {
1843 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1844 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001846 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001847 Py_ReprLeave((PyObject *)mp);
1848 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849 }
Tim Petersa7259592001-06-16 05:11:17 +00001850
Victor Stinnerf91929b2013-11-19 13:07:38 +01001851 _PyUnicodeWriter_Init(&writer);
1852 writer.overallocate = 1;
1853 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
1854 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00001855
Victor Stinnerf91929b2013-11-19 13:07:38 +01001856 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
1857 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 /* Do repr() on each key+value pair, and insert ": " between them.
1860 Note that repr may mutate the dict. */
1861 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001862 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001864 PyObject *s;
1865 int res;
1866
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001867 /* Prevent repr from deleting key or value during key format. */
1868 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02001870
Victor Stinnerf91929b2013-11-19 13:07:38 +01001871 if (!first) {
1872 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
1873 goto error;
1874 }
1875 first = 0;
1876
1877 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01001879 goto error;
1880 res = _PyUnicodeWriter_WriteStr(&writer, s);
1881 Py_DECREF(s);
1882 if (res < 0)
1883 goto error;
1884
1885 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
1886 goto error;
1887
1888 s = PyObject_Repr(value);
1889 if (s == NULL)
1890 goto error;
1891 res = _PyUnicodeWriter_WriteStr(&writer, s);
1892 Py_DECREF(s);
1893 if (res < 0)
1894 goto error;
1895
1896 Py_CLEAR(key);
1897 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 }
Tim Petersa7259592001-06-16 05:11:17 +00001899
Victor Stinnerf91929b2013-11-19 13:07:38 +01001900 writer.overallocate = 0;
1901 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
1902 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01001905
1906 return _PyUnicodeWriter_Finish(&writer);
1907
1908error:
1909 Py_ReprLeave((PyObject *)mp);
1910 _PyUnicodeWriter_Dealloc(&writer);
1911 Py_XDECREF(key);
1912 Py_XDECREF(value);
1913 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001914}
1915
Martin v. Löwis18e16552006-02-15 17:27:45 +00001916static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001917dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001918{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001920}
1921
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001922static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001923dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001924{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001925 PyObject *v;
Victor Stinner742da042016-09-07 17:40:12 -07001926 Py_ssize_t ix;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001927 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001928 PyObject **value_addr;
1929
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001931 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 hash = PyObject_Hash(key);
1933 if (hash == -1)
1934 return NULL;
1935 }
Victor Stinner742da042016-09-07 17:40:12 -07001936 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
1937 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07001939 if (ix == DKIX_EMPTY || *value_addr == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 if (!PyDict_CheckExact(mp)) {
1941 /* Look up __missing__ method if we're a subclass. */
1942 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05001943 _Py_IDENTIFIER(__missing__);
1944 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945 if (missing != NULL) {
1946 res = PyObject_CallFunctionObjArgs(missing,
1947 key, NULL);
1948 Py_DECREF(missing);
1949 return res;
1950 }
1951 else if (PyErr_Occurred())
1952 return NULL;
1953 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07001954 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 return NULL;
1956 }
Victor Stinner742da042016-09-07 17:40:12 -07001957 v = *value_addr;
1958 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001960}
1961
1962static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001963dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001964{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001965 if (w == NULL)
1966 return PyDict_DelItem((PyObject *)mp, v);
1967 else
1968 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001969}
1970
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001971static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 (lenfunc)dict_length, /*mp_length*/
1973 (binaryfunc)dict_subscript, /*mp_subscript*/
1974 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001975};
1976
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001977static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001978dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001979{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001980 PyObject *v;
1981 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001982 PyDictKeyEntry *ep;
1983 Py_ssize_t size, n, offset;
1984 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001985
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001986 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987 n = mp->ma_used;
1988 v = PyList_New(n);
1989 if (v == NULL)
1990 return NULL;
1991 if (n != mp->ma_used) {
1992 /* Durnit. The allocations caused the dict to resize.
1993 * Just start over, this shouldn't normally happen.
1994 */
1995 Py_DECREF(v);
1996 goto again;
1997 }
Victor Stinner742da042016-09-07 17:40:12 -07001998 ep = DK_ENTRIES(mp->ma_keys);
1999 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002000 if (mp->ma_values) {
2001 value_ptr = mp->ma_values;
2002 offset = sizeof(PyObject *);
2003 }
2004 else {
2005 value_ptr = &ep[0].me_value;
2006 offset = sizeof(PyDictKeyEntry);
2007 }
2008 for (i = 0, j = 0; i < size; i++) {
2009 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002010 PyObject *key = ep[i].me_key;
2011 Py_INCREF(key);
2012 PyList_SET_ITEM(v, j, key);
2013 j++;
2014 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002015 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016 }
2017 assert(j == n);
2018 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002019}
2020
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002021static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002022dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002023{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002024 PyObject *v;
2025 Py_ssize_t i, j;
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002026 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002027 Py_ssize_t size, n, offset;
2028 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002029
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002030 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002031 n = mp->ma_used;
2032 v = PyList_New(n);
2033 if (v == NULL)
2034 return NULL;
2035 if (n != mp->ma_used) {
2036 /* Durnit. The allocations caused the dict to resize.
2037 * Just start over, this shouldn't normally happen.
2038 */
2039 Py_DECREF(v);
2040 goto again;
2041 }
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002042 ep = DK_ENTRIES(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002043 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002044 if (mp->ma_values) {
2045 value_ptr = mp->ma_values;
2046 offset = sizeof(PyObject *);
2047 }
2048 else {
Benjamin Petersonf0acae22016-09-08 09:50:08 -07002049 value_ptr = &ep[0].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002050 offset = sizeof(PyDictKeyEntry);
2051 }
2052 for (i = 0, j = 0; i < size; i++) {
2053 PyObject *value = *value_ptr;
2054 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2055 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002056 Py_INCREF(value);
2057 PyList_SET_ITEM(v, j, value);
2058 j++;
2059 }
2060 }
2061 assert(j == n);
2062 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002063}
2064
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002065static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002066dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002067{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002068 PyObject *v;
2069 Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002070 Py_ssize_t size, offset;
2071 PyObject *item, *key;
2072 PyDictKeyEntry *ep;
2073 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002075 /* Preallocate the list of tuples, to avoid allocations during
2076 * the loop over the items, which could trigger GC, which
2077 * could resize the dict. :-(
2078 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002079 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002080 n = mp->ma_used;
2081 v = PyList_New(n);
2082 if (v == NULL)
2083 return NULL;
2084 for (i = 0; i < n; i++) {
2085 item = PyTuple_New(2);
2086 if (item == NULL) {
2087 Py_DECREF(v);
2088 return NULL;
2089 }
2090 PyList_SET_ITEM(v, i, item);
2091 }
2092 if (n != mp->ma_used) {
2093 /* Durnit. The allocations caused the dict to resize.
2094 * Just start over, this shouldn't normally happen.
2095 */
2096 Py_DECREF(v);
2097 goto again;
2098 }
2099 /* Nothing we do below makes any function calls. */
Victor Stinner742da042016-09-07 17:40:12 -07002100 ep = DK_ENTRIES(mp->ma_keys);
2101 size = mp->ma_keys->dk_nentries;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002102 if (mp->ma_values) {
2103 value_ptr = mp->ma_values;
2104 offset = sizeof(PyObject *);
2105 }
2106 else {
2107 value_ptr = &ep[0].me_value;
2108 offset = sizeof(PyDictKeyEntry);
2109 }
2110 for (i = 0, j = 0; i < size; i++) {
2111 PyObject *value = *value_ptr;
2112 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
2113 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114 key = ep[i].me_key;
2115 item = PyList_GET_ITEM(v, j);
2116 Py_INCREF(key);
2117 PyTuple_SET_ITEM(item, 0, key);
2118 Py_INCREF(value);
2119 PyTuple_SET_ITEM(item, 1, value);
2120 j++;
2121 }
2122 }
2123 assert(j == n);
2124 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00002125}
2126
Larry Hastings5c661892014-01-24 06:17:25 -08002127/*[clinic input]
2128@classmethod
2129dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08002130 iterable: object
2131 value: object=None
2132 /
2133
2134Returns a new dict with keys from iterable and values equal to value.
2135[clinic start generated code]*/
2136
Larry Hastings5c661892014-01-24 06:17:25 -08002137static PyObject *
2138dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002139/*[clinic end generated code: output=8fb98e4b10384999 input=b85a667f9bf4669d]*/
Larry Hastings5c661892014-01-24 06:17:25 -08002140{
Eric Snow96c6af92015-05-29 22:21:39 -06002141 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002142}
2143
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002144static int
Victor Stinner742da042016-09-07 17:40:12 -07002145dict_update_common(PyObject *self, PyObject *args, PyObject *kwds,
2146 const char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002147{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002148 PyObject *arg = NULL;
2149 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002151 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
2152 result = -1;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002155 _Py_IDENTIFIER(keys);
2156 if (_PyObject_HasAttrId(arg, &PyId_keys))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002157 result = PyDict_Merge(self, arg, 1);
2158 else
2159 result = PyDict_MergeFromSeq2(self, arg, 1);
2160 }
2161 if (result == 0 && kwds != NULL) {
2162 if (PyArg_ValidateKeywordArguments(kwds))
2163 result = PyDict_Merge(self, kwds, 1);
2164 else
2165 result = -1;
2166 }
2167 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002168}
2169
2170static PyObject *
2171dict_update(PyObject *self, PyObject *args, PyObject *kwds)
2172{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002173 if (dict_update_common(self, args, kwds, "update") != -1)
2174 Py_RETURN_NONE;
2175 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002176}
2177
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002178/* Update unconditionally replaces existing items.
2179 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00002180 otherwise it leaves existing items unchanged.
2181
2182 PyDict_{Update,Merge} update/merge from a mapping object.
2183
Tim Petersf582b822001-12-11 18:51:08 +00002184 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00002185 producing iterable objects of length 2.
2186*/
2187
Tim Petersf582b822001-12-11 18:51:08 +00002188int
Tim Peters1fc240e2001-10-26 05:06:50 +00002189PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
2190{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 PyObject *it; /* iter(seq2) */
2192 Py_ssize_t i; /* index into seq2 of current element */
2193 PyObject *item; /* seq2[i] */
2194 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00002195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002196 assert(d != NULL);
2197 assert(PyDict_Check(d));
2198 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00002199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002200 it = PyObject_GetIter(seq2);
2201 if (it == NULL)
2202 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002204 for (i = 0; ; ++i) {
2205 PyObject *key, *value;
2206 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00002207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002208 fast = NULL;
2209 item = PyIter_Next(it);
2210 if (item == NULL) {
2211 if (PyErr_Occurred())
2212 goto Fail;
2213 break;
2214 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002216 /* Convert item to sequence, and verify length 2. */
2217 fast = PySequence_Fast(item, "");
2218 if (fast == NULL) {
2219 if (PyErr_ExceptionMatches(PyExc_TypeError))
2220 PyErr_Format(PyExc_TypeError,
2221 "cannot convert dictionary update "
2222 "sequence element #%zd to a sequence",
2223 i);
2224 goto Fail;
2225 }
2226 n = PySequence_Fast_GET_SIZE(fast);
2227 if (n != 2) {
2228 PyErr_Format(PyExc_ValueError,
2229 "dictionary update sequence element #%zd "
2230 "has length %zd; 2 is required",
2231 i, n);
2232 goto Fail;
2233 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002235 /* Update/merge with this (key, value) pair. */
2236 key = PySequence_Fast_GET_ITEM(fast, 0);
2237 value = PySequence_Fast_GET_ITEM(fast, 1);
2238 if (override || PyDict_GetItem(d, key) == NULL) {
2239 int status = PyDict_SetItem(d, key, value);
2240 if (status < 0)
2241 goto Fail;
2242 }
2243 Py_DECREF(fast);
2244 Py_DECREF(item);
2245 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002247 i = 0;
2248 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002249Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002250 Py_XDECREF(item);
2251 Py_XDECREF(fast);
2252 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002253Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002254 Py_DECREF(it);
2255 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002256}
2257
Tim Peters6d6c1a32001-08-02 04:15:00 +00002258int
2259PyDict_Update(PyObject *a, PyObject *b)
2260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 return PyDict_Merge(a, b, 1);
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002262}
2263
2264int
2265PyDict_Merge(PyObject *a, PyObject *b, int override)
2266{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002267 PyDictObject *mp, *other;
2268 Py_ssize_t i, n;
Victor Stinner742da042016-09-07 17:40:12 -07002269 PyDictKeyEntry *entry, *ep0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002271 /* We accept for the argument either a concrete dictionary object,
2272 * or an abstract "mapping" object. For the former, we can do
2273 * things quite efficiently. For the latter, we only require that
2274 * PyMapping_Keys() and PyObject_GetItem() be supported.
2275 */
2276 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2277 PyErr_BadInternalCall();
2278 return -1;
2279 }
2280 mp = (PyDictObject*)a;
2281 if (PyDict_Check(b)) {
2282 other = (PyDictObject*)b;
2283 if (other == mp || other->ma_used == 0)
2284 /* a.update(a) or a.update({}); nothing to do */
2285 return 0;
2286 if (mp->ma_used == 0)
2287 /* Since the target dict is empty, PyDict_GetItem()
2288 * always returns NULL. Setting override to 1
2289 * skips the unnecessary test.
2290 */
2291 override = 1;
2292 /* Do one big resize at the start, rather than
2293 * incrementally resizing as we insert new items. Expect
2294 * that there will be no (or few) overlapping keys.
2295 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002296 if (mp->ma_keys->dk_usable * 3 < other->ma_used * 2)
2297 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 return -1;
Victor Stinner742da042016-09-07 17:40:12 -07002299 ep0 = DK_ENTRIES(other->ma_keys);
2300 for (i = 0, n = other->ma_keys->dk_nentries; i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002301 PyObject *key, *value;
2302 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002303 entry = &ep0[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002304 key = entry->me_key;
2305 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002306 if (other->ma_values)
2307 value = other->ma_values[i];
2308 else
2309 value = entry->me_value;
2310
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002311 if (value != NULL) {
2312 int err = 0;
2313 Py_INCREF(key);
2314 Py_INCREF(value);
2315 if (override || PyDict_GetItem(a, key) == NULL)
2316 err = insertdict(mp, key, hash, value);
2317 Py_DECREF(value);
2318 Py_DECREF(key);
2319 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002320 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002321
Victor Stinner742da042016-09-07 17:40:12 -07002322 if (n != other->ma_keys->dk_nentries) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002323 PyErr_SetString(PyExc_RuntimeError,
2324 "dict mutated during update");
2325 return -1;
2326 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002327 }
2328 }
2329 }
2330 else {
2331 /* Do it the generic, slower way */
2332 PyObject *keys = PyMapping_Keys(b);
2333 PyObject *iter;
2334 PyObject *key, *value;
2335 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002337 if (keys == NULL)
2338 /* Docstring says this is equivalent to E.keys() so
2339 * if E doesn't have a .keys() method we want
2340 * AttributeError to percolate up. Might as well
2341 * do the same for any other error.
2342 */
2343 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002345 iter = PyObject_GetIter(keys);
2346 Py_DECREF(keys);
2347 if (iter == NULL)
2348 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002350 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
2351 if (!override && PyDict_GetItem(a, key) != NULL) {
2352 Py_DECREF(key);
2353 continue;
2354 }
2355 value = PyObject_GetItem(b, key);
2356 if (value == NULL) {
2357 Py_DECREF(iter);
2358 Py_DECREF(key);
2359 return -1;
2360 }
2361 status = PyDict_SetItem(a, key, value);
2362 Py_DECREF(key);
2363 Py_DECREF(value);
2364 if (status < 0) {
2365 Py_DECREF(iter);
2366 return -1;
2367 }
2368 }
2369 Py_DECREF(iter);
2370 if (PyErr_Occurred())
2371 /* Iterator completed, via error */
2372 return -1;
2373 }
2374 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002375}
2376
2377static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002378dict_copy(PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002379{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002381}
2382
2383PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002384PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002385{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002387 PyDictObject *mp;
2388 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002390 if (o == NULL || !PyDict_Check(o)) {
2391 PyErr_BadInternalCall();
2392 return NULL;
2393 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002394 mp = (PyDictObject *)o;
2395 if (_PyDict_HasSplitTable(mp)) {
2396 PyDictObject *split_copy;
Victor Stinner742da042016-09-07 17:40:12 -07002397 Py_ssize_t size = USABLE_FRACTION(DK_SIZE(mp->ma_keys));
2398 PyObject **newvalues;
2399 newvalues = new_values(size);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002400 if (newvalues == NULL)
2401 return PyErr_NoMemory();
2402 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2403 if (split_copy == NULL) {
2404 free_values(newvalues);
2405 return NULL;
2406 }
2407 split_copy->ma_values = newvalues;
2408 split_copy->ma_keys = mp->ma_keys;
2409 split_copy->ma_used = mp->ma_used;
2410 DK_INCREF(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002411 for (i = 0, n = size; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002412 PyObject *value = mp->ma_values[i];
2413 Py_XINCREF(value);
2414 split_copy->ma_values[i] = value;
2415 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002416 if (_PyObject_GC_IS_TRACKED(mp))
2417 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002418 return (PyObject *)split_copy;
2419 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002420 copy = PyDict_New();
2421 if (copy == NULL)
2422 return NULL;
2423 if (PyDict_Merge(copy, o, 1) == 0)
2424 return copy;
2425 Py_DECREF(copy);
2426 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002427}
2428
Martin v. Löwis18e16552006-02-15 17:27:45 +00002429Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002430PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002431{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 if (mp == NULL || !PyDict_Check(mp)) {
2433 PyErr_BadInternalCall();
2434 return -1;
2435 }
2436 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002437}
2438
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002439PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002440PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002441{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002442 if (mp == NULL || !PyDict_Check(mp)) {
2443 PyErr_BadInternalCall();
2444 return NULL;
2445 }
2446 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002447}
2448
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002449PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002450PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002451{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002452 if (mp == NULL || !PyDict_Check(mp)) {
2453 PyErr_BadInternalCall();
2454 return NULL;
2455 }
2456 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002457}
2458
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002459PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002460PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002461{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002462 if (mp == NULL || !PyDict_Check(mp)) {
2463 PyErr_BadInternalCall();
2464 return NULL;
2465 }
2466 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002467}
2468
Tim Peterse63415e2001-05-08 04:38:29 +00002469/* Return 1 if dicts equal, 0 if not, -1 if error.
2470 * Gets out as soon as any difference is detected.
2471 * Uses only Py_EQ comparison.
2472 */
2473static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002474dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002475{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002476 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002478 if (a->ma_used != b->ma_used)
2479 /* can't be equal if # of entries differ */
2480 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002481 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Victor Stinner742da042016-09-07 17:40:12 -07002482 for (i = 0; i < a->ma_keys->dk_nentries; i++) {
2483 PyDictKeyEntry *ep = &DK_ENTRIES(a->ma_keys)[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002484 PyObject *aval;
2485 if (a->ma_values)
2486 aval = a->ma_values[i];
2487 else
2488 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002489 if (aval != NULL) {
2490 int cmp;
2491 PyObject *bval;
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002492 PyObject **vaddr;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002493 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002494 /* temporarily bump aval's refcount to ensure it stays
2495 alive until we're done with it */
2496 Py_INCREF(aval);
2497 /* ditto for key */
2498 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002499 /* reuse the known hash value */
Victor Stinner742da042016-09-07 17:40:12 -07002500 if ((b->ma_keys->dk_lookup)(b, key, ep->me_hash, &vaddr, NULL) < 0)
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002501 bval = NULL;
2502 else
2503 bval = *vaddr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002504 Py_DECREF(key);
2505 if (bval == NULL) {
2506 Py_DECREF(aval);
2507 if (PyErr_Occurred())
2508 return -1;
2509 return 0;
2510 }
2511 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
2512 Py_DECREF(aval);
2513 if (cmp <= 0) /* error or not equal */
2514 return cmp;
2515 }
2516 }
2517 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002518}
Tim Peterse63415e2001-05-08 04:38:29 +00002519
2520static PyObject *
2521dict_richcompare(PyObject *v, PyObject *w, int op)
2522{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002523 int cmp;
2524 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002526 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2527 res = Py_NotImplemented;
2528 }
2529 else if (op == Py_EQ || op == Py_NE) {
2530 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2531 if (cmp < 0)
2532 return NULL;
2533 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2534 }
2535 else
2536 res = Py_NotImplemented;
2537 Py_INCREF(res);
2538 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002539}
Tim Peterse63415e2001-05-08 04:38:29 +00002540
Larry Hastings61272b72014-01-07 12:41:53 -08002541/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002542
2543@coexist
2544dict.__contains__
2545
2546 key: object
2547 /
2548
Meador Ingee02de8c2014-01-14 16:48:31 -06002549True if D has a key k, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002550[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002551
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002552static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002553dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002554/*[clinic end generated code: output=a3d03db709ed6e6b input=b852b2a19b51ab24]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002555{
Larry Hastingsc2047262014-01-25 20:43:29 -08002556 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002557 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002558 Py_ssize_t ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002559 PyObject **value_addr;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002561 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002562 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002563 hash = PyObject_Hash(key);
2564 if (hash == -1)
2565 return NULL;
2566 }
Victor Stinner742da042016-09-07 17:40:12 -07002567 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
2568 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002569 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002570 if (ix == DKIX_EMPTY || *value_addr == NULL)
2571 Py_RETURN_FALSE;
2572 Py_RETURN_TRUE;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002573}
2574
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002575static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002576dict_get(PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00002577{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002578 PyObject *key;
2579 PyObject *failobj = Py_None;
2580 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002581 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002582 Py_ssize_t ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002583 PyObject **value_addr;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002585 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
2586 return NULL;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002588 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002589 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002590 hash = PyObject_Hash(key);
2591 if (hash == -1)
2592 return NULL;
2593 }
Victor Stinner742da042016-09-07 17:40:12 -07002594 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
2595 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002596 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002597 if (ix == DKIX_EMPTY || *value_addr == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002598 val = failobj;
Victor Stinner742da042016-09-07 17:40:12 -07002599 else
2600 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002601 Py_INCREF(val);
2602 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002603}
2604
Benjamin Peterson00e98862013-03-07 22:16:29 -05002605PyObject *
2606PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002607{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002608 PyDictObject *mp = (PyDictObject *)d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002609 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002610 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002611 Py_ssize_t hashpos, ix;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002612 PyDictKeyEntry *ep;
2613 PyObject **value_addr;
Guido van Rossum164452c2000-08-08 16:12:54 +00002614
Benjamin Peterson00e98862013-03-07 22:16:29 -05002615 if (!PyDict_Check(d)) {
2616 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002617 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002618 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002619 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002620 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002621 hash = PyObject_Hash(key);
2622 if (hash == -1)
2623 return NULL;
2624 }
Victor Stinner742da042016-09-07 17:40:12 -07002625 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, &hashpos);
2626 if (ix == DKIX_ERROR)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002627 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002628 if (ix == DKIX_EMPTY || *value_addr == NULL) {
2629 val = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002630 if (mp->ma_keys->dk_usable <= 0) {
2631 /* Need to resize. */
2632 if (insertion_resize(mp) < 0)
2633 return NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002634 find_empty_slot(mp, key, hash, &value_addr, &hashpos);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002635 }
Victor Stinner742da042016-09-07 17:40:12 -07002636 ix = mp->ma_keys->dk_nentries;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002637 Py_INCREF(defaultobj);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002638 Py_INCREF(key);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002639 MAINTAIN_TRACKING(mp, key, defaultobj);
Victor Stinner742da042016-09-07 17:40:12 -07002640 dk_set_index(mp->ma_keys, hashpos, ix);
2641 ep = &DK_ENTRIES(mp->ma_keys)[ix];
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002642 ep->me_key = key;
2643 ep->me_hash = hash;
Victor Stinner742da042016-09-07 17:40:12 -07002644 if (mp->ma_values) {
2645 mp->ma_values[ix] = val;
2646 }
2647 else {
2648 ep->me_value = val;
2649 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002650 mp->ma_keys->dk_usable--;
Victor Stinner742da042016-09-07 17:40:12 -07002651 mp->ma_keys->dk_nentries++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002652 mp->ma_used++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002653 }
Victor Stinner742da042016-09-07 17:40:12 -07002654 else
2655 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002656 return val;
Guido van Rossum164452c2000-08-08 16:12:54 +00002657}
2658
Benjamin Peterson00e98862013-03-07 22:16:29 -05002659static PyObject *
2660dict_setdefault(PyDictObject *mp, PyObject *args)
2661{
2662 PyObject *key, *val;
2663 PyObject *defaultobj = Py_None;
2664
2665 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &defaultobj))
2666 return NULL;
2667
Benjamin Peterson55898502013-03-08 08:36:49 -05002668 val = PyDict_SetDefault((PyObject *)mp, key, defaultobj);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002669 Py_XINCREF(val);
2670 return val;
2671}
Guido van Rossum164452c2000-08-08 16:12:54 +00002672
2673static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002674dict_clear(PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002675{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002676 PyDict_Clear((PyObject *)mp);
2677 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002678}
2679
Guido van Rossumba6ab842000-12-12 22:02:18 +00002680static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002681dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002682{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002683 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00002684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002685 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2686 return NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06002687
2688 return _PyDict_Pop(mp, key, deflt);
Guido van Rossume027d982002-04-12 15:11:59 +00002689}
2690
2691static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002692dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002693{
Victor Stinner742da042016-09-07 17:40:12 -07002694 Py_ssize_t i, j;
2695 PyDictKeyEntry *ep0, *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002696 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002698 /* Allocate the result tuple before checking the size. Believe it
2699 * or not, this allocation could trigger a garbage collection which
2700 * could empty the dict, so if we checked the size first and that
2701 * happened, the result would be an infinite loop (searching for an
2702 * entry that no longer exists). Note that the usual popitem()
2703 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2704 * tuple away if the dict *is* empty isn't a significant
2705 * inefficiency -- possible, but unlikely in practice.
2706 */
2707 res = PyTuple_New(2);
2708 if (res == NULL)
2709 return NULL;
2710 if (mp->ma_used == 0) {
2711 Py_DECREF(res);
2712 PyErr_SetString(PyExc_KeyError,
2713 "popitem(): dictionary is empty");
2714 return NULL;
2715 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002716 /* Convert split table to combined table */
2717 if (mp->ma_keys->dk_lookup == lookdict_split) {
2718 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2719 Py_DECREF(res);
2720 return NULL;
2721 }
2722 }
2723 ENSURE_ALLOWS_DELETIONS(mp);
Victor Stinner742da042016-09-07 17:40:12 -07002724
2725 /* Pop last item */
2726 ep0 = DK_ENTRIES(mp->ma_keys);
2727 i = mp->ma_keys->dk_nentries - 1;
2728 while (i >= 0 && ep0[i].me_value == NULL) {
2729 i--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002730 }
Victor Stinner742da042016-09-07 17:40:12 -07002731 assert(i >= 0);
2732
2733 ep = &ep0[i];
2734 j = lookdict_index(mp->ma_keys, ep->me_hash, i);
2735 assert(j >= 0);
2736 assert(dk_get_index(mp->ma_keys, j) == i);
2737 dk_set_index(mp->ma_keys, j, DKIX_DUMMY);
2738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002739 PyTuple_SET_ITEM(res, 0, ep->me_key);
2740 PyTuple_SET_ITEM(res, 1, ep->me_value);
Victor Stinner742da042016-09-07 17:40:12 -07002741 ep->me_key = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002742 ep->me_value = NULL;
Victor Stinner742da042016-09-07 17:40:12 -07002743 /* We can't dk_usable++ since there is DKIX_DUMMY in indices */
2744 mp->ma_keys->dk_nentries = i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002745 mp->ma_used--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002746 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002747}
2748
Jeremy Hylton8caad492000-06-23 14:18:11 +00002749static int
2750dict_traverse(PyObject *op, visitproc visit, void *arg)
2751{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002752 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson55f44522016-09-05 12:12:59 -07002753 PyDictKeysObject *keys = mp->ma_keys;
Victor Stinner742da042016-09-07 17:40:12 -07002754 PyDictKeyEntry *entries = DK_ENTRIES(mp->ma_keys);
2755 Py_ssize_t i, n = keys->dk_nentries;
2756
Benjamin Peterson55f44522016-09-05 12:12:59 -07002757 if (keys->dk_lookup == lookdict) {
2758 for (i = 0; i < n; i++) {
2759 if (entries[i].me_value != NULL) {
2760 Py_VISIT(entries[i].me_value);
2761 Py_VISIT(entries[i].me_key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002762 }
2763 }
Victor Stinner742da042016-09-07 17:40:12 -07002764 }
2765 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002766 if (mp->ma_values != NULL) {
Benjamin Peterson55f44522016-09-05 12:12:59 -07002767 for (i = 0; i < n; i++) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002768 Py_VISIT(mp->ma_values[i]);
2769 }
2770 }
2771 else {
Benjamin Peterson55f44522016-09-05 12:12:59 -07002772 for (i = 0; i < n; i++) {
2773 Py_VISIT(entries[i].me_value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002774 }
2775 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002776 }
2777 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002778}
2779
2780static int
2781dict_tp_clear(PyObject *op)
2782{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002783 PyDict_Clear(op);
2784 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002785}
2786
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002787static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002788
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002789Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06002790_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002791{
Victor Stinner742da042016-09-07 17:40:12 -07002792 Py_ssize_t size, usable, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002793
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002794 size = DK_SIZE(mp->ma_keys);
Victor Stinner742da042016-09-07 17:40:12 -07002795 usable = USABLE_FRACTION(size);
2796
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002797 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002798 if (mp->ma_values)
Victor Stinner742da042016-09-07 17:40:12 -07002799 res += usable * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002800 /* If the dictionary is split, the keys portion is accounted-for
2801 in the type object. */
2802 if (mp->ma_keys->dk_refcnt == 1)
Victor Stinner98ee9d52016-09-08 09:33:56 -07002803 res += (sizeof(PyDictKeysObject)
2804 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
2805 + DK_IXSIZE(mp->ma_keys) * size
2806 + sizeof(PyDictKeyEntry) * usable);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002807 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002808}
2809
2810Py_ssize_t
2811_PyDict_KeysSize(PyDictKeysObject *keys)
2812{
Victor Stinner98ee9d52016-09-08 09:33:56 -07002813 return (sizeof(PyDictKeysObject)
2814 - Py_MEMBER_SIZE(PyDictKeysObject, dk_indices)
2815 + DK_IXSIZE(keys) * DK_SIZE(keys)
2816 + USABLE_FRACTION(DK_SIZE(keys)) * sizeof(PyDictKeyEntry));
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002817}
2818
doko@ubuntu.com17210f52016-01-14 14:04:59 +01002819static PyObject *
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002820dict_sizeof(PyDictObject *mp)
2821{
2822 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
2823}
2824
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002825PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
2826
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002827PyDoc_STRVAR(sizeof__doc__,
2828"D.__sizeof__() -> size of D in memory, in bytes");
2829
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002830PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002831"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002832
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002833PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002834"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 +00002835
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002836PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002837"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002838If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00002839
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002840PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00002841"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000028422-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002843
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002844PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04002845"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
2846If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
2847If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
2848In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00002849
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002850PyDoc_STRVAR(clear__doc__,
2851"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002852
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002853PyDoc_STRVAR(copy__doc__,
2854"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00002855
Guido van Rossumb90c8482007-02-10 01:11:45 +00002856/* Forward */
2857static PyObject *dictkeys_new(PyObject *);
2858static PyObject *dictitems_new(PyObject *);
2859static PyObject *dictvalues_new(PyObject *);
2860
Guido van Rossum45c85d12007-07-27 16:31:40 +00002861PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002862 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002863PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002864 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002865PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002866 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00002867
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002868static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07002869 DICT___CONTAINS___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
2871 getitem__doc__},
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002872 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002873 sizeof__doc__},
2874 {"get", (PyCFunction)dict_get, METH_VARARGS,
2875 get__doc__},
2876 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
2877 setdefault_doc__},
2878 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
2879 pop__doc__},
2880 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
2881 popitem__doc__},
2882 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
2883 keys__doc__},
2884 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
2885 items__doc__},
2886 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
2887 values__doc__},
2888 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
2889 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08002890 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002891 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
2892 clear__doc__},
2893 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
2894 copy__doc__},
2895 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002896};
2897
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002898/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002899int
2900PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002901{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002902 Py_hash_t hash;
Victor Stinner742da042016-09-07 17:40:12 -07002903 Py_ssize_t ix;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002904 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002905 PyObject **value_addr;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002906
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002907 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002908 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002909 hash = PyObject_Hash(key);
2910 if (hash == -1)
2911 return -1;
2912 }
Victor Stinner742da042016-09-07 17:40:12 -07002913 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
2914 if (ix == DKIX_ERROR)
2915 return -1;
2916 return (ix != DKIX_EMPTY && *value_addr != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002917}
2918
Thomas Wouterscf297e42007-02-23 15:07:44 +00002919/* Internal version of PyDict_Contains used when the hash value is already known */
2920int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002921_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00002922{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002923 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002924 PyObject **value_addr;
Victor Stinner742da042016-09-07 17:40:12 -07002925 Py_ssize_t ix;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002926
Victor Stinner742da042016-09-07 17:40:12 -07002927 ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr, NULL);
2928 if (ix == DKIX_ERROR)
2929 return -1;
2930 return (ix != DKIX_EMPTY && *value_addr != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00002931}
2932
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002933/* Hack to implement "key in dict" */
2934static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002935 0, /* sq_length */
2936 0, /* sq_concat */
2937 0, /* sq_repeat */
2938 0, /* sq_item */
2939 0, /* sq_slice */
2940 0, /* sq_ass_item */
2941 0, /* sq_ass_slice */
2942 PyDict_Contains, /* sq_contains */
2943 0, /* sq_inplace_concat */
2944 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002945};
2946
Guido van Rossum09e563a2001-05-01 12:10:21 +00002947static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002948dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2949{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002950 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02002951 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002953 assert(type != NULL && type->tp_alloc != NULL);
2954 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02002955 if (self == NULL)
2956 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02002957 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002958
Victor Stinnera9f61a52013-07-16 22:17:26 +02002959 /* The object has been implicitly tracked by tp_alloc */
2960 if (type == &PyDict_Type)
2961 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002962
2963 d->ma_used = 0;
Victor Stinner742da042016-09-07 17:40:12 -07002964 d->ma_keys = new_keys_object(PyDict_MINSIZE);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002965 if (d->ma_keys == NULL) {
2966 Py_DECREF(self);
2967 return NULL;
2968 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002969 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002970}
2971
Tim Peters25786c02001-09-02 08:22:48 +00002972static int
2973dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2974{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002975 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002976}
2977
Tim Peters6d6c1a32001-08-02 04:15:00 +00002978static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002979dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002980{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002981 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002982}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002983
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002984PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002985"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002986"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002987" (key, value) pairs\n"
2988"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002989" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002990" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002991" d[k] = v\n"
2992"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2993" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002994
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002995PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2997 "dict",
2998 sizeof(PyDictObject),
2999 0,
3000 (destructor)dict_dealloc, /* tp_dealloc */
3001 0, /* tp_print */
3002 0, /* tp_getattr */
3003 0, /* tp_setattr */
3004 0, /* tp_reserved */
3005 (reprfunc)dict_repr, /* tp_repr */
3006 0, /* tp_as_number */
3007 &dict_as_sequence, /* tp_as_sequence */
3008 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00003009 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003010 0, /* tp_call */
3011 0, /* tp_str */
3012 PyObject_GenericGetAttr, /* tp_getattro */
3013 0, /* tp_setattro */
3014 0, /* tp_as_buffer */
3015 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3016 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
3017 dictionary_doc, /* tp_doc */
3018 dict_traverse, /* tp_traverse */
3019 dict_tp_clear, /* tp_clear */
3020 dict_richcompare, /* tp_richcompare */
3021 0, /* tp_weaklistoffset */
3022 (getiterfunc)dict_iter, /* tp_iter */
3023 0, /* tp_iternext */
3024 mapp_methods, /* tp_methods */
3025 0, /* tp_members */
3026 0, /* tp_getset */
3027 0, /* tp_base */
3028 0, /* tp_dict */
3029 0, /* tp_descr_get */
3030 0, /* tp_descr_set */
3031 0, /* tp_dictoffset */
3032 dict_init, /* tp_init */
3033 PyType_GenericAlloc, /* tp_alloc */
3034 dict_new, /* tp_new */
3035 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003036};
3037
Victor Stinner3c1e4812012-03-26 22:10:51 +02003038PyObject *
3039_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
3040{
3041 PyObject *kv;
3042 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02003043 if (kv == NULL) {
3044 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003045 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02003046 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02003047 return PyDict_GetItem(dp, kv);
3048}
3049
Guido van Rossum3cca2451997-05-16 14:23:33 +00003050/* For backward compatibility with old dictionary interface */
3051
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003052PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003053PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003054{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003055 PyObject *kv, *rv;
3056 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003057 if (kv == NULL) {
3058 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003059 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02003060 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003061 rv = PyDict_GetItem(v, kv);
3062 Py_DECREF(kv);
3063 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003064}
3065
3066int
Victor Stinner3c1e4812012-03-26 22:10:51 +02003067_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
3068{
3069 PyObject *kv;
3070 kv = _PyUnicode_FromId(key); /* borrowed */
3071 if (kv == NULL)
3072 return -1;
3073 return PyDict_SetItem(v, kv, item);
3074}
3075
3076int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003077PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003078{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003079 PyObject *kv;
3080 int err;
3081 kv = PyUnicode_FromString(key);
3082 if (kv == NULL)
3083 return -1;
3084 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
3085 err = PyDict_SetItem(v, kv, item);
3086 Py_DECREF(kv);
3087 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003088}
3089
3090int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01003091_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
3092{
3093 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
3094 if (kv == NULL)
3095 return -1;
3096 return PyDict_DelItem(v, kv);
3097}
3098
3099int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00003100PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003101{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003102 PyObject *kv;
3103 int err;
3104 kv = PyUnicode_FromString(key);
3105 if (kv == NULL)
3106 return -1;
3107 err = PyDict_DelItem(v, kv);
3108 Py_DECREF(kv);
3109 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00003110}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003111
Raymond Hettinger019a1482004-03-18 02:41:19 +00003112/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003113
3114typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003115 PyObject_HEAD
3116 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
3117 Py_ssize_t di_used;
3118 Py_ssize_t di_pos;
3119 PyObject* di_result; /* reusable result tuple for iteritems */
3120 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003121} dictiterobject;
3122
3123static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00003124dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003125{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003126 dictiterobject *di;
3127 di = PyObject_GC_New(dictiterobject, itertype);
3128 if (di == NULL)
3129 return NULL;
3130 Py_INCREF(dict);
3131 di->di_dict = dict;
3132 di->di_used = dict->ma_used;
3133 di->di_pos = 0;
3134 di->len = dict->ma_used;
3135 if (itertype == &PyDictIterItem_Type) {
3136 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
3137 if (di->di_result == NULL) {
3138 Py_DECREF(di);
3139 return NULL;
3140 }
3141 }
3142 else
3143 di->di_result = NULL;
3144 _PyObject_GC_TRACK(di);
3145 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003146}
3147
3148static void
3149dictiter_dealloc(dictiterobject *di)
3150{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003151 Py_XDECREF(di->di_dict);
3152 Py_XDECREF(di->di_result);
3153 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003154}
3155
3156static int
3157dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
3158{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003159 Py_VISIT(di->di_dict);
3160 Py_VISIT(di->di_result);
3161 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003162}
3163
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003164static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003165dictiter_len(dictiterobject *di)
3166{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003167 Py_ssize_t len = 0;
3168 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
3169 len = di->len;
3170 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003171}
3172
Guido van Rossumb90c8482007-02-10 01:11:45 +00003173PyDoc_STRVAR(length_hint_doc,
3174 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003175
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003176static PyObject *
3177dictiter_reduce(dictiterobject *di);
3178
3179PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
3180
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00003181static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003182 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
3183 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003184 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
3185 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003186 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00003187};
3188
Raymond Hettinger019a1482004-03-18 02:41:19 +00003189static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00003190{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003191 PyObject *key;
Victor Stinner742da042016-09-07 17:40:12 -07003192 Py_ssize_t i, n, offset;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003193 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003194 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003195 PyObject **value_ptr;
Guido van Rossum213c7a62001-04-23 14:08:49 +00003196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003197 if (d == NULL)
3198 return NULL;
3199 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00003200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003201 if (di->di_used != d->ma_used) {
3202 PyErr_SetString(PyExc_RuntimeError,
3203 "dictionary changed size during iteration");
3204 di->di_used = -1; /* Make this state sticky */
3205 return NULL;
3206 }
Guido van Rossum2147df72002-07-16 20:30:22 +00003207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003208 i = di->di_pos;
3209 if (i < 0)
3210 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003211 k = d->ma_keys;
3212 if (d->ma_values) {
3213 value_ptr = &d->ma_values[i];
3214 offset = sizeof(PyObject *);
3215 }
3216 else {
Victor Stinner742da042016-09-07 17:40:12 -07003217 value_ptr = &DK_ENTRIES(k)[i].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003218 offset = sizeof(PyDictKeyEntry);
3219 }
Victor Stinner742da042016-09-07 17:40:12 -07003220 n = k->dk_nentries - 1;
3221 while (i <= n && *value_ptr == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003222 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003223 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003224 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003225 di->di_pos = i+1;
Victor Stinner742da042016-09-07 17:40:12 -07003226 if (i > n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003227 goto fail;
3228 di->len--;
Victor Stinner742da042016-09-07 17:40:12 -07003229 key = DK_ENTRIES(k)[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003230 Py_INCREF(key);
3231 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003232
3233fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003234 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003235 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003236 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003237}
3238
Raymond Hettinger019a1482004-03-18 02:41:19 +00003239PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003240 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3241 "dict_keyiterator", /* tp_name */
3242 sizeof(dictiterobject), /* tp_basicsize */
3243 0, /* tp_itemsize */
3244 /* methods */
3245 (destructor)dictiter_dealloc, /* tp_dealloc */
3246 0, /* tp_print */
3247 0, /* tp_getattr */
3248 0, /* tp_setattr */
3249 0, /* tp_reserved */
3250 0, /* tp_repr */
3251 0, /* tp_as_number */
3252 0, /* tp_as_sequence */
3253 0, /* tp_as_mapping */
3254 0, /* tp_hash */
3255 0, /* tp_call */
3256 0, /* tp_str */
3257 PyObject_GenericGetAttr, /* tp_getattro */
3258 0, /* tp_setattro */
3259 0, /* tp_as_buffer */
3260 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3261 0, /* tp_doc */
3262 (traverseproc)dictiter_traverse, /* tp_traverse */
3263 0, /* tp_clear */
3264 0, /* tp_richcompare */
3265 0, /* tp_weaklistoffset */
3266 PyObject_SelfIter, /* tp_iter */
3267 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3268 dictiter_methods, /* tp_methods */
3269 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003270};
3271
3272static PyObject *dictiter_iternextvalue(dictiterobject *di)
3273{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003274 PyObject *value;
Victor Stinner742da042016-09-07 17:40:12 -07003275 Py_ssize_t i, n, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003276 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003277 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003279 if (d == NULL)
3280 return NULL;
3281 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003283 if (di->di_used != d->ma_used) {
3284 PyErr_SetString(PyExc_RuntimeError,
3285 "dictionary changed size during iteration");
3286 di->di_used = -1; /* Make this state sticky */
3287 return NULL;
3288 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003290 i = di->di_pos;
Victor Stinner742da042016-09-07 17:40:12 -07003291 n = d->ma_keys->dk_nentries - 1;
3292 if (i < 0 || i > n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003293 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003294 if (d->ma_values) {
3295 value_ptr = &d->ma_values[i];
3296 offset = sizeof(PyObject *);
3297 }
3298 else {
Victor Stinner742da042016-09-07 17:40:12 -07003299 value_ptr = &DK_ENTRIES(d->ma_keys)[i].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003300 offset = sizeof(PyDictKeyEntry);
3301 }
Victor Stinner742da042016-09-07 17:40:12 -07003302 while (i <= n && *value_ptr == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003303 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003304 i++;
Victor Stinner742da042016-09-07 17:40:12 -07003305 if (i > n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003306 goto fail;
3307 }
3308 di->di_pos = i+1;
3309 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003310 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003311 Py_INCREF(value);
3312 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003313
3314fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003315 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003316 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003317 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003318}
3319
3320PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003321 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3322 "dict_valueiterator", /* tp_name */
3323 sizeof(dictiterobject), /* tp_basicsize */
3324 0, /* tp_itemsize */
3325 /* methods */
3326 (destructor)dictiter_dealloc, /* tp_dealloc */
3327 0, /* tp_print */
3328 0, /* tp_getattr */
3329 0, /* tp_setattr */
3330 0, /* tp_reserved */
3331 0, /* tp_repr */
3332 0, /* tp_as_number */
3333 0, /* tp_as_sequence */
3334 0, /* tp_as_mapping */
3335 0, /* tp_hash */
3336 0, /* tp_call */
3337 0, /* tp_str */
3338 PyObject_GenericGetAttr, /* tp_getattro */
3339 0, /* tp_setattro */
3340 0, /* tp_as_buffer */
3341 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3342 0, /* tp_doc */
3343 (traverseproc)dictiter_traverse, /* tp_traverse */
3344 0, /* tp_clear */
3345 0, /* tp_richcompare */
3346 0, /* tp_weaklistoffset */
3347 PyObject_SelfIter, /* tp_iter */
3348 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3349 dictiter_methods, /* tp_methods */
3350 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003351};
3352
3353static PyObject *dictiter_iternextitem(dictiterobject *di)
3354{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003355 PyObject *key, *value, *result = di->di_result;
Victor Stinner742da042016-09-07 17:40:12 -07003356 Py_ssize_t i, n, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003357 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003358 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003360 if (d == NULL)
3361 return NULL;
3362 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003364 if (di->di_used != d->ma_used) {
3365 PyErr_SetString(PyExc_RuntimeError,
3366 "dictionary changed size during iteration");
3367 di->di_used = -1; /* Make this state sticky */
3368 return NULL;
3369 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003371 i = di->di_pos;
3372 if (i < 0)
3373 goto fail;
Victor Stinner742da042016-09-07 17:40:12 -07003374 n = d->ma_keys->dk_nentries - 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003375 if (d->ma_values) {
3376 value_ptr = &d->ma_values[i];
3377 offset = sizeof(PyObject *);
3378 }
3379 else {
Victor Stinner742da042016-09-07 17:40:12 -07003380 value_ptr = &DK_ENTRIES(d->ma_keys)[i].me_value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003381 offset = sizeof(PyDictKeyEntry);
3382 }
Victor Stinner742da042016-09-07 17:40:12 -07003383 while (i <= n && *value_ptr == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003384 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003385 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003386 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003387 di->di_pos = i+1;
Victor Stinner742da042016-09-07 17:40:12 -07003388 if (i > n)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003389 goto fail;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003391 if (result->ob_refcnt == 1) {
3392 Py_INCREF(result);
3393 Py_DECREF(PyTuple_GET_ITEM(result, 0));
3394 Py_DECREF(PyTuple_GET_ITEM(result, 1));
3395 } else {
3396 result = PyTuple_New(2);
3397 if (result == NULL)
3398 return NULL;
3399 }
3400 di->len--;
Victor Stinner742da042016-09-07 17:40:12 -07003401 key = DK_ENTRIES(d->ma_keys)[i].me_key;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003402 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003403 Py_INCREF(key);
3404 Py_INCREF(value);
Eric Snow96c6af92015-05-29 22:21:39 -06003405 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3406 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003407 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003408
3409fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003410 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003411 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003412 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003413}
3414
3415PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003416 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3417 "dict_itemiterator", /* tp_name */
3418 sizeof(dictiterobject), /* tp_basicsize */
3419 0, /* tp_itemsize */
3420 /* methods */
3421 (destructor)dictiter_dealloc, /* tp_dealloc */
3422 0, /* tp_print */
3423 0, /* tp_getattr */
3424 0, /* tp_setattr */
3425 0, /* tp_reserved */
3426 0, /* tp_repr */
3427 0, /* tp_as_number */
3428 0, /* tp_as_sequence */
3429 0, /* tp_as_mapping */
3430 0, /* tp_hash */
3431 0, /* tp_call */
3432 0, /* tp_str */
3433 PyObject_GenericGetAttr, /* tp_getattro */
3434 0, /* tp_setattro */
3435 0, /* tp_as_buffer */
3436 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3437 0, /* tp_doc */
3438 (traverseproc)dictiter_traverse, /* tp_traverse */
3439 0, /* tp_clear */
3440 0, /* tp_richcompare */
3441 0, /* tp_weaklistoffset */
3442 PyObject_SelfIter, /* tp_iter */
3443 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3444 dictiter_methods, /* tp_methods */
3445 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003446};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003447
3448
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003449static PyObject *
3450dictiter_reduce(dictiterobject *di)
3451{
3452 PyObject *list;
3453 dictiterobject tmp;
3454
3455 list = PyList_New(0);
3456 if (!list)
3457 return NULL;
3458
3459 /* copy the itertor state */
3460 tmp = *di;
3461 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003462
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003463 /* iterate the temporary into a list */
3464 for(;;) {
3465 PyObject *element = 0;
3466 if (Py_TYPE(di) == &PyDictIterItem_Type)
3467 element = dictiter_iternextitem(&tmp);
3468 else if (Py_TYPE(di) == &PyDictIterKey_Type)
3469 element = dictiter_iternextkey(&tmp);
3470 else if (Py_TYPE(di) == &PyDictIterValue_Type)
3471 element = dictiter_iternextvalue(&tmp);
3472 else
3473 assert(0);
3474 if (element) {
3475 if (PyList_Append(list, element)) {
3476 Py_DECREF(element);
3477 Py_DECREF(list);
3478 Py_XDECREF(tmp.di_dict);
3479 return NULL;
3480 }
3481 Py_DECREF(element);
3482 } else
3483 break;
3484 }
3485 Py_XDECREF(tmp.di_dict);
3486 /* check for error */
3487 if (tmp.di_dict != NULL) {
3488 /* we have an error */
3489 Py_DECREF(list);
3490 return NULL;
3491 }
Antoine Pitroua7013882012-04-05 00:04:20 +02003492 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003493}
3494
Guido van Rossum3ac67412007-02-10 18:55:06 +00003495/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003496/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003497/***********************************************/
3498
Guido van Rossumb90c8482007-02-10 01:11:45 +00003499/* The instance lay-out is the same for all three; but the type differs. */
3500
Guido van Rossumb90c8482007-02-10 01:11:45 +00003501static void
Eric Snow96c6af92015-05-29 22:21:39 -06003502dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003503{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003504 Py_XDECREF(dv->dv_dict);
3505 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003506}
3507
3508static int
Eric Snow96c6af92015-05-29 22:21:39 -06003509dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003510{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003511 Py_VISIT(dv->dv_dict);
3512 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003513}
3514
Guido van Rossum83825ac2007-02-10 04:54:19 +00003515static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003516dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003517{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003518 Py_ssize_t len = 0;
3519 if (dv->dv_dict != NULL)
3520 len = dv->dv_dict->ma_used;
3521 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003522}
3523
Eric Snow96c6af92015-05-29 22:21:39 -06003524PyObject *
3525_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003526{
Eric Snow96c6af92015-05-29 22:21:39 -06003527 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003528 if (dict == NULL) {
3529 PyErr_BadInternalCall();
3530 return NULL;
3531 }
3532 if (!PyDict_Check(dict)) {
3533 /* XXX Get rid of this restriction later */
3534 PyErr_Format(PyExc_TypeError,
3535 "%s() requires a dict argument, not '%s'",
3536 type->tp_name, dict->ob_type->tp_name);
3537 return NULL;
3538 }
Eric Snow96c6af92015-05-29 22:21:39 -06003539 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003540 if (dv == NULL)
3541 return NULL;
3542 Py_INCREF(dict);
3543 dv->dv_dict = (PyDictObject *)dict;
3544 _PyObject_GC_TRACK(dv);
3545 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003546}
3547
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003548/* TODO(guido): The views objects are not complete:
3549
3550 * support more set operations
3551 * support arbitrary mappings?
3552 - either these should be static or exported in dictobject.h
3553 - if public then they should probably be in builtins
3554*/
3555
Guido van Rossumaac530c2007-08-24 22:33:45 +00003556/* Return 1 if self is a subset of other, iterating over self;
3557 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003558static int
3559all_contained_in(PyObject *self, PyObject *other)
3560{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003561 PyObject *iter = PyObject_GetIter(self);
3562 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003564 if (iter == NULL)
3565 return -1;
3566 for (;;) {
3567 PyObject *next = PyIter_Next(iter);
3568 if (next == NULL) {
3569 if (PyErr_Occurred())
3570 ok = -1;
3571 break;
3572 }
3573 ok = PySequence_Contains(other, next);
3574 Py_DECREF(next);
3575 if (ok <= 0)
3576 break;
3577 }
3578 Py_DECREF(iter);
3579 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003580}
3581
3582static PyObject *
3583dictview_richcompare(PyObject *self, PyObject *other, int op)
3584{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003585 Py_ssize_t len_self, len_other;
3586 int ok;
3587 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003589 assert(self != NULL);
3590 assert(PyDictViewSet_Check(self));
3591 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003592
Brian Curtindfc80e32011-08-10 20:28:54 -05003593 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3594 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003596 len_self = PyObject_Size(self);
3597 if (len_self < 0)
3598 return NULL;
3599 len_other = PyObject_Size(other);
3600 if (len_other < 0)
3601 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003603 ok = 0;
3604 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003606 case Py_NE:
3607 case Py_EQ:
3608 if (len_self == len_other)
3609 ok = all_contained_in(self, other);
3610 if (op == Py_NE && ok >= 0)
3611 ok = !ok;
3612 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003614 case Py_LT:
3615 if (len_self < len_other)
3616 ok = all_contained_in(self, other);
3617 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003619 case Py_LE:
3620 if (len_self <= len_other)
3621 ok = all_contained_in(self, other);
3622 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003624 case Py_GT:
3625 if (len_self > len_other)
3626 ok = all_contained_in(other, self);
3627 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003629 case Py_GE:
3630 if (len_self >= len_other)
3631 ok = all_contained_in(other, self);
3632 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003634 }
3635 if (ok < 0)
3636 return NULL;
3637 result = ok ? Py_True : Py_False;
3638 Py_INCREF(result);
3639 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003640}
3641
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003642static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003643dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003644{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003645 PyObject *seq;
3646 PyObject *result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003648 seq = PySequence_List((PyObject *)dv);
3649 if (seq == NULL)
3650 return NULL;
3651
3652 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
3653 Py_DECREF(seq);
3654 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003655}
3656
Guido van Rossum3ac67412007-02-10 18:55:06 +00003657/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003658
3659static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003660dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003661{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003662 if (dv->dv_dict == NULL) {
3663 Py_RETURN_NONE;
3664 }
3665 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003666}
3667
3668static int
Eric Snow96c6af92015-05-29 22:21:39 -06003669dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00003670{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003671 if (dv->dv_dict == NULL)
3672 return 0;
3673 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003674}
3675
Guido van Rossum83825ac2007-02-10 04:54:19 +00003676static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003677 (lenfunc)dictview_len, /* sq_length */
3678 0, /* sq_concat */
3679 0, /* sq_repeat */
3680 0, /* sq_item */
3681 0, /* sq_slice */
3682 0, /* sq_ass_item */
3683 0, /* sq_ass_slice */
3684 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003685};
3686
Guido van Rossum523259b2007-08-24 23:41:22 +00003687static PyObject*
3688dictviews_sub(PyObject* self, PyObject *other)
3689{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003690 PyObject *result = PySet_New(self);
3691 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003692 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003694 if (result == NULL)
3695 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003696
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003697 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003698 if (tmp == NULL) {
3699 Py_DECREF(result);
3700 return NULL;
3701 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003703 Py_DECREF(tmp);
3704 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003705}
3706
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003707PyObject*
3708_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00003709{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003710 PyObject *result = PySet_New(self);
3711 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003712 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003714 if (result == NULL)
3715 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003716
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003717 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_intersection_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003718 if (tmp == NULL) {
3719 Py_DECREF(result);
3720 return NULL;
3721 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003723 Py_DECREF(tmp);
3724 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003725}
3726
3727static PyObject*
3728dictviews_or(PyObject* self, PyObject *other)
3729{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003730 PyObject *result = PySet_New(self);
3731 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003732 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003734 if (result == NULL)
3735 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003736
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003737 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003738 if (tmp == NULL) {
3739 Py_DECREF(result);
3740 return NULL;
3741 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003743 Py_DECREF(tmp);
3744 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003745}
3746
3747static PyObject*
3748dictviews_xor(PyObject* self, PyObject *other)
3749{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003750 PyObject *result = PySet_New(self);
3751 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003752 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003754 if (result == NULL)
3755 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003756
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003757 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_symmetric_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003758 if (tmp == NULL) {
3759 Py_DECREF(result);
3760 return NULL;
3761 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003763 Py_DECREF(tmp);
3764 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003765}
3766
3767static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003768 0, /*nb_add*/
3769 (binaryfunc)dictviews_sub, /*nb_subtract*/
3770 0, /*nb_multiply*/
3771 0, /*nb_remainder*/
3772 0, /*nb_divmod*/
3773 0, /*nb_power*/
3774 0, /*nb_negative*/
3775 0, /*nb_positive*/
3776 0, /*nb_absolute*/
3777 0, /*nb_bool*/
3778 0, /*nb_invert*/
3779 0, /*nb_lshift*/
3780 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003781 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003782 (binaryfunc)dictviews_xor, /*nb_xor*/
3783 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00003784};
3785
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003786static PyObject*
3787dictviews_isdisjoint(PyObject *self, PyObject *other)
3788{
3789 PyObject *it;
3790 PyObject *item = NULL;
3791
3792 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06003793 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003794 Py_RETURN_TRUE;
3795 else
3796 Py_RETURN_FALSE;
3797 }
3798
3799 /* Iterate over the shorter object (only if other is a set,
3800 * because PySequence_Contains may be expensive otherwise): */
3801 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06003802 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003803 Py_ssize_t len_other = PyObject_Size(other);
3804 if (len_other == -1)
3805 return NULL;
3806
3807 if ((len_other > len_self)) {
3808 PyObject *tmp = other;
3809 other = self;
3810 self = tmp;
3811 }
3812 }
3813
3814 it = PyObject_GetIter(other);
3815 if (it == NULL)
3816 return NULL;
3817
3818 while ((item = PyIter_Next(it)) != NULL) {
3819 int contains = PySequence_Contains(self, item);
3820 Py_DECREF(item);
3821 if (contains == -1) {
3822 Py_DECREF(it);
3823 return NULL;
3824 }
3825
3826 if (contains) {
3827 Py_DECREF(it);
3828 Py_RETURN_FALSE;
3829 }
3830 }
3831 Py_DECREF(it);
3832 if (PyErr_Occurred())
3833 return NULL; /* PyIter_Next raised an exception. */
3834 Py_RETURN_TRUE;
3835}
3836
3837PyDoc_STRVAR(isdisjoint_doc,
3838"Return True if the view and the given iterable have a null intersection.");
3839
Guido van Rossumb90c8482007-02-10 01:11:45 +00003840static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003841 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3842 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003843 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003844};
3845
3846PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003847 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3848 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06003849 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003850 0, /* tp_itemsize */
3851 /* methods */
3852 (destructor)dictview_dealloc, /* tp_dealloc */
3853 0, /* tp_print */
3854 0, /* tp_getattr */
3855 0, /* tp_setattr */
3856 0, /* tp_reserved */
3857 (reprfunc)dictview_repr, /* tp_repr */
3858 &dictviews_as_number, /* tp_as_number */
3859 &dictkeys_as_sequence, /* tp_as_sequence */
3860 0, /* tp_as_mapping */
3861 0, /* tp_hash */
3862 0, /* tp_call */
3863 0, /* tp_str */
3864 PyObject_GenericGetAttr, /* tp_getattro */
3865 0, /* tp_setattro */
3866 0, /* tp_as_buffer */
3867 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3868 0, /* tp_doc */
3869 (traverseproc)dictview_traverse, /* tp_traverse */
3870 0, /* tp_clear */
3871 dictview_richcompare, /* tp_richcompare */
3872 0, /* tp_weaklistoffset */
3873 (getiterfunc)dictkeys_iter, /* tp_iter */
3874 0, /* tp_iternext */
3875 dictkeys_methods, /* tp_methods */
3876 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003877};
3878
3879static PyObject *
3880dictkeys_new(PyObject *dict)
3881{
Eric Snow96c6af92015-05-29 22:21:39 -06003882 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003883}
3884
Guido van Rossum3ac67412007-02-10 18:55:06 +00003885/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003886
3887static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003888dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003889{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003890 if (dv->dv_dict == NULL) {
3891 Py_RETURN_NONE;
3892 }
3893 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003894}
3895
3896static int
Eric Snow96c6af92015-05-29 22:21:39 -06003897dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00003898{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003899 PyObject *key, *value, *found;
3900 if (dv->dv_dict == NULL)
3901 return 0;
3902 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
3903 return 0;
3904 key = PyTuple_GET_ITEM(obj, 0);
3905 value = PyTuple_GET_ITEM(obj, 1);
3906 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
3907 if (found == NULL) {
3908 if (PyErr_Occurred())
3909 return -1;
3910 return 0;
3911 }
3912 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003913}
3914
Guido van Rossum83825ac2007-02-10 04:54:19 +00003915static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003916 (lenfunc)dictview_len, /* sq_length */
3917 0, /* sq_concat */
3918 0, /* sq_repeat */
3919 0, /* sq_item */
3920 0, /* sq_slice */
3921 0, /* sq_ass_item */
3922 0, /* sq_ass_slice */
3923 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003924};
3925
Guido van Rossumb90c8482007-02-10 01:11:45 +00003926static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003927 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3928 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003929 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003930};
3931
3932PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003933 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3934 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06003935 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003936 0, /* tp_itemsize */
3937 /* methods */
3938 (destructor)dictview_dealloc, /* tp_dealloc */
3939 0, /* tp_print */
3940 0, /* tp_getattr */
3941 0, /* tp_setattr */
3942 0, /* tp_reserved */
3943 (reprfunc)dictview_repr, /* tp_repr */
3944 &dictviews_as_number, /* tp_as_number */
3945 &dictitems_as_sequence, /* tp_as_sequence */
3946 0, /* tp_as_mapping */
3947 0, /* tp_hash */
3948 0, /* tp_call */
3949 0, /* tp_str */
3950 PyObject_GenericGetAttr, /* tp_getattro */
3951 0, /* tp_setattro */
3952 0, /* tp_as_buffer */
3953 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3954 0, /* tp_doc */
3955 (traverseproc)dictview_traverse, /* tp_traverse */
3956 0, /* tp_clear */
3957 dictview_richcompare, /* tp_richcompare */
3958 0, /* tp_weaklistoffset */
3959 (getiterfunc)dictitems_iter, /* tp_iter */
3960 0, /* tp_iternext */
3961 dictitems_methods, /* tp_methods */
3962 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003963};
3964
3965static PyObject *
3966dictitems_new(PyObject *dict)
3967{
Eric Snow96c6af92015-05-29 22:21:39 -06003968 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003969}
3970
Guido van Rossum3ac67412007-02-10 18:55:06 +00003971/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003972
3973static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003974dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003975{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003976 if (dv->dv_dict == NULL) {
3977 Py_RETURN_NONE;
3978 }
3979 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003980}
3981
Guido van Rossum83825ac2007-02-10 04:54:19 +00003982static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003983 (lenfunc)dictview_len, /* sq_length */
3984 0, /* sq_concat */
3985 0, /* sq_repeat */
3986 0, /* sq_item */
3987 0, /* sq_slice */
3988 0, /* sq_ass_item */
3989 0, /* sq_ass_slice */
3990 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003991};
3992
Guido van Rossumb90c8482007-02-10 01:11:45 +00003993static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003994 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003995};
3996
3997PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003998 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3999 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06004000 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004001 0, /* tp_itemsize */
4002 /* methods */
4003 (destructor)dictview_dealloc, /* tp_dealloc */
4004 0, /* tp_print */
4005 0, /* tp_getattr */
4006 0, /* tp_setattr */
4007 0, /* tp_reserved */
4008 (reprfunc)dictview_repr, /* tp_repr */
4009 0, /* tp_as_number */
4010 &dictvalues_as_sequence, /* tp_as_sequence */
4011 0, /* tp_as_mapping */
4012 0, /* tp_hash */
4013 0, /* tp_call */
4014 0, /* tp_str */
4015 PyObject_GenericGetAttr, /* tp_getattro */
4016 0, /* tp_setattro */
4017 0, /* tp_as_buffer */
4018 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
4019 0, /* tp_doc */
4020 (traverseproc)dictview_traverse, /* tp_traverse */
4021 0, /* tp_clear */
4022 0, /* tp_richcompare */
4023 0, /* tp_weaklistoffset */
4024 (getiterfunc)dictvalues_iter, /* tp_iter */
4025 0, /* tp_iternext */
4026 dictvalues_methods, /* tp_methods */
4027 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00004028};
4029
4030static PyObject *
4031dictvalues_new(PyObject *dict)
4032{
Eric Snow96c6af92015-05-29 22:21:39 -06004033 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00004034}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004035
4036/* Returns NULL if cannot allocate a new PyDictKeysObject,
4037 but does not set an error */
4038PyDictKeysObject *
4039_PyDict_NewKeysForClass(void)
4040{
Victor Stinner742da042016-09-07 17:40:12 -07004041 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004042 if (keys == NULL)
4043 PyErr_Clear();
4044 else
4045 keys->dk_lookup = lookdict_split;
4046 return keys;
4047}
4048
4049#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
4050
4051PyObject *
4052PyObject_GenericGetDict(PyObject *obj, void *context)
4053{
4054 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
4055 if (dictptr == NULL) {
4056 PyErr_SetString(PyExc_AttributeError,
4057 "This object has no __dict__");
4058 return NULL;
4059 }
4060 dict = *dictptr;
4061 if (dict == NULL) {
4062 PyTypeObject *tp = Py_TYPE(obj);
4063 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
4064 DK_INCREF(CACHED_KEYS(tp));
4065 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
4066 }
4067 else {
4068 *dictptr = dict = PyDict_New();
4069 }
4070 }
4071 Py_XINCREF(dict);
4072 return dict;
4073}
4074
4075int
4076_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
Victor Stinner742da042016-09-07 17:40:12 -07004077 PyObject *key, PyObject *value)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004078{
4079 PyObject *dict;
4080 int res;
4081 PyDictKeysObject *cached;
4082
4083 assert(dictptr != NULL);
4084 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
4085 assert(dictptr != NULL);
4086 dict = *dictptr;
4087 if (dict == NULL) {
4088 DK_INCREF(cached);
4089 dict = new_dict_with_shared_keys(cached);
4090 if (dict == NULL)
4091 return -1;
4092 *dictptr = dict;
4093 }
4094 if (value == NULL) {
4095 res = PyDict_DelItem(dict, key);
4096 if (cached != ((PyDictObject *)dict)->ma_keys) {
4097 CACHED_KEYS(tp) = NULL;
4098 DK_DECREF(cached);
4099 }
4100 } else {
4101 res = PyDict_SetItem(dict, key, value);
4102 if (cached != ((PyDictObject *)dict)->ma_keys) {
4103 /* Either update tp->ht_cached_keys or delete it */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004104 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004105 CACHED_KEYS(tp) = make_keys_shared(dict);
Victor Stinner742da042016-09-07 17:40:12 -07004106 }
4107 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004108 CACHED_KEYS(tp) = NULL;
4109 }
4110 DK_DECREF(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04004111 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
4112 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04004113 }
4114 }
4115 } else {
4116 dict = *dictptr;
4117 if (dict == NULL) {
4118 dict = PyDict_New();
4119 if (dict == NULL)
4120 return -1;
4121 *dictptr = dict;
4122 }
4123 if (value == NULL) {
4124 res = PyDict_DelItem(dict, key);
4125 } else {
4126 res = PyDict_SetItem(dict, key, value);
4127 }
4128 }
4129 return res;
4130}
4131
4132void
4133_PyDictKeys_DecRef(PyDictKeysObject *keys)
4134{
4135 DK_DECREF(keys);
4136}