blob: 5a8a8a7a09a755a1f1808cd8322cf09b80512837 [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
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100220int
221PyDict_ClearFreeList(void)
Christian Heimes77c02eb2008-02-09 02:18:51 +0000222{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 PyDictObject *op;
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100224 int ret = numfree;
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 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100230 return ret;
231}
232
233void
234PyDict_Fini(void)
235{
236 PyDict_ClearFreeList();
Christian Heimes77c02eb2008-02-09 02:18:51 +0000237}
238
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000239PyObject *
Thomas Wouters78890102000-07-22 19:25:51 +0000240PyDict_New(void)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000241{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 register PyDictObject *mp;
243 if (dummy == NULL) { /* Auto-initialize dummy */
244 dummy = PyUnicode_FromString("<dummy key>");
245 if (dummy == NULL)
246 return NULL;
Fred Drake1bff34a2000-08-31 19:31:38 +0000247#ifdef SHOW_CONVERSION_COUNTS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 Py_AtExit(show_counts);
Fred Drake1bff34a2000-08-31 19:31:38 +0000249#endif
Christian Heimes77c02eb2008-02-09 02:18:51 +0000250#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 Py_AtExit(show_alloc);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000252#endif
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000253#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 Py_AtExit(show_track);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000255#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 }
257 if (numfree) {
258 mp = free_list[--numfree];
259 assert (mp != NULL);
260 assert (Py_TYPE(mp) == &PyDict_Type);
261 _Py_NewReference((PyObject *)mp);
262 if (mp->ma_fill) {
263 EMPTY_TO_MINSIZE(mp);
264 } else {
265 /* At least set ma_table and ma_mask; these are wrong
266 if an empty but presized dict is added to freelist */
267 INIT_NONZERO_DICT_SLOTS(mp);
268 }
269 assert (mp->ma_used == 0);
270 assert (mp->ma_table == mp->ma_smalltable);
271 assert (mp->ma_mask == PyDict_MINSIZE - 1);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000272#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 count_reuse++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000274#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 } else {
276 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
277 if (mp == NULL)
278 return NULL;
279 EMPTY_TO_MINSIZE(mp);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000280#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 count_alloc++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000282#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 }
284 mp->ma_lookup = lookdict_unicode;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000285#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 count_untracked++;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000287#endif
Fred Drake1bff34a2000-08-31 19:31:38 +0000288#ifdef SHOW_CONVERSION_COUNTS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 ++created;
Fred Drake1bff34a2000-08-31 19:31:38 +0000290#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000292}
293
294/*
295The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000296This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000297Open addressing is preferred over chaining since the link overhead for
298chaining would be substantial (100% with typical malloc overhead).
299
Tim Peterseb28ef22001-06-02 05:27:19 +0000300The initial probe index is computed as hash mod the table size. Subsequent
301probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000302
303All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000304
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000305The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000306contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000307Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000308
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000309lookdict() is general-purpose, and may return NULL if (and only if) a
310comparison raises an exception (this was new in Python 2.5).
Guido van Rossum89d8c602007-09-18 17:26:56 +0000311lookdict_unicode() below is specialized to string keys, comparison of which can
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000312never raise an exception; that function can never return NULL. For both, when
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000313the key isn't found a PyDictEntry* is returned for which the me_value field is
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000314NULL; this is the slot in the dict at which the key would have been found, and
315the caller can (if it wishes) add the <key, value> pair to the returned
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000316PyDictEntry*.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000317*/
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000318static PyDictEntry *
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000319lookdict(PyDictObject *mp, PyObject *key, register Py_hash_t hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000320{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 register size_t i;
322 register size_t perturb;
323 register PyDictEntry *freeslot;
324 register size_t mask = (size_t)mp->ma_mask;
325 PyDictEntry *ep0 = mp->ma_table;
326 register PyDictEntry *ep;
327 register int cmp;
328 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 i = (size_t)hash & mask;
331 ep = &ep0[i];
332 if (ep->me_key == NULL || ep->me_key == key)
333 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 if (ep->me_key == dummy)
336 freeslot = ep;
337 else {
338 if (ep->me_hash == hash) {
339 startkey = ep->me_key;
340 Py_INCREF(startkey);
341 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
342 Py_DECREF(startkey);
343 if (cmp < 0)
344 return NULL;
345 if (ep0 == mp->ma_table && ep->me_key == startkey) {
346 if (cmp > 0)
347 return ep;
348 }
349 else {
350 /* The compare did major nasty stuff to the
351 * dict: start over.
352 * XXX A clever adversary could prevent this
353 * XXX from terminating.
354 */
355 return lookdict(mp, key, hash);
356 }
357 }
358 freeslot = NULL;
359 }
Tim Peters15d49292001-05-27 07:39:22 +0000360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 /* In the loop, me_key == dummy is by far (factor of 100s) the
362 least likely outcome, so test for that last. */
363 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
364 i = (i << 2) + i + perturb + 1;
365 ep = &ep0[i & mask];
366 if (ep->me_key == NULL)
367 return freeslot == NULL ? ep : freeslot;
368 if (ep->me_key == key)
369 return ep;
370 if (ep->me_hash == hash && ep->me_key != dummy) {
371 startkey = ep->me_key;
372 Py_INCREF(startkey);
373 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
374 Py_DECREF(startkey);
375 if (cmp < 0)
376 return NULL;
377 if (ep0 == mp->ma_table && ep->me_key == startkey) {
378 if (cmp > 0)
379 return ep;
380 }
381 else {
382 /* The compare did major nasty stuff to the
383 * dict: start over.
384 * XXX A clever adversary could prevent this
385 * XXX from terminating.
386 */
387 return lookdict(mp, key, hash);
388 }
389 }
390 else if (ep->me_key == dummy && freeslot == NULL)
391 freeslot = ep;
392 }
393 assert(0); /* NOT REACHED */
394 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000395}
396
397/*
Guido van Rossum89d8c602007-09-18 17:26:56 +0000398 * Hacked up version of lookdict which can assume keys are always
399 * unicodes; this assumption allows testing for errors during
400 * PyObject_RichCompareBool() to be dropped; unicode-unicode
401 * comparisons never raise exceptions. This also means we don't need
402 * to go through PyObject_RichCompareBool(); we can always use
403 * unicode_eq() directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000404 *
Guido van Rossum89d8c602007-09-18 17:26:56 +0000405 * This is valuable because dicts with only unicode keys are very common.
Fred Drake1bff34a2000-08-31 19:31:38 +0000406 */
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000407static PyDictEntry *
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000408lookdict_unicode(PyDictObject *mp, PyObject *key, register Py_hash_t hash)
Fred Drake1bff34a2000-08-31 19:31:38 +0000409{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 register size_t i;
411 register size_t perturb;
412 register PyDictEntry *freeslot;
413 register size_t mask = (size_t)mp->ma_mask;
414 PyDictEntry *ep0 = mp->ma_table;
415 register PyDictEntry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 /* Make sure this function doesn't have to handle non-unicode keys,
418 including subclasses of str; e.g., one reason to subclass
419 unicodes is to override __eq__, and for speed we don't cater to
420 that here. */
421 if (!PyUnicode_CheckExact(key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000422#ifdef SHOW_CONVERSION_COUNTS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 ++converted;
Fred Drake1bff34a2000-08-31 19:31:38 +0000424#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 mp->ma_lookup = lookdict;
426 return lookdict(mp, key, hash);
427 }
Mark Dickinson57e683e2011-09-24 18:18:40 +0100428 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 ep = &ep0[i];
430 if (ep->me_key == NULL || ep->me_key == key)
431 return ep;
432 if (ep->me_key == dummy)
433 freeslot = ep;
434 else {
435 if (ep->me_hash == hash && unicode_eq(ep->me_key, key))
436 return ep;
437 freeslot = NULL;
438 }
Tim Peters15d49292001-05-27 07:39:22 +0000439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 /* In the loop, me_key == dummy is by far (factor of 100s) the
441 least likely outcome, so test for that last. */
442 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
443 i = (i << 2) + i + perturb + 1;
444 ep = &ep0[i & mask];
445 if (ep->me_key == NULL)
446 return freeslot == NULL ? ep : freeslot;
447 if (ep->me_key == key
448 || (ep->me_hash == hash
449 && ep->me_key != dummy
450 && unicode_eq(ep->me_key, key)))
451 return ep;
452 if (ep->me_key == dummy && freeslot == NULL)
453 freeslot = ep;
454 }
455 assert(0); /* NOT REACHED */
456 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000457}
458
Benjamin Petersonfb886362010-04-24 18:21:17 +0000459int
460_PyDict_HasOnlyStringKeys(PyObject *dict)
461{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 Py_ssize_t pos = 0;
463 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000464 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 /* Shortcut */
466 if (((PyDictObject *)dict)->ma_lookup == lookdict_unicode)
467 return 1;
468 while (PyDict_Next(dict, &pos, &key, &value))
469 if (!PyUnicode_Check(key))
470 return 0;
471 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000472}
473
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000474#ifdef SHOW_TRACK_COUNT
475#define INCREASE_TRACK_COUNT \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 (count_tracked++, count_untracked--);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000477#define DECREASE_TRACK_COUNT \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 (count_tracked--, count_untracked++);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000479#else
480#define INCREASE_TRACK_COUNT
481#define DECREASE_TRACK_COUNT
482#endif
483
484#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 do { \
486 if (!_PyObject_GC_IS_TRACKED(mp)) { \
487 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
488 _PyObject_GC_MAY_BE_TRACKED(value)) { \
489 _PyObject_GC_TRACK(mp); \
490 INCREASE_TRACK_COUNT \
491 } \
492 } \
493 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000494
495void
496_PyDict_MaybeUntrack(PyObject *op)
497{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 PyDictObject *mp;
499 PyObject *value;
500 Py_ssize_t mask, i;
501 PyDictEntry *ep;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
504 return;
505
506 mp = (PyDictObject *) op;
507 ep = mp->ma_table;
508 mask = mp->ma_mask;
509 for (i = 0; i <= mask; i++) {
510 if ((value = ep[i].me_value) == NULL)
511 continue;
512 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
513 _PyObject_GC_MAY_BE_TRACKED(ep[i].me_key))
514 return;
515 }
516 DECREASE_TRACK_COUNT
517 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000518}
519
520
Fred Drake1bff34a2000-08-31 19:31:38 +0000521/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000522Internal routine to insert a new item into the table.
523Used both by the internal resize routine and by the public insert routine.
524Eats a reference to key and one to value.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000525Returns -1 if an error occurred, or 0 on success.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000526*/
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000527static int
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000528insertdict(register PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000529{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 PyObject *old_value;
531 register PyDictEntry *ep;
Antoine Pitroufbb1c612010-10-23 17:37:54 +0000532 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, Py_hash_t);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 assert(mp->ma_lookup != NULL);
535 ep = mp->ma_lookup(mp, key, hash);
536 if (ep == NULL) {
537 Py_DECREF(key);
538 Py_DECREF(value);
539 return -1;
540 }
541 MAINTAIN_TRACKING(mp, key, value);
542 if (ep->me_value != NULL) {
543 old_value = ep->me_value;
544 ep->me_value = value;
545 Py_DECREF(old_value); /* which **CAN** re-enter */
546 Py_DECREF(key);
547 }
548 else {
549 if (ep->me_key == NULL)
550 mp->ma_fill++;
551 else {
552 assert(ep->me_key == dummy);
553 Py_DECREF(dummy);
554 }
555 ep->me_key = key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000556 ep->me_hash = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 ep->me_value = value;
558 mp->ma_used++;
559 }
560 return 0;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000561}
562
563/*
564Internal routine used by dictresize() to insert an item which is
565known to be absent from the dict. This routine also assumes that
566the dict contains no deleted entries. Besides the performance benefit,
567using insertdict() in dictresize() is dangerous (SF bug #1456209).
568Note that no refcounts are changed by this routine; if needed, the caller
569is responsible for incref'ing `key` and `value`.
570*/
571static void
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000572insertdict_clean(register PyDictObject *mp, PyObject *key, Py_hash_t hash,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 PyObject *value)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000574{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 register size_t i;
576 register size_t perturb;
577 register size_t mask = (size_t)mp->ma_mask;
578 PyDictEntry *ep0 = mp->ma_table;
579 register PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 MAINTAIN_TRACKING(mp, key, value);
Mark Dickinson57e683e2011-09-24 18:18:40 +0100582 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 ep = &ep0[i];
584 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
585 i = (i << 2) + i + perturb + 1;
586 ep = &ep0[i & mask];
587 }
588 assert(ep->me_value == NULL);
589 mp->ma_fill++;
590 ep->me_key = key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000591 ep->me_hash = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 ep->me_value = value;
593 mp->ma_used++;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000594}
595
596/*
597Restructure the table by allocating a new table and reinserting all
598items again. When entries have been deleted, the new table may
599actually be smaller than the old one.
600*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000601static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000602dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000603{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 Py_ssize_t newsize;
605 PyDictEntry *oldtable, *newtable, *ep;
606 Py_ssize_t i;
607 int is_oldtable_malloced;
608 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 /* Find the smallest table size > minused. */
613 for (newsize = PyDict_MINSIZE;
614 newsize <= minused && newsize > 0;
615 newsize <<= 1)
616 ;
617 if (newsize <= 0) {
618 PyErr_NoMemory();
619 return -1;
620 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 /* Get space for a new table. */
623 oldtable = mp->ma_table;
624 assert(oldtable != NULL);
625 is_oldtable_malloced = oldtable != mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 if (newsize == PyDict_MINSIZE) {
628 /* A large table is shrinking, or we can't get any smaller. */
629 newtable = mp->ma_smalltable;
630 if (newtable == oldtable) {
631 if (mp->ma_fill == mp->ma_used) {
632 /* No dummies, so no point doing anything. */
633 return 0;
634 }
635 /* We're not going to resize it, but rebuild the
636 table anyway to purge old dummy entries.
637 Subtle: This is *necessary* if fill==size,
638 as lookdict needs at least one virgin slot to
639 terminate failing searches. If fill < size, it's
640 merely desirable, as dummies slow searches. */
641 assert(mp->ma_fill > mp->ma_used);
642 memcpy(small_copy, oldtable, sizeof(small_copy));
643 oldtable = small_copy;
644 }
645 }
646 else {
647 newtable = PyMem_NEW(PyDictEntry, newsize);
648 if (newtable == NULL) {
649 PyErr_NoMemory();
650 return -1;
651 }
652 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 /* Make the dict empty, using the new table. */
655 assert(newtable != oldtable);
656 mp->ma_table = newtable;
657 mp->ma_mask = newsize - 1;
658 memset(newtable, 0, sizeof(PyDictEntry) * newsize);
659 mp->ma_used = 0;
660 i = mp->ma_fill;
661 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 /* Copy the data over; this is refcount-neutral for active entries;
664 dummy entries aren't copied over, of course */
665 for (ep = oldtable; i > 0; ep++) {
666 if (ep->me_value != NULL) { /* active entry */
667 --i;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000668 insertdict_clean(mp, ep->me_key, ep->me_hash, ep->me_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 }
670 else if (ep->me_key != NULL) { /* dummy entry */
671 --i;
672 assert(ep->me_key == dummy);
673 Py_DECREF(ep->me_key);
674 }
675 /* else key == value == NULL: nothing to do */
676 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 if (is_oldtable_malloced)
679 PyMem_DEL(oldtable);
680 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000681}
682
Christian Heimes99170a52007-12-19 02:07:34 +0000683/* Create a new dictionary pre-sized to hold an estimated number of elements.
684 Underestimates are okay because the dictionary will resize as necessary.
685 Overestimates just mean the dictionary will be more sparse than usual.
686*/
687
688PyObject *
689_PyDict_NewPresized(Py_ssize_t minused)
690{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 PyObject *op = PyDict_New();
Christian Heimes99170a52007-12-19 02:07:34 +0000692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 if (minused>5 && op != NULL && dictresize((PyDictObject *)op, minused) == -1) {
694 Py_DECREF(op);
695 return NULL;
696 }
697 return op;
Christian Heimes99170a52007-12-19 02:07:34 +0000698}
699
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000700/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
701 * that may occur (originally dicts supported only string keys, and exceptions
702 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000703 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000704 * (suppressed) occurred while computing the key's hash, or that some error
705 * (suppressed) occurred when comparing keys in the dict's internal probe
706 * sequence. A nasty example of the latter is when a Python-coded comparison
707 * function hits a stack-depth error, which can cause this to return NULL
708 * even if the key is present.
709 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000710PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000711PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000712{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000713 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 PyDictObject *mp = (PyDictObject *)op;
715 PyDictEntry *ep;
716 PyThreadState *tstate;
717 if (!PyDict_Check(op))
718 return NULL;
719 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200720 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 {
722 hash = PyObject_Hash(key);
723 if (hash == -1) {
724 PyErr_Clear();
725 return NULL;
726 }
727 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 /* We can arrive here with a NULL tstate during initialization: try
730 running "python -Wi" for an example related to string interning.
731 Let's just hope that no exception occurs then... This must be
732 _PyThreadState_Current and not PyThreadState_GET() because in debug
733 mode, the latter complains if tstate is NULL. */
734 tstate = (PyThreadState*)_Py_atomic_load_relaxed(
735 &_PyThreadState_Current);
736 if (tstate != NULL && tstate->curexc_type != NULL) {
737 /* preserve the existing exception */
738 PyObject *err_type, *err_value, *err_tb;
739 PyErr_Fetch(&err_type, &err_value, &err_tb);
740 ep = (mp->ma_lookup)(mp, key, hash);
741 /* ignore errors */
742 PyErr_Restore(err_type, err_value, err_tb);
743 if (ep == NULL)
744 return NULL;
745 }
746 else {
747 ep = (mp->ma_lookup)(mp, key, hash);
748 if (ep == NULL) {
749 PyErr_Clear();
750 return NULL;
751 }
752 }
753 return ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000754}
755
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000756/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
757 This returns NULL *with* an exception set if an exception occurred.
758 It returns NULL *without* an exception set if the key wasn't present.
759*/
760PyObject *
761PyDict_GetItemWithError(PyObject *op, PyObject *key)
762{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000763 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 PyDictObject*mp = (PyDictObject *)op;
765 PyDictEntry *ep;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 if (!PyDict_Check(op)) {
768 PyErr_BadInternalCall();
769 return NULL;
770 }
771 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200772 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 {
774 hash = PyObject_Hash(key);
775 if (hash == -1) {
776 return NULL;
777 }
778 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 ep = (mp->ma_lookup)(mp, key, hash);
781 if (ep == NULL)
782 return NULL;
783 return ep->me_value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000784}
785
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000786/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
Tim Peters60b29962006-01-01 01:19:23 +0000787 * dictionary if it's merely replacing the value for an existing key.
788 * This means that it's safe to loop over a dictionary with PyDict_Next()
789 * and occasionally replace a value -- but you can't insert new keys or
790 * remove them.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000791 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000792int
Tim Peters1f5871e2000-07-04 17:44:48 +0000793PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000794{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 register PyDictObject *mp;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000796 register Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 register Py_ssize_t n_used;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 if (!PyDict_Check(op)) {
800 PyErr_BadInternalCall();
801 return -1;
802 }
803 assert(key);
804 assert(value);
805 mp = (PyDictObject *)op;
806 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200807 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 {
809 hash = PyObject_Hash(key);
810 if (hash == -1)
811 return -1;
812 }
813 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
814 n_used = mp->ma_used;
815 Py_INCREF(value);
816 Py_INCREF(key);
817 if (insertdict(mp, key, hash, value) != 0)
818 return -1;
819 /* If we added a key, we can safely resize. Otherwise just return!
820 * If fill >= 2/3 size, adjust size. Normally, this doubles or
821 * quaduples the size, but it's also possible for the dict to shrink
822 * (if ma_fill is much larger than ma_used, meaning a lot of dict
823 * keys have been * deleted).
824 *
825 * Quadrupling the size improves average dictionary sparseness
826 * (reducing collisions) at the cost of some memory and iteration
827 * speed (which loops over every possible entry). It also halves
828 * the number of expensive resize operations in a growing dictionary.
829 *
830 * Very large dictionaries (over 50K items) use doubling instead.
831 * This may help applications with severe memory constraints.
832 */
833 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
834 return 0;
835 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000836}
837
838int
Tim Peters1f5871e2000-07-04 17:44:48 +0000839PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000840{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 register PyDictObject *mp;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000842 register Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 register PyDictEntry *ep;
844 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 if (!PyDict_Check(op)) {
847 PyErr_BadInternalCall();
848 return -1;
849 }
850 assert(key);
851 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200852 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 hash = PyObject_Hash(key);
854 if (hash == -1)
855 return -1;
856 }
857 mp = (PyDictObject *)op;
858 ep = (mp->ma_lookup)(mp, key, hash);
859 if (ep == NULL)
860 return -1;
861 if (ep->me_value == NULL) {
862 set_key_error(key);
863 return -1;
864 }
865 old_key = ep->me_key;
866 Py_INCREF(dummy);
867 ep->me_key = dummy;
868 old_value = ep->me_value;
869 ep->me_value = NULL;
870 mp->ma_used--;
871 Py_DECREF(old_value);
872 Py_DECREF(old_key);
873 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000874}
875
Guido van Rossum25831651993-05-19 14:50:45 +0000876void
Tim Peters1f5871e2000-07-04 17:44:48 +0000877PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000878{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 PyDictObject *mp;
880 PyDictEntry *ep, *table;
881 int table_is_malloced;
882 Py_ssize_t fill;
883 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000884#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +0000886#endif
887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 if (!PyDict_Check(op))
889 return;
890 mp = (PyDictObject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000891#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 n = mp->ma_mask + 1;
893 i = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000894#endif
895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 table = mp->ma_table;
897 assert(table != NULL);
898 table_is_malloced = table != mp->ma_smalltable;
Tim Petersdea48ec2001-05-22 20:40:22 +0000899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 /* This is delicate. During the process of clearing the dict,
901 * decrefs can cause the dict to mutate. To avoid fatal confusion
902 * (voice of experience), we have to make the dict empty before
903 * clearing the slots, and never refer to anything via mp->xxx while
904 * clearing.
905 */
906 fill = mp->ma_fill;
907 if (table_is_malloced)
908 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 else if (fill > 0) {
911 /* It's a small table with something that needs to be cleared.
912 * Afraid the only safe way is to copy the dict entries into
913 * another small table first.
914 */
915 memcpy(small_copy, table, sizeof(small_copy));
916 table = small_copy;
917 EMPTY_TO_MINSIZE(mp);
918 }
919 /* else it's a small table that's already empty */
Tim Petersdea48ec2001-05-22 20:40:22 +0000920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 /* Now we can finally clear things. If C had refcounts, we could
922 * assert that the refcount on table is 1 now, i.e. that this function
923 * has unique access to it, so decref side-effects can't alter it.
924 */
925 for (ep = table; fill > 0; ++ep) {
Tim Petersdea48ec2001-05-22 20:40:22 +0000926#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 assert(i < n);
928 ++i;
Tim Petersdea48ec2001-05-22 20:40:22 +0000929#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 if (ep->me_key) {
931 --fill;
932 Py_DECREF(ep->me_key);
933 Py_XDECREF(ep->me_value);
934 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000935#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 else
937 assert(ep->me_value == NULL);
Tim Petersdea48ec2001-05-22 20:40:22 +0000938#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000940
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 if (table_is_malloced)
942 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000943}
944
Tim Peters080c88b2003-02-15 03:01:11 +0000945/*
946 * Iterate over a dict. Use like so:
947 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000948 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +0000949 * PyObject *key, *value;
950 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +0000951 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +0000952 * Refer to borrowed references in key and value.
953 * }
954 *
955 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +0000956 * mutates the dict. One exception: it is safe if the loop merely changes
957 * the values associated with the keys (but doesn't insert new keys or
958 * delete keys), via PyDict_SetItem().
959 */
Guido van Rossum25831651993-05-19 14:50:45 +0000960int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000961PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000962{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 register Py_ssize_t i;
964 register Py_ssize_t mask;
965 register PyDictEntry *ep;
Raymond Hettinger43442782004-03-17 21:55:03 +0000966
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 if (!PyDict_Check(op))
968 return 0;
969 i = *ppos;
970 if (i < 0)
971 return 0;
972 ep = ((PyDictObject *)op)->ma_table;
973 mask = ((PyDictObject *)op)->ma_mask;
974 while (i <= mask && ep[i].me_value == NULL)
975 i++;
976 *ppos = i+1;
977 if (i > mask)
978 return 0;
979 if (pkey)
980 *pkey = ep[i].me_key;
981 if (pvalue)
982 *pvalue = ep[i].me_value;
983 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000984}
985
Thomas Wouterscf297e42007-02-23 15:07:44 +0000986/* Internal version of PyDict_Next that returns a hash value in addition to the key and value.*/
987int
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000988_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, Py_hash_t *phash)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000989{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 register Py_ssize_t i;
991 register Py_ssize_t mask;
992 register PyDictEntry *ep;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000993
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 if (!PyDict_Check(op))
995 return 0;
996 i = *ppos;
997 if (i < 0)
998 return 0;
999 ep = ((PyDictObject *)op)->ma_table;
1000 mask = ((PyDictObject *)op)->ma_mask;
1001 while (i <= mask && ep[i].me_value == NULL)
1002 i++;
1003 *ppos = i+1;
1004 if (i > mask)
1005 return 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001006 *phash = ep[i].me_hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 if (pkey)
1008 *pkey = ep[i].me_key;
1009 if (pvalue)
1010 *pvalue = ep[i].me_value;
1011 return 1;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001012}
1013
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001014/* Methods */
1015
1016static void
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001017dict_dealloc(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001018{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 register PyDictEntry *ep;
1020 Py_ssize_t fill = mp->ma_fill;
1021 PyObject_GC_UnTrack(mp);
1022 Py_TRASHCAN_SAFE_BEGIN(mp)
1023 for (ep = mp->ma_table; fill > 0; ep++) {
1024 if (ep->me_key) {
1025 --fill;
1026 Py_DECREF(ep->me_key);
1027 Py_XDECREF(ep->me_value);
1028 }
1029 }
1030 if (mp->ma_table != mp->ma_smalltable)
1031 PyMem_DEL(mp->ma_table);
1032 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1033 free_list[numfree++] = mp;
1034 else
1035 Py_TYPE(mp)->tp_free((PyObject *)mp);
1036 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001037}
1038
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001039static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001040dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001041{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 Py_ssize_t i;
1043 PyObject *s, *temp, *colon = NULL;
1044 PyObject *pieces = NULL, *result = NULL;
1045 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +00001046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 i = Py_ReprEnter((PyObject *)mp);
1048 if (i != 0) {
1049 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1050 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 if (mp->ma_used == 0) {
1053 result = PyUnicode_FromString("{}");
1054 goto Done;
1055 }
Tim Petersa7259592001-06-16 05:11:17 +00001056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 pieces = PyList_New(0);
1058 if (pieces == NULL)
1059 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 colon = PyUnicode_FromString(": ");
1062 if (colon == NULL)
1063 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 /* Do repr() on each key+value pair, and insert ": " between them.
1066 Note that repr may mutate the dict. */
1067 i = 0;
1068 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
1069 int status;
1070 /* Prevent repr from deleting value during key format. */
1071 Py_INCREF(value);
1072 s = PyObject_Repr(key);
1073 PyUnicode_Append(&s, colon);
1074 PyUnicode_AppendAndDel(&s, PyObject_Repr(value));
1075 Py_DECREF(value);
1076 if (s == NULL)
1077 goto Done;
1078 status = PyList_Append(pieces, s);
1079 Py_DECREF(s); /* append created a new ref */
1080 if (status < 0)
1081 goto Done;
1082 }
Tim Petersa7259592001-06-16 05:11:17 +00001083
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 /* Add "{}" decorations to the first and last items. */
1085 assert(PyList_GET_SIZE(pieces) > 0);
1086 s = PyUnicode_FromString("{");
1087 if (s == NULL)
1088 goto Done;
1089 temp = PyList_GET_ITEM(pieces, 0);
1090 PyUnicode_AppendAndDel(&s, temp);
1091 PyList_SET_ITEM(pieces, 0, s);
1092 if (s == NULL)
1093 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001094
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 s = PyUnicode_FromString("}");
1096 if (s == NULL)
1097 goto Done;
1098 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
1099 PyUnicode_AppendAndDel(&temp, s);
1100 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
1101 if (temp == NULL)
1102 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 /* Paste them all together with ", " between. */
1105 s = PyUnicode_FromString(", ");
1106 if (s == NULL)
1107 goto Done;
1108 result = PyUnicode_Join(s, pieces);
1109 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +00001110
1111Done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 Py_XDECREF(pieces);
1113 Py_XDECREF(colon);
1114 Py_ReprLeave((PyObject *)mp);
1115 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001116}
1117
Martin v. Löwis18e16552006-02-15 17:27:45 +00001118static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001119dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001120{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001122}
1123
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001124static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001125dict_subscript(PyDictObject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001126{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 PyObject *v;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001128 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 PyDictEntry *ep;
1130 assert(mp->ma_table != NULL);
1131 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001132 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 hash = PyObject_Hash(key);
1134 if (hash == -1)
1135 return NULL;
1136 }
1137 ep = (mp->ma_lookup)(mp, key, hash);
1138 if (ep == NULL)
1139 return NULL;
1140 v = ep->me_value;
1141 if (v == NULL) {
1142 if (!PyDict_CheckExact(mp)) {
1143 /* Look up __missing__ method if we're a subclass. */
1144 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05001145 _Py_IDENTIFIER(__missing__);
1146 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 if (missing != NULL) {
1148 res = PyObject_CallFunctionObjArgs(missing,
1149 key, NULL);
1150 Py_DECREF(missing);
1151 return res;
1152 }
1153 else if (PyErr_Occurred())
1154 return NULL;
1155 }
1156 set_key_error(key);
1157 return NULL;
1158 }
1159 else
1160 Py_INCREF(v);
1161 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001162}
1163
1164static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001165dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001166{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 if (w == NULL)
1168 return PyDict_DelItem((PyObject *)mp, v);
1169 else
1170 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001171}
1172
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001173static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 (lenfunc)dict_length, /*mp_length*/
1175 (binaryfunc)dict_subscript, /*mp_subscript*/
1176 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001177};
1178
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001179static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001180dict_keys(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001181{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 register PyObject *v;
1183 register Py_ssize_t i, j;
1184 PyDictEntry *ep;
1185 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001186
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001187 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 n = mp->ma_used;
1189 v = PyList_New(n);
1190 if (v == NULL)
1191 return NULL;
1192 if (n != mp->ma_used) {
1193 /* Durnit. The allocations caused the dict to resize.
1194 * Just start over, this shouldn't normally happen.
1195 */
1196 Py_DECREF(v);
1197 goto again;
1198 }
1199 ep = mp->ma_table;
1200 mask = mp->ma_mask;
1201 for (i = 0, j = 0; i <= mask; i++) {
1202 if (ep[i].me_value != NULL) {
1203 PyObject *key = ep[i].me_key;
1204 Py_INCREF(key);
1205 PyList_SET_ITEM(v, j, key);
1206 j++;
1207 }
1208 }
1209 assert(j == n);
1210 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001211}
1212
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001213static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001214dict_values(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001215{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 register PyObject *v;
1217 register Py_ssize_t i, j;
1218 PyDictEntry *ep;
1219 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001220
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001221 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 n = mp->ma_used;
1223 v = PyList_New(n);
1224 if (v == NULL)
1225 return NULL;
1226 if (n != mp->ma_used) {
1227 /* Durnit. The allocations caused the dict to resize.
1228 * Just start over, this shouldn't normally happen.
1229 */
1230 Py_DECREF(v);
1231 goto again;
1232 }
1233 ep = mp->ma_table;
1234 mask = mp->ma_mask;
1235 for (i = 0, j = 0; i <= mask; i++) {
1236 if (ep[i].me_value != NULL) {
1237 PyObject *value = ep[i].me_value;
1238 Py_INCREF(value);
1239 PyList_SET_ITEM(v, j, value);
1240 j++;
1241 }
1242 }
1243 assert(j == n);
1244 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001245}
1246
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001247static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001248dict_items(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001249{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 register PyObject *v;
1251 register Py_ssize_t i, j, n;
1252 Py_ssize_t mask;
1253 PyObject *item, *key, *value;
1254 PyDictEntry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 /* Preallocate the list of tuples, to avoid allocations during
1257 * the loop over the items, which could trigger GC, which
1258 * could resize the dict. :-(
1259 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001260 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 n = mp->ma_used;
1262 v = PyList_New(n);
1263 if (v == NULL)
1264 return NULL;
1265 for (i = 0; i < n; i++) {
1266 item = PyTuple_New(2);
1267 if (item == NULL) {
1268 Py_DECREF(v);
1269 return NULL;
1270 }
1271 PyList_SET_ITEM(v, i, item);
1272 }
1273 if (n != mp->ma_used) {
1274 /* Durnit. The allocations caused the dict to resize.
1275 * Just start over, this shouldn't normally happen.
1276 */
1277 Py_DECREF(v);
1278 goto again;
1279 }
1280 /* Nothing we do below makes any function calls. */
1281 ep = mp->ma_table;
1282 mask = mp->ma_mask;
1283 for (i = 0, j = 0; i <= mask; i++) {
1284 if ((value=ep[i].me_value) != NULL) {
1285 key = ep[i].me_key;
1286 item = PyList_GET_ITEM(v, j);
1287 Py_INCREF(key);
1288 PyTuple_SET_ITEM(item, 0, key);
1289 Py_INCREF(value);
1290 PyTuple_SET_ITEM(item, 1, value);
1291 j++;
1292 }
1293 }
1294 assert(j == n);
1295 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001296}
1297
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001298static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001299dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001300{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 PyObject *seq;
1302 PyObject *value = Py_None;
1303 PyObject *it; /* iter(seq) */
1304 PyObject *key;
1305 PyObject *d;
1306 int status;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
1309 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 d = PyObject_CallObject(cls, NULL);
1312 if (d == NULL)
1313 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) {
1316 PyDictObject *mp = (PyDictObject *)d;
1317 PyObject *oldvalue;
1318 Py_ssize_t pos = 0;
1319 PyObject *key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001320 Py_hash_t hash;
Guido van Rossum58da9312007-11-10 23:39:45 +00001321
Petri Lehtinena94200e2011-10-24 21:12:58 +03001322 if (dictresize(mp, Py_SIZE(seq))) {
1323 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001325 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
1328 Py_INCREF(key);
1329 Py_INCREF(value);
Petri Lehtinena94200e2011-10-24 21:12:58 +03001330 if (insertdict(mp, key, hash, value)) {
1331 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001333 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 }
1335 return d;
1336 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
1339 PyDictObject *mp = (PyDictObject *)d;
1340 Py_ssize_t pos = 0;
1341 PyObject *key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001342 Py_hash_t hash;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001343
Petri Lehtinena94200e2011-10-24 21:12:58 +03001344 if (dictresize(mp, PySet_GET_SIZE(seq))) {
1345 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001347 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
1350 Py_INCREF(key);
1351 Py_INCREF(value);
Petri Lehtinena94200e2011-10-24 21:12:58 +03001352 if (insertdict(mp, key, hash, value)) {
1353 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001355 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 }
1357 return d;
1358 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 it = PyObject_GetIter(seq);
1361 if (it == NULL){
1362 Py_DECREF(d);
1363 return NULL;
1364 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 if (PyDict_CheckExact(d)) {
1367 while ((key = PyIter_Next(it)) != NULL) {
1368 status = PyDict_SetItem(d, key, value);
1369 Py_DECREF(key);
1370 if (status < 0)
1371 goto Fail;
1372 }
1373 } else {
1374 while ((key = PyIter_Next(it)) != NULL) {
1375 status = PyObject_SetItem(d, key, value);
1376 Py_DECREF(key);
1377 if (status < 0)
1378 goto Fail;
1379 }
1380 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 if (PyErr_Occurred())
1383 goto Fail;
1384 Py_DECREF(it);
1385 return d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001386
1387Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 Py_DECREF(it);
1389 Py_DECREF(d);
1390 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001391}
1392
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001393static int
1394dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001395{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 PyObject *arg = NULL;
1397 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1400 result = -1;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001403 _Py_IDENTIFIER(keys);
1404 if (_PyObject_HasAttrId(arg, &PyId_keys))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 result = PyDict_Merge(self, arg, 1);
1406 else
1407 result = PyDict_MergeFromSeq2(self, arg, 1);
1408 }
1409 if (result == 0 && kwds != NULL) {
1410 if (PyArg_ValidateKeywordArguments(kwds))
1411 result = PyDict_Merge(self, kwds, 1);
1412 else
1413 result = -1;
1414 }
1415 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001416}
1417
1418static PyObject *
1419dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1420{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 if (dict_update_common(self, args, kwds, "update") != -1)
1422 Py_RETURN_NONE;
1423 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001424}
1425
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001426/* Update unconditionally replaces existing items.
1427 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001428 otherwise it leaves existing items unchanged.
1429
1430 PyDict_{Update,Merge} update/merge from a mapping object.
1431
Tim Petersf582b822001-12-11 18:51:08 +00001432 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001433 producing iterable objects of length 2.
1434*/
1435
Tim Petersf582b822001-12-11 18:51:08 +00001436int
Tim Peters1fc240e2001-10-26 05:06:50 +00001437PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1438{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 PyObject *it; /* iter(seq2) */
1440 Py_ssize_t i; /* index into seq2 of current element */
1441 PyObject *item; /* seq2[i] */
1442 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00001443
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 assert(d != NULL);
1445 assert(PyDict_Check(d));
1446 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00001447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 it = PyObject_GetIter(seq2);
1449 if (it == NULL)
1450 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 for (i = 0; ; ++i) {
1453 PyObject *key, *value;
1454 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 fast = NULL;
1457 item = PyIter_Next(it);
1458 if (item == NULL) {
1459 if (PyErr_Occurred())
1460 goto Fail;
1461 break;
1462 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 /* Convert item to sequence, and verify length 2. */
1465 fast = PySequence_Fast(item, "");
1466 if (fast == NULL) {
1467 if (PyErr_ExceptionMatches(PyExc_TypeError))
1468 PyErr_Format(PyExc_TypeError,
1469 "cannot convert dictionary update "
1470 "sequence element #%zd to a sequence",
1471 i);
1472 goto Fail;
1473 }
1474 n = PySequence_Fast_GET_SIZE(fast);
1475 if (n != 2) {
1476 PyErr_Format(PyExc_ValueError,
1477 "dictionary update sequence element #%zd "
1478 "has length %zd; 2 is required",
1479 i, n);
1480 goto Fail;
1481 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 /* Update/merge with this (key, value) pair. */
1484 key = PySequence_Fast_GET_ITEM(fast, 0);
1485 value = PySequence_Fast_GET_ITEM(fast, 1);
1486 if (override || PyDict_GetItem(d, key) == NULL) {
1487 int status = PyDict_SetItem(d, key, value);
1488 if (status < 0)
1489 goto Fail;
1490 }
1491 Py_DECREF(fast);
1492 Py_DECREF(item);
1493 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 i = 0;
1496 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00001497Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001498 Py_XDECREF(item);
1499 Py_XDECREF(fast);
1500 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001501Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001502 Py_DECREF(it);
1503 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001504}
1505
Tim Peters6d6c1a32001-08-02 04:15:00 +00001506int
1507PyDict_Update(PyObject *a, PyObject *b)
1508{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 return PyDict_Merge(a, b, 1);
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001510}
1511
1512int
1513PyDict_Merge(PyObject *a, PyObject *b, int override)
1514{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 register PyDictObject *mp, *other;
1516 register Py_ssize_t i;
1517 PyDictEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 /* We accept for the argument either a concrete dictionary object,
1520 * or an abstract "mapping" object. For the former, we can do
1521 * things quite efficiently. For the latter, we only require that
1522 * PyMapping_Keys() and PyObject_GetItem() be supported.
1523 */
1524 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1525 PyErr_BadInternalCall();
1526 return -1;
1527 }
1528 mp = (PyDictObject*)a;
1529 if (PyDict_Check(b)) {
1530 other = (PyDictObject*)b;
1531 if (other == mp || other->ma_used == 0)
1532 /* a.update(a) or a.update({}); nothing to do */
1533 return 0;
1534 if (mp->ma_used == 0)
1535 /* Since the target dict is empty, PyDict_GetItem()
1536 * always returns NULL. Setting override to 1
1537 * skips the unnecessary test.
1538 */
1539 override = 1;
1540 /* Do one big resize at the start, rather than
1541 * incrementally resizing as we insert new items. Expect
1542 * that there will be no (or few) overlapping keys.
1543 */
1544 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
1545 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
1546 return -1;
1547 }
1548 for (i = 0; i <= other->ma_mask; i++) {
1549 entry = &other->ma_table[i];
1550 if (entry->me_value != NULL &&
1551 (override ||
1552 PyDict_GetItem(a, entry->me_key) == NULL)) {
1553 Py_INCREF(entry->me_key);
1554 Py_INCREF(entry->me_value);
1555 if (insertdict(mp, entry->me_key,
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001556 entry->me_hash,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 entry->me_value) != 0)
1558 return -1;
1559 }
1560 }
1561 }
1562 else {
1563 /* Do it the generic, slower way */
1564 PyObject *keys = PyMapping_Keys(b);
1565 PyObject *iter;
1566 PyObject *key, *value;
1567 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 if (keys == NULL)
1570 /* Docstring says this is equivalent to E.keys() so
1571 * if E doesn't have a .keys() method we want
1572 * AttributeError to percolate up. Might as well
1573 * do the same for any other error.
1574 */
1575 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001577 iter = PyObject_GetIter(keys);
1578 Py_DECREF(keys);
1579 if (iter == NULL)
1580 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
1583 if (!override && PyDict_GetItem(a, key) != NULL) {
1584 Py_DECREF(key);
1585 continue;
1586 }
1587 value = PyObject_GetItem(b, key);
1588 if (value == NULL) {
1589 Py_DECREF(iter);
1590 Py_DECREF(key);
1591 return -1;
1592 }
1593 status = PyDict_SetItem(a, key, value);
1594 Py_DECREF(key);
1595 Py_DECREF(value);
1596 if (status < 0) {
1597 Py_DECREF(iter);
1598 return -1;
1599 }
1600 }
1601 Py_DECREF(iter);
1602 if (PyErr_Occurred())
1603 /* Iterator completed, via error */
1604 return -1;
1605 }
1606 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001607}
1608
1609static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001610dict_copy(register PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001611{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001613}
1614
1615PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001616PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001617{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 if (o == NULL || !PyDict_Check(o)) {
1621 PyErr_BadInternalCall();
1622 return NULL;
1623 }
1624 copy = PyDict_New();
1625 if (copy == NULL)
1626 return NULL;
1627 if (PyDict_Merge(copy, o, 1) == 0)
1628 return copy;
1629 Py_DECREF(copy);
1630 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001631}
1632
Martin v. Löwis18e16552006-02-15 17:27:45 +00001633Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001634PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001635{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 if (mp == NULL || !PyDict_Check(mp)) {
1637 PyErr_BadInternalCall();
1638 return -1;
1639 }
1640 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001641}
1642
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001643PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001644PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001645{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 if (mp == NULL || !PyDict_Check(mp)) {
1647 PyErr_BadInternalCall();
1648 return NULL;
1649 }
1650 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001651}
1652
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001653PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001654PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001655{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 if (mp == NULL || !PyDict_Check(mp)) {
1657 PyErr_BadInternalCall();
1658 return NULL;
1659 }
1660 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001661}
1662
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001663PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001664PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001665{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 if (mp == NULL || !PyDict_Check(mp)) {
1667 PyErr_BadInternalCall();
1668 return NULL;
1669 }
1670 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001671}
1672
Tim Peterse63415e2001-05-08 04:38:29 +00001673/* Return 1 if dicts equal, 0 if not, -1 if error.
1674 * Gets out as soon as any difference is detected.
1675 * Uses only Py_EQ comparison.
1676 */
1677static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001678dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00001679{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00001681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 if (a->ma_used != b->ma_used)
1683 /* can't be equal if # of entries differ */
1684 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 /* Same # of entries -- check all of 'em. Exit early on any diff. */
1687 for (i = 0; i <= a->ma_mask; i++) {
1688 PyObject *aval = a->ma_table[i].me_value;
1689 if (aval != NULL) {
1690 int cmp;
1691 PyObject *bval;
1692 PyObject *key = a->ma_table[i].me_key;
1693 /* temporarily bump aval's refcount to ensure it stays
1694 alive until we're done with it */
1695 Py_INCREF(aval);
1696 /* ditto for key */
1697 Py_INCREF(key);
1698 bval = PyDict_GetItemWithError((PyObject *)b, key);
1699 Py_DECREF(key);
1700 if (bval == NULL) {
1701 Py_DECREF(aval);
1702 if (PyErr_Occurred())
1703 return -1;
1704 return 0;
1705 }
1706 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1707 Py_DECREF(aval);
1708 if (cmp <= 0) /* error or not equal */
1709 return cmp;
1710 }
1711 }
1712 return 1;
Tim Peterse63415e2001-05-08 04:38:29 +00001713 }
1714
1715static PyObject *
1716dict_richcompare(PyObject *v, PyObject *w, int op)
1717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 int cmp;
1719 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00001720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1722 res = Py_NotImplemented;
1723 }
1724 else if (op == Py_EQ || op == Py_NE) {
1725 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
1726 if (cmp < 0)
1727 return NULL;
1728 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1729 }
1730 else
1731 res = Py_NotImplemented;
1732 Py_INCREF(res);
1733 return res;
Tim Peterse63415e2001-05-08 04:38:29 +00001734 }
1735
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001736static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001737dict_contains(register PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001738{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001739 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001743 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001744 hash = PyObject_Hash(key);
1745 if (hash == -1)
1746 return NULL;
1747 }
1748 ep = (mp->ma_lookup)(mp, key, hash);
1749 if (ep == NULL)
1750 return NULL;
1751 return PyBool_FromLong(ep->me_value != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001752}
1753
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001754static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001755dict_get(register PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001756{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001757 PyObject *key;
1758 PyObject *failobj = Py_None;
1759 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001760 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001761 PyDictEntry *ep;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
1764 return NULL;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001767 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 hash = PyObject_Hash(key);
1769 if (hash == -1)
1770 return NULL;
1771 }
1772 ep = (mp->ma_lookup)(mp, key, hash);
1773 if (ep == NULL)
1774 return NULL;
1775 val = ep->me_value;
1776 if (val == NULL)
1777 val = failobj;
1778 Py_INCREF(val);
1779 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001780}
1781
1782
1783static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001784dict_setdefault(register PyDictObject *mp, PyObject *args)
Guido van Rossum164452c2000-08-08 16:12:54 +00001785{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 PyObject *key;
1787 PyObject *failobj = Py_None;
1788 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001789 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 PyDictEntry *ep;
Guido van Rossum164452c2000-08-08 16:12:54 +00001791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001792 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
1793 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001794
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001795 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001796 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 hash = PyObject_Hash(key);
1798 if (hash == -1)
1799 return NULL;
1800 }
1801 ep = (mp->ma_lookup)(mp, key, hash);
1802 if (ep == NULL)
1803 return NULL;
1804 val = ep->me_value;
1805 if (val == NULL) {
1806 val = failobj;
1807 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1808 val = NULL;
1809 }
1810 Py_XINCREF(val);
1811 return val;
Guido van Rossum164452c2000-08-08 16:12:54 +00001812}
1813
1814
1815static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001816dict_clear(register PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001817{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 PyDict_Clear((PyObject *)mp);
1819 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001820}
1821
Guido van Rossumba6ab842000-12-12 22:02:18 +00001822static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001823dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00001824{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001825 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 PyDictEntry *ep;
1827 PyObject *old_value, *old_key;
1828 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1831 return NULL;
1832 if (mp->ma_used == 0) {
1833 if (deflt) {
1834 Py_INCREF(deflt);
1835 return deflt;
1836 }
Raymond Hettingerdd421542010-10-30 08:10:29 +00001837 set_key_error(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001838 return NULL;
1839 }
1840 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001841 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001842 hash = PyObject_Hash(key);
1843 if (hash == -1)
1844 return NULL;
1845 }
1846 ep = (mp->ma_lookup)(mp, key, hash);
1847 if (ep == NULL)
1848 return NULL;
1849 if (ep->me_value == NULL) {
1850 if (deflt) {
1851 Py_INCREF(deflt);
1852 return deflt;
1853 }
1854 set_key_error(key);
1855 return NULL;
1856 }
1857 old_key = ep->me_key;
1858 Py_INCREF(dummy);
1859 ep->me_key = dummy;
1860 old_value = ep->me_value;
1861 ep->me_value = NULL;
1862 mp->ma_used--;
1863 Py_DECREF(old_key);
1864 return old_value;
Guido van Rossume027d982002-04-12 15:11:59 +00001865}
1866
1867static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001868dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001869{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001870 Py_hash_t i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 PyDictEntry *ep;
1872 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 /* Allocate the result tuple before checking the size. Believe it
1875 * or not, this allocation could trigger a garbage collection which
1876 * could empty the dict, so if we checked the size first and that
1877 * happened, the result would be an infinite loop (searching for an
1878 * entry that no longer exists). Note that the usual popitem()
1879 * idiom is "while d: k, v = d.popitem()". so needing to throw the
1880 * tuple away if the dict *is* empty isn't a significant
1881 * inefficiency -- possible, but unlikely in practice.
1882 */
1883 res = PyTuple_New(2);
1884 if (res == NULL)
1885 return NULL;
1886 if (mp->ma_used == 0) {
1887 Py_DECREF(res);
1888 PyErr_SetString(PyExc_KeyError,
1889 "popitem(): dictionary is empty");
1890 return NULL;
1891 }
1892 /* Set ep to "the first" dict entry with a value. We abuse the hash
1893 * field of slot 0 to hold a search finger:
1894 * If slot 0 has a value, use slot 0.
1895 * Else slot 0 is being used to hold a search finger,
1896 * and we use its hash value as the first index to look.
1897 */
1898 ep = &mp->ma_table[0];
1899 if (ep->me_value == NULL) {
1900 i = ep->me_hash;
1901 /* The hash field may be a real hash value, or it may be a
1902 * legit search finger, or it may be a once-legit search
1903 * finger that's out of bounds now because it wrapped around
1904 * or the table shrunk -- simply make sure it's in bounds now.
1905 */
1906 if (i > mp->ma_mask || i < 1)
1907 i = 1; /* skip slot 0 */
1908 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1909 i++;
1910 if (i > mp->ma_mask)
1911 i = 1;
1912 }
1913 }
1914 PyTuple_SET_ITEM(res, 0, ep->me_key);
1915 PyTuple_SET_ITEM(res, 1, ep->me_value);
1916 Py_INCREF(dummy);
1917 ep->me_key = dummy;
1918 ep->me_value = NULL;
1919 mp->ma_used--;
1920 assert(mp->ma_table[0].me_value == NULL);
1921 mp->ma_table[0].me_hash = i + 1; /* next place to start */
1922 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001923}
1924
Jeremy Hylton8caad492000-06-23 14:18:11 +00001925static int
1926dict_traverse(PyObject *op, visitproc visit, void *arg)
1927{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928 Py_ssize_t i = 0;
1929 PyObject *pk;
1930 PyObject *pv;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 while (PyDict_Next(op, &i, &pk, &pv)) {
1933 Py_VISIT(pk);
1934 Py_VISIT(pv);
1935 }
1936 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001937}
1938
1939static int
1940dict_tp_clear(PyObject *op)
1941{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001942 PyDict_Clear(op);
1943 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001944}
1945
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001946static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001947
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001948static PyObject *
1949dict_sizeof(PyDictObject *mp)
1950{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 Py_ssize_t res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953 res = sizeof(PyDictObject);
1954 if (mp->ma_table != mp->ma_smalltable)
1955 res = res + (mp->ma_mask + 1) * sizeof(PyDictEntry);
1956 return PyLong_FromSsize_t(res);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001957}
1958
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001959PyDoc_STRVAR(contains__doc__,
1960"D.__contains__(k) -> True if D has a key k, else False");
1961
1962PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
1963
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001964PyDoc_STRVAR(sizeof__doc__,
1965"D.__sizeof__() -> size of D in memory, in bytes");
1966
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001967PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001968"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001969
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001970PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001971"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 +00001972
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001973PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001974"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001975If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00001976
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001977PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00001978"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000019792-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001980
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001981PyDoc_STRVAR(update__doc__,
Georg Brandlac0675c2011-12-18 19:30:55 +01001982"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n"
1983"If E present and has a .keys() method, does: for k in E: D[k] = E[k]\n\
1984If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001985In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00001986
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001987PyDoc_STRVAR(fromkeys__doc__,
1988"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
1989v defaults to None.");
1990
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001991PyDoc_STRVAR(clear__doc__,
1992"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001993
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001994PyDoc_STRVAR(copy__doc__,
1995"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00001996
Guido van Rossumb90c8482007-02-10 01:11:45 +00001997/* Forward */
1998static PyObject *dictkeys_new(PyObject *);
1999static PyObject *dictitems_new(PyObject *);
2000static PyObject *dictvalues_new(PyObject *);
2001
Guido van Rossum45c85d12007-07-27 16:31:40 +00002002PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002004PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002005 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002006PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002007 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00002008
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002009static PyMethodDef mapp_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002010 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
2011 contains__doc__},
2012 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
2013 getitem__doc__},
2014 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
2015 sizeof__doc__},
2016 {"get", (PyCFunction)dict_get, METH_VARARGS,
2017 get__doc__},
2018 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
2019 setdefault_doc__},
2020 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
2021 pop__doc__},
2022 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
2023 popitem__doc__},
2024 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
2025 keys__doc__},
2026 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
2027 items__doc__},
2028 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
2029 values__doc__},
2030 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
2031 update__doc__},
2032 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
2033 fromkeys__doc__},
2034 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
2035 clear__doc__},
2036 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
2037 copy__doc__},
2038 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002039};
2040
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002041/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002042int
2043PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002044{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002045 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002046 PyDictObject *mp = (PyDictObject *)op;
2047 PyDictEntry *ep;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002049 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002050 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002051 hash = PyObject_Hash(key);
2052 if (hash == -1)
2053 return -1;
2054 }
2055 ep = (mp->ma_lookup)(mp, key, hash);
2056 return ep == NULL ? -1 : (ep->me_value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002057}
2058
Thomas Wouterscf297e42007-02-23 15:07:44 +00002059/* Internal version of PyDict_Contains used when the hash value is already known */
2060int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002061_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00002062{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002063 PyDictObject *mp = (PyDictObject *)op;
2064 PyDictEntry *ep;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002066 ep = (mp->ma_lookup)(mp, key, hash);
2067 return ep == NULL ? -1 : (ep->me_value != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00002068}
2069
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002070/* Hack to implement "key in dict" */
2071static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002072 0, /* sq_length */
2073 0, /* sq_concat */
2074 0, /* sq_repeat */
2075 0, /* sq_item */
2076 0, /* sq_slice */
2077 0, /* sq_ass_item */
2078 0, /* sq_ass_slice */
2079 PyDict_Contains, /* sq_contains */
2080 0, /* sq_inplace_concat */
2081 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002082};
2083
Guido van Rossum09e563a2001-05-01 12:10:21 +00002084static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002085dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2086{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002087 PyObject *self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002089 assert(type != NULL && type->tp_alloc != NULL);
2090 self = type->tp_alloc(type, 0);
2091 if (self != NULL) {
2092 PyDictObject *d = (PyDictObject *)self;
2093 /* It's guaranteed that tp->alloc zeroed out the struct. */
2094 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
2095 INIT_NONZERO_DICT_SLOTS(d);
2096 d->ma_lookup = lookdict_unicode;
Ezio Melotti13925002011-03-16 11:05:33 +02002097 /* The object has been implicitly tracked by tp_alloc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002098 if (type == &PyDict_Type)
2099 _PyObject_GC_UNTRACK(d);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002100#ifdef SHOW_CONVERSION_COUNTS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002101 ++created;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002102#endif
Antoine Pitrou3a652b12009-03-23 18:52:06 +00002103#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002104 if (_PyObject_GC_IS_TRACKED(d))
2105 count_tracked++;
2106 else
2107 count_untracked++;
Antoine Pitrou3a652b12009-03-23 18:52:06 +00002108#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002109 }
2110 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002111}
2112
Tim Peters25786c02001-09-02 08:22:48 +00002113static int
2114dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2115{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002116 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002117}
2118
Tim Peters6d6c1a32001-08-02 04:15:00 +00002119static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002120dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002121{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002123}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002124
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002125PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002126"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002127"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002128" (key, value) pairs\n"
2129"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002130" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002131" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002132" d[k] = v\n"
2133"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2134" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002135
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002136PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002137 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2138 "dict",
2139 sizeof(PyDictObject),
2140 0,
2141 (destructor)dict_dealloc, /* tp_dealloc */
2142 0, /* tp_print */
2143 0, /* tp_getattr */
2144 0, /* tp_setattr */
2145 0, /* tp_reserved */
2146 (reprfunc)dict_repr, /* tp_repr */
2147 0, /* tp_as_number */
2148 &dict_as_sequence, /* tp_as_sequence */
2149 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002150 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002151 0, /* tp_call */
2152 0, /* tp_str */
2153 PyObject_GenericGetAttr, /* tp_getattro */
2154 0, /* tp_setattro */
2155 0, /* tp_as_buffer */
2156 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2157 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
2158 dictionary_doc, /* tp_doc */
2159 dict_traverse, /* tp_traverse */
2160 dict_tp_clear, /* tp_clear */
2161 dict_richcompare, /* tp_richcompare */
2162 0, /* tp_weaklistoffset */
2163 (getiterfunc)dict_iter, /* tp_iter */
2164 0, /* tp_iternext */
2165 mapp_methods, /* tp_methods */
2166 0, /* tp_members */
2167 0, /* tp_getset */
2168 0, /* tp_base */
2169 0, /* tp_dict */
2170 0, /* tp_descr_get */
2171 0, /* tp_descr_set */
2172 0, /* tp_dictoffset */
2173 dict_init, /* tp_init */
2174 PyType_GenericAlloc, /* tp_alloc */
2175 dict_new, /* tp_new */
2176 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002177};
2178
Guido van Rossum3cca2451997-05-16 14:23:33 +00002179/* For backward compatibility with old dictionary interface */
2180
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002181PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002182PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002183{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002184 PyObject *kv, *rv;
2185 kv = PyUnicode_FromString(key);
2186 if (kv == NULL)
2187 return NULL;
2188 rv = PyDict_GetItem(v, kv);
2189 Py_DECREF(kv);
2190 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002191}
2192
2193int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002194PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002196 PyObject *kv;
2197 int err;
2198 kv = PyUnicode_FromString(key);
2199 if (kv == NULL)
2200 return -1;
2201 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
2202 err = PyDict_SetItem(v, kv, item);
2203 Py_DECREF(kv);
2204 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002205}
2206
2207int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002208PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002210 PyObject *kv;
2211 int err;
2212 kv = PyUnicode_FromString(key);
2213 if (kv == NULL)
2214 return -1;
2215 err = PyDict_DelItem(v, kv);
2216 Py_DECREF(kv);
2217 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002218}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002219
Raymond Hettinger019a1482004-03-18 02:41:19 +00002220/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002221
2222typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002223 PyObject_HEAD
2224 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
2225 Py_ssize_t di_used;
2226 Py_ssize_t di_pos;
2227 PyObject* di_result; /* reusable result tuple for iteritems */
2228 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002229} dictiterobject;
2230
2231static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002232dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002233{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002234 dictiterobject *di;
2235 di = PyObject_GC_New(dictiterobject, itertype);
2236 if (di == NULL)
2237 return NULL;
2238 Py_INCREF(dict);
2239 di->di_dict = dict;
2240 di->di_used = dict->ma_used;
2241 di->di_pos = 0;
2242 di->len = dict->ma_used;
2243 if (itertype == &PyDictIterItem_Type) {
2244 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2245 if (di->di_result == NULL) {
2246 Py_DECREF(di);
2247 return NULL;
2248 }
2249 }
2250 else
2251 di->di_result = NULL;
2252 _PyObject_GC_TRACK(di);
2253 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002254}
2255
2256static void
2257dictiter_dealloc(dictiterobject *di)
2258{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002259 Py_XDECREF(di->di_dict);
2260 Py_XDECREF(di->di_result);
2261 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002262}
2263
2264static int
2265dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
2266{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002267 Py_VISIT(di->di_dict);
2268 Py_VISIT(di->di_result);
2269 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002270}
2271
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002272static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002273dictiter_len(dictiterobject *di)
2274{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 Py_ssize_t len = 0;
2276 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
2277 len = di->len;
2278 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002279}
2280
Guido van Rossumb90c8482007-02-10 01:11:45 +00002281PyDoc_STRVAR(length_hint_doc,
2282 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002283
2284static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002285 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
2286 length_hint_doc},
2287 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002288};
2289
Raymond Hettinger019a1482004-03-18 02:41:19 +00002290static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002291{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292 PyObject *key;
2293 register Py_ssize_t i, mask;
2294 register PyDictEntry *ep;
2295 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002297 if (d == NULL)
2298 return NULL;
2299 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002301 if (di->di_used != d->ma_used) {
2302 PyErr_SetString(PyExc_RuntimeError,
2303 "dictionary changed size during iteration");
2304 di->di_used = -1; /* Make this state sticky */
2305 return NULL;
2306 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002308 i = di->di_pos;
2309 if (i < 0)
2310 goto fail;
2311 ep = d->ma_table;
2312 mask = d->ma_mask;
2313 while (i <= mask && ep[i].me_value == NULL)
2314 i++;
2315 di->di_pos = i+1;
2316 if (i > mask)
2317 goto fail;
2318 di->len--;
2319 key = ep[i].me_key;
2320 Py_INCREF(key);
2321 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002322
2323fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002324 Py_DECREF(d);
2325 di->di_dict = NULL;
2326 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002327}
2328
Raymond Hettinger019a1482004-03-18 02:41:19 +00002329PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002330 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2331 "dict_keyiterator", /* tp_name */
2332 sizeof(dictiterobject), /* tp_basicsize */
2333 0, /* tp_itemsize */
2334 /* methods */
2335 (destructor)dictiter_dealloc, /* tp_dealloc */
2336 0, /* tp_print */
2337 0, /* tp_getattr */
2338 0, /* tp_setattr */
2339 0, /* tp_reserved */
2340 0, /* tp_repr */
2341 0, /* tp_as_number */
2342 0, /* tp_as_sequence */
2343 0, /* tp_as_mapping */
2344 0, /* tp_hash */
2345 0, /* tp_call */
2346 0, /* tp_str */
2347 PyObject_GenericGetAttr, /* tp_getattro */
2348 0, /* tp_setattro */
2349 0, /* tp_as_buffer */
2350 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2351 0, /* tp_doc */
2352 (traverseproc)dictiter_traverse, /* tp_traverse */
2353 0, /* tp_clear */
2354 0, /* tp_richcompare */
2355 0, /* tp_weaklistoffset */
2356 PyObject_SelfIter, /* tp_iter */
2357 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
2358 dictiter_methods, /* tp_methods */
2359 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002360};
2361
2362static PyObject *dictiter_iternextvalue(dictiterobject *di)
2363{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 PyObject *value;
2365 register Py_ssize_t i, mask;
2366 register PyDictEntry *ep;
2367 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 if (d == NULL)
2370 return NULL;
2371 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373 if (di->di_used != d->ma_used) {
2374 PyErr_SetString(PyExc_RuntimeError,
2375 "dictionary changed size during iteration");
2376 di->di_used = -1; /* Make this state sticky */
2377 return NULL;
2378 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380 i = di->di_pos;
2381 mask = d->ma_mask;
2382 if (i < 0 || i > mask)
2383 goto fail;
2384 ep = d->ma_table;
2385 while ((value=ep[i].me_value) == NULL) {
2386 i++;
2387 if (i > mask)
2388 goto fail;
2389 }
2390 di->di_pos = i+1;
2391 di->len--;
2392 Py_INCREF(value);
2393 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002394
2395fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002396 Py_DECREF(d);
2397 di->di_dict = NULL;
2398 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002399}
2400
2401PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002402 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2403 "dict_valueiterator", /* tp_name */
2404 sizeof(dictiterobject), /* tp_basicsize */
2405 0, /* tp_itemsize */
2406 /* methods */
2407 (destructor)dictiter_dealloc, /* tp_dealloc */
2408 0, /* tp_print */
2409 0, /* tp_getattr */
2410 0, /* tp_setattr */
2411 0, /* tp_reserved */
2412 0, /* tp_repr */
2413 0, /* tp_as_number */
2414 0, /* tp_as_sequence */
2415 0, /* tp_as_mapping */
2416 0, /* tp_hash */
2417 0, /* tp_call */
2418 0, /* tp_str */
2419 PyObject_GenericGetAttr, /* tp_getattro */
2420 0, /* tp_setattro */
2421 0, /* tp_as_buffer */
2422 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2423 0, /* tp_doc */
2424 (traverseproc)dictiter_traverse, /* tp_traverse */
2425 0, /* tp_clear */
2426 0, /* tp_richcompare */
2427 0, /* tp_weaklistoffset */
2428 PyObject_SelfIter, /* tp_iter */
2429 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
2430 dictiter_methods, /* tp_methods */
2431 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002432};
2433
2434static PyObject *dictiter_iternextitem(dictiterobject *di)
2435{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436 PyObject *key, *value, *result = di->di_result;
2437 register Py_ssize_t i, mask;
2438 register PyDictEntry *ep;
2439 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002441 if (d == NULL)
2442 return NULL;
2443 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002445 if (di->di_used != d->ma_used) {
2446 PyErr_SetString(PyExc_RuntimeError,
2447 "dictionary changed size during iteration");
2448 di->di_used = -1; /* Make this state sticky */
2449 return NULL;
2450 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002452 i = di->di_pos;
2453 if (i < 0)
2454 goto fail;
2455 ep = d->ma_table;
2456 mask = d->ma_mask;
2457 while (i <= mask && ep[i].me_value == NULL)
2458 i++;
2459 di->di_pos = i+1;
2460 if (i > mask)
2461 goto fail;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002463 if (result->ob_refcnt == 1) {
2464 Py_INCREF(result);
2465 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2466 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2467 } else {
2468 result = PyTuple_New(2);
2469 if (result == NULL)
2470 return NULL;
2471 }
2472 di->len--;
2473 key = ep[i].me_key;
2474 value = ep[i].me_value;
2475 Py_INCREF(key);
2476 Py_INCREF(value);
2477 PyTuple_SET_ITEM(result, 0, key);
2478 PyTuple_SET_ITEM(result, 1, value);
2479 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002480
2481fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 Py_DECREF(d);
2483 di->di_dict = NULL;
2484 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002485}
2486
2487PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002488 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2489 "dict_itemiterator", /* tp_name */
2490 sizeof(dictiterobject), /* tp_basicsize */
2491 0, /* tp_itemsize */
2492 /* methods */
2493 (destructor)dictiter_dealloc, /* tp_dealloc */
2494 0, /* tp_print */
2495 0, /* tp_getattr */
2496 0, /* tp_setattr */
2497 0, /* tp_reserved */
2498 0, /* tp_repr */
2499 0, /* tp_as_number */
2500 0, /* tp_as_sequence */
2501 0, /* tp_as_mapping */
2502 0, /* tp_hash */
2503 0, /* tp_call */
2504 0, /* tp_str */
2505 PyObject_GenericGetAttr, /* tp_getattro */
2506 0, /* tp_setattro */
2507 0, /* tp_as_buffer */
2508 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2509 0, /* tp_doc */
2510 (traverseproc)dictiter_traverse, /* tp_traverse */
2511 0, /* tp_clear */
2512 0, /* tp_richcompare */
2513 0, /* tp_weaklistoffset */
2514 PyObject_SelfIter, /* tp_iter */
2515 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
2516 dictiter_methods, /* tp_methods */
2517 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002518};
Guido van Rossumb90c8482007-02-10 01:11:45 +00002519
2520
Guido van Rossum3ac67412007-02-10 18:55:06 +00002521/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002522/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002523/***********************************************/
2524
Guido van Rossumb90c8482007-02-10 01:11:45 +00002525/* The instance lay-out is the same for all three; but the type differs. */
2526
2527typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002528 PyObject_HEAD
2529 PyDictObject *dv_dict;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002530} dictviewobject;
2531
2532
2533static void
Guido van Rossum3ac67412007-02-10 18:55:06 +00002534dictview_dealloc(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002535{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002536 Py_XDECREF(dv->dv_dict);
2537 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002538}
2539
2540static int
2541dictview_traverse(dictviewobject *dv, visitproc visit, void *arg)
2542{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002543 Py_VISIT(dv->dv_dict);
2544 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002545}
2546
Guido van Rossum83825ac2007-02-10 04:54:19 +00002547static Py_ssize_t
Guido van Rossum3ac67412007-02-10 18:55:06 +00002548dictview_len(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002549{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002550 Py_ssize_t len = 0;
2551 if (dv->dv_dict != NULL)
2552 len = dv->dv_dict->ma_used;
2553 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002554}
2555
2556static PyObject *
2557dictview_new(PyObject *dict, PyTypeObject *type)
2558{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002559 dictviewobject *dv;
2560 if (dict == NULL) {
2561 PyErr_BadInternalCall();
2562 return NULL;
2563 }
2564 if (!PyDict_Check(dict)) {
2565 /* XXX Get rid of this restriction later */
2566 PyErr_Format(PyExc_TypeError,
2567 "%s() requires a dict argument, not '%s'",
2568 type->tp_name, dict->ob_type->tp_name);
2569 return NULL;
2570 }
2571 dv = PyObject_GC_New(dictviewobject, type);
2572 if (dv == NULL)
2573 return NULL;
2574 Py_INCREF(dict);
2575 dv->dv_dict = (PyDictObject *)dict;
2576 _PyObject_GC_TRACK(dv);
2577 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002578}
2579
Neal Norwitze36f2ba2007-02-26 23:12:28 +00002580/* TODO(guido): The views objects are not complete:
2581
2582 * support more set operations
2583 * support arbitrary mappings?
2584 - either these should be static or exported in dictobject.h
2585 - if public then they should probably be in builtins
2586*/
2587
Guido van Rossumaac530c2007-08-24 22:33:45 +00002588/* Return 1 if self is a subset of other, iterating over self;
2589 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002590static int
2591all_contained_in(PyObject *self, PyObject *other)
2592{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002593 PyObject *iter = PyObject_GetIter(self);
2594 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00002595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002596 if (iter == NULL)
2597 return -1;
2598 for (;;) {
2599 PyObject *next = PyIter_Next(iter);
2600 if (next == NULL) {
2601 if (PyErr_Occurred())
2602 ok = -1;
2603 break;
2604 }
2605 ok = PySequence_Contains(other, next);
2606 Py_DECREF(next);
2607 if (ok <= 0)
2608 break;
2609 }
2610 Py_DECREF(iter);
2611 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00002612}
2613
2614static PyObject *
2615dictview_richcompare(PyObject *self, PyObject *other, int op)
2616{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002617 Py_ssize_t len_self, len_other;
2618 int ok;
2619 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002621 assert(self != NULL);
2622 assert(PyDictViewSet_Check(self));
2623 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00002624
Brian Curtindfc80e32011-08-10 20:28:54 -05002625 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
2626 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002628 len_self = PyObject_Size(self);
2629 if (len_self < 0)
2630 return NULL;
2631 len_other = PyObject_Size(other);
2632 if (len_other < 0)
2633 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002635 ok = 0;
2636 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00002637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002638 case Py_NE:
2639 case Py_EQ:
2640 if (len_self == len_other)
2641 ok = all_contained_in(self, other);
2642 if (op == Py_NE && ok >= 0)
2643 ok = !ok;
2644 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002646 case Py_LT:
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_LE:
2652 if (len_self <= len_other)
2653 ok = all_contained_in(self, other);
2654 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002656 case Py_GT:
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 case Py_GE:
2662 if (len_self >= len_other)
2663 ok = all_contained_in(other, self);
2664 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002666 }
2667 if (ok < 0)
2668 return NULL;
2669 result = ok ? Py_True : Py_False;
2670 Py_INCREF(result);
2671 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00002672}
2673
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00002674static PyObject *
2675dictview_repr(dictviewobject *dv)
2676{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002677 PyObject *seq;
2678 PyObject *result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00002679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002680 seq = PySequence_List((PyObject *)dv);
2681 if (seq == NULL)
2682 return NULL;
2683
2684 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
2685 Py_DECREF(seq);
2686 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00002687}
2688
Guido van Rossum3ac67412007-02-10 18:55:06 +00002689/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002690
2691static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002692dictkeys_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002693{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002694 if (dv->dv_dict == NULL) {
2695 Py_RETURN_NONE;
2696 }
2697 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00002698}
2699
2700static int
2701dictkeys_contains(dictviewobject *dv, PyObject *obj)
2702{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002703 if (dv->dv_dict == NULL)
2704 return 0;
2705 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002706}
2707
Guido van Rossum83825ac2007-02-10 04:54:19 +00002708static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002709 (lenfunc)dictview_len, /* sq_length */
2710 0, /* sq_concat */
2711 0, /* sq_repeat */
2712 0, /* sq_item */
2713 0, /* sq_slice */
2714 0, /* sq_ass_item */
2715 0, /* sq_ass_slice */
2716 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002717};
2718
Guido van Rossum523259b2007-08-24 23:41:22 +00002719static PyObject*
2720dictviews_sub(PyObject* self, PyObject *other)
2721{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002722 PyObject *result = PySet_New(self);
2723 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002724 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002725
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002726 if (result == NULL)
2727 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00002728
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002729 tmp = _PyObject_CallMethodId(result, &PyId_difference_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002730 if (tmp == NULL) {
2731 Py_DECREF(result);
2732 return NULL;
2733 }
Guido van Rossum523259b2007-08-24 23:41:22 +00002734
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002735 Py_DECREF(tmp);
2736 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00002737}
2738
2739static PyObject*
2740dictviews_and(PyObject* self, PyObject *other)
2741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002742 PyObject *result = PySet_New(self);
2743 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002744 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002746 if (result == NULL)
2747 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00002748
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002749 tmp = _PyObject_CallMethodId(result, &PyId_intersection_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002750 if (tmp == NULL) {
2751 Py_DECREF(result);
2752 return NULL;
2753 }
Guido van Rossum523259b2007-08-24 23:41:22 +00002754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002755 Py_DECREF(tmp);
2756 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00002757}
2758
2759static PyObject*
2760dictviews_or(PyObject* self, PyObject *other)
2761{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002762 PyObject *result = PySet_New(self);
2763 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002764 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02002765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002766 if (result == NULL)
2767 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00002768
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02002769 tmp = _PyObject_CallMethodId(result, &PyId_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002770 if (tmp == NULL) {
2771 Py_DECREF(result);
2772 return NULL;
2773 }
Guido van Rossum523259b2007-08-24 23:41:22 +00002774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002775 Py_DECREF(tmp);
2776 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00002777}
2778
2779static PyObject*
2780dictviews_xor(PyObject* self, PyObject *other)
2781{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002782 PyObject *result = PySet_New(self);
2783 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002784 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002786 if (result == NULL)
2787 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00002788
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002789 tmp = _PyObject_CallMethodId(result, &PyId_symmetric_difference_update, "O",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002790 other);
2791 if (tmp == NULL) {
2792 Py_DECREF(result);
2793 return NULL;
2794 }
Guido van Rossum523259b2007-08-24 23:41:22 +00002795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002796 Py_DECREF(tmp);
2797 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00002798}
2799
2800static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002801 0, /*nb_add*/
2802 (binaryfunc)dictviews_sub, /*nb_subtract*/
2803 0, /*nb_multiply*/
2804 0, /*nb_remainder*/
2805 0, /*nb_divmod*/
2806 0, /*nb_power*/
2807 0, /*nb_negative*/
2808 0, /*nb_positive*/
2809 0, /*nb_absolute*/
2810 0, /*nb_bool*/
2811 0, /*nb_invert*/
2812 0, /*nb_lshift*/
2813 0, /*nb_rshift*/
2814 (binaryfunc)dictviews_and, /*nb_and*/
2815 (binaryfunc)dictviews_xor, /*nb_xor*/
2816 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00002817};
2818
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00002819static PyObject*
2820dictviews_isdisjoint(PyObject *self, PyObject *other)
2821{
2822 PyObject *it;
2823 PyObject *item = NULL;
2824
2825 if (self == other) {
2826 if (dictview_len((dictviewobject *)self) == 0)
2827 Py_RETURN_TRUE;
2828 else
2829 Py_RETURN_FALSE;
2830 }
2831
2832 /* Iterate over the shorter object (only if other is a set,
2833 * because PySequence_Contains may be expensive otherwise): */
2834 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
2835 Py_ssize_t len_self = dictview_len((dictviewobject *)self);
2836 Py_ssize_t len_other = PyObject_Size(other);
2837 if (len_other == -1)
2838 return NULL;
2839
2840 if ((len_other > len_self)) {
2841 PyObject *tmp = other;
2842 other = self;
2843 self = tmp;
2844 }
2845 }
2846
2847 it = PyObject_GetIter(other);
2848 if (it == NULL)
2849 return NULL;
2850
2851 while ((item = PyIter_Next(it)) != NULL) {
2852 int contains = PySequence_Contains(self, item);
2853 Py_DECREF(item);
2854 if (contains == -1) {
2855 Py_DECREF(it);
2856 return NULL;
2857 }
2858
2859 if (contains) {
2860 Py_DECREF(it);
2861 Py_RETURN_FALSE;
2862 }
2863 }
2864 Py_DECREF(it);
2865 if (PyErr_Occurred())
2866 return NULL; /* PyIter_Next raised an exception. */
2867 Py_RETURN_TRUE;
2868}
2869
2870PyDoc_STRVAR(isdisjoint_doc,
2871"Return True if the view and the given iterable have a null intersection.");
2872
Guido van Rossumb90c8482007-02-10 01:11:45 +00002873static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00002874 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
2875 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002877};
2878
2879PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2881 "dict_keys", /* tp_name */
2882 sizeof(dictviewobject), /* tp_basicsize */
2883 0, /* tp_itemsize */
2884 /* methods */
2885 (destructor)dictview_dealloc, /* tp_dealloc */
2886 0, /* tp_print */
2887 0, /* tp_getattr */
2888 0, /* tp_setattr */
2889 0, /* tp_reserved */
2890 (reprfunc)dictview_repr, /* tp_repr */
2891 &dictviews_as_number, /* tp_as_number */
2892 &dictkeys_as_sequence, /* tp_as_sequence */
2893 0, /* tp_as_mapping */
2894 0, /* tp_hash */
2895 0, /* tp_call */
2896 0, /* tp_str */
2897 PyObject_GenericGetAttr, /* tp_getattro */
2898 0, /* tp_setattro */
2899 0, /* tp_as_buffer */
2900 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2901 0, /* tp_doc */
2902 (traverseproc)dictview_traverse, /* tp_traverse */
2903 0, /* tp_clear */
2904 dictview_richcompare, /* tp_richcompare */
2905 0, /* tp_weaklistoffset */
2906 (getiterfunc)dictkeys_iter, /* tp_iter */
2907 0, /* tp_iternext */
2908 dictkeys_methods, /* tp_methods */
2909 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00002910};
2911
2912static PyObject *
2913dictkeys_new(PyObject *dict)
2914{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002915 return dictview_new(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002916}
2917
Guido van Rossum3ac67412007-02-10 18:55:06 +00002918/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002919
2920static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002921dictitems_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002922{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002923 if (dv->dv_dict == NULL) {
2924 Py_RETURN_NONE;
2925 }
2926 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00002927}
2928
2929static int
2930dictitems_contains(dictviewobject *dv, PyObject *obj)
2931{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002932 PyObject *key, *value, *found;
2933 if (dv->dv_dict == NULL)
2934 return 0;
2935 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
2936 return 0;
2937 key = PyTuple_GET_ITEM(obj, 0);
2938 value = PyTuple_GET_ITEM(obj, 1);
2939 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
2940 if (found == NULL) {
2941 if (PyErr_Occurred())
2942 return -1;
2943 return 0;
2944 }
2945 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002946}
2947
Guido van Rossum83825ac2007-02-10 04:54:19 +00002948static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002949 (lenfunc)dictview_len, /* sq_length */
2950 0, /* sq_concat */
2951 0, /* sq_repeat */
2952 0, /* sq_item */
2953 0, /* sq_slice */
2954 0, /* sq_ass_item */
2955 0, /* sq_ass_slice */
2956 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002957};
2958
Guido van Rossumb90c8482007-02-10 01:11:45 +00002959static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00002960 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
2961 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002962 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002963};
2964
2965PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002966 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2967 "dict_items", /* tp_name */
2968 sizeof(dictviewobject), /* tp_basicsize */
2969 0, /* tp_itemsize */
2970 /* methods */
2971 (destructor)dictview_dealloc, /* tp_dealloc */
2972 0, /* tp_print */
2973 0, /* tp_getattr */
2974 0, /* tp_setattr */
2975 0, /* tp_reserved */
2976 (reprfunc)dictview_repr, /* tp_repr */
2977 &dictviews_as_number, /* tp_as_number */
2978 &dictitems_as_sequence, /* tp_as_sequence */
2979 0, /* tp_as_mapping */
2980 0, /* tp_hash */
2981 0, /* tp_call */
2982 0, /* tp_str */
2983 PyObject_GenericGetAttr, /* tp_getattro */
2984 0, /* tp_setattro */
2985 0, /* tp_as_buffer */
2986 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2987 0, /* tp_doc */
2988 (traverseproc)dictview_traverse, /* tp_traverse */
2989 0, /* tp_clear */
2990 dictview_richcompare, /* tp_richcompare */
2991 0, /* tp_weaklistoffset */
2992 (getiterfunc)dictitems_iter, /* tp_iter */
2993 0, /* tp_iternext */
2994 dictitems_methods, /* tp_methods */
2995 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00002996};
2997
2998static PyObject *
2999dictitems_new(PyObject *dict)
3000{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003001 return dictview_new(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003002}
3003
Guido van Rossum3ac67412007-02-10 18:55:06 +00003004/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003005
3006static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003007dictvalues_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003008{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003009 if (dv->dv_dict == NULL) {
3010 Py_RETURN_NONE;
3011 }
3012 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003013}
3014
Guido van Rossum83825ac2007-02-10 04:54:19 +00003015static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003016 (lenfunc)dictview_len, /* sq_length */
3017 0, /* sq_concat */
3018 0, /* sq_repeat */
3019 0, /* sq_item */
3020 0, /* sq_slice */
3021 0, /* sq_ass_item */
3022 0, /* sq_ass_slice */
3023 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003024};
3025
Guido van Rossumb90c8482007-02-10 01:11:45 +00003026static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003027 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003028};
3029
3030PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003031 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3032 "dict_values", /* tp_name */
3033 sizeof(dictviewobject), /* tp_basicsize */
3034 0, /* tp_itemsize */
3035 /* methods */
3036 (destructor)dictview_dealloc, /* tp_dealloc */
3037 0, /* tp_print */
3038 0, /* tp_getattr */
3039 0, /* tp_setattr */
3040 0, /* tp_reserved */
3041 (reprfunc)dictview_repr, /* tp_repr */
3042 0, /* tp_as_number */
3043 &dictvalues_as_sequence, /* tp_as_sequence */
3044 0, /* tp_as_mapping */
3045 0, /* tp_hash */
3046 0, /* tp_call */
3047 0, /* tp_str */
3048 PyObject_GenericGetAttr, /* tp_getattro */
3049 0, /* tp_setattro */
3050 0, /* tp_as_buffer */
3051 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3052 0, /* tp_doc */
3053 (traverseproc)dictview_traverse, /* tp_traverse */
3054 0, /* tp_clear */
3055 0, /* tp_richcompare */
3056 0, /* tp_weaklistoffset */
3057 (getiterfunc)dictvalues_iter, /* tp_iter */
3058 0, /* tp_iternext */
3059 dictvalues_methods, /* tp_methods */
3060 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003061};
3062
3063static PyObject *
3064dictvalues_new(PyObject *dict)
3065{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003066 return dictview_new(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003067}