blob: f8f072d4009e9b8f3130c47599434f4b43a58735 [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;
1145 static PyObject *missing_str = NULL;
1146 missing = _PyObject_LookupSpecial((PyObject *)mp,
1147 "__missing__",
1148 &missing_str);
1149 if (missing != NULL) {
1150 res = PyObject_CallFunctionObjArgs(missing,
1151 key, NULL);
1152 Py_DECREF(missing);
1153 return res;
1154 }
1155 else if (PyErr_Occurred())
1156 return NULL;
1157 }
1158 set_key_error(key);
1159 return NULL;
1160 }
1161 else
1162 Py_INCREF(v);
1163 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001164}
1165
1166static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001167dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001168{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 if (w == NULL)
1170 return PyDict_DelItem((PyObject *)mp, v);
1171 else
1172 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001173}
1174
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001175static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 (lenfunc)dict_length, /*mp_length*/
1177 (binaryfunc)dict_subscript, /*mp_subscript*/
1178 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001179};
1180
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001181static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001182dict_keys(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001183{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 register PyObject *v;
1185 register Py_ssize_t i, j;
1186 PyDictEntry *ep;
1187 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001188
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001189 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 n = mp->ma_used;
1191 v = PyList_New(n);
1192 if (v == NULL)
1193 return NULL;
1194 if (n != mp->ma_used) {
1195 /* Durnit. The allocations caused the dict to resize.
1196 * Just start over, this shouldn't normally happen.
1197 */
1198 Py_DECREF(v);
1199 goto again;
1200 }
1201 ep = mp->ma_table;
1202 mask = mp->ma_mask;
1203 for (i = 0, j = 0; i <= mask; i++) {
1204 if (ep[i].me_value != NULL) {
1205 PyObject *key = ep[i].me_key;
1206 Py_INCREF(key);
1207 PyList_SET_ITEM(v, j, key);
1208 j++;
1209 }
1210 }
1211 assert(j == n);
1212 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001213}
1214
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001215static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001216dict_values(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001217{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 register PyObject *v;
1219 register Py_ssize_t i, j;
1220 PyDictEntry *ep;
1221 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001222
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001223 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 n = mp->ma_used;
1225 v = PyList_New(n);
1226 if (v == NULL)
1227 return NULL;
1228 if (n != mp->ma_used) {
1229 /* Durnit. The allocations caused the dict to resize.
1230 * Just start over, this shouldn't normally happen.
1231 */
1232 Py_DECREF(v);
1233 goto again;
1234 }
1235 ep = mp->ma_table;
1236 mask = mp->ma_mask;
1237 for (i = 0, j = 0; i <= mask; i++) {
1238 if (ep[i].me_value != NULL) {
1239 PyObject *value = ep[i].me_value;
1240 Py_INCREF(value);
1241 PyList_SET_ITEM(v, j, value);
1242 j++;
1243 }
1244 }
1245 assert(j == n);
1246 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001247}
1248
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001249static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001250dict_items(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001251{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 register PyObject *v;
1253 register Py_ssize_t i, j, n;
1254 Py_ssize_t mask;
1255 PyObject *item, *key, *value;
1256 PyDictEntry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 /* Preallocate the list of tuples, to avoid allocations during
1259 * the loop over the items, which could trigger GC, which
1260 * could resize the dict. :-(
1261 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001262 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 n = mp->ma_used;
1264 v = PyList_New(n);
1265 if (v == NULL)
1266 return NULL;
1267 for (i = 0; i < n; i++) {
1268 item = PyTuple_New(2);
1269 if (item == NULL) {
1270 Py_DECREF(v);
1271 return NULL;
1272 }
1273 PyList_SET_ITEM(v, i, item);
1274 }
1275 if (n != mp->ma_used) {
1276 /* Durnit. The allocations caused the dict to resize.
1277 * Just start over, this shouldn't normally happen.
1278 */
1279 Py_DECREF(v);
1280 goto again;
1281 }
1282 /* Nothing we do below makes any function calls. */
1283 ep = mp->ma_table;
1284 mask = mp->ma_mask;
1285 for (i = 0, j = 0; i <= mask; i++) {
1286 if ((value=ep[i].me_value) != NULL) {
1287 key = ep[i].me_key;
1288 item = PyList_GET_ITEM(v, j);
1289 Py_INCREF(key);
1290 PyTuple_SET_ITEM(item, 0, key);
1291 Py_INCREF(value);
1292 PyTuple_SET_ITEM(item, 1, value);
1293 j++;
1294 }
1295 }
1296 assert(j == n);
1297 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001298}
1299
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001300static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001301dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001302{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 PyObject *seq;
1304 PyObject *value = Py_None;
1305 PyObject *it; /* iter(seq) */
1306 PyObject *key;
1307 PyObject *d;
1308 int status;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
1311 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 d = PyObject_CallObject(cls, NULL);
1314 if (d == NULL)
1315 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) {
1318 PyDictObject *mp = (PyDictObject *)d;
1319 PyObject *oldvalue;
1320 Py_ssize_t pos = 0;
1321 PyObject *key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001322 Py_hash_t hash;
Guido van Rossum58da9312007-11-10 23:39:45 +00001323
Petri Lehtinena94200e2011-10-24 21:12:58 +03001324 if (dictresize(mp, Py_SIZE(seq))) {
1325 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001327 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
1330 Py_INCREF(key);
1331 Py_INCREF(value);
Petri Lehtinena94200e2011-10-24 21:12:58 +03001332 if (insertdict(mp, key, hash, value)) {
1333 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001335 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 }
1337 return d;
1338 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
1341 PyDictObject *mp = (PyDictObject *)d;
1342 Py_ssize_t pos = 0;
1343 PyObject *key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001344 Py_hash_t hash;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001345
Petri Lehtinena94200e2011-10-24 21:12:58 +03001346 if (dictresize(mp, PySet_GET_SIZE(seq))) {
1347 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001349 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
1352 Py_INCREF(key);
1353 Py_INCREF(value);
Petri Lehtinena94200e2011-10-24 21:12:58 +03001354 if (insertdict(mp, key, hash, value)) {
1355 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001357 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001358 }
1359 return d;
1360 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 it = PyObject_GetIter(seq);
1363 if (it == NULL){
1364 Py_DECREF(d);
1365 return NULL;
1366 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 if (PyDict_CheckExact(d)) {
1369 while ((key = PyIter_Next(it)) != NULL) {
1370 status = PyDict_SetItem(d, key, value);
1371 Py_DECREF(key);
1372 if (status < 0)
1373 goto Fail;
1374 }
1375 } else {
1376 while ((key = PyIter_Next(it)) != NULL) {
1377 status = PyObject_SetItem(d, key, value);
1378 Py_DECREF(key);
1379 if (status < 0)
1380 goto Fail;
1381 }
1382 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 if (PyErr_Occurred())
1385 goto Fail;
1386 Py_DECREF(it);
1387 return d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001388
1389Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 Py_DECREF(it);
1391 Py_DECREF(d);
1392 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001393}
1394
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001395static int
1396dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001397{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 PyObject *arg = NULL;
1399 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1402 result = -1;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001405 _Py_IDENTIFIER(keys);
1406 if (_PyObject_HasAttrId(arg, &PyId_keys))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 result = PyDict_Merge(self, arg, 1);
1408 else
1409 result = PyDict_MergeFromSeq2(self, arg, 1);
1410 }
1411 if (result == 0 && kwds != NULL) {
1412 if (PyArg_ValidateKeywordArguments(kwds))
1413 result = PyDict_Merge(self, kwds, 1);
1414 else
1415 result = -1;
1416 }
1417 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001418}
1419
1420static PyObject *
1421dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1422{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 if (dict_update_common(self, args, kwds, "update") != -1)
1424 Py_RETURN_NONE;
1425 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001426}
1427
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001428/* Update unconditionally replaces existing items.
1429 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001430 otherwise it leaves existing items unchanged.
1431
1432 PyDict_{Update,Merge} update/merge from a mapping object.
1433
Tim Petersf582b822001-12-11 18:51:08 +00001434 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001435 producing iterable objects of length 2.
1436*/
1437
Tim Petersf582b822001-12-11 18:51:08 +00001438int
Tim Peters1fc240e2001-10-26 05:06:50 +00001439PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 PyObject *it; /* iter(seq2) */
1442 Py_ssize_t i; /* index into seq2 of current element */
1443 PyObject *item; /* seq2[i] */
1444 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00001445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 assert(d != NULL);
1447 assert(PyDict_Check(d));
1448 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00001449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 it = PyObject_GetIter(seq2);
1451 if (it == NULL)
1452 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 for (i = 0; ; ++i) {
1455 PyObject *key, *value;
1456 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 fast = NULL;
1459 item = PyIter_Next(it);
1460 if (item == NULL) {
1461 if (PyErr_Occurred())
1462 goto Fail;
1463 break;
1464 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 /* Convert item to sequence, and verify length 2. */
1467 fast = PySequence_Fast(item, "");
1468 if (fast == NULL) {
1469 if (PyErr_ExceptionMatches(PyExc_TypeError))
1470 PyErr_Format(PyExc_TypeError,
1471 "cannot convert dictionary update "
1472 "sequence element #%zd to a sequence",
1473 i);
1474 goto Fail;
1475 }
1476 n = PySequence_Fast_GET_SIZE(fast);
1477 if (n != 2) {
1478 PyErr_Format(PyExc_ValueError,
1479 "dictionary update sequence element #%zd "
1480 "has length %zd; 2 is required",
1481 i, n);
1482 goto Fail;
1483 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 /* Update/merge with this (key, value) pair. */
1486 key = PySequence_Fast_GET_ITEM(fast, 0);
1487 value = PySequence_Fast_GET_ITEM(fast, 1);
1488 if (override || PyDict_GetItem(d, key) == NULL) {
1489 int status = PyDict_SetItem(d, key, value);
1490 if (status < 0)
1491 goto Fail;
1492 }
1493 Py_DECREF(fast);
1494 Py_DECREF(item);
1495 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 i = 0;
1498 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00001499Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001500 Py_XDECREF(item);
1501 Py_XDECREF(fast);
1502 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001503Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 Py_DECREF(it);
1505 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001506}
1507
Tim Peters6d6c1a32001-08-02 04:15:00 +00001508int
1509PyDict_Update(PyObject *a, PyObject *b)
1510{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511 return PyDict_Merge(a, b, 1);
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001512}
1513
1514int
1515PyDict_Merge(PyObject *a, PyObject *b, int override)
1516{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001517 register PyDictObject *mp, *other;
1518 register Py_ssize_t i;
1519 PyDictEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 /* We accept for the argument either a concrete dictionary object,
1522 * or an abstract "mapping" object. For the former, we can do
1523 * things quite efficiently. For the latter, we only require that
1524 * PyMapping_Keys() and PyObject_GetItem() be supported.
1525 */
1526 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1527 PyErr_BadInternalCall();
1528 return -1;
1529 }
1530 mp = (PyDictObject*)a;
1531 if (PyDict_Check(b)) {
1532 other = (PyDictObject*)b;
1533 if (other == mp || other->ma_used == 0)
1534 /* a.update(a) or a.update({}); nothing to do */
1535 return 0;
1536 if (mp->ma_used == 0)
1537 /* Since the target dict is empty, PyDict_GetItem()
1538 * always returns NULL. Setting override to 1
1539 * skips the unnecessary test.
1540 */
1541 override = 1;
1542 /* Do one big resize at the start, rather than
1543 * incrementally resizing as we insert new items. Expect
1544 * that there will be no (or few) overlapping keys.
1545 */
1546 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
1547 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
1548 return -1;
1549 }
1550 for (i = 0; i <= other->ma_mask; i++) {
1551 entry = &other->ma_table[i];
1552 if (entry->me_value != NULL &&
1553 (override ||
1554 PyDict_GetItem(a, entry->me_key) == NULL)) {
1555 Py_INCREF(entry->me_key);
1556 Py_INCREF(entry->me_value);
1557 if (insertdict(mp, entry->me_key,
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001558 entry->me_hash,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 entry->me_value) != 0)
1560 return -1;
1561 }
1562 }
1563 }
1564 else {
1565 /* Do it the generic, slower way */
1566 PyObject *keys = PyMapping_Keys(b);
1567 PyObject *iter;
1568 PyObject *key, *value;
1569 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001571 if (keys == NULL)
1572 /* Docstring says this is equivalent to E.keys() so
1573 * if E doesn't have a .keys() method we want
1574 * AttributeError to percolate up. Might as well
1575 * do the same for any other error.
1576 */
1577 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 iter = PyObject_GetIter(keys);
1580 Py_DECREF(keys);
1581 if (iter == NULL)
1582 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
1585 if (!override && PyDict_GetItem(a, key) != NULL) {
1586 Py_DECREF(key);
1587 continue;
1588 }
1589 value = PyObject_GetItem(b, key);
1590 if (value == NULL) {
1591 Py_DECREF(iter);
1592 Py_DECREF(key);
1593 return -1;
1594 }
1595 status = PyDict_SetItem(a, key, value);
1596 Py_DECREF(key);
1597 Py_DECREF(value);
1598 if (status < 0) {
1599 Py_DECREF(iter);
1600 return -1;
1601 }
1602 }
1603 Py_DECREF(iter);
1604 if (PyErr_Occurred())
1605 /* Iterator completed, via error */
1606 return -1;
1607 }
1608 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001609}
1610
1611static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001612dict_copy(register PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001613{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001615}
1616
1617PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001618PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001619{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 if (o == NULL || !PyDict_Check(o)) {
1623 PyErr_BadInternalCall();
1624 return NULL;
1625 }
1626 copy = PyDict_New();
1627 if (copy == NULL)
1628 return NULL;
1629 if (PyDict_Merge(copy, o, 1) == 0)
1630 return copy;
1631 Py_DECREF(copy);
1632 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001633}
1634
Martin v. Löwis18e16552006-02-15 17:27:45 +00001635Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001636PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001637{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 if (mp == NULL || !PyDict_Check(mp)) {
1639 PyErr_BadInternalCall();
1640 return -1;
1641 }
1642 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001643}
1644
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001645PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001646PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001647{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 if (mp == NULL || !PyDict_Check(mp)) {
1649 PyErr_BadInternalCall();
1650 return NULL;
1651 }
1652 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001653}
1654
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001655PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001656PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001657{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 if (mp == NULL || !PyDict_Check(mp)) {
1659 PyErr_BadInternalCall();
1660 return NULL;
1661 }
1662 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001663}
1664
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001665PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001666PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001667{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 if (mp == NULL || !PyDict_Check(mp)) {
1669 PyErr_BadInternalCall();
1670 return NULL;
1671 }
1672 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001673}
1674
Tim Peterse63415e2001-05-08 04:38:29 +00001675/* Return 1 if dicts equal, 0 if not, -1 if error.
1676 * Gets out as soon as any difference is detected.
1677 * Uses only Py_EQ comparison.
1678 */
1679static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001680dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00001681{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00001683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 if (a->ma_used != b->ma_used)
1685 /* can't be equal if # of entries differ */
1686 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 /* Same # of entries -- check all of 'em. Exit early on any diff. */
1689 for (i = 0; i <= a->ma_mask; i++) {
1690 PyObject *aval = a->ma_table[i].me_value;
1691 if (aval != NULL) {
1692 int cmp;
1693 PyObject *bval;
1694 PyObject *key = a->ma_table[i].me_key;
1695 /* temporarily bump aval's refcount to ensure it stays
1696 alive until we're done with it */
1697 Py_INCREF(aval);
1698 /* ditto for key */
1699 Py_INCREF(key);
1700 bval = PyDict_GetItemWithError((PyObject *)b, key);
1701 Py_DECREF(key);
1702 if (bval == NULL) {
1703 Py_DECREF(aval);
1704 if (PyErr_Occurred())
1705 return -1;
1706 return 0;
1707 }
1708 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1709 Py_DECREF(aval);
1710 if (cmp <= 0) /* error or not equal */
1711 return cmp;
1712 }
1713 }
1714 return 1;
Tim Peterse63415e2001-05-08 04:38:29 +00001715 }
1716
1717static PyObject *
1718dict_richcompare(PyObject *v, PyObject *w, int op)
1719{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 int cmp;
1721 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00001722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1724 res = Py_NotImplemented;
1725 }
1726 else if (op == Py_EQ || op == Py_NE) {
1727 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
1728 if (cmp < 0)
1729 return NULL;
1730 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1731 }
1732 else
1733 res = Py_NotImplemented;
1734 Py_INCREF(res);
1735 return res;
Tim Peterse63415e2001-05-08 04:38:29 +00001736 }
1737
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001738static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001739dict_contains(register PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001740{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001741 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001744 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001745 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 hash = PyObject_Hash(key);
1747 if (hash == -1)
1748 return NULL;
1749 }
1750 ep = (mp->ma_lookup)(mp, key, hash);
1751 if (ep == NULL)
1752 return NULL;
1753 return PyBool_FromLong(ep->me_value != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001754}
1755
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001756static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001757dict_get(register PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001758{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759 PyObject *key;
1760 PyObject *failobj = Py_None;
1761 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001762 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763 PyDictEntry *ep;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001765 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
1766 return NULL;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001769 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 hash = PyObject_Hash(key);
1771 if (hash == -1)
1772 return NULL;
1773 }
1774 ep = (mp->ma_lookup)(mp, key, hash);
1775 if (ep == NULL)
1776 return NULL;
1777 val = ep->me_value;
1778 if (val == NULL)
1779 val = failobj;
1780 Py_INCREF(val);
1781 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001782}
1783
1784
1785static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001786dict_setdefault(register PyDictObject *mp, PyObject *args)
Guido van Rossum164452c2000-08-08 16:12:54 +00001787{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001788 PyObject *key;
1789 PyObject *failobj = Py_None;
1790 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001791 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001792 PyDictEntry *ep;
Guido van Rossum164452c2000-08-08 16:12:54 +00001793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001794 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
1795 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001798 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 hash = PyObject_Hash(key);
1800 if (hash == -1)
1801 return NULL;
1802 }
1803 ep = (mp->ma_lookup)(mp, key, hash);
1804 if (ep == NULL)
1805 return NULL;
1806 val = ep->me_value;
1807 if (val == NULL) {
1808 val = failobj;
1809 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1810 val = NULL;
1811 }
1812 Py_XINCREF(val);
1813 return val;
Guido van Rossum164452c2000-08-08 16:12:54 +00001814}
1815
1816
1817static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001818dict_clear(register PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001819{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001820 PyDict_Clear((PyObject *)mp);
1821 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001822}
1823
Guido van Rossumba6ab842000-12-12 22:02:18 +00001824static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001825dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00001826{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001827 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001828 PyDictEntry *ep;
1829 PyObject *old_value, *old_key;
1830 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1833 return NULL;
1834 if (mp->ma_used == 0) {
1835 if (deflt) {
1836 Py_INCREF(deflt);
1837 return deflt;
1838 }
Raymond Hettingerdd421542010-10-30 08:10:29 +00001839 set_key_error(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001840 return NULL;
1841 }
1842 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001843 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 hash = PyObject_Hash(key);
1845 if (hash == -1)
1846 return NULL;
1847 }
1848 ep = (mp->ma_lookup)(mp, key, hash);
1849 if (ep == NULL)
1850 return NULL;
1851 if (ep->me_value == NULL) {
1852 if (deflt) {
1853 Py_INCREF(deflt);
1854 return deflt;
1855 }
1856 set_key_error(key);
1857 return NULL;
1858 }
1859 old_key = ep->me_key;
1860 Py_INCREF(dummy);
1861 ep->me_key = dummy;
1862 old_value = ep->me_value;
1863 ep->me_value = NULL;
1864 mp->ma_used--;
1865 Py_DECREF(old_key);
1866 return old_value;
Guido van Rossume027d982002-04-12 15:11:59 +00001867}
1868
1869static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001870dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001871{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001872 Py_hash_t i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001873 PyDictEntry *ep;
1874 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 /* Allocate the result tuple before checking the size. Believe it
1877 * or not, this allocation could trigger a garbage collection which
1878 * could empty the dict, so if we checked the size first and that
1879 * happened, the result would be an infinite loop (searching for an
1880 * entry that no longer exists). Note that the usual popitem()
1881 * idiom is "while d: k, v = d.popitem()". so needing to throw the
1882 * tuple away if the dict *is* empty isn't a significant
1883 * inefficiency -- possible, but unlikely in practice.
1884 */
1885 res = PyTuple_New(2);
1886 if (res == NULL)
1887 return NULL;
1888 if (mp->ma_used == 0) {
1889 Py_DECREF(res);
1890 PyErr_SetString(PyExc_KeyError,
1891 "popitem(): dictionary is empty");
1892 return NULL;
1893 }
1894 /* Set ep to "the first" dict entry with a value. We abuse the hash
1895 * field of slot 0 to hold a search finger:
1896 * If slot 0 has a value, use slot 0.
1897 * Else slot 0 is being used to hold a search finger,
1898 * and we use its hash value as the first index to look.
1899 */
1900 ep = &mp->ma_table[0];
1901 if (ep->me_value == NULL) {
1902 i = ep->me_hash;
1903 /* The hash field may be a real hash value, or it may be a
1904 * legit search finger, or it may be a once-legit search
1905 * finger that's out of bounds now because it wrapped around
1906 * or the table shrunk -- simply make sure it's in bounds now.
1907 */
1908 if (i > mp->ma_mask || i < 1)
1909 i = 1; /* skip slot 0 */
1910 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1911 i++;
1912 if (i > mp->ma_mask)
1913 i = 1;
1914 }
1915 }
1916 PyTuple_SET_ITEM(res, 0, ep->me_key);
1917 PyTuple_SET_ITEM(res, 1, ep->me_value);
1918 Py_INCREF(dummy);
1919 ep->me_key = dummy;
1920 ep->me_value = NULL;
1921 mp->ma_used--;
1922 assert(mp->ma_table[0].me_value == NULL);
1923 mp->ma_table[0].me_hash = i + 1; /* next place to start */
1924 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001925}
1926
Jeremy Hylton8caad492000-06-23 14:18:11 +00001927static int
1928dict_traverse(PyObject *op, visitproc visit, void *arg)
1929{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 Py_ssize_t i = 0;
1931 PyObject *pk;
1932 PyObject *pv;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 while (PyDict_Next(op, &i, &pk, &pv)) {
1935 Py_VISIT(pk);
1936 Py_VISIT(pv);
1937 }
1938 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001939}
1940
1941static int
1942dict_tp_clear(PyObject *op)
1943{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001944 PyDict_Clear(op);
1945 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001946}
1947
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001948static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001949
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001950static PyObject *
1951dict_sizeof(PyDictObject *mp)
1952{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953 Py_ssize_t res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 res = sizeof(PyDictObject);
1956 if (mp->ma_table != mp->ma_smalltable)
1957 res = res + (mp->ma_mask + 1) * sizeof(PyDictEntry);
1958 return PyLong_FromSsize_t(res);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001959}
1960
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001961PyDoc_STRVAR(contains__doc__,
1962"D.__contains__(k) -> True if D has a key k, else False");
1963
1964PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
1965
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001966PyDoc_STRVAR(sizeof__doc__,
1967"D.__sizeof__() -> size of D in memory, in bytes");
1968
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001969PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001970"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001971
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001972PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001973"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 +00001974
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001975PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001976"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001977If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00001978
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001979PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00001980"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000019812-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001982
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001983PyDoc_STRVAR(update__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001984"D.update(E, **F) -> None. Update D from dict/iterable E and F.\n"
1985"If E has a .keys() method, does: for k in E: D[k] = E[k]\n\
1986If E lacks .keys() method, does: for (k, v) in E: D[k] = v\n\
1987In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00001988
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001989PyDoc_STRVAR(fromkeys__doc__,
1990"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
1991v defaults to None.");
1992
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001993PyDoc_STRVAR(clear__doc__,
1994"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001995
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001996PyDoc_STRVAR(copy__doc__,
1997"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00001998
Guido van Rossumb90c8482007-02-10 01:11:45 +00001999/* Forward */
2000static PyObject *dictkeys_new(PyObject *);
2001static PyObject *dictitems_new(PyObject *);
2002static PyObject *dictvalues_new(PyObject *);
2003
Guido van Rossum45c85d12007-07-27 16:31:40 +00002004PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002005 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002006PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002007 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002008PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00002010
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002011static PyMethodDef mapp_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002012 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
2013 contains__doc__},
2014 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
2015 getitem__doc__},
2016 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
2017 sizeof__doc__},
2018 {"get", (PyCFunction)dict_get, METH_VARARGS,
2019 get__doc__},
2020 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
2021 setdefault_doc__},
2022 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
2023 pop__doc__},
2024 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
2025 popitem__doc__},
2026 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
2027 keys__doc__},
2028 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
2029 items__doc__},
2030 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
2031 values__doc__},
2032 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
2033 update__doc__},
2034 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
2035 fromkeys__doc__},
2036 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
2037 clear__doc__},
2038 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
2039 copy__doc__},
2040 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002041};
2042
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002043/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002044int
2045PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002046{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002047 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048 PyDictObject *mp = (PyDictObject *)op;
2049 PyDictEntry *ep;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002051 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002052 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 hash = PyObject_Hash(key);
2054 if (hash == -1)
2055 return -1;
2056 }
2057 ep = (mp->ma_lookup)(mp, key, hash);
2058 return ep == NULL ? -1 : (ep->me_value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002059}
2060
Thomas Wouterscf297e42007-02-23 15:07:44 +00002061/* Internal version of PyDict_Contains used when the hash value is already known */
2062int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002063_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00002064{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002065 PyDictObject *mp = (PyDictObject *)op;
2066 PyDictEntry *ep;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002068 ep = (mp->ma_lookup)(mp, key, hash);
2069 return ep == NULL ? -1 : (ep->me_value != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00002070}
2071
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002072/* Hack to implement "key in dict" */
2073static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002074 0, /* sq_length */
2075 0, /* sq_concat */
2076 0, /* sq_repeat */
2077 0, /* sq_item */
2078 0, /* sq_slice */
2079 0, /* sq_ass_item */
2080 0, /* sq_ass_slice */
2081 PyDict_Contains, /* sq_contains */
2082 0, /* sq_inplace_concat */
2083 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002084};
2085
Guido van Rossum09e563a2001-05-01 12:10:21 +00002086static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002087dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2088{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002089 PyObject *self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002091 assert(type != NULL && type->tp_alloc != NULL);
2092 self = type->tp_alloc(type, 0);
2093 if (self != NULL) {
2094 PyDictObject *d = (PyDictObject *)self;
2095 /* It's guaranteed that tp->alloc zeroed out the struct. */
2096 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
2097 INIT_NONZERO_DICT_SLOTS(d);
2098 d->ma_lookup = lookdict_unicode;
Ezio Melotti13925002011-03-16 11:05:33 +02002099 /* The object has been implicitly tracked by tp_alloc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002100 if (type == &PyDict_Type)
2101 _PyObject_GC_UNTRACK(d);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002102#ifdef SHOW_CONVERSION_COUNTS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002103 ++created;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002104#endif
Antoine Pitrou3a652b12009-03-23 18:52:06 +00002105#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002106 if (_PyObject_GC_IS_TRACKED(d))
2107 count_tracked++;
2108 else
2109 count_untracked++;
Antoine Pitrou3a652b12009-03-23 18:52:06 +00002110#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002111 }
2112 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002113}
2114
Tim Peters25786c02001-09-02 08:22:48 +00002115static int
2116dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2117{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002118 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002119}
2120
Tim Peters6d6c1a32001-08-02 04:15:00 +00002121static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002122dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002123{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002124 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002125}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002126
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002127PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002128"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002129"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002130" (key, value) pairs\n"
2131"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002132" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002133" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002134" d[k] = v\n"
2135"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2136" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002137
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002138PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002139 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2140 "dict",
2141 sizeof(PyDictObject),
2142 0,
2143 (destructor)dict_dealloc, /* tp_dealloc */
2144 0, /* tp_print */
2145 0, /* tp_getattr */
2146 0, /* tp_setattr */
2147 0, /* tp_reserved */
2148 (reprfunc)dict_repr, /* tp_repr */
2149 0, /* tp_as_number */
2150 &dict_as_sequence, /* tp_as_sequence */
2151 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002152 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002153 0, /* tp_call */
2154 0, /* tp_str */
2155 PyObject_GenericGetAttr, /* tp_getattro */
2156 0, /* tp_setattro */
2157 0, /* tp_as_buffer */
2158 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2159 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
2160 dictionary_doc, /* tp_doc */
2161 dict_traverse, /* tp_traverse */
2162 dict_tp_clear, /* tp_clear */
2163 dict_richcompare, /* tp_richcompare */
2164 0, /* tp_weaklistoffset */
2165 (getiterfunc)dict_iter, /* tp_iter */
2166 0, /* tp_iternext */
2167 mapp_methods, /* tp_methods */
2168 0, /* tp_members */
2169 0, /* tp_getset */
2170 0, /* tp_base */
2171 0, /* tp_dict */
2172 0, /* tp_descr_get */
2173 0, /* tp_descr_set */
2174 0, /* tp_dictoffset */
2175 dict_init, /* tp_init */
2176 PyType_GenericAlloc, /* tp_alloc */
2177 dict_new, /* tp_new */
2178 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002179};
2180
Guido van Rossum3cca2451997-05-16 14:23:33 +00002181/* For backward compatibility with old dictionary interface */
2182
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002183PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002184PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002185{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002186 PyObject *kv, *rv;
2187 kv = PyUnicode_FromString(key);
2188 if (kv == NULL)
2189 return NULL;
2190 rv = PyDict_GetItem(v, kv);
2191 Py_DECREF(kv);
2192 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002193}
2194
2195int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002196PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198 PyObject *kv;
2199 int err;
2200 kv = PyUnicode_FromString(key);
2201 if (kv == NULL)
2202 return -1;
2203 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
2204 err = PyDict_SetItem(v, kv, item);
2205 Py_DECREF(kv);
2206 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002207}
2208
2209int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002210PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002211{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002212 PyObject *kv;
2213 int err;
2214 kv = PyUnicode_FromString(key);
2215 if (kv == NULL)
2216 return -1;
2217 err = PyDict_DelItem(v, kv);
2218 Py_DECREF(kv);
2219 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002220}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002221
Raymond Hettinger019a1482004-03-18 02:41:19 +00002222/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002223
2224typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002225 PyObject_HEAD
2226 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
2227 Py_ssize_t di_used;
2228 Py_ssize_t di_pos;
2229 PyObject* di_result; /* reusable result tuple for iteritems */
2230 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002231} dictiterobject;
2232
2233static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002234dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002235{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 dictiterobject *di;
2237 di = PyObject_GC_New(dictiterobject, itertype);
2238 if (di == NULL)
2239 return NULL;
2240 Py_INCREF(dict);
2241 di->di_dict = dict;
2242 di->di_used = dict->ma_used;
2243 di->di_pos = 0;
2244 di->len = dict->ma_used;
2245 if (itertype == &PyDictIterItem_Type) {
2246 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2247 if (di->di_result == NULL) {
2248 Py_DECREF(di);
2249 return NULL;
2250 }
2251 }
2252 else
2253 di->di_result = NULL;
2254 _PyObject_GC_TRACK(di);
2255 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002256}
2257
2258static void
2259dictiter_dealloc(dictiterobject *di)
2260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 Py_XDECREF(di->di_dict);
2262 Py_XDECREF(di->di_result);
2263 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002264}
2265
2266static int
2267dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
2268{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002269 Py_VISIT(di->di_dict);
2270 Py_VISIT(di->di_result);
2271 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002272}
2273
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002274static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002275dictiter_len(dictiterobject *di)
2276{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 Py_ssize_t len = 0;
2278 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
2279 len = di->len;
2280 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002281}
2282
Guido van Rossumb90c8482007-02-10 01:11:45 +00002283PyDoc_STRVAR(length_hint_doc,
2284 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002285
2286static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
2288 length_hint_doc},
2289 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002290};
2291
Raymond Hettinger019a1482004-03-18 02:41:19 +00002292static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002293{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 PyObject *key;
2295 register Py_ssize_t i, mask;
2296 register PyDictEntry *ep;
2297 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002299 if (d == NULL)
2300 return NULL;
2301 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002303 if (di->di_used != d->ma_used) {
2304 PyErr_SetString(PyExc_RuntimeError,
2305 "dictionary changed size during iteration");
2306 di->di_used = -1; /* Make this state sticky */
2307 return NULL;
2308 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 i = di->di_pos;
2311 if (i < 0)
2312 goto fail;
2313 ep = d->ma_table;
2314 mask = d->ma_mask;
2315 while (i <= mask && ep[i].me_value == NULL)
2316 i++;
2317 di->di_pos = i+1;
2318 if (i > mask)
2319 goto fail;
2320 di->len--;
2321 key = ep[i].me_key;
2322 Py_INCREF(key);
2323 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002324
2325fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002326 Py_DECREF(d);
2327 di->di_dict = NULL;
2328 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002329}
2330
Raymond Hettinger019a1482004-03-18 02:41:19 +00002331PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002332 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2333 "dict_keyiterator", /* tp_name */
2334 sizeof(dictiterobject), /* tp_basicsize */
2335 0, /* tp_itemsize */
2336 /* methods */
2337 (destructor)dictiter_dealloc, /* tp_dealloc */
2338 0, /* tp_print */
2339 0, /* tp_getattr */
2340 0, /* tp_setattr */
2341 0, /* tp_reserved */
2342 0, /* tp_repr */
2343 0, /* tp_as_number */
2344 0, /* tp_as_sequence */
2345 0, /* tp_as_mapping */
2346 0, /* tp_hash */
2347 0, /* tp_call */
2348 0, /* tp_str */
2349 PyObject_GenericGetAttr, /* tp_getattro */
2350 0, /* tp_setattro */
2351 0, /* tp_as_buffer */
2352 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2353 0, /* tp_doc */
2354 (traverseproc)dictiter_traverse, /* tp_traverse */
2355 0, /* tp_clear */
2356 0, /* tp_richcompare */
2357 0, /* tp_weaklistoffset */
2358 PyObject_SelfIter, /* tp_iter */
2359 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
2360 dictiter_methods, /* tp_methods */
2361 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002362};
2363
2364static PyObject *dictiter_iternextvalue(dictiterobject *di)
2365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 PyObject *value;
2367 register Py_ssize_t i, mask;
2368 register PyDictEntry *ep;
2369 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371 if (d == NULL)
2372 return NULL;
2373 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002375 if (di->di_used != d->ma_used) {
2376 PyErr_SetString(PyExc_RuntimeError,
2377 "dictionary changed size during iteration");
2378 di->di_used = -1; /* Make this state sticky */
2379 return NULL;
2380 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002382 i = di->di_pos;
2383 mask = d->ma_mask;
2384 if (i < 0 || i > mask)
2385 goto fail;
2386 ep = d->ma_table;
2387 while ((value=ep[i].me_value) == NULL) {
2388 i++;
2389 if (i > mask)
2390 goto fail;
2391 }
2392 di->di_pos = i+1;
2393 di->len--;
2394 Py_INCREF(value);
2395 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002396
2397fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 Py_DECREF(d);
2399 di->di_dict = NULL;
2400 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002401}
2402
2403PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002404 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2405 "dict_valueiterator", /* tp_name */
2406 sizeof(dictiterobject), /* tp_basicsize */
2407 0, /* tp_itemsize */
2408 /* methods */
2409 (destructor)dictiter_dealloc, /* tp_dealloc */
2410 0, /* tp_print */
2411 0, /* tp_getattr */
2412 0, /* tp_setattr */
2413 0, /* tp_reserved */
2414 0, /* tp_repr */
2415 0, /* tp_as_number */
2416 0, /* tp_as_sequence */
2417 0, /* tp_as_mapping */
2418 0, /* tp_hash */
2419 0, /* tp_call */
2420 0, /* tp_str */
2421 PyObject_GenericGetAttr, /* tp_getattro */
2422 0, /* tp_setattro */
2423 0, /* tp_as_buffer */
2424 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2425 0, /* tp_doc */
2426 (traverseproc)dictiter_traverse, /* tp_traverse */
2427 0, /* tp_clear */
2428 0, /* tp_richcompare */
2429 0, /* tp_weaklistoffset */
2430 PyObject_SelfIter, /* tp_iter */
2431 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
2432 dictiter_methods, /* tp_methods */
2433 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002434};
2435
2436static PyObject *dictiter_iternextitem(dictiterobject *di)
2437{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002438 PyObject *key, *value, *result = di->di_result;
2439 register Py_ssize_t i, mask;
2440 register PyDictEntry *ep;
2441 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002443 if (d == NULL)
2444 return NULL;
2445 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002447 if (di->di_used != d->ma_used) {
2448 PyErr_SetString(PyExc_RuntimeError,
2449 "dictionary changed size during iteration");
2450 di->di_used = -1; /* Make this state sticky */
2451 return NULL;
2452 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002454 i = di->di_pos;
2455 if (i < 0)
2456 goto fail;
2457 ep = d->ma_table;
2458 mask = d->ma_mask;
2459 while (i <= mask && ep[i].me_value == NULL)
2460 i++;
2461 di->di_pos = i+1;
2462 if (i > mask)
2463 goto fail;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002465 if (result->ob_refcnt == 1) {
2466 Py_INCREF(result);
2467 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2468 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2469 } else {
2470 result = PyTuple_New(2);
2471 if (result == NULL)
2472 return NULL;
2473 }
2474 di->len--;
2475 key = ep[i].me_key;
2476 value = ep[i].me_value;
2477 Py_INCREF(key);
2478 Py_INCREF(value);
2479 PyTuple_SET_ITEM(result, 0, key);
2480 PyTuple_SET_ITEM(result, 1, value);
2481 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002482
2483fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002484 Py_DECREF(d);
2485 di->di_dict = NULL;
2486 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002487}
2488
2489PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002490 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2491 "dict_itemiterator", /* tp_name */
2492 sizeof(dictiterobject), /* tp_basicsize */
2493 0, /* tp_itemsize */
2494 /* methods */
2495 (destructor)dictiter_dealloc, /* tp_dealloc */
2496 0, /* tp_print */
2497 0, /* tp_getattr */
2498 0, /* tp_setattr */
2499 0, /* tp_reserved */
2500 0, /* tp_repr */
2501 0, /* tp_as_number */
2502 0, /* tp_as_sequence */
2503 0, /* tp_as_mapping */
2504 0, /* tp_hash */
2505 0, /* tp_call */
2506 0, /* tp_str */
2507 PyObject_GenericGetAttr, /* tp_getattro */
2508 0, /* tp_setattro */
2509 0, /* tp_as_buffer */
2510 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2511 0, /* tp_doc */
2512 (traverseproc)dictiter_traverse, /* tp_traverse */
2513 0, /* tp_clear */
2514 0, /* tp_richcompare */
2515 0, /* tp_weaklistoffset */
2516 PyObject_SelfIter, /* tp_iter */
2517 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
2518 dictiter_methods, /* tp_methods */
2519 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002520};
Guido van Rossumb90c8482007-02-10 01:11:45 +00002521
2522
Guido van Rossum3ac67412007-02-10 18:55:06 +00002523/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002524/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002525/***********************************************/
2526
Guido van Rossumb90c8482007-02-10 01:11:45 +00002527/* The instance lay-out is the same for all three; but the type differs. */
2528
2529typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002530 PyObject_HEAD
2531 PyDictObject *dv_dict;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002532} dictviewobject;
2533
2534
2535static void
Guido van Rossum3ac67412007-02-10 18:55:06 +00002536dictview_dealloc(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002537{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002538 Py_XDECREF(dv->dv_dict);
2539 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002540}
2541
2542static int
2543dictview_traverse(dictviewobject *dv, visitproc visit, void *arg)
2544{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002545 Py_VISIT(dv->dv_dict);
2546 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002547}
2548
Guido van Rossum83825ac2007-02-10 04:54:19 +00002549static Py_ssize_t
Guido van Rossum3ac67412007-02-10 18:55:06 +00002550dictview_len(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002551{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002552 Py_ssize_t len = 0;
2553 if (dv->dv_dict != NULL)
2554 len = dv->dv_dict->ma_used;
2555 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002556}
2557
2558static PyObject *
2559dictview_new(PyObject *dict, PyTypeObject *type)
2560{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002561 dictviewobject *dv;
2562 if (dict == NULL) {
2563 PyErr_BadInternalCall();
2564 return NULL;
2565 }
2566 if (!PyDict_Check(dict)) {
2567 /* XXX Get rid of this restriction later */
2568 PyErr_Format(PyExc_TypeError,
2569 "%s() requires a dict argument, not '%s'",
2570 type->tp_name, dict->ob_type->tp_name);
2571 return NULL;
2572 }
2573 dv = PyObject_GC_New(dictviewobject, type);
2574 if (dv == NULL)
2575 return NULL;
2576 Py_INCREF(dict);
2577 dv->dv_dict = (PyDictObject *)dict;
2578 _PyObject_GC_TRACK(dv);
2579 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002580}
2581
Neal Norwitze36f2ba2007-02-26 23:12:28 +00002582/* TODO(guido): The views objects are not complete:
2583
2584 * support more set operations
2585 * support arbitrary mappings?
2586 - either these should be static or exported in dictobject.h
2587 - if public then they should probably be in builtins
2588*/
2589
Guido van Rossumaac530c2007-08-24 22:33:45 +00002590/* Return 1 if self is a subset of other, iterating over self;
2591 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002592static int
2593all_contained_in(PyObject *self, PyObject *other)
2594{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002595 PyObject *iter = PyObject_GetIter(self);
2596 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00002597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002598 if (iter == NULL)
2599 return -1;
2600 for (;;) {
2601 PyObject *next = PyIter_Next(iter);
2602 if (next == NULL) {
2603 if (PyErr_Occurred())
2604 ok = -1;
2605 break;
2606 }
2607 ok = PySequence_Contains(other, next);
2608 Py_DECREF(next);
2609 if (ok <= 0)
2610 break;
2611 }
2612 Py_DECREF(iter);
2613 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00002614}
2615
2616static PyObject *
2617dictview_richcompare(PyObject *self, PyObject *other, int op)
2618{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002619 Py_ssize_t len_self, len_other;
2620 int ok;
2621 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002623 assert(self != NULL);
2624 assert(PyDictViewSet_Check(self));
2625 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00002626
Brian Curtindfc80e32011-08-10 20:28:54 -05002627 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
2628 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002630 len_self = PyObject_Size(self);
2631 if (len_self < 0)
2632 return NULL;
2633 len_other = PyObject_Size(other);
2634 if (len_other < 0)
2635 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002637 ok = 0;
2638 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00002639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002640 case Py_NE:
2641 case Py_EQ:
2642 if (len_self == len_other)
2643 ok = all_contained_in(self, other);
2644 if (op == Py_NE && ok >= 0)
2645 ok = !ok;
2646 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002648 case Py_LT:
2649 if (len_self < len_other)
2650 ok = all_contained_in(self, other);
2651 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002653 case Py_LE:
2654 if (len_self <= len_other)
2655 ok = all_contained_in(self, other);
2656 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002658 case Py_GT:
2659 if (len_self > len_other)
2660 ok = all_contained_in(other, self);
2661 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002663 case Py_GE:
2664 if (len_self >= len_other)
2665 ok = all_contained_in(other, self);
2666 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002668 }
2669 if (ok < 0)
2670 return NULL;
2671 result = ok ? Py_True : Py_False;
2672 Py_INCREF(result);
2673 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00002674}
2675
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00002676static PyObject *
2677dictview_repr(dictviewobject *dv)
2678{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002679 PyObject *seq;
2680 PyObject *result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00002681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002682 seq = PySequence_List((PyObject *)dv);
2683 if (seq == NULL)
2684 return NULL;
2685
2686 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
2687 Py_DECREF(seq);
2688 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00002689}
2690
Guido van Rossum3ac67412007-02-10 18:55:06 +00002691/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002692
2693static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002694dictkeys_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002695{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002696 if (dv->dv_dict == NULL) {
2697 Py_RETURN_NONE;
2698 }
2699 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00002700}
2701
2702static int
2703dictkeys_contains(dictviewobject *dv, PyObject *obj)
2704{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002705 if (dv->dv_dict == NULL)
2706 return 0;
2707 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002708}
2709
Guido van Rossum83825ac2007-02-10 04:54:19 +00002710static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002711 (lenfunc)dictview_len, /* sq_length */
2712 0, /* sq_concat */
2713 0, /* sq_repeat */
2714 0, /* sq_item */
2715 0, /* sq_slice */
2716 0, /* sq_ass_item */
2717 0, /* sq_ass_slice */
2718 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002719};
2720
Guido van Rossum523259b2007-08-24 23:41:22 +00002721static PyObject*
2722dictviews_sub(PyObject* self, PyObject *other)
2723{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002724 PyObject *result = PySet_New(self);
2725 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002726 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002727
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002728 if (result == NULL)
2729 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00002730
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002731 tmp = _PyObject_CallMethodId(result, &PyId_difference_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002732 if (tmp == NULL) {
2733 Py_DECREF(result);
2734 return NULL;
2735 }
Guido van Rossum523259b2007-08-24 23:41:22 +00002736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002737 Py_DECREF(tmp);
2738 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00002739}
2740
2741static PyObject*
2742dictviews_and(PyObject* self, PyObject *other)
2743{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002744 PyObject *result = PySet_New(self);
2745 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002746 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002747
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002748 if (result == NULL)
2749 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00002750
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002751 tmp = _PyObject_CallMethodId(result, &PyId_intersection_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002752 if (tmp == NULL) {
2753 Py_DECREF(result);
2754 return NULL;
2755 }
Guido van Rossum523259b2007-08-24 23:41:22 +00002756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 Py_DECREF(tmp);
2758 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00002759}
2760
2761static PyObject*
2762dictviews_or(PyObject* self, PyObject *other)
2763{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002764 PyObject *result = PySet_New(self);
2765 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002766 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02002767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002768 if (result == NULL)
2769 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00002770
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02002771 tmp = _PyObject_CallMethodId(result, &PyId_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002772 if (tmp == NULL) {
2773 Py_DECREF(result);
2774 return NULL;
2775 }
Guido van Rossum523259b2007-08-24 23:41:22 +00002776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002777 Py_DECREF(tmp);
2778 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00002779}
2780
2781static PyObject*
2782dictviews_xor(PyObject* self, PyObject *other)
2783{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002784 PyObject *result = PySet_New(self);
2785 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002786 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002788 if (result == NULL)
2789 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00002790
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002791 tmp = _PyObject_CallMethodId(result, &PyId_symmetric_difference_update, "O",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002792 other);
2793 if (tmp == NULL) {
2794 Py_DECREF(result);
2795 return NULL;
2796 }
Guido van Rossum523259b2007-08-24 23:41:22 +00002797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002798 Py_DECREF(tmp);
2799 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00002800}
2801
2802static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002803 0, /*nb_add*/
2804 (binaryfunc)dictviews_sub, /*nb_subtract*/
2805 0, /*nb_multiply*/
2806 0, /*nb_remainder*/
2807 0, /*nb_divmod*/
2808 0, /*nb_power*/
2809 0, /*nb_negative*/
2810 0, /*nb_positive*/
2811 0, /*nb_absolute*/
2812 0, /*nb_bool*/
2813 0, /*nb_invert*/
2814 0, /*nb_lshift*/
2815 0, /*nb_rshift*/
2816 (binaryfunc)dictviews_and, /*nb_and*/
2817 (binaryfunc)dictviews_xor, /*nb_xor*/
2818 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00002819};
2820
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00002821static PyObject*
2822dictviews_isdisjoint(PyObject *self, PyObject *other)
2823{
2824 PyObject *it;
2825 PyObject *item = NULL;
2826
2827 if (self == other) {
2828 if (dictview_len((dictviewobject *)self) == 0)
2829 Py_RETURN_TRUE;
2830 else
2831 Py_RETURN_FALSE;
2832 }
2833
2834 /* Iterate over the shorter object (only if other is a set,
2835 * because PySequence_Contains may be expensive otherwise): */
2836 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
2837 Py_ssize_t len_self = dictview_len((dictviewobject *)self);
2838 Py_ssize_t len_other = PyObject_Size(other);
2839 if (len_other == -1)
2840 return NULL;
2841
2842 if ((len_other > len_self)) {
2843 PyObject *tmp = other;
2844 other = self;
2845 self = tmp;
2846 }
2847 }
2848
2849 it = PyObject_GetIter(other);
2850 if (it == NULL)
2851 return NULL;
2852
2853 while ((item = PyIter_Next(it)) != NULL) {
2854 int contains = PySequence_Contains(self, item);
2855 Py_DECREF(item);
2856 if (contains == -1) {
2857 Py_DECREF(it);
2858 return NULL;
2859 }
2860
2861 if (contains) {
2862 Py_DECREF(it);
2863 Py_RETURN_FALSE;
2864 }
2865 }
2866 Py_DECREF(it);
2867 if (PyErr_Occurred())
2868 return NULL; /* PyIter_Next raised an exception. */
2869 Py_RETURN_TRUE;
2870}
2871
2872PyDoc_STRVAR(isdisjoint_doc,
2873"Return True if the view and the given iterable have a null intersection.");
2874
Guido van Rossumb90c8482007-02-10 01:11:45 +00002875static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00002876 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
2877 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002878 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002879};
2880
2881PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002882 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2883 "dict_keys", /* tp_name */
2884 sizeof(dictviewobject), /* tp_basicsize */
2885 0, /* tp_itemsize */
2886 /* methods */
2887 (destructor)dictview_dealloc, /* tp_dealloc */
2888 0, /* tp_print */
2889 0, /* tp_getattr */
2890 0, /* tp_setattr */
2891 0, /* tp_reserved */
2892 (reprfunc)dictview_repr, /* tp_repr */
2893 &dictviews_as_number, /* tp_as_number */
2894 &dictkeys_as_sequence, /* tp_as_sequence */
2895 0, /* tp_as_mapping */
2896 0, /* tp_hash */
2897 0, /* tp_call */
2898 0, /* tp_str */
2899 PyObject_GenericGetAttr, /* tp_getattro */
2900 0, /* tp_setattro */
2901 0, /* tp_as_buffer */
2902 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2903 0, /* tp_doc */
2904 (traverseproc)dictview_traverse, /* tp_traverse */
2905 0, /* tp_clear */
2906 dictview_richcompare, /* tp_richcompare */
2907 0, /* tp_weaklistoffset */
2908 (getiterfunc)dictkeys_iter, /* tp_iter */
2909 0, /* tp_iternext */
2910 dictkeys_methods, /* tp_methods */
2911 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00002912};
2913
2914static PyObject *
2915dictkeys_new(PyObject *dict)
2916{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 return dictview_new(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002918}
2919
Guido van Rossum3ac67412007-02-10 18:55:06 +00002920/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002921
2922static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002923dictitems_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002924{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002925 if (dv->dv_dict == NULL) {
2926 Py_RETURN_NONE;
2927 }
2928 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00002929}
2930
2931static int
2932dictitems_contains(dictviewobject *dv, PyObject *obj)
2933{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002934 PyObject *key, *value, *found;
2935 if (dv->dv_dict == NULL)
2936 return 0;
2937 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
2938 return 0;
2939 key = PyTuple_GET_ITEM(obj, 0);
2940 value = PyTuple_GET_ITEM(obj, 1);
2941 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
2942 if (found == NULL) {
2943 if (PyErr_Occurred())
2944 return -1;
2945 return 0;
2946 }
2947 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002948}
2949
Guido van Rossum83825ac2007-02-10 04:54:19 +00002950static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002951 (lenfunc)dictview_len, /* sq_length */
2952 0, /* sq_concat */
2953 0, /* sq_repeat */
2954 0, /* sq_item */
2955 0, /* sq_slice */
2956 0, /* sq_ass_item */
2957 0, /* sq_ass_slice */
2958 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002959};
2960
Guido van Rossumb90c8482007-02-10 01:11:45 +00002961static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00002962 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
2963 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002964 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002965};
2966
2967PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002968 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2969 "dict_items", /* tp_name */
2970 sizeof(dictviewobject), /* tp_basicsize */
2971 0, /* tp_itemsize */
2972 /* methods */
2973 (destructor)dictview_dealloc, /* tp_dealloc */
2974 0, /* tp_print */
2975 0, /* tp_getattr */
2976 0, /* tp_setattr */
2977 0, /* tp_reserved */
2978 (reprfunc)dictview_repr, /* tp_repr */
2979 &dictviews_as_number, /* tp_as_number */
2980 &dictitems_as_sequence, /* tp_as_sequence */
2981 0, /* tp_as_mapping */
2982 0, /* tp_hash */
2983 0, /* tp_call */
2984 0, /* tp_str */
2985 PyObject_GenericGetAttr, /* tp_getattro */
2986 0, /* tp_setattro */
2987 0, /* tp_as_buffer */
2988 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2989 0, /* tp_doc */
2990 (traverseproc)dictview_traverse, /* tp_traverse */
2991 0, /* tp_clear */
2992 dictview_richcompare, /* tp_richcompare */
2993 0, /* tp_weaklistoffset */
2994 (getiterfunc)dictitems_iter, /* tp_iter */
2995 0, /* tp_iternext */
2996 dictitems_methods, /* tp_methods */
2997 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00002998};
2999
3000static PyObject *
3001dictitems_new(PyObject *dict)
3002{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003003 return dictview_new(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003004}
3005
Guido van Rossum3ac67412007-02-10 18:55:06 +00003006/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003007
3008static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003009dictvalues_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003010{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003011 if (dv->dv_dict == NULL) {
3012 Py_RETURN_NONE;
3013 }
3014 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003015}
3016
Guido van Rossum83825ac2007-02-10 04:54:19 +00003017static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003018 (lenfunc)dictview_len, /* sq_length */
3019 0, /* sq_concat */
3020 0, /* sq_repeat */
3021 0, /* sq_item */
3022 0, /* sq_slice */
3023 0, /* sq_ass_item */
3024 0, /* sq_ass_slice */
3025 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003026};
3027
Guido van Rossumb90c8482007-02-10 01:11:45 +00003028static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003029 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003030};
3031
3032PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003033 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3034 "dict_values", /* tp_name */
3035 sizeof(dictviewobject), /* tp_basicsize */
3036 0, /* tp_itemsize */
3037 /* methods */
3038 (destructor)dictview_dealloc, /* tp_dealloc */
3039 0, /* tp_print */
3040 0, /* tp_getattr */
3041 0, /* tp_setattr */
3042 0, /* tp_reserved */
3043 (reprfunc)dictview_repr, /* tp_repr */
3044 0, /* tp_as_number */
3045 &dictvalues_as_sequence, /* tp_as_sequence */
3046 0, /* tp_as_mapping */
3047 0, /* tp_hash */
3048 0, /* tp_call */
3049 0, /* tp_str */
3050 PyObject_GenericGetAttr, /* tp_getattro */
3051 0, /* tp_setattro */
3052 0, /* tp_as_buffer */
3053 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3054 0, /* tp_doc */
3055 (traverseproc)dictview_traverse, /* tp_traverse */
3056 0, /* tp_clear */
3057 0, /* tp_richcompare */
3058 0, /* tp_weaklistoffset */
3059 (getiterfunc)dictvalues_iter, /* tp_iter */
3060 0, /* tp_iternext */
3061 dictvalues_methods, /* tp_methods */
3062 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003063};
3064
3065static PyObject *
3066dictvalues_new(PyObject *dict)
3067{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003068 return dictview_new(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003069}