blob: 7a3ed42f0f38c5391ccee7badb267d72e47c98df [file] [log] [blame]
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001
Guido van Rossum2bc13791999-03-24 19:06:42 +00002/* Dictionary object implementation using a hash table */
Guido van Rossum9bfef441993-03-29 10:43:31 +00003
Raymond Hettinger930427b2003-05-03 06:51:59 +00004/* The distribution includes a separate file, Objects/dictnotes.txt,
Tim Peters60b29962006-01-01 01:19:23 +00005 describing explorations into dictionary design and optimization.
Raymond Hettinger930427b2003-05-03 06:51:59 +00006 It covers typical dictionary use patterns, the parameters for
7 tuning dictionaries, and several ideas for possible optimizations.
8*/
9
Benjamin Peterson7d95e402012-04-23 11:24:50 -040010
11/*
12There are four kinds of slots in the table:
13
141. Unused. me_key == me_value == NULL
15 Does not hold an active (key, value) pair now and never did. Unused can
16 transition to Active upon key insertion. This is the only case in which
17 me_key is NULL, and is each slot's initial state.
18
192. Active. me_key != NULL and me_key != dummy and me_value != NULL
20 Holds an active (key, value) pair. Active can transition to Dummy or
21 Pending upon key deletion (for combined and split tables respectively).
22 This is the only case in which me_value != NULL.
23
243. Dummy. me_key == dummy and me_value == NULL
25 Previously held an active (key, value) pair, but that was deleted and an
26 active pair has not yet overwritten the slot. Dummy can transition to
27 Active upon key insertion. Dummy slots cannot be made Unused again
28 (cannot have me_key set to NULL), else the probe sequence in case of
29 collision would have no way to know they were once active.
30
314. Pending. Not yet inserted or deleted from a split-table.
32 key != NULL, key != dummy and value == NULL
33
34The DictObject can be in one of two forms.
35Either:
36 A combined table:
37 ma_values == NULL, dk_refcnt == 1.
38 Values are stored in the me_value field of the PyDictKeysObject.
39 Slot kind 4 is not allowed i.e.
40 key != NULL, key != dummy and value == NULL is illegal.
41Or:
42 A split table:
43 ma_values != NULL, dk_refcnt >= 1
44 Values are stored in the ma_values array.
45 Only string (unicode) keys are allowed, no <dummy> keys are present.
46
47Note: .popitem() abuses the me_hash field of an Unused or Dummy slot to
48hold a search finger. The me_hash field of Unused or Dummy slots has no
49meaning otherwise. As a consequence of this popitem always converts the dict
50to the combined-table form.
51*/
52
53/* PyDict_MINSIZE_SPLIT is the minimum size of a split dictionary.
54 * It must be a power of 2, and at least 4.
55 * Resizing of split dictionaries is very rare, so the saving memory is more
56 * important than the cost of resizing.
57 */
58#define PyDict_MINSIZE_SPLIT 4
59
60/* PyDict_MINSIZE_COMBINED is the starting size for any new, non-split dict.
61 * 8 allows dicts with no more than 5 active entries; experiments suggested
62 * this suffices for the majority of dicts (consisting mostly of usually-small
63 * dicts created to pass keyword arguments).
64 * Making this 8, rather than 4 reduces the number of resizes for most
65 * dictionaries, without any significant extra memory use.
66 */
67#define PyDict_MINSIZE_COMBINED 8
68
Guido van Rossumc0b618a1997-05-02 03:12:38 +000069#include "Python.h"
Eric Snow96c6af92015-05-29 22:21:39 -060070#include "dict-common.h"
Christian Heimes0ded5b52007-12-10 15:50:56 +000071#include "stringlib/eq.h"
Guido van Rossum4b1302b1993-03-27 18:11:32 +000072
Larry Hastings61272b72014-01-07 12:41:53 -080073/*[clinic input]
Larry Hastingsc2047262014-01-25 20:43:29 -080074class dict "PyDictObject *" "&PyDict_Type"
Larry Hastings61272b72014-01-07 12:41:53 -080075[clinic start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -080076/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f157a5a0ce9589d6]*/
Larry Hastings44e2eaa2013-11-23 15:37:55 -080077
Benjamin Peterson7d95e402012-04-23 11:24:50 -040078
79/*
80To ensure the lookup algorithm terminates, there must be at least one Unused
81slot (NULL key) in the table.
82To avoid slowing down lookups on a near-full table, we resize the table when
83it's USABLE_FRACTION (currently two-thirds) full.
84*/
Guido van Rossum16e93a81997-01-28 00:00:11 +000085
Tim Peterseb28ef22001-06-02 05:27:19 +000086#define PERTURB_SHIFT 5
87
Guido van Rossum16e93a81997-01-28 00:00:11 +000088/*
Tim Peterseb28ef22001-06-02 05:27:19 +000089Major subtleties ahead: Most hash schemes depend on having a "good" hash
90function, in the sense of simulating randomness. Python doesn't: its most
R David Murray537ad7a2016-07-10 12:33:18 -040091important hash functions (for ints) are very regular in common
Tim Peterseb28ef22001-06-02 05:27:19 +000092cases:
Tim Peters15d49292001-05-27 07:39:22 +000093
R David Murray537ad7a2016-07-10 12:33:18 -040094 >>>[hash(i) for i in range(4)]
Guido van Rossumdc5f6b22006-08-24 21:29:26 +000095 [0, 1, 2, 3]
Tim Peters15d49292001-05-27 07:39:22 +000096
Tim Peterseb28ef22001-06-02 05:27:19 +000097This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
98the low-order i bits as the initial table index is extremely fast, and there
R David Murray537ad7a2016-07-10 12:33:18 -040099are no collisions at all for dicts indexed by a contiguous range of ints. So
100this gives better-than-random behavior in common cases, and that's very
101desirable.
Tim Peters15d49292001-05-27 07:39:22 +0000102
Tim Peterseb28ef22001-06-02 05:27:19 +0000103OTOH, when collisions occur, the tendency to fill contiguous slices of the
104hash table makes a good collision resolution strategy crucial. Taking only
105the last i bits of the hash code is also vulnerable: for example, consider
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000107their own hash codes, and this fits in a dict of size 2**15, the last 15 bits
108 of every hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +0000109
Tim Peterseb28ef22001-06-02 05:27:19 +0000110But catering to unusual cases should not slow the usual ones, so we just take
111the last i bits anyway. It's up to collision resolution to do the rest. If
112we *usually* find the key we're looking for on the first try (and, it turns
113out, we usually do -- the table load factor is kept under 2/3, so the odds
114are solidly in our favor), then it makes best sense to keep the initial index
115computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +0000116
Tim Peterseb28ef22001-06-02 05:27:19 +0000117The first half of collision resolution is to visit table indices via this
118recurrence:
Tim Peters15d49292001-05-27 07:39:22 +0000119
Tim Peterseb28ef22001-06-02 05:27:19 +0000120 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +0000121
Tim Peterseb28ef22001-06-02 05:27:19 +0000122For any initial j in range(2**i), repeating that 2**i times generates each
123int in range(2**i) exactly once (see any text on random-number generation for
124proof). By itself, this doesn't help much: like linear probing (setting
125j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
126order. This would be bad, except that's not the only thing we do, and it's
127actually *good* in the common cases where hash keys are consecutive. In an
128example that's really too small to make this entirely clear, for a table of
129size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +0000130
Tim Peterseb28ef22001-06-02 05:27:19 +0000131 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
132
133If two things come in at index 5, the first place we look after is index 2,
134not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
135Linear probing is deadly in this case because there the fixed probe order
136is the *same* as the order consecutive keys are likely to arrive. But it's
137extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
138and certain that consecutive hash codes do not.
139
140The other half of the strategy is to get the other bits of the hash code
141into play. This is done by initializing a (unsigned) vrbl "perturb" to the
142full hash code, and changing the recurrence to:
143
144 j = (5*j) + 1 + perturb;
145 perturb >>= PERTURB_SHIFT;
146 use j % 2**i as the next table index;
147
148Now the probe sequence depends (eventually) on every bit in the hash code,
149and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
150because it quickly magnifies small differences in the bits that didn't affect
151the initial index. Note that because perturb is unsigned, if the recurrence
152is executed often enough perturb eventually becomes and remains 0. At that
153point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
154that's certain to find an empty slot eventually (since it generates every int
155in range(2**i), and we make sure there's always at least one empty slot).
156
157Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
158small so that the high bits of the hash code continue to affect the probe
159sequence across iterations; but you want it large so that in really bad cases
160the high-order hash bits have an effect on early iterations. 5 was "the
161best" in minimizing total collisions across experiments Tim Peters ran (on
162both normal and pathological cases), but 4 and 6 weren't significantly worse.
163
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000164Historical: Reimer Behrends contributed the idea of using a polynomial-based
Tim Peterseb28ef22001-06-02 05:27:19 +0000165approach, using repeated multiplication by x in GF(2**n) where an irreducible
166polynomial for each table size was chosen such that x was a primitive root.
167Christian Tismer later extended that to use division by x instead, as an
168efficient way to get the high bits of the hash code into play. This scheme
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000169also gave excellent collision statistics, but was more expensive: two
170if-tests were required inside the loop; computing "the next" index took about
171the same number of operations but without as much potential parallelism
172(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
173above, and then shifting perturb can be done while the table index is being
174masked); and the PyDictObject struct required a member to hold the table's
175polynomial. In Tim's experiments the current scheme ran faster, produced
176equally good collision statistics, needed less code & used less memory.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000177
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000178*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000179
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400180/* Object used as dummy key to fill deleted entries
181 * This could be any unique object,
182 * use a custom type in order to minimise coupling.
183*/
184static PyObject _dummy_struct;
185
186#define dummy (&_dummy_struct)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000187
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000188#ifdef Py_REF_DEBUG
189PyObject *
190_PyDict_Dummy(void)
191{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 return dummy;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000193}
194#endif
195
Fred Drake1bff34a2000-08-31 19:31:38 +0000196/* forward declarations */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400197static PyDictKeyEntry *lookdict(PyDictObject *mp, PyObject *key,
198 Py_hash_t hash, PyObject ***value_addr);
199static PyDictKeyEntry *lookdict_unicode(PyDictObject *mp, PyObject *key,
200 Py_hash_t hash, PyObject ***value_addr);
201static PyDictKeyEntry *
202lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
203 Py_hash_t hash, PyObject ***value_addr);
204static PyDictKeyEntry *lookdict_split(PyDictObject *mp, PyObject *key,
205 Py_hash_t hash, PyObject ***value_addr);
Fred Drake1bff34a2000-08-31 19:31:38 +0000206
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400207static int dictresize(PyDictObject *mp, Py_ssize_t minused);
Tim Petersdea48ec2001-05-22 20:40:22 +0000208
Raymond Hettinger43442782004-03-17 21:55:03 +0000209/* Dictionary reuse scheme to save calls to malloc, free, and memset */
Christian Heimes2202f872008-02-06 14:31:34 +0000210#ifndef PyDict_MAXFREELIST
211#define PyDict_MAXFREELIST 80
212#endif
213static PyDictObject *free_list[PyDict_MAXFREELIST];
214static int numfree = 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000215
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300216#include "clinic/dictobject.c.h"
217
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100218int
219PyDict_ClearFreeList(void)
Christian Heimes77c02eb2008-02-09 02:18:51 +0000220{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 PyDictObject *op;
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100222 int ret = numfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 while (numfree) {
224 op = free_list[--numfree];
225 assert(PyDict_CheckExact(op));
226 PyObject_GC_Del(op);
227 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100228 return ret;
229}
230
David Malcolm49526f42012-06-22 14:55:41 -0400231/* Print summary info about the state of the optimized allocator */
232void
233_PyDict_DebugMallocStats(FILE *out)
234{
235 _PyDebugAllocatorStats(out,
236 "free PyDictObject", numfree, sizeof(PyDictObject));
237}
238
239
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100240void
241PyDict_Fini(void)
242{
243 PyDict_ClearFreeList();
Christian Heimes77c02eb2008-02-09 02:18:51 +0000244}
245
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200246#define DK_DEBUG_INCREF _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA
247#define DK_DEBUG_DECREF _Py_DEC_REFTOTAL _Py_REF_DEBUG_COMMA
248
249#define DK_INCREF(dk) (DK_DEBUG_INCREF ++(dk)->dk_refcnt)
250#define DK_DECREF(dk) if (DK_DEBUG_DECREF (--(dk)->dk_refcnt) == 0) free_keys_object(dk)
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400251#define DK_SIZE(dk) ((dk)->dk_size)
252#define DK_MASK(dk) (((dk)->dk_size)-1)
253#define IS_POWER_OF_2(x) (((x) & (x-1)) == 0)
254
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200255/* USABLE_FRACTION is the maximum dictionary load.
256 * Currently set to (2n+1)/3. Increasing this ratio makes dictionaries more
257 * dense resulting in more collisions. Decreasing it improves sparseness
258 * at the expense of spreading entries over more cache lines and at the
259 * cost of total memory consumed.
260 *
261 * USABLE_FRACTION must obey the following:
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400262 * (0 < USABLE_FRACTION(n) < n) for all n >= 2
263 *
264 * USABLE_FRACTION should be very quick to calculate.
265 * Fractions around 5/8 to 2/3 seem to work well in practice.
266 */
267
268/* Use (2n+1)/3 rather than 2n+3 because: it makes no difference for
269 * combined tables (the two fractions round to the same number n < ),
270 * but 2*4/3 is 2 whereas (2*4+1)/3 is 3 which potentially saves quite
271 * a lot of space for small, split tables */
272#define USABLE_FRACTION(n) ((((n) << 1)+1)/3)
273
274/* Alternative fraction that is otherwise close enough to (2n+1)/3 to make
275 * little difference. 8 * 2/3 == 8 * 5/8 == 5. 16 * 2/3 == 16 * 5/8 == 10.
276 * 32 * 2/3 = 21, 32 * 5/8 = 20.
277 * Its advantage is that it is faster to compute on machines with slow division.
278 * #define USABLE_FRACTION(n) (((n) >> 1) + ((n) >> 2) - ((n) >> 3))
279*/
280
Victor Stinnera9f61a52013-07-16 22:17:26 +0200281/* GROWTH_RATE. Growth rate upon hitting maximum load.
282 * Currently set to used*2 + capacity/2.
283 * This means that dicts double in size when growing without deletions,
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700284 * but have more head room when the number of deletions is on a par with the
285 * number of insertions.
286 * Raising this to used*4 doubles memory consumption depending on the size of
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200287 * the dictionary, but results in half the number of resizes, less effort to
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700288 * resize.
289 * GROWTH_RATE was set to used*4 up to version 3.2.
290 * GROWTH_RATE was set to used*2 in version 3.3.0
Antoine Pitroua504a7a2012-06-24 21:03:45 +0200291 */
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700292#define GROWTH_RATE(d) (((d)->ma_used*2)+((d)->ma_keys->dk_size>>1))
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400293
294#define ENSURE_ALLOWS_DELETIONS(d) \
295 if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \
296 (d)->ma_keys->dk_lookup = lookdict_unicode; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400298
299/* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
300 * (which cannot fail and thus can do no allocation).
301 */
302static PyDictKeysObject empty_keys_struct = {
303 2, /* dk_refcnt 1 for this struct, 1 for dummy_struct */
304 1, /* dk_size */
305 lookdict_split, /* dk_lookup */
306 0, /* dk_usable (immutable) */
307 {
308 { 0, 0, 0 } /* dk_entries (empty) */
309 }
310};
311
312static PyObject *empty_values[1] = { NULL };
313
314#define Py_EMPTY_KEYS &empty_keys_struct
315
316static PyDictKeysObject *new_keys_object(Py_ssize_t size)
317{
318 PyDictKeysObject *dk;
319 Py_ssize_t i;
320 PyDictKeyEntry *ep0;
321
322 assert(size >= PyDict_MINSIZE_SPLIT);
323 assert(IS_POWER_OF_2(size));
Raymond Hettingerce5179f2016-01-31 08:56:21 -0800324 dk = PyObject_MALLOC(sizeof(PyDictKeysObject) +
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400325 sizeof(PyDictKeyEntry) * (size-1));
326 if (dk == NULL) {
327 PyErr_NoMemory();
328 return NULL;
329 }
Antoine Pitrou2d169b22012-05-12 23:43:44 +0200330 DK_DEBUG_INCREF dk->dk_refcnt = 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400331 dk->dk_size = size;
332 dk->dk_usable = USABLE_FRACTION(size);
333 ep0 = &dk->dk_entries[0];
334 /* Hash value of slot 0 is used by popitem, so it must be initialized */
335 ep0->me_hash = 0;
336 for (i = 0; i < size; i++) {
337 ep0[i].me_key = NULL;
338 ep0[i].me_value = NULL;
339 }
340 dk->dk_lookup = lookdict_unicode_nodummy;
341 return dk;
342}
343
344static void
345free_keys_object(PyDictKeysObject *keys)
346{
347 PyDictKeyEntry *entries = &keys->dk_entries[0];
348 Py_ssize_t i, n;
349 for (i = 0, n = DK_SIZE(keys); i < n; i++) {
350 Py_XDECREF(entries[i].me_key);
351 Py_XDECREF(entries[i].me_value);
352 }
Raymond Hettingerce5179f2016-01-31 08:56:21 -0800353 PyObject_FREE(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400354}
355
356#define new_values(size) PyMem_NEW(PyObject *, size)
357
358#define free_values(values) PyMem_FREE(values)
359
360/* Consumes a reference to the keys object */
361static PyObject *
362new_dict(PyDictKeysObject *keys, PyObject **values)
363{
364 PyDictObject *mp;
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200365 assert(keys != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 if (numfree) {
367 mp = free_list[--numfree];
368 assert (mp != NULL);
369 assert (Py_TYPE(mp) == &PyDict_Type);
370 _Py_NewReference((PyObject *)mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400372 else {
373 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
374 if (mp == NULL) {
375 DK_DECREF(keys);
376 free_values(values);
377 return NULL;
378 }
379 }
380 mp->ma_keys = keys;
381 mp->ma_values = values;
382 mp->ma_used = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000384}
385
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400386/* Consumes a reference to the keys object */
387static PyObject *
388new_dict_with_shared_keys(PyDictKeysObject *keys)
389{
390 PyObject **values;
391 Py_ssize_t i, size;
392
393 size = DK_SIZE(keys);
394 values = new_values(size);
395 if (values == NULL) {
396 DK_DECREF(keys);
397 return PyErr_NoMemory();
398 }
399 for (i = 0; i < size; i++) {
400 values[i] = NULL;
401 }
402 return new_dict(keys, values);
403}
404
405PyObject *
406PyDict_New(void)
407{
Victor Stinnerc9b7f512013-07-08 22:19:20 +0200408 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE_COMBINED);
409 if (keys == NULL)
410 return NULL;
411 return new_dict(keys, NULL);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400412}
413
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000414/*
415The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000416This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000417Open addressing is preferred over chaining since the link overhead for
418chaining would be substantial (100% with typical malloc overhead).
419
Tim Peterseb28ef22001-06-02 05:27:19 +0000420The initial probe index is computed as hash mod the table size. Subsequent
421probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000422
423All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000424
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000425The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000426contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000427Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000428
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000429lookdict() is general-purpose, and may return NULL if (and only if) a
430comparison raises an exception (this was new in Python 2.5).
Guido van Rossum89d8c602007-09-18 17:26:56 +0000431lookdict_unicode() below is specialized to string keys, comparison of which can
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400432never raise an exception; that function can never return NULL.
433lookdict_unicode_nodummy is further specialized for string keys that cannot be
434the <dummy> value.
435For both, when the key isn't found a PyDictEntry* is returned
436where the key would have been found, *value_addr points to the matching value
437slot.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000438*/
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400439static PyDictKeyEntry *
440lookdict(PyDictObject *mp, PyObject *key,
441 Py_hash_t hash, PyObject ***value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000442{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200443 size_t i;
444 size_t perturb;
445 PyDictKeyEntry *freeslot;
446 size_t mask;
Antoine Pitrou9a234902012-05-13 20:48:01 +0200447 PyDictKeyEntry *ep0;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200448 PyDictKeyEntry *ep;
449 int cmp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000451
Antoine Pitrou9a234902012-05-13 20:48:01 +0200452top:
453 mask = DK_MASK(mp->ma_keys);
454 ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 i = (size_t)hash & mask;
456 ep = &ep0[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400457 if (ep->me_key == NULL || ep->me_key == key) {
458 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400460 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 if (ep->me_key == dummy)
462 freeslot = ep;
463 else {
464 if (ep->me_hash == hash) {
465 startkey = ep->me_key;
466 Py_INCREF(startkey);
467 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
468 Py_DECREF(startkey);
469 if (cmp < 0)
470 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400471 if (ep0 == mp->ma_keys->dk_entries && ep->me_key == startkey) {
472 if (cmp > 0) {
473 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400475 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 }
477 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200478 /* The dict was mutated, restart */
479 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 }
481 }
482 freeslot = NULL;
483 }
Tim Peters15d49292001-05-27 07:39:22 +0000484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 /* In the loop, me_key == dummy is by far (factor of 100s) the
486 least likely outcome, so test for that last. */
487 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
488 i = (i << 2) + i + perturb + 1;
489 ep = &ep0[i & mask];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400490 if (ep->me_key == NULL) {
491 if (freeslot == NULL) {
492 *value_addr = &ep->me_value;
493 return ep;
494 } else {
495 *value_addr = &freeslot->me_value;
496 return freeslot;
497 }
498 }
499 if (ep->me_key == key) {
500 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400502 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 if (ep->me_hash == hash && ep->me_key != dummy) {
504 startkey = ep->me_key;
505 Py_INCREF(startkey);
506 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
507 Py_DECREF(startkey);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400508 if (cmp < 0) {
509 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400511 }
512 if (ep0 == mp->ma_keys->dk_entries && ep->me_key == startkey) {
513 if (cmp > 0) {
514 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400516 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 }
518 else {
Antoine Pitrou9a234902012-05-13 20:48:01 +0200519 /* The dict was mutated, restart */
520 goto top;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 }
522 }
523 else if (ep->me_key == dummy && freeslot == NULL)
524 freeslot = ep;
525 }
526 assert(0); /* NOT REACHED */
527 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000528}
529
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400530/* Specialized version for string-only keys */
531static PyDictKeyEntry *
532lookdict_unicode(PyDictObject *mp, PyObject *key,
533 Py_hash_t hash, PyObject ***value_addr)
Fred Drake1bff34a2000-08-31 19:31:38 +0000534{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200535 size_t i;
536 size_t perturb;
537 PyDictKeyEntry *freeslot;
538 size_t mask = DK_MASK(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400539 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200540 PyDictKeyEntry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 /* Make sure this function doesn't have to handle non-unicode keys,
543 including subclasses of str; e.g., one reason to subclass
544 unicodes is to override __eq__, and for speed we don't cater to
545 that here. */
546 if (!PyUnicode_CheckExact(key)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400547 mp->ma_keys->dk_lookup = lookdict;
548 return lookdict(mp, key, hash, value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 }
Mark Dickinson57e683e2011-09-24 18:18:40 +0100550 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 ep = &ep0[i];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400552 if (ep->me_key == NULL || ep->me_key == key) {
553 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400555 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 if (ep->me_key == dummy)
557 freeslot = ep;
558 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400559 if (ep->me_hash == hash && unicode_eq(ep->me_key, key)) {
560 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400562 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 freeslot = NULL;
564 }
Tim Peters15d49292001-05-27 07:39:22 +0000565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 /* In the loop, me_key == dummy is by far (factor of 100s) the
567 least likely outcome, so test for that last. */
568 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
569 i = (i << 2) + i + perturb + 1;
570 ep = &ep0[i & mask];
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400571 if (ep->me_key == NULL) {
572 if (freeslot == NULL) {
573 *value_addr = &ep->me_value;
574 return ep;
575 } else {
576 *value_addr = &freeslot->me_value;
577 return freeslot;
578 }
579 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 if (ep->me_key == key
581 || (ep->me_hash == hash
582 && ep->me_key != dummy
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400583 && unicode_eq(ep->me_key, key))) {
584 *value_addr = &ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400586 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 if (ep->me_key == dummy && freeslot == NULL)
588 freeslot = ep;
589 }
590 assert(0); /* NOT REACHED */
591 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000592}
593
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400594/* Faster version of lookdict_unicode when it is known that no <dummy> keys
595 * will be present. */
596static PyDictKeyEntry *
597lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
598 Py_hash_t hash, PyObject ***value_addr)
599{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200600 size_t i;
601 size_t perturb;
602 size_t mask = DK_MASK(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400603 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200604 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400605
606 /* Make sure this function doesn't have to handle non-unicode keys,
607 including subclasses of str; e.g., one reason to subclass
608 unicodes is to override __eq__, and for speed we don't cater to
609 that here. */
610 if (!PyUnicode_CheckExact(key)) {
611 mp->ma_keys->dk_lookup = lookdict;
612 return lookdict(mp, key, hash, value_addr);
613 }
614 i = (size_t)hash & mask;
615 ep = &ep0[i];
616 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
617 if (ep->me_key == NULL || ep->me_key == key ||
618 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
619 *value_addr = &ep->me_value;
620 return ep;
621 }
622 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
623 i = (i << 2) + i + perturb + 1;
624 ep = &ep0[i & mask];
625 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
626 if (ep->me_key == NULL || ep->me_key == key ||
627 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
628 *value_addr = &ep->me_value;
629 return ep;
630 }
631 }
632 assert(0); /* NOT REACHED */
633 return 0;
634}
635
636/* Version of lookdict for split tables.
637 * All split tables and only split tables use this lookup function.
638 * Split tables only contain unicode keys and no dummy keys,
639 * so algorithm is the same as lookdict_unicode_nodummy.
640 */
641static PyDictKeyEntry *
642lookdict_split(PyDictObject *mp, PyObject *key,
643 Py_hash_t hash, PyObject ***value_addr)
644{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200645 size_t i;
646 size_t perturb;
647 size_t mask = DK_MASK(mp->ma_keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400648 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200649 PyDictKeyEntry *ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400650
651 if (!PyUnicode_CheckExact(key)) {
Benjamin Petersondb780d02012-04-23 13:44:32 -0400652 ep = lookdict(mp, key, hash, value_addr);
653 /* lookdict expects a combined-table, so fix value_addr */
654 i = ep - ep0;
655 *value_addr = &mp->ma_values[i];
656 return ep;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400657 }
658 i = (size_t)hash & mask;
659 ep = &ep0[i];
660 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
661 if (ep->me_key == NULL || ep->me_key == key ||
662 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
663 *value_addr = &mp->ma_values[i];
664 return ep;
665 }
666 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
667 i = (i << 2) + i + perturb + 1;
668 ep = &ep0[i & mask];
669 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
670 if (ep->me_key == NULL || ep->me_key == key ||
671 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
672 *value_addr = &mp->ma_values[i & mask];
673 return ep;
674 }
675 }
676 assert(0); /* NOT REACHED */
677 return 0;
678}
679
Benjamin Petersonfb886362010-04-24 18:21:17 +0000680int
681_PyDict_HasOnlyStringKeys(PyObject *dict)
682{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 Py_ssize_t pos = 0;
684 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000685 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 /* Shortcut */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400687 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 return 1;
689 while (PyDict_Next(dict, &pos, &key, &value))
690 if (!PyUnicode_Check(key))
691 return 0;
692 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000693}
694
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000695#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 do { \
697 if (!_PyObject_GC_IS_TRACKED(mp)) { \
698 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
699 _PyObject_GC_MAY_BE_TRACKED(value)) { \
700 _PyObject_GC_TRACK(mp); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 } \
702 } \
703 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000704
705void
706_PyDict_MaybeUntrack(PyObject *op)
707{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 PyDictObject *mp;
709 PyObject *value;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400710 Py_ssize_t i, size;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
713 return;
714
715 mp = (PyDictObject *) op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400716 size = DK_SIZE(mp->ma_keys);
717 if (_PyDict_HasSplitTable(mp)) {
718 for (i = 0; i < size; i++) {
719 if ((value = mp->ma_values[i]) == NULL)
720 continue;
721 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
722 assert(!_PyObject_GC_MAY_BE_TRACKED(
723 mp->ma_keys->dk_entries[i].me_key));
724 return;
725 }
726 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400728 else {
729 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
730 for (i = 0; i < size; i++) {
731 if ((value = ep0[i].me_value) == NULL)
732 continue;
733 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
734 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
735 return;
736 }
737 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000739}
740
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400741/* Internal function to find slot for an item from its hash
742 * when it is known that the key is not present in the dict.
743 */
744static PyDictKeyEntry *
745find_empty_slot(PyDictObject *mp, PyObject *key, Py_hash_t hash,
746 PyObject ***value_addr)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000747{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400748 size_t i;
749 size_t perturb;
750 size_t mask = DK_MASK(mp->ma_keys);
751 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
752 PyDictKeyEntry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000753
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400754 assert(key != NULL);
755 if (!PyUnicode_CheckExact(key))
756 mp->ma_keys->dk_lookup = lookdict;
757 i = hash & mask;
758 ep = &ep0[i];
759 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
760 i = (i << 2) + i + perturb + 1;
761 ep = &ep0[i & mask];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400763 assert(ep->me_value == NULL);
764 if (mp->ma_values)
765 *value_addr = &mp->ma_values[i & mask];
766 else
767 *value_addr = &ep->me_value;
768 return ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000769}
770
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400771static int
772insertion_resize(PyDictObject *mp)
773{
Raymond Hettinger36f74aa2013-05-17 03:01:13 -0700774 return dictresize(mp, GROWTH_RATE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400775}
Antoine Pitroue965d972012-02-27 00:45:12 +0100776
777/*
778Internal routine to insert a new item into the table.
779Used both by the internal resize routine and by the public insert routine.
Antoine Pitroue965d972012-02-27 00:45:12 +0100780Returns -1 if an error occurred, or 0 on success.
781*/
782static int
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400783insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +0100784{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400785 PyObject *old_value;
786 PyObject **value_addr;
787 PyDictKeyEntry *ep;
788 assert(key != dummy);
Antoine Pitroue965d972012-02-27 00:45:12 +0100789
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400790 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
791 if (insertion_resize(mp) < 0)
792 return -1;
793 }
794
795 ep = mp->ma_keys->dk_lookup(mp, key, hash, &value_addr);
Antoine Pitroue965d972012-02-27 00:45:12 +0100796 if (ep == NULL) {
Antoine Pitroue965d972012-02-27 00:45:12 +0100797 return -1;
798 }
Antoine Pitroud6967322014-10-18 00:35:00 +0200799 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400800 Py_INCREF(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400801 MAINTAIN_TRACKING(mp, key, value);
802 old_value = *value_addr;
803 if (old_value != NULL) {
804 assert(ep->me_key != NULL && ep->me_key != dummy);
805 *value_addr = value;
Antoine Pitroud6967322014-10-18 00:35:00 +0200806 Py_DECREF(old_value); /* which **CAN** re-enter (see issue #22653) */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400807 }
808 else {
809 if (ep->me_key == NULL) {
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400810 Py_INCREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400811 if (mp->ma_keys->dk_usable <= 0) {
812 /* Need to resize. */
813 if (insertion_resize(mp) < 0) {
814 Py_DECREF(key);
815 Py_DECREF(value);
816 return -1;
817 }
818 ep = find_empty_slot(mp, key, hash, &value_addr);
819 }
820 mp->ma_keys->dk_usable--;
821 assert(mp->ma_keys->dk_usable >= 0);
822 ep->me_key = key;
823 ep->me_hash = hash;
824 }
825 else {
826 if (ep->me_key == dummy) {
Benjamin Petersona6f195e2012-04-30 10:23:40 -0400827 Py_INCREF(key);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400828 ep->me_key = key;
829 ep->me_hash = hash;
830 Py_DECREF(dummy);
831 } else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400832 assert(_PyDict_HasSplitTable(mp));
833 }
834 }
835 mp->ma_used++;
836 *value_addr = value;
Antoine Pitroud6967322014-10-18 00:35:00 +0200837 assert(ep->me_key != NULL && ep->me_key != dummy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400838 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400839 return 0;
Antoine Pitroue965d972012-02-27 00:45:12 +0100840}
841
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000842/*
843Internal routine used by dictresize() to insert an item which is
844known to be absent from the dict. This routine also assumes that
845the dict contains no deleted entries. Besides the performance benefit,
846using insertdict() in dictresize() is dangerous (SF bug #1456209).
847Note that no refcounts are changed by this routine; if needed, the caller
848is responsible for incref'ing `key` and `value`.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400849Neither mp->ma_used nor k->dk_usable are modified by this routine; the caller
850must set them correctly
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000851*/
852static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400853insertdict_clean(PyDictObject *mp, PyObject *key, Py_hash_t hash,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 PyObject *value)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000855{
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400856 size_t i;
857 size_t perturb;
858 PyDictKeysObject *k = mp->ma_keys;
859 size_t mask = (size_t)DK_SIZE(k)-1;
860 PyDictKeyEntry *ep0 = &k->dk_entries[0];
861 PyDictKeyEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000862
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400863 assert(k->dk_lookup != NULL);
864 assert(value != NULL);
865 assert(key != NULL);
866 assert(key != dummy);
867 assert(PyUnicode_CheckExact(key) || k->dk_lookup == lookdict);
868 i = hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 ep = &ep0[i];
870 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
871 i = (i << 2) + i + perturb + 1;
872 ep = &ep0[i & mask];
873 }
874 assert(ep->me_value == NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 ep->me_key = key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000876 ep->me_hash = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000878}
879
880/*
881Restructure the table by allocating a new table and reinserting all
882items again. When entries have been deleted, the new table may
883actually be smaller than the old one.
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400884If a table is split (its keys and hashes are shared, its values are not),
885then the values are temporarily copied into the table, it is resized as
886a combined table, then the me_value slots in the old table are NULLed out.
887After resizing a table is always combined,
888but can be resplit by make_keys_shared().
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000889*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000890static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000891dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000892{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 Py_ssize_t newsize;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400894 PyDictKeysObject *oldkeys;
895 PyObject **oldvalues;
896 Py_ssize_t i, oldsize;
Tim Peters91a364d2001-05-19 07:04:38 +0000897
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400898/* Find the smallest table size > minused. */
899 for (newsize = PyDict_MINSIZE_COMBINED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 newsize <= minused && newsize > 0;
901 newsize <<= 1)
902 ;
903 if (newsize <= 0) {
904 PyErr_NoMemory();
905 return -1;
906 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400907 oldkeys = mp->ma_keys;
908 oldvalues = mp->ma_values;
909 /* Allocate a new table. */
910 mp->ma_keys = new_keys_object(newsize);
911 if (mp->ma_keys == NULL) {
912 mp->ma_keys = oldkeys;
913 return -1;
914 }
915 if (oldkeys->dk_lookup == lookdict)
916 mp->ma_keys->dk_lookup = lookdict;
917 oldsize = DK_SIZE(oldkeys);
918 mp->ma_values = NULL;
919 /* If empty then nothing to copy so just return */
920 if (oldsize == 1) {
921 assert(oldkeys == Py_EMPTY_KEYS);
922 DK_DECREF(oldkeys);
923 return 0;
924 }
925 /* Main loop below assumes we can transfer refcount to new keys
926 * and that value is stored in me_value.
927 * Increment ref-counts and copy values here to compensate
928 * This (resizing a split table) should be relatively rare */
929 if (oldvalues != NULL) {
930 for (i = 0; i < oldsize; i++) {
931 if (oldvalues[i] != NULL) {
932 Py_INCREF(oldkeys->dk_entries[i].me_key);
933 oldkeys->dk_entries[i].me_value = oldvalues[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 }
936 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400937 /* Main loop */
938 for (i = 0; i < oldsize; i++) {
939 PyDictKeyEntry *ep = &oldkeys->dk_entries[i];
940 if (ep->me_value != NULL) {
941 assert(ep->me_key != dummy);
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000942 insertdict_clean(mp, ep->me_key, ep->me_hash, ep->me_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400945 mp->ma_keys->dk_usable -= mp->ma_used;
946 if (oldvalues != NULL) {
947 /* NULL out me_value slot in oldkeys, in case it was shared */
948 for (i = 0; i < oldsize; i++)
949 oldkeys->dk_entries[i].me_value = NULL;
950 assert(oldvalues != empty_values);
951 free_values(oldvalues);
952 DK_DECREF(oldkeys);
953 }
954 else {
955 assert(oldkeys->dk_lookup != lookdict_split);
956 if (oldkeys->dk_lookup != lookdict_unicode_nodummy) {
957 PyDictKeyEntry *ep0 = &oldkeys->dk_entries[0];
958 for (i = 0; i < oldsize; i++) {
959 if (ep0[i].me_key == dummy)
960 Py_DECREF(dummy);
961 }
962 }
963 assert(oldkeys->dk_refcnt == 1);
Raymond Hettingerce5179f2016-01-31 08:56:21 -0800964 DK_DEBUG_DECREF PyObject_FREE(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400965 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000967}
968
Benjamin Peterson15ee8212012-04-24 14:44:18 -0400969/* Returns NULL if unable to split table.
970 * A NULL return does not necessarily indicate an error */
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400971static PyDictKeysObject *
972make_keys_shared(PyObject *op)
973{
974 Py_ssize_t i;
975 Py_ssize_t size;
976 PyDictObject *mp = (PyDictObject *)op;
977
Benjamin Peterson15ee8212012-04-24 14:44:18 -0400978 if (!PyDict_CheckExact(op))
979 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -0400980 if (!_PyDict_HasSplitTable(mp)) {
981 PyDictKeyEntry *ep0;
982 PyObject **values;
983 assert(mp->ma_keys->dk_refcnt == 1);
984 if (mp->ma_keys->dk_lookup == lookdict) {
985 return NULL;
986 }
987 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
988 /* Remove dummy keys */
989 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
990 return NULL;
991 }
992 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
993 /* Copy values into a new array */
994 ep0 = &mp->ma_keys->dk_entries[0];
995 size = DK_SIZE(mp->ma_keys);
996 values = new_values(size);
997 if (values == NULL) {
998 PyErr_SetString(PyExc_MemoryError,
999 "Not enough memory to allocate new values array");
1000 return NULL;
1001 }
1002 for (i = 0; i < size; i++) {
1003 values[i] = ep0[i].me_value;
1004 ep0[i].me_value = NULL;
1005 }
1006 mp->ma_keys->dk_lookup = lookdict_split;
1007 mp->ma_values = values;
1008 }
1009 DK_INCREF(mp->ma_keys);
1010 return mp->ma_keys;
1011}
Christian Heimes99170a52007-12-19 02:07:34 +00001012
1013PyObject *
1014_PyDict_NewPresized(Py_ssize_t minused)
1015{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001016 Py_ssize_t newsize;
1017 PyDictKeysObject *new_keys;
1018 for (newsize = PyDict_MINSIZE_COMBINED;
1019 newsize <= minused && newsize > 0;
1020 newsize <<= 1)
1021 ;
1022 new_keys = new_keys_object(newsize);
1023 if (new_keys == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001025 return new_dict(new_keys, NULL);
Christian Heimes99170a52007-12-19 02:07:34 +00001026}
1027
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001028/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
1029 * that may occur (originally dicts supported only string keys, and exceptions
1030 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001031 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001032 * (suppressed) occurred while computing the key's hash, or that some error
1033 * (suppressed) occurred when comparing keys in the dict's internal probe
1034 * sequence. A nasty example of the latter is when a Python-coded comparison
1035 * function hits a stack-depth error, which can cause this to return NULL
1036 * even if the key is present.
1037 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001038PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001039PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001040{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001041 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001043 PyDictKeyEntry *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 PyThreadState *tstate;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001045 PyObject **value_addr;
1046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 if (!PyDict_Check(op))
1048 return NULL;
1049 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001050 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 {
1052 hash = PyObject_Hash(key);
1053 if (hash == -1) {
1054 PyErr_Clear();
1055 return NULL;
1056 }
1057 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 /* We can arrive here with a NULL tstate during initialization: try
1060 running "python -Wi" for an example related to string interning.
1061 Let's just hope that no exception occurs then... This must be
1062 _PyThreadState_Current and not PyThreadState_GET() because in debug
1063 mode, the latter complains if tstate is NULL. */
Victor Stinnerbfd316e2016-01-20 11:12:38 +01001064 tstate = _PyThreadState_UncheckedGet();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 if (tstate != NULL && tstate->curexc_type != NULL) {
1066 /* preserve the existing exception */
1067 PyObject *err_type, *err_value, *err_tb;
1068 PyErr_Fetch(&err_type, &err_value, &err_tb);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001069 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 /* ignore errors */
1071 PyErr_Restore(err_type, err_value, err_tb);
1072 if (ep == NULL)
1073 return NULL;
1074 }
1075 else {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001076 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 if (ep == NULL) {
1078 PyErr_Clear();
1079 return NULL;
1080 }
1081 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001082 return *value_addr;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001083}
1084
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001085PyObject *
1086_PyDict_GetItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1087{
1088 PyDictObject *mp = (PyDictObject *)op;
1089 PyDictKeyEntry *ep;
1090 PyThreadState *tstate;
1091 PyObject **value_addr;
1092
1093 if (!PyDict_Check(op))
1094 return NULL;
1095
1096 /* We can arrive here with a NULL tstate during initialization: try
1097 running "python -Wi" for an example related to string interning.
1098 Let's just hope that no exception occurs then... This must be
1099 _PyThreadState_Current and not PyThreadState_GET() because in debug
1100 mode, the latter complains if tstate is NULL. */
Victor Stinnerbfd316e2016-01-20 11:12:38 +01001101 tstate = _PyThreadState_UncheckedGet();
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001102 if (tstate != NULL && tstate->curexc_type != NULL) {
1103 /* preserve the existing exception */
1104 PyObject *err_type, *err_value, *err_tb;
1105 PyErr_Fetch(&err_type, &err_value, &err_tb);
1106 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
1107 /* ignore errors */
1108 PyErr_Restore(err_type, err_value, err_tb);
1109 if (ep == NULL)
1110 return NULL;
1111 }
1112 else {
1113 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
1114 if (ep == NULL) {
1115 PyErr_Clear();
1116 return NULL;
1117 }
1118 }
1119 return *value_addr;
1120}
1121
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001122/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
1123 This returns NULL *with* an exception set if an exception occurred.
1124 It returns NULL *without* an exception set if the key wasn't present.
1125*/
1126PyObject *
1127PyDict_GetItemWithError(PyObject *op, PyObject *key)
1128{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001129 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 PyDictObject*mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001131 PyDictKeyEntry *ep;
1132 PyObject **value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001133
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 if (!PyDict_Check(op)) {
1135 PyErr_BadInternalCall();
1136 return NULL;
1137 }
1138 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001139 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 {
1141 hash = PyObject_Hash(key);
1142 if (hash == -1) {
1143 return NULL;
1144 }
1145 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001146
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001147 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 if (ep == NULL)
1149 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001150 return *value_addr;
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001151}
1152
Brett Cannonfd074152012-04-14 14:10:13 -04001153PyObject *
1154_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
1155{
1156 PyObject *kv;
1157 kv = _PyUnicode_FromId(key); /* borrowed */
1158 if (kv == NULL)
1159 return NULL;
1160 return PyDict_GetItemWithError(dp, kv);
1161}
1162
Victor Stinnerb4efc962015-11-20 09:24:02 +01001163/* Fast version of global value lookup (LOAD_GLOBAL).
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001164 * Lookup in globals, then builtins.
Victor Stinnerb4efc962015-11-20 09:24:02 +01001165 *
1166 * Raise an exception and return NULL if an error occurred (ex: computing the
1167 * key hash failed, key comparison failed, ...). Return NULL if the key doesn't
1168 * exist. Return the value if the key exists.
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001169 */
1170PyObject *
1171_PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001172{
Victor Stinnerb4efc962015-11-20 09:24:02 +01001173 Py_hash_t hash;
1174 PyDictKeyEntry *entry;
1175 PyObject **value_addr;
1176 PyObject *value;
1177
1178 if (!PyUnicode_CheckExact(key) ||
1179 (hash = ((PyASCIIObject *) key)->hash) == -1)
1180 {
1181 hash = PyObject_Hash(key);
1182 if (hash == -1)
1183 return NULL;
Antoine Pitroue965d972012-02-27 00:45:12 +01001184 }
Victor Stinnerb4efc962015-11-20 09:24:02 +01001185
1186 /* namespace 1: globals */
1187 entry = globals->ma_keys->dk_lookup(globals, key, hash, &value_addr);
1188 if (entry == NULL)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001189 return NULL;
Victor Stinnerb4efc962015-11-20 09:24:02 +01001190 value = *value_addr;
1191 if (value != NULL)
1192 return value;
1193
1194 /* namespace 2: builtins */
1195 entry = builtins->ma_keys->dk_lookup(builtins, key, hash, &value_addr);
1196 if (entry == NULL)
1197 return NULL;
1198 return *value_addr;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001199}
1200
Antoine Pitroue965d972012-02-27 00:45:12 +01001201/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
1202 * dictionary if it's merely replacing the value for an existing key.
1203 * This means that it's safe to loop over a dictionary with PyDict_Next()
1204 * and occasionally replace a value -- but you can't insert new keys or
1205 * remove them.
1206 */
1207int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001208PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
Antoine Pitroue965d972012-02-27 00:45:12 +01001209{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001210 PyDictObject *mp;
1211 Py_hash_t hash;
Antoine Pitroue965d972012-02-27 00:45:12 +01001212 if (!PyDict_Check(op)) {
1213 PyErr_BadInternalCall();
1214 return -1;
1215 }
1216 assert(key);
1217 assert(value);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001218 mp = (PyDictObject *)op;
1219 if (!PyUnicode_CheckExact(key) ||
1220 (hash = ((PyASCIIObject *) key)->hash) == -1)
1221 {
Antoine Pitroue965d972012-02-27 00:45:12 +01001222 hash = PyObject_Hash(key);
1223 if (hash == -1)
1224 return -1;
1225 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001226
1227 /* insertdict() handles any resizing that might be necessary */
1228 return insertdict(mp, key, hash, value);
Antoine Pitroue965d972012-02-27 00:45:12 +01001229}
1230
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001231int
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001232_PyDict_SetItem_KnownHash(PyObject *op, PyObject *key, PyObject *value,
1233 Py_hash_t hash)
1234{
1235 PyDictObject *mp;
1236
1237 if (!PyDict_Check(op)) {
1238 PyErr_BadInternalCall();
1239 return -1;
1240 }
1241 assert(key);
1242 assert(value);
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001243 assert(hash != -1);
Raymond Hettinger4b74fba2014-05-03 16:32:11 -07001244 mp = (PyDictObject *)op;
1245
1246 /* insertdict() handles any resizing that might be necessary */
1247 return insertdict(mp, key, hash, value);
1248}
1249
1250int
Tim Peters1f5871e2000-07-04 17:44:48 +00001251PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001252{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001253 PyDictObject *mp;
1254 Py_hash_t hash;
1255 PyDictKeyEntry *ep;
1256 PyObject *old_key, *old_value;
1257 PyObject **value_addr;
Guido van Rossumd7047b31995-01-02 19:07:15 +00001258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 if (!PyDict_Check(op)) {
1260 PyErr_BadInternalCall();
1261 return -1;
1262 }
1263 assert(key);
1264 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001265 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 hash = PyObject_Hash(key);
1267 if (hash == -1)
1268 return -1;
1269 }
1270 mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001271 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 if (ep == NULL)
1273 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001274 if (*value_addr == NULL) {
Raymond Hettinger69492da2013-09-02 15:59:26 -07001275 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 return -1;
1277 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001278 old_value = *value_addr;
1279 *value_addr = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001281 if (!_PyDict_HasSplitTable(mp)) {
1282 ENSURE_ALLOWS_DELETIONS(mp);
1283 old_key = ep->me_key;
1284 Py_INCREF(dummy);
1285 ep->me_key = dummy;
1286 Py_DECREF(old_key);
1287 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 Py_DECREF(old_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001290}
1291
Serhiy Storchakab9d98d52015-10-02 12:47:11 +03001292int
1293_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash)
1294{
1295 PyDictObject *mp;
1296 PyDictKeyEntry *ep;
1297 PyObject *old_key, *old_value;
1298 PyObject **value_addr;
1299
1300 if (!PyDict_Check(op)) {
1301 PyErr_BadInternalCall();
1302 return -1;
1303 }
1304 assert(key);
1305 assert(hash != -1);
1306 mp = (PyDictObject *)op;
1307 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
1308 if (ep == NULL)
1309 return -1;
1310 if (*value_addr == NULL) {
1311 _PyErr_SetKeyError(key);
1312 return -1;
1313 }
1314 old_value = *value_addr;
1315 *value_addr = NULL;
1316 mp->ma_used--;
1317 if (!_PyDict_HasSplitTable(mp)) {
1318 ENSURE_ALLOWS_DELETIONS(mp);
1319 old_key = ep->me_key;
1320 Py_INCREF(dummy);
1321 ep->me_key = dummy;
1322 Py_DECREF(old_key);
1323 }
1324 Py_DECREF(old_value);
1325 return 0;
1326}
1327
Guido van Rossum25831651993-05-19 14:50:45 +00001328void
Tim Peters1f5871e2000-07-04 17:44:48 +00001329PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001330{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 PyDictObject *mp;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001332 PyDictKeysObject *oldkeys;
1333 PyObject **oldvalues;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +00001335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 if (!PyDict_Check(op))
1337 return;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001338 mp = ((PyDictObject *)op);
1339 oldkeys = mp->ma_keys;
1340 oldvalues = mp->ma_values;
1341 if (oldvalues == empty_values)
1342 return;
1343 /* Empty the dict... */
1344 DK_INCREF(Py_EMPTY_KEYS);
1345 mp->ma_keys = Py_EMPTY_KEYS;
1346 mp->ma_values = empty_values;
1347 mp->ma_used = 0;
1348 /* ...then clear the keys and values */
1349 if (oldvalues != NULL) {
1350 n = DK_SIZE(oldkeys);
1351 for (i = 0; i < n; i++)
1352 Py_CLEAR(oldvalues[i]);
1353 free_values(oldvalues);
1354 DK_DECREF(oldkeys);
1355 }
1356 else {
1357 assert(oldkeys->dk_refcnt == 1);
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001358 DK_DECREF(oldkeys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001359 }
1360}
1361
1362/* Returns -1 if no more items (or op is not a dict),
1363 * index of item otherwise. Stores value in pvalue
1364 */
1365Py_LOCAL_INLINE(Py_ssize_t)
1366dict_next(PyObject *op, Py_ssize_t i, PyObject **pvalue)
1367{
1368 Py_ssize_t mask, offset;
1369 PyDictObject *mp;
1370 PyObject **value_ptr;
1371
1372
1373 if (!PyDict_Check(op))
1374 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001376 if (i < 0)
1377 return -1;
1378 if (mp->ma_values) {
1379 value_ptr = &mp->ma_values[i];
1380 offset = sizeof(PyObject *);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001382 else {
1383 value_ptr = &mp->ma_keys->dk_entries[i].me_value;
1384 offset = sizeof(PyDictKeyEntry);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001386 mask = DK_MASK(mp->ma_keys);
1387 while (i <= mask && *value_ptr == NULL) {
1388 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1389 i++;
1390 }
1391 if (i > mask)
1392 return -1;
1393 if (pvalue)
1394 *pvalue = *value_ptr;
1395 return i;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001396}
1397
Tim Peters080c88b2003-02-15 03:01:11 +00001398/*
1399 * Iterate over a dict. Use like so:
1400 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001401 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +00001402 * PyObject *key, *value;
1403 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +00001404 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +00001405 * Refer to borrowed references in key and value.
1406 * }
1407 *
1408 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +00001409 * mutates the dict. One exception: it is safe if the loop merely changes
1410 * the values associated with the keys (but doesn't insert new keys or
1411 * delete keys), via PyDict_SetItem().
1412 */
Guido van Rossum25831651993-05-19 14:50:45 +00001413int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001414PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001415{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001416 PyDictObject *mp;
1417 Py_ssize_t i = dict_next(op, *ppos, pvalue);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 if (i < 0)
1419 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001420 mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 *ppos = i+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 if (pkey)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001423 *pkey = mp->ma_keys->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001425}
1426
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001427/* Internal version of PyDict_Next that returns a hash value in addition
1428 * to the key and value.
1429 */
Thomas Wouterscf297e42007-02-23 15:07:44 +00001430int
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001431_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
1432 PyObject **pvalue, Py_hash_t *phash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00001433{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001434 PyDictObject *mp;
1435 Py_ssize_t i = dict_next(op, *ppos, pvalue);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 if (i < 0)
1437 return 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001438 mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 *ppos = i+1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001440 *phash = mp->ma_keys->dk_entries[i].me_hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 if (pkey)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001442 *pkey = mp->ma_keys->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 return 1;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001444}
1445
Eric Snow96c6af92015-05-29 22:21:39 -06001446/* Internal version of dict.pop(). */
1447PyObject *
1448_PyDict_Pop(PyDictObject *mp, PyObject *key, PyObject *deflt)
1449{
1450 Py_hash_t hash;
1451 PyObject *old_value, *old_key;
1452 PyDictKeyEntry *ep;
1453 PyObject **value_addr;
1454
1455 if (mp->ma_used == 0) {
1456 if (deflt) {
1457 Py_INCREF(deflt);
1458 return deflt;
1459 }
1460 _PyErr_SetKeyError(key);
1461 return NULL;
1462 }
1463 if (!PyUnicode_CheckExact(key) ||
1464 (hash = ((PyASCIIObject *) key)->hash) == -1) {
1465 hash = PyObject_Hash(key);
1466 if (hash == -1)
1467 return NULL;
1468 }
1469 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
1470 if (ep == NULL)
1471 return NULL;
1472 old_value = *value_addr;
1473 if (old_value == NULL) {
1474 if (deflt) {
1475 Py_INCREF(deflt);
1476 return deflt;
1477 }
1478 _PyErr_SetKeyError(key);
1479 return NULL;
1480 }
1481 *value_addr = NULL;
1482 mp->ma_used--;
1483 if (!_PyDict_HasSplitTable(mp)) {
1484 ENSURE_ALLOWS_DELETIONS(mp);
1485 old_key = ep->me_key;
1486 Py_INCREF(dummy);
1487 ep->me_key = dummy;
1488 Py_DECREF(old_key);
1489 }
1490 return old_value;
1491}
1492
1493/* Internal version of dict.from_keys(). It is subclass-friendly. */
1494PyObject *
1495_PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
1496{
1497 PyObject *it; /* iter(iterable) */
1498 PyObject *key;
1499 PyObject *d;
1500 int status;
1501
1502 d = PyObject_CallObject(cls, NULL);
1503 if (d == NULL)
1504 return NULL;
1505
1506 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
1507 if (PyDict_CheckExact(iterable)) {
1508 PyDictObject *mp = (PyDictObject *)d;
1509 PyObject *oldvalue;
1510 Py_ssize_t pos = 0;
1511 PyObject *key;
1512 Py_hash_t hash;
1513
1514 if (dictresize(mp, Py_SIZE(iterable))) {
1515 Py_DECREF(d);
1516 return NULL;
1517 }
1518
1519 while (_PyDict_Next(iterable, &pos, &key, &oldvalue, &hash)) {
1520 if (insertdict(mp, key, hash, value)) {
1521 Py_DECREF(d);
1522 return NULL;
1523 }
1524 }
1525 return d;
1526 }
1527 if (PyAnySet_CheckExact(iterable)) {
1528 PyDictObject *mp = (PyDictObject *)d;
1529 Py_ssize_t pos = 0;
1530 PyObject *key;
1531 Py_hash_t hash;
1532
1533 if (dictresize(mp, PySet_GET_SIZE(iterable))) {
1534 Py_DECREF(d);
1535 return NULL;
1536 }
1537
1538 while (_PySet_NextEntry(iterable, &pos, &key, &hash)) {
1539 if (insertdict(mp, key, hash, value)) {
1540 Py_DECREF(d);
1541 return NULL;
1542 }
1543 }
1544 return d;
1545 }
1546 }
1547
1548 it = PyObject_GetIter(iterable);
1549 if (it == NULL){
1550 Py_DECREF(d);
1551 return NULL;
1552 }
1553
1554 if (PyDict_CheckExact(d)) {
1555 while ((key = PyIter_Next(it)) != NULL) {
1556 status = PyDict_SetItem(d, key, value);
1557 Py_DECREF(key);
1558 if (status < 0)
1559 goto Fail;
1560 }
1561 } else {
1562 while ((key = PyIter_Next(it)) != NULL) {
1563 status = PyObject_SetItem(d, key, value);
1564 Py_DECREF(key);
1565 if (status < 0)
1566 goto Fail;
1567 }
1568 }
1569
1570 if (PyErr_Occurred())
1571 goto Fail;
1572 Py_DECREF(it);
1573 return d;
1574
1575Fail:
1576 Py_DECREF(it);
1577 Py_DECREF(d);
1578 return NULL;
1579}
1580
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001581/* Methods */
1582
1583static void
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001584dict_dealloc(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001585{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001586 PyObject **values = mp->ma_values;
1587 PyDictKeysObject *keys = mp->ma_keys;
1588 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001589 PyObject_GC_UnTrack(mp);
1590 Py_TRASHCAN_SAFE_BEGIN(mp)
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001591 if (values != NULL) {
1592 if (values != empty_values) {
1593 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
1594 Py_XDECREF(values[i]);
1595 }
1596 free_values(values);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001598 DK_DECREF(keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 }
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02001600 else if (keys != NULL) {
Antoine Pitrou2d169b22012-05-12 23:43:44 +02001601 assert(keys->dk_refcnt == 1);
1602 DK_DECREF(keys);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001603 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1605 free_list[numfree++] = mp;
1606 else
1607 Py_TYPE(mp)->tp_free((PyObject *)mp);
1608 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001609}
1610
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001611
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001612static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001613dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001614{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 Py_ssize_t i;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001616 PyObject *key = NULL, *value = NULL;
1617 _PyUnicodeWriter writer;
1618 int first;
Guido van Rossum255443b1998-04-10 22:47:14 +00001619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 i = Py_ReprEnter((PyObject *)mp);
1621 if (i != 0) {
1622 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1623 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 if (mp->ma_used == 0) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001626 Py_ReprLeave((PyObject *)mp);
1627 return PyUnicode_FromString("{}");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 }
Tim Petersa7259592001-06-16 05:11:17 +00001629
Victor Stinnerf91929b2013-11-19 13:07:38 +01001630 _PyUnicodeWriter_Init(&writer);
1631 writer.overallocate = 1;
1632 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
1633 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
Tim Petersa7259592001-06-16 05:11:17 +00001634
Victor Stinnerf91929b2013-11-19 13:07:38 +01001635 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
1636 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 /* Do repr() on each key+value pair, and insert ": " between them.
1639 Note that repr may mutate the dict. */
1640 i = 0;
Victor Stinnerf91929b2013-11-19 13:07:38 +01001641 first = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Victor Stinnerf91929b2013-11-19 13:07:38 +01001643 PyObject *s;
1644 int res;
1645
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001646 /* Prevent repr from deleting key or value during key format. */
1647 Py_INCREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 Py_INCREF(value);
Victor Stinnerf97dfd72013-07-18 01:00:45 +02001649
Victor Stinnerf91929b2013-11-19 13:07:38 +01001650 if (!first) {
1651 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
1652 goto error;
1653 }
1654 first = 0;
1655
1656 s = PyObject_Repr(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 if (s == NULL)
Victor Stinnerf91929b2013-11-19 13:07:38 +01001658 goto error;
1659 res = _PyUnicodeWriter_WriteStr(&writer, s);
1660 Py_DECREF(s);
1661 if (res < 0)
1662 goto error;
1663
1664 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
1665 goto error;
1666
1667 s = PyObject_Repr(value);
1668 if (s == NULL)
1669 goto error;
1670 res = _PyUnicodeWriter_WriteStr(&writer, s);
1671 Py_DECREF(s);
1672 if (res < 0)
1673 goto error;
1674
1675 Py_CLEAR(key);
1676 Py_CLEAR(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 }
Tim Petersa7259592001-06-16 05:11:17 +00001678
Victor Stinnerf91929b2013-11-19 13:07:38 +01001679 writer.overallocate = 0;
1680 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
1681 goto error;
Tim Petersa7259592001-06-16 05:11:17 +00001682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 Py_ReprLeave((PyObject *)mp);
Victor Stinnerf91929b2013-11-19 13:07:38 +01001684
1685 return _PyUnicodeWriter_Finish(&writer);
1686
1687error:
1688 Py_ReprLeave((PyObject *)mp);
1689 _PyUnicodeWriter_Dealloc(&writer);
1690 Py_XDECREF(key);
1691 Py_XDECREF(value);
1692 return NULL;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001693}
1694
Martin v. Löwis18e16552006-02-15 17:27:45 +00001695static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001696dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001697{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001699}
1700
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001701static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001702dict_subscript(PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001703{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 PyObject *v;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001705 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001706 PyDictKeyEntry *ep;
1707 PyObject **value_addr;
1708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001710 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 hash = PyObject_Hash(key);
1712 if (hash == -1)
1713 return NULL;
1714 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001715 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716 if (ep == NULL)
1717 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001718 v = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 if (v == NULL) {
1720 if (!PyDict_CheckExact(mp)) {
1721 /* Look up __missing__ method if we're a subclass. */
1722 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05001723 _Py_IDENTIFIER(__missing__);
1724 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 if (missing != NULL) {
1726 res = PyObject_CallFunctionObjArgs(missing,
1727 key, NULL);
1728 Py_DECREF(missing);
1729 return res;
1730 }
1731 else if (PyErr_Occurred())
1732 return NULL;
1733 }
Raymond Hettinger69492da2013-09-02 15:59:26 -07001734 _PyErr_SetKeyError(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735 return NULL;
1736 }
1737 else
1738 Py_INCREF(v);
1739 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001740}
1741
1742static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001743dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001744{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001745 if (w == NULL)
1746 return PyDict_DelItem((PyObject *)mp, v);
1747 else
1748 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001749}
1750
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001751static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 (lenfunc)dict_length, /*mp_length*/
1753 (binaryfunc)dict_subscript, /*mp_subscript*/
1754 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001755};
1756
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001757static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001758dict_keys(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001759{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001760 PyObject *v;
1761 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001762 PyDictKeyEntry *ep;
1763 Py_ssize_t size, n, offset;
1764 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001765
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001766 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001767 n = mp->ma_used;
1768 v = PyList_New(n);
1769 if (v == NULL)
1770 return NULL;
1771 if (n != mp->ma_used) {
1772 /* Durnit. The allocations caused the dict to resize.
1773 * Just start over, this shouldn't normally happen.
1774 */
1775 Py_DECREF(v);
1776 goto again;
1777 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001778 ep = &mp->ma_keys->dk_entries[0];
1779 size = DK_SIZE(mp->ma_keys);
1780 if (mp->ma_values) {
1781 value_ptr = mp->ma_values;
1782 offset = sizeof(PyObject *);
1783 }
1784 else {
1785 value_ptr = &ep[0].me_value;
1786 offset = sizeof(PyDictKeyEntry);
1787 }
1788 for (i = 0, j = 0; i < size; i++) {
1789 if (*value_ptr != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 PyObject *key = ep[i].me_key;
1791 Py_INCREF(key);
1792 PyList_SET_ITEM(v, j, key);
1793 j++;
1794 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001795 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001796 }
1797 assert(j == n);
1798 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001799}
1800
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001801static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001802dict_values(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001803{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001804 PyObject *v;
1805 Py_ssize_t i, j;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001806 Py_ssize_t size, n, offset;
1807 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001808
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001809 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001810 n = mp->ma_used;
1811 v = PyList_New(n);
1812 if (v == NULL)
1813 return NULL;
1814 if (n != mp->ma_used) {
1815 /* Durnit. The allocations caused the dict to resize.
1816 * Just start over, this shouldn't normally happen.
1817 */
1818 Py_DECREF(v);
1819 goto again;
1820 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001821 size = DK_SIZE(mp->ma_keys);
1822 if (mp->ma_values) {
1823 value_ptr = mp->ma_values;
1824 offset = sizeof(PyObject *);
1825 }
1826 else {
1827 value_ptr = &mp->ma_keys->dk_entries[0].me_value;
1828 offset = sizeof(PyDictKeyEntry);
1829 }
1830 for (i = 0, j = 0; i < size; i++) {
1831 PyObject *value = *value_ptr;
1832 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1833 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 Py_INCREF(value);
1835 PyList_SET_ITEM(v, j, value);
1836 j++;
1837 }
1838 }
1839 assert(j == n);
1840 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001841}
1842
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001843static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001844dict_items(PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001845{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001846 PyObject *v;
1847 Py_ssize_t i, j, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001848 Py_ssize_t size, offset;
1849 PyObject *item, *key;
1850 PyDictKeyEntry *ep;
1851 PyObject **value_ptr;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 /* Preallocate the list of tuples, to avoid allocations during
1854 * the loop over the items, which could trigger GC, which
1855 * could resize the dict. :-(
1856 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001857 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 n = mp->ma_used;
1859 v = PyList_New(n);
1860 if (v == NULL)
1861 return NULL;
1862 for (i = 0; i < n; i++) {
1863 item = PyTuple_New(2);
1864 if (item == NULL) {
1865 Py_DECREF(v);
1866 return NULL;
1867 }
1868 PyList_SET_ITEM(v, i, item);
1869 }
1870 if (n != mp->ma_used) {
1871 /* Durnit. The allocations caused the dict to resize.
1872 * Just start over, this shouldn't normally happen.
1873 */
1874 Py_DECREF(v);
1875 goto again;
1876 }
1877 /* Nothing we do below makes any function calls. */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04001878 ep = mp->ma_keys->dk_entries;
1879 size = DK_SIZE(mp->ma_keys);
1880 if (mp->ma_values) {
1881 value_ptr = mp->ma_values;
1882 offset = sizeof(PyObject *);
1883 }
1884 else {
1885 value_ptr = &ep[0].me_value;
1886 offset = sizeof(PyDictKeyEntry);
1887 }
1888 for (i = 0, j = 0; i < size; i++) {
1889 PyObject *value = *value_ptr;
1890 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
1891 if (value != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 key = ep[i].me_key;
1893 item = PyList_GET_ITEM(v, j);
1894 Py_INCREF(key);
1895 PyTuple_SET_ITEM(item, 0, key);
1896 Py_INCREF(value);
1897 PyTuple_SET_ITEM(item, 1, value);
1898 j++;
1899 }
1900 }
1901 assert(j == n);
1902 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001903}
1904
Larry Hastings5c661892014-01-24 06:17:25 -08001905/*[clinic input]
1906@classmethod
1907dict.fromkeys
Larry Hastings5c661892014-01-24 06:17:25 -08001908 iterable: object
1909 value: object=None
1910 /
1911
1912Returns a new dict with keys from iterable and values equal to value.
1913[clinic start generated code]*/
1914
Larry Hastings5c661892014-01-24 06:17:25 -08001915static PyObject *
1916dict_fromkeys_impl(PyTypeObject *type, PyObject *iterable, PyObject *value)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001917/*[clinic end generated code: output=8fb98e4b10384999 input=b85a667f9bf4669d]*/
Larry Hastings5c661892014-01-24 06:17:25 -08001918{
Eric Snow96c6af92015-05-29 22:21:39 -06001919 return _PyDict_FromKeys((PyObject *)type, iterable, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001920}
1921
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001922static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001923dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, const char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001924{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001925 PyObject *arg = NULL;
1926 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1929 result = -1;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001932 _Py_IDENTIFIER(keys);
1933 if (_PyObject_HasAttrId(arg, &PyId_keys))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 result = PyDict_Merge(self, arg, 1);
1935 else
1936 result = PyDict_MergeFromSeq2(self, arg, 1);
1937 }
1938 if (result == 0 && kwds != NULL) {
1939 if (PyArg_ValidateKeywordArguments(kwds))
1940 result = PyDict_Merge(self, kwds, 1);
1941 else
1942 result = -1;
1943 }
1944 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001945}
1946
1947static PyObject *
1948dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1949{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 if (dict_update_common(self, args, kwds, "update") != -1)
1951 Py_RETURN_NONE;
1952 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001953}
1954
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001955/* Update unconditionally replaces existing items.
1956 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001957 otherwise it leaves existing items unchanged.
1958
1959 PyDict_{Update,Merge} update/merge from a mapping object.
1960
Tim Petersf582b822001-12-11 18:51:08 +00001961 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001962 producing iterable objects of length 2.
1963*/
1964
Tim Petersf582b822001-12-11 18:51:08 +00001965int
Tim Peters1fc240e2001-10-26 05:06:50 +00001966PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1967{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968 PyObject *it; /* iter(seq2) */
1969 Py_ssize_t i; /* index into seq2 of current element */
1970 PyObject *item; /* seq2[i] */
1971 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00001972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001973 assert(d != NULL);
1974 assert(PyDict_Check(d));
1975 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00001976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001977 it = PyObject_GetIter(seq2);
1978 if (it == NULL)
1979 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 for (i = 0; ; ++i) {
1982 PyObject *key, *value;
1983 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001985 fast = NULL;
1986 item = PyIter_Next(it);
1987 if (item == NULL) {
1988 if (PyErr_Occurred())
1989 goto Fail;
1990 break;
1991 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001993 /* Convert item to sequence, and verify length 2. */
1994 fast = PySequence_Fast(item, "");
1995 if (fast == NULL) {
1996 if (PyErr_ExceptionMatches(PyExc_TypeError))
1997 PyErr_Format(PyExc_TypeError,
1998 "cannot convert dictionary update "
1999 "sequence element #%zd to a sequence",
2000 i);
2001 goto Fail;
2002 }
2003 n = PySequence_Fast_GET_SIZE(fast);
2004 if (n != 2) {
2005 PyErr_Format(PyExc_ValueError,
2006 "dictionary update sequence element #%zd "
2007 "has length %zd; 2 is required",
2008 i, n);
2009 goto Fail;
2010 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002012 /* Update/merge with this (key, value) pair. */
2013 key = PySequence_Fast_GET_ITEM(fast, 0);
2014 value = PySequence_Fast_GET_ITEM(fast, 1);
2015 if (override || PyDict_GetItem(d, key) == NULL) {
2016 int status = PyDict_SetItem(d, key, value);
2017 if (status < 0)
2018 goto Fail;
2019 }
2020 Py_DECREF(fast);
2021 Py_DECREF(item);
2022 }
Tim Peters1fc240e2001-10-26 05:06:50 +00002023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002024 i = 0;
2025 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00002026Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 Py_XDECREF(item);
2028 Py_XDECREF(fast);
2029 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00002030Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002031 Py_DECREF(it);
2032 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00002033}
2034
Tim Peters6d6c1a32001-08-02 04:15:00 +00002035int
2036PyDict_Update(PyObject *a, PyObject *b)
2037{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002038 return PyDict_Merge(a, b, 1);
Guido van Rossum05ac6de2001-08-10 20:28:28 +00002039}
2040
2041int
2042PyDict_Merge(PyObject *a, PyObject *b, int override)
2043{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002044 PyDictObject *mp, *other;
2045 Py_ssize_t i, n;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002046 PyDictKeyEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048 /* We accept for the argument either a concrete dictionary object,
2049 * or an abstract "mapping" object. For the former, we can do
2050 * things quite efficiently. For the latter, we only require that
2051 * PyMapping_Keys() and PyObject_GetItem() be supported.
2052 */
2053 if (a == NULL || !PyDict_Check(a) || b == NULL) {
2054 PyErr_BadInternalCall();
2055 return -1;
2056 }
2057 mp = (PyDictObject*)a;
2058 if (PyDict_Check(b)) {
2059 other = (PyDictObject*)b;
2060 if (other == mp || other->ma_used == 0)
2061 /* a.update(a) or a.update({}); nothing to do */
2062 return 0;
2063 if (mp->ma_used == 0)
2064 /* Since the target dict is empty, PyDict_GetItem()
2065 * always returns NULL. Setting override to 1
2066 * skips the unnecessary test.
2067 */
2068 override = 1;
2069 /* Do one big resize at the start, rather than
2070 * incrementally resizing as we insert new items. Expect
2071 * that there will be no (or few) overlapping keys.
2072 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002073 if (mp->ma_keys->dk_usable * 3 < other->ma_used * 2)
2074 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002075 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002076 for (i = 0, n = DK_SIZE(other->ma_keys); i < n; i++) {
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002077 PyObject *key, *value;
2078 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002079 entry = &other->ma_keys->dk_entries[i];
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002080 key = entry->me_key;
2081 hash = entry->me_hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002082 if (other->ma_values)
2083 value = other->ma_values[i];
2084 else
2085 value = entry->me_value;
2086
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002087 if (value != NULL) {
2088 int err = 0;
2089 Py_INCREF(key);
2090 Py_INCREF(value);
2091 if (override || PyDict_GetItem(a, key) == NULL)
2092 err = insertdict(mp, key, hash, value);
2093 Py_DECREF(value);
2094 Py_DECREF(key);
2095 if (err != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096 return -1;
Benjamin Petersona82f77f2015-07-04 19:55:16 -05002097
2098 if (n != DK_SIZE(other->ma_keys)) {
2099 PyErr_SetString(PyExc_RuntimeError,
2100 "dict mutated during update");
2101 return -1;
2102 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002103 }
2104 }
2105 }
2106 else {
2107 /* Do it the generic, slower way */
2108 PyObject *keys = PyMapping_Keys(b);
2109 PyObject *iter;
2110 PyObject *key, *value;
2111 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002113 if (keys == NULL)
2114 /* Docstring says this is equivalent to E.keys() so
2115 * if E doesn't have a .keys() method we want
2116 * AttributeError to percolate up. Might as well
2117 * do the same for any other error.
2118 */
2119 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 iter = PyObject_GetIter(keys);
2122 Py_DECREF(keys);
2123 if (iter == NULL)
2124 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00002125
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002126 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
2127 if (!override && PyDict_GetItem(a, key) != NULL) {
2128 Py_DECREF(key);
2129 continue;
2130 }
2131 value = PyObject_GetItem(b, key);
2132 if (value == NULL) {
2133 Py_DECREF(iter);
2134 Py_DECREF(key);
2135 return -1;
2136 }
2137 status = PyDict_SetItem(a, key, value);
2138 Py_DECREF(key);
2139 Py_DECREF(value);
2140 if (status < 0) {
2141 Py_DECREF(iter);
2142 return -1;
2143 }
2144 }
2145 Py_DECREF(iter);
2146 if (PyErr_Occurred())
2147 /* Iterator completed, via error */
2148 return -1;
2149 }
2150 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002151}
2152
2153static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002154dict_copy(PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002155{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002156 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002157}
2158
2159PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002160PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002161{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162 PyObject *copy;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002163 PyDictObject *mp;
2164 Py_ssize_t i, n;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00002165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002166 if (o == NULL || !PyDict_Check(o)) {
2167 PyErr_BadInternalCall();
2168 return NULL;
2169 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002170 mp = (PyDictObject *)o;
2171 if (_PyDict_HasSplitTable(mp)) {
2172 PyDictObject *split_copy;
2173 PyObject **newvalues = new_values(DK_SIZE(mp->ma_keys));
2174 if (newvalues == NULL)
2175 return PyErr_NoMemory();
2176 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
2177 if (split_copy == NULL) {
2178 free_values(newvalues);
2179 return NULL;
2180 }
2181 split_copy->ma_values = newvalues;
2182 split_copy->ma_keys = mp->ma_keys;
2183 split_copy->ma_used = mp->ma_used;
2184 DK_INCREF(mp->ma_keys);
2185 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2186 PyObject *value = mp->ma_values[i];
2187 Py_XINCREF(value);
2188 split_copy->ma_values[i] = value;
2189 }
Benjamin Peterson7ce67e42012-04-24 10:32:57 -04002190 if (_PyObject_GC_IS_TRACKED(mp))
2191 _PyObject_GC_TRACK(split_copy);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002192 return (PyObject *)split_copy;
2193 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 copy = PyDict_New();
2195 if (copy == NULL)
2196 return NULL;
2197 if (PyDict_Merge(copy, o, 1) == 0)
2198 return copy;
2199 Py_DECREF(copy);
2200 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00002201}
2202
Martin v. Löwis18e16552006-02-15 17:27:45 +00002203Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00002204PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00002205{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002206 if (mp == NULL || !PyDict_Check(mp)) {
2207 PyErr_BadInternalCall();
2208 return -1;
2209 }
2210 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00002211}
2212
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002213PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002214PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002215{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002216 if (mp == NULL || !PyDict_Check(mp)) {
2217 PyErr_BadInternalCall();
2218 return NULL;
2219 }
2220 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002221}
2222
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002223PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002224PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002226 if (mp == NULL || !PyDict_Check(mp)) {
2227 PyErr_BadInternalCall();
2228 return NULL;
2229 }
2230 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002231}
2232
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002233PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00002234PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00002235{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 if (mp == NULL || !PyDict_Check(mp)) {
2237 PyErr_BadInternalCall();
2238 return NULL;
2239 }
2240 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00002241}
2242
Tim Peterse63415e2001-05-08 04:38:29 +00002243/* Return 1 if dicts equal, 0 if not, -1 if error.
2244 * Gets out as soon as any difference is detected.
2245 * Uses only Py_EQ comparison.
2246 */
2247static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002248dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00002249{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002250 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00002251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002252 if (a->ma_used != b->ma_used)
2253 /* can't be equal if # of entries differ */
2254 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002255 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002256 for (i = 0; i < DK_SIZE(a->ma_keys); i++) {
2257 PyDictKeyEntry *ep = &a->ma_keys->dk_entries[i];
2258 PyObject *aval;
2259 if (a->ma_values)
2260 aval = a->ma_values[i];
2261 else
2262 aval = ep->me_value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002263 if (aval != NULL) {
2264 int cmp;
2265 PyObject *bval;
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002266 PyObject **vaddr;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002267 PyObject *key = ep->me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002268 /* temporarily bump aval's refcount to ensure it stays
2269 alive until we're done with it */
2270 Py_INCREF(aval);
2271 /* ditto for key */
2272 Py_INCREF(key);
Antoine Pitrou0e9958b2012-12-02 19:10:07 +01002273 /* reuse the known hash value */
2274 if ((b->ma_keys->dk_lookup)(b, key, ep->me_hash, &vaddr) == NULL)
2275 bval = NULL;
2276 else
2277 bval = *vaddr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 Py_DECREF(key);
2279 if (bval == NULL) {
2280 Py_DECREF(aval);
2281 if (PyErr_Occurred())
2282 return -1;
2283 return 0;
2284 }
2285 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
2286 Py_DECREF(aval);
2287 if (cmp <= 0) /* error or not equal */
2288 return cmp;
2289 }
2290 }
2291 return 1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002292}
Tim Peterse63415e2001-05-08 04:38:29 +00002293
2294static PyObject *
2295dict_richcompare(PyObject *v, PyObject *w, int op)
2296{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002297 int cmp;
2298 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00002299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002300 if (!PyDict_Check(v) || !PyDict_Check(w)) {
2301 res = Py_NotImplemented;
2302 }
2303 else if (op == Py_EQ || op == Py_NE) {
2304 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
2305 if (cmp < 0)
2306 return NULL;
2307 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
2308 }
2309 else
2310 res = Py_NotImplemented;
2311 Py_INCREF(res);
2312 return res;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002313}
Tim Peterse63415e2001-05-08 04:38:29 +00002314
Larry Hastings61272b72014-01-07 12:41:53 -08002315/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002316
2317@coexist
2318dict.__contains__
2319
2320 key: object
2321 /
2322
Meador Ingee02de8c2014-01-14 16:48:31 -06002323True if D has a key k, else False.
Larry Hastings61272b72014-01-07 12:41:53 -08002324[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002325
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002326static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08002327dict___contains__(PyDictObject *self, PyObject *key)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03002328/*[clinic end generated code: output=a3d03db709ed6e6b input=b852b2a19b51ab24]*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002329{
Larry Hastingsc2047262014-01-25 20:43:29 -08002330 register PyDictObject *mp = self;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002331 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002332 PyDictKeyEntry *ep;
2333 PyObject **value_addr;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002335 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002336 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002337 hash = PyObject_Hash(key);
2338 if (hash == -1)
2339 return NULL;
2340 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002341 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002342 if (ep == NULL)
2343 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002344 return PyBool_FromLong(*value_addr != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002345}
2346
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002347static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002348dict_get(PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00002349{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002350 PyObject *key;
2351 PyObject *failobj = Py_None;
2352 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002353 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002354 PyDictKeyEntry *ep;
2355 PyObject **value_addr;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
2358 return NULL;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002361 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002362 hash = PyObject_Hash(key);
2363 if (hash == -1)
2364 return NULL;
2365 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002366 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 if (ep == NULL)
2368 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002369 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002370 if (val == NULL)
2371 val = failobj;
2372 Py_INCREF(val);
2373 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00002374}
2375
Benjamin Peterson00e98862013-03-07 22:16:29 -05002376PyObject *
2377PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
Guido van Rossum164452c2000-08-08 16:12:54 +00002378{
Benjamin Peterson00e98862013-03-07 22:16:29 -05002379 PyDictObject *mp = (PyDictObject *)d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002381 Py_hash_t hash;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002382 PyDictKeyEntry *ep;
2383 PyObject **value_addr;
Guido van Rossum164452c2000-08-08 16:12:54 +00002384
Benjamin Peterson00e98862013-03-07 22:16:29 -05002385 if (!PyDict_Check(d)) {
2386 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002387 return NULL;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002388 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002390 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 hash = PyObject_Hash(key);
2392 if (hash == -1)
2393 return NULL;
2394 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002395 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002396 if (ep == NULL)
2397 return NULL;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002398 val = *value_addr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002399 if (val == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002400 if (mp->ma_keys->dk_usable <= 0) {
2401 /* Need to resize. */
2402 if (insertion_resize(mp) < 0)
2403 return NULL;
2404 ep = find_empty_slot(mp, key, hash, &value_addr);
2405 }
Benjamin Peterson00e98862013-03-07 22:16:29 -05002406 Py_INCREF(defaultobj);
Benjamin Petersonb1efa532013-03-04 09:47:50 -05002407 Py_INCREF(key);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002408 MAINTAIN_TRACKING(mp, key, defaultobj);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002409 ep->me_key = key;
2410 ep->me_hash = hash;
Benjamin Peterson00e98862013-03-07 22:16:29 -05002411 *value_addr = defaultobj;
2412 val = defaultobj;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002413 mp->ma_keys->dk_usable--;
2414 mp->ma_used++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002415 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 return val;
Guido van Rossum164452c2000-08-08 16:12:54 +00002417}
2418
Benjamin Peterson00e98862013-03-07 22:16:29 -05002419static PyObject *
2420dict_setdefault(PyDictObject *mp, PyObject *args)
2421{
2422 PyObject *key, *val;
2423 PyObject *defaultobj = Py_None;
2424
2425 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &defaultobj))
2426 return NULL;
2427
Benjamin Peterson55898502013-03-08 08:36:49 -05002428 val = PyDict_SetDefault((PyObject *)mp, key, defaultobj);
Benjamin Peterson00e98862013-03-07 22:16:29 -05002429 Py_XINCREF(val);
2430 return val;
2431}
Guido van Rossum164452c2000-08-08 16:12:54 +00002432
2433static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002434dict_clear(PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002435{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436 PyDict_Clear((PyObject *)mp);
2437 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00002438}
2439
Guido van Rossumba6ab842000-12-12 22:02:18 +00002440static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002441dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002442{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002443 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00002444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002445 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2446 return NULL;
Eric Snow96c6af92015-05-29 22:21:39 -06002447
2448 return _PyDict_Pop(mp, key, deflt);
Guido van Rossume027d982002-04-12 15:11:59 +00002449}
2450
2451static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002452dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002453{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002454 Py_hash_t i = 0;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002455 PyDictKeyEntry *ep;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002456 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002457
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002459 /* Allocate the result tuple before checking the size. Believe it
2460 * or not, this allocation could trigger a garbage collection which
2461 * could empty the dict, so if we checked the size first and that
2462 * happened, the result would be an infinite loop (searching for an
2463 * entry that no longer exists). Note that the usual popitem()
2464 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2465 * tuple away if the dict *is* empty isn't a significant
2466 * inefficiency -- possible, but unlikely in practice.
2467 */
2468 res = PyTuple_New(2);
2469 if (res == NULL)
2470 return NULL;
2471 if (mp->ma_used == 0) {
2472 Py_DECREF(res);
2473 PyErr_SetString(PyExc_KeyError,
2474 "popitem(): dictionary is empty");
2475 return NULL;
2476 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002477 /* Convert split table to combined table */
2478 if (mp->ma_keys->dk_lookup == lookdict_split) {
2479 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
2480 Py_DECREF(res);
2481 return NULL;
2482 }
2483 }
2484 ENSURE_ALLOWS_DELETIONS(mp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002485 /* Set ep to "the first" dict entry with a value. We abuse the hash
2486 * field of slot 0 to hold a search finger:
2487 * If slot 0 has a value, use slot 0.
2488 * Else slot 0 is being used to hold a search finger,
2489 * and we use its hash value as the first index to look.
2490 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002491 ep = &mp->ma_keys->dk_entries[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002492 if (ep->me_value == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002493 Py_ssize_t mask = DK_MASK(mp->ma_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002494 i = ep->me_hash;
2495 /* The hash field may be a real hash value, or it may be a
2496 * legit search finger, or it may be a once-legit search
2497 * finger that's out of bounds now because it wrapped around
2498 * or the table shrunk -- simply make sure it's in bounds now.
2499 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002500 if (i > mask || i < 1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002501 i = 1; /* skip slot 0 */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002502 while ((ep = &mp->ma_keys->dk_entries[i])->me_value == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002503 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002504 if (i > mask)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002505 i = 1;
2506 }
2507 }
2508 PyTuple_SET_ITEM(res, 0, ep->me_key);
2509 PyTuple_SET_ITEM(res, 1, ep->me_value);
2510 Py_INCREF(dummy);
2511 ep->me_key = dummy;
2512 ep->me_value = NULL;
2513 mp->ma_used--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002514 assert(mp->ma_keys->dk_entries[0].me_value == NULL);
2515 mp->ma_keys->dk_entries[0].me_hash = i + 1; /* next place to start */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002516 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002517}
2518
Jeremy Hylton8caad492000-06-23 14:18:11 +00002519static int
2520dict_traverse(PyObject *op, visitproc visit, void *arg)
2521{
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002522 Py_ssize_t i, n;
2523 PyDictObject *mp = (PyDictObject *)op;
2524 if (mp->ma_keys->dk_lookup == lookdict) {
2525 for (i = 0; i < DK_SIZE(mp->ma_keys); i++) {
2526 if (mp->ma_keys->dk_entries[i].me_value != NULL) {
2527 Py_VISIT(mp->ma_keys->dk_entries[i].me_value);
2528 Py_VISIT(mp->ma_keys->dk_entries[i].me_key);
2529 }
2530 }
2531 } else {
2532 if (mp->ma_values != NULL) {
2533 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2534 Py_VISIT(mp->ma_values[i]);
2535 }
2536 }
2537 else {
2538 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
2539 Py_VISIT(mp->ma_keys->dk_entries[i].me_value);
2540 }
2541 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002542 }
2543 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002544}
2545
2546static int
2547dict_tp_clear(PyObject *op)
2548{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002549 PyDict_Clear(op);
2550 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002551}
2552
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002553static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002554
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002555Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06002556_PyDict_SizeOf(PyDictObject *mp)
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002557{
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002558 Py_ssize_t size, res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002559
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002560 size = DK_SIZE(mp->ma_keys);
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02002561 res = _PyObject_SIZE(Py_TYPE(mp));
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002562 if (mp->ma_values)
2563 res += size * sizeof(PyObject*);
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002564 /* If the dictionary is split, the keys portion is accounted-for
2565 in the type object. */
2566 if (mp->ma_keys->dk_refcnt == 1)
2567 res += sizeof(PyDictKeysObject) + (size-1) * sizeof(PyDictKeyEntry);
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002568 return res;
Martin v. Loewis4f2f3b62012-04-24 19:13:57 +02002569}
2570
2571Py_ssize_t
2572_PyDict_KeysSize(PyDictKeysObject *keys)
2573{
2574 return sizeof(PyDictKeysObject) + (DK_SIZE(keys)-1) * sizeof(PyDictKeyEntry);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002575}
2576
doko@ubuntu.com17210f52016-01-14 14:04:59 +01002577static PyObject *
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002578dict_sizeof(PyDictObject *mp)
2579{
2580 return PyLong_FromSsize_t(_PyDict_SizeOf(mp));
2581}
2582
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002583PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
2584
Martin v. Löwis00709aa2008-06-04 14:18:43 +00002585PyDoc_STRVAR(sizeof__doc__,
2586"D.__sizeof__() -> size of D in memory, in bytes");
2587
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002588PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002589"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002590
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002591PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002592"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 +00002593
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002594PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002595"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002596If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00002597
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002598PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00002599"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000026002-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002601
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002602PyDoc_STRVAR(update__doc__,
Brett Cannonf2754162013-05-11 14:46:48 -04002603"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
2604If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
2605If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
2606In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00002607
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002608PyDoc_STRVAR(clear__doc__,
2609"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002610
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002611PyDoc_STRVAR(copy__doc__,
2612"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00002613
Guido van Rossumb90c8482007-02-10 01:11:45 +00002614/* Forward */
2615static PyObject *dictkeys_new(PyObject *);
2616static PyObject *dictitems_new(PyObject *);
2617static PyObject *dictvalues_new(PyObject *);
2618
Guido van Rossum45c85d12007-07-27 16:31:40 +00002619PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002620 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002621PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002622 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002623PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002624 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00002625
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002626static PyMethodDef mapp_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -07002627 DICT___CONTAINS___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002628 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
2629 getitem__doc__},
Serhiy Storchaka0ce7a3a2015-12-22 08:16:18 +02002630 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002631 sizeof__doc__},
2632 {"get", (PyCFunction)dict_get, METH_VARARGS,
2633 get__doc__},
2634 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
2635 setdefault_doc__},
2636 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
2637 pop__doc__},
2638 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
2639 popitem__doc__},
2640 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
2641 keys__doc__},
2642 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
2643 items__doc__},
2644 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
2645 values__doc__},
2646 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
2647 update__doc__},
Larry Hastings5c661892014-01-24 06:17:25 -08002648 DICT_FROMKEYS_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002649 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
2650 clear__doc__},
2651 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
2652 copy__doc__},
2653 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002654};
2655
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002656/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002657int
2658PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002659{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002660 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002661 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002662 PyDictKeyEntry *ep;
2663 PyObject **value_addr;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002665 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002666 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002667 hash = PyObject_Hash(key);
2668 if (hash == -1)
2669 return -1;
2670 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002671 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
2672 return (ep == NULL) ? -1 : (*value_addr != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002673}
2674
Thomas Wouterscf297e42007-02-23 15:07:44 +00002675/* Internal version of PyDict_Contains used when the hash value is already known */
2676int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002677_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00002678{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002679 PyDictObject *mp = (PyDictObject *)op;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002680 PyDictKeyEntry *ep;
2681 PyObject **value_addr;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002682
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002683 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
2684 return (ep == NULL) ? -1 : (*value_addr != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00002685}
2686
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002687/* Hack to implement "key in dict" */
2688static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002689 0, /* sq_length */
2690 0, /* sq_concat */
2691 0, /* sq_repeat */
2692 0, /* sq_item */
2693 0, /* sq_slice */
2694 0, /* sq_ass_item */
2695 0, /* sq_ass_slice */
2696 PyDict_Contains, /* sq_contains */
2697 0, /* sq_inplace_concat */
2698 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002699};
2700
Guido van Rossum09e563a2001-05-01 12:10:21 +00002701static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002702dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2703{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002704 PyObject *self;
Victor Stinnera9f61a52013-07-16 22:17:26 +02002705 PyDictObject *d;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002707 assert(type != NULL && type->tp_alloc != NULL);
2708 self = type->tp_alloc(type, 0);
Victor Stinnera9f61a52013-07-16 22:17:26 +02002709 if (self == NULL)
2710 return NULL;
Victor Stinnera9f61a52013-07-16 22:17:26 +02002711 d = (PyDictObject *)self;
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002712
Victor Stinnera9f61a52013-07-16 22:17:26 +02002713 /* The object has been implicitly tracked by tp_alloc */
2714 if (type == &PyDict_Type)
2715 _PyObject_GC_UNTRACK(d);
Victor Stinnerac2a4fe2013-07-16 22:19:00 +02002716
2717 d->ma_used = 0;
2718 d->ma_keys = new_keys_object(PyDict_MINSIZE_COMBINED);
2719 if (d->ma_keys == NULL) {
2720 Py_DECREF(self);
2721 return NULL;
2722 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002723 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002724}
2725
Tim Peters25786c02001-09-02 08:22:48 +00002726static int
2727dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2728{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002729 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002730}
2731
Tim Peters6d6c1a32001-08-02 04:15:00 +00002732static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002733dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002734{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002735 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002736}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002737
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002738PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002739"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002740"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002741" (key, value) pairs\n"
2742"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002743" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002744" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002745" d[k] = v\n"
2746"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2747" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002748
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002749PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002750 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2751 "dict",
2752 sizeof(PyDictObject),
2753 0,
2754 (destructor)dict_dealloc, /* tp_dealloc */
2755 0, /* tp_print */
2756 0, /* tp_getattr */
2757 0, /* tp_setattr */
2758 0, /* tp_reserved */
2759 (reprfunc)dict_repr, /* tp_repr */
2760 0, /* tp_as_number */
2761 &dict_as_sequence, /* tp_as_sequence */
2762 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002763 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002764 0, /* tp_call */
2765 0, /* tp_str */
2766 PyObject_GenericGetAttr, /* tp_getattro */
2767 0, /* tp_setattro */
2768 0, /* tp_as_buffer */
2769 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2770 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
2771 dictionary_doc, /* tp_doc */
2772 dict_traverse, /* tp_traverse */
2773 dict_tp_clear, /* tp_clear */
2774 dict_richcompare, /* tp_richcompare */
2775 0, /* tp_weaklistoffset */
2776 (getiterfunc)dict_iter, /* tp_iter */
2777 0, /* tp_iternext */
2778 mapp_methods, /* tp_methods */
2779 0, /* tp_members */
2780 0, /* tp_getset */
2781 0, /* tp_base */
2782 0, /* tp_dict */
2783 0, /* tp_descr_get */
2784 0, /* tp_descr_set */
2785 0, /* tp_dictoffset */
2786 dict_init, /* tp_init */
2787 PyType_GenericAlloc, /* tp_alloc */
2788 dict_new, /* tp_new */
2789 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002790};
2791
Victor Stinner3c1e4812012-03-26 22:10:51 +02002792PyObject *
2793_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
2794{
2795 PyObject *kv;
2796 kv = _PyUnicode_FromId(key); /* borrowed */
Victor Stinner5b3b1002013-07-22 23:50:57 +02002797 if (kv == NULL) {
2798 PyErr_Clear();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002799 return NULL;
Victor Stinner5b3b1002013-07-22 23:50:57 +02002800 }
Victor Stinner3c1e4812012-03-26 22:10:51 +02002801 return PyDict_GetItem(dp, kv);
2802}
2803
Guido van Rossum3cca2451997-05-16 14:23:33 +00002804/* For backward compatibility with old dictionary interface */
2805
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002806PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002807PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002808{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002809 PyObject *kv, *rv;
2810 kv = PyUnicode_FromString(key);
Victor Stinnerfdcbab92013-07-16 22:16:05 +02002811 if (kv == NULL) {
2812 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002813 return NULL;
Victor Stinnerfdcbab92013-07-16 22:16:05 +02002814 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002815 rv = PyDict_GetItem(v, kv);
2816 Py_DECREF(kv);
2817 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002818}
2819
2820int
Victor Stinner3c1e4812012-03-26 22:10:51 +02002821_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
2822{
2823 PyObject *kv;
2824 kv = _PyUnicode_FromId(key); /* borrowed */
2825 if (kv == NULL)
2826 return -1;
2827 return PyDict_SetItem(v, kv, item);
2828}
2829
2830int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002831PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002832{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002833 PyObject *kv;
2834 int err;
2835 kv = PyUnicode_FromString(key);
2836 if (kv == NULL)
2837 return -1;
2838 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
2839 err = PyDict_SetItem(v, kv, item);
2840 Py_DECREF(kv);
2841 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002842}
2843
2844int
Victor Stinner5fd2e5a2013-11-06 18:58:22 +01002845_PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
2846{
2847 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
2848 if (kv == NULL)
2849 return -1;
2850 return PyDict_DelItem(v, kv);
2851}
2852
2853int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002854PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002855{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002856 PyObject *kv;
2857 int err;
2858 kv = PyUnicode_FromString(key);
2859 if (kv == NULL)
2860 return -1;
2861 err = PyDict_DelItem(v, kv);
2862 Py_DECREF(kv);
2863 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002864}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002865
Raymond Hettinger019a1482004-03-18 02:41:19 +00002866/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002867
2868typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002869 PyObject_HEAD
2870 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
2871 Py_ssize_t di_used;
2872 Py_ssize_t di_pos;
2873 PyObject* di_result; /* reusable result tuple for iteritems */
2874 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002875} dictiterobject;
2876
2877static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002878dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002879{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 dictiterobject *di;
2881 di = PyObject_GC_New(dictiterobject, itertype);
2882 if (di == NULL)
2883 return NULL;
2884 Py_INCREF(dict);
2885 di->di_dict = dict;
2886 di->di_used = dict->ma_used;
2887 di->di_pos = 0;
2888 di->len = dict->ma_used;
2889 if (itertype == &PyDictIterItem_Type) {
2890 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2891 if (di->di_result == NULL) {
2892 Py_DECREF(di);
2893 return NULL;
2894 }
2895 }
2896 else
2897 di->di_result = NULL;
2898 _PyObject_GC_TRACK(di);
2899 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002900}
2901
2902static void
2903dictiter_dealloc(dictiterobject *di)
2904{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002905 Py_XDECREF(di->di_dict);
2906 Py_XDECREF(di->di_result);
2907 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002908}
2909
2910static int
2911dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
2912{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 Py_VISIT(di->di_dict);
2914 Py_VISIT(di->di_result);
2915 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002916}
2917
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002918static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002919dictiter_len(dictiterobject *di)
2920{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002921 Py_ssize_t len = 0;
2922 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
2923 len = di->len;
2924 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002925}
2926
Guido van Rossumb90c8482007-02-10 01:11:45 +00002927PyDoc_STRVAR(length_hint_doc,
2928 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002929
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002930static PyObject *
2931dictiter_reduce(dictiterobject *di);
2932
2933PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
2934
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002935static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002936 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
2937 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002938 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
2939 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002940 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002941};
2942
Raymond Hettinger019a1482004-03-18 02:41:19 +00002943static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002944{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002945 PyObject *key;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002946 Py_ssize_t i, mask, offset;
2947 PyDictKeysObject *k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002948 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002949 PyObject **value_ptr;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002951 if (d == NULL)
2952 return NULL;
2953 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002955 if (di->di_used != d->ma_used) {
2956 PyErr_SetString(PyExc_RuntimeError,
2957 "dictionary changed size during iteration");
2958 di->di_used = -1; /* Make this state sticky */
2959 return NULL;
2960 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002962 i = di->di_pos;
2963 if (i < 0)
2964 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002965 k = d->ma_keys;
2966 if (d->ma_values) {
2967 value_ptr = &d->ma_values[i];
2968 offset = sizeof(PyObject *);
2969 }
2970 else {
2971 value_ptr = &k->dk_entries[i].me_value;
2972 offset = sizeof(PyDictKeyEntry);
2973 }
2974 mask = DK_SIZE(k)-1;
2975 while (i <= mask && *value_ptr == NULL) {
2976 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002977 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002978 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002979 di->di_pos = i+1;
2980 if (i > mask)
2981 goto fail;
2982 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002983 key = k->dk_entries[i].me_key;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002984 Py_INCREF(key);
2985 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002986
2987fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002988 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03002989 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002990 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002991}
2992
Raymond Hettinger019a1482004-03-18 02:41:19 +00002993PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002994 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2995 "dict_keyiterator", /* tp_name */
2996 sizeof(dictiterobject), /* tp_basicsize */
2997 0, /* tp_itemsize */
2998 /* methods */
2999 (destructor)dictiter_dealloc, /* tp_dealloc */
3000 0, /* tp_print */
3001 0, /* tp_getattr */
3002 0, /* tp_setattr */
3003 0, /* tp_reserved */
3004 0, /* tp_repr */
3005 0, /* tp_as_number */
3006 0, /* tp_as_sequence */
3007 0, /* tp_as_mapping */
3008 0, /* tp_hash */
3009 0, /* tp_call */
3010 0, /* tp_str */
3011 PyObject_GenericGetAttr, /* tp_getattro */
3012 0, /* tp_setattro */
3013 0, /* tp_as_buffer */
3014 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3015 0, /* tp_doc */
3016 (traverseproc)dictiter_traverse, /* tp_traverse */
3017 0, /* tp_clear */
3018 0, /* tp_richcompare */
3019 0, /* tp_weaklistoffset */
3020 PyObject_SelfIter, /* tp_iter */
3021 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
3022 dictiter_methods, /* tp_methods */
3023 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003024};
3025
3026static PyObject *dictiter_iternextvalue(dictiterobject *di)
3027{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003028 PyObject *value;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003029 Py_ssize_t i, mask, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003030 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003031 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003032
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003033 if (d == NULL)
3034 return NULL;
3035 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003036
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003037 if (di->di_used != d->ma_used) {
3038 PyErr_SetString(PyExc_RuntimeError,
3039 "dictionary changed size during iteration");
3040 di->di_used = -1; /* Make this state sticky */
3041 return NULL;
3042 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003044 i = di->di_pos;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003045 mask = DK_SIZE(d->ma_keys)-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003046 if (i < 0 || i > mask)
3047 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003048 if (d->ma_values) {
3049 value_ptr = &d->ma_values[i];
3050 offset = sizeof(PyObject *);
3051 }
3052 else {
3053 value_ptr = &d->ma_keys->dk_entries[i].me_value;
3054 offset = sizeof(PyDictKeyEntry);
3055 }
3056 while (i <= mask && *value_ptr == NULL) {
3057 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003058 i++;
3059 if (i > mask)
3060 goto fail;
3061 }
3062 di->di_pos = i+1;
3063 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003064 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003065 Py_INCREF(value);
3066 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003067
3068fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003069 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003070 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003071 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003072}
3073
3074PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003075 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3076 "dict_valueiterator", /* tp_name */
3077 sizeof(dictiterobject), /* tp_basicsize */
3078 0, /* tp_itemsize */
3079 /* methods */
3080 (destructor)dictiter_dealloc, /* tp_dealloc */
3081 0, /* tp_print */
3082 0, /* tp_getattr */
3083 0, /* tp_setattr */
3084 0, /* tp_reserved */
3085 0, /* tp_repr */
3086 0, /* tp_as_number */
3087 0, /* tp_as_sequence */
3088 0, /* tp_as_mapping */
3089 0, /* tp_hash */
3090 0, /* tp_call */
3091 0, /* tp_str */
3092 PyObject_GenericGetAttr, /* tp_getattro */
3093 0, /* tp_setattro */
3094 0, /* tp_as_buffer */
3095 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3096 0, /* tp_doc */
3097 (traverseproc)dictiter_traverse, /* tp_traverse */
3098 0, /* tp_clear */
3099 0, /* tp_richcompare */
3100 0, /* tp_weaklistoffset */
3101 PyObject_SelfIter, /* tp_iter */
3102 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
3103 dictiter_methods, /* tp_methods */
3104 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00003105};
3106
3107static PyObject *dictiter_iternextitem(dictiterobject *di)
3108{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003109 PyObject *key, *value, *result = di->di_result;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003110 Py_ssize_t i, mask, offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003111 PyDictObject *d = di->di_dict;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003112 PyObject **value_ptr;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003114 if (d == NULL)
3115 return NULL;
3116 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00003117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003118 if (di->di_used != d->ma_used) {
3119 PyErr_SetString(PyExc_RuntimeError,
3120 "dictionary changed size during iteration");
3121 di->di_used = -1; /* Make this state sticky */
3122 return NULL;
3123 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00003124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003125 i = di->di_pos;
3126 if (i < 0)
3127 goto fail;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003128 mask = DK_SIZE(d->ma_keys)-1;
3129 if (d->ma_values) {
3130 value_ptr = &d->ma_values[i];
3131 offset = sizeof(PyObject *);
3132 }
3133 else {
3134 value_ptr = &d->ma_keys->dk_entries[i].me_value;
3135 offset = sizeof(PyDictKeyEntry);
3136 }
3137 while (i <= mask && *value_ptr == NULL) {
3138 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003139 i++;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003140 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003141 di->di_pos = i+1;
3142 if (i > mask)
3143 goto fail;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003145 if (result->ob_refcnt == 1) {
3146 Py_INCREF(result);
3147 Py_DECREF(PyTuple_GET_ITEM(result, 0));
3148 Py_DECREF(PyTuple_GET_ITEM(result, 1));
3149 } else {
3150 result = PyTuple_New(2);
3151 if (result == NULL)
3152 return NULL;
3153 }
3154 di->len--;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003155 key = d->ma_keys->dk_entries[i].me_key;
3156 value = *value_ptr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003157 Py_INCREF(key);
3158 Py_INCREF(value);
Eric Snow96c6af92015-05-29 22:21:39 -06003159 PyTuple_SET_ITEM(result, 0, key); /* steals reference */
3160 PyTuple_SET_ITEM(result, 1, value); /* steals reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003161 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003162
3163fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003164 di->di_dict = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +03003165 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003166 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00003167}
3168
3169PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003170 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3171 "dict_itemiterator", /* tp_name */
3172 sizeof(dictiterobject), /* tp_basicsize */
3173 0, /* tp_itemsize */
3174 /* methods */
3175 (destructor)dictiter_dealloc, /* tp_dealloc */
3176 0, /* tp_print */
3177 0, /* tp_getattr */
3178 0, /* tp_setattr */
3179 0, /* tp_reserved */
3180 0, /* tp_repr */
3181 0, /* tp_as_number */
3182 0, /* tp_as_sequence */
3183 0, /* tp_as_mapping */
3184 0, /* tp_hash */
3185 0, /* tp_call */
3186 0, /* tp_str */
3187 PyObject_GenericGetAttr, /* tp_getattro */
3188 0, /* tp_setattro */
3189 0, /* tp_as_buffer */
3190 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3191 0, /* tp_doc */
3192 (traverseproc)dictiter_traverse, /* tp_traverse */
3193 0, /* tp_clear */
3194 0, /* tp_richcompare */
3195 0, /* tp_weaklistoffset */
3196 PyObject_SelfIter, /* tp_iter */
3197 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
3198 dictiter_methods, /* tp_methods */
3199 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003200};
Guido van Rossumb90c8482007-02-10 01:11:45 +00003201
3202
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003203static PyObject *
3204dictiter_reduce(dictiterobject *di)
3205{
3206 PyObject *list;
3207 dictiterobject tmp;
3208
3209 list = PyList_New(0);
3210 if (!list)
3211 return NULL;
3212
3213 /* copy the itertor state */
3214 tmp = *di;
3215 Py_XINCREF(tmp.di_dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003216
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003217 /* iterate the temporary into a list */
3218 for(;;) {
3219 PyObject *element = 0;
3220 if (Py_TYPE(di) == &PyDictIterItem_Type)
3221 element = dictiter_iternextitem(&tmp);
3222 else if (Py_TYPE(di) == &PyDictIterKey_Type)
3223 element = dictiter_iternextkey(&tmp);
3224 else if (Py_TYPE(di) == &PyDictIterValue_Type)
3225 element = dictiter_iternextvalue(&tmp);
3226 else
3227 assert(0);
3228 if (element) {
3229 if (PyList_Append(list, element)) {
3230 Py_DECREF(element);
3231 Py_DECREF(list);
3232 Py_XDECREF(tmp.di_dict);
3233 return NULL;
3234 }
3235 Py_DECREF(element);
3236 } else
3237 break;
3238 }
3239 Py_XDECREF(tmp.di_dict);
3240 /* check for error */
3241 if (tmp.di_dict != NULL) {
3242 /* we have an error */
3243 Py_DECREF(list);
3244 return NULL;
3245 }
Antoine Pitroua7013882012-04-05 00:04:20 +02003246 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00003247}
3248
Guido van Rossum3ac67412007-02-10 18:55:06 +00003249/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003250/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00003251/***********************************************/
3252
Guido van Rossumb90c8482007-02-10 01:11:45 +00003253/* The instance lay-out is the same for all three; but the type differs. */
3254
Guido van Rossumb90c8482007-02-10 01:11:45 +00003255static void
Eric Snow96c6af92015-05-29 22:21:39 -06003256dictview_dealloc(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003257{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003258 Py_XDECREF(dv->dv_dict);
3259 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003260}
3261
3262static int
Eric Snow96c6af92015-05-29 22:21:39 -06003263dictview_traverse(_PyDictViewObject *dv, visitproc visit, void *arg)
Antoine Pitrou7ddda782009-01-01 15:35:33 +00003264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003265 Py_VISIT(dv->dv_dict);
3266 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003267}
3268
Guido van Rossum83825ac2007-02-10 04:54:19 +00003269static Py_ssize_t
Eric Snow96c6af92015-05-29 22:21:39 -06003270dictview_len(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003271{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003272 Py_ssize_t len = 0;
3273 if (dv->dv_dict != NULL)
3274 len = dv->dv_dict->ma_used;
3275 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003276}
3277
Eric Snow96c6af92015-05-29 22:21:39 -06003278PyObject *
3279_PyDictView_New(PyObject *dict, PyTypeObject *type)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003280{
Eric Snow96c6af92015-05-29 22:21:39 -06003281 _PyDictViewObject *dv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003282 if (dict == NULL) {
3283 PyErr_BadInternalCall();
3284 return NULL;
3285 }
3286 if (!PyDict_Check(dict)) {
3287 /* XXX Get rid of this restriction later */
3288 PyErr_Format(PyExc_TypeError,
3289 "%s() requires a dict argument, not '%s'",
3290 type->tp_name, dict->ob_type->tp_name);
3291 return NULL;
3292 }
Eric Snow96c6af92015-05-29 22:21:39 -06003293 dv = PyObject_GC_New(_PyDictViewObject, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003294 if (dv == NULL)
3295 return NULL;
3296 Py_INCREF(dict);
3297 dv->dv_dict = (PyDictObject *)dict;
3298 _PyObject_GC_TRACK(dv);
3299 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00003300}
3301
Neal Norwitze36f2ba2007-02-26 23:12:28 +00003302/* TODO(guido): The views objects are not complete:
3303
3304 * support more set operations
3305 * support arbitrary mappings?
3306 - either these should be static or exported in dictobject.h
3307 - if public then they should probably be in builtins
3308*/
3309
Guido van Rossumaac530c2007-08-24 22:33:45 +00003310/* Return 1 if self is a subset of other, iterating over self;
3311 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00003312static int
3313all_contained_in(PyObject *self, PyObject *other)
3314{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003315 PyObject *iter = PyObject_GetIter(self);
3316 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003318 if (iter == NULL)
3319 return -1;
3320 for (;;) {
3321 PyObject *next = PyIter_Next(iter);
3322 if (next == NULL) {
3323 if (PyErr_Occurred())
3324 ok = -1;
3325 break;
3326 }
3327 ok = PySequence_Contains(other, next);
3328 Py_DECREF(next);
3329 if (ok <= 0)
3330 break;
3331 }
3332 Py_DECREF(iter);
3333 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003334}
3335
3336static PyObject *
3337dictview_richcompare(PyObject *self, PyObject *other, int op)
3338{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003339 Py_ssize_t len_self, len_other;
3340 int ok;
3341 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003343 assert(self != NULL);
3344 assert(PyDictViewSet_Check(self));
3345 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00003346
Brian Curtindfc80e32011-08-10 20:28:54 -05003347 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
3348 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003350 len_self = PyObject_Size(self);
3351 if (len_self < 0)
3352 return NULL;
3353 len_other = PyObject_Size(other);
3354 if (len_other < 0)
3355 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003357 ok = 0;
3358 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00003359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003360 case Py_NE:
3361 case Py_EQ:
3362 if (len_self == len_other)
3363 ok = all_contained_in(self, other);
3364 if (op == Py_NE && ok >= 0)
3365 ok = !ok;
3366 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003368 case Py_LT:
3369 if (len_self < len_other)
3370 ok = all_contained_in(self, other);
3371 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003373 case Py_LE:
3374 if (len_self <= len_other)
3375 ok = all_contained_in(self, other);
3376 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003378 case Py_GT:
3379 if (len_self > len_other)
3380 ok = all_contained_in(other, self);
3381 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003383 case Py_GE:
3384 if (len_self >= len_other)
3385 ok = all_contained_in(other, self);
3386 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00003387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003388 }
3389 if (ok < 0)
3390 return NULL;
3391 result = ok ? Py_True : Py_False;
3392 Py_INCREF(result);
3393 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00003394}
3395
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003396static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003397dictview_repr(_PyDictViewObject *dv)
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003398{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003399 PyObject *seq;
3400 PyObject *result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003402 seq = PySequence_List((PyObject *)dv);
3403 if (seq == NULL)
3404 return NULL;
3405
3406 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
3407 Py_DECREF(seq);
3408 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00003409}
3410
Guido van Rossum3ac67412007-02-10 18:55:06 +00003411/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003412
3413static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003414dictkeys_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003415{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003416 if (dv->dv_dict == NULL) {
3417 Py_RETURN_NONE;
3418 }
3419 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003420}
3421
3422static int
Eric Snow96c6af92015-05-29 22:21:39 -06003423dictkeys_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00003424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003425 if (dv->dv_dict == NULL)
3426 return 0;
3427 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003428}
3429
Guido van Rossum83825ac2007-02-10 04:54:19 +00003430static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003431 (lenfunc)dictview_len, /* sq_length */
3432 0, /* sq_concat */
3433 0, /* sq_repeat */
3434 0, /* sq_item */
3435 0, /* sq_slice */
3436 0, /* sq_ass_item */
3437 0, /* sq_ass_slice */
3438 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003439};
3440
Guido van Rossum523259b2007-08-24 23:41:22 +00003441static PyObject*
3442dictviews_sub(PyObject* self, PyObject *other)
3443{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003444 PyObject *result = PySet_New(self);
3445 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003446 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003448 if (result == NULL)
3449 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003450
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003451 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003452 if (tmp == NULL) {
3453 Py_DECREF(result);
3454 return NULL;
3455 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003457 Py_DECREF(tmp);
3458 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003459}
3460
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003461PyObject*
3462_PyDictView_Intersect(PyObject* self, PyObject *other)
Guido van Rossum523259b2007-08-24 23:41:22 +00003463{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003464 PyObject *result = PySet_New(self);
3465 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003466 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003468 if (result == NULL)
3469 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003470
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003471 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_intersection_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003472 if (tmp == NULL) {
3473 Py_DECREF(result);
3474 return NULL;
3475 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003477 Py_DECREF(tmp);
3478 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003479}
3480
3481static PyObject*
3482dictviews_or(PyObject* self, PyObject *other)
3483{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003484 PyObject *result = PySet_New(self);
3485 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02003486 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02003487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003488 if (result == NULL)
3489 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003490
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003491 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003492 if (tmp == NULL) {
3493 Py_DECREF(result);
3494 return NULL;
3495 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003497 Py_DECREF(tmp);
3498 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003499}
3500
3501static PyObject*
3502dictviews_xor(PyObject* self, PyObject *other)
3503{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003504 PyObject *result = PySet_New(self);
3505 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02003506 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02003507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003508 if (result == NULL)
3509 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00003510
Benjamin Petersonf11b25b2016-03-03 22:05:36 -08003511 tmp = _PyObject_CallMethodIdObjArgs(result, &PyId_symmetric_difference_update, other, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003512 if (tmp == NULL) {
3513 Py_DECREF(result);
3514 return NULL;
3515 }
Guido van Rossum523259b2007-08-24 23:41:22 +00003516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003517 Py_DECREF(tmp);
3518 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00003519}
3520
3521static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003522 0, /*nb_add*/
3523 (binaryfunc)dictviews_sub, /*nb_subtract*/
3524 0, /*nb_multiply*/
3525 0, /*nb_remainder*/
3526 0, /*nb_divmod*/
3527 0, /*nb_power*/
3528 0, /*nb_negative*/
3529 0, /*nb_positive*/
3530 0, /*nb_absolute*/
3531 0, /*nb_bool*/
3532 0, /*nb_invert*/
3533 0, /*nb_lshift*/
3534 0, /*nb_rshift*/
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003535 (binaryfunc)_PyDictView_Intersect, /*nb_and*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003536 (binaryfunc)dictviews_xor, /*nb_xor*/
3537 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00003538};
3539
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003540static PyObject*
3541dictviews_isdisjoint(PyObject *self, PyObject *other)
3542{
3543 PyObject *it;
3544 PyObject *item = NULL;
3545
3546 if (self == other) {
Eric Snow96c6af92015-05-29 22:21:39 -06003547 if (dictview_len((_PyDictViewObject *)self) == 0)
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003548 Py_RETURN_TRUE;
3549 else
3550 Py_RETURN_FALSE;
3551 }
3552
3553 /* Iterate over the shorter object (only if other is a set,
3554 * because PySequence_Contains may be expensive otherwise): */
3555 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
Eric Snow96c6af92015-05-29 22:21:39 -06003556 Py_ssize_t len_self = dictview_len((_PyDictViewObject *)self);
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003557 Py_ssize_t len_other = PyObject_Size(other);
3558 if (len_other == -1)
3559 return NULL;
3560
3561 if ((len_other > len_self)) {
3562 PyObject *tmp = other;
3563 other = self;
3564 self = tmp;
3565 }
3566 }
3567
3568 it = PyObject_GetIter(other);
3569 if (it == NULL)
3570 return NULL;
3571
3572 while ((item = PyIter_Next(it)) != NULL) {
3573 int contains = PySequence_Contains(self, item);
3574 Py_DECREF(item);
3575 if (contains == -1) {
3576 Py_DECREF(it);
3577 return NULL;
3578 }
3579
3580 if (contains) {
3581 Py_DECREF(it);
3582 Py_RETURN_FALSE;
3583 }
3584 }
3585 Py_DECREF(it);
3586 if (PyErr_Occurred())
3587 return NULL; /* PyIter_Next raised an exception. */
3588 Py_RETURN_TRUE;
3589}
3590
3591PyDoc_STRVAR(isdisjoint_doc,
3592"Return True if the view and the given iterable have a null intersection.");
3593
Guido van Rossumb90c8482007-02-10 01:11:45 +00003594static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003595 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3596 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003597 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003598};
3599
3600PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003601 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3602 "dict_keys", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06003603 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003604 0, /* tp_itemsize */
3605 /* methods */
3606 (destructor)dictview_dealloc, /* tp_dealloc */
3607 0, /* tp_print */
3608 0, /* tp_getattr */
3609 0, /* tp_setattr */
3610 0, /* tp_reserved */
3611 (reprfunc)dictview_repr, /* tp_repr */
3612 &dictviews_as_number, /* tp_as_number */
3613 &dictkeys_as_sequence, /* tp_as_sequence */
3614 0, /* tp_as_mapping */
3615 0, /* tp_hash */
3616 0, /* tp_call */
3617 0, /* tp_str */
3618 PyObject_GenericGetAttr, /* tp_getattro */
3619 0, /* tp_setattro */
3620 0, /* tp_as_buffer */
3621 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3622 0, /* tp_doc */
3623 (traverseproc)dictview_traverse, /* tp_traverse */
3624 0, /* tp_clear */
3625 dictview_richcompare, /* tp_richcompare */
3626 0, /* tp_weaklistoffset */
3627 (getiterfunc)dictkeys_iter, /* tp_iter */
3628 0, /* tp_iternext */
3629 dictkeys_methods, /* tp_methods */
3630 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003631};
3632
3633static PyObject *
3634dictkeys_new(PyObject *dict)
3635{
Eric Snow96c6af92015-05-29 22:21:39 -06003636 return _PyDictView_New(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003637}
3638
Guido van Rossum3ac67412007-02-10 18:55:06 +00003639/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003640
3641static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003642dictitems_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003643{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003644 if (dv->dv_dict == NULL) {
3645 Py_RETURN_NONE;
3646 }
3647 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003648}
3649
3650static int
Eric Snow96c6af92015-05-29 22:21:39 -06003651dictitems_contains(_PyDictViewObject *dv, PyObject *obj)
Guido van Rossum3ac67412007-02-10 18:55:06 +00003652{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003653 PyObject *key, *value, *found;
3654 if (dv->dv_dict == NULL)
3655 return 0;
3656 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
3657 return 0;
3658 key = PyTuple_GET_ITEM(obj, 0);
3659 value = PyTuple_GET_ITEM(obj, 1);
3660 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
3661 if (found == NULL) {
3662 if (PyErr_Occurred())
3663 return -1;
3664 return 0;
3665 }
3666 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003667}
3668
Guido van Rossum83825ac2007-02-10 04:54:19 +00003669static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003670 (lenfunc)dictview_len, /* sq_length */
3671 0, /* sq_concat */
3672 0, /* sq_repeat */
3673 0, /* sq_item */
3674 0, /* sq_slice */
3675 0, /* sq_ass_item */
3676 0, /* sq_ass_slice */
3677 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003678};
3679
Guido van Rossumb90c8482007-02-10 01:11:45 +00003680static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003681 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3682 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003683 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003684};
3685
3686PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003687 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3688 "dict_items", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06003689 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003690 0, /* tp_itemsize */
3691 /* methods */
3692 (destructor)dictview_dealloc, /* tp_dealloc */
3693 0, /* tp_print */
3694 0, /* tp_getattr */
3695 0, /* tp_setattr */
3696 0, /* tp_reserved */
3697 (reprfunc)dictview_repr, /* tp_repr */
3698 &dictviews_as_number, /* tp_as_number */
3699 &dictitems_as_sequence, /* tp_as_sequence */
3700 0, /* tp_as_mapping */
3701 0, /* tp_hash */
3702 0, /* tp_call */
3703 0, /* tp_str */
3704 PyObject_GenericGetAttr, /* tp_getattro */
3705 0, /* tp_setattro */
3706 0, /* tp_as_buffer */
3707 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3708 0, /* tp_doc */
3709 (traverseproc)dictview_traverse, /* tp_traverse */
3710 0, /* tp_clear */
3711 dictview_richcompare, /* tp_richcompare */
3712 0, /* tp_weaklistoffset */
3713 (getiterfunc)dictitems_iter, /* tp_iter */
3714 0, /* tp_iternext */
3715 dictitems_methods, /* tp_methods */
3716 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003717};
3718
3719static PyObject *
3720dictitems_new(PyObject *dict)
3721{
Eric Snow96c6af92015-05-29 22:21:39 -06003722 return _PyDictView_New(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003723}
3724
Guido van Rossum3ac67412007-02-10 18:55:06 +00003725/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003726
3727static PyObject *
Eric Snow96c6af92015-05-29 22:21:39 -06003728dictvalues_iter(_PyDictViewObject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003729{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003730 if (dv->dv_dict == NULL) {
3731 Py_RETURN_NONE;
3732 }
3733 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003734}
3735
Guido van Rossum83825ac2007-02-10 04:54:19 +00003736static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003737 (lenfunc)dictview_len, /* sq_length */
3738 0, /* sq_concat */
3739 0, /* sq_repeat */
3740 0, /* sq_item */
3741 0, /* sq_slice */
3742 0, /* sq_ass_item */
3743 0, /* sq_ass_slice */
3744 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003745};
3746
Guido van Rossumb90c8482007-02-10 01:11:45 +00003747static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003748 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003749};
3750
3751PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003752 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3753 "dict_values", /* tp_name */
Eric Snow96c6af92015-05-29 22:21:39 -06003754 sizeof(_PyDictViewObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003755 0, /* tp_itemsize */
3756 /* methods */
3757 (destructor)dictview_dealloc, /* tp_dealloc */
3758 0, /* tp_print */
3759 0, /* tp_getattr */
3760 0, /* tp_setattr */
3761 0, /* tp_reserved */
3762 (reprfunc)dictview_repr, /* tp_repr */
3763 0, /* tp_as_number */
3764 &dictvalues_as_sequence, /* tp_as_sequence */
3765 0, /* tp_as_mapping */
3766 0, /* tp_hash */
3767 0, /* tp_call */
3768 0, /* tp_str */
3769 PyObject_GenericGetAttr, /* tp_getattro */
3770 0, /* tp_setattro */
3771 0, /* tp_as_buffer */
3772 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3773 0, /* tp_doc */
3774 (traverseproc)dictview_traverse, /* tp_traverse */
3775 0, /* tp_clear */
3776 0, /* tp_richcompare */
3777 0, /* tp_weaklistoffset */
3778 (getiterfunc)dictvalues_iter, /* tp_iter */
3779 0, /* tp_iternext */
3780 dictvalues_methods, /* tp_methods */
3781 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003782};
3783
3784static PyObject *
3785dictvalues_new(PyObject *dict)
3786{
Eric Snow96c6af92015-05-29 22:21:39 -06003787 return _PyDictView_New(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003788}
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003789
3790/* Returns NULL if cannot allocate a new PyDictKeysObject,
3791 but does not set an error */
3792PyDictKeysObject *
3793_PyDict_NewKeysForClass(void)
3794{
3795 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE_SPLIT);
3796 if (keys == NULL)
3797 PyErr_Clear();
3798 else
3799 keys->dk_lookup = lookdict_split;
3800 return keys;
3801}
3802
3803#define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
3804
3805PyObject *
3806PyObject_GenericGetDict(PyObject *obj, void *context)
3807{
3808 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
3809 if (dictptr == NULL) {
3810 PyErr_SetString(PyExc_AttributeError,
3811 "This object has no __dict__");
3812 return NULL;
3813 }
3814 dict = *dictptr;
3815 if (dict == NULL) {
3816 PyTypeObject *tp = Py_TYPE(obj);
3817 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
3818 DK_INCREF(CACHED_KEYS(tp));
3819 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
3820 }
3821 else {
3822 *dictptr = dict = PyDict_New();
3823 }
3824 }
3825 Py_XINCREF(dict);
3826 return dict;
3827}
3828
3829int
3830_PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
3831 PyObject *key, PyObject *value)
3832{
3833 PyObject *dict;
3834 int res;
3835 PyDictKeysObject *cached;
3836
3837 assert(dictptr != NULL);
3838 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
3839 assert(dictptr != NULL);
3840 dict = *dictptr;
3841 if (dict == NULL) {
3842 DK_INCREF(cached);
3843 dict = new_dict_with_shared_keys(cached);
3844 if (dict == NULL)
3845 return -1;
3846 *dictptr = dict;
3847 }
3848 if (value == NULL) {
3849 res = PyDict_DelItem(dict, key);
3850 if (cached != ((PyDictObject *)dict)->ma_keys) {
3851 CACHED_KEYS(tp) = NULL;
3852 DK_DECREF(cached);
3853 }
3854 } else {
3855 res = PyDict_SetItem(dict, key, value);
3856 if (cached != ((PyDictObject *)dict)->ma_keys) {
3857 /* Either update tp->ht_cached_keys or delete it */
Benjamin Peterson15ee8212012-04-24 14:44:18 -04003858 if (cached->dk_refcnt == 1) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003859 CACHED_KEYS(tp) = make_keys_shared(dict);
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003860 } else {
3861 CACHED_KEYS(tp) = NULL;
3862 }
3863 DK_DECREF(cached);
Benjamin Peterson15ee8212012-04-24 14:44:18 -04003864 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
3865 return -1;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003866 }
3867 }
3868 } else {
3869 dict = *dictptr;
3870 if (dict == NULL) {
3871 dict = PyDict_New();
3872 if (dict == NULL)
3873 return -1;
3874 *dictptr = dict;
3875 }
3876 if (value == NULL) {
3877 res = PyDict_DelItem(dict, key);
3878 } else {
3879 res = PyDict_SetItem(dict, key, value);
3880 }
3881 }
3882 return res;
3883}
3884
3885void
3886_PyDictKeys_DecRef(PyDictKeysObject *keys)
3887{
3888 DK_DECREF(keys);
3889}
3890
3891
3892/* ARGSUSED */
3893static PyObject *
3894dummy_repr(PyObject *op)
3895{
3896 return PyUnicode_FromString("<dummy key>");
3897}
3898
3899/* ARGUSED */
3900static void
3901dummy_dealloc(PyObject* ignore)
3902{
3903 /* This should never get called, but we also don't want to SEGV if
3904 * we accidentally decref dummy-key out of existence.
3905 */
3906 Py_FatalError("deallocating <dummy key>");
3907}
3908
3909static PyTypeObject PyDictDummy_Type = {
3910 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3911 "<dummy key> type",
3912 0,
3913 0,
3914 dummy_dealloc, /*tp_dealloc*/ /*never called*/
3915 0, /*tp_print*/
3916 0, /*tp_getattr*/
3917 0, /*tp_setattr*/
3918 0, /*tp_reserved*/
3919 dummy_repr, /*tp_repr*/
3920 0, /*tp_as_number*/
3921 0, /*tp_as_sequence*/
3922 0, /*tp_as_mapping*/
3923 0, /*tp_hash */
3924 0, /*tp_call */
3925 0, /*tp_str */
3926 0, /*tp_getattro */
3927 0, /*tp_setattro */
3928 0, /*tp_as_buffer */
3929 Py_TPFLAGS_DEFAULT, /*tp_flags */
3930};
3931
3932static PyObject _dummy_struct = {
3933 _PyObject_EXTRA_INIT
3934 2, &PyDictDummy_Type
3935};
3936