blob: ac99cfb18e7b292cbbac76300db1db6bf31175fd [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
505
Fred Drake1bff34a2000-08-31 19:31:38 +0000506/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000507Internal routine to insert a new item into the table.
508Used both by the internal resize routine and by the public insert routine.
509Eats a reference to key and one to value.
Tim Petersd770ebd2006-06-01 15:50:44 +0000510Returns -1 if an error occurred, or 0 on success.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000511*/
Armin Rigo35f6d362006-06-01 13:19:12 +0000512static int
Brett Cannon77ae87c2007-10-10 00:07:50 +0000513insertdict(register PyDictObject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000514{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000515 PyObject *old_value;
516 register PyDictEntry *ep;
517 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000518
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000519 assert(mp->ma_lookup != NULL);
520 ep = mp->ma_lookup(mp, key, hash);
521 if (ep == NULL) {
522 Py_DECREF(key);
523 Py_DECREF(value);
524 return -1;
525 }
526 MAINTAIN_TRACKING(mp, key, value);
527 if (ep->me_value != NULL) {
528 old_value = ep->me_value;
529 ep->me_value = value;
530 Py_DECREF(old_value); /* which **CAN** re-enter */
531 Py_DECREF(key);
532 }
533 else {
534 if (ep->me_key == NULL)
535 mp->ma_fill++;
536 else {
537 assert(ep->me_key == dummy);
538 Py_DECREF(dummy);
539 }
540 ep->me_key = key;
541 ep->me_hash = (Py_ssize_t)hash;
542 ep->me_value = value;
543 mp->ma_used++;
544 }
545 return 0;
Armin Rigo35f6d362006-06-01 13:19:12 +0000546}
547
548/*
549Internal routine used by dictresize() to insert an item which is
550known to be absent from the dict. This routine also assumes that
551the dict contains no deleted entries. Besides the performance benefit,
552using insertdict() in dictresize() is dangerous (SF bug #1456209).
Tim Petersd770ebd2006-06-01 15:50:44 +0000553Note that no refcounts are changed by this routine; if needed, the caller
554is responsible for incref'ing `key` and `value`.
Armin Rigo35f6d362006-06-01 13:19:12 +0000555*/
556static void
Brett Cannon77ae87c2007-10-10 00:07:50 +0000557insertdict_clean(register PyDictObject *mp, PyObject *key, long hash,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000558 PyObject *value)
Armin Rigo35f6d362006-06-01 13:19:12 +0000559{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000560 register size_t i;
561 register size_t perturb;
562 register size_t mask = (size_t)mp->ma_mask;
563 PyDictEntry *ep0 = mp->ma_table;
564 register PyDictEntry *ep;
Armin Rigo35f6d362006-06-01 13:19:12 +0000565
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000566 MAINTAIN_TRACKING(mp, key, value);
567 i = hash & mask;
568 ep = &ep0[i];
569 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
570 i = (i << 2) + i + perturb + 1;
571 ep = &ep0[i & mask];
572 }
573 assert(ep->me_value == NULL);
574 mp->ma_fill++;
575 ep->me_key = key;
576 ep->me_hash = (Py_ssize_t)hash;
577 ep->me_value = value;
578 mp->ma_used++;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000579}
580
581/*
582Restructure the table by allocating a new table and reinserting all
583items again. When entries have been deleted, the new table may
584actually be smaller than the old one.
585*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000586static int
Brett Cannon77ae87c2007-10-10 00:07:50 +0000587dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000588{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000589 Py_ssize_t newsize;
590 PyDictEntry *oldtable, *newtable, *ep;
591 Py_ssize_t i;
592 int is_oldtable_malloced;
593 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000594
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000595 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000596
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000597 /* Find the smallest table size > minused. */
598 for (newsize = PyDict_MINSIZE;
599 newsize <= minused && newsize > 0;
600 newsize <<= 1)
601 ;
602 if (newsize <= 0) {
603 PyErr_NoMemory();
604 return -1;
605 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000606
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000607 /* Get space for a new table. */
608 oldtable = mp->ma_table;
609 assert(oldtable != NULL);
610 is_oldtable_malloced = oldtable != mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000611
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000612 if (newsize == PyDict_MINSIZE) {
613 /* A large table is shrinking, or we can't get any smaller. */
614 newtable = mp->ma_smalltable;
615 if (newtable == oldtable) {
616 if (mp->ma_fill == mp->ma_used) {
617 /* No dummies, so no point doing anything. */
618 return 0;
619 }
620 /* We're not going to resize it, but rebuild the
621 table anyway to purge old dummy entries.
622 Subtle: This is *necessary* if fill==size,
623 as lookdict needs at least one virgin slot to
624 terminate failing searches. If fill < size, it's
625 merely desirable, as dummies slow searches. */
626 assert(mp->ma_fill > mp->ma_used);
627 memcpy(small_copy, oldtable, sizeof(small_copy));
628 oldtable = small_copy;
629 }
630 }
631 else {
632 newtable = PyMem_NEW(PyDictEntry, newsize);
633 if (newtable == NULL) {
634 PyErr_NoMemory();
635 return -1;
636 }
637 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000638
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000639 /* Make the dict empty, using the new table. */
640 assert(newtable != oldtable);
641 mp->ma_table = newtable;
642 mp->ma_mask = newsize - 1;
643 memset(newtable, 0, sizeof(PyDictEntry) * newsize);
644 mp->ma_used = 0;
645 i = mp->ma_fill;
646 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000647
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000648 /* Copy the data over; this is refcount-neutral for active entries;
649 dummy entries aren't copied over, of course */
650 for (ep = oldtable; i > 0; ep++) {
651 if (ep->me_value != NULL) { /* active entry */
652 --i;
653 insertdict_clean(mp, ep->me_key, (long)ep->me_hash,
654 ep->me_value);
655 }
656 else if (ep->me_key != NULL) { /* dummy entry */
657 --i;
658 assert(ep->me_key == dummy);
659 Py_DECREF(ep->me_key);
660 }
661 /* else key == value == NULL: nothing to do */
662 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000663
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000664 if (is_oldtable_malloced)
665 PyMem_DEL(oldtable);
666 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000667}
668
Raymond Hettingerfd7ed402007-12-18 21:24:09 +0000669/* Create a new dictionary pre-sized to hold an estimated number of elements.
670 Underestimates are okay because the dictionary will resize as necessary.
671 Overestimates just mean the dictionary will be more sparse than usual.
672*/
673
674PyObject *
675_PyDict_NewPresized(Py_ssize_t minused)
676{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000677 PyObject *op = PyDict_New();
Raymond Hettingerfd7ed402007-12-18 21:24:09 +0000678
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000679 if (minused>5 && op != NULL && dictresize((PyDictObject *)op, minused) == -1) {
680 Py_DECREF(op);
681 return NULL;
682 }
683 return op;
Raymond Hettingerfd7ed402007-12-18 21:24:09 +0000684}
685
Tim Petersd770ebd2006-06-01 15:50:44 +0000686/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
687 * that may occur (originally dicts supported only string keys, and exceptions
688 * weren't possible). So, while the original intent was that a NULL return
Andrew M. Kuchling0067b5f2006-08-04 20:37:43 +0000689 * meant the key wasn't present, in reality it can mean that, or that an error
Tim Petersd770ebd2006-06-01 15:50:44 +0000690 * (suppressed) occurred while computing the key's hash, or that some error
691 * (suppressed) occurred when comparing keys in the dict's internal probe
692 * sequence. A nasty example of the latter is when a Python-coded comparison
693 * function hits a stack-depth error, which can cause this to return NULL
694 * even if the key is present.
695 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000696PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000697PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000698{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000699 long hash;
700 PyDictObject *mp = (PyDictObject *)op;
701 PyDictEntry *ep;
702 PyThreadState *tstate;
703 if (!PyDict_Check(op))
704 return NULL;
705 if (!PyString_CheckExact(key) ||
706 (hash = ((PyStringObject *) key)->ob_shash) == -1)
707 {
708 hash = PyObject_Hash(key);
709 if (hash == -1) {
710 PyErr_Clear();
711 return NULL;
712 }
713 }
Armin Rigo35f6d362006-06-01 13:19:12 +0000714
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000715 /* We can arrive here with a NULL tstate during initialization: try
716 running "python -Wi" for an example related to string interning.
717 Let's just hope that no exception occurs then... This must be
718 _PyThreadState_Current and not PyThreadState_GET() because in debug
719 mode, the latter complains if tstate is NULL. */
720 tstate = _PyThreadState_Current;
721 if (tstate != NULL && tstate->curexc_type != NULL) {
722 /* preserve the existing exception */
723 PyObject *err_type, *err_value, *err_tb;
724 PyErr_Fetch(&err_type, &err_value, &err_tb);
725 ep = (mp->ma_lookup)(mp, key, hash);
726 /* ignore errors */
727 PyErr_Restore(err_type, err_value, err_tb);
728 if (ep == NULL)
729 return NULL;
730 }
731 else {
732 ep = (mp->ma_lookup)(mp, key, hash);
733 if (ep == NULL) {
734 PyErr_Clear();
735 return NULL;
736 }
737 }
738 return ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000739}
740
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000741/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
Tim Peters60b29962006-01-01 01:19:23 +0000742 * dictionary if it's merely replacing the value for an existing key.
743 * This means that it's safe to loop over a dictionary with PyDict_Next()
744 * and occasionally replace a value -- but you can't insert new keys or
745 * remove them.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000746 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000747int
Tim Peters1f5871e2000-07-04 17:44:48 +0000748PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000749{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000750 register PyDictObject *mp;
751 register long hash;
752 register Py_ssize_t n_used;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000753
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000754 if (!PyDict_Check(op)) {
755 PyErr_BadInternalCall();
756 return -1;
757 }
758 assert(key);
759 assert(value);
760 mp = (PyDictObject *)op;
761 if (PyString_CheckExact(key)) {
762 hash = ((PyStringObject *)key)->ob_shash;
763 if (hash == -1)
764 hash = PyObject_Hash(key);
765 }
766 else {
767 hash = PyObject_Hash(key);
768 if (hash == -1)
769 return -1;
770 }
771 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
772 n_used = mp->ma_used;
773 Py_INCREF(value);
774 Py_INCREF(key);
775 if (insertdict(mp, key, hash, value) != 0)
776 return -1;
777 /* If we added a key, we can safely resize. Otherwise just return!
778 * If fill >= 2/3 size, adjust size. Normally, this doubles or
779 * quaduples the size, but it's also possible for the dict to shrink
780 * (if ma_fill is much larger than ma_used, meaning a lot of dict
781 * keys have been * deleted).
782 *
783 * Quadrupling the size improves average dictionary sparseness
784 * (reducing collisions) at the cost of some memory and iteration
785 * speed (which loops over every possible entry). It also halves
786 * the number of expensive resize operations in a growing dictionary.
787 *
788 * Very large dictionaries (over 50K items) use doubling instead.
789 * This may help applications with severe memory constraints.
790 */
791 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
792 return 0;
793 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000794}
795
796int
Tim Peters1f5871e2000-07-04 17:44:48 +0000797PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000798{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000799 register PyDictObject *mp;
800 register long hash;
801 register PyDictEntry *ep;
802 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000803
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000804 if (!PyDict_Check(op)) {
805 PyErr_BadInternalCall();
806 return -1;
807 }
808 assert(key);
809 if (!PyString_CheckExact(key) ||
810 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
811 hash = PyObject_Hash(key);
812 if (hash == -1)
813 return -1;
814 }
815 mp = (PyDictObject *)op;
816 ep = (mp->ma_lookup)(mp, key, hash);
817 if (ep == NULL)
818 return -1;
819 if (ep->me_value == NULL) {
820 set_key_error(key);
821 return -1;
822 }
823 old_key = ep->me_key;
824 Py_INCREF(dummy);
825 ep->me_key = dummy;
826 old_value = ep->me_value;
827 ep->me_value = NULL;
828 mp->ma_used--;
829 Py_DECREF(old_value);
830 Py_DECREF(old_key);
831 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000832}
833
Guido van Rossum25831651993-05-19 14:50:45 +0000834void
Tim Peters1f5871e2000-07-04 17:44:48 +0000835PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000836{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000837 PyDictObject *mp;
838 PyDictEntry *ep, *table;
839 int table_is_malloced;
840 Py_ssize_t fill;
841 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000842#ifdef Py_DEBUG
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000843 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +0000844#endif
845
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000846 if (!PyDict_Check(op))
847 return;
848 mp = (PyDictObject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000849#ifdef Py_DEBUG
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000850 n = mp->ma_mask + 1;
851 i = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000852#endif
853
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000854 table = mp->ma_table;
855 assert(table != NULL);
856 table_is_malloced = table != mp->ma_smalltable;
Tim Petersdea48ec2001-05-22 20:40:22 +0000857
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000858 /* This is delicate. During the process of clearing the dict,
859 * decrefs can cause the dict to mutate. To avoid fatal confusion
860 * (voice of experience), we have to make the dict empty before
861 * clearing the slots, and never refer to anything via mp->xxx while
862 * clearing.
863 */
864 fill = mp->ma_fill;
865 if (table_is_malloced)
866 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000867
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000868 else if (fill > 0) {
869 /* It's a small table with something that needs to be cleared.
870 * Afraid the only safe way is to copy the dict entries into
871 * another small table first.
872 */
873 memcpy(small_copy, table, sizeof(small_copy));
874 table = small_copy;
875 EMPTY_TO_MINSIZE(mp);
876 }
877 /* else it's a small table that's already empty */
Tim Petersdea48ec2001-05-22 20:40:22 +0000878
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000879 /* Now we can finally clear things. If C had refcounts, we could
880 * assert that the refcount on table is 1 now, i.e. that this function
881 * has unique access to it, so decref side-effects can't alter it.
882 */
883 for (ep = table; fill > 0; ++ep) {
Tim Petersdea48ec2001-05-22 20:40:22 +0000884#ifdef Py_DEBUG
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000885 assert(i < n);
886 ++i;
Tim Petersdea48ec2001-05-22 20:40:22 +0000887#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000888 if (ep->me_key) {
889 --fill;
890 Py_DECREF(ep->me_key);
891 Py_XDECREF(ep->me_value);
892 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000893#ifdef Py_DEBUG
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000894 else
895 assert(ep->me_value == NULL);
Tim Petersdea48ec2001-05-22 20:40:22 +0000896#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000897 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000898
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000899 if (table_is_malloced)
900 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000901}
902
Tim Peters080c88b2003-02-15 03:01:11 +0000903/*
904 * Iterate over a dict. Use like so:
905 *
Tim Peters9b10f7e2006-05-30 04:16:25 +0000906 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +0000907 * PyObject *key, *value;
908 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +0000909 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +0000910 * Refer to borrowed references in key and value.
911 * }
912 *
913 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +0000914 * mutates the dict. One exception: it is safe if the loop merely changes
915 * the values associated with the keys (but doesn't insert new keys or
916 * delete keys), via PyDict_SetItem().
917 */
Guido van Rossum25831651993-05-19 14:50:45 +0000918int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000919PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000920{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000921 register Py_ssize_t i;
922 register Py_ssize_t mask;
923 register PyDictEntry *ep;
Raymond Hettinger43442782004-03-17 21:55:03 +0000924
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000925 if (!PyDict_Check(op))
926 return 0;
927 i = *ppos;
928 if (i < 0)
929 return 0;
930 ep = ((PyDictObject *)op)->ma_table;
931 mask = ((PyDictObject *)op)->ma_mask;
932 while (i <= mask && ep[i].me_value == NULL)
933 i++;
934 *ppos = i+1;
935 if (i > mask)
936 return 0;
937 if (pkey)
938 *pkey = ep[i].me_key;
939 if (pvalue)
940 *pvalue = ep[i].me_value;
941 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000942}
943
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000944/* Internal version of PyDict_Next that returns a hash value in addition to the key and value.*/
945int
946_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, long *phash)
947{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000948 register Py_ssize_t i;
949 register Py_ssize_t mask;
950 register PyDictEntry *ep;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000951
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000952 if (!PyDict_Check(op))
953 return 0;
954 i = *ppos;
955 if (i < 0)
956 return 0;
957 ep = ((PyDictObject *)op)->ma_table;
958 mask = ((PyDictObject *)op)->ma_mask;
959 while (i <= mask && ep[i].me_value == NULL)
960 i++;
961 *ppos = i+1;
962 if (i > mask)
963 return 0;
964 *phash = (long)(ep[i].me_hash);
965 if (pkey)
966 *pkey = ep[i].me_key;
967 if (pvalue)
968 *pvalue = ep[i].me_value;
969 return 1;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000970}
971
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000972/* Methods */
973
974static void
Brett Cannon77ae87c2007-10-10 00:07:50 +0000975dict_dealloc(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000976{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000977 register PyDictEntry *ep;
978 Py_ssize_t fill = mp->ma_fill;
979 PyObject_GC_UnTrack(mp);
980 Py_TRASHCAN_SAFE_BEGIN(mp)
981 for (ep = mp->ma_table; fill > 0; ep++) {
982 if (ep->me_key) {
983 --fill;
984 Py_DECREF(ep->me_key);
985 Py_XDECREF(ep->me_value);
986 }
987 }
988 if (mp->ma_table != mp->ma_smalltable)
989 PyMem_DEL(mp->ma_table);
990 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
991 free_list[numfree++] = mp;
992 else
993 Py_TYPE(mp)->tp_free((PyObject *)mp);
994 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000995}
996
997static int
Brett Cannon77ae87c2007-10-10 00:07:50 +0000998dict_print(register PyDictObject *mp, register FILE *fp, register int flags)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000999{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001000 register Py_ssize_t i;
1001 register Py_ssize_t any;
1002 int status;
Guido van Rossum255443b1998-04-10 22:47:14 +00001003
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001004 status = Py_ReprEnter((PyObject*)mp);
1005 if (status != 0) {
1006 if (status < 0)
1007 return status;
1008 Py_BEGIN_ALLOW_THREADS
1009 fprintf(fp, "{...}");
1010 Py_END_ALLOW_THREADS
1011 return 0;
1012 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001013
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001014 Py_BEGIN_ALLOW_THREADS
1015 fprintf(fp, "{");
1016 Py_END_ALLOW_THREADS
1017 any = 0;
1018 for (i = 0; i <= mp->ma_mask; i++) {
1019 PyDictEntry *ep = mp->ma_table + i;
1020 PyObject *pvalue = ep->me_value;
1021 if (pvalue != NULL) {
1022 /* Prevent PyObject_Repr from deleting value during
1023 key format */
1024 Py_INCREF(pvalue);
1025 if (any++ > 0) {
1026 Py_BEGIN_ALLOW_THREADS
1027 fprintf(fp, ", ");
1028 Py_END_ALLOW_THREADS
1029 }
1030 if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
1031 Py_DECREF(pvalue);
1032 Py_ReprLeave((PyObject*)mp);
1033 return -1;
1034 }
1035 Py_BEGIN_ALLOW_THREADS
1036 fprintf(fp, ": ");
1037 Py_END_ALLOW_THREADS
1038 if (PyObject_Print(pvalue, fp, 0) != 0) {
1039 Py_DECREF(pvalue);
1040 Py_ReprLeave((PyObject*)mp);
1041 return -1;
1042 }
1043 Py_DECREF(pvalue);
1044 }
1045 }
1046 Py_BEGIN_ALLOW_THREADS
1047 fprintf(fp, "}");
1048 Py_END_ALLOW_THREADS
1049 Py_ReprLeave((PyObject*)mp);
1050 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001051}
1052
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001053static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001054dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001055{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001056 Py_ssize_t i;
1057 PyObject *s, *temp, *colon = NULL;
1058 PyObject *pieces = NULL, *result = NULL;
1059 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +00001060
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001061 i = Py_ReprEnter((PyObject *)mp);
1062 if (i != 0) {
1063 return i > 0 ? PyString_FromString("{...}") : NULL;
1064 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001065
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001066 if (mp->ma_used == 0) {
1067 result = PyString_FromString("{}");
1068 goto Done;
1069 }
Tim Petersa7259592001-06-16 05:11:17 +00001070
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001071 pieces = PyList_New(0);
1072 if (pieces == NULL)
1073 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001074
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001075 colon = PyString_FromString(": ");
1076 if (colon == NULL)
1077 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001078
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001079 /* Do repr() on each key+value pair, and insert ": " between them.
1080 Note that repr may mutate the dict. */
1081 i = 0;
1082 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
1083 int status;
1084 /* Prevent repr from deleting value during key format. */
1085 Py_INCREF(value);
1086 s = PyObject_Repr(key);
1087 PyString_Concat(&s, colon);
1088 PyString_ConcatAndDel(&s, PyObject_Repr(value));
1089 Py_DECREF(value);
1090 if (s == NULL)
1091 goto Done;
1092 status = PyList_Append(pieces, s);
1093 Py_DECREF(s); /* append created a new ref */
1094 if (status < 0)
1095 goto Done;
1096 }
Tim Petersa7259592001-06-16 05:11:17 +00001097
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001098 /* Add "{}" decorations to the first and last items. */
1099 assert(PyList_GET_SIZE(pieces) > 0);
1100 s = PyString_FromString("{");
1101 if (s == NULL)
1102 goto Done;
1103 temp = PyList_GET_ITEM(pieces, 0);
1104 PyString_ConcatAndDel(&s, temp);
1105 PyList_SET_ITEM(pieces, 0, s);
1106 if (s == NULL)
1107 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001108
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001109 s = PyString_FromString("}");
1110 if (s == NULL)
1111 goto Done;
1112 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
1113 PyString_ConcatAndDel(&temp, s);
1114 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
1115 if (temp == NULL)
1116 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001117
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001118 /* Paste them all together with ", " between. */
1119 s = PyString_FromString(", ");
1120 if (s == NULL)
1121 goto Done;
1122 result = _PyString_Join(s, pieces);
1123 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +00001124
1125Done:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001126 Py_XDECREF(pieces);
1127 Py_XDECREF(colon);
1128 Py_ReprLeave((PyObject *)mp);
1129 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001130}
1131
Martin v. Löwis18e16552006-02-15 17:27:45 +00001132static Py_ssize_t
Brett Cannon77ae87c2007-10-10 00:07:50 +00001133dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001134{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001135 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001136}
1137
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001138static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001139dict_subscript(PyDictObject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001140{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001141 PyObject *v;
1142 long hash;
1143 PyDictEntry *ep;
1144 assert(mp->ma_table != NULL);
1145 if (!PyString_CheckExact(key) ||
1146 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1147 hash = PyObject_Hash(key);
1148 if (hash == -1)
1149 return NULL;
1150 }
1151 ep = (mp->ma_lookup)(mp, key, hash);
1152 if (ep == NULL)
1153 return NULL;
1154 v = ep->me_value;
1155 if (v == NULL) {
1156 if (!PyDict_CheckExact(mp)) {
1157 /* Look up __missing__ method if we're a subclass. */
1158 PyObject *missing, *res;
1159 static PyObject *missing_str = NULL;
1160 missing = _PyObject_LookupSpecial((PyObject *)mp,
1161 "__missing__",
1162 &missing_str);
1163 if (missing != NULL) {
1164 res = PyObject_CallFunctionObjArgs(missing,
1165 key, NULL);
1166 Py_DECREF(missing);
1167 return res;
1168 }
1169 else if (PyErr_Occurred())
1170 return NULL;
1171 }
1172 set_key_error(key);
1173 return NULL;
1174 }
1175 else
1176 Py_INCREF(v);
1177 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001178}
1179
1180static int
Brett Cannon77ae87c2007-10-10 00:07:50 +00001181dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001182{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001183 if (w == NULL)
1184 return PyDict_DelItem((PyObject *)mp, v);
1185 else
1186 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001187}
1188
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001189static PyMappingMethods dict_as_mapping = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001190 (lenfunc)dict_length, /*mp_length*/
1191 (binaryfunc)dict_subscript, /*mp_subscript*/
1192 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001193};
1194
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001195static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001196dict_keys(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001197{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001198 register PyObject *v;
1199 register Py_ssize_t i, j;
1200 PyDictEntry *ep;
1201 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001202
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001203 again:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001204 n = mp->ma_used;
1205 v = PyList_New(n);
1206 if (v == NULL)
1207 return NULL;
1208 if (n != mp->ma_used) {
1209 /* Durnit. The allocations caused the dict to resize.
1210 * Just start over, this shouldn't normally happen.
1211 */
1212 Py_DECREF(v);
1213 goto again;
1214 }
1215 ep = mp->ma_table;
1216 mask = mp->ma_mask;
1217 for (i = 0, j = 0; i <= mask; i++) {
1218 if (ep[i].me_value != NULL) {
1219 PyObject *key = ep[i].me_key;
1220 Py_INCREF(key);
1221 PyList_SET_ITEM(v, j, key);
1222 j++;
1223 }
1224 }
1225 assert(j == n);
1226 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001227}
1228
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001229static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001230dict_values(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001231{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001232 register PyObject *v;
1233 register Py_ssize_t i, j;
1234 PyDictEntry *ep;
1235 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001236
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001237 again:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001238 n = mp->ma_used;
1239 v = PyList_New(n);
1240 if (v == NULL)
1241 return NULL;
1242 if (n != mp->ma_used) {
1243 /* Durnit. The allocations caused the dict to resize.
1244 * Just start over, this shouldn't normally happen.
1245 */
1246 Py_DECREF(v);
1247 goto again;
1248 }
1249 ep = mp->ma_table;
1250 mask = mp->ma_mask;
1251 for (i = 0, j = 0; i <= mask; i++) {
1252 if (ep[i].me_value != NULL) {
1253 PyObject *value = ep[i].me_value;
1254 Py_INCREF(value);
1255 PyList_SET_ITEM(v, j, value);
1256 j++;
1257 }
1258 }
1259 assert(j == n);
1260 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001261}
1262
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001263static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001264dict_items(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001265{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001266 register PyObject *v;
1267 register Py_ssize_t i, j, n;
1268 Py_ssize_t mask;
1269 PyObject *item, *key, *value;
1270 PyDictEntry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001271
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001272 /* Preallocate the list of tuples, to avoid allocations during
1273 * the loop over the items, which could trigger GC, which
1274 * could resize the dict. :-(
1275 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001276 again:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001277 n = mp->ma_used;
1278 v = PyList_New(n);
1279 if (v == NULL)
1280 return NULL;
1281 for (i = 0; i < n; i++) {
1282 item = PyTuple_New(2);
1283 if (item == NULL) {
1284 Py_DECREF(v);
1285 return NULL;
1286 }
1287 PyList_SET_ITEM(v, i, item);
1288 }
1289 if (n != mp->ma_used) {
1290 /* Durnit. The allocations caused the dict to resize.
1291 * Just start over, this shouldn't normally happen.
1292 */
1293 Py_DECREF(v);
1294 goto again;
1295 }
1296 /* Nothing we do below makes any function calls. */
1297 ep = mp->ma_table;
1298 mask = mp->ma_mask;
1299 for (i = 0, j = 0; i <= mask; i++) {
1300 if ((value=ep[i].me_value) != NULL) {
1301 key = ep[i].me_key;
1302 item = PyList_GET_ITEM(v, j);
1303 Py_INCREF(key);
1304 PyTuple_SET_ITEM(item, 0, key);
1305 Py_INCREF(value);
1306 PyTuple_SET_ITEM(item, 1, value);
1307 j++;
1308 }
1309 }
1310 assert(j == n);
1311 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001312}
1313
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001314static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001315dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001316{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001317 PyObject *seq;
1318 PyObject *value = Py_None;
1319 PyObject *it; /* iter(seq) */
1320 PyObject *key;
1321 PyObject *d;
1322 int status;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001323
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001324 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
1325 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001326
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001327 d = PyObject_CallObject(cls, NULL);
1328 if (d == NULL)
1329 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001330
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001331 if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) {
1332 PyDictObject *mp = (PyDictObject *)d;
1333 PyObject *oldvalue;
1334 Py_ssize_t pos = 0;
1335 PyObject *key;
1336 long hash;
Raymond Hettingercdcf8872007-11-07 02:26:17 +00001337
Petri Lehtinen8ffbab82011-10-24 20:59:29 +03001338 if (dictresize(mp, Py_SIZE(seq))) {
1339 Py_DECREF(d);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001340 return NULL;
Petri Lehtinen8ffbab82011-10-24 20:59:29 +03001341 }
Raymond Hettingercdcf8872007-11-07 02:26:17 +00001342
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001343 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
1344 Py_INCREF(key);
1345 Py_INCREF(value);
Petri Lehtinen8ffbab82011-10-24 20:59:29 +03001346 if (insertdict(mp, key, hash, value)) {
1347 Py_DECREF(d);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001348 return NULL;
Petri Lehtinen8ffbab82011-10-24 20:59:29 +03001349 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001350 }
1351 return d;
1352 }
Raymond Hettingercdcf8872007-11-07 02:26:17 +00001353
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001354 if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
1355 PyDictObject *mp = (PyDictObject *)d;
1356 Py_ssize_t pos = 0;
1357 PyObject *key;
1358 long hash;
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00001359
Petri Lehtinen8ffbab82011-10-24 20:59:29 +03001360 if (dictresize(mp, PySet_GET_SIZE(seq))) {
1361 Py_DECREF(d);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001362 return NULL;
Petri Lehtinen8ffbab82011-10-24 20:59:29 +03001363 }
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00001364
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001365 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
1366 Py_INCREF(key);
1367 Py_INCREF(value);
Petri Lehtinen8ffbab82011-10-24 20:59:29 +03001368 if (insertdict(mp, key, hash, value)) {
1369 Py_DECREF(d);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001370 return NULL;
Petri Lehtinen8ffbab82011-10-24 20:59:29 +03001371 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001372 }
1373 return d;
1374 }
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00001375
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001376 it = PyObject_GetIter(seq);
1377 if (it == NULL){
1378 Py_DECREF(d);
1379 return NULL;
1380 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001381
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001382 if (PyDict_CheckExact(d)) {
1383 while ((key = PyIter_Next(it)) != NULL) {
1384 status = PyDict_SetItem(d, key, value);
1385 Py_DECREF(key);
1386 if (status < 0)
1387 goto Fail;
1388 }
1389 } else {
1390 while ((key = PyIter_Next(it)) != NULL) {
1391 status = PyObject_SetItem(d, key, value);
1392 Py_DECREF(key);
1393 if (status < 0)
1394 goto Fail;
1395 }
1396 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001397
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001398 if (PyErr_Occurred())
1399 goto Fail;
1400 Py_DECREF(it);
1401 return d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001402
1403Fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001404 Py_DECREF(it);
1405 Py_DECREF(d);
1406 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001407}
1408
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001409static int
1410dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001411{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001412 PyObject *arg = NULL;
1413 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001414
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001415 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1416 result = -1;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001417
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001418 else if (arg != NULL) {
1419 if (PyObject_HasAttrString(arg, "keys"))
1420 result = PyDict_Merge(self, arg, 1);
1421 else
1422 result = PyDict_MergeFromSeq2(self, arg, 1);
1423 }
1424 if (result == 0 && kwds != NULL)
1425 result = PyDict_Merge(self, kwds, 1);
1426 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001427}
1428
1429static PyObject *
1430dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1431{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001432 if (dict_update_common(self, args, kwds, "update") != -1)
1433 Py_RETURN_NONE;
1434 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001435}
1436
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001437/* Update unconditionally replaces existing items.
1438 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001439 otherwise it leaves existing items unchanged.
1440
1441 PyDict_{Update,Merge} update/merge from a mapping object.
1442
Tim Petersf582b822001-12-11 18:51:08 +00001443 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001444 producing iterable objects of length 2.
1445*/
1446
Tim Petersf582b822001-12-11 18:51:08 +00001447int
Tim Peters1fc240e2001-10-26 05:06:50 +00001448PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1449{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001450 PyObject *it; /* iter(seq2) */
1451 Py_ssize_t i; /* index into seq2 of current element */
1452 PyObject *item; /* seq2[i] */
1453 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00001454
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001455 assert(d != NULL);
1456 assert(PyDict_Check(d));
1457 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00001458
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001459 it = PyObject_GetIter(seq2);
1460 if (it == NULL)
1461 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001462
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001463 for (i = 0; ; ++i) {
1464 PyObject *key, *value;
1465 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001466
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001467 fast = NULL;
1468 item = PyIter_Next(it);
1469 if (item == NULL) {
1470 if (PyErr_Occurred())
1471 goto Fail;
1472 break;
1473 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001474
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001475 /* Convert item to sequence, and verify length 2. */
1476 fast = PySequence_Fast(item, "");
1477 if (fast == NULL) {
1478 if (PyErr_ExceptionMatches(PyExc_TypeError))
1479 PyErr_Format(PyExc_TypeError,
1480 "cannot convert dictionary update "
1481 "sequence element #%zd to a sequence",
1482 i);
1483 goto Fail;
1484 }
1485 n = PySequence_Fast_GET_SIZE(fast);
1486 if (n != 2) {
1487 PyErr_Format(PyExc_ValueError,
1488 "dictionary update sequence element #%zd "
1489 "has length %zd; 2 is required",
1490 i, n);
1491 goto Fail;
1492 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001493
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001494 /* Update/merge with this (key, value) pair. */
1495 key = PySequence_Fast_GET_ITEM(fast, 0);
1496 value = PySequence_Fast_GET_ITEM(fast, 1);
1497 if (override || PyDict_GetItem(d, key) == NULL) {
1498 int status = PyDict_SetItem(d, key, value);
1499 if (status < 0)
1500 goto Fail;
1501 }
1502 Py_DECREF(fast);
1503 Py_DECREF(item);
1504 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001505
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001506 i = 0;
1507 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00001508Fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001509 Py_XDECREF(item);
1510 Py_XDECREF(fast);
1511 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001512Return:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001513 Py_DECREF(it);
1514 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001515}
1516
Tim Peters6d6c1a32001-08-02 04:15:00 +00001517int
1518PyDict_Update(PyObject *a, PyObject *b)
1519{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001520 return PyDict_Merge(a, b, 1);
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001521}
1522
1523int
1524PyDict_Merge(PyObject *a, PyObject *b, int override)
1525{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001526 register PyDictObject *mp, *other;
1527 register Py_ssize_t i;
1528 PyDictEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001529
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001530 /* We accept for the argument either a concrete dictionary object,
1531 * or an abstract "mapping" object. For the former, we can do
1532 * things quite efficiently. For the latter, we only require that
1533 * PyMapping_Keys() and PyObject_GetItem() be supported.
1534 */
1535 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1536 PyErr_BadInternalCall();
1537 return -1;
1538 }
1539 mp = (PyDictObject*)a;
1540 if (PyDict_Check(b)) {
1541 other = (PyDictObject*)b;
1542 if (other == mp || other->ma_used == 0)
1543 /* a.update(a) or a.update({}); nothing to do */
1544 return 0;
1545 if (mp->ma_used == 0)
1546 /* Since the target dict is empty, PyDict_GetItem()
1547 * always returns NULL. Setting override to 1
1548 * skips the unnecessary test.
1549 */
1550 override = 1;
1551 /* Do one big resize at the start, rather than
1552 * incrementally resizing as we insert new items. Expect
1553 * that there will be no (or few) overlapping keys.
1554 */
1555 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
1556 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
1557 return -1;
1558 }
1559 for (i = 0; i <= other->ma_mask; i++) {
1560 entry = &other->ma_table[i];
1561 if (entry->me_value != NULL &&
1562 (override ||
1563 PyDict_GetItem(a, entry->me_key) == NULL)) {
1564 Py_INCREF(entry->me_key);
1565 Py_INCREF(entry->me_value);
1566 if (insertdict(mp, entry->me_key,
1567 (long)entry->me_hash,
1568 entry->me_value) != 0)
1569 return -1;
1570 }
1571 }
1572 }
1573 else {
1574 /* Do it the generic, slower way */
1575 PyObject *keys = PyMapping_Keys(b);
1576 PyObject *iter;
1577 PyObject *key, *value;
1578 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001579
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001580 if (keys == NULL)
1581 /* Docstring says this is equivalent to E.keys() so
1582 * if E doesn't have a .keys() method we want
1583 * AttributeError to percolate up. Might as well
1584 * do the same for any other error.
1585 */
1586 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001587
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001588 iter = PyObject_GetIter(keys);
1589 Py_DECREF(keys);
1590 if (iter == NULL)
1591 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001592
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001593 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
1594 if (!override && PyDict_GetItem(a, key) != NULL) {
1595 Py_DECREF(key);
1596 continue;
1597 }
1598 value = PyObject_GetItem(b, key);
1599 if (value == NULL) {
1600 Py_DECREF(iter);
1601 Py_DECREF(key);
1602 return -1;
1603 }
1604 status = PyDict_SetItem(a, key, value);
1605 Py_DECREF(key);
1606 Py_DECREF(value);
1607 if (status < 0) {
1608 Py_DECREF(iter);
1609 return -1;
1610 }
1611 }
1612 Py_DECREF(iter);
1613 if (PyErr_Occurred())
1614 /* Iterator completed, via error */
1615 return -1;
1616 }
1617 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001618}
1619
1620static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001621dict_copy(register PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001622{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001623 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001624}
1625
1626PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001627PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001628{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001629 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001630
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001631 if (o == NULL || !PyDict_Check(o)) {
1632 PyErr_BadInternalCall();
1633 return NULL;
1634 }
1635 copy = PyDict_New();
1636 if (copy == NULL)
1637 return NULL;
1638 if (PyDict_Merge(copy, o, 1) == 0)
1639 return copy;
1640 Py_DECREF(copy);
1641 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001642}
1643
Martin v. Löwis18e16552006-02-15 17:27:45 +00001644Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001645PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001646{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001647 if (mp == NULL || !PyDict_Check(mp)) {
1648 PyErr_BadInternalCall();
1649 return -1;
1650 }
1651 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001652}
1653
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001654PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001655PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001656{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001657 if (mp == NULL || !PyDict_Check(mp)) {
1658 PyErr_BadInternalCall();
1659 return NULL;
1660 }
1661 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001662}
1663
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001664PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001665PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001666{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001667 if (mp == NULL || !PyDict_Check(mp)) {
1668 PyErr_BadInternalCall();
1669 return NULL;
1670 }
1671 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001672}
1673
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001674PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001675PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001676{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001677 if (mp == NULL || !PyDict_Check(mp)) {
1678 PyErr_BadInternalCall();
1679 return NULL;
1680 }
1681 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001682}
1683
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001684/* Subroutine which returns the smallest key in a for which b's value
1685 is different or absent. The value is returned too, through the
Tim Peters95bf9392001-05-10 08:32:44 +00001686 pval argument. Both are NULL if no key in a is found for which b's status
1687 differs. The refcounts on (and only on) non-NULL *pval and function return
1688 values must be decremented by the caller (characterize() increments them
1689 to ensure that mutating comparison and PyDict_GetItem calls can't delete
1690 them before the caller is done looking at them). */
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001691
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001692static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001693characterize(PyDictObject *a, PyDictObject *b, PyObject **pval)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001694{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001695 PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
1696 PyObject *aval = NULL; /* a[akey] */
1697 Py_ssize_t i;
1698 int cmp;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001699
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001700 for (i = 0; i <= a->ma_mask; i++) {
1701 PyObject *thiskey, *thisaval, *thisbval;
1702 if (a->ma_table[i].me_value == NULL)
1703 continue;
1704 thiskey = a->ma_table[i].me_key;
1705 Py_INCREF(thiskey); /* keep alive across compares */
1706 if (akey != NULL) {
1707 cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
1708 if (cmp < 0) {
1709 Py_DECREF(thiskey);
1710 goto Fail;
1711 }
1712 if (cmp > 0 ||
1713 i > a->ma_mask ||
1714 a->ma_table[i].me_value == NULL)
1715 {
1716 /* Not the *smallest* a key; or maybe it is
1717 * but the compare shrunk the dict so we can't
1718 * find its associated value anymore; or
1719 * maybe it is but the compare deleted the
1720 * a[thiskey] entry.
1721 */
1722 Py_DECREF(thiskey);
1723 continue;
1724 }
1725 }
Tim Peters95bf9392001-05-10 08:32:44 +00001726
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001727 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
1728 thisaval = a->ma_table[i].me_value;
1729 assert(thisaval);
1730 Py_INCREF(thisaval); /* keep alive */
1731 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
1732 if (thisbval == NULL)
1733 cmp = 0;
1734 else {
1735 /* both dicts have thiskey: same values? */
1736 cmp = PyObject_RichCompareBool(
1737 thisaval, thisbval, Py_EQ);
1738 if (cmp < 0) {
1739 Py_DECREF(thiskey);
1740 Py_DECREF(thisaval);
1741 goto Fail;
1742 }
1743 }
1744 if (cmp == 0) {
1745 /* New winner. */
1746 Py_XDECREF(akey);
1747 Py_XDECREF(aval);
1748 akey = thiskey;
1749 aval = thisaval;
1750 }
1751 else {
1752 Py_DECREF(thiskey);
1753 Py_DECREF(thisaval);
1754 }
1755 }
1756 *pval = aval;
1757 return akey;
Tim Peters95bf9392001-05-10 08:32:44 +00001758
1759Fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001760 Py_XDECREF(akey);
1761 Py_XDECREF(aval);
1762 *pval = NULL;
1763 return NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001764}
1765
1766static int
Brett Cannon77ae87c2007-10-10 00:07:50 +00001767dict_compare(PyDictObject *a, PyDictObject *b)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001768{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001769 PyObject *adiff, *bdiff, *aval, *bval;
1770 int res;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001771
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001772 /* Compare lengths first */
1773 if (a->ma_used < b->ma_used)
1774 return -1; /* a is shorter */
1775 else if (a->ma_used > b->ma_used)
1776 return 1; /* b is shorter */
Tim Peters95bf9392001-05-10 08:32:44 +00001777
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001778 /* Same length -- check all keys */
1779 bdiff = bval = NULL;
1780 adiff = characterize(a, b, &aval);
1781 if (adiff == NULL) {
1782 assert(!aval);
1783 /* Either an error, or a is a subset with the same length so
1784 * must be equal.
1785 */
1786 res = PyErr_Occurred() ? -1 : 0;
1787 goto Finished;
1788 }
1789 bdiff = characterize(b, a, &bval);
1790 if (bdiff == NULL && PyErr_Occurred()) {
1791 assert(!bval);
1792 res = -1;
1793 goto Finished;
1794 }
1795 res = 0;
1796 if (bdiff) {
1797 /* bdiff == NULL "should be" impossible now, but perhaps
1798 * the last comparison done by the characterize() on a had
1799 * the side effect of making the dicts equal!
1800 */
1801 res = PyObject_Compare(adiff, bdiff);
1802 }
1803 if (res == 0 && bval != NULL)
1804 res = PyObject_Compare(aval, bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001805
1806Finished:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001807 Py_XDECREF(adiff);
1808 Py_XDECREF(bdiff);
1809 Py_XDECREF(aval);
1810 Py_XDECREF(bval);
1811 return res;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001812}
1813
Tim Peterse63415e2001-05-08 04:38:29 +00001814/* Return 1 if dicts equal, 0 if not, -1 if error.
1815 * Gets out as soon as any difference is detected.
1816 * Uses only Py_EQ comparison.
1817 */
1818static int
Brett Cannon77ae87c2007-10-10 00:07:50 +00001819dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00001820{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001821 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00001822
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001823 if (a->ma_used != b->ma_used)
1824 /* can't be equal if # of entries differ */
1825 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001826
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001827 /* Same # of entries -- check all of 'em. Exit early on any diff. */
1828 for (i = 0; i <= a->ma_mask; i++) {
1829 PyObject *aval = a->ma_table[i].me_value;
1830 if (aval != NULL) {
1831 int cmp;
1832 PyObject *bval;
1833 PyObject *key = a->ma_table[i].me_key;
1834 /* temporarily bump aval's refcount to ensure it stays
1835 alive until we're done with it */
1836 Py_INCREF(aval);
1837 /* ditto for key */
1838 Py_INCREF(key);
1839 bval = PyDict_GetItem((PyObject *)b, key);
1840 Py_DECREF(key);
1841 if (bval == NULL) {
1842 Py_DECREF(aval);
1843 return 0;
1844 }
1845 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1846 Py_DECREF(aval);
1847 if (cmp <= 0) /* error or not equal */
1848 return cmp;
1849 }
1850 }
1851 return 1;
Tim Peterse63415e2001-05-08 04:38:29 +00001852 }
1853
1854static PyObject *
1855dict_richcompare(PyObject *v, PyObject *w, int op)
1856{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001857 int cmp;
1858 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00001859
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001860 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1861 res = Py_NotImplemented;
1862 }
1863 else if (op == Py_EQ || op == Py_NE) {
1864 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
1865 if (cmp < 0)
1866 return NULL;
1867 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1868 }
1869 else {
1870 /* Py3K warning if comparison isn't == or != */
1871 if (PyErr_WarnPy3k("dict inequality comparisons not supported "
1872 "in 3.x", 1) < 0) {
1873 return NULL;
1874 }
1875 res = Py_NotImplemented;
1876 }
1877 Py_INCREF(res);
1878 return res;
Tim Peterse63415e2001-05-08 04:38:29 +00001879 }
1880
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001881static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001882dict_contains(register PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001883{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001884 long hash;
1885 PyDictEntry *ep;
Tim Petersd770ebd2006-06-01 15:50:44 +00001886
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001887 if (!PyString_CheckExact(key) ||
1888 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1889 hash = PyObject_Hash(key);
1890 if (hash == -1)
1891 return NULL;
1892 }
1893 ep = (mp->ma_lookup)(mp, key, hash);
1894 if (ep == NULL)
1895 return NULL;
1896 return PyBool_FromLong(ep->me_value != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001897}
1898
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001899static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001900dict_has_key(register PyDictObject *mp, PyObject *key)
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +00001901{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001902 if (PyErr_WarnPy3k("dict.has_key() not supported in 3.x; "
1903 "use the in operator", 1) < 0)
1904 return NULL;
1905 return dict_contains(mp, key);
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +00001906}
1907
1908static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001909dict_get(register PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001910{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001911 PyObject *key;
1912 PyObject *failobj = Py_None;
1913 PyObject *val = NULL;
1914 long hash;
1915 PyDictEntry *ep;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001916
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001917 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
1918 return NULL;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001919
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001920 if (!PyString_CheckExact(key) ||
1921 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1922 hash = PyObject_Hash(key);
1923 if (hash == -1)
1924 return NULL;
1925 }
1926 ep = (mp->ma_lookup)(mp, key, hash);
1927 if (ep == NULL)
1928 return NULL;
1929 val = ep->me_value;
1930 if (val == NULL)
1931 val = failobj;
1932 Py_INCREF(val);
1933 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001934}
1935
1936
1937static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001938dict_setdefault(register PyDictObject *mp, PyObject *args)
Guido van Rossum164452c2000-08-08 16:12:54 +00001939{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001940 PyObject *key;
1941 PyObject *failobj = Py_None;
1942 PyObject *val = NULL;
1943 long hash;
1944 PyDictEntry *ep;
Guido van Rossum164452c2000-08-08 16:12:54 +00001945
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001946 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
1947 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001948
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001949 if (!PyString_CheckExact(key) ||
1950 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1951 hash = PyObject_Hash(key);
1952 if (hash == -1)
1953 return NULL;
1954 }
1955 ep = (mp->ma_lookup)(mp, key, hash);
1956 if (ep == NULL)
1957 return NULL;
1958 val = ep->me_value;
1959 if (val == NULL) {
1960 val = failobj;
1961 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1962 val = NULL;
1963 }
1964 Py_XINCREF(val);
1965 return val;
Guido van Rossum164452c2000-08-08 16:12:54 +00001966}
1967
1968
1969static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001970dict_clear(register PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001971{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001972 PyDict_Clear((PyObject *)mp);
1973 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001974}
1975
Guido van Rossumba6ab842000-12-12 22:02:18 +00001976static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001977dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00001978{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001979 long hash;
1980 PyDictEntry *ep;
1981 PyObject *old_value, *old_key;
1982 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001983
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001984 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1985 return NULL;
1986 if (mp->ma_used == 0) {
1987 if (deflt) {
1988 Py_INCREF(deflt);
1989 return deflt;
1990 }
Raymond Hettinger2ad17e12010-10-30 08:17:46 +00001991 set_key_error(key);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001992 return NULL;
1993 }
1994 if (!PyString_CheckExact(key) ||
1995 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1996 hash = PyObject_Hash(key);
1997 if (hash == -1)
1998 return NULL;
1999 }
2000 ep = (mp->ma_lookup)(mp, key, hash);
2001 if (ep == NULL)
2002 return NULL;
2003 if (ep->me_value == NULL) {
2004 if (deflt) {
2005 Py_INCREF(deflt);
2006 return deflt;
2007 }
2008 set_key_error(key);
2009 return NULL;
2010 }
2011 old_key = ep->me_key;
2012 Py_INCREF(dummy);
2013 ep->me_key = dummy;
2014 old_value = ep->me_value;
2015 ep->me_value = NULL;
2016 mp->ma_used--;
2017 Py_DECREF(old_key);
2018 return old_value;
Guido van Rossume027d982002-04-12 15:11:59 +00002019}
2020
2021static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002022dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002023{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002024 Py_ssize_t i = 0;
2025 PyDictEntry *ep;
2026 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002027
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002028 /* Allocate the result tuple before checking the size. Believe it
2029 * or not, this allocation could trigger a garbage collection which
2030 * could empty the dict, so if we checked the size first and that
2031 * happened, the result would be an infinite loop (searching for an
2032 * entry that no longer exists). Note that the usual popitem()
2033 * idiom is "while d: k, v = d.popitem()". so needing to throw the
2034 * tuple away if the dict *is* empty isn't a significant
2035 * inefficiency -- possible, but unlikely in practice.
2036 */
2037 res = PyTuple_New(2);
2038 if (res == NULL)
2039 return NULL;
2040 if (mp->ma_used == 0) {
2041 Py_DECREF(res);
2042 PyErr_SetString(PyExc_KeyError,
2043 "popitem(): dictionary is empty");
2044 return NULL;
2045 }
2046 /* Set ep to "the first" dict entry with a value. We abuse the hash
2047 * field of slot 0 to hold a search finger:
2048 * If slot 0 has a value, use slot 0.
2049 * Else slot 0 is being used to hold a search finger,
2050 * and we use its hash value as the first index to look.
2051 */
2052 ep = &mp->ma_table[0];
2053 if (ep->me_value == NULL) {
2054 i = ep->me_hash;
2055 /* The hash field may be a real hash value, or it may be a
2056 * legit search finger, or it may be a once-legit search
2057 * finger that's out of bounds now because it wrapped around
2058 * or the table shrunk -- simply make sure it's in bounds now.
2059 */
2060 if (i > mp->ma_mask || i < 1)
2061 i = 1; /* skip slot 0 */
2062 while ((ep = &mp->ma_table[i])->me_value == NULL) {
2063 i++;
2064 if (i > mp->ma_mask)
2065 i = 1;
2066 }
2067 }
2068 PyTuple_SET_ITEM(res, 0, ep->me_key);
2069 PyTuple_SET_ITEM(res, 1, ep->me_value);
2070 Py_INCREF(dummy);
2071 ep->me_key = dummy;
2072 ep->me_value = NULL;
2073 mp->ma_used--;
2074 assert(mp->ma_table[0].me_value == NULL);
2075 mp->ma_table[0].me_hash = i + 1; /* next place to start */
2076 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002077}
2078
Jeremy Hylton8caad492000-06-23 14:18:11 +00002079static int
2080dict_traverse(PyObject *op, visitproc visit, void *arg)
2081{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002082 Py_ssize_t i = 0;
2083 PyObject *pk;
2084 PyObject *pv;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002085
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002086 while (PyDict_Next(op, &i, &pk, &pv)) {
2087 Py_VISIT(pk);
2088 Py_VISIT(pv);
2089 }
2090 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002091}
2092
2093static int
2094dict_tp_clear(PyObject *op)
2095{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002096 PyDict_Clear(op);
2097 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002098}
2099
Tim Petersf7f88b12000-12-13 23:18:45 +00002100
Raymond Hettinger019a1482004-03-18 02:41:19 +00002101extern PyTypeObject PyDictIterKey_Type; /* Forward */
2102extern PyTypeObject PyDictIterValue_Type; /* Forward */
2103extern PyTypeObject PyDictIterItem_Type; /* Forward */
Brett Cannon77ae87c2007-10-10 00:07:50 +00002104static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002105
2106static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002107dict_iterkeys(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002108{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002109 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002110}
2111
2112static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002113dict_itervalues(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002114{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002115 return dictiter_new(dict, &PyDictIterValue_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002116}
2117
2118static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002119dict_iteritems(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002120{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002121 return dictiter_new(dict, &PyDictIterItem_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002122}
2123
Robert Schuppenies51df0642008-06-01 16:16:17 +00002124static PyObject *
2125dict_sizeof(PyDictObject *mp)
2126{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002127 Py_ssize_t res;
Robert Schuppenies51df0642008-06-01 16:16:17 +00002128
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002129 res = sizeof(PyDictObject);
2130 if (mp->ma_table != mp->ma_smalltable)
2131 res = res + (mp->ma_mask + 1) * sizeof(PyDictEntry);
2132 return PyInt_FromSsize_t(res);
Robert Schuppenies51df0642008-06-01 16:16:17 +00002133}
Guido van Rossum09e563a2001-05-01 12:10:21 +00002134
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002135PyDoc_STRVAR(has_key__doc__,
Raymond Hettinger574aa322003-09-01 22:12:08 +00002136"D.has_key(k) -> True if D has a key k, else False");
Tim Petersf7f88b12000-12-13 23:18:45 +00002137
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002138PyDoc_STRVAR(contains__doc__,
2139"D.__contains__(k) -> True if D has a key k, else False");
2140
2141PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
2142
Robert Schuppenies51df0642008-06-01 16:16:17 +00002143PyDoc_STRVAR(sizeof__doc__,
Georg Brandl7a6de8b2008-06-01 16:42:16 +00002144"D.__sizeof__() -> size of D in memory, in bytes");
Robert Schuppenies51df0642008-06-01 16:16:17 +00002145
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002146PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002147"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002148
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002149PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002150"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 +00002151
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002152PyDoc_STRVAR(pop__doc__,
Andrew M. Kuchlinga4127172008-10-04 21:51:59 +00002153"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002154If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00002155
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002156PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00002157"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Andrew M. Kuchlinga4127172008-10-04 21:51:59 +000021582-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002159
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002160PyDoc_STRVAR(keys__doc__,
2161"D.keys() -> list of D's keys");
Tim Petersf7f88b12000-12-13 23:18:45 +00002162
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002163PyDoc_STRVAR(items__doc__,
2164"D.items() -> list of D's (key, value) pairs, as 2-tuples");
Tim Petersf7f88b12000-12-13 23:18:45 +00002165
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002166PyDoc_STRVAR(values__doc__,
2167"D.values() -> list of D's values");
Tim Petersf7f88b12000-12-13 23:18:45 +00002168
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002169PyDoc_STRVAR(update__doc__,
Georg Brandl6f14c332011-12-18 19:30:55 +01002170"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n"
2171"If E present and has a .keys() method, does: for k in E: D[k] = E[k]\n\
2172If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v\n\
Andrew M. Kuchlinga4127172008-10-04 21:51:59 +00002173In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00002174
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002175PyDoc_STRVAR(fromkeys__doc__,
2176"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
2177v defaults to None.");
2178
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002179PyDoc_STRVAR(clear__doc__,
2180"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002181
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002182PyDoc_STRVAR(copy__doc__,
2183"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00002184
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002185PyDoc_STRVAR(iterkeys__doc__,
2186"D.iterkeys() -> an iterator over the keys of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00002187
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002188PyDoc_STRVAR(itervalues__doc__,
2189"D.itervalues() -> an iterator over the values of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00002190
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002191PyDoc_STRVAR(iteritems__doc__,
2192"D.iteritems() -> an iterator over the (key, value) items of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00002193
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002194/* Forward */
2195static PyObject *dictkeys_new(PyObject *);
2196static PyObject *dictitems_new(PyObject *);
2197static PyObject *dictvalues_new(PyObject *);
2198
2199PyDoc_STRVAR(viewkeys__doc__,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002200 "D.viewkeys() -> a set-like object providing a view on D's keys");
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002201PyDoc_STRVAR(viewitems__doc__,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002202 "D.viewitems() -> a set-like object providing a view on D's items");
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002203PyDoc_STRVAR(viewvalues__doc__,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002204 "D.viewvalues() -> an object providing a view on D's values");
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002205
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002206static PyMethodDef mapp_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002207 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
2208 contains__doc__},
2209 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
2210 getitem__doc__},
2211 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
2212 sizeof__doc__},
2213 {"has_key", (PyCFunction)dict_has_key, METH_O,
2214 has_key__doc__},
2215 {"get", (PyCFunction)dict_get, METH_VARARGS,
2216 get__doc__},
2217 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
2218 setdefault_doc__},
2219 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
2220 pop__doc__},
2221 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
2222 popitem__doc__},
2223 {"keys", (PyCFunction)dict_keys, METH_NOARGS,
2224 keys__doc__},
2225 {"items", (PyCFunction)dict_items, METH_NOARGS,
2226 items__doc__},
2227 {"values", (PyCFunction)dict_values, METH_NOARGS,
2228 values__doc__},
2229 {"viewkeys", (PyCFunction)dictkeys_new, METH_NOARGS,
2230 viewkeys__doc__},
2231 {"viewitems", (PyCFunction)dictitems_new, METH_NOARGS,
2232 viewitems__doc__},
2233 {"viewvalues", (PyCFunction)dictvalues_new, METH_NOARGS,
2234 viewvalues__doc__},
2235 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
2236 update__doc__},
2237 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
2238 fromkeys__doc__},
2239 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
2240 clear__doc__},
2241 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
2242 copy__doc__},
2243 {"iterkeys", (PyCFunction)dict_iterkeys, METH_NOARGS,
2244 iterkeys__doc__},
2245 {"itervalues", (PyCFunction)dict_itervalues, METH_NOARGS,
2246 itervalues__doc__},
2247 {"iteritems", (PyCFunction)dict_iteritems, METH_NOARGS,
2248 iteritems__doc__},
2249 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002250};
2251
Tim Petersd770ebd2006-06-01 15:50:44 +00002252/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002253int
2254PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002255{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002256 long hash;
2257 PyDictObject *mp = (PyDictObject *)op;
2258 PyDictEntry *ep;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002259
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002260 if (!PyString_CheckExact(key) ||
2261 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
2262 hash = PyObject_Hash(key);
2263 if (hash == -1)
2264 return -1;
2265 }
2266 ep = (mp->ma_lookup)(mp, key, hash);
2267 return ep == NULL ? -1 : (ep->me_value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002268}
2269
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +00002270/* Internal version of PyDict_Contains used when the hash value is already known */
2271int
2272_PyDict_Contains(PyObject *op, PyObject *key, long hash)
2273{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002274 PyDictObject *mp = (PyDictObject *)op;
2275 PyDictEntry *ep;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +00002276
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002277 ep = (mp->ma_lookup)(mp, key, hash);
2278 return ep == NULL ? -1 : (ep->me_value != NULL);
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +00002279}
2280
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002281/* Hack to implement "key in dict" */
2282static PySequenceMethods dict_as_sequence = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002283 0, /* sq_length */
2284 0, /* sq_concat */
2285 0, /* sq_repeat */
2286 0, /* sq_item */
2287 0, /* sq_slice */
2288 0, /* sq_ass_item */
2289 0, /* sq_ass_slice */
2290 PyDict_Contains, /* sq_contains */
2291 0, /* sq_inplace_concat */
2292 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002293};
2294
Guido van Rossum09e563a2001-05-01 12:10:21 +00002295static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002296dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2297{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002298 PyObject *self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002299
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002300 assert(type != NULL && type->tp_alloc != NULL);
2301 self = type->tp_alloc(type, 0);
2302 if (self != NULL) {
2303 PyDictObject *d = (PyDictObject *)self;
2304 /* It's guaranteed that tp->alloc zeroed out the struct. */
2305 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
2306 INIT_NONZERO_DICT_SLOTS(d);
2307 d->ma_lookup = lookdict_string;
Ezio Melottic2077b02011-03-16 12:34:31 +02002308 /* The object has been implicitly tracked by tp_alloc */
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002309 if (type == &PyDict_Type)
2310 _PyObject_GC_UNTRACK(d);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002311#ifdef SHOW_CONVERSION_COUNTS
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002312 ++created;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002313#endif
Antoine Pitrouf8387af2009-03-23 18:41:45 +00002314#ifdef SHOW_TRACK_COUNT
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002315 if (_PyObject_GC_IS_TRACKED(d))
2316 count_tracked++;
2317 else
2318 count_untracked++;
Antoine Pitrouf8387af2009-03-23 18:41:45 +00002319#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002320 }
2321 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002322}
2323
Tim Peters25786c02001-09-02 08:22:48 +00002324static int
2325dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2326{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002327 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002328}
2329
Tim Peters6d6c1a32001-08-02 04:15:00 +00002330static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002331dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002332{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002333 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002334}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002335
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002336PyDoc_STRVAR(dictionary_doc,
Ezio Melottifb501122010-02-28 23:59:00 +00002337"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002338"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melottifb501122010-02-28 23:59:00 +00002339" (key, value) pairs\n"
Georg Brandlbca11692010-02-28 18:19:17 +00002340"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002341" d = {}\n"
Georg Brandlbca11692010-02-28 18:19:17 +00002342" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002343" d[k] = v\n"
2344"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2345" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002346
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002347PyTypeObject PyDict_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002348 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2349 "dict",
2350 sizeof(PyDictObject),
2351 0,
2352 (destructor)dict_dealloc, /* tp_dealloc */
2353 (printfunc)dict_print, /* tp_print */
2354 0, /* tp_getattr */
2355 0, /* tp_setattr */
2356 (cmpfunc)dict_compare, /* tp_compare */
2357 (reprfunc)dict_repr, /* tp_repr */
2358 0, /* tp_as_number */
2359 &dict_as_sequence, /* tp_as_sequence */
2360 &dict_as_mapping, /* tp_as_mapping */
2361 (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
2362 0, /* tp_call */
2363 0, /* tp_str */
2364 PyObject_GenericGetAttr, /* tp_getattro */
2365 0, /* tp_setattro */
2366 0, /* tp_as_buffer */
2367 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2368 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
2369 dictionary_doc, /* tp_doc */
2370 dict_traverse, /* tp_traverse */
2371 dict_tp_clear, /* tp_clear */
2372 dict_richcompare, /* tp_richcompare */
2373 0, /* tp_weaklistoffset */
2374 (getiterfunc)dict_iter, /* tp_iter */
2375 0, /* tp_iternext */
2376 mapp_methods, /* tp_methods */
2377 0, /* tp_members */
2378 0, /* tp_getset */
2379 0, /* tp_base */
2380 0, /* tp_dict */
2381 0, /* tp_descr_get */
2382 0, /* tp_descr_set */
2383 0, /* tp_dictoffset */
2384 dict_init, /* tp_init */
2385 PyType_GenericAlloc, /* tp_alloc */
2386 dict_new, /* tp_new */
2387 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002388};
2389
Guido van Rossum3cca2451997-05-16 14:23:33 +00002390/* For backward compatibility with old dictionary interface */
2391
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002392PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002393PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002394{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002395 PyObject *kv, *rv;
2396 kv = PyString_FromString(key);
2397 if (kv == NULL)
2398 return NULL;
2399 rv = PyDict_GetItem(v, kv);
2400 Py_DECREF(kv);
2401 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002402}
2403
2404int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002405PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002406{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002407 PyObject *kv;
2408 int err;
2409 kv = PyString_FromString(key);
2410 if (kv == NULL)
2411 return -1;
2412 PyString_InternInPlace(&kv); /* XXX Should we really? */
2413 err = PyDict_SetItem(v, kv, item);
2414 Py_DECREF(kv);
2415 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002416}
2417
2418int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002419PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002420{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002421 PyObject *kv;
2422 int err;
2423 kv = PyString_FromString(key);
2424 if (kv == NULL)
2425 return -1;
2426 err = PyDict_DelItem(v, kv);
2427 Py_DECREF(kv);
2428 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002429}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002430
Raymond Hettinger019a1482004-03-18 02:41:19 +00002431/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002432
2433typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002434 PyObject_HEAD
2435 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
2436 Py_ssize_t di_used;
2437 Py_ssize_t di_pos;
2438 PyObject* di_result; /* reusable result tuple for iteritems */
2439 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002440} dictiterobject;
2441
2442static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002443dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002444{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002445 dictiterobject *di;
2446 di = PyObject_GC_New(dictiterobject, itertype);
2447 if (di == NULL)
2448 return NULL;
2449 Py_INCREF(dict);
2450 di->di_dict = dict;
2451 di->di_used = dict->ma_used;
2452 di->di_pos = 0;
2453 di->len = dict->ma_used;
2454 if (itertype == &PyDictIterItem_Type) {
2455 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2456 if (di->di_result == NULL) {
2457 Py_DECREF(di);
2458 return NULL;
2459 }
2460 }
2461 else
2462 di->di_result = NULL;
2463 _PyObject_GC_TRACK(di);
2464 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002465}
2466
2467static void
2468dictiter_dealloc(dictiterobject *di)
2469{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002470 Py_XDECREF(di->di_dict);
2471 Py_XDECREF(di->di_result);
2472 PyObject_GC_Del(di);
Antoine Pitrouaa687902009-01-01 14:11:22 +00002473}
2474
2475static int
2476dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
2477{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002478 Py_VISIT(di->di_dict);
2479 Py_VISIT(di->di_result);
2480 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002481}
2482
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002483static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002484dictiter_len(dictiterobject *di)
2485{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002486 Py_ssize_t len = 0;
2487 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
2488 len = di->len;
2489 return PyInt_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002490}
2491
Armin Rigof5b3e362006-02-11 21:32:43 +00002492PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002493
2494static PyMethodDef dictiter_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002495 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS, length_hint_doc},
2496 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002497};
2498
Raymond Hettinger019a1482004-03-18 02:41:19 +00002499static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002500{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002501 PyObject *key;
2502 register Py_ssize_t i, mask;
2503 register PyDictEntry *ep;
2504 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002505
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002506 if (d == NULL)
2507 return NULL;
2508 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002509
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002510 if (di->di_used != d->ma_used) {
2511 PyErr_SetString(PyExc_RuntimeError,
2512 "dictionary changed size during iteration");
2513 di->di_used = -1; /* Make this state sticky */
2514 return NULL;
2515 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002516
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002517 i = di->di_pos;
2518 if (i < 0)
2519 goto fail;
2520 ep = d->ma_table;
2521 mask = d->ma_mask;
2522 while (i <= mask && ep[i].me_value == NULL)
2523 i++;
2524 di->di_pos = i+1;
2525 if (i > mask)
2526 goto fail;
2527 di->len--;
2528 key = ep[i].me_key;
2529 Py_INCREF(key);
2530 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002531
2532fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002533 Py_DECREF(d);
2534 di->di_dict = NULL;
2535 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002536}
2537
Raymond Hettinger019a1482004-03-18 02:41:19 +00002538PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002539 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2540 "dictionary-keyiterator", /* tp_name */
2541 sizeof(dictiterobject), /* tp_basicsize */
2542 0, /* tp_itemsize */
2543 /* methods */
2544 (destructor)dictiter_dealloc, /* tp_dealloc */
2545 0, /* tp_print */
2546 0, /* tp_getattr */
2547 0, /* tp_setattr */
2548 0, /* tp_compare */
2549 0, /* tp_repr */
2550 0, /* tp_as_number */
2551 0, /* tp_as_sequence */
2552 0, /* tp_as_mapping */
2553 0, /* tp_hash */
2554 0, /* tp_call */
2555 0, /* tp_str */
2556 PyObject_GenericGetAttr, /* tp_getattro */
2557 0, /* tp_setattro */
2558 0, /* tp_as_buffer */
2559 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2560 0, /* tp_doc */
2561 (traverseproc)dictiter_traverse, /* tp_traverse */
2562 0, /* tp_clear */
2563 0, /* tp_richcompare */
2564 0, /* tp_weaklistoffset */
2565 PyObject_SelfIter, /* tp_iter */
2566 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
2567 dictiter_methods, /* tp_methods */
2568 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002569};
2570
2571static PyObject *dictiter_iternextvalue(dictiterobject *di)
2572{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002573 PyObject *value;
2574 register Py_ssize_t i, mask;
2575 register PyDictEntry *ep;
2576 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002577
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002578 if (d == NULL)
2579 return NULL;
2580 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002581
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002582 if (di->di_used != d->ma_used) {
2583 PyErr_SetString(PyExc_RuntimeError,
2584 "dictionary changed size during iteration");
2585 di->di_used = -1; /* Make this state sticky */
2586 return NULL;
2587 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002588
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002589 i = di->di_pos;
2590 mask = d->ma_mask;
2591 if (i < 0 || i > mask)
2592 goto fail;
2593 ep = d->ma_table;
2594 while ((value=ep[i].me_value) == NULL) {
2595 i++;
2596 if (i > mask)
2597 goto fail;
2598 }
2599 di->di_pos = i+1;
2600 di->len--;
2601 Py_INCREF(value);
2602 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002603
2604fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002605 Py_DECREF(d);
2606 di->di_dict = NULL;
2607 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002608}
2609
2610PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002611 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2612 "dictionary-valueiterator", /* tp_name */
2613 sizeof(dictiterobject), /* tp_basicsize */
2614 0, /* tp_itemsize */
2615 /* methods */
2616 (destructor)dictiter_dealloc, /* tp_dealloc */
2617 0, /* tp_print */
2618 0, /* tp_getattr */
2619 0, /* tp_setattr */
2620 0, /* tp_compare */
2621 0, /* tp_repr */
2622 0, /* tp_as_number */
2623 0, /* tp_as_sequence */
2624 0, /* tp_as_mapping */
2625 0, /* tp_hash */
2626 0, /* tp_call */
2627 0, /* tp_str */
2628 PyObject_GenericGetAttr, /* tp_getattro */
2629 0, /* tp_setattro */
2630 0, /* tp_as_buffer */
2631 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2632 0, /* tp_doc */
2633 (traverseproc)dictiter_traverse, /* tp_traverse */
2634 0, /* tp_clear */
2635 0, /* tp_richcompare */
2636 0, /* tp_weaklistoffset */
2637 PyObject_SelfIter, /* tp_iter */
2638 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
2639 dictiter_methods, /* tp_methods */
2640 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002641};
2642
2643static PyObject *dictiter_iternextitem(dictiterobject *di)
2644{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002645 PyObject *key, *value, *result = di->di_result;
2646 register Py_ssize_t i, mask;
2647 register PyDictEntry *ep;
2648 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002649
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002650 if (d == NULL)
2651 return NULL;
2652 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002653
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002654 if (di->di_used != d->ma_used) {
2655 PyErr_SetString(PyExc_RuntimeError,
2656 "dictionary changed size during iteration");
2657 di->di_used = -1; /* Make this state sticky */
2658 return NULL;
2659 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002660
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002661 i = di->di_pos;
2662 if (i < 0)
2663 goto fail;
2664 ep = d->ma_table;
2665 mask = d->ma_mask;
2666 while (i <= mask && ep[i].me_value == NULL)
2667 i++;
2668 di->di_pos = i+1;
2669 if (i > mask)
2670 goto fail;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002671
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002672 if (result->ob_refcnt == 1) {
2673 Py_INCREF(result);
2674 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2675 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2676 } else {
2677 result = PyTuple_New(2);
2678 if (result == NULL)
2679 return NULL;
2680 }
2681 di->len--;
2682 key = ep[i].me_key;
2683 value = ep[i].me_value;
2684 Py_INCREF(key);
2685 Py_INCREF(value);
2686 PyTuple_SET_ITEM(result, 0, key);
2687 PyTuple_SET_ITEM(result, 1, value);
2688 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002689
2690fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002691 Py_DECREF(d);
2692 di->di_dict = NULL;
2693 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002694}
2695
2696PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002697 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2698 "dictionary-itemiterator", /* tp_name */
2699 sizeof(dictiterobject), /* tp_basicsize */
2700 0, /* tp_itemsize */
2701 /* methods */
2702 (destructor)dictiter_dealloc, /* tp_dealloc */
2703 0, /* tp_print */
2704 0, /* tp_getattr */
2705 0, /* tp_setattr */
2706 0, /* tp_compare */
2707 0, /* tp_repr */
2708 0, /* tp_as_number */
2709 0, /* tp_as_sequence */
2710 0, /* tp_as_mapping */
2711 0, /* tp_hash */
2712 0, /* tp_call */
2713 0, /* tp_str */
2714 PyObject_GenericGetAttr, /* tp_getattro */
2715 0, /* tp_setattro */
2716 0, /* tp_as_buffer */
2717 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2718 0, /* tp_doc */
2719 (traverseproc)dictiter_traverse, /* tp_traverse */
2720 0, /* tp_clear */
2721 0, /* tp_richcompare */
2722 0, /* tp_weaklistoffset */
2723 PyObject_SelfIter, /* tp_iter */
2724 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
2725 dictiter_methods, /* tp_methods */
2726 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002727};
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002728
2729/***********************************************/
2730/* View objects for keys(), items(), values(). */
2731/***********************************************/
2732
2733/* The instance lay-out is the same for all three; but the type differs. */
2734
2735typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002736 PyObject_HEAD
2737 PyDictObject *dv_dict;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002738} dictviewobject;
2739
2740
2741static void
2742dictview_dealloc(dictviewobject *dv)
2743{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002744 Py_XDECREF(dv->dv_dict);
2745 PyObject_GC_Del(dv);
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002746}
2747
2748static int
2749dictview_traverse(dictviewobject *dv, visitproc visit, void *arg)
2750{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002751 Py_VISIT(dv->dv_dict);
2752 return 0;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002753}
2754
2755static Py_ssize_t
2756dictview_len(dictviewobject *dv)
2757{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002758 Py_ssize_t len = 0;
2759 if (dv->dv_dict != NULL)
2760 len = dv->dv_dict->ma_used;
2761 return len;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002762}
2763
2764static PyObject *
2765dictview_new(PyObject *dict, PyTypeObject *type)
2766{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002767 dictviewobject *dv;
2768 if (dict == NULL) {
2769 PyErr_BadInternalCall();
2770 return NULL;
2771 }
2772 if (!PyDict_Check(dict)) {
2773 /* XXX Get rid of this restriction later */
2774 PyErr_Format(PyExc_TypeError,
2775 "%s() requires a dict argument, not '%s'",
2776 type->tp_name, dict->ob_type->tp_name);
2777 return NULL;
2778 }
2779 dv = PyObject_GC_New(dictviewobject, type);
2780 if (dv == NULL)
2781 return NULL;
2782 Py_INCREF(dict);
2783 dv->dv_dict = (PyDictObject *)dict;
2784 _PyObject_GC_TRACK(dv);
2785 return (PyObject *)dv;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002786}
2787
2788/* TODO(guido): The views objects are not complete:
2789
2790 * support more set operations
2791 * support arbitrary mappings?
2792 - either these should be static or exported in dictobject.h
2793 - if public then they should probably be in builtins
2794*/
2795
2796/* Return 1 if self is a subset of other, iterating over self;
2797 0 if not; -1 if an error occurred. */
2798static int
2799all_contained_in(PyObject *self, PyObject *other)
2800{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002801 PyObject *iter = PyObject_GetIter(self);
2802 int ok = 1;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002803
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002804 if (iter == NULL)
2805 return -1;
2806 for (;;) {
2807 PyObject *next = PyIter_Next(iter);
2808 if (next == NULL) {
2809 if (PyErr_Occurred())
2810 ok = -1;
2811 break;
2812 }
2813 ok = PySequence_Contains(other, next);
2814 Py_DECREF(next);
2815 if (ok <= 0)
2816 break;
2817 }
2818 Py_DECREF(iter);
2819 return ok;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002820}
2821
2822static PyObject *
2823dictview_richcompare(PyObject *self, PyObject *other, int op)
2824{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002825 Py_ssize_t len_self, len_other;
2826 int ok;
2827 PyObject *result;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002828
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002829 assert(self != NULL);
2830 assert(PyDictViewSet_Check(self));
2831 assert(other != NULL);
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002832
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002833 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other)) {
2834 Py_INCREF(Py_NotImplemented);
2835 return Py_NotImplemented;
2836 }
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002837
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002838 len_self = PyObject_Size(self);
2839 if (len_self < 0)
2840 return NULL;
2841 len_other = PyObject_Size(other);
2842 if (len_other < 0)
2843 return NULL;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002844
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002845 ok = 0;
2846 switch(op) {
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002847
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002848 case Py_NE:
2849 case Py_EQ:
2850 if (len_self == len_other)
2851 ok = all_contained_in(self, other);
2852 if (op == Py_NE && ok >= 0)
2853 ok = !ok;
2854 break;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002855
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002856 case Py_LT:
2857 if (len_self < len_other)
2858 ok = all_contained_in(self, other);
2859 break;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002860
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002861 case Py_LE:
2862 if (len_self <= len_other)
2863 ok = all_contained_in(self, other);
2864 break;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002865
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002866 case Py_GT:
2867 if (len_self > len_other)
2868 ok = all_contained_in(other, self);
2869 break;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002870
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002871 case Py_GE:
2872 if (len_self >= len_other)
2873 ok = all_contained_in(other, self);
2874 break;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002875
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002876 }
2877 if (ok < 0)
2878 return NULL;
2879 result = ok ? Py_True : Py_False;
2880 Py_INCREF(result);
2881 return result;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002882}
2883
2884static PyObject *
2885dictview_repr(dictviewobject *dv)
2886{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002887 PyObject *seq;
2888 PyObject *seq_str;
2889 PyObject *result;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002890
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002891 seq = PySequence_List((PyObject *)dv);
2892 if (seq == NULL)
2893 return NULL;
2894
2895 seq_str = PyObject_Repr(seq);
2896 result = PyString_FromFormat("%s(%s)", Py_TYPE(dv)->tp_name,
2897 PyString_AS_STRING(seq_str));
2898 Py_DECREF(seq_str);
2899 Py_DECREF(seq);
2900 return result;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002901}
2902
2903/*** dict_keys ***/
2904
2905static PyObject *
2906dictkeys_iter(dictviewobject *dv)
2907{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002908 if (dv->dv_dict == NULL) {
2909 Py_RETURN_NONE;
2910 }
2911 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002912}
2913
2914static int
2915dictkeys_contains(dictviewobject *dv, PyObject *obj)
2916{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002917 if (dv->dv_dict == NULL)
2918 return 0;
2919 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002920}
2921
2922static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002923 (lenfunc)dictview_len, /* sq_length */
2924 0, /* sq_concat */
2925 0, /* sq_repeat */
2926 0, /* sq_item */
2927 0, /* sq_slice */
2928 0, /* sq_ass_item */
2929 0, /* sq_ass_slice */
2930 (objobjproc)dictkeys_contains, /* sq_contains */
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002931};
2932
2933static PyObject*
2934dictviews_sub(PyObject* self, PyObject *other)
2935{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002936 PyObject *result = PySet_New(self);
2937 PyObject *tmp;
2938 if (result == NULL)
2939 return NULL;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002940
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002941 tmp = PyObject_CallMethod(result, "difference_update", "O", other);
2942 if (tmp == NULL) {
2943 Py_DECREF(result);
2944 return NULL;
2945 }
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002946
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002947 Py_DECREF(tmp);
2948 return result;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002949}
2950
2951static PyObject*
2952dictviews_and(PyObject* self, PyObject *other)
2953{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002954 PyObject *result = PySet_New(self);
2955 PyObject *tmp;
2956 if (result == NULL)
2957 return NULL;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002958
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002959 tmp = PyObject_CallMethod(result, "intersection_update", "O", other);
2960 if (tmp == NULL) {
2961 Py_DECREF(result);
2962 return NULL;
2963 }
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002964
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002965 Py_DECREF(tmp);
2966 return result;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002967}
2968
2969static PyObject*
2970dictviews_or(PyObject* self, PyObject *other)
2971{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002972 PyObject *result = PySet_New(self);
2973 PyObject *tmp;
2974 if (result == NULL)
2975 return NULL;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002976
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002977 tmp = PyObject_CallMethod(result, "update", "O", other);
2978 if (tmp == NULL) {
2979 Py_DECREF(result);
2980 return NULL;
2981 }
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002982
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002983 Py_DECREF(tmp);
2984 return result;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002985}
2986
2987static PyObject*
2988dictviews_xor(PyObject* self, PyObject *other)
2989{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002990 PyObject *result = PySet_New(self);
2991 PyObject *tmp;
2992 if (result == NULL)
2993 return NULL;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00002994
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002995 tmp = PyObject_CallMethod(result, "symmetric_difference_update", "O",
2996 other);
2997 if (tmp == NULL) {
2998 Py_DECREF(result);
2999 return NULL;
3000 }
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003001
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003002 Py_DECREF(tmp);
3003 return result;
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003004}
3005
3006static PyNumberMethods dictviews_as_number = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003007 0, /*nb_add*/
3008 (binaryfunc)dictviews_sub, /*nb_subtract*/
3009 0, /*nb_multiply*/
3010 0, /*nb_divide*/
3011 0, /*nb_remainder*/
3012 0, /*nb_divmod*/
3013 0, /*nb_power*/
3014 0, /*nb_negative*/
3015 0, /*nb_positive*/
3016 0, /*nb_absolute*/
3017 0, /*nb_nonzero*/
3018 0, /*nb_invert*/
3019 0, /*nb_lshift*/
3020 0, /*nb_rshift*/
3021 (binaryfunc)dictviews_and, /*nb_and*/
3022 (binaryfunc)dictviews_xor, /*nb_xor*/
3023 (binaryfunc)dictviews_or, /*nb_or*/
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003024};
3025
3026static PyMethodDef dictkeys_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003027 {NULL, NULL} /* sentinel */
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003028};
3029
3030PyTypeObject PyDictKeys_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003031 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3032 "dict_keys", /* tp_name */
3033 sizeof(dictviewobject), /* tp_basicsize */
3034 0, /* tp_itemsize */
3035 /* methods */
3036 (destructor)dictview_dealloc, /* tp_dealloc */
3037 0, /* tp_print */
3038 0, /* tp_getattr */
3039 0, /* tp_setattr */
3040 0, /* tp_reserved */
3041 (reprfunc)dictview_repr, /* tp_repr */
3042 &dictviews_as_number, /* tp_as_number */
3043 &dictkeys_as_sequence, /* tp_as_sequence */
3044 0, /* tp_as_mapping */
3045 0, /* tp_hash */
3046 0, /* tp_call */
3047 0, /* tp_str */
3048 PyObject_GenericGetAttr, /* tp_getattro */
3049 0, /* tp_setattro */
3050 0, /* tp_as_buffer */
3051 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3052 Py_TPFLAGS_CHECKTYPES, /* tp_flags */
3053 0, /* tp_doc */
3054 (traverseproc)dictview_traverse, /* tp_traverse */
3055 0, /* tp_clear */
3056 dictview_richcompare, /* tp_richcompare */
3057 0, /* tp_weaklistoffset */
3058 (getiterfunc)dictkeys_iter, /* tp_iter */
3059 0, /* tp_iternext */
3060 dictkeys_methods, /* tp_methods */
3061 0,
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003062};
3063
3064static PyObject *
3065dictkeys_new(PyObject *dict)
3066{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003067 return dictview_new(dict, &PyDictKeys_Type);
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003068}
3069
3070/*** dict_items ***/
3071
3072static PyObject *
3073dictitems_iter(dictviewobject *dv)
3074{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003075 if (dv->dv_dict == NULL) {
3076 Py_RETURN_NONE;
3077 }
3078 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003079}
3080
3081static int
3082dictitems_contains(dictviewobject *dv, PyObject *obj)
3083{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003084 PyObject *key, *value, *found;
3085 if (dv->dv_dict == NULL)
3086 return 0;
3087 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
3088 return 0;
3089 key = PyTuple_GET_ITEM(obj, 0);
3090 value = PyTuple_GET_ITEM(obj, 1);
3091 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
3092 if (found == NULL) {
3093 if (PyErr_Occurred())
3094 return -1;
3095 return 0;
3096 }
3097 return PyObject_RichCompareBool(value, found, Py_EQ);
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003098}
3099
3100static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003101 (lenfunc)dictview_len, /* sq_length */
3102 0, /* sq_concat */
3103 0, /* sq_repeat */
3104 0, /* sq_item */
3105 0, /* sq_slice */
3106 0, /* sq_ass_item */
3107 0, /* sq_ass_slice */
3108 (objobjproc)dictitems_contains, /* sq_contains */
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003109};
3110
3111static PyMethodDef dictitems_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003112 {NULL, NULL} /* sentinel */
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003113};
3114
3115PyTypeObject PyDictItems_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003116 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3117 "dict_items", /* tp_name */
3118 sizeof(dictviewobject), /* tp_basicsize */
3119 0, /* tp_itemsize */
3120 /* methods */
3121 (destructor)dictview_dealloc, /* tp_dealloc */
3122 0, /* tp_print */
3123 0, /* tp_getattr */
3124 0, /* tp_setattr */
3125 0, /* tp_reserved */
3126 (reprfunc)dictview_repr, /* tp_repr */
3127 &dictviews_as_number, /* tp_as_number */
3128 &dictitems_as_sequence, /* tp_as_sequence */
3129 0, /* tp_as_mapping */
3130 0, /* tp_hash */
3131 0, /* tp_call */
3132 0, /* tp_str */
3133 PyObject_GenericGetAttr, /* tp_getattro */
3134 0, /* tp_setattro */
3135 0, /* tp_as_buffer */
3136 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3137 Py_TPFLAGS_CHECKTYPES, /* tp_flags */
3138 0, /* tp_doc */
3139 (traverseproc)dictview_traverse, /* tp_traverse */
3140 0, /* tp_clear */
3141 dictview_richcompare, /* tp_richcompare */
3142 0, /* tp_weaklistoffset */
3143 (getiterfunc)dictitems_iter, /* tp_iter */
3144 0, /* tp_iternext */
3145 dictitems_methods, /* tp_methods */
3146 0,
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003147};
3148
3149static PyObject *
3150dictitems_new(PyObject *dict)
3151{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003152 return dictview_new(dict, &PyDictItems_Type);
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003153}
3154
3155/*** dict_values ***/
3156
3157static PyObject *
3158dictvalues_iter(dictviewobject *dv)
3159{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003160 if (dv->dv_dict == NULL) {
3161 Py_RETURN_NONE;
3162 }
3163 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003164}
3165
3166static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003167 (lenfunc)dictview_len, /* sq_length */
3168 0, /* sq_concat */
3169 0, /* sq_repeat */
3170 0, /* sq_item */
3171 0, /* sq_slice */
3172 0, /* sq_ass_item */
3173 0, /* sq_ass_slice */
3174 (objobjproc)0, /* sq_contains */
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003175};
3176
3177static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003178 {NULL, NULL} /* sentinel */
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003179};
3180
3181PyTypeObject PyDictValues_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003182 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3183 "dict_values", /* tp_name */
3184 sizeof(dictviewobject), /* tp_basicsize */
3185 0, /* tp_itemsize */
3186 /* methods */
3187 (destructor)dictview_dealloc, /* tp_dealloc */
3188 0, /* tp_print */
3189 0, /* tp_getattr */
3190 0, /* tp_setattr */
3191 0, /* tp_reserved */
3192 (reprfunc)dictview_repr, /* tp_repr */
3193 0, /* tp_as_number */
3194 &dictvalues_as_sequence, /* tp_as_sequence */
3195 0, /* tp_as_mapping */
3196 0, /* tp_hash */
3197 0, /* tp_call */
3198 0, /* tp_str */
3199 PyObject_GenericGetAttr, /* tp_getattro */
3200 0, /* tp_setattro */
3201 0, /* tp_as_buffer */
3202 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3203 0, /* tp_doc */
3204 (traverseproc)dictview_traverse, /* tp_traverse */
3205 0, /* tp_clear */
3206 0, /* tp_richcompare */
3207 0, /* tp_weaklistoffset */
3208 (getiterfunc)dictvalues_iter, /* tp_iter */
3209 0, /* tp_iternext */
3210 dictvalues_methods, /* tp_methods */
3211 0,
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003212};
3213
3214static PyObject *
3215dictvalues_new(PyObject *dict)
3216{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003217 return dictview_new(dict, &PyDictValues_Type);
Alexandre Vassalotti69eb5162010-01-11 23:17:10 +00003218}