blob: 82735e6753b348373c44b99db927f414dd2cb814 [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
Guido van Rossumc0b618a1997-05-02 03:12:38 +000010#include "Python.h"
Christian Heimes0ded5b52007-12-10 15:50:56 +000011#include "stringlib/eq.h"
Guido van Rossum4b1302b1993-03-27 18:11:32 +000012
Guido van Rossum16e93a81997-01-28 00:00:11 +000013
Thomas Wouters89f507f2006-12-13 04:49:30 +000014/* Set a key error with the specified argument, wrapping it in a
15 * tuple automatically so that tuple keys are not unpacked as the
16 * exception arguments. */
17static void
18set_key_error(PyObject *arg)
19{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000020 PyObject *tup;
21 tup = PyTuple_Pack(1, arg);
22 if (!tup)
23 return; /* caller will expect error to be set anyway */
24 PyErr_SetObject(PyExc_KeyError, tup);
25 Py_DECREF(tup);
Thomas Wouters89f507f2006-12-13 04:49:30 +000026}
27
Tim Peterseb28ef22001-06-02 05:27:19 +000028/* Define this out if you don't want conversion statistics on exit. */
Fred Drake1bff34a2000-08-31 19:31:38 +000029#undef SHOW_CONVERSION_COUNTS
30
Tim Peterseb28ef22001-06-02 05:27:19 +000031/* See large comment block below. This must be >= 1. */
32#define PERTURB_SHIFT 5
33
Guido van Rossum16e93a81997-01-28 00:00:11 +000034/*
Tim Peterseb28ef22001-06-02 05:27:19 +000035Major subtleties ahead: Most hash schemes depend on having a "good" hash
36function, in the sense of simulating randomness. Python doesn't: its most
37important hash functions (for strings and ints) are very regular in common
38cases:
Tim Peters15d49292001-05-27 07:39:22 +000039
Guido van Rossumdc5f6b22006-08-24 21:29:26 +000040 >>> map(hash, (0, 1, 2, 3))
41 [0, 1, 2, 3]
42 >>> map(hash, ("namea", "nameb", "namec", "named"))
43 [-1658398457, -1658398460, -1658398459, -1658398462]
44 >>>
Tim Peters15d49292001-05-27 07:39:22 +000045
Tim Peterseb28ef22001-06-02 05:27:19 +000046This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
47the low-order i bits as the initial table index is extremely fast, and there
48are no collisions at all for dicts indexed by a contiguous range of ints.
49The same is approximately true when keys are "consecutive" strings. So this
50gives better-than-random behavior in common cases, and that's very desirable.
Tim Peters15d49292001-05-27 07:39:22 +000051
Tim Peterseb28ef22001-06-02 05:27:19 +000052OTOH, when collisions occur, the tendency to fill contiguous slices of the
53hash table makes a good collision resolution strategy crucial. Taking only
54the last i bits of the hash code is also vulnerable: for example, consider
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000055the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
Guido van Rossumdc5f6b22006-08-24 21:29:26 +000056their own hash codes, and this fits in a dict of size 2**15, the last 15 bits
57 of every hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +000058
Tim Peterseb28ef22001-06-02 05:27:19 +000059But catering to unusual cases should not slow the usual ones, so we just take
60the last i bits anyway. It's up to collision resolution to do the rest. If
61we *usually* find the key we're looking for on the first try (and, it turns
62out, we usually do -- the table load factor is kept under 2/3, so the odds
63are solidly in our favor), then it makes best sense to keep the initial index
64computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +000065
Tim Peterseb28ef22001-06-02 05:27:19 +000066The first half of collision resolution is to visit table indices via this
67recurrence:
Tim Peters15d49292001-05-27 07:39:22 +000068
Tim Peterseb28ef22001-06-02 05:27:19 +000069 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +000070
Tim Peterseb28ef22001-06-02 05:27:19 +000071For any initial j in range(2**i), repeating that 2**i times generates each
72int in range(2**i) exactly once (see any text on random-number generation for
73proof). By itself, this doesn't help much: like linear probing (setting
74j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
75order. This would be bad, except that's not the only thing we do, and it's
76actually *good* in the common cases where hash keys are consecutive. In an
77example that's really too small to make this entirely clear, for a table of
78size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +000079
Tim Peterseb28ef22001-06-02 05:27:19 +000080 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
81
82If two things come in at index 5, the first place we look after is index 2,
83not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
84Linear probing is deadly in this case because there the fixed probe order
85is the *same* as the order consecutive keys are likely to arrive. But it's
86extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
87and certain that consecutive hash codes do not.
88
89The other half of the strategy is to get the other bits of the hash code
90into play. This is done by initializing a (unsigned) vrbl "perturb" to the
91full hash code, and changing the recurrence to:
92
93 j = (5*j) + 1 + perturb;
94 perturb >>= PERTURB_SHIFT;
95 use j % 2**i as the next table index;
96
97Now the probe sequence depends (eventually) on every bit in the hash code,
98and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
99because it quickly magnifies small differences in the bits that didn't affect
100the initial index. Note that because perturb is unsigned, if the recurrence
101is executed often enough perturb eventually becomes and remains 0. At that
102point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
103that's certain to find an empty slot eventually (since it generates every int
104in range(2**i), and we make sure there's always at least one empty slot).
105
106Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
107small so that the high bits of the hash code continue to affect the probe
108sequence across iterations; but you want it large so that in really bad cases
109the high-order hash bits have an effect on early iterations. 5 was "the
110best" in minimizing total collisions across experiments Tim Peters ran (on
111both normal and pathological cases), but 4 and 6 weren't significantly worse.
112
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000113Historical: Reimer Behrends contributed the idea of using a polynomial-based
Tim Peterseb28ef22001-06-02 05:27:19 +0000114approach, using repeated multiplication by x in GF(2**n) where an irreducible
115polynomial for each table size was chosen such that x was a primitive root.
116Christian Tismer later extended that to use division by x instead, as an
117efficient way to get the high bits of the hash code into play. This scheme
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000118also gave excellent collision statistics, but was more expensive: two
119if-tests were required inside the loop; computing "the next" index took about
120the same number of operations but without as much potential parallelism
121(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
122above, and then shifting perturb can be done while the table index is being
123masked); and the PyDictObject struct required a member to hold the table's
124polynomial. In Tim's experiments the current scheme ran faster, produced
125equally good collision statistics, needed less code & used less memory.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000126
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000127*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000128
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000129/* Object used as dummy key to fill deleted entries */
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000130static PyObject *dummy = NULL; /* Initialized by first call to newPyDictObject() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000131
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000132#ifdef Py_REF_DEBUG
133PyObject *
134_PyDict_Dummy(void)
135{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 return dummy;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000137}
138#endif
139
Fred Drake1bff34a2000-08-31 19:31:38 +0000140/* forward declarations */
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000141static PyDictEntry *
Benjamin Petersone6baa462010-10-17 21:20:58 +0000142lookdict_unicode(PyDictObject *mp, PyObject *key, Py_hash_t hash);
Fred Drake1bff34a2000-08-31 19:31:38 +0000143
144#ifdef SHOW_CONVERSION_COUNTS
145static long created = 0L;
146static long converted = 0L;
147
148static void
149show_counts(void)
150{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 fprintf(stderr, "created %ld string dicts\n", created);
152 fprintf(stderr, "converted %ld to normal dicts\n", converted);
153 fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created);
Fred Drake1bff34a2000-08-31 19:31:38 +0000154}
155#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000156
Christian Heimes77c02eb2008-02-09 02:18:51 +0000157/* Debug statistic to compare allocations with reuse through the free list */
158#undef SHOW_ALLOC_COUNT
159#ifdef SHOW_ALLOC_COUNT
160static size_t count_alloc = 0;
161static size_t count_reuse = 0;
162
163static void
164show_alloc(void)
165{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 fprintf(stderr, "Dict allocations: %" PY_FORMAT_SIZE_T "d\n",
167 count_alloc);
168 fprintf(stderr, "Dict reuse through freelist: %" PY_FORMAT_SIZE_T
169 "d\n", count_reuse);
170 fprintf(stderr, "%.2f%% reuse rate\n\n",
171 (100.0*count_reuse/(count_alloc+count_reuse)));
Christian Heimes77c02eb2008-02-09 02:18:51 +0000172}
173#endif
174
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000175/* Debug statistic to count GC tracking of dicts */
176#ifdef SHOW_TRACK_COUNT
177static Py_ssize_t count_untracked = 0;
178static Py_ssize_t count_tracked = 0;
179
180static void
181show_track(void)
182{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 fprintf(stderr, "Dicts created: %" PY_FORMAT_SIZE_T "d\n",
184 count_tracked + count_untracked);
185 fprintf(stderr, "Dicts tracked by the GC: %" PY_FORMAT_SIZE_T
186 "d\n", count_tracked);
187 fprintf(stderr, "%.2f%% dict tracking rate\n\n",
188 (100.0*count_tracked/(count_untracked+count_tracked)));
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000189}
190#endif
191
192
Tim Peters6d6c1a32001-08-02 04:15:00 +0000193/* Initialization macros.
194 There are two ways to create a dict: PyDict_New() is the main C API
195 function, and the tp_new slot maps to dict_new(). In the latter case we
196 can save a little time over what PyDict_New does because it's guaranteed
197 that the PyDictObject struct is already zeroed out.
198 Everyone except dict_new() should use EMPTY_TO_MINSIZE (unless they have
199 an excellent reason not to).
200*/
201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202#define INIT_NONZERO_DICT_SLOTS(mp) do { \
203 (mp)->ma_table = (mp)->ma_smalltable; \
204 (mp)->ma_mask = PyDict_MINSIZE - 1; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000205 } while(0)
206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207#define EMPTY_TO_MINSIZE(mp) do { \
208 memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \
209 (mp)->ma_used = (mp)->ma_fill = 0; \
210 INIT_NONZERO_DICT_SLOTS(mp); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000211 } while(0)
212
Raymond Hettinger43442782004-03-17 21:55:03 +0000213/* Dictionary reuse scheme to save calls to malloc, free, and memset */
Christian Heimes2202f872008-02-06 14:31:34 +0000214#ifndef PyDict_MAXFREELIST
215#define PyDict_MAXFREELIST 80
216#endif
217static PyDictObject *free_list[PyDict_MAXFREELIST];
218static int numfree = 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000219
Christian Heimes77c02eb2008-02-09 02:18:51 +0000220void
221PyDict_Fini(void)
222{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 PyDictObject *op;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 while (numfree) {
226 op = free_list[--numfree];
227 assert(PyDict_CheckExact(op));
228 PyObject_GC_Del(op);
229 }
Christian Heimes77c02eb2008-02-09 02:18:51 +0000230}
231
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000232PyObject *
Thomas Wouters78890102000-07-22 19:25:51 +0000233PyDict_New(void)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000234{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 register PyDictObject *mp;
236 if (dummy == NULL) { /* Auto-initialize dummy */
237 dummy = PyUnicode_FromString("<dummy key>");
238 if (dummy == NULL)
239 return NULL;
Fred Drake1bff34a2000-08-31 19:31:38 +0000240#ifdef SHOW_CONVERSION_COUNTS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 Py_AtExit(show_counts);
Fred Drake1bff34a2000-08-31 19:31:38 +0000242#endif
Christian Heimes77c02eb2008-02-09 02:18:51 +0000243#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 Py_AtExit(show_alloc);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000245#endif
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000246#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 Py_AtExit(show_track);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000248#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 }
250 if (numfree) {
251 mp = free_list[--numfree];
252 assert (mp != NULL);
253 assert (Py_TYPE(mp) == &PyDict_Type);
254 _Py_NewReference((PyObject *)mp);
255 if (mp->ma_fill) {
256 EMPTY_TO_MINSIZE(mp);
257 } else {
258 /* At least set ma_table and ma_mask; these are wrong
259 if an empty but presized dict is added to freelist */
260 INIT_NONZERO_DICT_SLOTS(mp);
261 }
262 assert (mp->ma_used == 0);
263 assert (mp->ma_table == mp->ma_smalltable);
264 assert (mp->ma_mask == PyDict_MINSIZE - 1);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000265#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 count_reuse++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000267#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 } else {
269 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
270 if (mp == NULL)
271 return NULL;
272 EMPTY_TO_MINSIZE(mp);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000273#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 count_alloc++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000275#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 }
277 mp->ma_lookup = lookdict_unicode;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000278#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 count_untracked++;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000280#endif
Fred Drake1bff34a2000-08-31 19:31:38 +0000281#ifdef SHOW_CONVERSION_COUNTS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 ++created;
Fred Drake1bff34a2000-08-31 19:31:38 +0000283#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000285}
286
287/*
288The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000289This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000290Open addressing is preferred over chaining since the link overhead for
291chaining would be substantial (100% with typical malloc overhead).
292
Tim Peterseb28ef22001-06-02 05:27:19 +0000293The initial probe index is computed as hash mod the table size. Subsequent
294probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000295
296All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000297
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000298The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000299contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000300Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000301
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000302lookdict() is general-purpose, and may return NULL if (and only if) a
303comparison raises an exception (this was new in Python 2.5).
Guido van Rossum89d8c602007-09-18 17:26:56 +0000304lookdict_unicode() below is specialized to string keys, comparison of which can
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000305never raise an exception; that function can never return NULL. For both, when
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000306the key isn't found a PyDictEntry* is returned for which the me_value field is
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000307NULL; this is the slot in the dict at which the key would have been found, and
308the caller can (if it wishes) add the <key, value> pair to the returned
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000309PyDictEntry*.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000310*/
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000311static PyDictEntry *
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000312lookdict(PyDictObject *mp, PyObject *key, register Py_hash_t hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000313{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 register size_t i;
315 register size_t perturb;
316 register PyDictEntry *freeslot;
317 register size_t mask = (size_t)mp->ma_mask;
318 PyDictEntry *ep0 = mp->ma_table;
319 register PyDictEntry *ep;
320 register int cmp;
321 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 i = (size_t)hash & mask;
324 ep = &ep0[i];
325 if (ep->me_key == NULL || ep->me_key == key)
326 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 if (ep->me_key == dummy)
329 freeslot = ep;
330 else {
331 if (ep->me_hash == hash) {
332 startkey = ep->me_key;
333 Py_INCREF(startkey);
334 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
335 Py_DECREF(startkey);
336 if (cmp < 0)
337 return NULL;
338 if (ep0 == mp->ma_table && ep->me_key == startkey) {
339 if (cmp > 0)
340 return ep;
341 }
342 else {
343 /* The compare did major nasty stuff to the
344 * dict: start over.
345 * XXX A clever adversary could prevent this
346 * XXX from terminating.
347 */
348 return lookdict(mp, key, hash);
349 }
350 }
351 freeslot = NULL;
352 }
Tim Peters15d49292001-05-27 07:39:22 +0000353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 /* In the loop, me_key == dummy is by far (factor of 100s) the
355 least likely outcome, so test for that last. */
356 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
357 i = (i << 2) + i + perturb + 1;
358 ep = &ep0[i & mask];
359 if (ep->me_key == NULL)
360 return freeslot == NULL ? ep : freeslot;
361 if (ep->me_key == key)
362 return ep;
363 if (ep->me_hash == hash && ep->me_key != dummy) {
364 startkey = ep->me_key;
365 Py_INCREF(startkey);
366 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
367 Py_DECREF(startkey);
368 if (cmp < 0)
369 return NULL;
370 if (ep0 == mp->ma_table && ep->me_key == startkey) {
371 if (cmp > 0)
372 return ep;
373 }
374 else {
375 /* The compare did major nasty stuff to the
376 * dict: start over.
377 * XXX A clever adversary could prevent this
378 * XXX from terminating.
379 */
380 return lookdict(mp, key, hash);
381 }
382 }
383 else if (ep->me_key == dummy && freeslot == NULL)
384 freeslot = ep;
385 }
386 assert(0); /* NOT REACHED */
387 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000388}
389
390/*
Guido van Rossum89d8c602007-09-18 17:26:56 +0000391 * Hacked up version of lookdict which can assume keys are always
392 * unicodes; this assumption allows testing for errors during
393 * PyObject_RichCompareBool() to be dropped; unicode-unicode
394 * comparisons never raise exceptions. This also means we don't need
395 * to go through PyObject_RichCompareBool(); we can always use
396 * unicode_eq() directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000397 *
Guido van Rossum89d8c602007-09-18 17:26:56 +0000398 * This is valuable because dicts with only unicode keys are very common.
Fred Drake1bff34a2000-08-31 19:31:38 +0000399 */
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000400static PyDictEntry *
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000401lookdict_unicode(PyDictObject *mp, PyObject *key, register Py_hash_t hash)
Fred Drake1bff34a2000-08-31 19:31:38 +0000402{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 register size_t i;
404 register size_t perturb;
405 register PyDictEntry *freeslot;
406 register size_t mask = (size_t)mp->ma_mask;
407 PyDictEntry *ep0 = mp->ma_table;
408 register PyDictEntry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 /* Make sure this function doesn't have to handle non-unicode keys,
411 including subclasses of str; e.g., one reason to subclass
412 unicodes is to override __eq__, and for speed we don't cater to
413 that here. */
414 if (!PyUnicode_CheckExact(key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000415#ifdef SHOW_CONVERSION_COUNTS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 ++converted;
Fred Drake1bff34a2000-08-31 19:31:38 +0000417#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 mp->ma_lookup = lookdict;
419 return lookdict(mp, key, hash);
420 }
Mark Dickinson57e683e2011-09-24 18:18:40 +0100421 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 ep = &ep0[i];
423 if (ep->me_key == NULL || ep->me_key == key)
424 return ep;
425 if (ep->me_key == dummy)
426 freeslot = ep;
427 else {
428 if (ep->me_hash == hash && unicode_eq(ep->me_key, key))
429 return ep;
430 freeslot = NULL;
431 }
Tim Peters15d49292001-05-27 07:39:22 +0000432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 /* In the loop, me_key == dummy is by far (factor of 100s) the
434 least likely outcome, so test for that last. */
435 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
436 i = (i << 2) + i + perturb + 1;
437 ep = &ep0[i & mask];
438 if (ep->me_key == NULL)
439 return freeslot == NULL ? ep : freeslot;
440 if (ep->me_key == key
441 || (ep->me_hash == hash
442 && ep->me_key != dummy
443 && unicode_eq(ep->me_key, key)))
444 return ep;
445 if (ep->me_key == dummy && freeslot == NULL)
446 freeslot = ep;
447 }
448 assert(0); /* NOT REACHED */
449 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000450}
451
Benjamin Petersonfb886362010-04-24 18:21:17 +0000452int
453_PyDict_HasOnlyStringKeys(PyObject *dict)
454{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 Py_ssize_t pos = 0;
456 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000457 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 /* Shortcut */
459 if (((PyDictObject *)dict)->ma_lookup == lookdict_unicode)
460 return 1;
461 while (PyDict_Next(dict, &pos, &key, &value))
462 if (!PyUnicode_Check(key))
463 return 0;
464 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000465}
466
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000467#ifdef SHOW_TRACK_COUNT
468#define INCREASE_TRACK_COUNT \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 (count_tracked++, count_untracked--);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000470#define DECREASE_TRACK_COUNT \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 (count_tracked--, count_untracked++);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000472#else
473#define INCREASE_TRACK_COUNT
474#define DECREASE_TRACK_COUNT
475#endif
476
477#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 do { \
479 if (!_PyObject_GC_IS_TRACKED(mp)) { \
480 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
481 _PyObject_GC_MAY_BE_TRACKED(value)) { \
482 _PyObject_GC_TRACK(mp); \
483 INCREASE_TRACK_COUNT \
484 } \
485 } \
486 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000487
488void
489_PyDict_MaybeUntrack(PyObject *op)
490{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 PyDictObject *mp;
492 PyObject *value;
493 Py_ssize_t mask, i;
494 PyDictEntry *ep;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
497 return;
498
499 mp = (PyDictObject *) op;
500 ep = mp->ma_table;
501 mask = mp->ma_mask;
502 for (i = 0; i <= mask; i++) {
503 if ((value = ep[i].me_value) == NULL)
504 continue;
505 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
506 _PyObject_GC_MAY_BE_TRACKED(ep[i].me_key))
507 return;
508 }
509 DECREASE_TRACK_COUNT
510 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000511}
512
513
Fred Drake1bff34a2000-08-31 19:31:38 +0000514/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000515Internal routine to insert a new item into the table.
516Used both by the internal resize routine and by the public insert routine.
517Eats a reference to key and one to value.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000518Returns -1 if an error occurred, or 0 on success.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000519*/
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000520static int
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000521insertdict(register PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000522{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 PyObject *old_value;
524 register PyDictEntry *ep;
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000525 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, Py_hash_t);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 assert(mp->ma_lookup != NULL);
528 ep = mp->ma_lookup(mp, key, hash);
529 if (ep == NULL) {
530 Py_DECREF(key);
531 Py_DECREF(value);
532 return -1;
533 }
534 MAINTAIN_TRACKING(mp, key, value);
535 if (ep->me_value != NULL) {
536 old_value = ep->me_value;
537 ep->me_value = value;
538 Py_DECREF(old_value); /* which **CAN** re-enter */
539 Py_DECREF(key);
540 }
541 else {
542 if (ep->me_key == NULL)
543 mp->ma_fill++;
544 else {
545 assert(ep->me_key == dummy);
546 Py_DECREF(dummy);
547 }
548 ep->me_key = key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000549 ep->me_hash = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 ep->me_value = value;
551 mp->ma_used++;
552 }
553 return 0;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000554}
555
556/*
557Internal routine used by dictresize() to insert an item which is
558known to be absent from the dict. This routine also assumes that
559the dict contains no deleted entries. Besides the performance benefit,
560using insertdict() in dictresize() is dangerous (SF bug #1456209).
561Note that no refcounts are changed by this routine; if needed, the caller
562is responsible for incref'ing `key` and `value`.
563*/
564static void
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000565insertdict_clean(register PyDictObject *mp, PyObject *key, Py_hash_t hash,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 PyObject *value)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000567{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 register size_t i;
569 register size_t perturb;
570 register size_t mask = (size_t)mp->ma_mask;
571 PyDictEntry *ep0 = mp->ma_table;
572 register PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 MAINTAIN_TRACKING(mp, key, value);
Mark Dickinson57e683e2011-09-24 18:18:40 +0100575 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 ep = &ep0[i];
577 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
578 i = (i << 2) + i + perturb + 1;
579 ep = &ep0[i & mask];
580 }
581 assert(ep->me_value == NULL);
582 mp->ma_fill++;
583 ep->me_key = key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000584 ep->me_hash = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 ep->me_value = value;
586 mp->ma_used++;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000587}
588
589/*
590Restructure the table by allocating a new table and reinserting all
591items again. When entries have been deleted, the new table may
592actually be smaller than the old one.
593*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000594static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000595dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000596{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 Py_ssize_t newsize;
598 PyDictEntry *oldtable, *newtable, *ep;
599 Py_ssize_t i;
600 int is_oldtable_malloced;
601 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 /* Find the smallest table size > minused. */
606 for (newsize = PyDict_MINSIZE;
607 newsize <= minused && newsize > 0;
608 newsize <<= 1)
609 ;
610 if (newsize <= 0) {
611 PyErr_NoMemory();
612 return -1;
613 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 /* Get space for a new table. */
616 oldtable = mp->ma_table;
617 assert(oldtable != NULL);
618 is_oldtable_malloced = oldtable != mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 if (newsize == PyDict_MINSIZE) {
621 /* A large table is shrinking, or we can't get any smaller. */
622 newtable = mp->ma_smalltable;
623 if (newtable == oldtable) {
624 if (mp->ma_fill == mp->ma_used) {
625 /* No dummies, so no point doing anything. */
626 return 0;
627 }
628 /* We're not going to resize it, but rebuild the
629 table anyway to purge old dummy entries.
630 Subtle: This is *necessary* if fill==size,
631 as lookdict needs at least one virgin slot to
632 terminate failing searches. If fill < size, it's
633 merely desirable, as dummies slow searches. */
634 assert(mp->ma_fill > mp->ma_used);
635 memcpy(small_copy, oldtable, sizeof(small_copy));
636 oldtable = small_copy;
637 }
638 }
639 else {
640 newtable = PyMem_NEW(PyDictEntry, newsize);
641 if (newtable == NULL) {
642 PyErr_NoMemory();
643 return -1;
644 }
645 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 /* Make the dict empty, using the new table. */
648 assert(newtable != oldtable);
649 mp->ma_table = newtable;
650 mp->ma_mask = newsize - 1;
651 memset(newtable, 0, sizeof(PyDictEntry) * newsize);
652 mp->ma_used = 0;
653 i = mp->ma_fill;
654 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 /* Copy the data over; this is refcount-neutral for active entries;
657 dummy entries aren't copied over, of course */
658 for (ep = oldtable; i > 0; ep++) {
659 if (ep->me_value != NULL) { /* active entry */
660 --i;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000661 insertdict_clean(mp, ep->me_key, ep->me_hash, ep->me_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 }
663 else if (ep->me_key != NULL) { /* dummy entry */
664 --i;
665 assert(ep->me_key == dummy);
666 Py_DECREF(ep->me_key);
667 }
668 /* else key == value == NULL: nothing to do */
669 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 if (is_oldtable_malloced)
672 PyMem_DEL(oldtable);
673 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000674}
675
Christian Heimes99170a52007-12-19 02:07:34 +0000676/* Create a new dictionary pre-sized to hold an estimated number of elements.
677 Underestimates are okay because the dictionary will resize as necessary.
678 Overestimates just mean the dictionary will be more sparse than usual.
679*/
680
681PyObject *
682_PyDict_NewPresized(Py_ssize_t minused)
683{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 PyObject *op = PyDict_New();
Christian Heimes99170a52007-12-19 02:07:34 +0000685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 if (minused>5 && op != NULL && dictresize((PyDictObject *)op, minused) == -1) {
687 Py_DECREF(op);
688 return NULL;
689 }
690 return op;
Christian Heimes99170a52007-12-19 02:07:34 +0000691}
692
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000693/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
694 * that may occur (originally dicts supported only string keys, and exceptions
695 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000696 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000697 * (suppressed) occurred while computing the key's hash, or that some error
698 * (suppressed) occurred when comparing keys in the dict's internal probe
699 * sequence. A nasty example of the latter is when a Python-coded comparison
700 * function hits a stack-depth error, which can cause this to return NULL
701 * even if the key is present.
702 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000703PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000704PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000705{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000706 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 PyDictObject *mp = (PyDictObject *)op;
708 PyDictEntry *ep;
709 PyThreadState *tstate;
710 if (!PyDict_Check(op))
711 return NULL;
712 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200713 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 {
715 hash = PyObject_Hash(key);
716 if (hash == -1) {
717 PyErr_Clear();
718 return NULL;
719 }
720 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 /* We can arrive here with a NULL tstate during initialization: try
723 running "python -Wi" for an example related to string interning.
724 Let's just hope that no exception occurs then... This must be
725 _PyThreadState_Current and not PyThreadState_GET() because in debug
726 mode, the latter complains if tstate is NULL. */
727 tstate = (PyThreadState*)_Py_atomic_load_relaxed(
728 &_PyThreadState_Current);
729 if (tstate != NULL && tstate->curexc_type != NULL) {
730 /* preserve the existing exception */
731 PyObject *err_type, *err_value, *err_tb;
732 PyErr_Fetch(&err_type, &err_value, &err_tb);
733 ep = (mp->ma_lookup)(mp, key, hash);
734 /* ignore errors */
735 PyErr_Restore(err_type, err_value, err_tb);
736 if (ep == NULL)
737 return NULL;
738 }
739 else {
740 ep = (mp->ma_lookup)(mp, key, hash);
741 if (ep == NULL) {
742 PyErr_Clear();
743 return NULL;
744 }
745 }
746 return ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000747}
748
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000749/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
750 This returns NULL *with* an exception set if an exception occurred.
751 It returns NULL *without* an exception set if the key wasn't present.
752*/
753PyObject *
754PyDict_GetItemWithError(PyObject *op, PyObject *key)
755{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000756 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 PyDictObject*mp = (PyDictObject *)op;
758 PyDictEntry *ep;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 if (!PyDict_Check(op)) {
761 PyErr_BadInternalCall();
762 return NULL;
763 }
764 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200765 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 {
767 hash = PyObject_Hash(key);
768 if (hash == -1) {
769 return NULL;
770 }
771 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 ep = (mp->ma_lookup)(mp, key, hash);
774 if (ep == NULL)
775 return NULL;
776 return ep->me_value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000777}
778
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000779/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
Tim Peters60b29962006-01-01 01:19:23 +0000780 * dictionary if it's merely replacing the value for an existing key.
781 * This means that it's safe to loop over a dictionary with PyDict_Next()
782 * and occasionally replace a value -- but you can't insert new keys or
783 * remove them.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000784 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000785int
Tim Peters1f5871e2000-07-04 17:44:48 +0000786PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000787{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 register PyDictObject *mp;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000789 register Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 register Py_ssize_t n_used;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 if (!PyDict_Check(op)) {
793 PyErr_BadInternalCall();
794 return -1;
795 }
796 assert(key);
797 assert(value);
798 mp = (PyDictObject *)op;
799 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200800 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 {
802 hash = PyObject_Hash(key);
803 if (hash == -1)
804 return -1;
805 }
806 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
807 n_used = mp->ma_used;
808 Py_INCREF(value);
809 Py_INCREF(key);
810 if (insertdict(mp, key, hash, value) != 0)
811 return -1;
812 /* If we added a key, we can safely resize. Otherwise just return!
813 * If fill >= 2/3 size, adjust size. Normally, this doubles or
814 * quaduples the size, but it's also possible for the dict to shrink
815 * (if ma_fill is much larger than ma_used, meaning a lot of dict
816 * keys have been * deleted).
817 *
818 * Quadrupling the size improves average dictionary sparseness
819 * (reducing collisions) at the cost of some memory and iteration
820 * speed (which loops over every possible entry). It also halves
821 * the number of expensive resize operations in a growing dictionary.
822 *
823 * Very large dictionaries (over 50K items) use doubling instead.
824 * This may help applications with severe memory constraints.
825 */
826 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
827 return 0;
828 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000829}
830
831int
Tim Peters1f5871e2000-07-04 17:44:48 +0000832PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000833{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 register PyDictObject *mp;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000835 register Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 register PyDictEntry *ep;
837 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 if (!PyDict_Check(op)) {
840 PyErr_BadInternalCall();
841 return -1;
842 }
843 assert(key);
844 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200845 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 hash = PyObject_Hash(key);
847 if (hash == -1)
848 return -1;
849 }
850 mp = (PyDictObject *)op;
851 ep = (mp->ma_lookup)(mp, key, hash);
852 if (ep == NULL)
853 return -1;
854 if (ep->me_value == NULL) {
855 set_key_error(key);
856 return -1;
857 }
858 old_key = ep->me_key;
859 Py_INCREF(dummy);
860 ep->me_key = dummy;
861 old_value = ep->me_value;
862 ep->me_value = NULL;
863 mp->ma_used--;
864 Py_DECREF(old_value);
865 Py_DECREF(old_key);
866 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000867}
868
Guido van Rossum25831651993-05-19 14:50:45 +0000869void
Tim Peters1f5871e2000-07-04 17:44:48 +0000870PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000871{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 PyDictObject *mp;
873 PyDictEntry *ep, *table;
874 int table_is_malloced;
875 Py_ssize_t fill;
876 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000877#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +0000879#endif
880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 if (!PyDict_Check(op))
882 return;
883 mp = (PyDictObject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000884#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 n = mp->ma_mask + 1;
886 i = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000887#endif
888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 table = mp->ma_table;
890 assert(table != NULL);
891 table_is_malloced = table != mp->ma_smalltable;
Tim Petersdea48ec2001-05-22 20:40:22 +0000892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 /* This is delicate. During the process of clearing the dict,
894 * decrefs can cause the dict to mutate. To avoid fatal confusion
895 * (voice of experience), we have to make the dict empty before
896 * clearing the slots, and never refer to anything via mp->xxx while
897 * clearing.
898 */
899 fill = mp->ma_fill;
900 if (table_is_malloced)
901 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 else if (fill > 0) {
904 /* It's a small table with something that needs to be cleared.
905 * Afraid the only safe way is to copy the dict entries into
906 * another small table first.
907 */
908 memcpy(small_copy, table, sizeof(small_copy));
909 table = small_copy;
910 EMPTY_TO_MINSIZE(mp);
911 }
912 /* else it's a small table that's already empty */
Tim Petersdea48ec2001-05-22 20:40:22 +0000913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 /* Now we can finally clear things. If C had refcounts, we could
915 * assert that the refcount on table is 1 now, i.e. that this function
916 * has unique access to it, so decref side-effects can't alter it.
917 */
918 for (ep = table; fill > 0; ++ep) {
Tim Petersdea48ec2001-05-22 20:40:22 +0000919#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 assert(i < n);
921 ++i;
Tim Petersdea48ec2001-05-22 20:40:22 +0000922#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 if (ep->me_key) {
924 --fill;
925 Py_DECREF(ep->me_key);
926 Py_XDECREF(ep->me_value);
927 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000928#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 else
930 assert(ep->me_value == NULL);
Tim Petersdea48ec2001-05-22 20:40:22 +0000931#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 if (table_is_malloced)
935 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000936}
937
Tim Peters080c88b2003-02-15 03:01:11 +0000938/*
939 * Iterate over a dict. Use like so:
940 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000941 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +0000942 * PyObject *key, *value;
943 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +0000944 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +0000945 * Refer to borrowed references in key and value.
946 * }
947 *
948 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +0000949 * mutates the dict. One exception: it is safe if the loop merely changes
950 * the values associated with the keys (but doesn't insert new keys or
951 * delete keys), via PyDict_SetItem().
952 */
Guido van Rossum25831651993-05-19 14:50:45 +0000953int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000954PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000955{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 register Py_ssize_t i;
957 register Py_ssize_t mask;
958 register PyDictEntry *ep;
Raymond Hettinger43442782004-03-17 21:55:03 +0000959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 if (!PyDict_Check(op))
961 return 0;
962 i = *ppos;
963 if (i < 0)
964 return 0;
965 ep = ((PyDictObject *)op)->ma_table;
966 mask = ((PyDictObject *)op)->ma_mask;
967 while (i <= mask && ep[i].me_value == NULL)
968 i++;
969 *ppos = i+1;
970 if (i > mask)
971 return 0;
972 if (pkey)
973 *pkey = ep[i].me_key;
974 if (pvalue)
975 *pvalue = ep[i].me_value;
976 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000977}
978
Thomas Wouterscf297e42007-02-23 15:07:44 +0000979/* Internal version of PyDict_Next that returns a hash value in addition to the key and value.*/
980int
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000981_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, Py_hash_t *phash)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000982{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 register Py_ssize_t i;
984 register Py_ssize_t mask;
985 register PyDictEntry *ep;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 if (!PyDict_Check(op))
988 return 0;
989 i = *ppos;
990 if (i < 0)
991 return 0;
992 ep = ((PyDictObject *)op)->ma_table;
993 mask = ((PyDictObject *)op)->ma_mask;
994 while (i <= mask && ep[i].me_value == NULL)
995 i++;
996 *ppos = i+1;
997 if (i > mask)
998 return 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000999 *phash = ep[i].me_hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 if (pkey)
1001 *pkey = ep[i].me_key;
1002 if (pvalue)
1003 *pvalue = ep[i].me_value;
1004 return 1;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001005}
1006
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001007/* Methods */
1008
1009static void
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001010dict_dealloc(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001011{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 register PyDictEntry *ep;
1013 Py_ssize_t fill = mp->ma_fill;
1014 PyObject_GC_UnTrack(mp);
1015 Py_TRASHCAN_SAFE_BEGIN(mp)
1016 for (ep = mp->ma_table; fill > 0; ep++) {
1017 if (ep->me_key) {
1018 --fill;
1019 Py_DECREF(ep->me_key);
1020 Py_XDECREF(ep->me_value);
1021 }
1022 }
1023 if (mp->ma_table != mp->ma_smalltable)
1024 PyMem_DEL(mp->ma_table);
1025 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1026 free_list[numfree++] = mp;
1027 else
1028 Py_TYPE(mp)->tp_free((PyObject *)mp);
1029 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001030}
1031
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001032static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001033dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001034{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 Py_ssize_t i;
1036 PyObject *s, *temp, *colon = NULL;
1037 PyObject *pieces = NULL, *result = NULL;
1038 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +00001039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 i = Py_ReprEnter((PyObject *)mp);
1041 if (i != 0) {
1042 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1043 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 if (mp->ma_used == 0) {
1046 result = PyUnicode_FromString("{}");
1047 goto Done;
1048 }
Tim Petersa7259592001-06-16 05:11:17 +00001049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 pieces = PyList_New(0);
1051 if (pieces == NULL)
1052 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 colon = PyUnicode_FromString(": ");
1055 if (colon == NULL)
1056 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 /* Do repr() on each key+value pair, and insert ": " between them.
1059 Note that repr may mutate the dict. */
1060 i = 0;
1061 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
1062 int status;
1063 /* Prevent repr from deleting value during key format. */
1064 Py_INCREF(value);
1065 s = PyObject_Repr(key);
1066 PyUnicode_Append(&s, colon);
1067 PyUnicode_AppendAndDel(&s, PyObject_Repr(value));
1068 Py_DECREF(value);
1069 if (s == NULL)
1070 goto Done;
1071 status = PyList_Append(pieces, s);
1072 Py_DECREF(s); /* append created a new ref */
1073 if (status < 0)
1074 goto Done;
1075 }
Tim Petersa7259592001-06-16 05:11:17 +00001076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 /* Add "{}" decorations to the first and last items. */
1078 assert(PyList_GET_SIZE(pieces) > 0);
1079 s = PyUnicode_FromString("{");
1080 if (s == NULL)
1081 goto Done;
1082 temp = PyList_GET_ITEM(pieces, 0);
1083 PyUnicode_AppendAndDel(&s, temp);
1084 PyList_SET_ITEM(pieces, 0, s);
1085 if (s == NULL)
1086 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001087
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 s = PyUnicode_FromString("}");
1089 if (s == NULL)
1090 goto Done;
1091 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
1092 PyUnicode_AppendAndDel(&temp, s);
1093 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
1094 if (temp == NULL)
1095 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 /* Paste them all together with ", " between. */
1098 s = PyUnicode_FromString(", ");
1099 if (s == NULL)
1100 goto Done;
1101 result = PyUnicode_Join(s, pieces);
1102 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +00001103
1104Done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 Py_XDECREF(pieces);
1106 Py_XDECREF(colon);
1107 Py_ReprLeave((PyObject *)mp);
1108 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001109}
1110
Martin v. Löwis18e16552006-02-15 17:27:45 +00001111static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001112dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001115}
1116
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001117static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001118dict_subscript(PyDictObject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001119{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 PyObject *v;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001121 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 PyDictEntry *ep;
1123 assert(mp->ma_table != NULL);
1124 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001125 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 hash = PyObject_Hash(key);
1127 if (hash == -1)
1128 return NULL;
1129 }
1130 ep = (mp->ma_lookup)(mp, key, hash);
1131 if (ep == NULL)
1132 return NULL;
1133 v = ep->me_value;
1134 if (v == NULL) {
1135 if (!PyDict_CheckExact(mp)) {
1136 /* Look up __missing__ method if we're a subclass. */
1137 PyObject *missing, *res;
1138 static PyObject *missing_str = NULL;
1139 missing = _PyObject_LookupSpecial((PyObject *)mp,
1140 "__missing__",
1141 &missing_str);
1142 if (missing != NULL) {
1143 res = PyObject_CallFunctionObjArgs(missing,
1144 key, NULL);
1145 Py_DECREF(missing);
1146 return res;
1147 }
1148 else if (PyErr_Occurred())
1149 return NULL;
1150 }
1151 set_key_error(key);
1152 return NULL;
1153 }
1154 else
1155 Py_INCREF(v);
1156 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001157}
1158
1159static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001160dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001161{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 if (w == NULL)
1163 return PyDict_DelItem((PyObject *)mp, v);
1164 else
1165 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001166}
1167
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001168static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 (lenfunc)dict_length, /*mp_length*/
1170 (binaryfunc)dict_subscript, /*mp_subscript*/
1171 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001172};
1173
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001174static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001175dict_keys(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001176{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 register PyObject *v;
1178 register Py_ssize_t i, j;
1179 PyDictEntry *ep;
1180 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001181
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001182 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 n = mp->ma_used;
1184 v = PyList_New(n);
1185 if (v == NULL)
1186 return NULL;
1187 if (n != mp->ma_used) {
1188 /* Durnit. The allocations caused the dict to resize.
1189 * Just start over, this shouldn't normally happen.
1190 */
1191 Py_DECREF(v);
1192 goto again;
1193 }
1194 ep = mp->ma_table;
1195 mask = mp->ma_mask;
1196 for (i = 0, j = 0; i <= mask; i++) {
1197 if (ep[i].me_value != NULL) {
1198 PyObject *key = ep[i].me_key;
1199 Py_INCREF(key);
1200 PyList_SET_ITEM(v, j, key);
1201 j++;
1202 }
1203 }
1204 assert(j == n);
1205 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001206}
1207
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001208static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001209dict_values(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001210{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 register PyObject *v;
1212 register Py_ssize_t i, j;
1213 PyDictEntry *ep;
1214 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001215
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001216 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 n = mp->ma_used;
1218 v = PyList_New(n);
1219 if (v == NULL)
1220 return NULL;
1221 if (n != mp->ma_used) {
1222 /* Durnit. The allocations caused the dict to resize.
1223 * Just start over, this shouldn't normally happen.
1224 */
1225 Py_DECREF(v);
1226 goto again;
1227 }
1228 ep = mp->ma_table;
1229 mask = mp->ma_mask;
1230 for (i = 0, j = 0; i <= mask; i++) {
1231 if (ep[i].me_value != NULL) {
1232 PyObject *value = ep[i].me_value;
1233 Py_INCREF(value);
1234 PyList_SET_ITEM(v, j, value);
1235 j++;
1236 }
1237 }
1238 assert(j == n);
1239 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001240}
1241
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001242static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001243dict_items(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001244{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 register PyObject *v;
1246 register Py_ssize_t i, j, n;
1247 Py_ssize_t mask;
1248 PyObject *item, *key, *value;
1249 PyDictEntry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 /* Preallocate the list of tuples, to avoid allocations during
1252 * the loop over the items, which could trigger GC, which
1253 * could resize the dict. :-(
1254 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001255 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 n = mp->ma_used;
1257 v = PyList_New(n);
1258 if (v == NULL)
1259 return NULL;
1260 for (i = 0; i < n; i++) {
1261 item = PyTuple_New(2);
1262 if (item == NULL) {
1263 Py_DECREF(v);
1264 return NULL;
1265 }
1266 PyList_SET_ITEM(v, i, item);
1267 }
1268 if (n != mp->ma_used) {
1269 /* Durnit. The allocations caused the dict to resize.
1270 * Just start over, this shouldn't normally happen.
1271 */
1272 Py_DECREF(v);
1273 goto again;
1274 }
1275 /* Nothing we do below makes any function calls. */
1276 ep = mp->ma_table;
1277 mask = mp->ma_mask;
1278 for (i = 0, j = 0; i <= mask; i++) {
1279 if ((value=ep[i].me_value) != NULL) {
1280 key = ep[i].me_key;
1281 item = PyList_GET_ITEM(v, j);
1282 Py_INCREF(key);
1283 PyTuple_SET_ITEM(item, 0, key);
1284 Py_INCREF(value);
1285 PyTuple_SET_ITEM(item, 1, value);
1286 j++;
1287 }
1288 }
1289 assert(j == n);
1290 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001291}
1292
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001293static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001294dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001295{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 PyObject *seq;
1297 PyObject *value = Py_None;
1298 PyObject *it; /* iter(seq) */
1299 PyObject *key;
1300 PyObject *d;
1301 int status;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
1304 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 d = PyObject_CallObject(cls, NULL);
1307 if (d == NULL)
1308 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) {
1311 PyDictObject *mp = (PyDictObject *)d;
1312 PyObject *oldvalue;
1313 Py_ssize_t pos = 0;
1314 PyObject *key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001315 Py_hash_t hash;
Guido van Rossum58da9312007-11-10 23:39:45 +00001316
Petri Lehtinena94200e2011-10-24 21:12:58 +03001317 if (dictresize(mp, Py_SIZE(seq))) {
1318 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001320 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
1323 Py_INCREF(key);
1324 Py_INCREF(value);
Petri Lehtinena94200e2011-10-24 21:12:58 +03001325 if (insertdict(mp, key, hash, value)) {
1326 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001328 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 }
1330 return d;
1331 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
1334 PyDictObject *mp = (PyDictObject *)d;
1335 Py_ssize_t pos = 0;
1336 PyObject *key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001337 Py_hash_t hash;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001338
Petri Lehtinena94200e2011-10-24 21:12:58 +03001339 if (dictresize(mp, PySet_GET_SIZE(seq))) {
1340 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001342 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
1345 Py_INCREF(key);
1346 Py_INCREF(value);
Petri Lehtinena94200e2011-10-24 21:12:58 +03001347 if (insertdict(mp, key, hash, value)) {
1348 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001350 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 }
1352 return d;
1353 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 it = PyObject_GetIter(seq);
1356 if (it == NULL){
1357 Py_DECREF(d);
1358 return NULL;
1359 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 if (PyDict_CheckExact(d)) {
1362 while ((key = PyIter_Next(it)) != NULL) {
1363 status = PyDict_SetItem(d, key, value);
1364 Py_DECREF(key);
1365 if (status < 0)
1366 goto Fail;
1367 }
1368 } else {
1369 while ((key = PyIter_Next(it)) != NULL) {
1370 status = PyObject_SetItem(d, key, value);
1371 Py_DECREF(key);
1372 if (status < 0)
1373 goto Fail;
1374 }
1375 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 if (PyErr_Occurred())
1378 goto Fail;
1379 Py_DECREF(it);
1380 return d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001381
1382Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 Py_DECREF(it);
1384 Py_DECREF(d);
1385 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001386}
1387
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001388static int
1389dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001390{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 PyObject *arg = NULL;
1392 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1395 result = -1;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001398 _Py_IDENTIFIER(keys);
1399 if (_PyObject_HasAttrId(arg, &PyId_keys))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 result = PyDict_Merge(self, arg, 1);
1401 else
1402 result = PyDict_MergeFromSeq2(self, arg, 1);
1403 }
1404 if (result == 0 && kwds != NULL) {
1405 if (PyArg_ValidateKeywordArguments(kwds))
1406 result = PyDict_Merge(self, kwds, 1);
1407 else
1408 result = -1;
1409 }
1410 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001411}
1412
1413static PyObject *
1414dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1415{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 if (dict_update_common(self, args, kwds, "update") != -1)
1417 Py_RETURN_NONE;
1418 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001419}
1420
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001421/* Update unconditionally replaces existing items.
1422 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001423 otherwise it leaves existing items unchanged.
1424
1425 PyDict_{Update,Merge} update/merge from a mapping object.
1426
Tim Petersf582b822001-12-11 18:51:08 +00001427 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001428 producing iterable objects of length 2.
1429*/
1430
Tim Petersf582b822001-12-11 18:51:08 +00001431int
Tim Peters1fc240e2001-10-26 05:06:50 +00001432PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1433{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 PyObject *it; /* iter(seq2) */
1435 Py_ssize_t i; /* index into seq2 of current element */
1436 PyObject *item; /* seq2[i] */
1437 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00001438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 assert(d != NULL);
1440 assert(PyDict_Check(d));
1441 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00001442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 it = PyObject_GetIter(seq2);
1444 if (it == NULL)
1445 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 for (i = 0; ; ++i) {
1448 PyObject *key, *value;
1449 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 fast = NULL;
1452 item = PyIter_Next(it);
1453 if (item == NULL) {
1454 if (PyErr_Occurred())
1455 goto Fail;
1456 break;
1457 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001459 /* Convert item to sequence, and verify length 2. */
1460 fast = PySequence_Fast(item, "");
1461 if (fast == NULL) {
1462 if (PyErr_ExceptionMatches(PyExc_TypeError))
1463 PyErr_Format(PyExc_TypeError,
1464 "cannot convert dictionary update "
1465 "sequence element #%zd to a sequence",
1466 i);
1467 goto Fail;
1468 }
1469 n = PySequence_Fast_GET_SIZE(fast);
1470 if (n != 2) {
1471 PyErr_Format(PyExc_ValueError,
1472 "dictionary update sequence element #%zd "
1473 "has length %zd; 2 is required",
1474 i, n);
1475 goto Fail;
1476 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 /* Update/merge with this (key, value) pair. */
1479 key = PySequence_Fast_GET_ITEM(fast, 0);
1480 value = PySequence_Fast_GET_ITEM(fast, 1);
1481 if (override || PyDict_GetItem(d, key) == NULL) {
1482 int status = PyDict_SetItem(d, key, value);
1483 if (status < 0)
1484 goto Fail;
1485 }
1486 Py_DECREF(fast);
1487 Py_DECREF(item);
1488 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 i = 0;
1491 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00001492Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001493 Py_XDECREF(item);
1494 Py_XDECREF(fast);
1495 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001496Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 Py_DECREF(it);
1498 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001499}
1500
Tim Peters6d6c1a32001-08-02 04:15:00 +00001501int
1502PyDict_Update(PyObject *a, PyObject *b)
1503{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 return PyDict_Merge(a, b, 1);
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001505}
1506
1507int
1508PyDict_Merge(PyObject *a, PyObject *b, int override)
1509{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 register PyDictObject *mp, *other;
1511 register Py_ssize_t i;
1512 PyDictEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 /* We accept for the argument either a concrete dictionary object,
1515 * or an abstract "mapping" object. For the former, we can do
1516 * things quite efficiently. For the latter, we only require that
1517 * PyMapping_Keys() and PyObject_GetItem() be supported.
1518 */
1519 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1520 PyErr_BadInternalCall();
1521 return -1;
1522 }
1523 mp = (PyDictObject*)a;
1524 if (PyDict_Check(b)) {
1525 other = (PyDictObject*)b;
1526 if (other == mp || other->ma_used == 0)
1527 /* a.update(a) or a.update({}); nothing to do */
1528 return 0;
1529 if (mp->ma_used == 0)
1530 /* Since the target dict is empty, PyDict_GetItem()
1531 * always returns NULL. Setting override to 1
1532 * skips the unnecessary test.
1533 */
1534 override = 1;
1535 /* Do one big resize at the start, rather than
1536 * incrementally resizing as we insert new items. Expect
1537 * that there will be no (or few) overlapping keys.
1538 */
1539 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
1540 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
1541 return -1;
1542 }
1543 for (i = 0; i <= other->ma_mask; i++) {
1544 entry = &other->ma_table[i];
1545 if (entry->me_value != NULL &&
1546 (override ||
1547 PyDict_GetItem(a, entry->me_key) == NULL)) {
1548 Py_INCREF(entry->me_key);
1549 Py_INCREF(entry->me_value);
1550 if (insertdict(mp, entry->me_key,
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001551 entry->me_hash,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 entry->me_value) != 0)
1553 return -1;
1554 }
1555 }
1556 }
1557 else {
1558 /* Do it the generic, slower way */
1559 PyObject *keys = PyMapping_Keys(b);
1560 PyObject *iter;
1561 PyObject *key, *value;
1562 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 if (keys == NULL)
1565 /* Docstring says this is equivalent to E.keys() so
1566 * if E doesn't have a .keys() method we want
1567 * AttributeError to percolate up. Might as well
1568 * do the same for any other error.
1569 */
1570 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 iter = PyObject_GetIter(keys);
1573 Py_DECREF(keys);
1574 if (iter == NULL)
1575 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001577 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
1578 if (!override && PyDict_GetItem(a, key) != NULL) {
1579 Py_DECREF(key);
1580 continue;
1581 }
1582 value = PyObject_GetItem(b, key);
1583 if (value == NULL) {
1584 Py_DECREF(iter);
1585 Py_DECREF(key);
1586 return -1;
1587 }
1588 status = PyDict_SetItem(a, key, value);
1589 Py_DECREF(key);
1590 Py_DECREF(value);
1591 if (status < 0) {
1592 Py_DECREF(iter);
1593 return -1;
1594 }
1595 }
1596 Py_DECREF(iter);
1597 if (PyErr_Occurred())
1598 /* Iterator completed, via error */
1599 return -1;
1600 }
1601 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001602}
1603
1604static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001605dict_copy(register PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001606{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001608}
1609
1610PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001611PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001612{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 if (o == NULL || !PyDict_Check(o)) {
1616 PyErr_BadInternalCall();
1617 return NULL;
1618 }
1619 copy = PyDict_New();
1620 if (copy == NULL)
1621 return NULL;
1622 if (PyDict_Merge(copy, o, 1) == 0)
1623 return copy;
1624 Py_DECREF(copy);
1625 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001626}
1627
Martin v. Löwis18e16552006-02-15 17:27:45 +00001628Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001629PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 if (mp == NULL || !PyDict_Check(mp)) {
1632 PyErr_BadInternalCall();
1633 return -1;
1634 }
1635 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001636}
1637
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001638PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001639PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001640{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 if (mp == NULL || !PyDict_Check(mp)) {
1642 PyErr_BadInternalCall();
1643 return NULL;
1644 }
1645 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001646}
1647
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001648PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001649PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001650{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 if (mp == NULL || !PyDict_Check(mp)) {
1652 PyErr_BadInternalCall();
1653 return NULL;
1654 }
1655 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001656}
1657
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001658PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001659PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001660{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661 if (mp == NULL || !PyDict_Check(mp)) {
1662 PyErr_BadInternalCall();
1663 return NULL;
1664 }
1665 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001666}
1667
Tim Peterse63415e2001-05-08 04:38:29 +00001668/* Return 1 if dicts equal, 0 if not, -1 if error.
1669 * Gets out as soon as any difference is detected.
1670 * Uses only Py_EQ comparison.
1671 */
1672static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001673dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00001674{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00001676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 if (a->ma_used != b->ma_used)
1678 /* can't be equal if # of entries differ */
1679 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001681 /* Same # of entries -- check all of 'em. Exit early on any diff. */
1682 for (i = 0; i <= a->ma_mask; i++) {
1683 PyObject *aval = a->ma_table[i].me_value;
1684 if (aval != NULL) {
1685 int cmp;
1686 PyObject *bval;
1687 PyObject *key = a->ma_table[i].me_key;
1688 /* temporarily bump aval's refcount to ensure it stays
1689 alive until we're done with it */
1690 Py_INCREF(aval);
1691 /* ditto for key */
1692 Py_INCREF(key);
1693 bval = PyDict_GetItemWithError((PyObject *)b, key);
1694 Py_DECREF(key);
1695 if (bval == NULL) {
1696 Py_DECREF(aval);
1697 if (PyErr_Occurred())
1698 return -1;
1699 return 0;
1700 }
1701 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1702 Py_DECREF(aval);
1703 if (cmp <= 0) /* error or not equal */
1704 return cmp;
1705 }
1706 }
1707 return 1;
Tim Peterse63415e2001-05-08 04:38:29 +00001708 }
1709
1710static PyObject *
1711dict_richcompare(PyObject *v, PyObject *w, int op)
1712{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001713 int cmp;
1714 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00001715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1717 res = Py_NotImplemented;
1718 }
1719 else if (op == Py_EQ || op == Py_NE) {
1720 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
1721 if (cmp < 0)
1722 return NULL;
1723 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1724 }
1725 else
1726 res = Py_NotImplemented;
1727 Py_INCREF(res);
1728 return res;
Tim Peterse63415e2001-05-08 04:38:29 +00001729 }
1730
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001731static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001732dict_contains(register PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001733{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001734 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001738 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739 hash = PyObject_Hash(key);
1740 if (hash == -1)
1741 return NULL;
1742 }
1743 ep = (mp->ma_lookup)(mp, key, hash);
1744 if (ep == NULL)
1745 return NULL;
1746 return PyBool_FromLong(ep->me_value != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001747}
1748
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001749static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001750dict_get(register PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001751{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 PyObject *key;
1753 PyObject *failobj = Py_None;
1754 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001755 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001756 PyDictEntry *ep;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001757
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
1759 return NULL;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001761 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001762 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763 hash = PyObject_Hash(key);
1764 if (hash == -1)
1765 return NULL;
1766 }
1767 ep = (mp->ma_lookup)(mp, key, hash);
1768 if (ep == NULL)
1769 return NULL;
1770 val = ep->me_value;
1771 if (val == NULL)
1772 val = failobj;
1773 Py_INCREF(val);
1774 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001775}
1776
1777
1778static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001779dict_setdefault(register PyDictObject *mp, PyObject *args)
Guido van Rossum164452c2000-08-08 16:12:54 +00001780{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 PyObject *key;
1782 PyObject *failobj = Py_None;
1783 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001784 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 PyDictEntry *ep;
Guido van Rossum164452c2000-08-08 16:12:54 +00001786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
1788 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001791 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001792 hash = PyObject_Hash(key);
1793 if (hash == -1)
1794 return NULL;
1795 }
1796 ep = (mp->ma_lookup)(mp, key, hash);
1797 if (ep == NULL)
1798 return NULL;
1799 val = ep->me_value;
1800 if (val == NULL) {
1801 val = failobj;
1802 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1803 val = NULL;
1804 }
1805 Py_XINCREF(val);
1806 return val;
Guido van Rossum164452c2000-08-08 16:12:54 +00001807}
1808
1809
1810static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001811dict_clear(register PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001812{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813 PyDict_Clear((PyObject *)mp);
1814 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001815}
1816
Guido van Rossumba6ab842000-12-12 22:02:18 +00001817static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001818dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00001819{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001820 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001821 PyDictEntry *ep;
1822 PyObject *old_value, *old_key;
1823 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1826 return NULL;
1827 if (mp->ma_used == 0) {
1828 if (deflt) {
1829 Py_INCREF(deflt);
1830 return deflt;
1831 }
Raymond Hettingerdd421542010-10-30 08:10:29 +00001832 set_key_error(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001833 return NULL;
1834 }
1835 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001836 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 hash = PyObject_Hash(key);
1838 if (hash == -1)
1839 return NULL;
1840 }
1841 ep = (mp->ma_lookup)(mp, key, hash);
1842 if (ep == NULL)
1843 return NULL;
1844 if (ep->me_value == NULL) {
1845 if (deflt) {
1846 Py_INCREF(deflt);
1847 return deflt;
1848 }
1849 set_key_error(key);
1850 return NULL;
1851 }
1852 old_key = ep->me_key;
1853 Py_INCREF(dummy);
1854 ep->me_key = dummy;
1855 old_value = ep->me_value;
1856 ep->me_value = NULL;
1857 mp->ma_used--;
1858 Py_DECREF(old_key);
1859 return old_value;
Guido van Rossume027d982002-04-12 15:11:59 +00001860}
1861
1862static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001863dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001864{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001865 Py_hash_t i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 PyDictEntry *ep;
1867 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001868
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869 /* Allocate the result tuple before checking the size. Believe it
1870 * or not, this allocation could trigger a garbage collection which
1871 * could empty the dict, so if we checked the size first and that
1872 * happened, the result would be an infinite loop (searching for an
1873 * entry that no longer exists). Note that the usual popitem()
1874 * idiom is "while d: k, v = d.popitem()". so needing to throw the
1875 * tuple away if the dict *is* empty isn't a significant
1876 * inefficiency -- possible, but unlikely in practice.
1877 */
1878 res = PyTuple_New(2);
1879 if (res == NULL)
1880 return NULL;
1881 if (mp->ma_used == 0) {
1882 Py_DECREF(res);
1883 PyErr_SetString(PyExc_KeyError,
1884 "popitem(): dictionary is empty");
1885 return NULL;
1886 }
1887 /* Set ep to "the first" dict entry with a value. We abuse the hash
1888 * field of slot 0 to hold a search finger:
1889 * If slot 0 has a value, use slot 0.
1890 * Else slot 0 is being used to hold a search finger,
1891 * and we use its hash value as the first index to look.
1892 */
1893 ep = &mp->ma_table[0];
1894 if (ep->me_value == NULL) {
1895 i = ep->me_hash;
1896 /* The hash field may be a real hash value, or it may be a
1897 * legit search finger, or it may be a once-legit search
1898 * finger that's out of bounds now because it wrapped around
1899 * or the table shrunk -- simply make sure it's in bounds now.
1900 */
1901 if (i > mp->ma_mask || i < 1)
1902 i = 1; /* skip slot 0 */
1903 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1904 i++;
1905 if (i > mp->ma_mask)
1906 i = 1;
1907 }
1908 }
1909 PyTuple_SET_ITEM(res, 0, ep->me_key);
1910 PyTuple_SET_ITEM(res, 1, ep->me_value);
1911 Py_INCREF(dummy);
1912 ep->me_key = dummy;
1913 ep->me_value = NULL;
1914 mp->ma_used--;
1915 assert(mp->ma_table[0].me_value == NULL);
1916 mp->ma_table[0].me_hash = i + 1; /* next place to start */
1917 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001918}
1919
Jeremy Hylton8caad492000-06-23 14:18:11 +00001920static int
1921dict_traverse(PyObject *op, visitproc visit, void *arg)
1922{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001923 Py_ssize_t i = 0;
1924 PyObject *pk;
1925 PyObject *pv;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001927 while (PyDict_Next(op, &i, &pk, &pv)) {
1928 Py_VISIT(pk);
1929 Py_VISIT(pv);
1930 }
1931 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001932}
1933
1934static int
1935dict_tp_clear(PyObject *op)
1936{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 PyDict_Clear(op);
1938 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001939}
1940
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001941static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001942
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001943static PyObject *
1944dict_sizeof(PyDictObject *mp)
1945{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001946 Py_ssize_t res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 res = sizeof(PyDictObject);
1949 if (mp->ma_table != mp->ma_smalltable)
1950 res = res + (mp->ma_mask + 1) * sizeof(PyDictEntry);
1951 return PyLong_FromSsize_t(res);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001952}
1953
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001954PyDoc_STRVAR(contains__doc__,
1955"D.__contains__(k) -> True if D has a key k, else False");
1956
1957PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
1958
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001959PyDoc_STRVAR(sizeof__doc__,
1960"D.__sizeof__() -> size of D in memory, in bytes");
1961
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001962PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001963"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001964
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001965PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001966"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 +00001967
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001968PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001969"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001970If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00001971
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001972PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00001973"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000019742-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001975
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001976PyDoc_STRVAR(update__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001977"D.update(E, **F) -> None. Update D from dict/iterable E and F.\n"
1978"If E has a .keys() method, does: for k in E: D[k] = E[k]\n\
1979If E lacks .keys() method, does: for (k, v) in E: D[k] = v\n\
1980In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00001981
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001982PyDoc_STRVAR(fromkeys__doc__,
1983"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
1984v defaults to None.");
1985
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001986PyDoc_STRVAR(clear__doc__,
1987"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001988
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001989PyDoc_STRVAR(copy__doc__,
1990"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00001991
Guido van Rossumb90c8482007-02-10 01:11:45 +00001992/* Forward */
1993static PyObject *dictkeys_new(PyObject *);
1994static PyObject *dictitems_new(PyObject *);
1995static PyObject *dictvalues_new(PyObject *);
1996
Guido van Rossum45c85d12007-07-27 16:31:40 +00001997PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001998 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00001999PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002001PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002002 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00002003
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002004static PyMethodDef mapp_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002005 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
2006 contains__doc__},
2007 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
2008 getitem__doc__},
2009 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
2010 sizeof__doc__},
2011 {"get", (PyCFunction)dict_get, METH_VARARGS,
2012 get__doc__},
2013 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
2014 setdefault_doc__},
2015 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
2016 pop__doc__},
2017 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
2018 popitem__doc__},
2019 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
2020 keys__doc__},
2021 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
2022 items__doc__},
2023 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
2024 values__doc__},
2025 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
2026 update__doc__},
2027 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
2028 fromkeys__doc__},
2029 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
2030 clear__doc__},
2031 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
2032 copy__doc__},
2033 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002034};
2035
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002036/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002037int
2038PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002039{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002040 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002041 PyDictObject *mp = (PyDictObject *)op;
2042 PyDictEntry *ep;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002044 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002045 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002046 hash = PyObject_Hash(key);
2047 if (hash == -1)
2048 return -1;
2049 }
2050 ep = (mp->ma_lookup)(mp, key, hash);
2051 return ep == NULL ? -1 : (ep->me_value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002052}
2053
Thomas Wouterscf297e42007-02-23 15:07:44 +00002054/* Internal version of PyDict_Contains used when the hash value is already known */
2055int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002056_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00002057{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002058 PyDictObject *mp = (PyDictObject *)op;
2059 PyDictEntry *ep;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 ep = (mp->ma_lookup)(mp, key, hash);
2062 return ep == NULL ? -1 : (ep->me_value != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00002063}
2064
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002065/* Hack to implement "key in dict" */
2066static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 0, /* sq_length */
2068 0, /* sq_concat */
2069 0, /* sq_repeat */
2070 0, /* sq_item */
2071 0, /* sq_slice */
2072 0, /* sq_ass_item */
2073 0, /* sq_ass_slice */
2074 PyDict_Contains, /* sq_contains */
2075 0, /* sq_inplace_concat */
2076 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002077};
2078
Guido van Rossum09e563a2001-05-01 12:10:21 +00002079static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002080dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2081{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002082 PyObject *self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002083
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002084 assert(type != NULL && type->tp_alloc != NULL);
2085 self = type->tp_alloc(type, 0);
2086 if (self != NULL) {
2087 PyDictObject *d = (PyDictObject *)self;
2088 /* It's guaranteed that tp->alloc zeroed out the struct. */
2089 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
2090 INIT_NONZERO_DICT_SLOTS(d);
2091 d->ma_lookup = lookdict_unicode;
Ezio Melotti13925002011-03-16 11:05:33 +02002092 /* The object has been implicitly tracked by tp_alloc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002093 if (type == &PyDict_Type)
2094 _PyObject_GC_UNTRACK(d);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002095#ifdef SHOW_CONVERSION_COUNTS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096 ++created;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002097#endif
Antoine Pitrou3a652b12009-03-23 18:52:06 +00002098#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002099 if (_PyObject_GC_IS_TRACKED(d))
2100 count_tracked++;
2101 else
2102 count_untracked++;
Antoine Pitrou3a652b12009-03-23 18:52:06 +00002103#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002104 }
2105 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002106}
2107
Tim Peters25786c02001-09-02 08:22:48 +00002108static int
2109dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2110{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002111 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002112}
2113
Tim Peters6d6c1a32001-08-02 04:15:00 +00002114static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002115dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002116{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002117 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002118}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002119
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002120PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002121"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002122"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002123" (key, value) pairs\n"
2124"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002125" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002126" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002127" d[k] = v\n"
2128"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2129" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002130
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002131PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002132 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2133 "dict",
2134 sizeof(PyDictObject),
2135 0,
2136 (destructor)dict_dealloc, /* tp_dealloc */
2137 0, /* tp_print */
2138 0, /* tp_getattr */
2139 0, /* tp_setattr */
2140 0, /* tp_reserved */
2141 (reprfunc)dict_repr, /* tp_repr */
2142 0, /* tp_as_number */
2143 &dict_as_sequence, /* tp_as_sequence */
2144 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002145 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002146 0, /* tp_call */
2147 0, /* tp_str */
2148 PyObject_GenericGetAttr, /* tp_getattro */
2149 0, /* tp_setattro */
2150 0, /* tp_as_buffer */
2151 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2152 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
2153 dictionary_doc, /* tp_doc */
2154 dict_traverse, /* tp_traverse */
2155 dict_tp_clear, /* tp_clear */
2156 dict_richcompare, /* tp_richcompare */
2157 0, /* tp_weaklistoffset */
2158 (getiterfunc)dict_iter, /* tp_iter */
2159 0, /* tp_iternext */
2160 mapp_methods, /* tp_methods */
2161 0, /* tp_members */
2162 0, /* tp_getset */
2163 0, /* tp_base */
2164 0, /* tp_dict */
2165 0, /* tp_descr_get */
2166 0, /* tp_descr_set */
2167 0, /* tp_dictoffset */
2168 dict_init, /* tp_init */
2169 PyType_GenericAlloc, /* tp_alloc */
2170 dict_new, /* tp_new */
2171 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002172};
2173
Guido van Rossum3cca2451997-05-16 14:23:33 +00002174/* For backward compatibility with old dictionary interface */
2175
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002176PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002177PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002178{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002179 PyObject *kv, *rv;
2180 kv = PyUnicode_FromString(key);
2181 if (kv == NULL)
2182 return NULL;
2183 rv = PyDict_GetItem(v, kv);
2184 Py_DECREF(kv);
2185 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002186}
2187
2188int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002189PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002190{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 PyObject *kv;
2192 int err;
2193 kv = PyUnicode_FromString(key);
2194 if (kv == NULL)
2195 return -1;
2196 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
2197 err = PyDict_SetItem(v, kv, item);
2198 Py_DECREF(kv);
2199 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002200}
2201
2202int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002203PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002204{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002205 PyObject *kv;
2206 int err;
2207 kv = PyUnicode_FromString(key);
2208 if (kv == NULL)
2209 return -1;
2210 err = PyDict_DelItem(v, kv);
2211 Py_DECREF(kv);
2212 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002213}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002214
Raymond Hettinger019a1482004-03-18 02:41:19 +00002215/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002216
2217typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002218 PyObject_HEAD
2219 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
2220 Py_ssize_t di_used;
2221 Py_ssize_t di_pos;
2222 PyObject* di_result; /* reusable result tuple for iteritems */
2223 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002224} dictiterobject;
2225
2226static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002227dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002228{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002229 dictiterobject *di;
2230 di = PyObject_GC_New(dictiterobject, itertype);
2231 if (di == NULL)
2232 return NULL;
2233 Py_INCREF(dict);
2234 di->di_dict = dict;
2235 di->di_used = dict->ma_used;
2236 di->di_pos = 0;
2237 di->len = dict->ma_used;
2238 if (itertype == &PyDictIterItem_Type) {
2239 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2240 if (di->di_result == NULL) {
2241 Py_DECREF(di);
2242 return NULL;
2243 }
2244 }
2245 else
2246 di->di_result = NULL;
2247 _PyObject_GC_TRACK(di);
2248 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002249}
2250
2251static void
2252dictiter_dealloc(dictiterobject *di)
2253{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002254 Py_XDECREF(di->di_dict);
2255 Py_XDECREF(di->di_result);
2256 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002257}
2258
2259static int
2260dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
2261{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262 Py_VISIT(di->di_dict);
2263 Py_VISIT(di->di_result);
2264 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002265}
2266
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002267static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002268dictiter_len(dictiterobject *di)
2269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 Py_ssize_t len = 0;
2271 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
2272 len = di->len;
2273 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002274}
2275
Guido van Rossumb90c8482007-02-10 01:11:45 +00002276PyDoc_STRVAR(length_hint_doc,
2277 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002278
2279static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002280 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
2281 length_hint_doc},
2282 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002283};
2284
Raymond Hettinger019a1482004-03-18 02:41:19 +00002285static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287 PyObject *key;
2288 register Py_ssize_t i, mask;
2289 register PyDictEntry *ep;
2290 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292 if (d == NULL)
2293 return NULL;
2294 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002296 if (di->di_used != d->ma_used) {
2297 PyErr_SetString(PyExc_RuntimeError,
2298 "dictionary changed size during iteration");
2299 di->di_used = -1; /* Make this state sticky */
2300 return NULL;
2301 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002303 i = di->di_pos;
2304 if (i < 0)
2305 goto fail;
2306 ep = d->ma_table;
2307 mask = d->ma_mask;
2308 while (i <= mask && ep[i].me_value == NULL)
2309 i++;
2310 di->di_pos = i+1;
2311 if (i > mask)
2312 goto fail;
2313 di->len--;
2314 key = ep[i].me_key;
2315 Py_INCREF(key);
2316 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002317
2318fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002319 Py_DECREF(d);
2320 di->di_dict = NULL;
2321 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002322}
2323
Raymond Hettinger019a1482004-03-18 02:41:19 +00002324PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002325 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2326 "dict_keyiterator", /* tp_name */
2327 sizeof(dictiterobject), /* tp_basicsize */
2328 0, /* tp_itemsize */
2329 /* methods */
2330 (destructor)dictiter_dealloc, /* tp_dealloc */
2331 0, /* tp_print */
2332 0, /* tp_getattr */
2333 0, /* tp_setattr */
2334 0, /* tp_reserved */
2335 0, /* tp_repr */
2336 0, /* tp_as_number */
2337 0, /* tp_as_sequence */
2338 0, /* tp_as_mapping */
2339 0, /* tp_hash */
2340 0, /* tp_call */
2341 0, /* tp_str */
2342 PyObject_GenericGetAttr, /* tp_getattro */
2343 0, /* tp_setattro */
2344 0, /* tp_as_buffer */
2345 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2346 0, /* tp_doc */
2347 (traverseproc)dictiter_traverse, /* tp_traverse */
2348 0, /* tp_clear */
2349 0, /* tp_richcompare */
2350 0, /* tp_weaklistoffset */
2351 PyObject_SelfIter, /* tp_iter */
2352 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
2353 dictiter_methods, /* tp_methods */
2354 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002355};
2356
2357static PyObject *dictiter_iternextvalue(dictiterobject *di)
2358{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359 PyObject *value;
2360 register Py_ssize_t i, mask;
2361 register PyDictEntry *ep;
2362 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 if (d == NULL)
2365 return NULL;
2366 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368 if (di->di_used != d->ma_used) {
2369 PyErr_SetString(PyExc_RuntimeError,
2370 "dictionary changed size during iteration");
2371 di->di_used = -1; /* Make this state sticky */
2372 return NULL;
2373 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002375 i = di->di_pos;
2376 mask = d->ma_mask;
2377 if (i < 0 || i > mask)
2378 goto fail;
2379 ep = d->ma_table;
2380 while ((value=ep[i].me_value) == NULL) {
2381 i++;
2382 if (i > mask)
2383 goto fail;
2384 }
2385 di->di_pos = i+1;
2386 di->len--;
2387 Py_INCREF(value);
2388 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002389
2390fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 Py_DECREF(d);
2392 di->di_dict = NULL;
2393 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002394}
2395
2396PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002397 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2398 "dict_valueiterator", /* tp_name */
2399 sizeof(dictiterobject), /* tp_basicsize */
2400 0, /* tp_itemsize */
2401 /* methods */
2402 (destructor)dictiter_dealloc, /* tp_dealloc */
2403 0, /* tp_print */
2404 0, /* tp_getattr */
2405 0, /* tp_setattr */
2406 0, /* tp_reserved */
2407 0, /* tp_repr */
2408 0, /* tp_as_number */
2409 0, /* tp_as_sequence */
2410 0, /* tp_as_mapping */
2411 0, /* tp_hash */
2412 0, /* tp_call */
2413 0, /* tp_str */
2414 PyObject_GenericGetAttr, /* tp_getattro */
2415 0, /* tp_setattro */
2416 0, /* tp_as_buffer */
2417 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2418 0, /* tp_doc */
2419 (traverseproc)dictiter_traverse, /* tp_traverse */
2420 0, /* tp_clear */
2421 0, /* tp_richcompare */
2422 0, /* tp_weaklistoffset */
2423 PyObject_SelfIter, /* tp_iter */
2424 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
2425 dictiter_methods, /* tp_methods */
2426 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002427};
2428
2429static PyObject *dictiter_iternextitem(dictiterobject *di)
2430{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002431 PyObject *key, *value, *result = di->di_result;
2432 register Py_ssize_t i, mask;
2433 register PyDictEntry *ep;
2434 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436 if (d == NULL)
2437 return NULL;
2438 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002440 if (di->di_used != d->ma_used) {
2441 PyErr_SetString(PyExc_RuntimeError,
2442 "dictionary changed size during iteration");
2443 di->di_used = -1; /* Make this state sticky */
2444 return NULL;
2445 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002447 i = di->di_pos;
2448 if (i < 0)
2449 goto fail;
2450 ep = d->ma_table;
2451 mask = d->ma_mask;
2452 while (i <= mask && ep[i].me_value == NULL)
2453 i++;
2454 di->di_pos = i+1;
2455 if (i > mask)
2456 goto fail;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002458 if (result->ob_refcnt == 1) {
2459 Py_INCREF(result);
2460 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2461 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2462 } else {
2463 result = PyTuple_New(2);
2464 if (result == NULL)
2465 return NULL;
2466 }
2467 di->len--;
2468 key = ep[i].me_key;
2469 value = ep[i].me_value;
2470 Py_INCREF(key);
2471 Py_INCREF(value);
2472 PyTuple_SET_ITEM(result, 0, key);
2473 PyTuple_SET_ITEM(result, 1, value);
2474 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002475
2476fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002477 Py_DECREF(d);
2478 di->di_dict = NULL;
2479 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002480}
2481
2482PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002483 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2484 "dict_itemiterator", /* tp_name */
2485 sizeof(dictiterobject), /* tp_basicsize */
2486 0, /* tp_itemsize */
2487 /* methods */
2488 (destructor)dictiter_dealloc, /* tp_dealloc */
2489 0, /* tp_print */
2490 0, /* tp_getattr */
2491 0, /* tp_setattr */
2492 0, /* tp_reserved */
2493 0, /* tp_repr */
2494 0, /* tp_as_number */
2495 0, /* tp_as_sequence */
2496 0, /* tp_as_mapping */
2497 0, /* tp_hash */
2498 0, /* tp_call */
2499 0, /* tp_str */
2500 PyObject_GenericGetAttr, /* tp_getattro */
2501 0, /* tp_setattro */
2502 0, /* tp_as_buffer */
2503 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2504 0, /* tp_doc */
2505 (traverseproc)dictiter_traverse, /* tp_traverse */
2506 0, /* tp_clear */
2507 0, /* tp_richcompare */
2508 0, /* tp_weaklistoffset */
2509 PyObject_SelfIter, /* tp_iter */
2510 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
2511 dictiter_methods, /* tp_methods */
2512 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002513};
Guido van Rossumb90c8482007-02-10 01:11:45 +00002514
2515
Guido van Rossum3ac67412007-02-10 18:55:06 +00002516/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002517/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002518/***********************************************/
2519
Guido van Rossumb90c8482007-02-10 01:11:45 +00002520/* The instance lay-out is the same for all three; but the type differs. */
2521
2522typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002523 PyObject_HEAD
2524 PyDictObject *dv_dict;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002525} dictviewobject;
2526
2527
2528static void
Guido van Rossum3ac67412007-02-10 18:55:06 +00002529dictview_dealloc(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002530{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002531 Py_XDECREF(dv->dv_dict);
2532 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002533}
2534
2535static int
2536dictview_traverse(dictviewobject *dv, visitproc visit, void *arg)
2537{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002538 Py_VISIT(dv->dv_dict);
2539 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002540}
2541
Guido van Rossum83825ac2007-02-10 04:54:19 +00002542static Py_ssize_t
Guido van Rossum3ac67412007-02-10 18:55:06 +00002543dictview_len(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002544{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002545 Py_ssize_t len = 0;
2546 if (dv->dv_dict != NULL)
2547 len = dv->dv_dict->ma_used;
2548 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002549}
2550
2551static PyObject *
2552dictview_new(PyObject *dict, PyTypeObject *type)
2553{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002554 dictviewobject *dv;
2555 if (dict == NULL) {
2556 PyErr_BadInternalCall();
2557 return NULL;
2558 }
2559 if (!PyDict_Check(dict)) {
2560 /* XXX Get rid of this restriction later */
2561 PyErr_Format(PyExc_TypeError,
2562 "%s() requires a dict argument, not '%s'",
2563 type->tp_name, dict->ob_type->tp_name);
2564 return NULL;
2565 }
2566 dv = PyObject_GC_New(dictviewobject, type);
2567 if (dv == NULL)
2568 return NULL;
2569 Py_INCREF(dict);
2570 dv->dv_dict = (PyDictObject *)dict;
2571 _PyObject_GC_TRACK(dv);
2572 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002573}
2574
Neal Norwitze36f2ba2007-02-26 23:12:28 +00002575/* TODO(guido): The views objects are not complete:
2576
2577 * support more set operations
2578 * support arbitrary mappings?
2579 - either these should be static or exported in dictobject.h
2580 - if public then they should probably be in builtins
2581*/
2582
Guido van Rossumaac530c2007-08-24 22:33:45 +00002583/* Return 1 if self is a subset of other, iterating over self;
2584 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002585static int
2586all_contained_in(PyObject *self, PyObject *other)
2587{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002588 PyObject *iter = PyObject_GetIter(self);
2589 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00002590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002591 if (iter == NULL)
2592 return -1;
2593 for (;;) {
2594 PyObject *next = PyIter_Next(iter);
2595 if (next == NULL) {
2596 if (PyErr_Occurred())
2597 ok = -1;
2598 break;
2599 }
2600 ok = PySequence_Contains(other, next);
2601 Py_DECREF(next);
2602 if (ok <= 0)
2603 break;
2604 }
2605 Py_DECREF(iter);
2606 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00002607}
2608
2609static PyObject *
2610dictview_richcompare(PyObject *self, PyObject *other, int op)
2611{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002612 Py_ssize_t len_self, len_other;
2613 int ok;
2614 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002616 assert(self != NULL);
2617 assert(PyDictViewSet_Check(self));
2618 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00002619
Brian Curtindfc80e32011-08-10 20:28:54 -05002620 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
2621 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002623 len_self = PyObject_Size(self);
2624 if (len_self < 0)
2625 return NULL;
2626 len_other = PyObject_Size(other);
2627 if (len_other < 0)
2628 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002630 ok = 0;
2631 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00002632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002633 case Py_NE:
2634 case Py_EQ:
2635 if (len_self == len_other)
2636 ok = all_contained_in(self, other);
2637 if (op == Py_NE && ok >= 0)
2638 ok = !ok;
2639 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002641 case Py_LT:
2642 if (len_self < len_other)
2643 ok = all_contained_in(self, other);
2644 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002646 case Py_LE:
2647 if (len_self <= len_other)
2648 ok = all_contained_in(self, other);
2649 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002651 case Py_GT:
2652 if (len_self > len_other)
2653 ok = all_contained_in(other, self);
2654 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002656 case Py_GE:
2657 if (len_self >= len_other)
2658 ok = all_contained_in(other, self);
2659 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002661 }
2662 if (ok < 0)
2663 return NULL;
2664 result = ok ? Py_True : Py_False;
2665 Py_INCREF(result);
2666 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00002667}
2668
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00002669static PyObject *
2670dictview_repr(dictviewobject *dv)
2671{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002672 PyObject *seq;
2673 PyObject *result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00002674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002675 seq = PySequence_List((PyObject *)dv);
2676 if (seq == NULL)
2677 return NULL;
2678
2679 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
2680 Py_DECREF(seq);
2681 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00002682}
2683
Guido van Rossum3ac67412007-02-10 18:55:06 +00002684/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002685
2686static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002687dictkeys_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002688{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002689 if (dv->dv_dict == NULL) {
2690 Py_RETURN_NONE;
2691 }
2692 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00002693}
2694
2695static int
2696dictkeys_contains(dictviewobject *dv, PyObject *obj)
2697{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002698 if (dv->dv_dict == NULL)
2699 return 0;
2700 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002701}
2702
Guido van Rossum83825ac2007-02-10 04:54:19 +00002703static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002704 (lenfunc)dictview_len, /* sq_length */
2705 0, /* sq_concat */
2706 0, /* sq_repeat */
2707 0, /* sq_item */
2708 0, /* sq_slice */
2709 0, /* sq_ass_item */
2710 0, /* sq_ass_slice */
2711 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002712};
2713
Guido van Rossum523259b2007-08-24 23:41:22 +00002714static PyObject*
2715dictviews_sub(PyObject* self, PyObject *other)
2716{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002717 PyObject *result = PySet_New(self);
2718 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002719 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002721 if (result == NULL)
2722 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00002723
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002724 tmp = _PyObject_CallMethodId(result, &PyId_difference_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002725 if (tmp == NULL) {
2726 Py_DECREF(result);
2727 return NULL;
2728 }
Guido van Rossum523259b2007-08-24 23:41:22 +00002729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002730 Py_DECREF(tmp);
2731 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00002732}
2733
2734static PyObject*
2735dictviews_and(PyObject* self, PyObject *other)
2736{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002737 PyObject *result = PySet_New(self);
2738 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002739 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002741 if (result == NULL)
2742 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00002743
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002744 tmp = _PyObject_CallMethodId(result, &PyId_intersection_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002745 if (tmp == NULL) {
2746 Py_DECREF(result);
2747 return NULL;
2748 }
Guido van Rossum523259b2007-08-24 23:41:22 +00002749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002750 Py_DECREF(tmp);
2751 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00002752}
2753
2754static PyObject*
2755dictviews_or(PyObject* self, PyObject *other)
2756{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 PyObject *result = PySet_New(self);
2758 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002759 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02002760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002761 if (result == NULL)
2762 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00002763
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02002764 tmp = _PyObject_CallMethodId(result, &PyId_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002765 if (tmp == NULL) {
2766 Py_DECREF(result);
2767 return NULL;
2768 }
Guido van Rossum523259b2007-08-24 23:41:22 +00002769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002770 Py_DECREF(tmp);
2771 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00002772}
2773
2774static PyObject*
2775dictviews_xor(PyObject* self, PyObject *other)
2776{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002777 PyObject *result = PySet_New(self);
2778 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002779 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002781 if (result == NULL)
2782 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00002783
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002784 tmp = _PyObject_CallMethodId(result, &PyId_symmetric_difference_update, "O",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002785 other);
2786 if (tmp == NULL) {
2787 Py_DECREF(result);
2788 return NULL;
2789 }
Guido van Rossum523259b2007-08-24 23:41:22 +00002790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002791 Py_DECREF(tmp);
2792 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00002793}
2794
2795static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002796 0, /*nb_add*/
2797 (binaryfunc)dictviews_sub, /*nb_subtract*/
2798 0, /*nb_multiply*/
2799 0, /*nb_remainder*/
2800 0, /*nb_divmod*/
2801 0, /*nb_power*/
2802 0, /*nb_negative*/
2803 0, /*nb_positive*/
2804 0, /*nb_absolute*/
2805 0, /*nb_bool*/
2806 0, /*nb_invert*/
2807 0, /*nb_lshift*/
2808 0, /*nb_rshift*/
2809 (binaryfunc)dictviews_and, /*nb_and*/
2810 (binaryfunc)dictviews_xor, /*nb_xor*/
2811 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00002812};
2813
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00002814static PyObject*
2815dictviews_isdisjoint(PyObject *self, PyObject *other)
2816{
2817 PyObject *it;
2818 PyObject *item = NULL;
2819
2820 if (self == other) {
2821 if (dictview_len((dictviewobject *)self) == 0)
2822 Py_RETURN_TRUE;
2823 else
2824 Py_RETURN_FALSE;
2825 }
2826
2827 /* Iterate over the shorter object (only if other is a set,
2828 * because PySequence_Contains may be expensive otherwise): */
2829 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
2830 Py_ssize_t len_self = dictview_len((dictviewobject *)self);
2831 Py_ssize_t len_other = PyObject_Size(other);
2832 if (len_other == -1)
2833 return NULL;
2834
2835 if ((len_other > len_self)) {
2836 PyObject *tmp = other;
2837 other = self;
2838 self = tmp;
2839 }
2840 }
2841
2842 it = PyObject_GetIter(other);
2843 if (it == NULL)
2844 return NULL;
2845
2846 while ((item = PyIter_Next(it)) != NULL) {
2847 int contains = PySequence_Contains(self, item);
2848 Py_DECREF(item);
2849 if (contains == -1) {
2850 Py_DECREF(it);
2851 return NULL;
2852 }
2853
2854 if (contains) {
2855 Py_DECREF(it);
2856 Py_RETURN_FALSE;
2857 }
2858 }
2859 Py_DECREF(it);
2860 if (PyErr_Occurred())
2861 return NULL; /* PyIter_Next raised an exception. */
2862 Py_RETURN_TRUE;
2863}
2864
2865PyDoc_STRVAR(isdisjoint_doc,
2866"Return True if the view and the given iterable have a null intersection.");
2867
Guido van Rossumb90c8482007-02-10 01:11:45 +00002868static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00002869 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
2870 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002871 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002872};
2873
2874PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002875 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2876 "dict_keys", /* tp_name */
2877 sizeof(dictviewobject), /* tp_basicsize */
2878 0, /* tp_itemsize */
2879 /* methods */
2880 (destructor)dictview_dealloc, /* tp_dealloc */
2881 0, /* tp_print */
2882 0, /* tp_getattr */
2883 0, /* tp_setattr */
2884 0, /* tp_reserved */
2885 (reprfunc)dictview_repr, /* tp_repr */
2886 &dictviews_as_number, /* tp_as_number */
2887 &dictkeys_as_sequence, /* tp_as_sequence */
2888 0, /* tp_as_mapping */
2889 0, /* tp_hash */
2890 0, /* tp_call */
2891 0, /* tp_str */
2892 PyObject_GenericGetAttr, /* tp_getattro */
2893 0, /* tp_setattro */
2894 0, /* tp_as_buffer */
2895 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2896 0, /* tp_doc */
2897 (traverseproc)dictview_traverse, /* tp_traverse */
2898 0, /* tp_clear */
2899 dictview_richcompare, /* tp_richcompare */
2900 0, /* tp_weaklistoffset */
2901 (getiterfunc)dictkeys_iter, /* tp_iter */
2902 0, /* tp_iternext */
2903 dictkeys_methods, /* tp_methods */
2904 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00002905};
2906
2907static PyObject *
2908dictkeys_new(PyObject *dict)
2909{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002910 return dictview_new(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002911}
2912
Guido van Rossum3ac67412007-02-10 18:55:06 +00002913/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002914
2915static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002916dictitems_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002917{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002918 if (dv->dv_dict == NULL) {
2919 Py_RETURN_NONE;
2920 }
2921 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00002922}
2923
2924static int
2925dictitems_contains(dictviewobject *dv, PyObject *obj)
2926{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002927 PyObject *key, *value, *found;
2928 if (dv->dv_dict == NULL)
2929 return 0;
2930 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
2931 return 0;
2932 key = PyTuple_GET_ITEM(obj, 0);
2933 value = PyTuple_GET_ITEM(obj, 1);
2934 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
2935 if (found == NULL) {
2936 if (PyErr_Occurred())
2937 return -1;
2938 return 0;
2939 }
2940 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002941}
2942
Guido van Rossum83825ac2007-02-10 04:54:19 +00002943static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002944 (lenfunc)dictview_len, /* sq_length */
2945 0, /* sq_concat */
2946 0, /* sq_repeat */
2947 0, /* sq_item */
2948 0, /* sq_slice */
2949 0, /* sq_ass_item */
2950 0, /* sq_ass_slice */
2951 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002952};
2953
Guido van Rossumb90c8482007-02-10 01:11:45 +00002954static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00002955 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
2956 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002957 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002958};
2959
2960PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002961 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2962 "dict_items", /* tp_name */
2963 sizeof(dictviewobject), /* tp_basicsize */
2964 0, /* tp_itemsize */
2965 /* methods */
2966 (destructor)dictview_dealloc, /* tp_dealloc */
2967 0, /* tp_print */
2968 0, /* tp_getattr */
2969 0, /* tp_setattr */
2970 0, /* tp_reserved */
2971 (reprfunc)dictview_repr, /* tp_repr */
2972 &dictviews_as_number, /* tp_as_number */
2973 &dictitems_as_sequence, /* tp_as_sequence */
2974 0, /* tp_as_mapping */
2975 0, /* tp_hash */
2976 0, /* tp_call */
2977 0, /* tp_str */
2978 PyObject_GenericGetAttr, /* tp_getattro */
2979 0, /* tp_setattro */
2980 0, /* tp_as_buffer */
2981 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2982 0, /* tp_doc */
2983 (traverseproc)dictview_traverse, /* tp_traverse */
2984 0, /* tp_clear */
2985 dictview_richcompare, /* tp_richcompare */
2986 0, /* tp_weaklistoffset */
2987 (getiterfunc)dictitems_iter, /* tp_iter */
2988 0, /* tp_iternext */
2989 dictitems_methods, /* tp_methods */
2990 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00002991};
2992
2993static PyObject *
2994dictitems_new(PyObject *dict)
2995{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996 return dictview_new(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002997}
2998
Guido van Rossum3ac67412007-02-10 18:55:06 +00002999/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003000
3001static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003002dictvalues_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003003{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003004 if (dv->dv_dict == NULL) {
3005 Py_RETURN_NONE;
3006 }
3007 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003008}
3009
Guido van Rossum83825ac2007-02-10 04:54:19 +00003010static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003011 (lenfunc)dictview_len, /* sq_length */
3012 0, /* sq_concat */
3013 0, /* sq_repeat */
3014 0, /* sq_item */
3015 0, /* sq_slice */
3016 0, /* sq_ass_item */
3017 0, /* sq_ass_slice */
3018 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003019};
3020
Guido van Rossumb90c8482007-02-10 01:11:45 +00003021static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003022 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003023};
3024
3025PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003026 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3027 "dict_values", /* tp_name */
3028 sizeof(dictviewobject), /* tp_basicsize */
3029 0, /* tp_itemsize */
3030 /* methods */
3031 (destructor)dictview_dealloc, /* tp_dealloc */
3032 0, /* tp_print */
3033 0, /* tp_getattr */
3034 0, /* tp_setattr */
3035 0, /* tp_reserved */
3036 (reprfunc)dictview_repr, /* tp_repr */
3037 0, /* tp_as_number */
3038 &dictvalues_as_sequence, /* tp_as_sequence */
3039 0, /* tp_as_mapping */
3040 0, /* tp_hash */
3041 0, /* tp_call */
3042 0, /* tp_str */
3043 PyObject_GenericGetAttr, /* tp_getattro */
3044 0, /* tp_setattro */
3045 0, /* tp_as_buffer */
3046 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3047 0, /* tp_doc */
3048 (traverseproc)dictview_traverse, /* tp_traverse */
3049 0, /* tp_clear */
3050 0, /* tp_richcompare */
3051 0, /* tp_weaklistoffset */
3052 (getiterfunc)dictvalues_iter, /* tp_iter */
3053 0, /* tp_iternext */
3054 dictvalues_methods, /* tp_methods */
3055 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003056};
3057
3058static PyObject *
3059dictvalues_new(PyObject *dict)
3060{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003061 return dictview_new(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003062}