blob: 6c2b788b48d06dec3df9a036bfd053ed56bceae7 [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"
Guido van Rossum4b1302b1993-03-27 18:11:32 +000011
Guido van Rossum16e93a81997-01-28 00:00:11 +000012
Georg Brandlb9f4ad32006-10-29 18:31:42 +000013/* Set a key error with the specified argument, wrapping it in a
14 * tuple automatically so that tuple keys are not unpacked as the
15 * exception arguments. */
16static void
17set_key_error(PyObject *arg)
18{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000019 PyObject *tup;
20 tup = PyTuple_Pack(1, arg);
21 if (!tup)
22 return; /* caller will expect error to be set anyway */
23 PyErr_SetObject(PyExc_KeyError, tup);
24 Py_DECREF(tup);
Georg Brandlb9f4ad32006-10-29 18:31:42 +000025}
26
Tim Peterseb28ef22001-06-02 05:27:19 +000027/* Define this out if you don't want conversion statistics on exit. */
Fred Drake1bff34a2000-08-31 19:31:38 +000028#undef SHOW_CONVERSION_COUNTS
29
Tim Peterseb28ef22001-06-02 05:27:19 +000030/* See large comment block below. This must be >= 1. */
31#define PERTURB_SHIFT 5
32
Guido van Rossum16e93a81997-01-28 00:00:11 +000033/*
Tim Peterseb28ef22001-06-02 05:27:19 +000034Major subtleties ahead: Most hash schemes depend on having a "good" hash
35function, in the sense of simulating randomness. Python doesn't: its most
36important hash functions (for strings and ints) are very regular in common
37cases:
Tim Peters15d49292001-05-27 07:39:22 +000038
Tim Peterseb28ef22001-06-02 05:27:19 +000039>>> map(hash, (0, 1, 2, 3))
40[0, 1, 2, 3]
41>>> map(hash, ("namea", "nameb", "namec", "named"))
42[-1658398457, -1658398460, -1658398459, -1658398462]
43>>>
Tim Peters15d49292001-05-27 07:39:22 +000044
Tim Peterseb28ef22001-06-02 05:27:19 +000045This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
46the low-order i bits as the initial table index is extremely fast, and there
47are no collisions at all for dicts indexed by a contiguous range of ints.
48The same is approximately true when keys are "consecutive" strings. So this
49gives better-than-random behavior in common cases, and that's very desirable.
Tim Peters15d49292001-05-27 07:39:22 +000050
Tim Peterseb28ef22001-06-02 05:27:19 +000051OTOH, when collisions occur, the tendency to fill contiguous slices of the
52hash table makes a good collision resolution strategy crucial. Taking only
53the last i bits of the hash code is also vulnerable: for example, consider
54[i << 16 for i in range(20000)] as a set of keys. Since ints are their own
55hash codes, and this fits in a dict of size 2**15, the last 15 bits of every
56hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +000057
Tim Peterseb28ef22001-06-02 05:27:19 +000058But catering to unusual cases should not slow the usual ones, so we just take
59the last i bits anyway. It's up to collision resolution to do the rest. If
60we *usually* find the key we're looking for on the first try (and, it turns
61out, we usually do -- the table load factor is kept under 2/3, so the odds
62are solidly in our favor), then it makes best sense to keep the initial index
63computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +000064
Tim Peterseb28ef22001-06-02 05:27:19 +000065The first half of collision resolution is to visit table indices via this
66recurrence:
Tim Peters15d49292001-05-27 07:39:22 +000067
Tim Peterseb28ef22001-06-02 05:27:19 +000068 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +000069
Tim Peterseb28ef22001-06-02 05:27:19 +000070For any initial j in range(2**i), repeating that 2**i times generates each
71int in range(2**i) exactly once (see any text on random-number generation for
72proof). By itself, this doesn't help much: like linear probing (setting
73j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
74order. This would be bad, except that's not the only thing we do, and it's
75actually *good* in the common cases where hash keys are consecutive. In an
76example that's really too small to make this entirely clear, for a table of
77size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +000078
Tim Peterseb28ef22001-06-02 05:27:19 +000079 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
80
81If two things come in at index 5, the first place we look after is index 2,
82not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
83Linear probing is deadly in this case because there the fixed probe order
84is the *same* as the order consecutive keys are likely to arrive. But it's
85extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
86and certain that consecutive hash codes do not.
87
88The other half of the strategy is to get the other bits of the hash code
89into play. This is done by initializing a (unsigned) vrbl "perturb" to the
90full hash code, and changing the recurrence to:
91
92 j = (5*j) + 1 + perturb;
93 perturb >>= PERTURB_SHIFT;
94 use j % 2**i as the next table index;
95
96Now the probe sequence depends (eventually) on every bit in the hash code,
97and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
98because it quickly magnifies small differences in the bits that didn't affect
99the initial index. Note that because perturb is unsigned, if the recurrence
100is executed often enough perturb eventually becomes and remains 0. At that
101point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
102that's certain to find an empty slot eventually (since it generates every int
103in range(2**i), and we make sure there's always at least one empty slot).
104
105Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
106small so that the high bits of the hash code continue to affect the probe
107sequence across iterations; but you want it large so that in really bad cases
108the high-order hash bits have an effect on early iterations. 5 was "the
109best" in minimizing total collisions across experiments Tim Peters ran (on
110both normal and pathological cases), but 4 and 6 weren't significantly worse.
111
112Historical: Reimer Behrends contributed the idea of using a polynomial-based
113approach, using repeated multiplication by x in GF(2**n) where an irreducible
114polynomial for each table size was chosen such that x was a primitive root.
115Christian Tismer later extended that to use division by x instead, as an
116efficient way to get the high bits of the hash code into play. This scheme
117also gave excellent collision statistics, but was more expensive: two
118if-tests were required inside the loop; computing "the next" index took about
119the same number of operations but without as much potential parallelism
120(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
121above, and then shifting perturb can be done while the table index is being
Brett Cannon77ae87c2007-10-10 00:07:50 +0000122masked); and the PyDictObject struct required a member to hold the table's
Tim Peterseb28ef22001-06-02 05:27:19 +0000123polynomial. In Tim's experiments the current scheme ran faster, produced
124equally good collision statistics, needed less code & used less memory.
Tim Peters9b10f7e2006-05-30 04:16:25 +0000125
126Theoretical Python 2.5 headache: hash codes are only C "long", but
127sizeof(Py_ssize_t) > sizeof(long) may be possible. In that case, and if a
128dict is genuinely huge, then only the slots directly reachable via indexing
129by a C long can be the first slot in a probe sequence. The probe sequence
130will still eventually reach every slot in the table, but the collision rate
131on initial probes may be much higher than this scheme was designed for.
132Getting a hash code as fat as Py_ssize_t is the only real cure. But in
133practice, this probably won't make a lick of difference for many years (at
134which point everyone will have terabytes of RAM on 64-bit boxes).
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000135*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000136
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000137/* Object used as dummy key to fill deleted entries */
Brett Cannon77ae87c2007-10-10 00:07:50 +0000138static PyObject *dummy = NULL; /* Initialized by first call to newPyDictObject() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000139
Armin Rigoe1709372006-04-12 17:06:05 +0000140#ifdef Py_REF_DEBUG
141PyObject *
142_PyDict_Dummy(void)
143{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000144 return dummy;
Armin Rigoe1709372006-04-12 17:06:05 +0000145}
146#endif
147
Fred Drake1bff34a2000-08-31 19:31:38 +0000148/* forward declarations */
Brett Cannon77ae87c2007-10-10 00:07:50 +0000149static PyDictEntry *
150lookdict_string(PyDictObject *mp, PyObject *key, long hash);
Fred Drake1bff34a2000-08-31 19:31:38 +0000151
152#ifdef SHOW_CONVERSION_COUNTS
153static long created = 0L;
154static long converted = 0L;
155
156static void
157show_counts(void)
158{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000159 fprintf(stderr, "created %ld string dicts\n", created);
160 fprintf(stderr, "converted %ld to normal dicts\n", converted);
161 fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created);
Fred Drake1bff34a2000-08-31 19:31:38 +0000162}
163#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000164
Christian Heimesb4ee4a12008-02-07 17:15:30 +0000165/* Debug statistic to compare allocations with reuse through the free list */
166#undef SHOW_ALLOC_COUNT
167#ifdef SHOW_ALLOC_COUNT
168static size_t count_alloc = 0;
169static size_t count_reuse = 0;
170
171static void
172show_alloc(void)
173{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000174 fprintf(stderr, "Dict allocations: %" PY_FORMAT_SIZE_T "d\n",
175 count_alloc);
176 fprintf(stderr, "Dict reuse through freelist: %" PY_FORMAT_SIZE_T
177 "d\n", count_reuse);
178 fprintf(stderr, "%.2f%% reuse rate\n\n",
179 (100.0*count_reuse/(count_alloc+count_reuse)));
Christian Heimesb4ee4a12008-02-07 17:15:30 +0000180}
181#endif
182
Antoine Pitrouf8387af2009-03-23 18:41:45 +0000183/* Debug statistic to count GC tracking of dicts */
184#ifdef SHOW_TRACK_COUNT
185static Py_ssize_t count_untracked = 0;
186static Py_ssize_t count_tracked = 0;
187
188static void
189show_track(void)
190{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000191 fprintf(stderr, "Dicts created: %" PY_FORMAT_SIZE_T "d\n",
192 count_tracked + count_untracked);
193 fprintf(stderr, "Dicts tracked by the GC: %" PY_FORMAT_SIZE_T
194 "d\n", count_tracked);
195 fprintf(stderr, "%.2f%% dict tracking rate\n\n",
196 (100.0*count_tracked/(count_untracked+count_tracked)));
Antoine Pitrouf8387af2009-03-23 18:41:45 +0000197}
198#endif
199
200
Tim Peters6d6c1a32001-08-02 04:15:00 +0000201/* Initialization macros.
202 There are two ways to create a dict: PyDict_New() is the main C API
203 function, and the tp_new slot maps to dict_new(). In the latter case we
204 can save a little time over what PyDict_New does because it's guaranteed
205 that the PyDictObject struct is already zeroed out.
206 Everyone except dict_new() should use EMPTY_TO_MINSIZE (unless they have
207 an excellent reason not to).
208*/
209
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000210#define INIT_NONZERO_DICT_SLOTS(mp) do { \
211 (mp)->ma_table = (mp)->ma_smalltable; \
212 (mp)->ma_mask = PyDict_MINSIZE - 1; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000213 } while(0)
214
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000215#define EMPTY_TO_MINSIZE(mp) do { \
216 memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \
217 (mp)->ma_used = (mp)->ma_fill = 0; \
218 INIT_NONZERO_DICT_SLOTS(mp); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000219 } while(0)
220
Raymond Hettinger43442782004-03-17 21:55:03 +0000221/* Dictionary reuse scheme to save calls to malloc, free, and memset */
Christian Heimes5b970ad2008-02-06 13:33:44 +0000222#ifndef PyDict_MAXFREELIST
223#define PyDict_MAXFREELIST 80
224#endif
225static PyDictObject *free_list[PyDict_MAXFREELIST];
226static int numfree = 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000227
Christian Heimesf75dbef2008-02-08 00:11:31 +0000228void
229PyDict_Fini(void)
230{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000231 PyDictObject *op;
Christian Heimesf75dbef2008-02-08 00:11:31 +0000232
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000233 while (numfree) {
234 op = free_list[--numfree];
235 assert(PyDict_CheckExact(op));
236 PyObject_GC_Del(op);
237 }
Christian Heimesf75dbef2008-02-08 00:11:31 +0000238}
239
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000240PyObject *
Thomas Wouters78890102000-07-22 19:25:51 +0000241PyDict_New(void)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000242{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000243 register PyDictObject *mp;
244 if (dummy == NULL) { /* Auto-initialize dummy */
245 dummy = PyString_FromString("<dummy key>");
246 if (dummy == NULL)
247 return NULL;
Fred Drake1bff34a2000-08-31 19:31:38 +0000248#ifdef SHOW_CONVERSION_COUNTS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000249 Py_AtExit(show_counts);
Fred Drake1bff34a2000-08-31 19:31:38 +0000250#endif
Christian Heimesb4ee4a12008-02-07 17:15:30 +0000251#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000252 Py_AtExit(show_alloc);
Christian Heimesb4ee4a12008-02-07 17:15:30 +0000253#endif
Antoine Pitrouf8387af2009-03-23 18:41:45 +0000254#ifdef SHOW_TRACK_COUNT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000255 Py_AtExit(show_track);
Antoine Pitrouf8387af2009-03-23 18:41:45 +0000256#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000257 }
258 if (numfree) {
259 mp = free_list[--numfree];
260 assert (mp != NULL);
261 assert (Py_TYPE(mp) == &PyDict_Type);
262 _Py_NewReference((PyObject *)mp);
263 if (mp->ma_fill) {
264 EMPTY_TO_MINSIZE(mp);
265 } else {
266 /* At least set ma_table and ma_mask; these are wrong
267 if an empty but presized dict is added to freelist */
268 INIT_NONZERO_DICT_SLOTS(mp);
269 }
270 assert (mp->ma_used == 0);
271 assert (mp->ma_table == mp->ma_smalltable);
272 assert (mp->ma_mask == PyDict_MINSIZE - 1);
Christian Heimesb4ee4a12008-02-07 17:15:30 +0000273#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000274 count_reuse++;
Christian Heimesb4ee4a12008-02-07 17:15:30 +0000275#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000276 } else {
277 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
278 if (mp == NULL)
279 return NULL;
280 EMPTY_TO_MINSIZE(mp);
Christian Heimesb4ee4a12008-02-07 17:15:30 +0000281#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000282 count_alloc++;
Christian Heimesb4ee4a12008-02-07 17:15:30 +0000283#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000284 }
285 mp->ma_lookup = lookdict_string;
Antoine Pitrouf8387af2009-03-23 18:41:45 +0000286#ifdef SHOW_TRACK_COUNT
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000287 count_untracked++;
Antoine Pitrouf8387af2009-03-23 18:41:45 +0000288#endif
Fred Drake1bff34a2000-08-31 19:31:38 +0000289#ifdef SHOW_CONVERSION_COUNTS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000290 ++created;
Fred Drake1bff34a2000-08-31 19:31:38 +0000291#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000292 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000293}
294
295/*
296The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000297This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000298Open addressing is preferred over chaining since the link overhead for
299chaining would be substantial (100% with typical malloc overhead).
300
Tim Peterseb28ef22001-06-02 05:27:19 +0000301The initial probe index is computed as hash mod the table size. Subsequent
302probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000303
304All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000305
Tim Peterseb28ef22001-06-02 05:27:19 +0000306(The details in this version are due to Tim Peters, building on many past
307contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
308Christian Tismer).
Fred Drake1bff34a2000-08-31 19:31:38 +0000309
Tim Petersd770ebd2006-06-01 15:50:44 +0000310lookdict() is general-purpose, and may return NULL if (and only if) a
311comparison raises an exception (this was new in Python 2.5).
312lookdict_string() below is specialized to string keys, comparison of which can
313never raise an exception; that function can never return NULL. For both, when
Brett Cannon77ae87c2007-10-10 00:07:50 +0000314the key isn't found a PyDictEntry* is returned for which the me_value field is
Tim Petersd770ebd2006-06-01 15:50:44 +0000315NULL; this is the slot in the dict at which the key would have been found, and
316the caller can (if it wishes) add the <key, value> pair to the returned
Brett Cannon77ae87c2007-10-10 00:07:50 +0000317PyDictEntry*.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000318*/
Brett Cannon77ae87c2007-10-10 00:07:50 +0000319static PyDictEntry *
320lookdict(PyDictObject *mp, PyObject *key, register long hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000321{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000322 register size_t i;
323 register size_t perturb;
324 register PyDictEntry *freeslot;
325 register size_t mask = (size_t)mp->ma_mask;
326 PyDictEntry *ep0 = mp->ma_table;
327 register PyDictEntry *ep;
328 register int cmp;
329 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000330
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000331 i = (size_t)hash & mask;
332 ep = &ep0[i];
333 if (ep->me_key == NULL || ep->me_key == key)
334 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000335
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000336 if (ep->me_key == dummy)
337 freeslot = ep;
338 else {
339 if (ep->me_hash == hash) {
340 startkey = ep->me_key;
341 Py_INCREF(startkey);
342 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
343 Py_DECREF(startkey);
344 if (cmp < 0)
345 return NULL;
346 if (ep0 == mp->ma_table && ep->me_key == startkey) {
347 if (cmp > 0)
348 return ep;
349 }
350 else {
351 /* The compare did major nasty stuff to the
352 * dict: start over.
353 * XXX A clever adversary could prevent this
354 * XXX from terminating.
355 */
356 return lookdict(mp, key, hash);
357 }
358 }
359 freeslot = NULL;
360 }
Tim Peters15d49292001-05-27 07:39:22 +0000361
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000362 /* In the loop, me_key == dummy is by far (factor of 100s) the
363 least likely outcome, so test for that last. */
364 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
365 i = (i << 2) + i + perturb + 1;
366 ep = &ep0[i & mask];
367 if (ep->me_key == NULL)
368 return freeslot == NULL ? ep : freeslot;
369 if (ep->me_key == key)
370 return ep;
371 if (ep->me_hash == hash && ep->me_key != dummy) {
372 startkey = ep->me_key;
373 Py_INCREF(startkey);
374 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
375 Py_DECREF(startkey);
376 if (cmp < 0)
377 return NULL;
378 if (ep0 == mp->ma_table && ep->me_key == startkey) {
379 if (cmp > 0)
380 return ep;
381 }
382 else {
383 /* The compare did major nasty stuff to the
384 * dict: start over.
385 * XXX A clever adversary could prevent this
386 * XXX from terminating.
387 */
388 return lookdict(mp, key, hash);
389 }
390 }
391 else if (ep->me_key == dummy && freeslot == NULL)
392 freeslot = ep;
393 }
394 assert(0); /* NOT REACHED */
395 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000396}
397
398/*
Fred Drake1bff34a2000-08-31 19:31:38 +0000399 * Hacked up version of lookdict which can assume keys are always strings;
Tim Petersd770ebd2006-06-01 15:50:44 +0000400 * this assumption allows testing for errors during PyObject_RichCompareBool()
401 * to be dropped; string-string comparisons never raise exceptions. This also
402 * means we don't need to go through PyObject_RichCompareBool(); we can always
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000403 * use _PyString_Eq() directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000404 *
Tim Petersd770ebd2006-06-01 15:50:44 +0000405 * This is valuable because dicts with only string keys are very common.
Fred Drake1bff34a2000-08-31 19:31:38 +0000406 */
Brett Cannon77ae87c2007-10-10 00:07:50 +0000407static PyDictEntry *
408lookdict_string(PyDictObject *mp, PyObject *key, register long hash)
Fred Drake1bff34a2000-08-31 19:31:38 +0000409{
Antoine Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +0000417 /* Make sure this function doesn't have to handle non-string keys,
418 including subclasses of str; e.g., one reason to subclass
419 strings is to override __eq__, and for speed we don't cater to
420 that here. */
421 if (!PyString_CheckExact(key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000422#ifdef SHOW_CONVERSION_COUNTS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000423 ++converted;
Fred Drake1bff34a2000-08-31 19:31:38 +0000424#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000425 mp->ma_lookup = lookdict;
426 return lookdict(mp, key, hash);
427 }
428 i = hash & mask;
429 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 && _PyString_Eq(ep->me_key, key))
436 return ep;
437 freeslot = NULL;
438 }
Tim Peters15d49292001-05-27 07:39:22 +0000439
Antoine Pitrouc83ea132010-05-09 14:46:46 +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 && _PyString_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
Antoine Pitrouf8387af2009-03-23 18:41:45 +0000459#ifdef SHOW_TRACK_COUNT
460#define INCREASE_TRACK_COUNT \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000461 (count_tracked++, count_untracked--);
Antoine Pitrouf8387af2009-03-23 18:41:45 +0000462#define DECREASE_TRACK_COUNT \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000463 (count_tracked--, count_untracked++);
Antoine Pitrouf8387af2009-03-23 18:41:45 +0000464#else
465#define INCREASE_TRACK_COUNT
466#define DECREASE_TRACK_COUNT
467#endif
468
469#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000470 do { \
471 if (!_PyObject_GC_IS_TRACKED(mp)) { \
472 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
473 _PyObject_GC_MAY_BE_TRACKED(value)) { \
474 _PyObject_GC_TRACK(mp); \
475 INCREASE_TRACK_COUNT \
476 } \
477 } \
478 } while(0)
Antoine Pitrouf8387af2009-03-23 18:41:45 +0000479
480void
481_PyDict_MaybeUntrack(PyObject *op)
482{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000483 PyDictObject *mp;
484 PyObject *value;
485 Py_ssize_t mask, i;
486 PyDictEntry *ep;
Antoine Pitrouf8387af2009-03-23 18:41:45 +0000487
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000488 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
489 return;
490
491 mp = (PyDictObject *) op;
492 ep = mp->ma_table;
493 mask = mp->ma_mask;
494 for (i = 0; i <= mask; i++) {
495 if ((value = ep[i].me_value) == NULL)
496 continue;
497 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
498 _PyObject_GC_MAY_BE_TRACKED(ep[i].me_key))
499 return;
500 }
501 DECREASE_TRACK_COUNT
502 _PyObject_GC_UNTRACK(op);
Antoine Pitrouf8387af2009-03-23 18:41:45 +0000503}
504
Fred Drake1bff34a2000-08-31 19:31:38 +0000505/*
Antoine Pitrou6a1cd1b2012-02-27 00:45:12 +0100506Internal routine to insert a new item into the table when you have entry object.
507Used by insertdict.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000508*/
Armin Rigo35f6d362006-06-01 13:19:12 +0000509static int
Antoine Pitrou6a1cd1b2012-02-27 00:45:12 +0100510insertdict_by_entry(register PyDictObject *mp, PyObject *key, long hash,
511 PyDictEntry *ep, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000512{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000513 PyObject *old_value;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000514
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000515 MAINTAIN_TRACKING(mp, key, value);
516 if (ep->me_value != NULL) {
517 old_value = ep->me_value;
518 ep->me_value = value;
519 Py_DECREF(old_value); /* which **CAN** re-enter */
520 Py_DECREF(key);
521 }
522 else {
523 if (ep->me_key == NULL)
524 mp->ma_fill++;
525 else {
526 assert(ep->me_key == dummy);
527 Py_DECREF(dummy);
528 }
529 ep->me_key = key;
530 ep->me_hash = (Py_ssize_t)hash;
531 ep->me_value = value;
532 mp->ma_used++;
533 }
534 return 0;
Armin Rigo35f6d362006-06-01 13:19:12 +0000535}
536
Antoine Pitrou6a1cd1b2012-02-27 00:45:12 +0100537
538/*
539Internal routine to insert a new item into the table.
540Used both by the internal resize routine and by the public insert routine.
541Eats a reference to key and one to value.
542Returns -1 if an error occurred, or 0 on success.
543*/
544static int
545insertdict(register PyDictObject *mp, PyObject *key, long hash, PyObject *value)
546{
547 register PyDictEntry *ep;
548
549 assert(mp->ma_lookup != NULL);
550 ep = mp->ma_lookup(mp, key, hash);
551 if (ep == NULL) {
552 Py_DECREF(key);
553 Py_DECREF(value);
554 return -1;
555 }
556 return insertdict_by_entry(mp, key, hash, ep, value);
557}
558
Armin Rigo35f6d362006-06-01 13:19:12 +0000559/*
560Internal routine used by dictresize() to insert an item which is
561known to be absent from the dict. This routine also assumes that
562the dict contains no deleted entries. Besides the performance benefit,
563using insertdict() in dictresize() is dangerous (SF bug #1456209).
Tim Petersd770ebd2006-06-01 15:50:44 +0000564Note that no refcounts are changed by this routine; if needed, the caller
565is responsible for incref'ing `key` and `value`.
Armin Rigo35f6d362006-06-01 13:19:12 +0000566*/
567static void
Brett Cannon77ae87c2007-10-10 00:07:50 +0000568insertdict_clean(register PyDictObject *mp, PyObject *key, long hash,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000569 PyObject *value)
Armin Rigo35f6d362006-06-01 13:19:12 +0000570{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000571 register size_t i;
572 register size_t perturb;
573 register size_t mask = (size_t)mp->ma_mask;
574 PyDictEntry *ep0 = mp->ma_table;
575 register PyDictEntry *ep;
Armin Rigo35f6d362006-06-01 13:19:12 +0000576
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000577 MAINTAIN_TRACKING(mp, key, value);
578 i = hash & mask;
579 ep = &ep0[i];
580 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
581 i = (i << 2) + i + perturb + 1;
582 ep = &ep0[i & mask];
583 }
584 assert(ep->me_value == NULL);
585 mp->ma_fill++;
586 ep->me_key = key;
587 ep->me_hash = (Py_ssize_t)hash;
588 ep->me_value = value;
589 mp->ma_used++;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000590}
591
592/*
593Restructure the table by allocating a new table and reinserting all
594items again. When entries have been deleted, the new table may
595actually be smaller than the old one.
596*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000597static int
Brett Cannon77ae87c2007-10-10 00:07:50 +0000598dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000599{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000600 Py_ssize_t newsize;
601 PyDictEntry *oldtable, *newtable, *ep;
602 Py_ssize_t i;
603 int is_oldtable_malloced;
604 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000605
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000606 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000607
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000608 /* Find the smallest table size > minused. */
609 for (newsize = PyDict_MINSIZE;
610 newsize <= minused && newsize > 0;
611 newsize <<= 1)
612 ;
613 if (newsize <= 0) {
614 PyErr_NoMemory();
615 return -1;
616 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000617
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000618 /* Get space for a new table. */
619 oldtable = mp->ma_table;
620 assert(oldtable != NULL);
621 is_oldtable_malloced = oldtable != mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000622
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000623 if (newsize == PyDict_MINSIZE) {
624 /* A large table is shrinking, or we can't get any smaller. */
625 newtable = mp->ma_smalltable;
626 if (newtable == oldtable) {
627 if (mp->ma_fill == mp->ma_used) {
628 /* No dummies, so no point doing anything. */
629 return 0;
630 }
631 /* We're not going to resize it, but rebuild the
632 table anyway to purge old dummy entries.
633 Subtle: This is *necessary* if fill==size,
634 as lookdict needs at least one virgin slot to
635 terminate failing searches. If fill < size, it's
636 merely desirable, as dummies slow searches. */
637 assert(mp->ma_fill > mp->ma_used);
638 memcpy(small_copy, oldtable, sizeof(small_copy));
639 oldtable = small_copy;
640 }
641 }
642 else {
643 newtable = PyMem_NEW(PyDictEntry, newsize);
644 if (newtable == NULL) {
645 PyErr_NoMemory();
646 return -1;
647 }
648 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000649
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000650 /* Make the dict empty, using the new table. */
651 assert(newtable != oldtable);
652 mp->ma_table = newtable;
653 mp->ma_mask = newsize - 1;
654 memset(newtable, 0, sizeof(PyDictEntry) * newsize);
655 mp->ma_used = 0;
656 i = mp->ma_fill;
657 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000658
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000659 /* Copy the data over; this is refcount-neutral for active entries;
660 dummy entries aren't copied over, of course */
661 for (ep = oldtable; i > 0; ep++) {
662 if (ep->me_value != NULL) { /* active entry */
663 --i;
664 insertdict_clean(mp, ep->me_key, (long)ep->me_hash,
665 ep->me_value);
666 }
667 else if (ep->me_key != NULL) { /* dummy entry */
668 --i;
669 assert(ep->me_key == dummy);
670 Py_DECREF(ep->me_key);
671 }
672 /* else key == value == NULL: nothing to do */
673 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000674
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000675 if (is_oldtable_malloced)
676 PyMem_DEL(oldtable);
677 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000678}
679
Raymond Hettingerfd7ed402007-12-18 21:24:09 +0000680/* Create a new dictionary pre-sized to hold an estimated number of elements.
681 Underestimates are okay because the dictionary will resize as necessary.
682 Overestimates just mean the dictionary will be more sparse than usual.
683*/
684
685PyObject *
686_PyDict_NewPresized(Py_ssize_t minused)
687{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000688 PyObject *op = PyDict_New();
Raymond Hettingerfd7ed402007-12-18 21:24:09 +0000689
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000690 if (minused>5 && op != NULL && dictresize((PyDictObject *)op, minused) == -1) {
691 Py_DECREF(op);
692 return NULL;
693 }
694 return op;
Raymond Hettingerfd7ed402007-12-18 21:24:09 +0000695}
696
Tim Petersd770ebd2006-06-01 15:50:44 +0000697/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
698 * that may occur (originally dicts supported only string keys, and exceptions
699 * weren't possible). So, while the original intent was that a NULL return
Andrew M. Kuchling0067b5f2006-08-04 20:37:43 +0000700 * meant the key wasn't present, in reality it can mean that, or that an error
Tim Petersd770ebd2006-06-01 15:50:44 +0000701 * (suppressed) occurred while computing the key's hash, or that some error
702 * (suppressed) occurred when comparing keys in the dict's internal probe
703 * sequence. A nasty example of the latter is when a Python-coded comparison
704 * function hits a stack-depth error, which can cause this to return NULL
705 * even if the key is present.
706 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000707PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000708PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000709{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000710 long hash;
711 PyDictObject *mp = (PyDictObject *)op;
712 PyDictEntry *ep;
713 PyThreadState *tstate;
714 if (!PyDict_Check(op))
715 return NULL;
716 if (!PyString_CheckExact(key) ||
717 (hash = ((PyStringObject *) key)->ob_shash) == -1)
718 {
719 hash = PyObject_Hash(key);
720 if (hash == -1) {
721 PyErr_Clear();
722 return NULL;
723 }
724 }
Armin Rigo35f6d362006-06-01 13:19:12 +0000725
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000726 /* We can arrive here with a NULL tstate during initialization: try
727 running "python -Wi" for an example related to string interning.
728 Let's just hope that no exception occurs then... This must be
729 _PyThreadState_Current and not PyThreadState_GET() because in debug
730 mode, the latter complains if tstate is NULL. */
731 tstate = _PyThreadState_Current;
732 if (tstate != NULL && tstate->curexc_type != NULL) {
733 /* preserve the existing exception */
734 PyObject *err_type, *err_value, *err_tb;
735 PyErr_Fetch(&err_type, &err_value, &err_tb);
736 ep = (mp->ma_lookup)(mp, key, hash);
737 /* ignore errors */
738 PyErr_Restore(err_type, err_value, err_tb);
739 if (ep == NULL)
740 return NULL;
741 }
742 else {
743 ep = (mp->ma_lookup)(mp, key, hash);
744 if (ep == NULL) {
745 PyErr_Clear();
746 return NULL;
747 }
748 }
749 return ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000750}
751
Antoine Pitrou6a1cd1b2012-02-27 00:45:12 +0100752static int
753dict_set_item_by_hash_or_entry(register PyObject *op, PyObject *key,
754 long hash, PyDictEntry *ep, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000755{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000756 register PyDictObject *mp;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000757 register Py_ssize_t n_used;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000758
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000759 mp = (PyDictObject *)op;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000760 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
761 n_used = mp->ma_used;
762 Py_INCREF(value);
763 Py_INCREF(key);
Antoine Pitrou6a1cd1b2012-02-27 00:45:12 +0100764 if (ep == NULL) {
765 if (insertdict(mp, key, hash, value) != 0)
766 return -1;
767 }
768 else {
769 if (insertdict_by_entry(mp, key, hash, ep, value) != 0)
770 return -1;
771 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000772 /* If we added a key, we can safely resize. Otherwise just return!
773 * If fill >= 2/3 size, adjust size. Normally, this doubles or
774 * quaduples the size, but it's also possible for the dict to shrink
775 * (if ma_fill is much larger than ma_used, meaning a lot of dict
776 * keys have been * deleted).
777 *
778 * Quadrupling the size improves average dictionary sparseness
779 * (reducing collisions) at the cost of some memory and iteration
780 * speed (which loops over every possible entry). It also halves
781 * the number of expensive resize operations in a growing dictionary.
782 *
783 * Very large dictionaries (over 50K items) use doubling instead.
784 * This may help applications with severe memory constraints.
785 */
786 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
787 return 0;
788 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000789}
790
Antoine Pitrou6a1cd1b2012-02-27 00:45:12 +0100791/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
792 * dictionary if it's merely replacing the value for an existing key.
793 * This means that it's safe to loop over a dictionary with PyDict_Next()
794 * and occasionally replace a value -- but you can't insert new keys or
795 * remove them.
796 */
797int
798PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
799{
800 register long hash;
801
802 if (!PyDict_Check(op)) {
803 PyErr_BadInternalCall();
804 return -1;
805 }
806 assert(key);
807 assert(value);
808 if (PyString_CheckExact(key)) {
809 hash = ((PyStringObject *)key)->ob_shash;
810 if (hash == -1)
811 hash = PyObject_Hash(key);
812 }
813 else {
814 hash = PyObject_Hash(key);
815 if (hash == -1)
816 return -1;
817 }
818 return dict_set_item_by_hash_or_entry(op, key, hash, NULL, value);
819}
820
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000821int
Tim Peters1f5871e2000-07-04 17:44:48 +0000822PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000823{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000824 register PyDictObject *mp;
825 register long hash;
826 register PyDictEntry *ep;
827 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000828
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000829 if (!PyDict_Check(op)) {
830 PyErr_BadInternalCall();
831 return -1;
832 }
833 assert(key);
834 if (!PyString_CheckExact(key) ||
835 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
836 hash = PyObject_Hash(key);
837 if (hash == -1)
838 return -1;
839 }
840 mp = (PyDictObject *)op;
841 ep = (mp->ma_lookup)(mp, key, hash);
842 if (ep == NULL)
843 return -1;
844 if (ep->me_value == NULL) {
845 set_key_error(key);
846 return -1;
847 }
848 old_key = ep->me_key;
849 Py_INCREF(dummy);
850 ep->me_key = dummy;
851 old_value = ep->me_value;
852 ep->me_value = NULL;
853 mp->ma_used--;
854 Py_DECREF(old_value);
855 Py_DECREF(old_key);
856 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000857}
858
Guido van Rossum25831651993-05-19 14:50:45 +0000859void
Tim Peters1f5871e2000-07-04 17:44:48 +0000860PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000861{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000862 PyDictObject *mp;
863 PyDictEntry *ep, *table;
864 int table_is_malloced;
865 Py_ssize_t fill;
866 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000867#ifdef Py_DEBUG
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000868 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +0000869#endif
870
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000871 if (!PyDict_Check(op))
872 return;
873 mp = (PyDictObject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000874#ifdef Py_DEBUG
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000875 n = mp->ma_mask + 1;
876 i = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000877#endif
878
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000879 table = mp->ma_table;
880 assert(table != NULL);
881 table_is_malloced = table != mp->ma_smalltable;
Tim Petersdea48ec2001-05-22 20:40:22 +0000882
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000883 /* This is delicate. During the process of clearing the dict,
884 * decrefs can cause the dict to mutate. To avoid fatal confusion
885 * (voice of experience), we have to make the dict empty before
886 * clearing the slots, and never refer to anything via mp->xxx while
887 * clearing.
888 */
889 fill = mp->ma_fill;
890 if (table_is_malloced)
891 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000892
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000893 else if (fill > 0) {
894 /* It's a small table with something that needs to be cleared.
895 * Afraid the only safe way is to copy the dict entries into
896 * another small table first.
897 */
898 memcpy(small_copy, table, sizeof(small_copy));
899 table = small_copy;
900 EMPTY_TO_MINSIZE(mp);
901 }
902 /* else it's a small table that's already empty */
Tim Petersdea48ec2001-05-22 20:40:22 +0000903
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000904 /* Now we can finally clear things. If C had refcounts, we could
905 * assert that the refcount on table is 1 now, i.e. that this function
906 * has unique access to it, so decref side-effects can't alter it.
907 */
908 for (ep = table; fill > 0; ++ep) {
Tim Petersdea48ec2001-05-22 20:40:22 +0000909#ifdef Py_DEBUG
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000910 assert(i < n);
911 ++i;
Tim Petersdea48ec2001-05-22 20:40:22 +0000912#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000913 if (ep->me_key) {
914 --fill;
915 Py_DECREF(ep->me_key);
916 Py_XDECREF(ep->me_value);
917 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000918#ifdef Py_DEBUG
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000919 else
920 assert(ep->me_value == NULL);
Tim Petersdea48ec2001-05-22 20:40:22 +0000921#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000922 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000923
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000924 if (table_is_malloced)
925 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000926}
927
Tim Peters080c88b2003-02-15 03:01:11 +0000928/*
929 * Iterate over a dict. Use like so:
930 *
Tim Peters9b10f7e2006-05-30 04:16:25 +0000931 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +0000932 * PyObject *key, *value;
933 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +0000934 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +0000935 * Refer to borrowed references in key and value.
936 * }
937 *
938 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +0000939 * mutates the dict. One exception: it is safe if the loop merely changes
940 * the values associated with the keys (but doesn't insert new keys or
941 * delete keys), via PyDict_SetItem().
942 */
Guido van Rossum25831651993-05-19 14:50:45 +0000943int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000944PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000945{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000946 register Py_ssize_t i;
947 register Py_ssize_t mask;
948 register PyDictEntry *ep;
Raymond Hettinger43442782004-03-17 21:55:03 +0000949
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000950 if (!PyDict_Check(op))
951 return 0;
952 i = *ppos;
953 if (i < 0)
954 return 0;
955 ep = ((PyDictObject *)op)->ma_table;
956 mask = ((PyDictObject *)op)->ma_mask;
957 while (i <= mask && ep[i].me_value == NULL)
958 i++;
959 *ppos = i+1;
960 if (i > mask)
961 return 0;
962 if (pkey)
963 *pkey = ep[i].me_key;
964 if (pvalue)
965 *pvalue = ep[i].me_value;
966 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000967}
968
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000969/* Internal version of PyDict_Next that returns a hash value in addition to the key and value.*/
970int
971_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, long *phash)
972{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000973 register Py_ssize_t i;
974 register Py_ssize_t mask;
975 register PyDictEntry *ep;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000976
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000977 if (!PyDict_Check(op))
978 return 0;
979 i = *ppos;
980 if (i < 0)
981 return 0;
982 ep = ((PyDictObject *)op)->ma_table;
983 mask = ((PyDictObject *)op)->ma_mask;
984 while (i <= mask && ep[i].me_value == NULL)
985 i++;
986 *ppos = i+1;
987 if (i > mask)
988 return 0;
989 *phash = (long)(ep[i].me_hash);
990 if (pkey)
991 *pkey = ep[i].me_key;
992 if (pvalue)
993 *pvalue = ep[i].me_value;
994 return 1;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000995}
996
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000997/* Methods */
998
999static void
Brett Cannon77ae87c2007-10-10 00:07:50 +00001000dict_dealloc(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001001{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001002 register PyDictEntry *ep;
1003 Py_ssize_t fill = mp->ma_fill;
1004 PyObject_GC_UnTrack(mp);
1005 Py_TRASHCAN_SAFE_BEGIN(mp)
1006 for (ep = mp->ma_table; fill > 0; ep++) {
1007 if (ep->me_key) {
1008 --fill;
1009 Py_DECREF(ep->me_key);
1010 Py_XDECREF(ep->me_value);
1011 }
1012 }
1013 if (mp->ma_table != mp->ma_smalltable)
1014 PyMem_DEL(mp->ma_table);
1015 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1016 free_list[numfree++] = mp;
1017 else
1018 Py_TYPE(mp)->tp_free((PyObject *)mp);
1019 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001020}
1021
1022static int
Brett Cannon77ae87c2007-10-10 00:07:50 +00001023dict_print(register PyDictObject *mp, register FILE *fp, register int flags)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001024{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001025 register Py_ssize_t i;
1026 register Py_ssize_t any;
1027 int status;
Guido van Rossum255443b1998-04-10 22:47:14 +00001028
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001029 status = Py_ReprEnter((PyObject*)mp);
1030 if (status != 0) {
1031 if (status < 0)
1032 return status;
1033 Py_BEGIN_ALLOW_THREADS
1034 fprintf(fp, "{...}");
1035 Py_END_ALLOW_THREADS
1036 return 0;
1037 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001038
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001039 Py_BEGIN_ALLOW_THREADS
1040 fprintf(fp, "{");
1041 Py_END_ALLOW_THREADS
1042 any = 0;
1043 for (i = 0; i <= mp->ma_mask; i++) {
1044 PyDictEntry *ep = mp->ma_table + i;
1045 PyObject *pvalue = ep->me_value;
1046 if (pvalue != NULL) {
1047 /* Prevent PyObject_Repr from deleting value during
1048 key format */
1049 Py_INCREF(pvalue);
1050 if (any++ > 0) {
1051 Py_BEGIN_ALLOW_THREADS
1052 fprintf(fp, ", ");
1053 Py_END_ALLOW_THREADS
1054 }
1055 if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
1056 Py_DECREF(pvalue);
1057 Py_ReprLeave((PyObject*)mp);
1058 return -1;
1059 }
1060 Py_BEGIN_ALLOW_THREADS
1061 fprintf(fp, ": ");
1062 Py_END_ALLOW_THREADS
1063 if (PyObject_Print(pvalue, fp, 0) != 0) {
1064 Py_DECREF(pvalue);
1065 Py_ReprLeave((PyObject*)mp);
1066 return -1;
1067 }
1068 Py_DECREF(pvalue);
1069 }
1070 }
1071 Py_BEGIN_ALLOW_THREADS
1072 fprintf(fp, "}");
1073 Py_END_ALLOW_THREADS
1074 Py_ReprLeave((PyObject*)mp);
1075 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001076}
1077
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001078static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001079dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001080{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001081 Py_ssize_t i;
1082 PyObject *s, *temp, *colon = NULL;
1083 PyObject *pieces = NULL, *result = NULL;
1084 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +00001085
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001086 i = Py_ReprEnter((PyObject *)mp);
1087 if (i != 0) {
1088 return i > 0 ? PyString_FromString("{...}") : NULL;
1089 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001090
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001091 if (mp->ma_used == 0) {
1092 result = PyString_FromString("{}");
1093 goto Done;
1094 }
Tim Petersa7259592001-06-16 05:11:17 +00001095
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001096 pieces = PyList_New(0);
1097 if (pieces == NULL)
1098 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001099
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001100 colon = PyString_FromString(": ");
1101 if (colon == NULL)
1102 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001103
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001104 /* Do repr() on each key+value pair, and insert ": " between them.
1105 Note that repr may mutate the dict. */
1106 i = 0;
1107 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
1108 int status;
1109 /* Prevent repr from deleting value during key format. */
1110 Py_INCREF(value);
1111 s = PyObject_Repr(key);
1112 PyString_Concat(&s, colon);
1113 PyString_ConcatAndDel(&s, PyObject_Repr(value));
1114 Py_DECREF(value);
1115 if (s == NULL)
1116 goto Done;
1117 status = PyList_Append(pieces, s);
1118 Py_DECREF(s); /* append created a new ref */
1119 if (status < 0)
1120 goto Done;
1121 }
Tim Petersa7259592001-06-16 05:11:17 +00001122
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001123 /* Add "{}" decorations to the first and last items. */
1124 assert(PyList_GET_SIZE(pieces) > 0);
1125 s = PyString_FromString("{");
1126 if (s == NULL)
1127 goto Done;
1128 temp = PyList_GET_ITEM(pieces, 0);
1129 PyString_ConcatAndDel(&s, temp);
1130 PyList_SET_ITEM(pieces, 0, s);
1131 if (s == NULL)
1132 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001133
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001134 s = PyString_FromString("}");
1135 if (s == NULL)
1136 goto Done;
1137 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
1138 PyString_ConcatAndDel(&temp, s);
1139 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
1140 if (temp == NULL)
1141 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001142
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001143 /* Paste them all together with ", " between. */
1144 s = PyString_FromString(", ");
1145 if (s == NULL)
1146 goto Done;
1147 result = _PyString_Join(s, pieces);
1148 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +00001149
1150Done:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001151 Py_XDECREF(pieces);
1152 Py_XDECREF(colon);
1153 Py_ReprLeave((PyObject *)mp);
1154 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001155}
1156
Martin v. Löwis18e16552006-02-15 17:27:45 +00001157static Py_ssize_t
Brett Cannon77ae87c2007-10-10 00:07:50 +00001158dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001159{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001160 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001161}
1162
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001163static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001164dict_subscript(PyDictObject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001165{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001166 PyObject *v;
1167 long hash;
1168 PyDictEntry *ep;
1169 assert(mp->ma_table != NULL);
1170 if (!PyString_CheckExact(key) ||
1171 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1172 hash = PyObject_Hash(key);
1173 if (hash == -1)
1174 return NULL;
1175 }
1176 ep = (mp->ma_lookup)(mp, key, hash);
1177 if (ep == NULL)
1178 return NULL;
1179 v = ep->me_value;
1180 if (v == NULL) {
1181 if (!PyDict_CheckExact(mp)) {
1182 /* Look up __missing__ method if we're a subclass. */
1183 PyObject *missing, *res;
1184 static PyObject *missing_str = NULL;
1185 missing = _PyObject_LookupSpecial((PyObject *)mp,
1186 "__missing__",
1187 &missing_str);
1188 if (missing != NULL) {
1189 res = PyObject_CallFunctionObjArgs(missing,
1190 key, NULL);
1191 Py_DECREF(missing);
1192 return res;
1193 }
1194 else if (PyErr_Occurred())
1195 return NULL;
1196 }
1197 set_key_error(key);
1198 return NULL;
1199 }
1200 else
1201 Py_INCREF(v);
1202 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001203}
1204
1205static int
Brett Cannon77ae87c2007-10-10 00:07:50 +00001206dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001207{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001208 if (w == NULL)
1209 return PyDict_DelItem((PyObject *)mp, v);
1210 else
1211 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001212}
1213
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001214static PyMappingMethods dict_as_mapping = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001215 (lenfunc)dict_length, /*mp_length*/
1216 (binaryfunc)dict_subscript, /*mp_subscript*/
1217 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001218};
1219
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001220static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001221dict_keys(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001222{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001223 register PyObject *v;
1224 register Py_ssize_t i, j;
1225 PyDictEntry *ep;
1226 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001227
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001228 again:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001229 n = mp->ma_used;
1230 v = PyList_New(n);
1231 if (v == NULL)
1232 return NULL;
1233 if (n != mp->ma_used) {
1234 /* Durnit. The allocations caused the dict to resize.
1235 * Just start over, this shouldn't normally happen.
1236 */
1237 Py_DECREF(v);
1238 goto again;
1239 }
1240 ep = mp->ma_table;
1241 mask = mp->ma_mask;
1242 for (i = 0, j = 0; i <= mask; i++) {
1243 if (ep[i].me_value != NULL) {
1244 PyObject *key = ep[i].me_key;
1245 Py_INCREF(key);
1246 PyList_SET_ITEM(v, j, key);
1247 j++;
1248 }
1249 }
1250 assert(j == n);
1251 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001252}
1253
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001254static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001255dict_values(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001256{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001257 register PyObject *v;
1258 register Py_ssize_t i, j;
1259 PyDictEntry *ep;
1260 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001261
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001262 again:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001263 n = mp->ma_used;
1264 v = PyList_New(n);
1265 if (v == NULL)
1266 return NULL;
1267 if (n != mp->ma_used) {
1268 /* Durnit. The allocations caused the dict to resize.
1269 * Just start over, this shouldn't normally happen.
1270 */
1271 Py_DECREF(v);
1272 goto again;
1273 }
1274 ep = mp->ma_table;
1275 mask = mp->ma_mask;
1276 for (i = 0, j = 0; i <= mask; i++) {
1277 if (ep[i].me_value != NULL) {
1278 PyObject *value = ep[i].me_value;
1279 Py_INCREF(value);
1280 PyList_SET_ITEM(v, j, value);
1281 j++;
1282 }
1283 }
1284 assert(j == n);
1285 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001286}
1287
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001288static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001289dict_items(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001290{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001291 register PyObject *v;
1292 register Py_ssize_t i, j, n;
1293 Py_ssize_t mask;
1294 PyObject *item, *key, *value;
1295 PyDictEntry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001296
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001297 /* Preallocate the list of tuples, to avoid allocations during
1298 * the loop over the items, which could trigger GC, which
1299 * could resize the dict. :-(
1300 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001301 again:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001302 n = mp->ma_used;
1303 v = PyList_New(n);
1304 if (v == NULL)
1305 return NULL;
1306 for (i = 0; i < n; i++) {
1307 item = PyTuple_New(2);
1308 if (item == NULL) {
1309 Py_DECREF(v);
1310 return NULL;
1311 }
1312 PyList_SET_ITEM(v, i, item);
1313 }
1314 if (n != mp->ma_used) {
1315 /* Durnit. The allocations caused the dict to resize.
1316 * Just start over, this shouldn't normally happen.
1317 */
1318 Py_DECREF(v);
1319 goto again;
1320 }
1321 /* Nothing we do below makes any function calls. */
1322 ep = mp->ma_table;
1323 mask = mp->ma_mask;
1324 for (i = 0, j = 0; i <= mask; i++) {
1325 if ((value=ep[i].me_value) != NULL) {
1326 key = ep[i].me_key;
1327 item = PyList_GET_ITEM(v, j);
1328 Py_INCREF(key);
1329 PyTuple_SET_ITEM(item, 0, key);
1330 Py_INCREF(value);
1331 PyTuple_SET_ITEM(item, 1, value);
1332 j++;
1333 }
1334 }
1335 assert(j == n);
1336 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001337}
1338
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001339static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001340dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001341{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001342 PyObject *seq;
1343 PyObject *value = Py_None;
1344 PyObject *it; /* iter(seq) */
1345 PyObject *key;
1346 PyObject *d;
1347 int status;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001348
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001349 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
1350 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001351
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001352 d = PyObject_CallObject(cls, NULL);
1353 if (d == NULL)
1354 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001355
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001356 if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) {
1357 PyDictObject *mp = (PyDictObject *)d;
1358 PyObject *oldvalue;
1359 Py_ssize_t pos = 0;
1360 PyObject *key;
1361 long hash;
Raymond Hettingercdcf8872007-11-07 02:26:17 +00001362
Petri Lehtinen8ffbab82011-10-24 20:59:29 +03001363 if (dictresize(mp, Py_SIZE(seq))) {
1364 Py_DECREF(d);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001365 return NULL;
Petri Lehtinen8ffbab82011-10-24 20:59:29 +03001366 }
Raymond Hettingercdcf8872007-11-07 02:26:17 +00001367
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001368 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
1369 Py_INCREF(key);
1370 Py_INCREF(value);
Petri Lehtinen8ffbab82011-10-24 20:59:29 +03001371 if (insertdict(mp, key, hash, value)) {
1372 Py_DECREF(d);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001373 return NULL;
Petri Lehtinen8ffbab82011-10-24 20:59:29 +03001374 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001375 }
1376 return d;
1377 }
Raymond Hettingercdcf8872007-11-07 02:26:17 +00001378
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001379 if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
1380 PyDictObject *mp = (PyDictObject *)d;
1381 Py_ssize_t pos = 0;
1382 PyObject *key;
1383 long hash;
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00001384
Petri Lehtinen8ffbab82011-10-24 20:59:29 +03001385 if (dictresize(mp, PySet_GET_SIZE(seq))) {
1386 Py_DECREF(d);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001387 return NULL;
Petri Lehtinen8ffbab82011-10-24 20:59:29 +03001388 }
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00001389
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001390 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
1391 Py_INCREF(key);
1392 Py_INCREF(value);
Petri Lehtinen8ffbab82011-10-24 20:59:29 +03001393 if (insertdict(mp, key, hash, value)) {
1394 Py_DECREF(d);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001395 return NULL;
Petri Lehtinen8ffbab82011-10-24 20:59:29 +03001396 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001397 }
1398 return d;
1399 }
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00001400
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001401 it = PyObject_GetIter(seq);
1402 if (it == NULL){
1403 Py_DECREF(d);
1404 return NULL;
1405 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001406
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001407 if (PyDict_CheckExact(d)) {
1408 while ((key = PyIter_Next(it)) != NULL) {
1409 status = PyDict_SetItem(d, key, value);
1410 Py_DECREF(key);
1411 if (status < 0)
1412 goto Fail;
1413 }
1414 } else {
1415 while ((key = PyIter_Next(it)) != NULL) {
1416 status = PyObject_SetItem(d, key, value);
1417 Py_DECREF(key);
1418 if (status < 0)
1419 goto Fail;
1420 }
1421 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001422
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001423 if (PyErr_Occurred())
1424 goto Fail;
1425 Py_DECREF(it);
1426 return d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001427
1428Fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001429 Py_DECREF(it);
1430 Py_DECREF(d);
1431 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001432}
1433
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001434static int
1435dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001436{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001437 PyObject *arg = NULL;
1438 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001439
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001440 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1441 result = -1;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001442
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001443 else if (arg != NULL) {
1444 if (PyObject_HasAttrString(arg, "keys"))
1445 result = PyDict_Merge(self, arg, 1);
1446 else
1447 result = PyDict_MergeFromSeq2(self, arg, 1);
1448 }
1449 if (result == 0 && kwds != NULL)
1450 result = PyDict_Merge(self, kwds, 1);
1451 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001452}
1453
1454static PyObject *
1455dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1456{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001457 if (dict_update_common(self, args, kwds, "update") != -1)
1458 Py_RETURN_NONE;
1459 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001460}
1461
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001462/* Update unconditionally replaces existing items.
1463 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001464 otherwise it leaves existing items unchanged.
1465
1466 PyDict_{Update,Merge} update/merge from a mapping object.
1467
Tim Petersf582b822001-12-11 18:51:08 +00001468 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001469 producing iterable objects of length 2.
1470*/
1471
Tim Petersf582b822001-12-11 18:51:08 +00001472int
Tim Peters1fc240e2001-10-26 05:06:50 +00001473PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1474{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001475 PyObject *it; /* iter(seq2) */
1476 Py_ssize_t i; /* index into seq2 of current element */
1477 PyObject *item; /* seq2[i] */
1478 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00001479
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001480 assert(d != NULL);
1481 assert(PyDict_Check(d));
1482 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00001483
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001484 it = PyObject_GetIter(seq2);
1485 if (it == NULL)
1486 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001487
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001488 for (i = 0; ; ++i) {
1489 PyObject *key, *value;
1490 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001491
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001492 fast = NULL;
1493 item = PyIter_Next(it);
1494 if (item == NULL) {
1495 if (PyErr_Occurred())
1496 goto Fail;
1497 break;
1498 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001499
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001500 /* Convert item to sequence, and verify length 2. */
1501 fast = PySequence_Fast(item, "");
1502 if (fast == NULL) {
1503 if (PyErr_ExceptionMatches(PyExc_TypeError))
1504 PyErr_Format(PyExc_TypeError,
1505 "cannot convert dictionary update "
1506 "sequence element #%zd to a sequence",
1507 i);
1508 goto Fail;
1509 }
1510 n = PySequence_Fast_GET_SIZE(fast);
1511 if (n != 2) {
1512 PyErr_Format(PyExc_ValueError,
1513 "dictionary update sequence element #%zd "
1514 "has length %zd; 2 is required",
1515 i, n);
1516 goto Fail;
1517 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001518
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001519 /* Update/merge with this (key, value) pair. */
1520 key = PySequence_Fast_GET_ITEM(fast, 0);
1521 value = PySequence_Fast_GET_ITEM(fast, 1);
1522 if (override || PyDict_GetItem(d, key) == NULL) {
1523 int status = PyDict_SetItem(d, key, value);
1524 if (status < 0)
1525 goto Fail;
1526 }
1527 Py_DECREF(fast);
1528 Py_DECREF(item);
1529 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001530
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001531 i = 0;
1532 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00001533Fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001534 Py_XDECREF(item);
1535 Py_XDECREF(fast);
1536 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001537Return:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001538 Py_DECREF(it);
1539 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001540}
1541
Tim Peters6d6c1a32001-08-02 04:15:00 +00001542int
1543PyDict_Update(PyObject *a, PyObject *b)
1544{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001545 return PyDict_Merge(a, b, 1);
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001546}
1547
1548int
1549PyDict_Merge(PyObject *a, PyObject *b, int override)
1550{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001551 register PyDictObject *mp, *other;
1552 register Py_ssize_t i;
1553 PyDictEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001554
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001555 /* We accept for the argument either a concrete dictionary object,
1556 * or an abstract "mapping" object. For the former, we can do
1557 * things quite efficiently. For the latter, we only require that
1558 * PyMapping_Keys() and PyObject_GetItem() be supported.
1559 */
1560 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1561 PyErr_BadInternalCall();
1562 return -1;
1563 }
1564 mp = (PyDictObject*)a;
1565 if (PyDict_Check(b)) {
1566 other = (PyDictObject*)b;
1567 if (other == mp || other->ma_used == 0)
1568 /* a.update(a) or a.update({}); nothing to do */
1569 return 0;
1570 if (mp->ma_used == 0)
1571 /* Since the target dict is empty, PyDict_GetItem()
1572 * always returns NULL. Setting override to 1
1573 * skips the unnecessary test.
1574 */
1575 override = 1;
1576 /* Do one big resize at the start, rather than
1577 * incrementally resizing as we insert new items. Expect
1578 * that there will be no (or few) overlapping keys.
1579 */
1580 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
1581 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
1582 return -1;
1583 }
1584 for (i = 0; i <= other->ma_mask; i++) {
1585 entry = &other->ma_table[i];
1586 if (entry->me_value != NULL &&
1587 (override ||
1588 PyDict_GetItem(a, entry->me_key) == NULL)) {
1589 Py_INCREF(entry->me_key);
1590 Py_INCREF(entry->me_value);
1591 if (insertdict(mp, entry->me_key,
1592 (long)entry->me_hash,
1593 entry->me_value) != 0)
1594 return -1;
1595 }
1596 }
1597 }
1598 else {
1599 /* Do it the generic, slower way */
1600 PyObject *keys = PyMapping_Keys(b);
1601 PyObject *iter;
1602 PyObject *key, *value;
1603 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001604
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001605 if (keys == NULL)
1606 /* Docstring says this is equivalent to E.keys() so
1607 * if E doesn't have a .keys() method we want
1608 * AttributeError to percolate up. Might as well
1609 * do the same for any other error.
1610 */
1611 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001612
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001613 iter = PyObject_GetIter(keys);
1614 Py_DECREF(keys);
1615 if (iter == NULL)
1616 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001617
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001618 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
1619 if (!override && PyDict_GetItem(a, key) != NULL) {
1620 Py_DECREF(key);
1621 continue;
1622 }
1623 value = PyObject_GetItem(b, key);
1624 if (value == NULL) {
1625 Py_DECREF(iter);
1626 Py_DECREF(key);
1627 return -1;
1628 }
1629 status = PyDict_SetItem(a, key, value);
1630 Py_DECREF(key);
1631 Py_DECREF(value);
1632 if (status < 0) {
1633 Py_DECREF(iter);
1634 return -1;
1635 }
1636 }
1637 Py_DECREF(iter);
1638 if (PyErr_Occurred())
1639 /* Iterator completed, via error */
1640 return -1;
1641 }
1642 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001643}
1644
1645static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001646dict_copy(register PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001647{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001648 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001649}
1650
1651PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001652PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001653{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001654 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001655
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001656 if (o == NULL || !PyDict_Check(o)) {
1657 PyErr_BadInternalCall();
1658 return NULL;
1659 }
1660 copy = PyDict_New();
1661 if (copy == NULL)
1662 return NULL;
1663 if (PyDict_Merge(copy, o, 1) == 0)
1664 return copy;
1665 Py_DECREF(copy);
1666 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001667}
1668
Martin v. Löwis18e16552006-02-15 17:27:45 +00001669Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001670PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001671{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001672 if (mp == NULL || !PyDict_Check(mp)) {
1673 PyErr_BadInternalCall();
1674 return -1;
1675 }
1676 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001677}
1678
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001679PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001680PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001681{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001682 if (mp == NULL || !PyDict_Check(mp)) {
1683 PyErr_BadInternalCall();
1684 return NULL;
1685 }
1686 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001687}
1688
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001689PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001690PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001691{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001692 if (mp == NULL || !PyDict_Check(mp)) {
1693 PyErr_BadInternalCall();
1694 return NULL;
1695 }
1696 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001697}
1698
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001699PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001700PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001701{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001702 if (mp == NULL || !PyDict_Check(mp)) {
1703 PyErr_BadInternalCall();
1704 return NULL;
1705 }
1706 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001707}
1708
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001709/* Subroutine which returns the smallest key in a for which b's value
1710 is different or absent. The value is returned too, through the
Tim Peters95bf9392001-05-10 08:32:44 +00001711 pval argument. Both are NULL if no key in a is found for which b's status
1712 differs. The refcounts on (and only on) non-NULL *pval and function return
1713 values must be decremented by the caller (characterize() increments them
1714 to ensure that mutating comparison and PyDict_GetItem calls can't delete
1715 them before the caller is done looking at them). */
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001716
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001717static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001718characterize(PyDictObject *a, PyDictObject *b, PyObject **pval)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001719{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001720 PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
1721 PyObject *aval = NULL; /* a[akey] */
1722 Py_ssize_t i;
1723 int cmp;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001724
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001725 for (i = 0; i <= a->ma_mask; i++) {
1726 PyObject *thiskey, *thisaval, *thisbval;
1727 if (a->ma_table[i].me_value == NULL)
1728 continue;
1729 thiskey = a->ma_table[i].me_key;
1730 Py_INCREF(thiskey); /* keep alive across compares */
1731 if (akey != NULL) {
1732 cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
1733 if (cmp < 0) {
1734 Py_DECREF(thiskey);
1735 goto Fail;
1736 }
1737 if (cmp > 0 ||
1738 i > a->ma_mask ||
1739 a->ma_table[i].me_value == NULL)
1740 {
1741 /* Not the *smallest* a key; or maybe it is
1742 * but the compare shrunk the dict so we can't
1743 * find its associated value anymore; or
1744 * maybe it is but the compare deleted the
1745 * a[thiskey] entry.
1746 */
1747 Py_DECREF(thiskey);
1748 continue;
1749 }
1750 }
Tim Peters95bf9392001-05-10 08:32:44 +00001751
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001752 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
1753 thisaval = a->ma_table[i].me_value;
1754 assert(thisaval);
1755 Py_INCREF(thisaval); /* keep alive */
1756 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
1757 if (thisbval == NULL)
1758 cmp = 0;
1759 else {
1760 /* both dicts have thiskey: same values? */
1761 cmp = PyObject_RichCompareBool(
1762 thisaval, thisbval, Py_EQ);
1763 if (cmp < 0) {
1764 Py_DECREF(thiskey);
1765 Py_DECREF(thisaval);
1766 goto Fail;
1767 }
1768 }
1769 if (cmp == 0) {
1770 /* New winner. */
1771 Py_XDECREF(akey);
1772 Py_XDECREF(aval);
1773 akey = thiskey;
1774 aval = thisaval;
1775 }
1776 else {
1777 Py_DECREF(thiskey);
1778 Py_DECREF(thisaval);
1779 }
1780 }
1781 *pval = aval;
1782 return akey;
Tim Peters95bf9392001-05-10 08:32:44 +00001783
1784Fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001785 Py_XDECREF(akey);
1786 Py_XDECREF(aval);
1787 *pval = NULL;
1788 return NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001789}
1790
1791static int
Brett Cannon77ae87c2007-10-10 00:07:50 +00001792dict_compare(PyDictObject *a, PyDictObject *b)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001793{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001794 PyObject *adiff, *bdiff, *aval, *bval;
1795 int res;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001796
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001797 /* Compare lengths first */
1798 if (a->ma_used < b->ma_used)
1799 return -1; /* a is shorter */
1800 else if (a->ma_used > b->ma_used)
1801 return 1; /* b is shorter */
Tim Peters95bf9392001-05-10 08:32:44 +00001802
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001803 /* Same length -- check all keys */
1804 bdiff = bval = NULL;
1805 adiff = characterize(a, b, &aval);
1806 if (adiff == NULL) {
1807 assert(!aval);
1808 /* Either an error, or a is a subset with the same length so
1809 * must be equal.
1810 */
1811 res = PyErr_Occurred() ? -1 : 0;
1812 goto Finished;
1813 }
1814 bdiff = characterize(b, a, &bval);
1815 if (bdiff == NULL && PyErr_Occurred()) {
1816 assert(!bval);
1817 res = -1;
1818 goto Finished;
1819 }
1820 res = 0;
1821 if (bdiff) {
1822 /* bdiff == NULL "should be" impossible now, but perhaps
1823 * the last comparison done by the characterize() on a had
1824 * the side effect of making the dicts equal!
1825 */
1826 res = PyObject_Compare(adiff, bdiff);
1827 }
1828 if (res == 0 && bval != NULL)
1829 res = PyObject_Compare(aval, bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001830
1831Finished:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001832 Py_XDECREF(adiff);
1833 Py_XDECREF(bdiff);
1834 Py_XDECREF(aval);
1835 Py_XDECREF(bval);
1836 return res;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001837}
1838
Tim Peterse63415e2001-05-08 04:38:29 +00001839/* Return 1 if dicts equal, 0 if not, -1 if error.
1840 * Gets out as soon as any difference is detected.
1841 * Uses only Py_EQ comparison.
1842 */
1843static int
Brett Cannon77ae87c2007-10-10 00:07:50 +00001844dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00001845{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001846 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00001847
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001848 if (a->ma_used != b->ma_used)
1849 /* can't be equal if # of entries differ */
1850 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001851
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001852 /* Same # of entries -- check all of 'em. Exit early on any diff. */
1853 for (i = 0; i <= a->ma_mask; i++) {
1854 PyObject *aval = a->ma_table[i].me_value;
1855 if (aval != NULL) {
1856 int cmp;
1857 PyObject *bval;
1858 PyObject *key = a->ma_table[i].me_key;
1859 /* temporarily bump aval's refcount to ensure it stays
1860 alive until we're done with it */
1861 Py_INCREF(aval);
1862 /* ditto for key */
1863 Py_INCREF(key);
1864 bval = PyDict_GetItem((PyObject *)b, key);
1865 Py_DECREF(key);
1866 if (bval == NULL) {
1867 Py_DECREF(aval);
1868 return 0;
1869 }
1870 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1871 Py_DECREF(aval);
1872 if (cmp <= 0) /* error or not equal */
1873 return cmp;
1874 }
1875 }
1876 return 1;
Tim Peterse63415e2001-05-08 04:38:29 +00001877 }
1878
1879static PyObject *
1880dict_richcompare(PyObject *v, PyObject *w, int op)
1881{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001882 int cmp;
1883 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00001884
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001885 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1886 res = Py_NotImplemented;
1887 }
1888 else if (op == Py_EQ || op == Py_NE) {
1889 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
1890 if (cmp < 0)
1891 return NULL;
1892 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1893 }
1894 else {
1895 /* Py3K warning if comparison isn't == or != */
1896 if (PyErr_WarnPy3k("dict inequality comparisons not supported "
1897 "in 3.x", 1) < 0) {
1898 return NULL;
1899 }
1900 res = Py_NotImplemented;
1901 }
1902 Py_INCREF(res);
1903 return res;
Tim Peterse63415e2001-05-08 04:38:29 +00001904 }
1905
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001906static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001907dict_contains(register PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001908{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001909 long hash;
1910 PyDictEntry *ep;
Tim Petersd770ebd2006-06-01 15:50:44 +00001911
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001912 if (!PyString_CheckExact(key) ||
1913 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1914 hash = PyObject_Hash(key);
1915 if (hash == -1)
1916 return NULL;
1917 }
1918 ep = (mp->ma_lookup)(mp, key, hash);
1919 if (ep == NULL)
1920 return NULL;
1921 return PyBool_FromLong(ep->me_value != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001922}
1923
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001924static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001925dict_has_key(register PyDictObject *mp, PyObject *key)
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +00001926{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001927 if (PyErr_WarnPy3k("dict.has_key() not supported in 3.x; "
1928 "use the in operator", 1) < 0)
1929 return NULL;
1930 return dict_contains(mp, key);
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +00001931}
1932
1933static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001934dict_get(register PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001935{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001936 PyObject *key;
1937 PyObject *failobj = Py_None;
1938 PyObject *val = NULL;
1939 long hash;
1940 PyDictEntry *ep;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001941
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001942 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
1943 return NULL;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001944
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001945 if (!PyString_CheckExact(key) ||
1946 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1947 hash = PyObject_Hash(key);
1948 if (hash == -1)
1949 return NULL;
1950 }
1951 ep = (mp->ma_lookup)(mp, key, hash);
1952 if (ep == NULL)
1953 return NULL;
1954 val = ep->me_value;
1955 if (val == NULL)
1956 val = failobj;
1957 Py_INCREF(val);
1958 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001959}
1960
1961
1962static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001963dict_setdefault(register PyDictObject *mp, PyObject *args)
Guido van Rossum164452c2000-08-08 16:12:54 +00001964{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001965 PyObject *key;
1966 PyObject *failobj = Py_None;
1967 PyObject *val = NULL;
1968 long hash;
1969 PyDictEntry *ep;
Guido van Rossum164452c2000-08-08 16:12:54 +00001970
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001971 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
1972 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001973
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001974 if (!PyString_CheckExact(key) ||
1975 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1976 hash = PyObject_Hash(key);
1977 if (hash == -1)
1978 return NULL;
1979 }
1980 ep = (mp->ma_lookup)(mp, key, hash);
1981 if (ep == NULL)
1982 return NULL;
1983 val = ep->me_value;
1984 if (val == NULL) {
Antoine Pitrou6a1cd1b2012-02-27 00:45:12 +01001985 if (dict_set_item_by_hash_or_entry((PyObject*)mp, key, hash, ep,
1986 failobj) == 0)
1987 val = failobj;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001988 }
1989 Py_XINCREF(val);
1990 return val;
Guido van Rossum164452c2000-08-08 16:12:54 +00001991}
1992
1993
1994static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001995dict_clear(register PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001996{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001997 PyDict_Clear((PyObject *)mp);
1998 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001999}
2000
Guido van Rossumba6ab842000-12-12 22:02:18 +00002001static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002002dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00002003{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002004 long hash;
2005 PyDictEntry *ep;
2006 PyObject *old_value, *old_key;
2007 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00002008
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002009 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
2010 return NULL;
2011 if (mp->ma_used == 0) {
2012 if (deflt) {
2013 Py_INCREF(deflt);
2014 return deflt;
2015 }
Raymond Hettinger2ad17e12010-10-30 08:17:46 +00002016 set_key_error(key);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002017 return NULL;
2018 }
2019 if (!PyString_CheckExact(key) ||
2020 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
2021 hash = PyObject_Hash(key);
2022 if (hash == -1)
2023 return NULL;
2024 }
2025 ep = (mp->ma_lookup)(mp, key, hash);
2026 if (ep == NULL)
2027 return NULL;
2028 if (ep->me_value == NULL) {
2029 if (deflt) {
2030 Py_INCREF(deflt);
2031 return deflt;
2032 }
2033 set_key_error(key);
2034 return NULL;
2035 }
2036 old_key = ep->me_key;
2037 Py_INCREF(dummy);
2038 ep->me_key = dummy;
2039 old_value = ep->me_value;
2040 ep->me_value = NULL;
2041 mp->ma_used--;
2042 Py_DECREF(old_key);
2043 return old_value;
Guido van Rossume027d982002-04-12 15:11:59 +00002044}
2045
2046static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002047dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002048{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002049 Py_ssize_t i = 0;
2050 PyDictEntry *ep;
2051 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002052
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002053 /* Allocate the result tuple before checking the size. Believe it
2054 * or not, this allocation could trigger a garbage collection which
2055 * could empty the dict, so if we checked the size first and that
2056 * happened, the result would be an infinite loop (searching for an
2057 * entry that no longer exists). Note that the usual popitem()
2058 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2059 * tuple away if the dict *is* empty isn't a significant
2060 * inefficiency -- possible, but unlikely in practice.
2061 */
2062 res = PyTuple_New(2);
2063 if (res == NULL)
2064 return NULL;
2065 if (mp->ma_used == 0) {
2066 Py_DECREF(res);
2067 PyErr_SetString(PyExc_KeyError,
2068 "popitem(): dictionary is empty");
2069 return NULL;
2070 }
2071 /* Set ep to "the first" dict entry with a value. We abuse the hash
2072 * field of slot 0 to hold a search finger:
2073 * If slot 0 has a value, use slot 0.
2074 * Else slot 0 is being used to hold a search finger,
2075 * and we use its hash value as the first index to look.
2076 */
2077 ep = &mp->ma_table[0];
2078 if (ep->me_value == NULL) {
2079 i = ep->me_hash;
2080 /* The hash field may be a real hash value, or it may be a
2081 * legit search finger, or it may be a once-legit search
2082 * finger that's out of bounds now because it wrapped around
2083 * or the table shrunk -- simply make sure it's in bounds now.
2084 */
2085 if (i > mp->ma_mask || i < 1)
2086 i = 1; /* skip slot 0 */
2087 while ((ep = &mp->ma_table[i])->me_value == NULL) {
2088 i++;
2089 if (i > mp->ma_mask)
2090 i = 1;
2091 }
2092 }
2093 PyTuple_SET_ITEM(res, 0, ep->me_key);
2094 PyTuple_SET_ITEM(res, 1, ep->me_value);
2095 Py_INCREF(dummy);
2096 ep->me_key = dummy;
2097 ep->me_value = NULL;
2098 mp->ma_used--;
2099 assert(mp->ma_table[0].me_value == NULL);
2100 mp->ma_table[0].me_hash = i + 1; /* next place to start */
2101 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002102}
2103
Jeremy Hylton8caad492000-06-23 14:18:11 +00002104static int
2105dict_traverse(PyObject *op, visitproc visit, void *arg)
2106{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002107 Py_ssize_t i = 0;
2108 PyObject *pk;
2109 PyObject *pv;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002110
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002111 while (PyDict_Next(op, &i, &pk, &pv)) {
2112 Py_VISIT(pk);
2113 Py_VISIT(pv);
2114 }
2115 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002116}
2117
2118static int
2119dict_tp_clear(PyObject *op)
2120{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002121 PyDict_Clear(op);
2122 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002123}
2124
Tim Petersf7f88b12000-12-13 23:18:45 +00002125
Raymond Hettinger019a1482004-03-18 02:41:19 +00002126extern PyTypeObject PyDictIterKey_Type; /* Forward */
2127extern PyTypeObject PyDictIterValue_Type; /* Forward */
2128extern PyTypeObject PyDictIterItem_Type; /* Forward */
Brett Cannon77ae87c2007-10-10 00:07:50 +00002129static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002130
2131static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002132dict_iterkeys(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002133{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002134 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002135}
2136
2137static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002138dict_itervalues(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002139{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002140 return dictiter_new(dict, &PyDictIterValue_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002141}
2142
2143static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002144dict_iteritems(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002145{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002146 return dictiter_new(dict, &PyDictIterItem_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002147}
2148
Robert Schuppenies51df0642008-06-01 16:16:17 +00002149static PyObject *
2150dict_sizeof(PyDictObject *mp)
2151{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002152 Py_ssize_t res;
Robert Schuppenies51df0642008-06-01 16:16:17 +00002153
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002154 res = sizeof(PyDictObject);
2155 if (mp->ma_table != mp->ma_smalltable)
2156 res = res + (mp->ma_mask + 1) * sizeof(PyDictEntry);
2157 return PyInt_FromSsize_t(res);
Robert Schuppenies51df0642008-06-01 16:16:17 +00002158}
Guido van Rossum09e563a2001-05-01 12:10:21 +00002159
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002160PyDoc_STRVAR(has_key__doc__,
Raymond Hettinger574aa322003-09-01 22:12:08 +00002161"D.has_key(k) -> True if D has a key k, else False");
Tim Petersf7f88b12000-12-13 23:18:45 +00002162
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002163PyDoc_STRVAR(contains__doc__,
2164"D.__contains__(k) -> True if D has a key k, else False");
2165
2166PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
2167
Robert Schuppenies51df0642008-06-01 16:16:17 +00002168PyDoc_STRVAR(sizeof__doc__,
Georg Brandl7a6de8b2008-06-01 16:42:16 +00002169"D.__sizeof__() -> size of D in memory, in bytes");
Robert Schuppenies51df0642008-06-01 16:16:17 +00002170
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002171PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002172"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002173
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002174PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002175"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 +00002176
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002177PyDoc_STRVAR(pop__doc__,
Andrew M. Kuchlinga4127172008-10-04 21:51:59 +00002178"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002179If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00002180
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002181PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00002182"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Andrew M. Kuchlinga4127172008-10-04 21:51:59 +000021832-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002184
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002185PyDoc_STRVAR(keys__doc__,
2186"D.keys() -> list of D's keys");
Tim Petersf7f88b12000-12-13 23:18:45 +00002187
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002188PyDoc_STRVAR(items__doc__,
2189"D.items() -> list of D's (key, value) pairs, as 2-tuples");
Tim Petersf7f88b12000-12-13 23:18:45 +00002190
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002191PyDoc_STRVAR(values__doc__,
2192"D.values() -> list of D's values");
Tim Petersf7f88b12000-12-13 23:18:45 +00002193
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002194PyDoc_STRVAR(update__doc__,
Georg Brandl6f14c332011-12-18 19:30:55 +01002195"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n"
2196"If E present and has a .keys() method, does: for k in E: D[k] = E[k]\n\
2197If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v\n\
Andrew M. Kuchlinga4127172008-10-04 21:51:59 +00002198In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00002199
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002200PyDoc_STRVAR(fromkeys__doc__,
2201"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
2202v defaults to None.");
2203
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002204PyDoc_STRVAR(clear__doc__,
2205"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002206
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002207PyDoc_STRVAR(copy__doc__,
2208"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00002209
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002210PyDoc_STRVAR(iterkeys__doc__,
2211"D.iterkeys() -> an iterator over the keys of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00002212
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002213PyDoc_STRVAR(itervalues__doc__,
2214"D.itervalues() -> an iterator over the values of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00002215
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002216PyDoc_STRVAR(iteritems__doc__,
2217"D.iteritems() -> an iterator over the (key, value) items of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00002218
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002219/* Forward */
2220static PyObject *dictkeys_new(PyObject *);
2221static PyObject *dictitems_new(PyObject *);
2222static PyObject *dictvalues_new(PyObject *);
2223
2224PyDoc_STRVAR(viewkeys__doc__,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002225 "D.viewkeys() -> a set-like object providing a view on D's keys");
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002226PyDoc_STRVAR(viewitems__doc__,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002227 "D.viewitems() -> a set-like object providing a view on D's items");
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002228PyDoc_STRVAR(viewvalues__doc__,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002229 "D.viewvalues() -> an object providing a view on D's values");
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002230
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002231static PyMethodDef mapp_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002232 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
2233 contains__doc__},
2234 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
2235 getitem__doc__},
2236 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
2237 sizeof__doc__},
2238 {"has_key", (PyCFunction)dict_has_key, METH_O,
2239 has_key__doc__},
2240 {"get", (PyCFunction)dict_get, METH_VARARGS,
2241 get__doc__},
2242 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
2243 setdefault_doc__},
2244 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
2245 pop__doc__},
2246 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
2247 popitem__doc__},
2248 {"keys", (PyCFunction)dict_keys, METH_NOARGS,
2249 keys__doc__},
2250 {"items", (PyCFunction)dict_items, METH_NOARGS,
2251 items__doc__},
2252 {"values", (PyCFunction)dict_values, METH_NOARGS,
2253 values__doc__},
2254 {"viewkeys", (PyCFunction)dictkeys_new, METH_NOARGS,
2255 viewkeys__doc__},
2256 {"viewitems", (PyCFunction)dictitems_new, METH_NOARGS,
2257 viewitems__doc__},
2258 {"viewvalues", (PyCFunction)dictvalues_new, METH_NOARGS,
2259 viewvalues__doc__},
2260 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
2261 update__doc__},
2262 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
2263 fromkeys__doc__},
2264 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
2265 clear__doc__},
2266 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
2267 copy__doc__},
2268 {"iterkeys", (PyCFunction)dict_iterkeys, METH_NOARGS,
2269 iterkeys__doc__},
2270 {"itervalues", (PyCFunction)dict_itervalues, METH_NOARGS,
2271 itervalues__doc__},
2272 {"iteritems", (PyCFunction)dict_iteritems, METH_NOARGS,
2273 iteritems__doc__},
2274 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002275};
2276
Tim Petersd770ebd2006-06-01 15:50:44 +00002277/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002278int
2279PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002280{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002281 long hash;
2282 PyDictObject *mp = (PyDictObject *)op;
2283 PyDictEntry *ep;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002284
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002285 if (!PyString_CheckExact(key) ||
2286 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
2287 hash = PyObject_Hash(key);
2288 if (hash == -1)
2289 return -1;
2290 }
2291 ep = (mp->ma_lookup)(mp, key, hash);
2292 return ep == NULL ? -1 : (ep->me_value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002293}
2294
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +00002295/* Internal version of PyDict_Contains used when the hash value is already known */
2296int
2297_PyDict_Contains(PyObject *op, PyObject *key, long hash)
2298{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002299 PyDictObject *mp = (PyDictObject *)op;
2300 PyDictEntry *ep;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +00002301
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002302 ep = (mp->ma_lookup)(mp, key, hash);
2303 return ep == NULL ? -1 : (ep->me_value != NULL);
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +00002304}
2305
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002306/* Hack to implement "key in dict" */
2307static PySequenceMethods dict_as_sequence = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002308 0, /* sq_length */
2309 0, /* sq_concat */
2310 0, /* sq_repeat */
2311 0, /* sq_item */
2312 0, /* sq_slice */
2313 0, /* sq_ass_item */
2314 0, /* sq_ass_slice */
2315 PyDict_Contains, /* sq_contains */
2316 0, /* sq_inplace_concat */
2317 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002318};
2319
Guido van Rossum09e563a2001-05-01 12:10:21 +00002320static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002321dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2322{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002323 PyObject *self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002324
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002325 assert(type != NULL && type->tp_alloc != NULL);
2326 self = type->tp_alloc(type, 0);
2327 if (self != NULL) {
2328 PyDictObject *d = (PyDictObject *)self;
2329 /* It's guaranteed that tp->alloc zeroed out the struct. */
2330 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
2331 INIT_NONZERO_DICT_SLOTS(d);
2332 d->ma_lookup = lookdict_string;
Ezio Melottic2077b02011-03-16 12:34:31 +02002333 /* The object has been implicitly tracked by tp_alloc */
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002334 if (type == &PyDict_Type)
2335 _PyObject_GC_UNTRACK(d);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002336#ifdef SHOW_CONVERSION_COUNTS
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002337 ++created;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002338#endif
Antoine Pitrouf8387af2009-03-23 18:41:45 +00002339#ifdef SHOW_TRACK_COUNT
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002340 if (_PyObject_GC_IS_TRACKED(d))
2341 count_tracked++;
2342 else
2343 count_untracked++;
Antoine Pitrouf8387af2009-03-23 18:41:45 +00002344#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002345 }
2346 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002347}
2348
Tim Peters25786c02001-09-02 08:22:48 +00002349static int
2350dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2351{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002352 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002353}
2354
Tim Peters6d6c1a32001-08-02 04:15:00 +00002355static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002356dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002357{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002358 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002359}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002360
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002361PyDoc_STRVAR(dictionary_doc,
Ezio Melottifb501122010-02-28 23:59:00 +00002362"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002363"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melottifb501122010-02-28 23:59:00 +00002364" (key, value) pairs\n"
Georg Brandlbca11692010-02-28 18:19:17 +00002365"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002366" d = {}\n"
Georg Brandlbca11692010-02-28 18:19:17 +00002367" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002368" d[k] = v\n"
2369"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2370" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002371
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002372PyTypeObject PyDict_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002373 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2374 "dict",
2375 sizeof(PyDictObject),
2376 0,
2377 (destructor)dict_dealloc, /* tp_dealloc */
2378 (printfunc)dict_print, /* tp_print */
2379 0, /* tp_getattr */
2380 0, /* tp_setattr */
2381 (cmpfunc)dict_compare, /* tp_compare */
2382 (reprfunc)dict_repr, /* tp_repr */
2383 0, /* tp_as_number */
2384 &dict_as_sequence, /* tp_as_sequence */
2385 &dict_as_mapping, /* tp_as_mapping */
2386 (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
2387 0, /* tp_call */
2388 0, /* tp_str */
2389 PyObject_GenericGetAttr, /* tp_getattro */
2390 0, /* tp_setattro */
2391 0, /* tp_as_buffer */
2392 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2393 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
2394 dictionary_doc, /* tp_doc */
2395 dict_traverse, /* tp_traverse */
2396 dict_tp_clear, /* tp_clear */
2397 dict_richcompare, /* tp_richcompare */
2398 0, /* tp_weaklistoffset */
2399 (getiterfunc)dict_iter, /* tp_iter */
2400 0, /* tp_iternext */
2401 mapp_methods, /* tp_methods */
2402 0, /* tp_members */
2403 0, /* tp_getset */
2404 0, /* tp_base */
2405 0, /* tp_dict */
2406 0, /* tp_descr_get */
2407 0, /* tp_descr_set */
2408 0, /* tp_dictoffset */
2409 dict_init, /* tp_init */
2410 PyType_GenericAlloc, /* tp_alloc */
2411 dict_new, /* tp_new */
2412 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002413};
2414
Guido van Rossum3cca2451997-05-16 14:23:33 +00002415/* For backward compatibility with old dictionary interface */
2416
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002417PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002418PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002419{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002420 PyObject *kv, *rv;
2421 kv = PyString_FromString(key);
2422 if (kv == NULL)
2423 return NULL;
2424 rv = PyDict_GetItem(v, kv);
2425 Py_DECREF(kv);
2426 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002427}
2428
2429int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002430PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002431{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002432 PyObject *kv;
2433 int err;
2434 kv = PyString_FromString(key);
2435 if (kv == NULL)
2436 return -1;
2437 PyString_InternInPlace(&kv); /* XXX Should we really? */
2438 err = PyDict_SetItem(v, kv, item);
2439 Py_DECREF(kv);
2440 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002441}
2442
2443int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002444PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002445{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002446 PyObject *kv;
2447 int err;
2448 kv = PyString_FromString(key);
2449 if (kv == NULL)
2450 return -1;
2451 err = PyDict_DelItem(v, kv);
2452 Py_DECREF(kv);
2453 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002454}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002455
Raymond Hettinger019a1482004-03-18 02:41:19 +00002456/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002457
2458typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002459 PyObject_HEAD
2460 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
2461 Py_ssize_t di_used;
2462 Py_ssize_t di_pos;
2463 PyObject* di_result; /* reusable result tuple for iteritems */
2464 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002465} dictiterobject;
2466
2467static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002468dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002469{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002470 dictiterobject *di;
2471 di = PyObject_GC_New(dictiterobject, itertype);
2472 if (di == NULL)
2473 return NULL;
2474 Py_INCREF(dict);
2475 di->di_dict = dict;
2476 di->di_used = dict->ma_used;
2477 di->di_pos = 0;
2478 di->len = dict->ma_used;
2479 if (itertype == &PyDictIterItem_Type) {
2480 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2481 if (di->di_result == NULL) {
2482 Py_DECREF(di);
2483 return NULL;
2484 }
2485 }
2486 else
2487 di->di_result = NULL;
2488 _PyObject_GC_TRACK(di);
2489 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002490}
2491
2492static void
2493dictiter_dealloc(dictiterobject *di)
2494{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002495 Py_XDECREF(di->di_dict);
2496 Py_XDECREF(di->di_result);
2497 PyObject_GC_Del(di);
Antoine Pitrouaa687902009-01-01 14:11:22 +00002498}
2499
2500static int
2501dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
2502{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002503 Py_VISIT(di->di_dict);
2504 Py_VISIT(di->di_result);
2505 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002506}
2507
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002508static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002509dictiter_len(dictiterobject *di)
2510{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002511 Py_ssize_t len = 0;
2512 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
2513 len = di->len;
2514 return PyInt_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002515}
2516
Armin Rigof5b3e362006-02-11 21:32:43 +00002517PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002518
2519static PyMethodDef dictiter_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002520 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS, length_hint_doc},
2521 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002522};
2523
Raymond Hettinger019a1482004-03-18 02:41:19 +00002524static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002525{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002526 PyObject *key;
2527 register Py_ssize_t i, mask;
2528 register PyDictEntry *ep;
2529 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002530
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002531 if (d == NULL)
2532 return NULL;
2533 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002534
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002535 if (di->di_used != d->ma_used) {
2536 PyErr_SetString(PyExc_RuntimeError,
2537 "dictionary changed size during iteration");
2538 di->di_used = -1; /* Make this state sticky */
2539 return NULL;
2540 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002541
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002542 i = di->di_pos;
2543 if (i < 0)
2544 goto fail;
2545 ep = d->ma_table;
2546 mask = d->ma_mask;
2547 while (i <= mask && ep[i].me_value == NULL)
2548 i++;
2549 di->di_pos = i+1;
2550 if (i > mask)
2551 goto fail;
2552 di->len--;
2553 key = ep[i].me_key;
2554 Py_INCREF(key);
2555 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002556
2557fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002558 Py_DECREF(d);
2559 di->di_dict = NULL;
2560 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002561}
2562
Raymond Hettinger019a1482004-03-18 02:41:19 +00002563PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002564 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2565 "dictionary-keyiterator", /* tp_name */
2566 sizeof(dictiterobject), /* tp_basicsize */
2567 0, /* tp_itemsize */
2568 /* methods */
2569 (destructor)dictiter_dealloc, /* tp_dealloc */
2570 0, /* tp_print */
2571 0, /* tp_getattr */
2572 0, /* tp_setattr */
2573 0, /* tp_compare */
2574 0, /* tp_repr */
2575 0, /* tp_as_number */
2576 0, /* tp_as_sequence */
2577 0, /* tp_as_mapping */
2578 0, /* tp_hash */
2579 0, /* tp_call */
2580 0, /* tp_str */
2581 PyObject_GenericGetAttr, /* tp_getattro */
2582 0, /* tp_setattro */
2583 0, /* tp_as_buffer */
2584 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2585 0, /* tp_doc */
2586 (traverseproc)dictiter_traverse, /* tp_traverse */
2587 0, /* tp_clear */
2588 0, /* tp_richcompare */
2589 0, /* tp_weaklistoffset */
2590 PyObject_SelfIter, /* tp_iter */
2591 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
2592 dictiter_methods, /* tp_methods */
2593 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002594};
2595
2596static PyObject *dictiter_iternextvalue(dictiterobject *di)
2597{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002598 PyObject *value;
2599 register Py_ssize_t i, mask;
2600 register PyDictEntry *ep;
2601 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002602
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002603 if (d == NULL)
2604 return NULL;
2605 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002606
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002607 if (di->di_used != d->ma_used) {
2608 PyErr_SetString(PyExc_RuntimeError,
2609 "dictionary changed size during iteration");
2610 di->di_used = -1; /* Make this state sticky */
2611 return NULL;
2612 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002613
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002614 i = di->di_pos;
2615 mask = d->ma_mask;
2616 if (i < 0 || i > mask)
2617 goto fail;
2618 ep = d->ma_table;
2619 while ((value=ep[i].me_value) == NULL) {
2620 i++;
2621 if (i > mask)
2622 goto fail;
2623 }
2624 di->di_pos = i+1;
2625 di->len--;
2626 Py_INCREF(value);
2627 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002628
2629fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002630 Py_DECREF(d);
2631 di->di_dict = NULL;
2632 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002633}
2634
2635PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002636 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2637 "dictionary-valueiterator", /* tp_name */
2638 sizeof(dictiterobject), /* tp_basicsize */
2639 0, /* tp_itemsize */
2640 /* methods */
2641 (destructor)dictiter_dealloc, /* tp_dealloc */
2642 0, /* tp_print */
2643 0, /* tp_getattr */
2644 0, /* tp_setattr */
2645 0, /* tp_compare */
2646 0, /* tp_repr */
2647 0, /* tp_as_number */
2648 0, /* tp_as_sequence */
2649 0, /* tp_as_mapping */
2650 0, /* tp_hash */
2651 0, /* tp_call */
2652 0, /* tp_str */
2653 PyObject_GenericGetAttr, /* tp_getattro */
2654 0, /* tp_setattro */
2655 0, /* tp_as_buffer */
2656 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2657 0, /* tp_doc */
2658 (traverseproc)dictiter_traverse, /* tp_traverse */
2659 0, /* tp_clear */
2660 0, /* tp_richcompare */
2661 0, /* tp_weaklistoffset */
2662 PyObject_SelfIter, /* tp_iter */
2663 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
2664 dictiter_methods, /* tp_methods */
2665 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002666};
2667
2668static PyObject *dictiter_iternextitem(dictiterobject *di)
2669{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002670 PyObject *key, *value, *result = di->di_result;
2671 register Py_ssize_t i, mask;
2672 register PyDictEntry *ep;
2673 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002674
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002675 if (d == NULL)
2676 return NULL;
2677 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002678
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002679 if (di->di_used != d->ma_used) {
2680 PyErr_SetString(PyExc_RuntimeError,
2681 "dictionary changed size during iteration");
2682 di->di_used = -1; /* Make this state sticky */
2683 return NULL;
2684 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002685
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002686 i = di->di_pos;
2687 if (i < 0)
2688 goto fail;
2689 ep = d->ma_table;
2690 mask = d->ma_mask;
2691 while (i <= mask && ep[i].me_value == NULL)
2692 i++;
2693 di->di_pos = i+1;
2694 if (i > mask)
2695 goto fail;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002696
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002697 if (result->ob_refcnt == 1) {
2698 Py_INCREF(result);
2699 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2700 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2701 } else {
2702 result = PyTuple_New(2);
2703 if (result == NULL)
2704 return NULL;
2705 }
2706 di->len--;
2707 key = ep[i].me_key;
2708 value = ep[i].me_value;
2709 Py_INCREF(key);
2710 Py_INCREF(value);
2711 PyTuple_SET_ITEM(result, 0, key);
2712 PyTuple_SET_ITEM(result, 1, value);
2713 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002714
2715fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002716 Py_DECREF(d);
2717 di->di_dict = NULL;
2718 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002719}
2720
2721PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002722 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2723 "dictionary-itemiterator", /* tp_name */
2724 sizeof(dictiterobject), /* tp_basicsize */
2725 0, /* tp_itemsize */
2726 /* methods */
2727 (destructor)dictiter_dealloc, /* tp_dealloc */
2728 0, /* tp_print */
2729 0, /* tp_getattr */
2730 0, /* tp_setattr */
2731 0, /* tp_compare */
2732 0, /* tp_repr */
2733 0, /* tp_as_number */
2734 0, /* tp_as_sequence */
2735 0, /* tp_as_mapping */
2736 0, /* tp_hash */
2737 0, /* tp_call */
2738 0, /* tp_str */
2739 PyObject_GenericGetAttr, /* tp_getattro */
2740 0, /* tp_setattro */
2741 0, /* tp_as_buffer */
2742 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2743 0, /* tp_doc */
2744 (traverseproc)dictiter_traverse, /* tp_traverse */
2745 0, /* tp_clear */
2746 0, /* tp_richcompare */
2747 0, /* tp_weaklistoffset */
2748 PyObject_SelfIter, /* tp_iter */
2749 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
2750 dictiter_methods, /* tp_methods */
2751 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002752};
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002753
2754/***********************************************/
2755/* View objects for keys(), items(), values(). */
2756/***********************************************/
2757
2758/* The instance lay-out is the same for all three; but the type differs. */
2759
2760typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002761 PyObject_HEAD
2762 PyDictObject *dv_dict;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002763} dictviewobject;
2764
2765
2766static void
2767dictview_dealloc(dictviewobject *dv)
2768{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002769 Py_XDECREF(dv->dv_dict);
2770 PyObject_GC_Del(dv);
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002771}
2772
2773static int
2774dictview_traverse(dictviewobject *dv, visitproc visit, void *arg)
2775{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002776 Py_VISIT(dv->dv_dict);
2777 return 0;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002778}
2779
2780static Py_ssize_t
2781dictview_len(dictviewobject *dv)
2782{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002783 Py_ssize_t len = 0;
2784 if (dv->dv_dict != NULL)
2785 len = dv->dv_dict->ma_used;
2786 return len;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002787}
2788
2789static PyObject *
2790dictview_new(PyObject *dict, PyTypeObject *type)
2791{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002792 dictviewobject *dv;
2793 if (dict == NULL) {
2794 PyErr_BadInternalCall();
2795 return NULL;
2796 }
2797 if (!PyDict_Check(dict)) {
2798 /* XXX Get rid of this restriction later */
2799 PyErr_Format(PyExc_TypeError,
2800 "%s() requires a dict argument, not '%s'",
2801 type->tp_name, dict->ob_type->tp_name);
2802 return NULL;
2803 }
2804 dv = PyObject_GC_New(dictviewobject, type);
2805 if (dv == NULL)
2806 return NULL;
2807 Py_INCREF(dict);
2808 dv->dv_dict = (PyDictObject *)dict;
2809 _PyObject_GC_TRACK(dv);
2810 return (PyObject *)dv;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002811}
2812
2813/* TODO(guido): The views objects are not complete:
2814
2815 * support more set operations
2816 * support arbitrary mappings?
2817 - either these should be static or exported in dictobject.h
2818 - if public then they should probably be in builtins
2819*/
2820
2821/* Return 1 if self is a subset of other, iterating over self;
2822 0 if not; -1 if an error occurred. */
2823static int
2824all_contained_in(PyObject *self, PyObject *other)
2825{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002826 PyObject *iter = PyObject_GetIter(self);
2827 int ok = 1;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002828
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002829 if (iter == NULL)
2830 return -1;
2831 for (;;) {
2832 PyObject *next = PyIter_Next(iter);
2833 if (next == NULL) {
2834 if (PyErr_Occurred())
2835 ok = -1;
2836 break;
2837 }
2838 ok = PySequence_Contains(other, next);
2839 Py_DECREF(next);
2840 if (ok <= 0)
2841 break;
2842 }
2843 Py_DECREF(iter);
2844 return ok;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002845}
2846
2847static PyObject *
2848dictview_richcompare(PyObject *self, PyObject *other, int op)
2849{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002850 Py_ssize_t len_self, len_other;
2851 int ok;
2852 PyObject *result;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002853
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002854 assert(self != NULL);
2855 assert(PyDictViewSet_Check(self));
2856 assert(other != NULL);
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002857
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002858 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other)) {
2859 Py_INCREF(Py_NotImplemented);
2860 return Py_NotImplemented;
2861 }
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002862
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002863 len_self = PyObject_Size(self);
2864 if (len_self < 0)
2865 return NULL;
2866 len_other = PyObject_Size(other);
2867 if (len_other < 0)
2868 return NULL;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002869
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002870 ok = 0;
2871 switch(op) {
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002872
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002873 case Py_NE:
2874 case Py_EQ:
2875 if (len_self == len_other)
2876 ok = all_contained_in(self, other);
2877 if (op == Py_NE && ok >= 0)
2878 ok = !ok;
2879 break;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002880
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002881 case Py_LT:
2882 if (len_self < len_other)
2883 ok = all_contained_in(self, other);
2884 break;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002885
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002886 case Py_LE:
2887 if (len_self <= len_other)
2888 ok = all_contained_in(self, other);
2889 break;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002890
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002891 case Py_GT:
2892 if (len_self > len_other)
2893 ok = all_contained_in(other, self);
2894 break;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002895
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002896 case Py_GE:
2897 if (len_self >= len_other)
2898 ok = all_contained_in(other, self);
2899 break;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002900
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002901 }
2902 if (ok < 0)
2903 return NULL;
2904 result = ok ? Py_True : Py_False;
2905 Py_INCREF(result);
2906 return result;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002907}
2908
2909static PyObject *
2910dictview_repr(dictviewobject *dv)
2911{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002912 PyObject *seq;
2913 PyObject *seq_str;
2914 PyObject *result;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002915
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002916 seq = PySequence_List((PyObject *)dv);
2917 if (seq == NULL)
2918 return NULL;
2919
2920 seq_str = PyObject_Repr(seq);
2921 result = PyString_FromFormat("%s(%s)", Py_TYPE(dv)->tp_name,
2922 PyString_AS_STRING(seq_str));
2923 Py_DECREF(seq_str);
2924 Py_DECREF(seq);
2925 return result;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002926}
2927
2928/*** dict_keys ***/
2929
2930static PyObject *
2931dictkeys_iter(dictviewobject *dv)
2932{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002933 if (dv->dv_dict == NULL) {
2934 Py_RETURN_NONE;
2935 }
2936 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002937}
2938
2939static int
2940dictkeys_contains(dictviewobject *dv, PyObject *obj)
2941{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002942 if (dv->dv_dict == NULL)
2943 return 0;
2944 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002945}
2946
2947static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002948 (lenfunc)dictview_len, /* sq_length */
2949 0, /* sq_concat */
2950 0, /* sq_repeat */
2951 0, /* sq_item */
2952 0, /* sq_slice */
2953 0, /* sq_ass_item */
2954 0, /* sq_ass_slice */
2955 (objobjproc)dictkeys_contains, /* sq_contains */
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002956};
2957
2958static PyObject*
2959dictviews_sub(PyObject* self, PyObject *other)
2960{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002961 PyObject *result = PySet_New(self);
2962 PyObject *tmp;
2963 if (result == NULL)
2964 return NULL;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002965
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002966 tmp = PyObject_CallMethod(result, "difference_update", "O", other);
2967 if (tmp == NULL) {
2968 Py_DECREF(result);
2969 return NULL;
2970 }
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002971
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002972 Py_DECREF(tmp);
2973 return result;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002974}
2975
2976static PyObject*
2977dictviews_and(PyObject* self, PyObject *other)
2978{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002979 PyObject *result = PySet_New(self);
2980 PyObject *tmp;
2981 if (result == NULL)
2982 return NULL;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002983
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002984 tmp = PyObject_CallMethod(result, "intersection_update", "O", other);
2985 if (tmp == NULL) {
2986 Py_DECREF(result);
2987 return NULL;
2988 }
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002989
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002990 Py_DECREF(tmp);
2991 return result;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002992}
2993
2994static PyObject*
2995dictviews_or(PyObject* self, PyObject *other)
2996{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002997 PyObject *result = PySet_New(self);
2998 PyObject *tmp;
2999 if (result == NULL)
3000 return NULL;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003001
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003002 tmp = PyObject_CallMethod(result, "update", "O", other);
3003 if (tmp == NULL) {
3004 Py_DECREF(result);
3005 return NULL;
3006 }
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003007
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003008 Py_DECREF(tmp);
3009 return result;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003010}
3011
3012static PyObject*
3013dictviews_xor(PyObject* self, PyObject *other)
3014{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003015 PyObject *result = PySet_New(self);
3016 PyObject *tmp;
3017 if (result == NULL)
3018 return NULL;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003019
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003020 tmp = PyObject_CallMethod(result, "symmetric_difference_update", "O",
3021 other);
3022 if (tmp == NULL) {
3023 Py_DECREF(result);
3024 return NULL;
3025 }
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003026
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003027 Py_DECREF(tmp);
3028 return result;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003029}
3030
3031static PyNumberMethods dictviews_as_number = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003032 0, /*nb_add*/
3033 (binaryfunc)dictviews_sub, /*nb_subtract*/
3034 0, /*nb_multiply*/
3035 0, /*nb_divide*/
3036 0, /*nb_remainder*/
3037 0, /*nb_divmod*/
3038 0, /*nb_power*/
3039 0, /*nb_negative*/
3040 0, /*nb_positive*/
3041 0, /*nb_absolute*/
3042 0, /*nb_nonzero*/
3043 0, /*nb_invert*/
3044 0, /*nb_lshift*/
3045 0, /*nb_rshift*/
3046 (binaryfunc)dictviews_and, /*nb_and*/
3047 (binaryfunc)dictviews_xor, /*nb_xor*/
3048 (binaryfunc)dictviews_or, /*nb_or*/
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003049};
3050
3051static PyMethodDef dictkeys_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003052 {NULL, NULL} /* sentinel */
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003053};
3054
3055PyTypeObject PyDictKeys_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003056 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3057 "dict_keys", /* tp_name */
3058 sizeof(dictviewobject), /* tp_basicsize */
3059 0, /* tp_itemsize */
3060 /* methods */
3061 (destructor)dictview_dealloc, /* tp_dealloc */
3062 0, /* tp_print */
3063 0, /* tp_getattr */
3064 0, /* tp_setattr */
3065 0, /* tp_reserved */
3066 (reprfunc)dictview_repr, /* tp_repr */
3067 &dictviews_as_number, /* tp_as_number */
3068 &dictkeys_as_sequence, /* tp_as_sequence */
3069 0, /* tp_as_mapping */
3070 0, /* tp_hash */
3071 0, /* tp_call */
3072 0, /* tp_str */
3073 PyObject_GenericGetAttr, /* tp_getattro */
3074 0, /* tp_setattro */
3075 0, /* tp_as_buffer */
3076 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3077 Py_TPFLAGS_CHECKTYPES, /* tp_flags */
3078 0, /* tp_doc */
3079 (traverseproc)dictview_traverse, /* tp_traverse */
3080 0, /* tp_clear */
3081 dictview_richcompare, /* tp_richcompare */
3082 0, /* tp_weaklistoffset */
3083 (getiterfunc)dictkeys_iter, /* tp_iter */
3084 0, /* tp_iternext */
3085 dictkeys_methods, /* tp_methods */
3086 0,
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003087};
3088
3089static PyObject *
3090dictkeys_new(PyObject *dict)
3091{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003092 return dictview_new(dict, &PyDictKeys_Type);
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003093}
3094
3095/*** dict_items ***/
3096
3097static PyObject *
3098dictitems_iter(dictviewobject *dv)
3099{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003100 if (dv->dv_dict == NULL) {
3101 Py_RETURN_NONE;
3102 }
3103 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003104}
3105
3106static int
3107dictitems_contains(dictviewobject *dv, PyObject *obj)
3108{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003109 PyObject *key, *value, *found;
3110 if (dv->dv_dict == NULL)
3111 return 0;
3112 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
3113 return 0;
3114 key = PyTuple_GET_ITEM(obj, 0);
3115 value = PyTuple_GET_ITEM(obj, 1);
3116 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
3117 if (found == NULL) {
3118 if (PyErr_Occurred())
3119 return -1;
3120 return 0;
3121 }
3122 return PyObject_RichCompareBool(value, found, Py_EQ);
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003123}
3124
3125static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003126 (lenfunc)dictview_len, /* sq_length */
3127 0, /* sq_concat */
3128 0, /* sq_repeat */
3129 0, /* sq_item */
3130 0, /* sq_slice */
3131 0, /* sq_ass_item */
3132 0, /* sq_ass_slice */
3133 (objobjproc)dictitems_contains, /* sq_contains */
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003134};
3135
3136static PyMethodDef dictitems_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003137 {NULL, NULL} /* sentinel */
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003138};
3139
3140PyTypeObject PyDictItems_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003141 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3142 "dict_items", /* tp_name */
3143 sizeof(dictviewobject), /* tp_basicsize */
3144 0, /* tp_itemsize */
3145 /* methods */
3146 (destructor)dictview_dealloc, /* tp_dealloc */
3147 0, /* tp_print */
3148 0, /* tp_getattr */
3149 0, /* tp_setattr */
3150 0, /* tp_reserved */
3151 (reprfunc)dictview_repr, /* tp_repr */
3152 &dictviews_as_number, /* tp_as_number */
3153 &dictitems_as_sequence, /* tp_as_sequence */
3154 0, /* tp_as_mapping */
3155 0, /* tp_hash */
3156 0, /* tp_call */
3157 0, /* tp_str */
3158 PyObject_GenericGetAttr, /* tp_getattro */
3159 0, /* tp_setattro */
3160 0, /* tp_as_buffer */
3161 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3162 Py_TPFLAGS_CHECKTYPES, /* tp_flags */
3163 0, /* tp_doc */
3164 (traverseproc)dictview_traverse, /* tp_traverse */
3165 0, /* tp_clear */
3166 dictview_richcompare, /* tp_richcompare */
3167 0, /* tp_weaklistoffset */
3168 (getiterfunc)dictitems_iter, /* tp_iter */
3169 0, /* tp_iternext */
3170 dictitems_methods, /* tp_methods */
3171 0,
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003172};
3173
3174static PyObject *
3175dictitems_new(PyObject *dict)
3176{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003177 return dictview_new(dict, &PyDictItems_Type);
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003178}
3179
3180/*** dict_values ***/
3181
3182static PyObject *
3183dictvalues_iter(dictviewobject *dv)
3184{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003185 if (dv->dv_dict == NULL) {
3186 Py_RETURN_NONE;
3187 }
3188 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003189}
3190
3191static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003192 (lenfunc)dictview_len, /* sq_length */
3193 0, /* sq_concat */
3194 0, /* sq_repeat */
3195 0, /* sq_item */
3196 0, /* sq_slice */
3197 0, /* sq_ass_item */
3198 0, /* sq_ass_slice */
3199 (objobjproc)0, /* sq_contains */
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003200};
3201
3202static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003203 {NULL, NULL} /* sentinel */
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003204};
3205
3206PyTypeObject PyDictValues_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003207 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3208 "dict_values", /* tp_name */
3209 sizeof(dictviewobject), /* tp_basicsize */
3210 0, /* tp_itemsize */
3211 /* methods */
3212 (destructor)dictview_dealloc, /* tp_dealloc */
3213 0, /* tp_print */
3214 0, /* tp_getattr */
3215 0, /* tp_setattr */
3216 0, /* tp_reserved */
3217 (reprfunc)dictview_repr, /* tp_repr */
3218 0, /* tp_as_number */
3219 &dictvalues_as_sequence, /* tp_as_sequence */
3220 0, /* tp_as_mapping */
3221 0, /* tp_hash */
3222 0, /* tp_call */
3223 0, /* tp_str */
3224 PyObject_GenericGetAttr, /* tp_getattro */
3225 0, /* tp_setattro */
3226 0, /* tp_as_buffer */
3227 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3228 0, /* tp_doc */
3229 (traverseproc)dictview_traverse, /* tp_traverse */
3230 0, /* tp_clear */
3231 0, /* tp_richcompare */
3232 0, /* tp_weaklistoffset */
3233 (getiterfunc)dictvalues_iter, /* tp_iter */
3234 0, /* tp_iternext */
3235 dictvalues_methods, /* tp_methods */
3236 0,
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003237};
3238
3239static PyObject *
3240dictvalues_new(PyObject *dict)
3241{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003242 return dictview_new(dict, &PyDictValues_Type);
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003243}