blob: 931b1fca06ed843f0209062c76670a5672af8fff [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{
19 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);
Neal Norwitz7b932da2006-10-29 23:39:03 +000024 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{
144 return dummy;
145}
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{
159 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);
162}
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{
Christian Heimes09bde042008-02-24 12:26:16 +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);
Christian Heimesb4ee4a12008-02-07 17:15:30 +0000178 fprintf(stderr, "%.2f%% reuse rate\n\n",
179 (100.0*count_reuse/(count_alloc+count_reuse)));
180}
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{
191 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)));
197}
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
210#define INIT_NONZERO_DICT_SLOTS(mp) do { \
Tim Petersdea48ec2001-05-22 20:40:22 +0000211 (mp)->ma_table = (mp)->ma_smalltable; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000212 (mp)->ma_mask = PyDict_MINSIZE - 1; \
213 } while(0)
214
215#define EMPTY_TO_MINSIZE(mp) do { \
216 memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000217 (mp)->ma_used = (mp)->ma_fill = 0; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000218 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{
Christian Heimes48397d62008-02-08 00:14:34 +0000231 PyDictObject *op;
Christian Heimesf75dbef2008-02-08 00:11:31 +0000232
233 while (numfree) {
Christian Heimes48397d62008-02-08 00:14:34 +0000234 op = free_list[--numfree];
Christian Heimesf75dbef2008-02-08 00:11:31 +0000235 assert(PyDict_CheckExact(op));
236 PyObject_GC_Del(op);
237 }
238}
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{
Brett Cannon77ae87c2007-10-10 00:07:50 +0000243 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000244 if (dummy == NULL) { /* Auto-initialize dummy */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000245 dummy = PyString_FromString("<dummy key>");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000246 if (dummy == NULL)
247 return NULL;
Fred Drake1bff34a2000-08-31 19:31:38 +0000248#ifdef SHOW_CONVERSION_COUNTS
249 Py_AtExit(show_counts);
250#endif
Christian Heimesb4ee4a12008-02-07 17:15:30 +0000251#ifdef SHOW_ALLOC_COUNT
252 Py_AtExit(show_alloc);
253#endif
Antoine Pitrouf8387af2009-03-23 18:41:45 +0000254#ifdef SHOW_TRACK_COUNT
255 Py_AtExit(show_track);
256#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000257 }
Christian Heimes5b970ad2008-02-06 13:33:44 +0000258 if (numfree) {
259 mp = free_list[--numfree];
Raymond Hettinger43442782004-03-17 21:55:03 +0000260 assert (mp != NULL);
Christian Heimese93237d2007-12-19 02:37:44 +0000261 assert (Py_TYPE(mp) == &PyDict_Type);
Raymond Hettinger43442782004-03-17 21:55:03 +0000262 _Py_NewReference((PyObject *)mp);
263 if (mp->ma_fill) {
264 EMPTY_TO_MINSIZE(mp);
Georg Brandl1e13ea92008-08-11 09:07:59 +0000265 } 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);
Raymond Hettinger43442782004-03-17 21:55:03 +0000269 }
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
274 count_reuse++;
275#endif
Raymond Hettinger43442782004-03-17 21:55:03 +0000276 } else {
Brett Cannon77ae87c2007-10-10 00:07:50 +0000277 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
Raymond Hettinger43442782004-03-17 21:55:03 +0000278 if (mp == NULL)
279 return NULL;
280 EMPTY_TO_MINSIZE(mp);
Christian Heimesb4ee4a12008-02-07 17:15:30 +0000281#ifdef SHOW_ALLOC_COUNT
282 count_alloc++;
283#endif
Raymond Hettinger43442782004-03-17 21:55:03 +0000284 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000285 mp->ma_lookup = lookdict_string;
Antoine Pitrouf8387af2009-03-23 18:41:45 +0000286#ifdef SHOW_TRACK_COUNT
287 count_untracked++;
288#endif
Fred Drake1bff34a2000-08-31 19:31:38 +0000289#ifdef SHOW_CONVERSION_COUNTS
290 ++created;
291#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +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{
Tim Petersd770ebd2006-06-01 15:50:44 +0000322 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000323 register size_t perturb;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000324 register PyDictEntry *freeslot;
Tim Petersd770ebd2006-06-01 15:50:44 +0000325 register size_t mask = (size_t)mp->ma_mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000326 PyDictEntry *ep0 = mp->ma_table;
327 register PyDictEntry *ep;
Fred Drakec88b99c2000-08-31 19:04:07 +0000328 register int cmp;
Tim Peters453163d2001-06-03 04:54:32 +0000329 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000330
Tim Petersd770ebd2006-06-01 15:50:44 +0000331 i = (size_t)hash & mask;
Guido van Rossumefb46091997-01-29 15:53:56 +0000332 ep = &ep0[i];
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000333 if (ep->me_key == NULL || ep->me_key == key)
Guido van Rossum7d181591997-01-16 21:06:45 +0000334 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000335
Guido van Rossum16e93a81997-01-28 00:00:11 +0000336 if (ep->me_key == dummy)
Guido van Rossum7d181591997-01-16 21:06:45 +0000337 freeslot = ep;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000338 else {
Fred Drakec88b99c2000-08-31 19:04:07 +0000339 if (ep->me_hash == hash) {
Tim Peters453163d2001-06-03 04:54:32 +0000340 startkey = ep->me_key;
Georg Brandlede3a322007-11-29 18:33:01 +0000341 Py_INCREF(startkey);
Tim Peters453163d2001-06-03 04:54:32 +0000342 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Georg Brandlede3a322007-11-29 18:33:01 +0000343 Py_DECREF(startkey);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000344 if (cmp < 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000345 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000346 if (ep0 == mp->ma_table && ep->me_key == startkey) {
347 if (cmp > 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000348 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000349 }
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 */
Armin Rigo35f6d362006-06-01 13:19:12 +0000356 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000357 }
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000358 }
359 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000360 }
Tim Peters15d49292001-05-27 07:39:22 +0000361
Tim Peters342c65e2001-05-13 06:43:53 +0000362 /* In the loop, me_key == dummy is by far (factor of 100s) the
363 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000364 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
365 i = (i << 2) + i + perturb + 1;
366 ep = &ep0[i & mask];
Armin Rigo35f6d362006-06-01 13:19:12 +0000367 if (ep->me_key == NULL)
368 return freeslot == NULL ? ep : freeslot;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000369 if (ep->me_key == key)
Armin Rigo35f6d362006-06-01 13:19:12 +0000370 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000371 if (ep->me_hash == hash && ep->me_key != dummy) {
Tim Peters453163d2001-06-03 04:54:32 +0000372 startkey = ep->me_key;
Georg Brandlede3a322007-11-29 18:33:01 +0000373 Py_INCREF(startkey);
Tim Peters453163d2001-06-03 04:54:32 +0000374 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Georg Brandlede3a322007-11-29 18:33:01 +0000375 Py_DECREF(startkey);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000376 if (cmp < 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000377 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000378 if (ep0 == mp->ma_table && ep->me_key == startkey) {
379 if (cmp > 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000380 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000381 }
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 */
Armin Rigo35f6d362006-06-01 13:19:12 +0000388 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000389 }
Guido van Rossum16e93a81997-01-28 00:00:11 +0000390 }
Tim Peters342c65e2001-05-13 06:43:53 +0000391 else if (ep->me_key == dummy && freeslot == NULL)
392 freeslot = ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000393 }
Neal Norwitza5ccda92006-10-28 21:16:54 +0000394 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{
Tim Petersd770ebd2006-06-01 15:50:44 +0000410 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000411 register size_t perturb;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000412 register PyDictEntry *freeslot;
Tim Petersd770ebd2006-06-01 15:50:44 +0000413 register size_t mask = (size_t)mp->ma_mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000414 PyDictEntry *ep0 = mp->ma_table;
415 register PyDictEntry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000416
Tim Peters0ab085c2001-09-14 00:25:33 +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. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000421 if (!PyString_CheckExact(key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000422#ifdef SHOW_CONVERSION_COUNTS
423 ++converted;
424#endif
425 mp->ma_lookup = lookdict;
426 return lookdict(mp, key, hash);
427 }
Tim Peters2f228e72001-05-13 00:19:31 +0000428 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000429 ep = &ep0[i];
430 if (ep->me_key == NULL || ep->me_key == key)
431 return ep;
432 if (ep->me_key == dummy)
433 freeslot = ep;
434 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000435 if (ep->me_hash == hash && _PyString_Eq(ep->me_key, key))
Fred Drake1bff34a2000-08-31 19:31:38 +0000436 return ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000437 freeslot = NULL;
438 }
Tim Peters15d49292001-05-27 07:39:22 +0000439
Tim Peters342c65e2001-05-13 06:43:53 +0000440 /* In the loop, me_key == dummy is by far (factor of 100s) the
441 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000442 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
443 i = (i << 2) + i + perturb + 1;
444 ep = &ep0[i & mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000445 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
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000450 && _PyString_Eq(ep->me_key, key)))
Fred Drake1bff34a2000-08-31 19:31:38 +0000451 return ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000452 if (ep->me_key == dummy && freeslot == NULL)
Tim Peters342c65e2001-05-13 06:43:53 +0000453 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000454 }
Neal Norwitza5ccda92006-10-28 21:16:54 +0000455 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 \
461 (count_tracked++, count_untracked--);
462#define DECREASE_TRACK_COUNT \
463 (count_tracked--, count_untracked++);
464#else
465#define INCREASE_TRACK_COUNT
466#define DECREASE_TRACK_COUNT
467#endif
468
469#define MAINTAIN_TRACKING(mp, key, value) \
470 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)
479
480void
481_PyDict_MaybeUntrack(PyObject *op)
482{
483 PyDictObject *mp;
484 PyObject *value;
485 Py_ssize_t mask, i;
486 PyDictEntry *ep;
487
488 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 }
Antoine Pitrou1fba6242009-03-23 19:17:00 +0000501 DECREASE_TRACK_COUNT
Antoine Pitrouf8387af2009-03-23 18:41:45 +0000502 _PyObject_GC_UNTRACK(op);
503}
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{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000515 PyObject *old_value;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000516 register PyDictEntry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000517 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
518
519 assert(mp->ma_lookup != NULL);
520 ep = mp->ma_lookup(mp, key, hash);
Armin Rigo35f6d362006-06-01 13:19:12 +0000521 if (ep == NULL) {
522 Py_DECREF(key);
523 Py_DECREF(value);
524 return -1;
525 }
Antoine Pitrouf8387af2009-03-23 18:41:45 +0000526 MAINTAIN_TRACKING(mp, key, value);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000527 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000528 old_value = ep->me_value;
529 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000530 Py_DECREF(old_value); /* which **CAN** re-enter */
531 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000532 }
533 else {
534 if (ep->me_key == NULL)
535 mp->ma_fill++;
Raymond Hettinger07ead172005-02-05 23:42:57 +0000536 else {
537 assert(ep->me_key == dummy);
538 Py_DECREF(dummy);
539 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000540 ep->me_key = key;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000541 ep->me_hash = (Py_ssize_t)hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000542 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000543 mp->ma_used++;
544 }
Armin Rigo35f6d362006-06-01 13:19:12 +0000545 return 0;
546}
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,
Armin Rigo35f6d362006-06-01 13:19:12 +0000558 PyObject *value)
559{
Tim Petersd770ebd2006-06-01 15:50:44 +0000560 register size_t i;
Armin Rigo35f6d362006-06-01 13:19:12 +0000561 register size_t perturb;
Tim Petersd770ebd2006-06-01 15:50:44 +0000562 register size_t mask = (size_t)mp->ma_mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000563 PyDictEntry *ep0 = mp->ma_table;
564 register PyDictEntry *ep;
Armin Rigo35f6d362006-06-01 13:19:12 +0000565
Antoine Pitrouf8387af2009-03-23 18:41:45 +0000566 MAINTAIN_TRACKING(mp, key, value);
Armin Rigo35f6d362006-06-01 13:19:12 +0000567 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 }
Tim Petersd770ebd2006-06-01 15:50:44 +0000573 assert(ep->me_value == NULL);
Armin Rigo35f6d362006-06-01 13:19:12 +0000574 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{
Tim Peters9b10f7e2006-05-30 04:16:25 +0000589 Py_ssize_t newsize;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000590 PyDictEntry *oldtable, *newtable, *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000591 Py_ssize_t i;
Tim Peters0c6010b2001-05-23 23:33:57 +0000592 int is_oldtable_malloced;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000593 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000594
595 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000596
Tim Peterseb28ef22001-06-02 05:27:19 +0000597 /* Find the smallest table size > minused. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000598 for (newsize = PyDict_MINSIZE;
Tim Peterseb28ef22001-06-02 05:27:19 +0000599 newsize <= minused && newsize > 0;
600 newsize <<= 1)
601 ;
602 if (newsize <= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000603 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000604 return -1;
605 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000606
607 /* Get space for a new table. */
608 oldtable = mp->ma_table;
609 assert(oldtable != NULL);
610 is_oldtable_malloced = oldtable != mp->ma_smalltable;
611
Tim Peters6d6c1a32001-08-02 04:15:00 +0000612 if (newsize == PyDict_MINSIZE) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000613 /* A large table is shrinking, or we can't get any smaller. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000614 newtable = mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000615 if (newtable == oldtable) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000616 if (mp->ma_fill == mp->ma_used) {
617 /* No dummies, so no point doing anything. */
Tim Peters0c6010b2001-05-23 23:33:57 +0000618 return 0;
Tim Petersf8a548c2001-05-24 16:26:40 +0000619 }
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);
Tim Peters0c6010b2001-05-23 23:33:57 +0000627 memcpy(small_copy, oldtable, sizeof(small_copy));
628 oldtable = small_copy;
629 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000630 }
631 else {
Brett Cannon77ae87c2007-10-10 00:07:50 +0000632 newtable = PyMem_NEW(PyDictEntry, newsize);
Tim Petersdea48ec2001-05-22 20:40:22 +0000633 if (newtable == NULL) {
634 PyErr_NoMemory();
635 return -1;
636 }
637 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000638
639 /* Make the dict empty, using the new table. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000640 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000641 mp->ma_table = newtable;
Tim Petersafb6ae82001-06-04 21:00:21 +0000642 mp->ma_mask = newsize - 1;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000643 memset(newtable, 0, sizeof(PyDictEntry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000644 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000645 i = mp->ma_fill;
646 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000647
Tim Peters19283142001-05-17 22:25:34 +0000648 /* Copy the data over; this is refcount-neutral for active entries;
649 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000650 for (ep = oldtable; i > 0; ep++) {
651 if (ep->me_value != NULL) { /* active entry */
652 --i;
Armin Rigo35f6d362006-06-01 13:19:12 +0000653 insertdict_clean(mp, ep->me_key, (long)ep->me_hash,
654 ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000655 }
Tim Peters19283142001-05-17 22:25:34 +0000656 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000657 --i;
Tim Peters19283142001-05-17 22:25:34 +0000658 assert(ep->me_key == dummy);
659 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000660 }
Tim Peters19283142001-05-17 22:25:34 +0000661 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000662 }
663
Tim Peters0c6010b2001-05-23 23:33:57 +0000664 if (is_oldtable_malloced)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000665 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000666 return 0;
667}
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{
677 PyObject *op = PyDict_New();
678
679 if (minused>5 && op != NULL && dictresize((PyDictObject *)op, minused) == -1) {
680 Py_DECREF(op);
681 return NULL;
682 }
683 return op;
684}
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{
699 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000700 PyDictObject *mp = (PyDictObject *)op;
701 PyDictEntry *ep;
Armin Rigo35f6d362006-06-01 13:19:12 +0000702 PyThreadState *tstate;
Tim Petersd770ebd2006-06-01 15:50:44 +0000703 if (!PyDict_Check(op))
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000704 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000705 if (!PyString_CheckExact(key) ||
706 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Guido van Rossumca756f21997-01-23 19:39:29 +0000707 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000708 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000709 if (hash == -1) {
710 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000711 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000712 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000713 }
Armin Rigo35f6d362006-06-01 13:19:12 +0000714
715 /* We can arrive here with a NULL tstate during initialization:
716 try running "python -Wi" for an example related to string
717 interning. Let's just hope that no exception occurs then... */
Armin Rigoacd0d6d2006-06-10 10:57:40 +0000718 tstate = _PyThreadState_Current;
Armin Rigo35f6d362006-06-01 13:19:12 +0000719 if (tstate != NULL && tstate->curexc_type != NULL) {
720 /* preserve the existing exception */
721 PyObject *err_type, *err_value, *err_tb;
722 PyErr_Fetch(&err_type, &err_value, &err_tb);
723 ep = (mp->ma_lookup)(mp, key, hash);
724 /* ignore errors */
725 PyErr_Restore(err_type, err_value, err_tb);
726 if (ep == NULL)
727 return NULL;
728 }
729 else {
730 ep = (mp->ma_lookup)(mp, key, hash);
731 if (ep == NULL) {
732 PyErr_Clear();
733 return NULL;
734 }
735 }
736 return ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000737}
738
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000739/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
Tim Peters60b29962006-01-01 01:19:23 +0000740 * dictionary if it's merely replacing the value for an existing key.
741 * This means that it's safe to loop over a dictionary with PyDict_Next()
742 * and occasionally replace a value -- but you can't insert new keys or
743 * remove them.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000744 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000745int
Tim Peters1f5871e2000-07-04 17:44:48 +0000746PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000747{
Brett Cannon77ae87c2007-10-10 00:07:50 +0000748 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000749 register long hash;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000750 register Py_ssize_t n_used;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000751
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000752 if (!PyDict_Check(op)) {
753 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000754 return -1;
755 }
Neal Norwitz48808a12006-07-21 05:29:58 +0000756 assert(key);
757 assert(value);
Brett Cannon77ae87c2007-10-10 00:07:50 +0000758 mp = (PyDictObject *)op;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000759 if (PyString_CheckExact(key)) {
760 hash = ((PyStringObject *)key)->ob_shash;
Guido van Rossum45ec02a2002-08-19 21:43:18 +0000761 if (hash == -1)
762 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000763 }
Tim Peters1f7df352002-03-29 03:29:08 +0000764 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000765 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000766 if (hash == -1)
767 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000768 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000769 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000770 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000771 Py_INCREF(value);
772 Py_INCREF(key);
Armin Rigo35f6d362006-06-01 13:19:12 +0000773 if (insertdict(mp, key, hash, value) != 0)
774 return -1;
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000775 /* If we added a key, we can safely resize. Otherwise just return!
776 * If fill >= 2/3 size, adjust size. Normally, this doubles or
777 * quaduples the size, but it's also possible for the dict to shrink
Tim Peters60b29962006-01-01 01:19:23 +0000778 * (if ma_fill is much larger than ma_used, meaning a lot of dict
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000779 * keys have been * deleted).
Tim Peters60b29962006-01-01 01:19:23 +0000780 *
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000781 * Quadrupling the size improves average dictionary sparseness
782 * (reducing collisions) at the cost of some memory and iteration
783 * speed (which loops over every possible entry). It also halves
Neal Norwitz80af59c2006-05-30 04:19:21 +0000784 * the number of expensive resize operations in a growing dictionary.
Tim Peters60b29962006-01-01 01:19:23 +0000785 *
786 * Very large dictionaries (over 50K items) use doubling instead.
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000787 * This may help applications with severe memory constraints.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000788 */
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000789 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
790 return 0;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000791 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000792}
793
794int
Tim Peters1f5871e2000-07-04 17:44:48 +0000795PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000796{
Brett Cannon77ae87c2007-10-10 00:07:50 +0000797 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000798 register long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000799 register PyDictEntry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000800 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000801
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000802 if (!PyDict_Check(op)) {
803 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000804 return -1;
805 }
Neal Norwitz48808a12006-07-21 05:29:58 +0000806 assert(key);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000807 if (!PyString_CheckExact(key) ||
808 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000809 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000810 if (hash == -1)
811 return -1;
812 }
Brett Cannon77ae87c2007-10-10 00:07:50 +0000813 mp = (PyDictObject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000814 ep = (mp->ma_lookup)(mp, key, hash);
Armin Rigo35f6d362006-06-01 13:19:12 +0000815 if (ep == NULL)
816 return -1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000817 if (ep->me_value == NULL) {
Georg Brandlb9f4ad32006-10-29 18:31:42 +0000818 set_key_error(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000819 return -1;
820 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000821 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000822 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000823 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000824 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000825 ep->me_value = NULL;
826 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000827 Py_DECREF(old_value);
828 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000829 return 0;
830}
831
Guido van Rossum25831651993-05-19 14:50:45 +0000832void
Tim Peters1f5871e2000-07-04 17:44:48 +0000833PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000834{
Brett Cannon77ae87c2007-10-10 00:07:50 +0000835 PyDictObject *mp;
836 PyDictEntry *ep, *table;
Tim Petersdea48ec2001-05-22 20:40:22 +0000837 int table_is_malloced;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000838 Py_ssize_t fill;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000839 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000840#ifdef Py_DEBUG
Tim Peters9b10f7e2006-05-30 04:16:25 +0000841 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +0000842#endif
843
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000844 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000845 return;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000846 mp = (PyDictObject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000847#ifdef Py_DEBUG
Tim Petersafb6ae82001-06-04 21:00:21 +0000848 n = mp->ma_mask + 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000849 i = 0;
850#endif
851
852 table = mp->ma_table;
853 assert(table != NULL);
854 table_is_malloced = table != mp->ma_smalltable;
855
856 /* This is delicate. During the process of clearing the dict,
857 * decrefs can cause the dict to mutate. To avoid fatal confusion
858 * (voice of experience), we have to make the dict empty before
859 * clearing the slots, and never refer to anything via mp->xxx while
860 * clearing.
861 */
862 fill = mp->ma_fill;
863 if (table_is_malloced)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000864 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000865
866 else if (fill > 0) {
867 /* It's a small table with something that needs to be cleared.
868 * Afraid the only safe way is to copy the dict entries into
869 * another small table first.
870 */
871 memcpy(small_copy, table, sizeof(small_copy));
872 table = small_copy;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000873 EMPTY_TO_MINSIZE(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000874 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000875 /* else it's a small table that's already empty */
876
877 /* Now we can finally clear things. If C had refcounts, we could
878 * assert that the refcount on table is 1 now, i.e. that this function
879 * has unique access to it, so decref side-effects can't alter it.
880 */
881 for (ep = table; fill > 0; ++ep) {
882#ifdef Py_DEBUG
883 assert(i < n);
884 ++i;
885#endif
886 if (ep->me_key) {
887 --fill;
888 Py_DECREF(ep->me_key);
889 Py_XDECREF(ep->me_value);
890 }
891#ifdef Py_DEBUG
892 else
893 assert(ep->me_value == NULL);
894#endif
895 }
896
897 if (table_is_malloced)
898 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000899}
900
Tim Peters080c88b2003-02-15 03:01:11 +0000901/*
902 * Iterate over a dict. Use like so:
903 *
Tim Peters9b10f7e2006-05-30 04:16:25 +0000904 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +0000905 * PyObject *key, *value;
906 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +0000907 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +0000908 * Refer to borrowed references in key and value.
909 * }
910 *
911 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +0000912 * mutates the dict. One exception: it is safe if the loop merely changes
913 * the values associated with the keys (but doesn't insert new keys or
914 * delete keys), via PyDict_SetItem().
915 */
Guido van Rossum25831651993-05-19 14:50:45 +0000916int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000917PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000918{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000919 register Py_ssize_t i;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000920 register Py_ssize_t mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000921 register PyDictEntry *ep;
Raymond Hettinger43442782004-03-17 21:55:03 +0000922
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000923 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000924 return 0;
Guido van Rossum25831651993-05-19 14:50:45 +0000925 i = *ppos;
926 if (i < 0)
927 return 0;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000928 ep = ((PyDictObject *)op)->ma_table;
929 mask = ((PyDictObject *)op)->ma_mask;
Raymond Hettinger43442782004-03-17 21:55:03 +0000930 while (i <= mask && ep[i].me_value == NULL)
Guido van Rossum25831651993-05-19 14:50:45 +0000931 i++;
932 *ppos = i+1;
Raymond Hettinger43442782004-03-17 21:55:03 +0000933 if (i > mask)
Guido van Rossum25831651993-05-19 14:50:45 +0000934 return 0;
935 if (pkey)
Raymond Hettinger43442782004-03-17 21:55:03 +0000936 *pkey = ep[i].me_key;
Guido van Rossum25831651993-05-19 14:50:45 +0000937 if (pvalue)
Raymond Hettinger43442782004-03-17 21:55:03 +0000938 *pvalue = ep[i].me_value;
Guido van Rossum25831651993-05-19 14:50:45 +0000939 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000940}
941
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000942/* Internal version of PyDict_Next that returns a hash value in addition to the key and value.*/
943int
944_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, long *phash)
945{
946 register Py_ssize_t i;
947 register Py_ssize_t mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000948 register PyDictEntry *ep;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000949
950 if (!PyDict_Check(op))
951 return 0;
952 i = *ppos;
953 if (i < 0)
954 return 0;
Brett Cannon77ae87c2007-10-10 00:07:50 +0000955 ep = ((PyDictObject *)op)->ma_table;
956 mask = ((PyDictObject *)op)->ma_mask;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000957 while (i <= mask && ep[i].me_value == NULL)
958 i++;
959 *ppos = i+1;
960 if (i > mask)
961 return 0;
962 *phash = (long)(ep[i].me_hash);
963 if (pkey)
964 *pkey = ep[i].me_key;
965 if (pvalue)
966 *pvalue = ep[i].me_value;
967 return 1;
968}
969
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000970/* Methods */
971
972static void
Brett Cannon77ae87c2007-10-10 00:07:50 +0000973dict_dealloc(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000974{
Brett Cannon77ae87c2007-10-10 00:07:50 +0000975 register PyDictEntry *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000976 Py_ssize_t fill = mp->ma_fill;
Guido van Rossumff413af2002-03-28 20:34:59 +0000977 PyObject_GC_UnTrack(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000978 Py_TRASHCAN_SAFE_BEGIN(mp)
Tim Petersdea48ec2001-05-22 20:40:22 +0000979 for (ep = mp->ma_table; fill > 0; ep++) {
980 if (ep->me_key) {
981 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000982 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000983 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000984 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000985 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000986 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000987 PyMem_DEL(mp->ma_table);
Christian Heimes5b970ad2008-02-06 13:33:44 +0000988 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
989 free_list[numfree++] = mp;
Tim Peters60b29962006-01-01 01:19:23 +0000990 else
Christian Heimese93237d2007-12-19 02:37:44 +0000991 Py_TYPE(mp)->tp_free((PyObject *)mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000992 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000993}
994
995static int
Brett Cannon77ae87c2007-10-10 00:07:50 +0000996dict_print(register PyDictObject *mp, register FILE *fp, register int flags)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000997{
Tim Peters9b10f7e2006-05-30 04:16:25 +0000998 register Py_ssize_t i;
999 register Py_ssize_t any;
Tim Peters33f4a6a2006-05-30 05:23:59 +00001000 int status;
Guido van Rossum255443b1998-04-10 22:47:14 +00001001
Tim Peters33f4a6a2006-05-30 05:23:59 +00001002 status = Py_ReprEnter((PyObject*)mp);
1003 if (status != 0) {
1004 if (status < 0)
1005 return status;
Brett Cannon01531592007-09-17 03:28:34 +00001006 Py_BEGIN_ALLOW_THREADS
Guido van Rossum255443b1998-04-10 22:47:14 +00001007 fprintf(fp, "{...}");
Brett Cannon01531592007-09-17 03:28:34 +00001008 Py_END_ALLOW_THREADS
Guido van Rossum255443b1998-04-10 22:47:14 +00001009 return 0;
1010 }
1011
Brett Cannon01531592007-09-17 03:28:34 +00001012 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001013 fprintf(fp, "{");
Brett Cannon01531592007-09-17 03:28:34 +00001014 Py_END_ALLOW_THREADS
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001015 any = 0;
Tim Petersafb6ae82001-06-04 21:00:21 +00001016 for (i = 0; i <= mp->ma_mask; i++) {
Brett Cannon77ae87c2007-10-10 00:07:50 +00001017 PyDictEntry *ep = mp->ma_table + i;
Tim Peters23cf6be2001-06-02 08:02:56 +00001018 PyObject *pvalue = ep->me_value;
1019 if (pvalue != NULL) {
1020 /* Prevent PyObject_Repr from deleting value during
1021 key format */
1022 Py_INCREF(pvalue);
Brett Cannon01531592007-09-17 03:28:34 +00001023 if (any++ > 0) {
1024 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001025 fprintf(fp, ", ");
Brett Cannon01531592007-09-17 03:28:34 +00001026 Py_END_ALLOW_THREADS
1027 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001028 if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
Tim Peters23cf6be2001-06-02 08:02:56 +00001029 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +00001030 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001031 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +00001032 }
Brett Cannon01531592007-09-17 03:28:34 +00001033 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001034 fprintf(fp, ": ");
Brett Cannon01531592007-09-17 03:28:34 +00001035 Py_END_ALLOW_THREADS
Tim Peters19b77cf2001-06-02 08:27:39 +00001036 if (PyObject_Print(pvalue, fp, 0) != 0) {
Tim Peters23cf6be2001-06-02 08:02:56 +00001037 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +00001038 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001039 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +00001040 }
Tim Peters23cf6be2001-06-02 08:02:56 +00001041 Py_DECREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001042 }
1043 }
Brett Cannon01531592007-09-17 03:28:34 +00001044 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001045 fprintf(fp, "}");
Brett Cannon01531592007-09-17 03:28:34 +00001046 Py_END_ALLOW_THREADS
Guido van Rossum255443b1998-04-10 22:47:14 +00001047 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001048 return 0;
1049}
1050
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001051static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001052dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001053{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001054 Py_ssize_t i;
Tim Petersa7259592001-06-16 05:11:17 +00001055 PyObject *s, *temp, *colon = NULL;
1056 PyObject *pieces = NULL, *result = NULL;
1057 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +00001058
Tim Petersa7259592001-06-16 05:11:17 +00001059 i = Py_ReprEnter((PyObject *)mp);
Guido van Rossum255443b1998-04-10 22:47:14 +00001060 if (i != 0) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001061 return i > 0 ? PyString_FromString("{...}") : NULL;
Guido van Rossum255443b1998-04-10 22:47:14 +00001062 }
1063
Tim Petersa7259592001-06-16 05:11:17 +00001064 if (mp->ma_used == 0) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001065 result = PyString_FromString("{}");
Tim Petersa7259592001-06-16 05:11:17 +00001066 goto Done;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001067 }
Tim Petersa7259592001-06-16 05:11:17 +00001068
1069 pieces = PyList_New(0);
1070 if (pieces == NULL)
1071 goto Done;
1072
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001073 colon = PyString_FromString(": ");
Tim Petersa7259592001-06-16 05:11:17 +00001074 if (colon == NULL)
1075 goto Done;
1076
1077 /* Do repr() on each key+value pair, and insert ": " between them.
1078 Note that repr may mutate the dict. */
Tim Petersc6057842001-06-16 07:52:53 +00001079 i = 0;
1080 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Tim Petersa7259592001-06-16 05:11:17 +00001081 int status;
1082 /* Prevent repr from deleting value during key format. */
1083 Py_INCREF(value);
1084 s = PyObject_Repr(key);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001085 PyString_Concat(&s, colon);
1086 PyString_ConcatAndDel(&s, PyObject_Repr(value));
Tim Petersa7259592001-06-16 05:11:17 +00001087 Py_DECREF(value);
1088 if (s == NULL)
1089 goto Done;
1090 status = PyList_Append(pieces, s);
1091 Py_DECREF(s); /* append created a new ref */
1092 if (status < 0)
1093 goto Done;
1094 }
1095
1096 /* Add "{}" decorations to the first and last items. */
1097 assert(PyList_GET_SIZE(pieces) > 0);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001098 s = PyString_FromString("{");
Tim Petersa7259592001-06-16 05:11:17 +00001099 if (s == NULL)
1100 goto Done;
1101 temp = PyList_GET_ITEM(pieces, 0);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001102 PyString_ConcatAndDel(&s, temp);
Tim Petersa7259592001-06-16 05:11:17 +00001103 PyList_SET_ITEM(pieces, 0, s);
1104 if (s == NULL)
1105 goto Done;
1106
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001107 s = PyString_FromString("}");
Tim Petersa7259592001-06-16 05:11:17 +00001108 if (s == NULL)
1109 goto Done;
1110 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001111 PyString_ConcatAndDel(&temp, s);
Tim Petersa7259592001-06-16 05:11:17 +00001112 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
1113 if (temp == NULL)
1114 goto Done;
1115
1116 /* Paste them all together with ", " between. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001117 s = PyString_FromString(", ");
Tim Petersa7259592001-06-16 05:11:17 +00001118 if (s == NULL)
1119 goto Done;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001120 result = _PyString_Join(s, pieces);
Tim Peters0ab085c2001-09-14 00:25:33 +00001121 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +00001122
1123Done:
1124 Py_XDECREF(pieces);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001125 Py_XDECREF(colon);
Tim Petersa7259592001-06-16 05:11:17 +00001126 Py_ReprLeave((PyObject *)mp);
1127 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001128}
1129
Martin v. Löwis18e16552006-02-15 17:27:45 +00001130static Py_ssize_t
Brett Cannon77ae87c2007-10-10 00:07:50 +00001131dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001132{
1133 return mp->ma_used;
1134}
1135
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001136static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001137dict_subscript(PyDictObject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001138{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001139 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001140 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001141 PyDictEntry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +00001142 assert(mp->ma_table != NULL);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001143 if (!PyString_CheckExact(key) ||
1144 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001145 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001146 if (hash == -1)
1147 return NULL;
1148 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001149 ep = (mp->ma_lookup)(mp, key, hash);
1150 if (ep == NULL)
1151 return NULL;
1152 v = ep->me_value;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001153 if (v == NULL) {
1154 if (!PyDict_CheckExact(mp)) {
1155 /* Look up __missing__ method if we're a subclass. */
Guido van Rossum4b92a822006-02-25 23:32:30 +00001156 PyObject *missing;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001157 static PyObject *missing_str = NULL;
1158 if (missing_str == NULL)
Tim Peters9b10f7e2006-05-30 04:16:25 +00001159 missing_str =
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001160 PyString_InternFromString("__missing__");
Christian Heimese93237d2007-12-19 02:37:44 +00001161 missing = _PyType_Lookup(Py_TYPE(mp), missing_str);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001162 if (missing != NULL)
1163 return PyObject_CallFunctionObjArgs(missing,
1164 (PyObject *)mp, key, NULL);
1165 }
Georg Brandlb9f4ad32006-10-29 18:31:42 +00001166 set_key_error(key);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001167 return NULL;
1168 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001169 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001170 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001171 return v;
1172}
1173
1174static int
Brett Cannon77ae87c2007-10-10 00:07:50 +00001175dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001176{
1177 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001178 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001179 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001180 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001181}
1182
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001183static PyMappingMethods dict_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001184 (lenfunc)dict_length, /*mp_length*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001185 (binaryfunc)dict_subscript, /*mp_subscript*/
1186 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001187};
1188
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001189static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001190dict_keys(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001191{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001192 register PyObject *v;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001193 register Py_ssize_t i, j;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001194 PyDictEntry *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001195 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001196
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001197 again:
1198 n = mp->ma_used;
1199 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001200 if (v == NULL)
1201 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001202 if (n != mp->ma_used) {
1203 /* Durnit. The allocations caused the dict to resize.
1204 * Just start over, this shouldn't normally happen.
1205 */
1206 Py_DECREF(v);
1207 goto again;
1208 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001209 ep = mp->ma_table;
1210 mask = mp->ma_mask;
1211 for (i = 0, j = 0; i <= mask; i++) {
1212 if (ep[i].me_value != NULL) {
1213 PyObject *key = ep[i].me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001214 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001215 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001216 j++;
1217 }
1218 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001219 assert(j == n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001220 return v;
1221}
1222
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001223static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001224dict_values(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001225{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001226 register PyObject *v;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001227 register Py_ssize_t i, j;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001228 PyDictEntry *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001229 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001230
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001231 again:
1232 n = mp->ma_used;
1233 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001234 if (v == NULL)
1235 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001236 if (n != mp->ma_used) {
1237 /* Durnit. The allocations caused the dict to resize.
1238 * Just start over, this shouldn't normally happen.
1239 */
1240 Py_DECREF(v);
1241 goto again;
1242 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001243 ep = mp->ma_table;
1244 mask = mp->ma_mask;
1245 for (i = 0, j = 0; i <= mask; i++) {
1246 if (ep[i].me_value != NULL) {
1247 PyObject *value = ep[i].me_value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001248 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001249 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001250 j++;
1251 }
1252 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001253 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001254 return v;
1255}
1256
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001257static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001258dict_items(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001259{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001260 register PyObject *v;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001261 register Py_ssize_t i, j, n;
1262 Py_ssize_t mask;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001263 PyObject *item, *key, *value;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001264 PyDictEntry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001265
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001266 /* Preallocate the list of tuples, to avoid allocations during
1267 * the loop over the items, which could trigger GC, which
1268 * could resize the dict. :-(
1269 */
1270 again:
1271 n = mp->ma_used;
1272 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001273 if (v == NULL)
1274 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001275 for (i = 0; i < n; i++) {
1276 item = PyTuple_New(2);
1277 if (item == NULL) {
1278 Py_DECREF(v);
1279 return NULL;
1280 }
1281 PyList_SET_ITEM(v, i, item);
1282 }
1283 if (n != mp->ma_used) {
1284 /* Durnit. The allocations caused the dict to resize.
1285 * Just start over, this shouldn't normally happen.
1286 */
1287 Py_DECREF(v);
1288 goto again;
1289 }
1290 /* Nothing we do below makes any function calls. */
Raymond Hettinger43442782004-03-17 21:55:03 +00001291 ep = mp->ma_table;
1292 mask = mp->ma_mask;
1293 for (i = 0, j = 0; i <= mask; i++) {
Raymond Hettinger06905122004-03-19 10:30:00 +00001294 if ((value=ep[i].me_value) != NULL) {
Raymond Hettinger43442782004-03-17 21:55:03 +00001295 key = ep[i].me_key;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001296 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001297 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001298 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001299 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001300 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001301 j++;
1302 }
1303 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001304 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001305 return v;
1306}
1307
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001308static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001309dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001310{
1311 PyObject *seq;
1312 PyObject *value = Py_None;
1313 PyObject *it; /* iter(seq) */
1314 PyObject *key;
1315 PyObject *d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001316 int status;
1317
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001318 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001319 return NULL;
1320
1321 d = PyObject_CallObject(cls, NULL);
1322 if (d == NULL)
1323 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001324
Raymond Hettingercdcf8872007-11-07 02:26:17 +00001325 if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) {
1326 PyDictObject *mp = (PyDictObject *)d;
1327 PyObject *oldvalue;
1328 Py_ssize_t pos = 0;
1329 PyObject *key;
1330 long hash;
1331
Raymond Hettingera27474c2008-06-28 22:16:53 +00001332 if (dictresize(mp, Py_SIZE(seq)))
Raymond Hettingercdcf8872007-11-07 02:26:17 +00001333 return NULL;
1334
1335 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
1336 Py_INCREF(key);
1337 Py_INCREF(value);
1338 if (insertdict(mp, key, hash, value))
1339 return NULL;
1340 }
1341 return d;
1342 }
1343
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00001344 if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
Brett Cannon77ae87c2007-10-10 00:07:50 +00001345 PyDictObject *mp = (PyDictObject *)d;
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00001346 Py_ssize_t pos = 0;
1347 PyObject *key;
1348 long hash;
1349
1350 if (dictresize(mp, PySet_GET_SIZE(seq)))
1351 return NULL;
1352
1353 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
1354 Py_INCREF(key);
Raymond Hettingere3146f52007-03-21 20:33:57 +00001355 Py_INCREF(value);
1356 if (insertdict(mp, key, hash, value))
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00001357 return NULL;
1358 }
1359 return d;
1360 }
1361
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001362 it = PyObject_GetIter(seq);
1363 if (it == NULL){
1364 Py_DECREF(d);
1365 return NULL;
1366 }
1367
Raymond Hettinger34448792007-11-09 23:14:44 +00001368 if (PyDict_CheckExact(d)) {
1369 while ((key = PyIter_Next(it)) != NULL) {
1370 status = PyDict_SetItem(d, key, value);
1371 Py_DECREF(key);
1372 if (status < 0)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001373 goto Fail;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001374 }
Raymond Hettinger34448792007-11-09 23:14:44 +00001375 } else {
1376 while ((key = PyIter_Next(it)) != NULL) {
1377 status = PyObject_SetItem(d, key, value);
1378 Py_DECREF(key);
1379 if (status < 0)
1380 goto Fail;
1381 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001382 }
1383
Raymond Hettinger34448792007-11-09 23:14:44 +00001384 if (PyErr_Occurred())
1385 goto Fail;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001386 Py_DECREF(it);
1387 return d;
1388
1389Fail:
1390 Py_DECREF(it);
1391 Py_DECREF(d);
1392 return NULL;
1393}
1394
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001395static int
1396dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001397{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001398 PyObject *arg = NULL;
1399 int result = 0;
1400
1401 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1402 result = -1;
1403
1404 else if (arg != NULL) {
1405 if (PyObject_HasAttrString(arg, "keys"))
1406 result = PyDict_Merge(self, arg, 1);
1407 else
1408 result = PyDict_MergeFromSeq2(self, arg, 1);
1409 }
1410 if (result == 0 && kwds != NULL)
1411 result = PyDict_Merge(self, kwds, 1);
1412 return result;
1413}
1414
1415static PyObject *
1416dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1417{
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001418 if (dict_update_common(self, args, kwds, "update") != -1)
1419 Py_RETURN_NONE;
1420 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001421}
1422
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001423/* Update unconditionally replaces existing items.
1424 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001425 otherwise it leaves existing items unchanged.
1426
1427 PyDict_{Update,Merge} update/merge from a mapping object.
1428
Tim Petersf582b822001-12-11 18:51:08 +00001429 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001430 producing iterable objects of length 2.
1431*/
1432
Tim Petersf582b822001-12-11 18:51:08 +00001433int
Tim Peters1fc240e2001-10-26 05:06:50 +00001434PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1435{
1436 PyObject *it; /* iter(seq2) */
Tim Peters9b10f7e2006-05-30 04:16:25 +00001437 Py_ssize_t i; /* index into seq2 of current element */
Tim Peters1fc240e2001-10-26 05:06:50 +00001438 PyObject *item; /* seq2[i] */
1439 PyObject *fast; /* item as a 2-tuple or 2-list */
1440
1441 assert(d != NULL);
1442 assert(PyDict_Check(d));
1443 assert(seq2 != NULL);
1444
1445 it = PyObject_GetIter(seq2);
1446 if (it == NULL)
1447 return -1;
1448
1449 for (i = 0; ; ++i) {
1450 PyObject *key, *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001451 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001452
1453 fast = NULL;
1454 item = PyIter_Next(it);
1455 if (item == NULL) {
1456 if (PyErr_Occurred())
1457 goto Fail;
1458 break;
1459 }
1460
1461 /* Convert item to sequence, and verify length 2. */
1462 fast = PySequence_Fast(item, "");
1463 if (fast == NULL) {
1464 if (PyErr_ExceptionMatches(PyExc_TypeError))
1465 PyErr_Format(PyExc_TypeError,
1466 "cannot convert dictionary update "
Neal Norwitzd3881b02006-05-30 04:25:05 +00001467 "sequence element #%zd to a sequence",
Tim Peters1fc240e2001-10-26 05:06:50 +00001468 i);
1469 goto Fail;
1470 }
1471 n = PySequence_Fast_GET_SIZE(fast);
1472 if (n != 2) {
1473 PyErr_Format(PyExc_ValueError,
Neal Norwitzd3881b02006-05-30 04:25:05 +00001474 "dictionary update sequence element #%zd "
Martin v. Löwis2c95cc62006-02-16 06:54:25 +00001475 "has length %zd; 2 is required",
Martin v. Löwise0e89f72006-02-16 06:59:22 +00001476 i, n);
Tim Peters1fc240e2001-10-26 05:06:50 +00001477 goto Fail;
1478 }
1479
1480 /* Update/merge with this (key, value) pair. */
1481 key = PySequence_Fast_GET_ITEM(fast, 0);
1482 value = PySequence_Fast_GET_ITEM(fast, 1);
1483 if (override || PyDict_GetItem(d, key) == NULL) {
1484 int status = PyDict_SetItem(d, key, value);
1485 if (status < 0)
1486 goto Fail;
1487 }
1488 Py_DECREF(fast);
1489 Py_DECREF(item);
1490 }
1491
1492 i = 0;
1493 goto Return;
1494Fail:
1495 Py_XDECREF(item);
1496 Py_XDECREF(fast);
1497 i = -1;
1498Return:
1499 Py_DECREF(it);
Neal Norwitzd3881b02006-05-30 04:25:05 +00001500 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001501}
1502
Tim Peters6d6c1a32001-08-02 04:15:00 +00001503int
1504PyDict_Update(PyObject *a, PyObject *b)
1505{
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001506 return PyDict_Merge(a, b, 1);
1507}
1508
1509int
1510PyDict_Merge(PyObject *a, PyObject *b, int override)
1511{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001512 register PyDictObject *mp, *other;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001513 register Py_ssize_t i;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001514 PyDictEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001515
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001516 /* We accept for the argument either a concrete dictionary object,
1517 * or an abstract "mapping" object. For the former, we can do
1518 * things quite efficiently. For the latter, we only require that
1519 * PyMapping_Keys() and PyObject_GetItem() be supported.
1520 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001521 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1522 PyErr_BadInternalCall();
1523 return -1;
1524 }
Brett Cannon77ae87c2007-10-10 00:07:50 +00001525 mp = (PyDictObject*)a;
Raymond Hettinger3dbd4c52008-01-25 19:24:46 +00001526 if (PyDict_Check(b)) {
Brett Cannon77ae87c2007-10-10 00:07:50 +00001527 other = (PyDictObject*)b;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001528 if (other == mp || other->ma_used == 0)
1529 /* a.update(a) or a.update({}); nothing to do */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001530 return 0;
Raymond Hettinger186e7392005-05-14 18:08:25 +00001531 if (mp->ma_used == 0)
1532 /* Since the target dict is empty, PyDict_GetItem()
1533 * always returns NULL. Setting override to 1
1534 * skips the unnecessary test.
1535 */
1536 override = 1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001537 /* Do one big resize at the start, rather than
1538 * incrementally resizing as we insert new items. Expect
1539 * that there will be no (or few) overlapping keys.
1540 */
1541 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
Raymond Hettingerc8d22902003-05-07 00:49:40 +00001542 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001543 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001544 }
1545 for (i = 0; i <= other->ma_mask; i++) {
1546 entry = &other->ma_table[i];
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001547 if (entry->me_value != NULL &&
1548 (override ||
1549 PyDict_GetItem(a, entry->me_key) == NULL)) {
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001550 Py_INCREF(entry->me_key);
1551 Py_INCREF(entry->me_value);
Armin Rigo35f6d362006-06-01 13:19:12 +00001552 if (insertdict(mp, entry->me_key,
1553 (long)entry->me_hash,
1554 entry->me_value) != 0)
1555 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001556 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001557 }
1558 }
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001559 else {
1560 /* Do it the generic, slower way */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001561 PyObject *keys = PyMapping_Keys(b);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001562 PyObject *iter;
1563 PyObject *key, *value;
1564 int status;
1565
1566 if (keys == NULL)
1567 /* Docstring says this is equivalent to E.keys() so
1568 * if E doesn't have a .keys() method we want
1569 * AttributeError to percolate up. Might as well
1570 * do the same for any other error.
1571 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001572 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001573
1574 iter = PyObject_GetIter(keys);
1575 Py_DECREF(keys);
1576 if (iter == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001577 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001578
1579 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001580 if (!override && PyDict_GetItem(a, key) != NULL) {
1581 Py_DECREF(key);
1582 continue;
1583 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001584 value = PyObject_GetItem(b, key);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001585 if (value == NULL) {
1586 Py_DECREF(iter);
1587 Py_DECREF(key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001588 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001589 }
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001590 status = PyDict_SetItem(a, key, value);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001591 Py_DECREF(key);
1592 Py_DECREF(value);
1593 if (status < 0) {
1594 Py_DECREF(iter);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001595 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001596 }
1597 }
1598 Py_DECREF(iter);
1599 if (PyErr_Occurred())
1600 /* Iterator completed, via error */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001601 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001602 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001603 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001604}
1605
1606static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001607dict_copy(register PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001608{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001609 return PyDict_Copy((PyObject*)mp);
1610}
1611
1612PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001613PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001614{
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001615 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001616
1617 if (o == NULL || !PyDict_Check(o)) {
1618 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001619 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001620 }
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001621 copy = PyDict_New();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001622 if (copy == NULL)
1623 return NULL;
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001624 if (PyDict_Merge(copy, o, 1) == 0)
1625 return copy;
1626 Py_DECREF(copy);
Raymond Hettinger1356f782005-04-15 15:58:42 +00001627 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001628}
1629
Martin v. Löwis18e16552006-02-15 17:27:45 +00001630Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001631PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001632{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001633 if (mp == NULL || !PyDict_Check(mp)) {
1634 PyErr_BadInternalCall();
Jeremy Hylton7083bb72004-02-17 20:10:11 +00001635 return -1;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001636 }
Brett Cannon77ae87c2007-10-10 00:07:50 +00001637 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001638}
1639
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001640PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001641PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001642{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001643 if (mp == NULL || !PyDict_Check(mp)) {
1644 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001645 return NULL;
1646 }
Brett Cannon77ae87c2007-10-10 00:07:50 +00001647 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001648}
1649
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001650PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001651PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001652{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001653 if (mp == NULL || !PyDict_Check(mp)) {
1654 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001655 return NULL;
1656 }
Brett Cannon77ae87c2007-10-10 00:07:50 +00001657 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001658}
1659
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001660PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001661PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001662{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001663 if (mp == NULL || !PyDict_Check(mp)) {
1664 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001665 return NULL;
1666 }
Brett Cannon77ae87c2007-10-10 00:07:50 +00001667 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001668}
1669
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001670/* Subroutine which returns the smallest key in a for which b's value
1671 is different or absent. The value is returned too, through the
Tim Peters95bf9392001-05-10 08:32:44 +00001672 pval argument. Both are NULL if no key in a is found for which b's status
1673 differs. The refcounts on (and only on) non-NULL *pval and function return
1674 values must be decremented by the caller (characterize() increments them
1675 to ensure that mutating comparison and PyDict_GetItem calls can't delete
1676 them before the caller is done looking at them). */
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001677
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001678static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001679characterize(PyDictObject *a, PyDictObject *b, PyObject **pval)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001680{
Tim Peters95bf9392001-05-10 08:32:44 +00001681 PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
1682 PyObject *aval = NULL; /* a[akey] */
Tim Peters9b10f7e2006-05-30 04:16:25 +00001683 Py_ssize_t i;
1684 int cmp;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001685
Tim Petersafb6ae82001-06-04 21:00:21 +00001686 for (i = 0; i <= a->ma_mask; i++) {
Tim Peters95bf9392001-05-10 08:32:44 +00001687 PyObject *thiskey, *thisaval, *thisbval;
1688 if (a->ma_table[i].me_value == NULL)
1689 continue;
1690 thiskey = a->ma_table[i].me_key;
1691 Py_INCREF(thiskey); /* keep alive across compares */
1692 if (akey != NULL) {
1693 cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
1694 if (cmp < 0) {
1695 Py_DECREF(thiskey);
1696 goto Fail;
1697 }
1698 if (cmp > 0 ||
Tim Petersafb6ae82001-06-04 21:00:21 +00001699 i > a->ma_mask ||
Tim Peters95bf9392001-05-10 08:32:44 +00001700 a->ma_table[i].me_value == NULL)
1701 {
1702 /* Not the *smallest* a key; or maybe it is
1703 * but the compare shrunk the dict so we can't
1704 * find its associated value anymore; or
1705 * maybe it is but the compare deleted the
1706 * a[thiskey] entry.
Neal Norwitzd3881b02006-05-30 04:25:05 +00001707 */
Tim Peters95bf9392001-05-10 08:32:44 +00001708 Py_DECREF(thiskey);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001709 continue;
Guido van Rossumb9324202001-01-18 00:39:02 +00001710 }
Tim Peters95bf9392001-05-10 08:32:44 +00001711 }
1712
1713 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
1714 thisaval = a->ma_table[i].me_value;
1715 assert(thisaval);
1716 Py_INCREF(thisaval); /* keep alive */
1717 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
1718 if (thisbval == NULL)
1719 cmp = 0;
1720 else {
1721 /* both dicts have thiskey: same values? */
1722 cmp = PyObject_RichCompareBool(
1723 thisaval, thisbval, Py_EQ);
1724 if (cmp < 0) {
1725 Py_DECREF(thiskey);
1726 Py_DECREF(thisaval);
1727 goto Fail;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001728 }
1729 }
Tim Peters95bf9392001-05-10 08:32:44 +00001730 if (cmp == 0) {
1731 /* New winner. */
1732 Py_XDECREF(akey);
1733 Py_XDECREF(aval);
1734 akey = thiskey;
1735 aval = thisaval;
1736 }
1737 else {
1738 Py_DECREF(thiskey);
1739 Py_DECREF(thisaval);
1740 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001741 }
Tim Peters95bf9392001-05-10 08:32:44 +00001742 *pval = aval;
1743 return akey;
1744
1745Fail:
1746 Py_XDECREF(akey);
1747 Py_XDECREF(aval);
1748 *pval = NULL;
1749 return NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001750}
1751
1752static int
Brett Cannon77ae87c2007-10-10 00:07:50 +00001753dict_compare(PyDictObject *a, PyDictObject *b)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001754{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001755 PyObject *adiff, *bdiff, *aval, *bval;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001756 int res;
1757
1758 /* Compare lengths first */
1759 if (a->ma_used < b->ma_used)
1760 return -1; /* a is shorter */
1761 else if (a->ma_used > b->ma_used)
1762 return 1; /* b is shorter */
Tim Peters95bf9392001-05-10 08:32:44 +00001763
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001764 /* Same length -- check all keys */
Tim Peters95bf9392001-05-10 08:32:44 +00001765 bdiff = bval = NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001766 adiff = characterize(a, b, &aval);
Tim Peters95bf9392001-05-10 08:32:44 +00001767 if (adiff == NULL) {
1768 assert(!aval);
Tim Peters3918fb22001-05-10 18:58:31 +00001769 /* Either an error, or a is a subset with the same length so
Tim Peters95bf9392001-05-10 08:32:44 +00001770 * must be equal.
1771 */
1772 res = PyErr_Occurred() ? -1 : 0;
1773 goto Finished;
1774 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001775 bdiff = characterize(b, a, &bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001776 if (bdiff == NULL && PyErr_Occurred()) {
1777 assert(!bval);
1778 res = -1;
1779 goto Finished;
1780 }
1781 res = 0;
1782 if (bdiff) {
1783 /* bdiff == NULL "should be" impossible now, but perhaps
1784 * the last comparison done by the characterize() on a had
1785 * the side effect of making the dicts equal!
1786 */
1787 res = PyObject_Compare(adiff, bdiff);
1788 }
1789 if (res == 0 && bval != NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001790 res = PyObject_Compare(aval, bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001791
1792Finished:
1793 Py_XDECREF(adiff);
1794 Py_XDECREF(bdiff);
1795 Py_XDECREF(aval);
1796 Py_XDECREF(bval);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001797 return res;
1798}
1799
Tim Peterse63415e2001-05-08 04:38:29 +00001800/* Return 1 if dicts equal, 0 if not, -1 if error.
1801 * Gets out as soon as any difference is detected.
1802 * Uses only Py_EQ comparison.
1803 */
1804static int
Brett Cannon77ae87c2007-10-10 00:07:50 +00001805dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00001806{
Tim Peters9b10f7e2006-05-30 04:16:25 +00001807 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00001808
1809 if (a->ma_used != b->ma_used)
1810 /* can't be equal if # of entries differ */
1811 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001812
Tim Peterse63415e2001-05-08 04:38:29 +00001813 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Tim Petersafb6ae82001-06-04 21:00:21 +00001814 for (i = 0; i <= a->ma_mask; i++) {
Tim Peterse63415e2001-05-08 04:38:29 +00001815 PyObject *aval = a->ma_table[i].me_value;
1816 if (aval != NULL) {
1817 int cmp;
1818 PyObject *bval;
1819 PyObject *key = a->ma_table[i].me_key;
1820 /* temporarily bump aval's refcount to ensure it stays
1821 alive until we're done with it */
1822 Py_INCREF(aval);
Neal Norwitza22975f2006-09-05 02:24:03 +00001823 /* ditto for key */
1824 Py_INCREF(key);
Tim Peterse63415e2001-05-08 04:38:29 +00001825 bval = PyDict_GetItem((PyObject *)b, key);
Neal Norwitza22975f2006-09-05 02:24:03 +00001826 Py_DECREF(key);
Tim Peterse63415e2001-05-08 04:38:29 +00001827 if (bval == NULL) {
1828 Py_DECREF(aval);
1829 return 0;
1830 }
1831 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1832 Py_DECREF(aval);
1833 if (cmp <= 0) /* error or not equal */
1834 return cmp;
1835 }
1836 }
1837 return 1;
1838 }
1839
1840static PyObject *
1841dict_richcompare(PyObject *v, PyObject *w, int op)
1842{
1843 int cmp;
1844 PyObject *res;
1845
1846 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1847 res = Py_NotImplemented;
1848 }
1849 else if (op == Py_EQ || op == Py_NE) {
Brett Cannon77ae87c2007-10-10 00:07:50 +00001850 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
Tim Peterse63415e2001-05-08 04:38:29 +00001851 if (cmp < 0)
1852 return NULL;
1853 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1854 }
Steven Bethardae42f332008-03-18 17:26:10 +00001855 else {
1856 /* Py3K warning if comparison isn't == or != */
Benjamin Peterson9f4f4812008-04-27 03:01:45 +00001857 if (PyErr_WarnPy3k("dict inequality comparisons not supported "
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00001858 "in 3.x", 1) < 0) {
Steven Bethardae42f332008-03-18 17:26:10 +00001859 return NULL;
1860 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001861 res = Py_NotImplemented;
Steven Bethardae42f332008-03-18 17:26:10 +00001862 }
Tim Peterse63415e2001-05-08 04:38:29 +00001863 Py_INCREF(res);
1864 return res;
1865 }
1866
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001867static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001868dict_contains(register PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001869{
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001870 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001871 PyDictEntry *ep;
Tim Petersd770ebd2006-06-01 15:50:44 +00001872
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001873 if (!PyString_CheckExact(key) ||
1874 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001875 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001876 if (hash == -1)
1877 return NULL;
1878 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001879 ep = (mp->ma_lookup)(mp, key, hash);
1880 if (ep == NULL)
1881 return NULL;
Tim Petersd770ebd2006-06-01 15:50:44 +00001882 return PyBool_FromLong(ep->me_value != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001883}
1884
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001885static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001886dict_has_key(register PyDictObject *mp, PyObject *key)
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +00001887{
Benjamin Peterson9f4f4812008-04-27 03:01:45 +00001888 if (PyErr_WarnPy3k("dict.has_key() not supported in 3.x; "
Benjamin Petersonf19a7b92008-04-27 18:40:21 +00001889 "use the in operator", 1) < 0)
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +00001890 return NULL;
1891 return dict_contains(mp, key);
1892}
1893
1894static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001895dict_get(register PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001896{
1897 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001898 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001899 PyObject *val = NULL;
1900 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001901 PyDictEntry *ep;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001902
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001903 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001904 return NULL;
1905
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001906 if (!PyString_CheckExact(key) ||
1907 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Barry Warsawc38c5da1997-10-06 17:49:20 +00001908 hash = PyObject_Hash(key);
1909 if (hash == -1)
1910 return NULL;
1911 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001912 ep = (mp->ma_lookup)(mp, key, hash);
1913 if (ep == NULL)
1914 return NULL;
1915 val = ep->me_value;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001916 if (val == NULL)
1917 val = failobj;
1918 Py_INCREF(val);
1919 return val;
1920}
1921
1922
1923static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001924dict_setdefault(register PyDictObject *mp, PyObject *args)
Guido van Rossum164452c2000-08-08 16:12:54 +00001925{
1926 PyObject *key;
1927 PyObject *failobj = Py_None;
1928 PyObject *val = NULL;
1929 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001930 PyDictEntry *ep;
Guido van Rossum164452c2000-08-08 16:12:54 +00001931
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001932 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
Guido van Rossum164452c2000-08-08 16:12:54 +00001933 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001934
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001935 if (!PyString_CheckExact(key) ||
1936 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum164452c2000-08-08 16:12:54 +00001937 hash = PyObject_Hash(key);
1938 if (hash == -1)
1939 return NULL;
1940 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001941 ep = (mp->ma_lookup)(mp, key, hash);
1942 if (ep == NULL)
1943 return NULL;
1944 val = ep->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001945 if (val == NULL) {
1946 val = failobj;
1947 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1948 val = NULL;
1949 }
1950 Py_XINCREF(val);
1951 return val;
1952}
1953
1954
1955static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001956dict_clear(register PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001957{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001958 PyDict_Clear((PyObject *)mp);
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001959 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001960}
1961
Guido van Rossumba6ab842000-12-12 22:02:18 +00001962static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00001963dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00001964{
1965 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00001966 PyDictEntry *ep;
Guido van Rossume027d982002-04-12 15:11:59 +00001967 PyObject *old_value, *old_key;
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001968 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001969
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001970 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1971 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001972 if (mp->ma_used == 0) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001973 if (deflt) {
1974 Py_INCREF(deflt);
1975 return deflt;
1976 }
Guido van Rossume027d982002-04-12 15:11:59 +00001977 PyErr_SetString(PyExc_KeyError,
1978 "pop(): dictionary is empty");
1979 return NULL;
1980 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001981 if (!PyString_CheckExact(key) ||
1982 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossume027d982002-04-12 15:11:59 +00001983 hash = PyObject_Hash(key);
1984 if (hash == -1)
1985 return NULL;
1986 }
1987 ep = (mp->ma_lookup)(mp, key, hash);
Armin Rigo35f6d362006-06-01 13:19:12 +00001988 if (ep == NULL)
1989 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001990 if (ep->me_value == NULL) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001991 if (deflt) {
1992 Py_INCREF(deflt);
1993 return deflt;
1994 }
Georg Brandlb9f4ad32006-10-29 18:31:42 +00001995 set_key_error(key);
Guido van Rossume027d982002-04-12 15:11:59 +00001996 return NULL;
1997 }
1998 old_key = ep->me_key;
1999 Py_INCREF(dummy);
2000 ep->me_key = dummy;
2001 old_value = ep->me_value;
2002 ep->me_value = NULL;
2003 mp->ma_used--;
2004 Py_DECREF(old_key);
2005 return old_value;
2006}
2007
2008static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002009dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002010{
Tim Peters9b10f7e2006-05-30 04:16:25 +00002011 Py_ssize_t i = 0;
Brett Cannon77ae87c2007-10-10 00:07:50 +00002012 PyDictEntry *ep;
Guido van Rossumba6ab842000-12-12 22:02:18 +00002013 PyObject *res;
2014
Tim Petersf4b33f62001-06-02 05:42:29 +00002015 /* Allocate the result tuple before checking the size. Believe it
2016 * or not, this allocation could trigger a garbage collection which
2017 * could empty the dict, so if we checked the size first and that
2018 * happened, the result would be an infinite loop (searching for an
2019 * entry that no longer exists). Note that the usual popitem()
2020 * idiom is "while d: k, v = d.popitem()". so needing to throw the
Tim Peters9b10f7e2006-05-30 04:16:25 +00002021 * tuple away if the dict *is* empty isn't a significant
Tim Petersf4b33f62001-06-02 05:42:29 +00002022 * inefficiency -- possible, but unlikely in practice.
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002023 */
2024 res = PyTuple_New(2);
2025 if (res == NULL)
2026 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00002027 if (mp->ma_used == 0) {
2028 Py_DECREF(res);
2029 PyErr_SetString(PyExc_KeyError,
2030 "popitem(): dictionary is empty");
2031 return NULL;
2032 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00002033 /* Set ep to "the first" dict entry with a value. We abuse the hash
2034 * field of slot 0 to hold a search finger:
2035 * If slot 0 has a value, use slot 0.
2036 * Else slot 0 is being used to hold a search finger,
Neal Norwitzd3881b02006-05-30 04:25:05 +00002037 * and we use its hash value as the first index to look.
Guido van Rossumba6ab842000-12-12 22:02:18 +00002038 */
2039 ep = &mp->ma_table[0];
2040 if (ep->me_value == NULL) {
Tim Peters9b10f7e2006-05-30 04:16:25 +00002041 i = ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00002042 /* The hash field may be a real hash value, or it may be a
2043 * legit search finger, or it may be a once-legit search
2044 * finger that's out of bounds now because it wrapped around
2045 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00002046 */
Tim Petersafb6ae82001-06-04 21:00:21 +00002047 if (i > mp->ma_mask || i < 1)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002048 i = 1; /* skip slot 0 */
2049 while ((ep = &mp->ma_table[i])->me_value == NULL) {
2050 i++;
Tim Petersafb6ae82001-06-04 21:00:21 +00002051 if (i > mp->ma_mask)
Guido van Rossumba6ab842000-12-12 22:02:18 +00002052 i = 1;
2053 }
2054 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00002055 PyTuple_SET_ITEM(res, 0, ep->me_key);
2056 PyTuple_SET_ITEM(res, 1, ep->me_value);
2057 Py_INCREF(dummy);
2058 ep->me_key = dummy;
2059 ep->me_value = NULL;
2060 mp->ma_used--;
2061 assert(mp->ma_table[0].me_value == NULL);
2062 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00002063 return res;
2064}
2065
Jeremy Hylton8caad492000-06-23 14:18:11 +00002066static int
2067dict_traverse(PyObject *op, visitproc visit, void *arg)
2068{
Martin v. Löwis18e16552006-02-15 17:27:45 +00002069 Py_ssize_t i = 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002070 PyObject *pk;
2071 PyObject *pv;
2072
2073 while (PyDict_Next(op, &i, &pk, &pv)) {
Thomas Woutersc6e55062006-04-15 21:47:09 +00002074 Py_VISIT(pk);
2075 Py_VISIT(pv);
Jeremy Hylton8caad492000-06-23 14:18:11 +00002076 }
2077 return 0;
2078}
2079
2080static int
2081dict_tp_clear(PyObject *op)
2082{
2083 PyDict_Clear(op);
2084 return 0;
2085}
2086
Tim Petersf7f88b12000-12-13 23:18:45 +00002087
Raymond Hettinger019a1482004-03-18 02:41:19 +00002088extern PyTypeObject PyDictIterKey_Type; /* Forward */
2089extern PyTypeObject PyDictIterValue_Type; /* Forward */
2090extern PyTypeObject PyDictIterItem_Type; /* Forward */
Brett Cannon77ae87c2007-10-10 00:07:50 +00002091static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002092
2093static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002094dict_iterkeys(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002095{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002096 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002097}
2098
2099static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002100dict_itervalues(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002101{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002102 return dictiter_new(dict, &PyDictIterValue_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002103}
2104
2105static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002106dict_iteritems(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002107{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002108 return dictiter_new(dict, &PyDictIterItem_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002109}
2110
Robert Schuppenies51df0642008-06-01 16:16:17 +00002111static PyObject *
2112dict_sizeof(PyDictObject *mp)
2113{
2114 Py_ssize_t res;
2115
Robert Schuppenies161b9212008-06-26 15:20:35 +00002116 res = sizeof(PyDictObject);
Robert Schuppenies51df0642008-06-01 16:16:17 +00002117 if (mp->ma_table != mp->ma_smalltable)
2118 res = res + (mp->ma_mask + 1) * sizeof(PyDictEntry);
2119 return PyInt_FromSsize_t(res);
2120}
Guido van Rossum09e563a2001-05-01 12:10:21 +00002121
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002122PyDoc_STRVAR(has_key__doc__,
Raymond Hettinger574aa322003-09-01 22:12:08 +00002123"D.has_key(k) -> True if D has a key k, else False");
Tim Petersf7f88b12000-12-13 23:18:45 +00002124
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002125PyDoc_STRVAR(contains__doc__,
2126"D.__contains__(k) -> True if D has a key k, else False");
2127
2128PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
2129
Robert Schuppenies51df0642008-06-01 16:16:17 +00002130PyDoc_STRVAR(sizeof__doc__,
Georg Brandl7a6de8b2008-06-01 16:42:16 +00002131"D.__sizeof__() -> size of D in memory, in bytes");
Robert Schuppenies51df0642008-06-01 16:16:17 +00002132
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002133PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002134"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002135
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002136PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002137"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 +00002138
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002139PyDoc_STRVAR(pop__doc__,
Andrew M. Kuchlinga4127172008-10-04 21:51:59 +00002140"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002141If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00002142
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002143PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00002144"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Andrew M. Kuchlinga4127172008-10-04 21:51:59 +000021452-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002146
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002147PyDoc_STRVAR(keys__doc__,
2148"D.keys() -> list of D's keys");
Tim Petersf7f88b12000-12-13 23:18:45 +00002149
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002150PyDoc_STRVAR(items__doc__,
2151"D.items() -> list of D's (key, value) pairs, as 2-tuples");
Tim Petersf7f88b12000-12-13 23:18:45 +00002152
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002153PyDoc_STRVAR(values__doc__,
2154"D.values() -> list of D's values");
Tim Petersf7f88b12000-12-13 23:18:45 +00002155
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002156PyDoc_STRVAR(update__doc__,
Andrew M. Kuchlinga4127172008-10-04 21:51:59 +00002157"D.update(E, **F) -> None. Update D from dict/iterable E and F.\n"
2158"If E has a .keys() method, does: for k in E: D[k] = E[k]\n\
2159If E lacks .keys() method, does: for (k, v) in E: D[k] = v\n\
2160In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00002161
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002162PyDoc_STRVAR(fromkeys__doc__,
2163"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
2164v defaults to None.");
2165
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002166PyDoc_STRVAR(clear__doc__,
2167"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002168
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002169PyDoc_STRVAR(copy__doc__,
2170"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00002171
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002172PyDoc_STRVAR(iterkeys__doc__,
2173"D.iterkeys() -> an iterator over the keys of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00002174
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002175PyDoc_STRVAR(itervalues__doc__,
2176"D.itervalues() -> an iterator over the values of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00002177
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002178PyDoc_STRVAR(iteritems__doc__,
2179"D.iteritems() -> an iterator over the (key, value) items of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00002180
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002181static PyMethodDef mapp_methods[] = {
Neal Norwitzc7926292007-05-23 06:57:35 +00002182 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002183 contains__doc__},
Raymond Hettinger0c669672003-12-13 13:31:55 +00002184 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002185 getitem__doc__},
Robert Schuppenies51df0642008-06-01 16:16:17 +00002186 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
2187 sizeof__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002188 {"has_key", (PyCFunction)dict_has_key, METH_O,
Tim Petersf7f88b12000-12-13 23:18:45 +00002189 has_key__doc__},
2190 {"get", (PyCFunction)dict_get, METH_VARARGS,
2191 get__doc__},
2192 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
2193 setdefault_doc__},
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002194 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
Just van Rossuma797d812002-11-23 09:45:04 +00002195 pop__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002196 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002197 popitem__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002198 {"keys", (PyCFunction)dict_keys, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002199 keys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002200 {"items", (PyCFunction)dict_items, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002201 items__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002202 {"values", (PyCFunction)dict_values, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002203 values__doc__},
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002204 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002205 update__doc__},
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002206 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
2207 fromkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002208 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002209 clear__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002210 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002211 copy__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002212 {"iterkeys", (PyCFunction)dict_iterkeys, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00002213 iterkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002214 {"itervalues", (PyCFunction)dict_itervalues, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00002215 itervalues__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002216 {"iteritems", (PyCFunction)dict_iteritems, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00002217 iteritems__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00002218 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002219};
2220
Tim Petersd770ebd2006-06-01 15:50:44 +00002221/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002222int
2223PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002224{
2225 long hash;
Brett Cannon77ae87c2007-10-10 00:07:50 +00002226 PyDictObject *mp = (PyDictObject *)op;
2227 PyDictEntry *ep;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002228
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002229 if (!PyString_CheckExact(key) ||
2230 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002231 hash = PyObject_Hash(key);
2232 if (hash == -1)
2233 return -1;
2234 }
Armin Rigo35f6d362006-06-01 13:19:12 +00002235 ep = (mp->ma_lookup)(mp, key, hash);
Tim Petersd770ebd2006-06-01 15:50:44 +00002236 return ep == NULL ? -1 : (ep->me_value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002237}
2238
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +00002239/* Internal version of PyDict_Contains used when the hash value is already known */
2240int
2241_PyDict_Contains(PyObject *op, PyObject *key, long hash)
2242{
Brett Cannon77ae87c2007-10-10 00:07:50 +00002243 PyDictObject *mp = (PyDictObject *)op;
2244 PyDictEntry *ep;
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +00002245
2246 ep = (mp->ma_lookup)(mp, key, hash);
2247 return ep == NULL ? -1 : (ep->me_value != NULL);
2248}
2249
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002250/* Hack to implement "key in dict" */
2251static PySequenceMethods dict_as_sequence = {
Georg Brandl347b3002006-03-30 11:57:00 +00002252 0, /* sq_length */
2253 0, /* sq_concat */
2254 0, /* sq_repeat */
2255 0, /* sq_item */
2256 0, /* sq_slice */
2257 0, /* sq_ass_item */
2258 0, /* sq_ass_slice */
2259 PyDict_Contains, /* sq_contains */
2260 0, /* sq_inplace_concat */
2261 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002262};
2263
Guido van Rossum09e563a2001-05-01 12:10:21 +00002264static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002265dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2266{
2267 PyObject *self;
2268
2269 assert(type != NULL && type->tp_alloc != NULL);
2270 self = type->tp_alloc(type, 0);
2271 if (self != NULL) {
2272 PyDictObject *d = (PyDictObject *)self;
2273 /* It's guaranteed that tp->alloc zeroed out the struct. */
2274 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
2275 INIT_NONZERO_DICT_SLOTS(d);
2276 d->ma_lookup = lookdict_string;
Antoine Pitrouf8387af2009-03-23 18:41:45 +00002277 /* The object has been implicitely tracked by tp_alloc */
2278 if (type == &PyDict_Type)
2279 _PyObject_GC_UNTRACK(d);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002280#ifdef SHOW_CONVERSION_COUNTS
2281 ++created;
2282#endif
Antoine Pitrouf8387af2009-03-23 18:41:45 +00002283#ifdef SHOW_TRACK_COUNT
2284 if (_PyObject_GC_IS_TRACKED(d))
2285 count_tracked++;
2286 else
2287 count_untracked++;
2288#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00002289 }
2290 return self;
2291}
2292
Tim Peters25786c02001-09-02 08:22:48 +00002293static int
2294dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2295{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002296 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002297}
2298
Tim Peters6d6c1a32001-08-02 04:15:00 +00002299static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002300dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002301{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002302 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002303}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002304
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002305PyDoc_STRVAR(dictionary_doc,
Tim Petersa427a2b2001-10-29 22:25:45 +00002306"dict() -> new empty dictionary.\n"
2307"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Tim Peters1fc240e2001-10-26 05:06:50 +00002308" (key, value) pairs.\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002309"dict(seq) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002310" d = {}\n"
2311" for k, v in seq:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002312" d[k] = v\n"
2313"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2314" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002315
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002316PyTypeObject PyDict_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002317 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Petersa427a2b2001-10-29 22:25:45 +00002318 "dict",
Brett Cannon77ae87c2007-10-10 00:07:50 +00002319 sizeof(PyDictObject),
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002320 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00002321 (destructor)dict_dealloc, /* tp_dealloc */
2322 (printfunc)dict_print, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002323 0, /* tp_getattr */
Guido van Rossumb9324202001-01-18 00:39:02 +00002324 0, /* tp_setattr */
Tim Peters4fa58bf2001-05-10 21:45:19 +00002325 (cmpfunc)dict_compare, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00002326 (reprfunc)dict_repr, /* tp_repr */
2327 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002328 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00002329 &dict_as_mapping, /* tp_as_mapping */
Nick Coghlan53663a62008-07-15 14:27:37 +00002330 (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
Guido van Rossumb9324202001-01-18 00:39:02 +00002331 0, /* tp_call */
2332 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002333 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumb9324202001-01-18 00:39:02 +00002334 0, /* tp_setattro */
2335 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002336 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Neal Norwitzee3a1b52007-02-25 19:44:48 +00002337 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
Tim Peters25786c02001-09-02 08:22:48 +00002338 dictionary_doc, /* tp_doc */
Georg Brandl347b3002006-03-30 11:57:00 +00002339 dict_traverse, /* tp_traverse */
2340 dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00002341 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002342 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00002343 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002344 0, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002345 mapp_methods, /* tp_methods */
2346 0, /* tp_members */
2347 0, /* tp_getset */
2348 0, /* tp_base */
2349 0, /* tp_dict */
2350 0, /* tp_descr_get */
2351 0, /* tp_descr_set */
2352 0, /* tp_dictoffset */
Georg Brandl347b3002006-03-30 11:57:00 +00002353 dict_init, /* tp_init */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002354 PyType_GenericAlloc, /* tp_alloc */
2355 dict_new, /* tp_new */
Neil Schemenauer6189b892002-04-12 02:43:00 +00002356 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002357};
2358
Guido van Rossum3cca2451997-05-16 14:23:33 +00002359/* For backward compatibility with old dictionary interface */
2360
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002361PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002362PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002363{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002364 PyObject *kv, *rv;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002365 kv = PyString_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002366 if (kv == NULL)
2367 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002368 rv = PyDict_GetItem(v, kv);
2369 Py_DECREF(kv);
2370 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002371}
2372
2373int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002374PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002375{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002376 PyObject *kv;
2377 int err;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002378 kv = PyString_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002379 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002380 return -1;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002381 PyString_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00002382 err = PyDict_SetItem(v, kv, item);
2383 Py_DECREF(kv);
2384 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002385}
2386
2387int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002388PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002389{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002390 PyObject *kv;
2391 int err;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002392 kv = PyString_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002393 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002394 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002395 err = PyDict_DelItem(v, kv);
2396 Py_DECREF(kv);
2397 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002398}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002399
Raymond Hettinger019a1482004-03-18 02:41:19 +00002400/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002401
2402typedef struct {
2403 PyObject_HEAD
Brett Cannon77ae87c2007-10-10 00:07:50 +00002404 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
Tim Peters9b10f7e2006-05-30 04:16:25 +00002405 Py_ssize_t di_used;
2406 Py_ssize_t di_pos;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002407 PyObject* di_result; /* reusable result tuple for iteritems */
Tim Peters9b10f7e2006-05-30 04:16:25 +00002408 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002409} dictiterobject;
2410
2411static PyObject *
Brett Cannon77ae87c2007-10-10 00:07:50 +00002412dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002413{
2414 dictiterobject *di;
Antoine Pitrouaa687902009-01-01 14:11:22 +00002415 di = PyObject_GC_New(dictiterobject, itertype);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002416 if (di == NULL)
2417 return NULL;
2418 Py_INCREF(dict);
2419 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00002420 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002421 di->di_pos = 0;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002422 di->len = dict->ma_used;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002423 if (itertype == &PyDictIterItem_Type) {
2424 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2425 if (di->di_result == NULL) {
2426 Py_DECREF(di);
2427 return NULL;
2428 }
2429 }
2430 else
2431 di->di_result = NULL;
Antoine Pitrouaa687902009-01-01 14:11:22 +00002432 _PyObject_GC_TRACK(di);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002433 return (PyObject *)di;
2434}
2435
2436static void
2437dictiter_dealloc(dictiterobject *di)
2438{
Guido van Rossum2147df72002-07-16 20:30:22 +00002439 Py_XDECREF(di->di_dict);
Raymond Hettinger019a1482004-03-18 02:41:19 +00002440 Py_XDECREF(di->di_result);
Antoine Pitrouaa687902009-01-01 14:11:22 +00002441 PyObject_GC_Del(di);
2442}
2443
2444static int
2445dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
2446{
2447 Py_VISIT(di->di_dict);
2448 Py_VISIT(di->di_result);
2449 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002450}
2451
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002452static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002453dictiter_len(dictiterobject *di)
2454{
Tim Peters9b10f7e2006-05-30 04:16:25 +00002455 Py_ssize_t len = 0;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00002456 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002457 len = di->len;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002458 return PyInt_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002459}
2460
Armin Rigof5b3e362006-02-11 21:32:43 +00002461PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002462
2463static PyMethodDef dictiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +00002464 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002465 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002466};
2467
Raymond Hettinger019a1482004-03-18 02:41:19 +00002468static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002469{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002470 PyObject *key;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002471 register Py_ssize_t i, mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +00002472 register PyDictEntry *ep;
2473 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002474
Raymond Hettinger019a1482004-03-18 02:41:19 +00002475 if (d == NULL)
Guido van Rossum2147df72002-07-16 20:30:22 +00002476 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002477 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002478
Raymond Hettinger019a1482004-03-18 02:41:19 +00002479 if (di->di_used != d->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00002480 PyErr_SetString(PyExc_RuntimeError,
2481 "dictionary changed size during iteration");
Guido van Rossum2147df72002-07-16 20:30:22 +00002482 di->di_used = -1; /* Make this state sticky */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002483 return NULL;
2484 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002485
Raymond Hettinger019a1482004-03-18 02:41:19 +00002486 i = di->di_pos;
2487 if (i < 0)
2488 goto fail;
2489 ep = d->ma_table;
2490 mask = d->ma_mask;
2491 while (i <= mask && ep[i].me_value == NULL)
2492 i++;
2493 di->di_pos = i+1;
2494 if (i > mask)
2495 goto fail;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002496 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002497 key = ep[i].me_key;
2498 Py_INCREF(key);
2499 return key;
2500
2501fail:
2502 Py_DECREF(d);
Guido van Rossum2147df72002-07-16 20:30:22 +00002503 di->di_dict = NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002504 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002505}
2506
Raymond Hettinger019a1482004-03-18 02:41:19 +00002507PyTypeObject PyDictIterKey_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002508 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002509 "dictionary-keyiterator", /* tp_name */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002510 sizeof(dictiterobject), /* tp_basicsize */
2511 0, /* tp_itemsize */
2512 /* methods */
2513 (destructor)dictiter_dealloc, /* tp_dealloc */
2514 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002515 0, /* tp_getattr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002516 0, /* tp_setattr */
2517 0, /* tp_compare */
2518 0, /* tp_repr */
2519 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002520 0, /* tp_as_sequence */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002521 0, /* tp_as_mapping */
2522 0, /* tp_hash */
2523 0, /* tp_call */
2524 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002525 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002526 0, /* tp_setattro */
2527 0, /* tp_as_buffer */
Antoine Pitrouaa687902009-01-01 14:11:22 +00002528 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002529 0, /* tp_doc */
Antoine Pitrouaa687902009-01-01 14:11:22 +00002530 (traverseproc)dictiter_traverse, /* tp_traverse */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002531 0, /* tp_clear */
2532 0, /* tp_richcompare */
2533 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002534 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002535 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002536 dictiter_methods, /* tp_methods */
2537 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002538};
2539
2540static PyObject *dictiter_iternextvalue(dictiterobject *di)
2541{
2542 PyObject *value;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002543 register Py_ssize_t i, mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +00002544 register PyDictEntry *ep;
2545 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002546
2547 if (d == NULL)
2548 return NULL;
2549 assert (PyDict_Check(d));
2550
2551 if (di->di_used != d->ma_used) {
2552 PyErr_SetString(PyExc_RuntimeError,
2553 "dictionary changed size during iteration");
2554 di->di_used = -1; /* Make this state sticky */
2555 return NULL;
2556 }
2557
2558 i = di->di_pos;
Guido van Rossum09240f62004-03-20 19:11:58 +00002559 mask = d->ma_mask;
2560 if (i < 0 || i > mask)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002561 goto fail;
2562 ep = d->ma_table;
Guido van Rossum09240f62004-03-20 19:11:58 +00002563 while ((value=ep[i].me_value) == NULL) {
Raymond Hettinger019a1482004-03-18 02:41:19 +00002564 i++;
Guido van Rossum09240f62004-03-20 19:11:58 +00002565 if (i > mask)
2566 goto fail;
2567 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002568 di->di_pos = i+1;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002569 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002570 Py_INCREF(value);
2571 return value;
2572
2573fail:
2574 Py_DECREF(d);
2575 di->di_dict = NULL;
2576 return NULL;
2577}
2578
2579PyTypeObject PyDictIterValue_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002580 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002581 "dictionary-valueiterator", /* tp_name */
2582 sizeof(dictiterobject), /* tp_basicsize */
2583 0, /* tp_itemsize */
2584 /* methods */
2585 (destructor)dictiter_dealloc, /* tp_dealloc */
2586 0, /* tp_print */
2587 0, /* tp_getattr */
2588 0, /* tp_setattr */
2589 0, /* tp_compare */
2590 0, /* tp_repr */
2591 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002592 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002593 0, /* tp_as_mapping */
2594 0, /* tp_hash */
2595 0, /* tp_call */
2596 0, /* tp_str */
2597 PyObject_GenericGetAttr, /* tp_getattro */
2598 0, /* tp_setattro */
2599 0, /* tp_as_buffer */
Antoine Pitrouaa687902009-01-01 14:11:22 +00002600 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002601 0, /* tp_doc */
Antoine Pitrouaa687902009-01-01 14:11:22 +00002602 (traverseproc)dictiter_traverse, /* tp_traverse */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002603 0, /* tp_clear */
2604 0, /* tp_richcompare */
2605 0, /* tp_weaklistoffset */
2606 PyObject_SelfIter, /* tp_iter */
2607 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002608 dictiter_methods, /* tp_methods */
2609 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002610};
2611
2612static PyObject *dictiter_iternextitem(dictiterobject *di)
2613{
2614 PyObject *key, *value, *result = di->di_result;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002615 register Py_ssize_t i, mask;
Brett Cannon77ae87c2007-10-10 00:07:50 +00002616 register PyDictEntry *ep;
2617 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002618
2619 if (d == NULL)
2620 return NULL;
2621 assert (PyDict_Check(d));
2622
2623 if (di->di_used != d->ma_used) {
2624 PyErr_SetString(PyExc_RuntimeError,
2625 "dictionary changed size during iteration");
2626 di->di_used = -1; /* Make this state sticky */
2627 return NULL;
2628 }
2629
2630 i = di->di_pos;
2631 if (i < 0)
2632 goto fail;
2633 ep = d->ma_table;
2634 mask = d->ma_mask;
2635 while (i <= mask && ep[i].me_value == NULL)
2636 i++;
2637 di->di_pos = i+1;
2638 if (i > mask)
2639 goto fail;
2640
2641 if (result->ob_refcnt == 1) {
2642 Py_INCREF(result);
2643 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2644 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2645 } else {
2646 result = PyTuple_New(2);
Tim Peters60b29962006-01-01 01:19:23 +00002647 if (result == NULL)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002648 return NULL;
2649 }
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002650 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002651 key = ep[i].me_key;
2652 value = ep[i].me_value;
2653 Py_INCREF(key);
2654 Py_INCREF(value);
2655 PyTuple_SET_ITEM(result, 0, key);
2656 PyTuple_SET_ITEM(result, 1, value);
2657 return result;
2658
2659fail:
2660 Py_DECREF(d);
2661 di->di_dict = NULL;
2662 return NULL;
2663}
2664
2665PyTypeObject PyDictIterItem_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002666 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002667 "dictionary-itemiterator", /* tp_name */
2668 sizeof(dictiterobject), /* tp_basicsize */
2669 0, /* tp_itemsize */
2670 /* methods */
2671 (destructor)dictiter_dealloc, /* tp_dealloc */
2672 0, /* tp_print */
2673 0, /* tp_getattr */
2674 0, /* tp_setattr */
2675 0, /* tp_compare */
2676 0, /* tp_repr */
2677 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002678 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002679 0, /* tp_as_mapping */
2680 0, /* tp_hash */
2681 0, /* tp_call */
2682 0, /* tp_str */
2683 PyObject_GenericGetAttr, /* tp_getattro */
2684 0, /* tp_setattro */
2685 0, /* tp_as_buffer */
Antoine Pitrouaa687902009-01-01 14:11:22 +00002686 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002687 0, /* tp_doc */
Antoine Pitrouaa687902009-01-01 14:11:22 +00002688 (traverseproc)dictiter_traverse, /* tp_traverse */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002689 0, /* tp_clear */
2690 0, /* tp_richcompare */
2691 0, /* tp_weaklistoffset */
2692 PyObject_SelfIter, /* tp_iter */
2693 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002694 dictiter_methods, /* tp_methods */
2695 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002696};