blob: aa61e8c5eb9bfb3b93abd60badd93cfc424108ba [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
Tim Peters6d6c1a32001-08-02 04:15:00 +000012typedef PyDictEntry dictentry;
13typedef PyDictObject dictobject;
Guido van Rossum16e93a81997-01-28 00:00:11 +000014
Georg Brandlb9f4ad32006-10-29 18:31:42 +000015/* Set a key error with the specified argument, wrapping it in a
16 * tuple automatically so that tuple keys are not unpacked as the
17 * exception arguments. */
18static void
19set_key_error(PyObject *arg)
20{
21 PyObject *tup;
22 tup = PyTuple_Pack(1, arg);
23 if (!tup)
24 return; /* caller will expect error to be set anyway */
25 PyErr_SetObject(PyExc_KeyError, tup);
Neal Norwitz7b932da2006-10-29 23:39:03 +000026 Py_DECREF(tup);
Georg Brandlb9f4ad32006-10-29 18:31:42 +000027}
28
Tim Peterseb28ef22001-06-02 05:27:19 +000029/* Define this out if you don't want conversion statistics on exit. */
Fred Drake1bff34a2000-08-31 19:31:38 +000030#undef SHOW_CONVERSION_COUNTS
31
Tim Peterseb28ef22001-06-02 05:27:19 +000032/* See large comment block below. This must be >= 1. */
33#define PERTURB_SHIFT 5
34
Guido van Rossum16e93a81997-01-28 00:00:11 +000035/*
Tim Peterseb28ef22001-06-02 05:27:19 +000036Major subtleties ahead: Most hash schemes depend on having a "good" hash
37function, in the sense of simulating randomness. Python doesn't: its most
38important hash functions (for strings and ints) are very regular in common
39cases:
Tim Peters15d49292001-05-27 07:39:22 +000040
Tim Peterseb28ef22001-06-02 05:27:19 +000041>>> map(hash, (0, 1, 2, 3))
42[0, 1, 2, 3]
43>>> map(hash, ("namea", "nameb", "namec", "named"))
44[-1658398457, -1658398460, -1658398459, -1658398462]
45>>>
Tim Peters15d49292001-05-27 07:39:22 +000046
Tim Peterseb28ef22001-06-02 05:27:19 +000047This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
48the low-order i bits as the initial table index is extremely fast, and there
49are no collisions at all for dicts indexed by a contiguous range of ints.
50The same is approximately true when keys are "consecutive" strings. So this
51gives better-than-random behavior in common cases, and that's very desirable.
Tim Peters15d49292001-05-27 07:39:22 +000052
Tim Peterseb28ef22001-06-02 05:27:19 +000053OTOH, when collisions occur, the tendency to fill contiguous slices of the
54hash table makes a good collision resolution strategy crucial. Taking only
55the last i bits of the hash code is also vulnerable: for example, consider
56[i << 16 for i in range(20000)] as a set of keys. Since ints are their own
57hash codes, and this fits in a dict of size 2**15, the last 15 bits of every
58hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +000059
Tim Peterseb28ef22001-06-02 05:27:19 +000060But catering to unusual cases should not slow the usual ones, so we just take
61the last i bits anyway. It's up to collision resolution to do the rest. If
62we *usually* find the key we're looking for on the first try (and, it turns
63out, we usually do -- the table load factor is kept under 2/3, so the odds
64are solidly in our favor), then it makes best sense to keep the initial index
65computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +000066
Tim Peterseb28ef22001-06-02 05:27:19 +000067The first half of collision resolution is to visit table indices via this
68recurrence:
Tim Peters15d49292001-05-27 07:39:22 +000069
Tim Peterseb28ef22001-06-02 05:27:19 +000070 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +000071
Tim Peterseb28ef22001-06-02 05:27:19 +000072For any initial j in range(2**i), repeating that 2**i times generates each
73int in range(2**i) exactly once (see any text on random-number generation for
74proof). By itself, this doesn't help much: like linear probing (setting
75j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
76order. This would be bad, except that's not the only thing we do, and it's
77actually *good* in the common cases where hash keys are consecutive. In an
78example that's really too small to make this entirely clear, for a table of
79size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +000080
Tim Peterseb28ef22001-06-02 05:27:19 +000081 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
82
83If two things come in at index 5, the first place we look after is index 2,
84not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
85Linear probing is deadly in this case because there the fixed probe order
86is the *same* as the order consecutive keys are likely to arrive. But it's
87extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
88and certain that consecutive hash codes do not.
89
90The other half of the strategy is to get the other bits of the hash code
91into play. This is done by initializing a (unsigned) vrbl "perturb" to the
92full hash code, and changing the recurrence to:
93
94 j = (5*j) + 1 + perturb;
95 perturb >>= PERTURB_SHIFT;
96 use j % 2**i as the next table index;
97
98Now the probe sequence depends (eventually) on every bit in the hash code,
99and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
100because it quickly magnifies small differences in the bits that didn't affect
101the initial index. Note that because perturb is unsigned, if the recurrence
102is executed often enough perturb eventually becomes and remains 0. At that
103point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
104that's certain to find an empty slot eventually (since it generates every int
105in range(2**i), and we make sure there's always at least one empty slot).
106
107Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
108small so that the high bits of the hash code continue to affect the probe
109sequence across iterations; but you want it large so that in really bad cases
110the high-order hash bits have an effect on early iterations. 5 was "the
111best" in minimizing total collisions across experiments Tim Peters ran (on
112both normal and pathological cases), but 4 and 6 weren't significantly worse.
113
114Historical: Reimer Behrends contributed the idea of using a polynomial-based
115approach, using repeated multiplication by x in GF(2**n) where an irreducible
116polynomial for each table size was chosen such that x was a primitive root.
117Christian Tismer later extended that to use division by x instead, as an
118efficient way to get the high bits of the hash code into play. This scheme
119also gave excellent collision statistics, but was more expensive: two
120if-tests were required inside the loop; computing "the next" index took about
121the same number of operations but without as much potential parallelism
122(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
123above, and then shifting perturb can be done while the table index is being
124masked); and the dictobject struct required a member to hold the table's
125polynomial. In Tim's experiments the current scheme ran faster, produced
126equally good collision statistics, needed less code & used less memory.
Tim Peters9b10f7e2006-05-30 04:16:25 +0000127
128Theoretical Python 2.5 headache: hash codes are only C "long", but
129sizeof(Py_ssize_t) > sizeof(long) may be possible. In that case, and if a
130dict is genuinely huge, then only the slots directly reachable via indexing
131by a C long can be the first slot in a probe sequence. The probe sequence
132will still eventually reach every slot in the table, but the collision rate
133on initial probes may be much higher than this scheme was designed for.
134Getting a hash code as fat as Py_ssize_t is the only real cure. But in
135practice, this probably won't make a lick of difference for many years (at
136which point everyone will have terabytes of RAM on 64-bit boxes).
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000137*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000138
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000139/* Object used as dummy key to fill deleted entries */
Raymond Hettingerf81e4502005-08-17 02:19:36 +0000140static PyObject *dummy = NULL; /* Initialized by first call to newdictobject() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000141
Armin Rigoe1709372006-04-12 17:06:05 +0000142#ifdef Py_REF_DEBUG
143PyObject *
144_PyDict_Dummy(void)
145{
146 return dummy;
147}
148#endif
149
Fred Drake1bff34a2000-08-31 19:31:38 +0000150/* forward declarations */
151static dictentry *
152lookdict_string(dictobject *mp, PyObject *key, long hash);
153
154#ifdef SHOW_CONVERSION_COUNTS
155static long created = 0L;
156static long converted = 0L;
157
158static void
159show_counts(void)
160{
161 fprintf(stderr, "created %ld string dicts\n", created);
162 fprintf(stderr, "converted %ld to normal dicts\n", converted);
163 fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created);
164}
165#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000166
Tim Peters6d6c1a32001-08-02 04:15:00 +0000167/* Initialization macros.
168 There are two ways to create a dict: PyDict_New() is the main C API
169 function, and the tp_new slot maps to dict_new(). In the latter case we
170 can save a little time over what PyDict_New does because it's guaranteed
171 that the PyDictObject struct is already zeroed out.
172 Everyone except dict_new() should use EMPTY_TO_MINSIZE (unless they have
173 an excellent reason not to).
174*/
175
176#define INIT_NONZERO_DICT_SLOTS(mp) do { \
Tim Petersdea48ec2001-05-22 20:40:22 +0000177 (mp)->ma_table = (mp)->ma_smalltable; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000178 (mp)->ma_mask = PyDict_MINSIZE - 1; \
179 } while(0)
180
181#define EMPTY_TO_MINSIZE(mp) do { \
182 memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000183 (mp)->ma_used = (mp)->ma_fill = 0; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000184 INIT_NONZERO_DICT_SLOTS(mp); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000185 } while(0)
186
Raymond Hettinger43442782004-03-17 21:55:03 +0000187/* Dictionary reuse scheme to save calls to malloc, free, and memset */
188#define MAXFREEDICTS 80
189static PyDictObject *free_dicts[MAXFREEDICTS];
190static int num_free_dicts = 0;
191
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000192PyObject *
Thomas Wouters78890102000-07-22 19:25:51 +0000193PyDict_New(void)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000194{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000195 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000196 if (dummy == NULL) { /* Auto-initialize dummy */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000197 dummy = PyString_FromString("<dummy key>");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000198 if (dummy == NULL)
199 return NULL;
Fred Drake1bff34a2000-08-31 19:31:38 +0000200#ifdef SHOW_CONVERSION_COUNTS
201 Py_AtExit(show_counts);
202#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000203 }
Raymond Hettinger43442782004-03-17 21:55:03 +0000204 if (num_free_dicts) {
205 mp = free_dicts[--num_free_dicts];
206 assert (mp != NULL);
Martin v. Löwis68192102007-07-21 06:55:02 +0000207 assert (Py_Type(mp) == &PyDict_Type);
Raymond Hettinger43442782004-03-17 21:55:03 +0000208 _Py_NewReference((PyObject *)mp);
209 if (mp->ma_fill) {
210 EMPTY_TO_MINSIZE(mp);
211 }
212 assert (mp->ma_used == 0);
213 assert (mp->ma_table == mp->ma_smalltable);
214 assert (mp->ma_mask == PyDict_MINSIZE - 1);
215 } else {
216 mp = PyObject_GC_New(dictobject, &PyDict_Type);
217 if (mp == NULL)
218 return NULL;
219 EMPTY_TO_MINSIZE(mp);
220 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000221 mp->ma_lookup = lookdict_string;
222#ifdef SHOW_CONVERSION_COUNTS
223 ++created;
224#endif
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000225 _PyObject_GC_TRACK(mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000226 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000227}
228
229/*
230The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000231This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000232Open addressing is preferred over chaining since the link overhead for
233chaining would be substantial (100% with typical malloc overhead).
234
Tim Peterseb28ef22001-06-02 05:27:19 +0000235The initial probe index is computed as hash mod the table size. Subsequent
236probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000237
238All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000239
Tim Peterseb28ef22001-06-02 05:27:19 +0000240(The details in this version are due to Tim Peters, building on many past
241contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
242Christian Tismer).
Fred Drake1bff34a2000-08-31 19:31:38 +0000243
Tim Petersd770ebd2006-06-01 15:50:44 +0000244lookdict() is general-purpose, and may return NULL if (and only if) a
245comparison raises an exception (this was new in Python 2.5).
246lookdict_string() below is specialized to string keys, comparison of which can
247never raise an exception; that function can never return NULL. For both, when
248the key isn't found a dictentry* is returned for which the me_value field is
249NULL; this is the slot in the dict at which the key would have been found, and
250the caller can (if it wishes) add the <key, value> pair to the returned
251dictentry*.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000252*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000253static dictentry *
Tim Peters1f5871e2000-07-04 17:44:48 +0000254lookdict(dictobject *mp, PyObject *key, register long hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000255{
Tim Petersd770ebd2006-06-01 15:50:44 +0000256 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000257 register size_t perturb;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000258 register dictentry *freeslot;
Tim Petersd770ebd2006-06-01 15:50:44 +0000259 register size_t mask = (size_t)mp->ma_mask;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000260 dictentry *ep0 = mp->ma_table;
261 register dictentry *ep;
Fred Drakec88b99c2000-08-31 19:04:07 +0000262 register int cmp;
Tim Peters453163d2001-06-03 04:54:32 +0000263 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000264
Tim Petersd770ebd2006-06-01 15:50:44 +0000265 i = (size_t)hash & mask;
Guido van Rossumefb46091997-01-29 15:53:56 +0000266 ep = &ep0[i];
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000267 if (ep->me_key == NULL || ep->me_key == key)
Guido van Rossum7d181591997-01-16 21:06:45 +0000268 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000269
Guido van Rossum16e93a81997-01-28 00:00:11 +0000270 if (ep->me_key == dummy)
Guido van Rossum7d181591997-01-16 21:06:45 +0000271 freeslot = ep;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000272 else {
Fred Drakec88b99c2000-08-31 19:04:07 +0000273 if (ep->me_hash == hash) {
Tim Peters453163d2001-06-03 04:54:32 +0000274 startkey = ep->me_key;
275 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000276 if (cmp < 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000277 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000278 if (ep0 == mp->ma_table && ep->me_key == startkey) {
279 if (cmp > 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000280 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000281 }
282 else {
283 /* The compare did major nasty stuff to the
284 * dict: start over.
285 * XXX A clever adversary could prevent this
286 * XXX from terminating.
287 */
Armin Rigo35f6d362006-06-01 13:19:12 +0000288 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000289 }
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000290 }
291 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000292 }
Tim Peters15d49292001-05-27 07:39:22 +0000293
Tim Peters342c65e2001-05-13 06:43:53 +0000294 /* In the loop, me_key == dummy is by far (factor of 100s) the
295 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000296 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
297 i = (i << 2) + i + perturb + 1;
298 ep = &ep0[i & mask];
Armin Rigo35f6d362006-06-01 13:19:12 +0000299 if (ep->me_key == NULL)
300 return freeslot == NULL ? ep : freeslot;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000301 if (ep->me_key == key)
Armin Rigo35f6d362006-06-01 13:19:12 +0000302 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000303 if (ep->me_hash == hash && ep->me_key != dummy) {
Tim Peters453163d2001-06-03 04:54:32 +0000304 startkey = ep->me_key;
305 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000306 if (cmp < 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000307 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000308 if (ep0 == mp->ma_table && ep->me_key == startkey) {
309 if (cmp > 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000310 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000311 }
312 else {
313 /* The compare did major nasty stuff to the
314 * dict: start over.
315 * XXX A clever adversary could prevent this
316 * XXX from terminating.
317 */
Armin Rigo35f6d362006-06-01 13:19:12 +0000318 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000319 }
Guido van Rossum16e93a81997-01-28 00:00:11 +0000320 }
Tim Peters342c65e2001-05-13 06:43:53 +0000321 else if (ep->me_key == dummy && freeslot == NULL)
322 freeslot = ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000323 }
Neal Norwitza5ccda92006-10-28 21:16:54 +0000324 assert(0); /* NOT REACHED */
325 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000326}
327
328/*
Fred Drake1bff34a2000-08-31 19:31:38 +0000329 * Hacked up version of lookdict which can assume keys are always strings;
Tim Petersd770ebd2006-06-01 15:50:44 +0000330 * this assumption allows testing for errors during PyObject_RichCompareBool()
331 * to be dropped; string-string comparisons never raise exceptions. This also
332 * means we don't need to go through PyObject_RichCompareBool(); we can always
333 * use _PyString_Eq() directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000334 *
Tim Petersd770ebd2006-06-01 15:50:44 +0000335 * This is valuable because dicts with only string keys are very common.
Fred Drake1bff34a2000-08-31 19:31:38 +0000336 */
337static dictentry *
338lookdict_string(dictobject *mp, PyObject *key, register long hash)
339{
Tim Petersd770ebd2006-06-01 15:50:44 +0000340 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000341 register size_t perturb;
Fred Drake1bff34a2000-08-31 19:31:38 +0000342 register dictentry *freeslot;
Tim Petersd770ebd2006-06-01 15:50:44 +0000343 register size_t mask = (size_t)mp->ma_mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000344 dictentry *ep0 = mp->ma_table;
345 register dictentry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000346
Tim Peters0ab085c2001-09-14 00:25:33 +0000347 /* Make sure this function doesn't have to handle non-string keys,
348 including subclasses of str; e.g., one reason to subclass
349 strings is to override __eq__, and for speed we don't cater to
350 that here. */
351 if (!PyString_CheckExact(key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000352#ifdef SHOW_CONVERSION_COUNTS
353 ++converted;
354#endif
355 mp->ma_lookup = lookdict;
356 return lookdict(mp, key, hash);
357 }
Tim Peters2f228e72001-05-13 00:19:31 +0000358 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000359 ep = &ep0[i];
360 if (ep->me_key == NULL || ep->me_key == key)
361 return ep;
362 if (ep->me_key == dummy)
363 freeslot = ep;
364 else {
Tim Petersd770ebd2006-06-01 15:50:44 +0000365 if (ep->me_hash == hash && _PyString_Eq(ep->me_key, key))
Fred Drake1bff34a2000-08-31 19:31:38 +0000366 return ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000367 freeslot = NULL;
368 }
Tim Peters15d49292001-05-27 07:39:22 +0000369
Tim Peters342c65e2001-05-13 06:43:53 +0000370 /* In the loop, me_key == dummy is by far (factor of 100s) the
371 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000372 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
373 i = (i << 2) + i + perturb + 1;
374 ep = &ep0[i & mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000375 if (ep->me_key == NULL)
376 return freeslot == NULL ? ep : freeslot;
377 if (ep->me_key == key
378 || (ep->me_hash == hash
379 && ep->me_key != dummy
Martin v. Löwiscd353062001-05-24 16:56:35 +0000380 && _PyString_Eq(ep->me_key, key)))
Fred Drake1bff34a2000-08-31 19:31:38 +0000381 return ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000382 if (ep->me_key == dummy && freeslot == NULL)
Tim Peters342c65e2001-05-13 06:43:53 +0000383 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000384 }
Neal Norwitza5ccda92006-10-28 21:16:54 +0000385 assert(0); /* NOT REACHED */
386 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000387}
388
389/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000390Internal routine to insert a new item into the table.
391Used both by the internal resize routine and by the public insert routine.
392Eats a reference to key and one to value.
Tim Petersd770ebd2006-06-01 15:50:44 +0000393Returns -1 if an error occurred, or 0 on success.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000394*/
Armin Rigo35f6d362006-06-01 13:19:12 +0000395static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000396insertdict(register dictobject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000397{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000398 PyObject *old_value;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000399 register dictentry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000400 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
401
402 assert(mp->ma_lookup != NULL);
403 ep = mp->ma_lookup(mp, key, hash);
Armin Rigo35f6d362006-06-01 13:19:12 +0000404 if (ep == NULL) {
405 Py_DECREF(key);
406 Py_DECREF(value);
407 return -1;
408 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000409 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000410 old_value = ep->me_value;
411 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000412 Py_DECREF(old_value); /* which **CAN** re-enter */
413 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000414 }
415 else {
416 if (ep->me_key == NULL)
417 mp->ma_fill++;
Raymond Hettinger07ead172005-02-05 23:42:57 +0000418 else {
419 assert(ep->me_key == dummy);
420 Py_DECREF(dummy);
421 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000422 ep->me_key = key;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000423 ep->me_hash = (Py_ssize_t)hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000424 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000425 mp->ma_used++;
426 }
Armin Rigo35f6d362006-06-01 13:19:12 +0000427 return 0;
428}
429
430/*
431Internal routine used by dictresize() to insert an item which is
432known to be absent from the dict. This routine also assumes that
433the dict contains no deleted entries. Besides the performance benefit,
434using insertdict() in dictresize() is dangerous (SF bug #1456209).
Tim Petersd770ebd2006-06-01 15:50:44 +0000435Note that no refcounts are changed by this routine; if needed, the caller
436is responsible for incref'ing `key` and `value`.
Armin Rigo35f6d362006-06-01 13:19:12 +0000437*/
438static void
439insertdict_clean(register dictobject *mp, PyObject *key, long hash,
440 PyObject *value)
441{
Tim Petersd770ebd2006-06-01 15:50:44 +0000442 register size_t i;
Armin Rigo35f6d362006-06-01 13:19:12 +0000443 register size_t perturb;
Tim Petersd770ebd2006-06-01 15:50:44 +0000444 register size_t mask = (size_t)mp->ma_mask;
Armin Rigo35f6d362006-06-01 13:19:12 +0000445 dictentry *ep0 = mp->ma_table;
446 register dictentry *ep;
447
448 i = hash & mask;
449 ep = &ep0[i];
450 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
451 i = (i << 2) + i + perturb + 1;
452 ep = &ep0[i & mask];
453 }
Tim Petersd770ebd2006-06-01 15:50:44 +0000454 assert(ep->me_value == NULL);
Armin Rigo35f6d362006-06-01 13:19:12 +0000455 mp->ma_fill++;
456 ep->me_key = key;
457 ep->me_hash = (Py_ssize_t)hash;
458 ep->me_value = value;
459 mp->ma_used++;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000460}
461
462/*
463Restructure the table by allocating a new table and reinserting all
464items again. When entries have been deleted, the new table may
465actually be smaller than the old one.
466*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000467static int
Tim Peters9b10f7e2006-05-30 04:16:25 +0000468dictresize(dictobject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000469{
Tim Peters9b10f7e2006-05-30 04:16:25 +0000470 Py_ssize_t newsize;
Tim Peters0c6010b2001-05-23 23:33:57 +0000471 dictentry *oldtable, *newtable, *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000472 Py_ssize_t i;
Tim Peters0c6010b2001-05-23 23:33:57 +0000473 int is_oldtable_malloced;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000474 dictentry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000475
476 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000477
Tim Peterseb28ef22001-06-02 05:27:19 +0000478 /* Find the smallest table size > minused. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000479 for (newsize = PyDict_MINSIZE;
Tim Peterseb28ef22001-06-02 05:27:19 +0000480 newsize <= minused && newsize > 0;
481 newsize <<= 1)
482 ;
483 if (newsize <= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000484 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000485 return -1;
486 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000487
488 /* Get space for a new table. */
489 oldtable = mp->ma_table;
490 assert(oldtable != NULL);
491 is_oldtable_malloced = oldtable != mp->ma_smalltable;
492
Tim Peters6d6c1a32001-08-02 04:15:00 +0000493 if (newsize == PyDict_MINSIZE) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000494 /* A large table is shrinking, or we can't get any smaller. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000495 newtable = mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000496 if (newtable == oldtable) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000497 if (mp->ma_fill == mp->ma_used) {
498 /* No dummies, so no point doing anything. */
Tim Peters0c6010b2001-05-23 23:33:57 +0000499 return 0;
Tim Petersf8a548c2001-05-24 16:26:40 +0000500 }
501 /* We're not going to resize it, but rebuild the
502 table anyway to purge old dummy entries.
503 Subtle: This is *necessary* if fill==size,
504 as lookdict needs at least one virgin slot to
505 terminate failing searches. If fill < size, it's
506 merely desirable, as dummies slow searches. */
507 assert(mp->ma_fill > mp->ma_used);
Tim Peters0c6010b2001-05-23 23:33:57 +0000508 memcpy(small_copy, oldtable, sizeof(small_copy));
509 oldtable = small_copy;
510 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000511 }
512 else {
513 newtable = PyMem_NEW(dictentry, newsize);
514 if (newtable == NULL) {
515 PyErr_NoMemory();
516 return -1;
517 }
518 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000519
520 /* Make the dict empty, using the new table. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000521 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000522 mp->ma_table = newtable;
Tim Petersafb6ae82001-06-04 21:00:21 +0000523 mp->ma_mask = newsize - 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000524 memset(newtable, 0, sizeof(dictentry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000525 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000526 i = mp->ma_fill;
527 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000528
Tim Peters19283142001-05-17 22:25:34 +0000529 /* Copy the data over; this is refcount-neutral for active entries;
530 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000531 for (ep = oldtable; i > 0; ep++) {
532 if (ep->me_value != NULL) { /* active entry */
533 --i;
Armin Rigo35f6d362006-06-01 13:19:12 +0000534 insertdict_clean(mp, ep->me_key, (long)ep->me_hash,
535 ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000536 }
Tim Peters19283142001-05-17 22:25:34 +0000537 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000538 --i;
Tim Peters19283142001-05-17 22:25:34 +0000539 assert(ep->me_key == dummy);
540 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000541 }
Tim Peters19283142001-05-17 22:25:34 +0000542 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000543 }
544
Tim Peters0c6010b2001-05-23 23:33:57 +0000545 if (is_oldtable_malloced)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000546 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000547 return 0;
548}
549
Tim Petersd770ebd2006-06-01 15:50:44 +0000550/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
551 * that may occur (originally dicts supported only string keys, and exceptions
552 * weren't possible). So, while the original intent was that a NULL return
Andrew M. Kuchling0067b5f2006-08-04 20:37:43 +0000553 * meant the key wasn't present, in reality it can mean that, or that an error
Tim Petersd770ebd2006-06-01 15:50:44 +0000554 * (suppressed) occurred while computing the key's hash, or that some error
555 * (suppressed) occurred when comparing keys in the dict's internal probe
556 * sequence. A nasty example of the latter is when a Python-coded comparison
557 * function hits a stack-depth error, which can cause this to return NULL
558 * even if the key is present.
559 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000560PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000561PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000562{
563 long hash;
Fred Drake1bff34a2000-08-31 19:31:38 +0000564 dictobject *mp = (dictobject *)op;
Armin Rigo35f6d362006-06-01 13:19:12 +0000565 dictentry *ep;
566 PyThreadState *tstate;
Tim Petersd770ebd2006-06-01 15:50:44 +0000567 if (!PyDict_Check(op))
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000568 return NULL;
Tim Peters0ab085c2001-09-14 00:25:33 +0000569 if (!PyString_CheckExact(key) ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000570 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Guido van Rossumca756f21997-01-23 19:39:29 +0000571 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000572 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000573 if (hash == -1) {
574 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000575 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000576 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000577 }
Armin Rigo35f6d362006-06-01 13:19:12 +0000578
579 /* We can arrive here with a NULL tstate during initialization:
580 try running "python -Wi" for an example related to string
581 interning. Let's just hope that no exception occurs then... */
Armin Rigoacd0d6d2006-06-10 10:57:40 +0000582 tstate = _PyThreadState_Current;
Armin Rigo35f6d362006-06-01 13:19:12 +0000583 if (tstate != NULL && tstate->curexc_type != NULL) {
584 /* preserve the existing exception */
585 PyObject *err_type, *err_value, *err_tb;
586 PyErr_Fetch(&err_type, &err_value, &err_tb);
587 ep = (mp->ma_lookup)(mp, key, hash);
588 /* ignore errors */
589 PyErr_Restore(err_type, err_value, err_tb);
590 if (ep == NULL)
591 return NULL;
592 }
593 else {
594 ep = (mp->ma_lookup)(mp, key, hash);
595 if (ep == NULL) {
596 PyErr_Clear();
597 return NULL;
598 }
599 }
600 return ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000601}
602
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000603/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
Tim Peters60b29962006-01-01 01:19:23 +0000604 * dictionary if it's merely replacing the value for an existing key.
605 * This means that it's safe to loop over a dictionary with PyDict_Next()
606 * and occasionally replace a value -- but you can't insert new keys or
607 * remove them.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000608 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000609int
Tim Peters1f5871e2000-07-04 17:44:48 +0000610PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000611{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000612 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000613 register long hash;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000614 register Py_ssize_t n_used;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000615
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000616 if (!PyDict_Check(op)) {
617 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000618 return -1;
619 }
Neal Norwitz48808a12006-07-21 05:29:58 +0000620 assert(key);
621 assert(value);
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000622 mp = (dictobject *)op;
Tim Peters0ab085c2001-09-14 00:25:33 +0000623 if (PyString_CheckExact(key)) {
Guido van Rossum45ec02a2002-08-19 21:43:18 +0000624 hash = ((PyStringObject *)key)->ob_shash;
625 if (hash == -1)
626 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000627 }
Tim Peters1f7df352002-03-29 03:29:08 +0000628 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000629 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000630 if (hash == -1)
631 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000632 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000633 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000634 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000635 Py_INCREF(value);
636 Py_INCREF(key);
Armin Rigo35f6d362006-06-01 13:19:12 +0000637 if (insertdict(mp, key, hash, value) != 0)
638 return -1;
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000639 /* If we added a key, we can safely resize. Otherwise just return!
640 * If fill >= 2/3 size, adjust size. Normally, this doubles or
641 * quaduples the size, but it's also possible for the dict to shrink
Tim Peters60b29962006-01-01 01:19:23 +0000642 * (if ma_fill is much larger than ma_used, meaning a lot of dict
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000643 * keys have been * deleted).
Tim Peters60b29962006-01-01 01:19:23 +0000644 *
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000645 * Quadrupling the size improves average dictionary sparseness
646 * (reducing collisions) at the cost of some memory and iteration
647 * speed (which loops over every possible entry). It also halves
Neal Norwitz80af59c2006-05-30 04:19:21 +0000648 * the number of expensive resize operations in a growing dictionary.
Tim Peters60b29962006-01-01 01:19:23 +0000649 *
650 * Very large dictionaries (over 50K items) use doubling instead.
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000651 * This may help applications with severe memory constraints.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000652 */
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000653 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
654 return 0;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000655 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000656}
657
658int
Tim Peters1f5871e2000-07-04 17:44:48 +0000659PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000660{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000661 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000662 register long hash;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000663 register dictentry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000664 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000665
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000666 if (!PyDict_Check(op)) {
667 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000668 return -1;
669 }
Neal Norwitz48808a12006-07-21 05:29:58 +0000670 assert(key);
Tim Peters0ab085c2001-09-14 00:25:33 +0000671 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +0000672 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000673 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000674 if (hash == -1)
675 return -1;
676 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000677 mp = (dictobject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000678 ep = (mp->ma_lookup)(mp, key, hash);
Armin Rigo35f6d362006-06-01 13:19:12 +0000679 if (ep == NULL)
680 return -1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000681 if (ep->me_value == NULL) {
Georg Brandlb9f4ad32006-10-29 18:31:42 +0000682 set_key_error(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000683 return -1;
684 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000685 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000686 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000687 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000688 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000689 ep->me_value = NULL;
690 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000691 Py_DECREF(old_value);
692 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000693 return 0;
694}
695
Guido van Rossum25831651993-05-19 14:50:45 +0000696void
Tim Peters1f5871e2000-07-04 17:44:48 +0000697PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000698{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000699 dictobject *mp;
Tim Petersdea48ec2001-05-22 20:40:22 +0000700 dictentry *ep, *table;
701 int table_is_malloced;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000702 Py_ssize_t fill;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000703 dictentry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000704#ifdef Py_DEBUG
Tim Peters9b10f7e2006-05-30 04:16:25 +0000705 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +0000706#endif
707
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000708 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000709 return;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000710 mp = (dictobject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000711#ifdef Py_DEBUG
Tim Petersafb6ae82001-06-04 21:00:21 +0000712 n = mp->ma_mask + 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000713 i = 0;
714#endif
715
716 table = mp->ma_table;
717 assert(table != NULL);
718 table_is_malloced = table != mp->ma_smalltable;
719
720 /* This is delicate. During the process of clearing the dict,
721 * decrefs can cause the dict to mutate. To avoid fatal confusion
722 * (voice of experience), we have to make the dict empty before
723 * clearing the slots, and never refer to anything via mp->xxx while
724 * clearing.
725 */
726 fill = mp->ma_fill;
727 if (table_is_malloced)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000728 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000729
730 else if (fill > 0) {
731 /* It's a small table with something that needs to be cleared.
732 * Afraid the only safe way is to copy the dict entries into
733 * another small table first.
734 */
735 memcpy(small_copy, table, sizeof(small_copy));
736 table = small_copy;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000737 EMPTY_TO_MINSIZE(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000738 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000739 /* else it's a small table that's already empty */
740
741 /* Now we can finally clear things. If C had refcounts, we could
742 * assert that the refcount on table is 1 now, i.e. that this function
743 * has unique access to it, so decref side-effects can't alter it.
744 */
745 for (ep = table; fill > 0; ++ep) {
746#ifdef Py_DEBUG
747 assert(i < n);
748 ++i;
749#endif
750 if (ep->me_key) {
751 --fill;
752 Py_DECREF(ep->me_key);
753 Py_XDECREF(ep->me_value);
754 }
755#ifdef Py_DEBUG
756 else
757 assert(ep->me_value == NULL);
758#endif
759 }
760
761 if (table_is_malloced)
762 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000763}
764
Tim Peters080c88b2003-02-15 03:01:11 +0000765/*
766 * Iterate over a dict. Use like so:
767 *
Tim Peters9b10f7e2006-05-30 04:16:25 +0000768 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +0000769 * PyObject *key, *value;
770 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +0000771 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +0000772 * Refer to borrowed references in key and value.
773 * }
774 *
775 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +0000776 * mutates the dict. One exception: it is safe if the loop merely changes
777 * the values associated with the keys (but doesn't insert new keys or
778 * delete keys), via PyDict_SetItem().
779 */
Guido van Rossum25831651993-05-19 14:50:45 +0000780int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000781PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000782{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000783 register Py_ssize_t i;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000784 register Py_ssize_t mask;
Raymond Hettinger43442782004-03-17 21:55:03 +0000785 register dictentry *ep;
786
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000787 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000788 return 0;
Guido van Rossum25831651993-05-19 14:50:45 +0000789 i = *ppos;
790 if (i < 0)
791 return 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000792 ep = ((dictobject *)op)->ma_table;
793 mask = ((dictobject *)op)->ma_mask;
794 while (i <= mask && ep[i].me_value == NULL)
Guido van Rossum25831651993-05-19 14:50:45 +0000795 i++;
796 *ppos = i+1;
Raymond Hettinger43442782004-03-17 21:55:03 +0000797 if (i > mask)
Guido van Rossum25831651993-05-19 14:50:45 +0000798 return 0;
799 if (pkey)
Raymond Hettinger43442782004-03-17 21:55:03 +0000800 *pkey = ep[i].me_key;
Guido van Rossum25831651993-05-19 14:50:45 +0000801 if (pvalue)
Raymond Hettinger43442782004-03-17 21:55:03 +0000802 *pvalue = ep[i].me_value;
Guido van Rossum25831651993-05-19 14:50:45 +0000803 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000804}
805
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +0000806/* Internal version of PyDict_Next that returns a hash value in addition to the key and value.*/
807int
808_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, long *phash)
809{
810 register Py_ssize_t i;
811 register Py_ssize_t mask;
812 register dictentry *ep;
813
814 if (!PyDict_Check(op))
815 return 0;
816 i = *ppos;
817 if (i < 0)
818 return 0;
819 ep = ((dictobject *)op)->ma_table;
820 mask = ((dictobject *)op)->ma_mask;
821 while (i <= mask && ep[i].me_value == NULL)
822 i++;
823 *ppos = i+1;
824 if (i > mask)
825 return 0;
826 *phash = (long)(ep[i].me_hash);
827 if (pkey)
828 *pkey = ep[i].me_key;
829 if (pvalue)
830 *pvalue = ep[i].me_value;
831 return 1;
832}
833
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000834/* Methods */
835
836static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000837dict_dealloc(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000838{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000839 register dictentry *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000840 Py_ssize_t fill = mp->ma_fill;
Guido van Rossumff413af2002-03-28 20:34:59 +0000841 PyObject_GC_UnTrack(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000842 Py_TRASHCAN_SAFE_BEGIN(mp)
Tim Petersdea48ec2001-05-22 20:40:22 +0000843 for (ep = mp->ma_table; fill > 0; ep++) {
844 if (ep->me_key) {
845 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000846 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000847 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000848 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000849 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000850 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000851 PyMem_DEL(mp->ma_table);
Martin v. Löwis68192102007-07-21 06:55:02 +0000852 if (num_free_dicts < MAXFREEDICTS && Py_Type(mp) == &PyDict_Type)
Raymond Hettinger43442782004-03-17 21:55:03 +0000853 free_dicts[num_free_dicts++] = mp;
Tim Peters60b29962006-01-01 01:19:23 +0000854 else
Martin v. Löwis68192102007-07-21 06:55:02 +0000855 Py_Type(mp)->tp_free((PyObject *)mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000856 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000857}
858
859static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000860dict_print(register dictobject *mp, register FILE *fp, register int flags)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000861{
Tim Peters9b10f7e2006-05-30 04:16:25 +0000862 register Py_ssize_t i;
863 register Py_ssize_t any;
Tim Peters33f4a6a2006-05-30 05:23:59 +0000864 int status;
Guido van Rossum255443b1998-04-10 22:47:14 +0000865
Tim Peters33f4a6a2006-05-30 05:23:59 +0000866 status = Py_ReprEnter((PyObject*)mp);
867 if (status != 0) {
868 if (status < 0)
869 return status;
Brett Cannon01531592007-09-17 03:28:34 +0000870 Py_BEGIN_ALLOW_THREADS
Guido van Rossum255443b1998-04-10 22:47:14 +0000871 fprintf(fp, "{...}");
Brett Cannon01531592007-09-17 03:28:34 +0000872 Py_END_ALLOW_THREADS
Guido van Rossum255443b1998-04-10 22:47:14 +0000873 return 0;
874 }
875
Brett Cannon01531592007-09-17 03:28:34 +0000876 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000877 fprintf(fp, "{");
Brett Cannon01531592007-09-17 03:28:34 +0000878 Py_END_ALLOW_THREADS
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000879 any = 0;
Tim Petersafb6ae82001-06-04 21:00:21 +0000880 for (i = 0; i <= mp->ma_mask; i++) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000881 dictentry *ep = mp->ma_table + i;
882 PyObject *pvalue = ep->me_value;
883 if (pvalue != NULL) {
884 /* Prevent PyObject_Repr from deleting value during
885 key format */
886 Py_INCREF(pvalue);
Brett Cannon01531592007-09-17 03:28:34 +0000887 if (any++ > 0) {
888 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000889 fprintf(fp, ", ");
Brett Cannon01531592007-09-17 03:28:34 +0000890 Py_END_ALLOW_THREADS
891 }
Guido van Rossum255443b1998-04-10 22:47:14 +0000892 if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000893 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000894 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000895 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000896 }
Brett Cannon01531592007-09-17 03:28:34 +0000897 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000898 fprintf(fp, ": ");
Brett Cannon01531592007-09-17 03:28:34 +0000899 Py_END_ALLOW_THREADS
Tim Peters19b77cf2001-06-02 08:27:39 +0000900 if (PyObject_Print(pvalue, fp, 0) != 0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000901 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000902 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000903 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000904 }
Tim Peters23cf6be2001-06-02 08:02:56 +0000905 Py_DECREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000906 }
907 }
Brett Cannon01531592007-09-17 03:28:34 +0000908 Py_BEGIN_ALLOW_THREADS
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000909 fprintf(fp, "}");
Brett Cannon01531592007-09-17 03:28:34 +0000910 Py_END_ALLOW_THREADS
Guido van Rossum255443b1998-04-10 22:47:14 +0000911 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000912 return 0;
913}
914
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000915static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000916dict_repr(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000917{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000918 Py_ssize_t i;
Tim Petersa7259592001-06-16 05:11:17 +0000919 PyObject *s, *temp, *colon = NULL;
920 PyObject *pieces = NULL, *result = NULL;
921 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +0000922
Tim Petersa7259592001-06-16 05:11:17 +0000923 i = Py_ReprEnter((PyObject *)mp);
Guido van Rossum255443b1998-04-10 22:47:14 +0000924 if (i != 0) {
Tim Petersa7259592001-06-16 05:11:17 +0000925 return i > 0 ? PyString_FromString("{...}") : NULL;
Guido van Rossum255443b1998-04-10 22:47:14 +0000926 }
927
Tim Petersa7259592001-06-16 05:11:17 +0000928 if (mp->ma_used == 0) {
929 result = PyString_FromString("{}");
930 goto Done;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000931 }
Tim Petersa7259592001-06-16 05:11:17 +0000932
933 pieces = PyList_New(0);
934 if (pieces == NULL)
935 goto Done;
936
937 colon = PyString_FromString(": ");
938 if (colon == NULL)
939 goto Done;
940
941 /* Do repr() on each key+value pair, and insert ": " between them.
942 Note that repr may mutate the dict. */
Tim Petersc6057842001-06-16 07:52:53 +0000943 i = 0;
944 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Tim Petersa7259592001-06-16 05:11:17 +0000945 int status;
946 /* Prevent repr from deleting value during key format. */
947 Py_INCREF(value);
948 s = PyObject_Repr(key);
949 PyString_Concat(&s, colon);
950 PyString_ConcatAndDel(&s, PyObject_Repr(value));
951 Py_DECREF(value);
952 if (s == NULL)
953 goto Done;
954 status = PyList_Append(pieces, s);
955 Py_DECREF(s); /* append created a new ref */
956 if (status < 0)
957 goto Done;
958 }
959
960 /* Add "{}" decorations to the first and last items. */
961 assert(PyList_GET_SIZE(pieces) > 0);
962 s = PyString_FromString("{");
963 if (s == NULL)
964 goto Done;
965 temp = PyList_GET_ITEM(pieces, 0);
966 PyString_ConcatAndDel(&s, temp);
967 PyList_SET_ITEM(pieces, 0, s);
968 if (s == NULL)
969 goto Done;
970
971 s = PyString_FromString("}");
972 if (s == NULL)
973 goto Done;
974 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
975 PyString_ConcatAndDel(&temp, s);
976 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
977 if (temp == NULL)
978 goto Done;
979
980 /* Paste them all together with ", " between. */
981 s = PyString_FromString(", ");
982 if (s == NULL)
983 goto Done;
984 result = _PyString_Join(s, pieces);
Tim Peters0ab085c2001-09-14 00:25:33 +0000985 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +0000986
987Done:
988 Py_XDECREF(pieces);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000989 Py_XDECREF(colon);
Tim Petersa7259592001-06-16 05:11:17 +0000990 Py_ReprLeave((PyObject *)mp);
991 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000992}
993
Martin v. Löwis18e16552006-02-15 17:27:45 +0000994static Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +0000995dict_length(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000996{
997 return mp->ma_used;
998}
999
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001000static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001001dict_subscript(dictobject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001002{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001003 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001004 long hash;
Armin Rigo35f6d362006-06-01 13:19:12 +00001005 dictentry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +00001006 assert(mp->ma_table != NULL);
Tim Peters0ab085c2001-09-14 00:25:33 +00001007 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001008 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001009 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001010 if (hash == -1)
1011 return NULL;
1012 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001013 ep = (mp->ma_lookup)(mp, key, hash);
1014 if (ep == NULL)
1015 return NULL;
1016 v = ep->me_value;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001017 if (v == NULL) {
1018 if (!PyDict_CheckExact(mp)) {
1019 /* Look up __missing__ method if we're a subclass. */
Guido van Rossum4b92a822006-02-25 23:32:30 +00001020 PyObject *missing;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001021 static PyObject *missing_str = NULL;
1022 if (missing_str == NULL)
Tim Peters9b10f7e2006-05-30 04:16:25 +00001023 missing_str =
Guido van Rossum1968ad32006-02-25 22:38:04 +00001024 PyString_InternFromString("__missing__");
Martin v. Löwis68192102007-07-21 06:55:02 +00001025 missing = _PyType_Lookup(Py_Type(mp), missing_str);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001026 if (missing != NULL)
1027 return PyObject_CallFunctionObjArgs(missing,
1028 (PyObject *)mp, key, NULL);
1029 }
Georg Brandlb9f4ad32006-10-29 18:31:42 +00001030 set_key_error(key);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001031 return NULL;
1032 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001033 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001034 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001035 return v;
1036}
1037
1038static int
Tim Peters1f5871e2000-07-04 17:44:48 +00001039dict_ass_sub(dictobject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001040{
1041 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001042 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001043 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001044 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001045}
1046
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001047static PyMappingMethods dict_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001048 (lenfunc)dict_length, /*mp_length*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001049 (binaryfunc)dict_subscript, /*mp_subscript*/
1050 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001051};
1052
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001053static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001054dict_keys(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001055{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001056 register PyObject *v;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001057 register Py_ssize_t i, j;
Raymond Hettinger43442782004-03-17 21:55:03 +00001058 dictentry *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001059 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001060
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001061 again:
1062 n = mp->ma_used;
1063 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001064 if (v == NULL)
1065 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001066 if (n != mp->ma_used) {
1067 /* Durnit. The allocations caused the dict to resize.
1068 * Just start over, this shouldn't normally happen.
1069 */
1070 Py_DECREF(v);
1071 goto again;
1072 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001073 ep = mp->ma_table;
1074 mask = mp->ma_mask;
1075 for (i = 0, j = 0; i <= mask; i++) {
1076 if (ep[i].me_value != NULL) {
1077 PyObject *key = ep[i].me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001078 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001079 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001080 j++;
1081 }
1082 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001083 assert(j == n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001084 return v;
1085}
1086
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001087static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001088dict_values(register dictobject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001089{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001090 register PyObject *v;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001091 register Py_ssize_t i, j;
Raymond Hettinger43442782004-03-17 21:55:03 +00001092 dictentry *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001093 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001094
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001095 again:
1096 n = mp->ma_used;
1097 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001098 if (v == NULL)
1099 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001100 if (n != mp->ma_used) {
1101 /* Durnit. The allocations caused the dict to resize.
1102 * Just start over, this shouldn't normally happen.
1103 */
1104 Py_DECREF(v);
1105 goto again;
1106 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001107 ep = mp->ma_table;
1108 mask = mp->ma_mask;
1109 for (i = 0, j = 0; i <= mask; i++) {
1110 if (ep[i].me_value != NULL) {
1111 PyObject *value = ep[i].me_value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001112 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001113 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001114 j++;
1115 }
1116 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001117 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001118 return v;
1119}
1120
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001121static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001122dict_items(register dictobject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001123{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001124 register PyObject *v;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001125 register Py_ssize_t i, j, n;
1126 Py_ssize_t mask;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001127 PyObject *item, *key, *value;
Raymond Hettinger43442782004-03-17 21:55:03 +00001128 dictentry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001129
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001130 /* Preallocate the list of tuples, to avoid allocations during
1131 * the loop over the items, which could trigger GC, which
1132 * could resize the dict. :-(
1133 */
1134 again:
1135 n = mp->ma_used;
1136 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001137 if (v == NULL)
1138 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001139 for (i = 0; i < n; i++) {
1140 item = PyTuple_New(2);
1141 if (item == NULL) {
1142 Py_DECREF(v);
1143 return NULL;
1144 }
1145 PyList_SET_ITEM(v, i, item);
1146 }
1147 if (n != mp->ma_used) {
1148 /* Durnit. The allocations caused the dict to resize.
1149 * Just start over, this shouldn't normally happen.
1150 */
1151 Py_DECREF(v);
1152 goto again;
1153 }
1154 /* Nothing we do below makes any function calls. */
Raymond Hettinger43442782004-03-17 21:55:03 +00001155 ep = mp->ma_table;
1156 mask = mp->ma_mask;
1157 for (i = 0, j = 0; i <= mask; i++) {
Raymond Hettinger06905122004-03-19 10:30:00 +00001158 if ((value=ep[i].me_value) != NULL) {
Raymond Hettinger43442782004-03-17 21:55:03 +00001159 key = ep[i].me_key;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001160 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001161 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001162 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001163 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001164 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001165 j++;
1166 }
1167 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001168 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001169 return v;
1170}
1171
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001172static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001173dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001174{
1175 PyObject *seq;
1176 PyObject *value = Py_None;
1177 PyObject *it; /* iter(seq) */
1178 PyObject *key;
1179 PyObject *d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001180 int status;
1181
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001182 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001183 return NULL;
1184
1185 d = PyObject_CallObject(cls, NULL);
1186 if (d == NULL)
1187 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001188
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00001189 if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
1190 dictobject *mp = (dictobject *)d;
1191 Py_ssize_t pos = 0;
1192 PyObject *key;
1193 long hash;
1194
1195 if (dictresize(mp, PySet_GET_SIZE(seq)))
1196 return NULL;
1197
1198 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
1199 Py_INCREF(key);
Raymond Hettingere3146f52007-03-21 20:33:57 +00001200 Py_INCREF(value);
1201 if (insertdict(mp, key, hash, value))
Raymond Hettinger0bbbfc42007-03-20 21:27:24 +00001202 return NULL;
1203 }
1204 return d;
1205 }
1206
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001207 it = PyObject_GetIter(seq);
1208 if (it == NULL){
1209 Py_DECREF(d);
1210 return NULL;
1211 }
1212
1213 for (;;) {
1214 key = PyIter_Next(it);
1215 if (key == NULL) {
1216 if (PyErr_Occurred())
1217 goto Fail;
1218 break;
1219 }
Raymond Hettingere03e5b12002-12-07 08:10:51 +00001220 status = PyObject_SetItem(d, key, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001221 Py_DECREF(key);
1222 if (status < 0)
1223 goto Fail;
1224 }
1225
1226 Py_DECREF(it);
1227 return d;
1228
1229Fail:
1230 Py_DECREF(it);
1231 Py_DECREF(d);
1232 return NULL;
1233}
1234
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001235static int
1236dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001237{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001238 PyObject *arg = NULL;
1239 int result = 0;
1240
1241 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1242 result = -1;
1243
1244 else if (arg != NULL) {
1245 if (PyObject_HasAttrString(arg, "keys"))
1246 result = PyDict_Merge(self, arg, 1);
1247 else
1248 result = PyDict_MergeFromSeq2(self, arg, 1);
1249 }
1250 if (result == 0 && kwds != NULL)
1251 result = PyDict_Merge(self, kwds, 1);
1252 return result;
1253}
1254
1255static PyObject *
1256dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1257{
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001258 if (dict_update_common(self, args, kwds, "update") != -1)
1259 Py_RETURN_NONE;
1260 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001261}
1262
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001263/* Update unconditionally replaces existing items.
1264 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001265 otherwise it leaves existing items unchanged.
1266
1267 PyDict_{Update,Merge} update/merge from a mapping object.
1268
Tim Petersf582b822001-12-11 18:51:08 +00001269 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001270 producing iterable objects of length 2.
1271*/
1272
Tim Petersf582b822001-12-11 18:51:08 +00001273int
Tim Peters1fc240e2001-10-26 05:06:50 +00001274PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1275{
1276 PyObject *it; /* iter(seq2) */
Tim Peters9b10f7e2006-05-30 04:16:25 +00001277 Py_ssize_t i; /* index into seq2 of current element */
Tim Peters1fc240e2001-10-26 05:06:50 +00001278 PyObject *item; /* seq2[i] */
1279 PyObject *fast; /* item as a 2-tuple or 2-list */
1280
1281 assert(d != NULL);
1282 assert(PyDict_Check(d));
1283 assert(seq2 != NULL);
1284
1285 it = PyObject_GetIter(seq2);
1286 if (it == NULL)
1287 return -1;
1288
1289 for (i = 0; ; ++i) {
1290 PyObject *key, *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001291 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001292
1293 fast = NULL;
1294 item = PyIter_Next(it);
1295 if (item == NULL) {
1296 if (PyErr_Occurred())
1297 goto Fail;
1298 break;
1299 }
1300
1301 /* Convert item to sequence, and verify length 2. */
1302 fast = PySequence_Fast(item, "");
1303 if (fast == NULL) {
1304 if (PyErr_ExceptionMatches(PyExc_TypeError))
1305 PyErr_Format(PyExc_TypeError,
1306 "cannot convert dictionary update "
Neal Norwitzd3881b02006-05-30 04:25:05 +00001307 "sequence element #%zd to a sequence",
Tim Peters1fc240e2001-10-26 05:06:50 +00001308 i);
1309 goto Fail;
1310 }
1311 n = PySequence_Fast_GET_SIZE(fast);
1312 if (n != 2) {
1313 PyErr_Format(PyExc_ValueError,
Neal Norwitzd3881b02006-05-30 04:25:05 +00001314 "dictionary update sequence element #%zd "
Martin v. Löwis2c95cc62006-02-16 06:54:25 +00001315 "has length %zd; 2 is required",
Martin v. Löwise0e89f72006-02-16 06:59:22 +00001316 i, n);
Tim Peters1fc240e2001-10-26 05:06:50 +00001317 goto Fail;
1318 }
1319
1320 /* Update/merge with this (key, value) pair. */
1321 key = PySequence_Fast_GET_ITEM(fast, 0);
1322 value = PySequence_Fast_GET_ITEM(fast, 1);
1323 if (override || PyDict_GetItem(d, key) == NULL) {
1324 int status = PyDict_SetItem(d, key, value);
1325 if (status < 0)
1326 goto Fail;
1327 }
1328 Py_DECREF(fast);
1329 Py_DECREF(item);
1330 }
1331
1332 i = 0;
1333 goto Return;
1334Fail:
1335 Py_XDECREF(item);
1336 Py_XDECREF(fast);
1337 i = -1;
1338Return:
1339 Py_DECREF(it);
Neal Norwitzd3881b02006-05-30 04:25:05 +00001340 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001341}
1342
Tim Peters6d6c1a32001-08-02 04:15:00 +00001343int
1344PyDict_Update(PyObject *a, PyObject *b)
1345{
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001346 return PyDict_Merge(a, b, 1);
1347}
1348
1349int
1350PyDict_Merge(PyObject *a, PyObject *b, int override)
1351{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001352 register PyDictObject *mp, *other;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001353 register Py_ssize_t i;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001354 dictentry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001355
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001356 /* We accept for the argument either a concrete dictionary object,
1357 * or an abstract "mapping" object. For the former, we can do
1358 * things quite efficiently. For the latter, we only require that
1359 * PyMapping_Keys() and PyObject_GetItem() be supported.
1360 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001361 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1362 PyErr_BadInternalCall();
1363 return -1;
1364 }
1365 mp = (dictobject*)a;
Raymond Hettinger0922d712007-02-07 20:08:22 +00001366 if (PyDict_CheckExact(b)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001367 other = (dictobject*)b;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001368 if (other == mp || other->ma_used == 0)
1369 /* a.update(a) or a.update({}); nothing to do */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001370 return 0;
Raymond Hettinger186e7392005-05-14 18:08:25 +00001371 if (mp->ma_used == 0)
1372 /* Since the target dict is empty, PyDict_GetItem()
1373 * always returns NULL. Setting override to 1
1374 * skips the unnecessary test.
1375 */
1376 override = 1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001377 /* Do one big resize at the start, rather than
1378 * incrementally resizing as we insert new items. Expect
1379 * that there will be no (or few) overlapping keys.
1380 */
1381 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
Raymond Hettingerc8d22902003-05-07 00:49:40 +00001382 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001383 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001384 }
1385 for (i = 0; i <= other->ma_mask; i++) {
1386 entry = &other->ma_table[i];
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001387 if (entry->me_value != NULL &&
1388 (override ||
1389 PyDict_GetItem(a, entry->me_key) == NULL)) {
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001390 Py_INCREF(entry->me_key);
1391 Py_INCREF(entry->me_value);
Armin Rigo35f6d362006-06-01 13:19:12 +00001392 if (insertdict(mp, entry->me_key,
1393 (long)entry->me_hash,
1394 entry->me_value) != 0)
1395 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001396 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001397 }
1398 }
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001399 else {
1400 /* Do it the generic, slower way */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001401 PyObject *keys = PyMapping_Keys(b);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001402 PyObject *iter;
1403 PyObject *key, *value;
1404 int status;
1405
1406 if (keys == NULL)
1407 /* Docstring says this is equivalent to E.keys() so
1408 * if E doesn't have a .keys() method we want
1409 * AttributeError to percolate up. Might as well
1410 * do the same for any other error.
1411 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001412 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001413
1414 iter = PyObject_GetIter(keys);
1415 Py_DECREF(keys);
1416 if (iter == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001417 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001418
1419 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001420 if (!override && PyDict_GetItem(a, key) != NULL) {
1421 Py_DECREF(key);
1422 continue;
1423 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001424 value = PyObject_GetItem(b, key);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001425 if (value == NULL) {
1426 Py_DECREF(iter);
1427 Py_DECREF(key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001428 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001429 }
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001430 status = PyDict_SetItem(a, key, value);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001431 Py_DECREF(key);
1432 Py_DECREF(value);
1433 if (status < 0) {
1434 Py_DECREF(iter);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001435 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001436 }
1437 }
1438 Py_DECREF(iter);
1439 if (PyErr_Occurred())
1440 /* Iterator completed, via error */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001441 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001442 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001443 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001444}
1445
1446static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001447dict_copy(register dictobject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001448{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001449 return PyDict_Copy((PyObject*)mp);
1450}
1451
1452PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001453PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001454{
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001455 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001456
1457 if (o == NULL || !PyDict_Check(o)) {
1458 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001459 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001460 }
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001461 copy = PyDict_New();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001462 if (copy == NULL)
1463 return NULL;
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001464 if (PyDict_Merge(copy, o, 1) == 0)
1465 return copy;
1466 Py_DECREF(copy);
Raymond Hettinger1356f782005-04-15 15:58:42 +00001467 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001468}
1469
Martin v. Löwis18e16552006-02-15 17:27:45 +00001470Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001471PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001472{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001473 if (mp == NULL || !PyDict_Check(mp)) {
1474 PyErr_BadInternalCall();
Jeremy Hylton7083bb72004-02-17 20:10:11 +00001475 return -1;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001476 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001477 return ((dictobject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001478}
1479
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001480PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001481PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001482{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001483 if (mp == NULL || !PyDict_Check(mp)) {
1484 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001485 return NULL;
1486 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001487 return dict_keys((dictobject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001488}
1489
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001490PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001491PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001492{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001493 if (mp == NULL || !PyDict_Check(mp)) {
1494 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001495 return NULL;
1496 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001497 return dict_values((dictobject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001498}
1499
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001500PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001501PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001502{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001503 if (mp == NULL || !PyDict_Check(mp)) {
1504 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001505 return NULL;
1506 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001507 return dict_items((dictobject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001508}
1509
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001510/* Subroutine which returns the smallest key in a for which b's value
1511 is different or absent. The value is returned too, through the
Tim Peters95bf9392001-05-10 08:32:44 +00001512 pval argument. Both are NULL if no key in a is found for which b's status
1513 differs. The refcounts on (and only on) non-NULL *pval and function return
1514 values must be decremented by the caller (characterize() increments them
1515 to ensure that mutating comparison and PyDict_GetItem calls can't delete
1516 them before the caller is done looking at them). */
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001517
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001518static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001519characterize(dictobject *a, dictobject *b, PyObject **pval)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001520{
Tim Peters95bf9392001-05-10 08:32:44 +00001521 PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
1522 PyObject *aval = NULL; /* a[akey] */
Tim Peters9b10f7e2006-05-30 04:16:25 +00001523 Py_ssize_t i;
1524 int cmp;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001525
Tim Petersafb6ae82001-06-04 21:00:21 +00001526 for (i = 0; i <= a->ma_mask; i++) {
Tim Peters95bf9392001-05-10 08:32:44 +00001527 PyObject *thiskey, *thisaval, *thisbval;
1528 if (a->ma_table[i].me_value == NULL)
1529 continue;
1530 thiskey = a->ma_table[i].me_key;
1531 Py_INCREF(thiskey); /* keep alive across compares */
1532 if (akey != NULL) {
1533 cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
1534 if (cmp < 0) {
1535 Py_DECREF(thiskey);
1536 goto Fail;
1537 }
1538 if (cmp > 0 ||
Tim Petersafb6ae82001-06-04 21:00:21 +00001539 i > a->ma_mask ||
Tim Peters95bf9392001-05-10 08:32:44 +00001540 a->ma_table[i].me_value == NULL)
1541 {
1542 /* Not the *smallest* a key; or maybe it is
1543 * but the compare shrunk the dict so we can't
1544 * find its associated value anymore; or
1545 * maybe it is but the compare deleted the
1546 * a[thiskey] entry.
Neal Norwitzd3881b02006-05-30 04:25:05 +00001547 */
Tim Peters95bf9392001-05-10 08:32:44 +00001548 Py_DECREF(thiskey);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001549 continue;
Guido van Rossumb9324202001-01-18 00:39:02 +00001550 }
Tim Peters95bf9392001-05-10 08:32:44 +00001551 }
1552
1553 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
1554 thisaval = a->ma_table[i].me_value;
1555 assert(thisaval);
1556 Py_INCREF(thisaval); /* keep alive */
1557 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
1558 if (thisbval == NULL)
1559 cmp = 0;
1560 else {
1561 /* both dicts have thiskey: same values? */
1562 cmp = PyObject_RichCompareBool(
1563 thisaval, thisbval, Py_EQ);
1564 if (cmp < 0) {
1565 Py_DECREF(thiskey);
1566 Py_DECREF(thisaval);
1567 goto Fail;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001568 }
1569 }
Tim Peters95bf9392001-05-10 08:32:44 +00001570 if (cmp == 0) {
1571 /* New winner. */
1572 Py_XDECREF(akey);
1573 Py_XDECREF(aval);
1574 akey = thiskey;
1575 aval = thisaval;
1576 }
1577 else {
1578 Py_DECREF(thiskey);
1579 Py_DECREF(thisaval);
1580 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001581 }
Tim Peters95bf9392001-05-10 08:32:44 +00001582 *pval = aval;
1583 return akey;
1584
1585Fail:
1586 Py_XDECREF(akey);
1587 Py_XDECREF(aval);
1588 *pval = NULL;
1589 return NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001590}
1591
1592static int
Tim Peters1f5871e2000-07-04 17:44:48 +00001593dict_compare(dictobject *a, dictobject *b)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001594{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001595 PyObject *adiff, *bdiff, *aval, *bval;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001596 int res;
1597
1598 /* Compare lengths first */
1599 if (a->ma_used < b->ma_used)
1600 return -1; /* a is shorter */
1601 else if (a->ma_used > b->ma_used)
1602 return 1; /* b is shorter */
Tim Peters95bf9392001-05-10 08:32:44 +00001603
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001604 /* Same length -- check all keys */
Tim Peters95bf9392001-05-10 08:32:44 +00001605 bdiff = bval = NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001606 adiff = characterize(a, b, &aval);
Tim Peters95bf9392001-05-10 08:32:44 +00001607 if (adiff == NULL) {
1608 assert(!aval);
Tim Peters3918fb22001-05-10 18:58:31 +00001609 /* Either an error, or a is a subset with the same length so
Tim Peters95bf9392001-05-10 08:32:44 +00001610 * must be equal.
1611 */
1612 res = PyErr_Occurred() ? -1 : 0;
1613 goto Finished;
1614 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001615 bdiff = characterize(b, a, &bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001616 if (bdiff == NULL && PyErr_Occurred()) {
1617 assert(!bval);
1618 res = -1;
1619 goto Finished;
1620 }
1621 res = 0;
1622 if (bdiff) {
1623 /* bdiff == NULL "should be" impossible now, but perhaps
1624 * the last comparison done by the characterize() on a had
1625 * the side effect of making the dicts equal!
1626 */
1627 res = PyObject_Compare(adiff, bdiff);
1628 }
1629 if (res == 0 && bval != NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001630 res = PyObject_Compare(aval, bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001631
1632Finished:
1633 Py_XDECREF(adiff);
1634 Py_XDECREF(bdiff);
1635 Py_XDECREF(aval);
1636 Py_XDECREF(bval);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001637 return res;
1638}
1639
Tim Peterse63415e2001-05-08 04:38:29 +00001640/* Return 1 if dicts equal, 0 if not, -1 if error.
1641 * Gets out as soon as any difference is detected.
1642 * Uses only Py_EQ comparison.
1643 */
1644static int
1645dict_equal(dictobject *a, dictobject *b)
1646{
Tim Peters9b10f7e2006-05-30 04:16:25 +00001647 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00001648
1649 if (a->ma_used != b->ma_used)
1650 /* can't be equal if # of entries differ */
1651 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001652
Tim Peterse63415e2001-05-08 04:38:29 +00001653 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Tim Petersafb6ae82001-06-04 21:00:21 +00001654 for (i = 0; i <= a->ma_mask; i++) {
Tim Peterse63415e2001-05-08 04:38:29 +00001655 PyObject *aval = a->ma_table[i].me_value;
1656 if (aval != NULL) {
1657 int cmp;
1658 PyObject *bval;
1659 PyObject *key = a->ma_table[i].me_key;
1660 /* temporarily bump aval's refcount to ensure it stays
1661 alive until we're done with it */
1662 Py_INCREF(aval);
Neal Norwitza22975f2006-09-05 02:24:03 +00001663 /* ditto for key */
1664 Py_INCREF(key);
Tim Peterse63415e2001-05-08 04:38:29 +00001665 bval = PyDict_GetItem((PyObject *)b, key);
Neal Norwitza22975f2006-09-05 02:24:03 +00001666 Py_DECREF(key);
Tim Peterse63415e2001-05-08 04:38:29 +00001667 if (bval == NULL) {
1668 Py_DECREF(aval);
1669 return 0;
1670 }
1671 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1672 Py_DECREF(aval);
1673 if (cmp <= 0) /* error or not equal */
1674 return cmp;
1675 }
1676 }
1677 return 1;
1678 }
1679
1680static PyObject *
1681dict_richcompare(PyObject *v, PyObject *w, int op)
1682{
1683 int cmp;
1684 PyObject *res;
1685
1686 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1687 res = Py_NotImplemented;
1688 }
1689 else if (op == Py_EQ || op == Py_NE) {
1690 cmp = dict_equal((dictobject *)v, (dictobject *)w);
1691 if (cmp < 0)
1692 return NULL;
1693 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1694 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001695 else
1696 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001697 Py_INCREF(res);
1698 return res;
1699 }
1700
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001701static PyObject *
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +00001702dict_contains(register dictobject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001703{
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001704 long hash;
Armin Rigo35f6d362006-06-01 13:19:12 +00001705 dictentry *ep;
Tim Petersd770ebd2006-06-01 15:50:44 +00001706
Tim Peters0ab085c2001-09-14 00:25:33 +00001707 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001708 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001709 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001710 if (hash == -1)
1711 return NULL;
1712 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001713 ep = (mp->ma_lookup)(mp, key, hash);
1714 if (ep == NULL)
1715 return NULL;
Tim Petersd770ebd2006-06-01 15:50:44 +00001716 return PyBool_FromLong(ep->me_value != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001717}
1718
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001719static PyObject *
Neal Norwitz8b2bfbc2007-05-23 06:35:32 +00001720dict_has_key(register dictobject *mp, PyObject *key)
1721{
1722 if (Py_Py3kWarningFlag &&
1723 PyErr_Warn(PyExc_DeprecationWarning,
1724 "dict.has_key() not supported in 3.x") < 0)
1725 return NULL;
1726 return dict_contains(mp, key);
1727}
1728
1729static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001730dict_get(register dictobject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001731{
1732 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001733 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001734 PyObject *val = NULL;
1735 long hash;
Armin Rigo35f6d362006-06-01 13:19:12 +00001736 dictentry *ep;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001737
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001738 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001739 return NULL;
1740
Tim Peters0ab085c2001-09-14 00:25:33 +00001741 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001742 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Barry Warsawc38c5da1997-10-06 17:49:20 +00001743 hash = PyObject_Hash(key);
1744 if (hash == -1)
1745 return NULL;
1746 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001747 ep = (mp->ma_lookup)(mp, key, hash);
1748 if (ep == NULL)
1749 return NULL;
1750 val = ep->me_value;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001751 if (val == NULL)
1752 val = failobj;
1753 Py_INCREF(val);
1754 return val;
1755}
1756
1757
1758static PyObject *
Guido van Rossum164452c2000-08-08 16:12:54 +00001759dict_setdefault(register dictobject *mp, PyObject *args)
1760{
1761 PyObject *key;
1762 PyObject *failobj = Py_None;
1763 PyObject *val = NULL;
1764 long hash;
Armin Rigo35f6d362006-06-01 13:19:12 +00001765 dictentry *ep;
Guido van Rossum164452c2000-08-08 16:12:54 +00001766
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001767 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
Guido van Rossum164452c2000-08-08 16:12:54 +00001768 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001769
Tim Peters0ab085c2001-09-14 00:25:33 +00001770 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001771 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum164452c2000-08-08 16:12:54 +00001772 hash = PyObject_Hash(key);
1773 if (hash == -1)
1774 return NULL;
1775 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001776 ep = (mp->ma_lookup)(mp, key, hash);
1777 if (ep == NULL)
1778 return NULL;
1779 val = ep->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001780 if (val == NULL) {
1781 val = failobj;
1782 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1783 val = NULL;
1784 }
1785 Py_XINCREF(val);
1786 return val;
1787}
1788
1789
1790static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001791dict_clear(register dictobject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001792{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001793 PyDict_Clear((PyObject *)mp);
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001794 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001795}
1796
Guido van Rossumba6ab842000-12-12 22:02:18 +00001797static PyObject *
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001798dict_pop(dictobject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00001799{
1800 long hash;
1801 dictentry *ep;
1802 PyObject *old_value, *old_key;
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001803 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001804
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001805 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1806 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001807 if (mp->ma_used == 0) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001808 if (deflt) {
1809 Py_INCREF(deflt);
1810 return deflt;
1811 }
Guido van Rossume027d982002-04-12 15:11:59 +00001812 PyErr_SetString(PyExc_KeyError,
1813 "pop(): dictionary is empty");
1814 return NULL;
1815 }
1816 if (!PyString_CheckExact(key) ||
1817 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1818 hash = PyObject_Hash(key);
1819 if (hash == -1)
1820 return NULL;
1821 }
1822 ep = (mp->ma_lookup)(mp, key, hash);
Armin Rigo35f6d362006-06-01 13:19:12 +00001823 if (ep == NULL)
1824 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001825 if (ep->me_value == NULL) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001826 if (deflt) {
1827 Py_INCREF(deflt);
1828 return deflt;
1829 }
Georg Brandlb9f4ad32006-10-29 18:31:42 +00001830 set_key_error(key);
Guido van Rossume027d982002-04-12 15:11:59 +00001831 return NULL;
1832 }
1833 old_key = ep->me_key;
1834 Py_INCREF(dummy);
1835 ep->me_key = dummy;
1836 old_value = ep->me_value;
1837 ep->me_value = NULL;
1838 mp->ma_used--;
1839 Py_DECREF(old_key);
1840 return old_value;
1841}
1842
1843static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001844dict_popitem(dictobject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001845{
Tim Peters9b10f7e2006-05-30 04:16:25 +00001846 Py_ssize_t i = 0;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001847 dictentry *ep;
1848 PyObject *res;
1849
Tim Petersf4b33f62001-06-02 05:42:29 +00001850 /* Allocate the result tuple before checking the size. Believe it
1851 * or not, this allocation could trigger a garbage collection which
1852 * could empty the dict, so if we checked the size first and that
1853 * happened, the result would be an infinite loop (searching for an
1854 * entry that no longer exists). Note that the usual popitem()
1855 * idiom is "while d: k, v = d.popitem()". so needing to throw the
Tim Peters9b10f7e2006-05-30 04:16:25 +00001856 * tuple away if the dict *is* empty isn't a significant
Tim Petersf4b33f62001-06-02 05:42:29 +00001857 * inefficiency -- possible, but unlikely in practice.
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001858 */
1859 res = PyTuple_New(2);
1860 if (res == NULL)
1861 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001862 if (mp->ma_used == 0) {
1863 Py_DECREF(res);
1864 PyErr_SetString(PyExc_KeyError,
1865 "popitem(): dictionary is empty");
1866 return NULL;
1867 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001868 /* Set ep to "the first" dict entry with a value. We abuse the hash
1869 * field of slot 0 to hold a search finger:
1870 * If slot 0 has a value, use slot 0.
1871 * Else slot 0 is being used to hold a search finger,
Neal Norwitzd3881b02006-05-30 04:25:05 +00001872 * and we use its hash value as the first index to look.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001873 */
1874 ep = &mp->ma_table[0];
1875 if (ep->me_value == NULL) {
Tim Peters9b10f7e2006-05-30 04:16:25 +00001876 i = ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001877 /* The hash field may be a real hash value, or it may be a
1878 * legit search finger, or it may be a once-legit search
1879 * finger that's out of bounds now because it wrapped around
1880 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001881 */
Tim Petersafb6ae82001-06-04 21:00:21 +00001882 if (i > mp->ma_mask || i < 1)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001883 i = 1; /* skip slot 0 */
1884 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1885 i++;
Tim Petersafb6ae82001-06-04 21:00:21 +00001886 if (i > mp->ma_mask)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001887 i = 1;
1888 }
1889 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001890 PyTuple_SET_ITEM(res, 0, ep->me_key);
1891 PyTuple_SET_ITEM(res, 1, ep->me_value);
1892 Py_INCREF(dummy);
1893 ep->me_key = dummy;
1894 ep->me_value = NULL;
1895 mp->ma_used--;
1896 assert(mp->ma_table[0].me_value == NULL);
1897 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001898 return res;
1899}
1900
Jeremy Hylton8caad492000-06-23 14:18:11 +00001901static int
1902dict_traverse(PyObject *op, visitproc visit, void *arg)
1903{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001904 Py_ssize_t i = 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001905 PyObject *pk;
1906 PyObject *pv;
1907
1908 while (PyDict_Next(op, &i, &pk, &pv)) {
Thomas Woutersc6e55062006-04-15 21:47:09 +00001909 Py_VISIT(pk);
1910 Py_VISIT(pv);
Jeremy Hylton8caad492000-06-23 14:18:11 +00001911 }
1912 return 0;
1913}
1914
1915static int
1916dict_tp_clear(PyObject *op)
1917{
1918 PyDict_Clear(op);
1919 return 0;
1920}
1921
Tim Petersf7f88b12000-12-13 23:18:45 +00001922
Raymond Hettinger019a1482004-03-18 02:41:19 +00001923extern PyTypeObject PyDictIterKey_Type; /* Forward */
1924extern PyTypeObject PyDictIterValue_Type; /* Forward */
1925extern PyTypeObject PyDictIterItem_Type; /* Forward */
1926static PyObject *dictiter_new(dictobject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001927
1928static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001929dict_iterkeys(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001930{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001931 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001932}
1933
1934static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001935dict_itervalues(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001936{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001937 return dictiter_new(dict, &PyDictIterValue_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001938}
1939
1940static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001941dict_iteritems(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001942{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001943 return dictiter_new(dict, &PyDictIterItem_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001944}
1945
1946
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001947PyDoc_STRVAR(has_key__doc__,
Raymond Hettinger574aa322003-09-01 22:12:08 +00001948"D.has_key(k) -> True if D has a key k, else False");
Tim Petersf7f88b12000-12-13 23:18:45 +00001949
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001950PyDoc_STRVAR(contains__doc__,
1951"D.__contains__(k) -> True if D has a key k, else False");
1952
1953PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
1954
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001955PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001956"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001957
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001958PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001959"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 +00001960
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001961PyDoc_STRVAR(pop__doc__,
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001962"D.pop(k[,d]) -> v, remove specified key and return the corresponding value\n\
1963If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00001964
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001965PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00001966"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000019672-tuple; but raise KeyError if D is empty");
Tim Petersf7f88b12000-12-13 23:18:45 +00001968
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001969PyDoc_STRVAR(keys__doc__,
1970"D.keys() -> list of D's keys");
Tim Petersf7f88b12000-12-13 23:18:45 +00001971
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001972PyDoc_STRVAR(items__doc__,
1973"D.items() -> list of D's (key, value) pairs, as 2-tuples");
Tim Petersf7f88b12000-12-13 23:18:45 +00001974
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001975PyDoc_STRVAR(values__doc__,
1976"D.values() -> list of D's values");
Tim Petersf7f88b12000-12-13 23:18:45 +00001977
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001978PyDoc_STRVAR(update__doc__,
Walter Dörwaldd70ad8a2004-05-28 20:59:21 +00001979"D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k]\n\
1980(if E has keys else: for (k, v) in E: D[k] = v) then: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00001981
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001982PyDoc_STRVAR(fromkeys__doc__,
1983"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
1984v defaults to None.");
1985
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001986PyDoc_STRVAR(clear__doc__,
1987"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001988
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001989PyDoc_STRVAR(copy__doc__,
1990"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00001991
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001992PyDoc_STRVAR(iterkeys__doc__,
1993"D.iterkeys() -> an iterator over the keys of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00001994
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001995PyDoc_STRVAR(itervalues__doc__,
1996"D.itervalues() -> an iterator over the values of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00001997
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001998PyDoc_STRVAR(iteritems__doc__,
1999"D.iteritems() -> an iterator over the (key, value) items of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00002000
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002001static PyMethodDef mapp_methods[] = {
Neal Norwitzc7926292007-05-23 06:57:35 +00002002 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002003 contains__doc__},
Raymond Hettinger0c669672003-12-13 13:31:55 +00002004 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00002005 getitem__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002006 {"has_key", (PyCFunction)dict_has_key, METH_O,
Tim Petersf7f88b12000-12-13 23:18:45 +00002007 has_key__doc__},
2008 {"get", (PyCFunction)dict_get, METH_VARARGS,
2009 get__doc__},
2010 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
2011 setdefault_doc__},
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002012 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
Just van Rossuma797d812002-11-23 09:45:04 +00002013 pop__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002014 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002015 popitem__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002016 {"keys", (PyCFunction)dict_keys, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002017 keys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002018 {"items", (PyCFunction)dict_items, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002019 items__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002020 {"values", (PyCFunction)dict_values, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002021 values__doc__},
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002022 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002023 update__doc__},
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002024 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
2025 fromkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002026 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002027 clear__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002028 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002029 copy__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002030 {"iterkeys", (PyCFunction)dict_iterkeys, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00002031 iterkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002032 {"itervalues", (PyCFunction)dict_itervalues, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00002033 itervalues__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002034 {"iteritems", (PyCFunction)dict_iteritems, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00002035 iteritems__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00002036 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002037};
2038
Tim Petersd770ebd2006-06-01 15:50:44 +00002039/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002040int
2041PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002042{
2043 long hash;
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002044 dictobject *mp = (dictobject *)op;
Armin Rigo35f6d362006-06-01 13:19:12 +00002045 dictentry *ep;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002046
Tim Peters0ab085c2001-09-14 00:25:33 +00002047 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00002048 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002049 hash = PyObject_Hash(key);
2050 if (hash == -1)
2051 return -1;
2052 }
Armin Rigo35f6d362006-06-01 13:19:12 +00002053 ep = (mp->ma_lookup)(mp, key, hash);
Tim Petersd770ebd2006-06-01 15:50:44 +00002054 return ep == NULL ? -1 : (ep->me_value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002055}
2056
Raymond Hettingerd6fc72a2007-02-19 02:03:19 +00002057/* Internal version of PyDict_Contains used when the hash value is already known */
2058int
2059_PyDict_Contains(PyObject *op, PyObject *key, long hash)
2060{
2061 dictobject *mp = (dictobject *)op;
2062 dictentry *ep;
2063
2064 ep = (mp->ma_lookup)(mp, key, hash);
2065 return ep == NULL ? -1 : (ep->me_value != NULL);
2066}
2067
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002068/* Hack to implement "key in dict" */
2069static PySequenceMethods dict_as_sequence = {
Georg Brandl347b3002006-03-30 11:57:00 +00002070 0, /* sq_length */
2071 0, /* sq_concat */
2072 0, /* sq_repeat */
2073 0, /* sq_item */
2074 0, /* sq_slice */
2075 0, /* sq_ass_item */
2076 0, /* sq_ass_slice */
2077 PyDict_Contains, /* sq_contains */
2078 0, /* sq_inplace_concat */
2079 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002080};
2081
Guido van Rossum09e563a2001-05-01 12:10:21 +00002082static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002083dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2084{
2085 PyObject *self;
2086
2087 assert(type != NULL && type->tp_alloc != NULL);
2088 self = type->tp_alloc(type, 0);
2089 if (self != NULL) {
2090 PyDictObject *d = (PyDictObject *)self;
2091 /* It's guaranteed that tp->alloc zeroed out the struct. */
2092 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
2093 INIT_NONZERO_DICT_SLOTS(d);
2094 d->ma_lookup = lookdict_string;
2095#ifdef SHOW_CONVERSION_COUNTS
2096 ++created;
2097#endif
2098 }
2099 return self;
2100}
2101
Tim Peters25786c02001-09-02 08:22:48 +00002102static int
2103dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2104{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002105 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002106}
2107
Guido van Rossumdbb53d92001-12-03 16:32:18 +00002108static long
2109dict_nohash(PyObject *self)
2110{
2111 PyErr_SetString(PyExc_TypeError, "dict objects are unhashable");
2112 return -1;
2113}
2114
Tim Peters6d6c1a32001-08-02 04:15:00 +00002115static PyObject *
Guido van Rossum09e563a2001-05-01 12:10:21 +00002116dict_iter(dictobject *dict)
2117{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002118 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002119}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002120
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002121PyDoc_STRVAR(dictionary_doc,
Tim Petersa427a2b2001-10-29 22:25:45 +00002122"dict() -> new empty dictionary.\n"
2123"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Tim Peters1fc240e2001-10-26 05:06:50 +00002124" (key, value) pairs.\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002125"dict(seq) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002126" d = {}\n"
2127" for k, v in seq:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002128" d[k] = v\n"
2129"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2130" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002131
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002132PyTypeObject PyDict_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002133 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Petersa427a2b2001-10-29 22:25:45 +00002134 "dict",
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002135 sizeof(dictobject),
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002136 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00002137 (destructor)dict_dealloc, /* tp_dealloc */
2138 (printfunc)dict_print, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002139 0, /* tp_getattr */
Guido van Rossumb9324202001-01-18 00:39:02 +00002140 0, /* tp_setattr */
Tim Peters4fa58bf2001-05-10 21:45:19 +00002141 (cmpfunc)dict_compare, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00002142 (reprfunc)dict_repr, /* tp_repr */
2143 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002144 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00002145 &dict_as_mapping, /* tp_as_mapping */
Guido van Rossumdbb53d92001-12-03 16:32:18 +00002146 dict_nohash, /* tp_hash */
Guido van Rossumb9324202001-01-18 00:39:02 +00002147 0, /* tp_call */
2148 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002149 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumb9324202001-01-18 00:39:02 +00002150 0, /* tp_setattro */
2151 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002152 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Neal Norwitzee3a1b52007-02-25 19:44:48 +00002153 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
Tim Peters25786c02001-09-02 08:22:48 +00002154 dictionary_doc, /* tp_doc */
Georg Brandl347b3002006-03-30 11:57:00 +00002155 dict_traverse, /* tp_traverse */
2156 dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00002157 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002158 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00002159 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002160 0, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002161 mapp_methods, /* tp_methods */
2162 0, /* tp_members */
2163 0, /* tp_getset */
2164 0, /* tp_base */
2165 0, /* tp_dict */
2166 0, /* tp_descr_get */
2167 0, /* tp_descr_set */
2168 0, /* tp_dictoffset */
Georg Brandl347b3002006-03-30 11:57:00 +00002169 dict_init, /* tp_init */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002170 PyType_GenericAlloc, /* tp_alloc */
2171 dict_new, /* tp_new */
Neil Schemenauer6189b892002-04-12 02:43:00 +00002172 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002173};
2174
Guido van Rossum3cca2451997-05-16 14:23:33 +00002175/* For backward compatibility with old dictionary interface */
2176
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002177PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002178PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002179{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002180 PyObject *kv, *rv;
2181 kv = PyString_FromString(key);
2182 if (kv == NULL)
2183 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002184 rv = PyDict_GetItem(v, kv);
2185 Py_DECREF(kv);
2186 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002187}
2188
2189int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002190PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002191{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002192 PyObject *kv;
2193 int err;
2194 kv = PyString_FromString(key);
2195 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002196 return -1;
Guido van Rossum4f3bf1e1997-09-29 23:31:11 +00002197 PyString_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00002198 err = PyDict_SetItem(v, kv, item);
2199 Py_DECREF(kv);
2200 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002201}
2202
2203int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002204PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002205{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002206 PyObject *kv;
2207 int err;
2208 kv = PyString_FromString(key);
2209 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002210 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002211 err = PyDict_DelItem(v, kv);
2212 Py_DECREF(kv);
2213 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002214}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002215
Raymond Hettinger019a1482004-03-18 02:41:19 +00002216/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002217
2218typedef struct {
2219 PyObject_HEAD
Guido van Rossum2147df72002-07-16 20:30:22 +00002220 dictobject *di_dict; /* Set to NULL when iterator is exhausted */
Tim Peters9b10f7e2006-05-30 04:16:25 +00002221 Py_ssize_t di_used;
2222 Py_ssize_t di_pos;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002223 PyObject* di_result; /* reusable result tuple for iteritems */
Tim Peters9b10f7e2006-05-30 04:16:25 +00002224 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002225} dictiterobject;
2226
2227static PyObject *
Raymond Hettinger019a1482004-03-18 02:41:19 +00002228dictiter_new(dictobject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002229{
2230 dictiterobject *di;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002231 di = PyObject_New(dictiterobject, itertype);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002232 if (di == NULL)
2233 return NULL;
2234 Py_INCREF(dict);
2235 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00002236 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002237 di->di_pos = 0;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002238 di->len = dict->ma_used;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002239 if (itertype == &PyDictIterItem_Type) {
2240 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2241 if (di->di_result == NULL) {
2242 Py_DECREF(di);
2243 return NULL;
2244 }
2245 }
2246 else
2247 di->di_result = NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002248 return (PyObject *)di;
2249}
2250
2251static void
2252dictiter_dealloc(dictiterobject *di)
2253{
Guido van Rossum2147df72002-07-16 20:30:22 +00002254 Py_XDECREF(di->di_dict);
Raymond Hettinger019a1482004-03-18 02:41:19 +00002255 Py_XDECREF(di->di_result);
Neil Schemenauer6189b892002-04-12 02:43:00 +00002256 PyObject_Del(di);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002257}
2258
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002259static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002260dictiter_len(dictiterobject *di)
2261{
Tim Peters9b10f7e2006-05-30 04:16:25 +00002262 Py_ssize_t len = 0;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00002263 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002264 len = di->len;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002265 return PyInt_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002266}
2267
Armin Rigof5b3e362006-02-11 21:32:43 +00002268PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002269
2270static PyMethodDef dictiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +00002271 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002272 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002273};
2274
Raymond Hettinger019a1482004-03-18 02:41:19 +00002275static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002276{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002277 PyObject *key;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002278 register Py_ssize_t i, mask;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002279 register dictentry *ep;
2280 dictobject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002281
Raymond Hettinger019a1482004-03-18 02:41:19 +00002282 if (d == NULL)
Guido van Rossum2147df72002-07-16 20:30:22 +00002283 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002284 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002285
Raymond Hettinger019a1482004-03-18 02:41:19 +00002286 if (di->di_used != d->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00002287 PyErr_SetString(PyExc_RuntimeError,
2288 "dictionary changed size during iteration");
Guido van Rossum2147df72002-07-16 20:30:22 +00002289 di->di_used = -1; /* Make this state sticky */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002290 return NULL;
2291 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002292
Raymond Hettinger019a1482004-03-18 02:41:19 +00002293 i = di->di_pos;
2294 if (i < 0)
2295 goto fail;
2296 ep = d->ma_table;
2297 mask = d->ma_mask;
2298 while (i <= mask && ep[i].me_value == NULL)
2299 i++;
2300 di->di_pos = i+1;
2301 if (i > mask)
2302 goto fail;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002303 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002304 key = ep[i].me_key;
2305 Py_INCREF(key);
2306 return key;
2307
2308fail:
2309 Py_DECREF(d);
Guido van Rossum2147df72002-07-16 20:30:22 +00002310 di->di_dict = NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002311 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002312}
2313
Raymond Hettinger019a1482004-03-18 02:41:19 +00002314PyTypeObject PyDictIterKey_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002315 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002316 "dictionary-keyiterator", /* tp_name */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002317 sizeof(dictiterobject), /* tp_basicsize */
2318 0, /* tp_itemsize */
2319 /* methods */
2320 (destructor)dictiter_dealloc, /* tp_dealloc */
2321 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002322 0, /* tp_getattr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002323 0, /* tp_setattr */
2324 0, /* tp_compare */
2325 0, /* tp_repr */
2326 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002327 0, /* tp_as_sequence */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002328 0, /* tp_as_mapping */
2329 0, /* tp_hash */
2330 0, /* tp_call */
2331 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002332 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002333 0, /* tp_setattro */
2334 0, /* tp_as_buffer */
2335 Py_TPFLAGS_DEFAULT, /* tp_flags */
2336 0, /* tp_doc */
2337 0, /* tp_traverse */
2338 0, /* tp_clear */
2339 0, /* tp_richcompare */
2340 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002341 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002342 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002343 dictiter_methods, /* tp_methods */
2344 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002345};
2346
2347static PyObject *dictiter_iternextvalue(dictiterobject *di)
2348{
2349 PyObject *value;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002350 register Py_ssize_t i, mask;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002351 register dictentry *ep;
2352 dictobject *d = di->di_dict;
2353
2354 if (d == NULL)
2355 return NULL;
2356 assert (PyDict_Check(d));
2357
2358 if (di->di_used != d->ma_used) {
2359 PyErr_SetString(PyExc_RuntimeError,
2360 "dictionary changed size during iteration");
2361 di->di_used = -1; /* Make this state sticky */
2362 return NULL;
2363 }
2364
2365 i = di->di_pos;
Guido van Rossum09240f62004-03-20 19:11:58 +00002366 mask = d->ma_mask;
2367 if (i < 0 || i > mask)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002368 goto fail;
2369 ep = d->ma_table;
Guido van Rossum09240f62004-03-20 19:11:58 +00002370 while ((value=ep[i].me_value) == NULL) {
Raymond Hettinger019a1482004-03-18 02:41:19 +00002371 i++;
Guido van Rossum09240f62004-03-20 19:11:58 +00002372 if (i > mask)
2373 goto fail;
2374 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002375 di->di_pos = i+1;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002376 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002377 Py_INCREF(value);
2378 return value;
2379
2380fail:
2381 Py_DECREF(d);
2382 di->di_dict = NULL;
2383 return NULL;
2384}
2385
2386PyTypeObject PyDictIterValue_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002387 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002388 "dictionary-valueiterator", /* tp_name */
2389 sizeof(dictiterobject), /* tp_basicsize */
2390 0, /* tp_itemsize */
2391 /* methods */
2392 (destructor)dictiter_dealloc, /* tp_dealloc */
2393 0, /* tp_print */
2394 0, /* tp_getattr */
2395 0, /* tp_setattr */
2396 0, /* tp_compare */
2397 0, /* tp_repr */
2398 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002399 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002400 0, /* tp_as_mapping */
2401 0, /* tp_hash */
2402 0, /* tp_call */
2403 0, /* tp_str */
2404 PyObject_GenericGetAttr, /* tp_getattro */
2405 0, /* tp_setattro */
2406 0, /* tp_as_buffer */
2407 Py_TPFLAGS_DEFAULT, /* tp_flags */
2408 0, /* tp_doc */
2409 0, /* tp_traverse */
2410 0, /* tp_clear */
2411 0, /* tp_richcompare */
2412 0, /* tp_weaklistoffset */
2413 PyObject_SelfIter, /* tp_iter */
2414 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002415 dictiter_methods, /* tp_methods */
2416 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002417};
2418
2419static PyObject *dictiter_iternextitem(dictiterobject *di)
2420{
2421 PyObject *key, *value, *result = di->di_result;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002422 register Py_ssize_t i, mask;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002423 register dictentry *ep;
2424 dictobject *d = di->di_dict;
2425
2426 if (d == NULL)
2427 return NULL;
2428 assert (PyDict_Check(d));
2429
2430 if (di->di_used != d->ma_used) {
2431 PyErr_SetString(PyExc_RuntimeError,
2432 "dictionary changed size during iteration");
2433 di->di_used = -1; /* Make this state sticky */
2434 return NULL;
2435 }
2436
2437 i = di->di_pos;
2438 if (i < 0)
2439 goto fail;
2440 ep = d->ma_table;
2441 mask = d->ma_mask;
2442 while (i <= mask && ep[i].me_value == NULL)
2443 i++;
2444 di->di_pos = i+1;
2445 if (i > mask)
2446 goto fail;
2447
2448 if (result->ob_refcnt == 1) {
2449 Py_INCREF(result);
2450 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2451 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2452 } else {
2453 result = PyTuple_New(2);
Tim Peters60b29962006-01-01 01:19:23 +00002454 if (result == NULL)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002455 return NULL;
2456 }
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002457 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002458 key = ep[i].me_key;
2459 value = ep[i].me_value;
2460 Py_INCREF(key);
2461 Py_INCREF(value);
2462 PyTuple_SET_ITEM(result, 0, key);
2463 PyTuple_SET_ITEM(result, 1, value);
2464 return result;
2465
2466fail:
2467 Py_DECREF(d);
2468 di->di_dict = NULL;
2469 return NULL;
2470}
2471
2472PyTypeObject PyDictIterItem_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00002473 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002474 "dictionary-itemiterator", /* tp_name */
2475 sizeof(dictiterobject), /* tp_basicsize */
2476 0, /* tp_itemsize */
2477 /* methods */
2478 (destructor)dictiter_dealloc, /* tp_dealloc */
2479 0, /* tp_print */
2480 0, /* tp_getattr */
2481 0, /* tp_setattr */
2482 0, /* tp_compare */
2483 0, /* tp_repr */
2484 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002485 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002486 0, /* tp_as_mapping */
2487 0, /* tp_hash */
2488 0, /* tp_call */
2489 0, /* tp_str */
2490 PyObject_GenericGetAttr, /* tp_getattro */
2491 0, /* tp_setattro */
2492 0, /* tp_as_buffer */
2493 Py_TPFLAGS_DEFAULT, /* tp_flags */
2494 0, /* tp_doc */
2495 0, /* tp_traverse */
2496 0, /* tp_clear */
2497 0, /* tp_richcompare */
2498 0, /* tp_weaklistoffset */
2499 PyObject_SelfIter, /* tp_iter */
2500 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002501 dictiter_methods, /* tp_methods */
2502 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002503};