blob: 8c8a2f92e70a8a29a3804132c06e2737da0027de [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 Brandl5e9f94a2006-10-29 18:31:45 +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 Norwitzae6b8412006-10-29 23:42:59 +000026 Py_DECREF(tup);
Georg Brandl5e9f94a2006-10-29 18:31:45 +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);
207 assert (mp->ob_type == &PyDict_Type);
208 _Py_NewReference((PyObject *)mp);
209 if (mp->ma_fill) {
210 EMPTY_TO_MINSIZE(mp);
Georg Brandl45c33ce2008-08-11 09:13:26 +0000211 } else {
212 /* At least set ma_table and ma_mask; these are wrong
213 if an empty but presized dict is added to freelist */
214 INIT_NONZERO_DICT_SLOTS(mp);
Raymond Hettinger43442782004-03-17 21:55:03 +0000215 }
216 assert (mp->ma_used == 0);
217 assert (mp->ma_table == mp->ma_smalltable);
218 assert (mp->ma_mask == PyDict_MINSIZE - 1);
219 } else {
220 mp = PyObject_GC_New(dictobject, &PyDict_Type);
221 if (mp == NULL)
222 return NULL;
223 EMPTY_TO_MINSIZE(mp);
224 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000225 mp->ma_lookup = lookdict_string;
226#ifdef SHOW_CONVERSION_COUNTS
227 ++created;
228#endif
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000229 _PyObject_GC_TRACK(mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000230 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000231}
232
233/*
234The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000235This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000236Open addressing is preferred over chaining since the link overhead for
237chaining would be substantial (100% with typical malloc overhead).
238
Tim Peterseb28ef22001-06-02 05:27:19 +0000239The initial probe index is computed as hash mod the table size. Subsequent
240probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000241
242All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000243
Tim Peterseb28ef22001-06-02 05:27:19 +0000244(The details in this version are due to Tim Peters, building on many past
245contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
246Christian Tismer).
Fred Drake1bff34a2000-08-31 19:31:38 +0000247
Tim Petersd770ebd2006-06-01 15:50:44 +0000248lookdict() is general-purpose, and may return NULL if (and only if) a
249comparison raises an exception (this was new in Python 2.5).
250lookdict_string() below is specialized to string keys, comparison of which can
251never raise an exception; that function can never return NULL. For both, when
252the key isn't found a dictentry* is returned for which the me_value field is
253NULL; this is the slot in the dict at which the key would have been found, and
254the caller can (if it wishes) add the <key, value> pair to the returned
255dictentry*.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000256*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000257static dictentry *
Tim Peters1f5871e2000-07-04 17:44:48 +0000258lookdict(dictobject *mp, PyObject *key, register long hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000259{
Tim Petersd770ebd2006-06-01 15:50:44 +0000260 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000261 register size_t perturb;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000262 register dictentry *freeslot;
Tim Petersd770ebd2006-06-01 15:50:44 +0000263 register size_t mask = (size_t)mp->ma_mask;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000264 dictentry *ep0 = mp->ma_table;
265 register dictentry *ep;
Fred Drakec88b99c2000-08-31 19:04:07 +0000266 register int cmp;
Tim Peters453163d2001-06-03 04:54:32 +0000267 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000268
Tim Petersd770ebd2006-06-01 15:50:44 +0000269 i = (size_t)hash & mask;
Guido van Rossumefb46091997-01-29 15:53:56 +0000270 ep = &ep0[i];
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000271 if (ep->me_key == NULL || ep->me_key == key)
Guido van Rossum7d181591997-01-16 21:06:45 +0000272 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000273
Guido van Rossum16e93a81997-01-28 00:00:11 +0000274 if (ep->me_key == dummy)
Guido van Rossum7d181591997-01-16 21:06:45 +0000275 freeslot = ep;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000276 else {
Fred Drakec88b99c2000-08-31 19:04:07 +0000277 if (ep->me_hash == hash) {
Tim Peters453163d2001-06-03 04:54:32 +0000278 startkey = ep->me_key;
Georg Brandla5463ab2007-11-29 18:33:04 +0000279 Py_INCREF(startkey);
Tim Peters453163d2001-06-03 04:54:32 +0000280 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Georg Brandla5463ab2007-11-29 18:33:04 +0000281 Py_DECREF(startkey);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000282 if (cmp < 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000283 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000284 if (ep0 == mp->ma_table && ep->me_key == startkey) {
285 if (cmp > 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000286 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000287 }
288 else {
289 /* The compare did major nasty stuff to the
290 * dict: start over.
291 * XXX A clever adversary could prevent this
292 * XXX from terminating.
293 */
Armin Rigo35f6d362006-06-01 13:19:12 +0000294 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000295 }
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000296 }
297 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000298 }
Tim Peters15d49292001-05-27 07:39:22 +0000299
Tim Peters342c65e2001-05-13 06:43:53 +0000300 /* In the loop, me_key == dummy is by far (factor of 100s) the
301 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000302 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
303 i = (i << 2) + i + perturb + 1;
304 ep = &ep0[i & mask];
Armin Rigo35f6d362006-06-01 13:19:12 +0000305 if (ep->me_key == NULL)
306 return freeslot == NULL ? ep : freeslot;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000307 if (ep->me_key == key)
Armin Rigo35f6d362006-06-01 13:19:12 +0000308 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000309 if (ep->me_hash == hash && ep->me_key != dummy) {
Tim Peters453163d2001-06-03 04:54:32 +0000310 startkey = ep->me_key;
Georg Brandla5463ab2007-11-29 18:33:04 +0000311 Py_INCREF(startkey);
Tim Peters453163d2001-06-03 04:54:32 +0000312 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Georg Brandla5463ab2007-11-29 18:33:04 +0000313 Py_DECREF(startkey);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000314 if (cmp < 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000315 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000316 if (ep0 == mp->ma_table && ep->me_key == startkey) {
317 if (cmp > 0)
Armin Rigo35f6d362006-06-01 13:19:12 +0000318 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000319 }
320 else {
321 /* The compare did major nasty stuff to the
322 * dict: start over.
323 * XXX A clever adversary could prevent this
324 * XXX from terminating.
325 */
Armin Rigo35f6d362006-06-01 13:19:12 +0000326 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000327 }
Guido van Rossum16e93a81997-01-28 00:00:11 +0000328 }
Tim Peters342c65e2001-05-13 06:43:53 +0000329 else if (ep->me_key == dummy && freeslot == NULL)
330 freeslot = ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000331 }
Neal Norwitz7e3ec042006-10-28 21:37:16 +0000332 assert(0); /* NOT REACHED */
333 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000334}
335
336/*
Fred Drake1bff34a2000-08-31 19:31:38 +0000337 * Hacked up version of lookdict which can assume keys are always strings;
Tim Petersd770ebd2006-06-01 15:50:44 +0000338 * this assumption allows testing for errors during PyObject_RichCompareBool()
339 * to be dropped; string-string comparisons never raise exceptions. This also
340 * means we don't need to go through PyObject_RichCompareBool(); we can always
341 * use _PyString_Eq() directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000342 *
Tim Petersd770ebd2006-06-01 15:50:44 +0000343 * This is valuable because dicts with only string keys are very common.
Fred Drake1bff34a2000-08-31 19:31:38 +0000344 */
345static dictentry *
346lookdict_string(dictobject *mp, PyObject *key, register long hash)
347{
Tim Petersd770ebd2006-06-01 15:50:44 +0000348 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000349 register size_t perturb;
Fred Drake1bff34a2000-08-31 19:31:38 +0000350 register dictentry *freeslot;
Tim Petersd770ebd2006-06-01 15:50:44 +0000351 register size_t mask = (size_t)mp->ma_mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000352 dictentry *ep0 = mp->ma_table;
353 register dictentry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000354
Tim Peters0ab085c2001-09-14 00:25:33 +0000355 /* Make sure this function doesn't have to handle non-string keys,
356 including subclasses of str; e.g., one reason to subclass
357 strings is to override __eq__, and for speed we don't cater to
358 that here. */
359 if (!PyString_CheckExact(key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000360#ifdef SHOW_CONVERSION_COUNTS
361 ++converted;
362#endif
363 mp->ma_lookup = lookdict;
364 return lookdict(mp, key, hash);
365 }
Tim Peters2f228e72001-05-13 00:19:31 +0000366 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000367 ep = &ep0[i];
368 if (ep->me_key == NULL || ep->me_key == key)
369 return ep;
370 if (ep->me_key == dummy)
371 freeslot = ep;
372 else {
Tim Petersd770ebd2006-06-01 15:50:44 +0000373 if (ep->me_hash == hash && _PyString_Eq(ep->me_key, key))
Fred Drake1bff34a2000-08-31 19:31:38 +0000374 return ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000375 freeslot = NULL;
376 }
Tim Peters15d49292001-05-27 07:39:22 +0000377
Tim Peters342c65e2001-05-13 06:43:53 +0000378 /* In the loop, me_key == dummy is by far (factor of 100s) the
379 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000380 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
381 i = (i << 2) + i + perturb + 1;
382 ep = &ep0[i & mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000383 if (ep->me_key == NULL)
384 return freeslot == NULL ? ep : freeslot;
385 if (ep->me_key == key
386 || (ep->me_hash == hash
387 && ep->me_key != dummy
Martin v. Löwiscd353062001-05-24 16:56:35 +0000388 && _PyString_Eq(ep->me_key, key)))
Fred Drake1bff34a2000-08-31 19:31:38 +0000389 return ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000390 if (ep->me_key == dummy && freeslot == NULL)
Tim Peters342c65e2001-05-13 06:43:53 +0000391 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000392 }
Neal Norwitz7e3ec042006-10-28 21:37:16 +0000393 assert(0); /* NOT REACHED */
394 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000395}
396
397/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000398Internal routine to insert a new item into the table.
399Used both by the internal resize routine and by the public insert routine.
400Eats a reference to key and one to value.
Tim Petersd770ebd2006-06-01 15:50:44 +0000401Returns -1 if an error occurred, or 0 on success.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000402*/
Armin Rigo35f6d362006-06-01 13:19:12 +0000403static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000404insertdict(register dictobject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000405{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000406 PyObject *old_value;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000407 register dictentry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000408 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
409
410 assert(mp->ma_lookup != NULL);
411 ep = mp->ma_lookup(mp, key, hash);
Armin Rigo35f6d362006-06-01 13:19:12 +0000412 if (ep == NULL) {
413 Py_DECREF(key);
414 Py_DECREF(value);
415 return -1;
416 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000417 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000418 old_value = ep->me_value;
419 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000420 Py_DECREF(old_value); /* which **CAN** re-enter */
421 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000422 }
423 else {
424 if (ep->me_key == NULL)
425 mp->ma_fill++;
Raymond Hettinger07ead172005-02-05 23:42:57 +0000426 else {
427 assert(ep->me_key == dummy);
428 Py_DECREF(dummy);
429 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000430 ep->me_key = key;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000431 ep->me_hash = (Py_ssize_t)hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000432 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000433 mp->ma_used++;
434 }
Armin Rigo35f6d362006-06-01 13:19:12 +0000435 return 0;
436}
437
438/*
439Internal routine used by dictresize() to insert an item which is
440known to be absent from the dict. This routine also assumes that
441the dict contains no deleted entries. Besides the performance benefit,
442using insertdict() in dictresize() is dangerous (SF bug #1456209).
Tim Petersd770ebd2006-06-01 15:50:44 +0000443Note that no refcounts are changed by this routine; if needed, the caller
444is responsible for incref'ing `key` and `value`.
Armin Rigo35f6d362006-06-01 13:19:12 +0000445*/
446static void
447insertdict_clean(register dictobject *mp, PyObject *key, long hash,
448 PyObject *value)
449{
Tim Petersd770ebd2006-06-01 15:50:44 +0000450 register size_t i;
Armin Rigo35f6d362006-06-01 13:19:12 +0000451 register size_t perturb;
Tim Petersd770ebd2006-06-01 15:50:44 +0000452 register size_t mask = (size_t)mp->ma_mask;
Armin Rigo35f6d362006-06-01 13:19:12 +0000453 dictentry *ep0 = mp->ma_table;
454 register dictentry *ep;
455
456 i = hash & mask;
457 ep = &ep0[i];
458 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
459 i = (i << 2) + i + perturb + 1;
460 ep = &ep0[i & mask];
461 }
Tim Petersd770ebd2006-06-01 15:50:44 +0000462 assert(ep->me_value == NULL);
Armin Rigo35f6d362006-06-01 13:19:12 +0000463 mp->ma_fill++;
464 ep->me_key = key;
465 ep->me_hash = (Py_ssize_t)hash;
466 ep->me_value = value;
467 mp->ma_used++;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000468}
469
470/*
471Restructure the table by allocating a new table and reinserting all
472items again. When entries have been deleted, the new table may
473actually be smaller than the old one.
474*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000475static int
Tim Peters9b10f7e2006-05-30 04:16:25 +0000476dictresize(dictobject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000477{
Tim Peters9b10f7e2006-05-30 04:16:25 +0000478 Py_ssize_t newsize;
Tim Peters0c6010b2001-05-23 23:33:57 +0000479 dictentry *oldtable, *newtable, *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000480 Py_ssize_t i;
Tim Peters0c6010b2001-05-23 23:33:57 +0000481 int is_oldtable_malloced;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000482 dictentry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000483
484 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000485
Tim Peterseb28ef22001-06-02 05:27:19 +0000486 /* Find the smallest table size > minused. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000487 for (newsize = PyDict_MINSIZE;
Tim Peterseb28ef22001-06-02 05:27:19 +0000488 newsize <= minused && newsize > 0;
489 newsize <<= 1)
490 ;
491 if (newsize <= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000492 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000493 return -1;
494 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000495
496 /* Get space for a new table. */
497 oldtable = mp->ma_table;
498 assert(oldtable != NULL);
499 is_oldtable_malloced = oldtable != mp->ma_smalltable;
500
Tim Peters6d6c1a32001-08-02 04:15:00 +0000501 if (newsize == PyDict_MINSIZE) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000502 /* A large table is shrinking, or we can't get any smaller. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000503 newtable = mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000504 if (newtable == oldtable) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000505 if (mp->ma_fill == mp->ma_used) {
506 /* No dummies, so no point doing anything. */
Tim Peters0c6010b2001-05-23 23:33:57 +0000507 return 0;
Tim Petersf8a548c2001-05-24 16:26:40 +0000508 }
509 /* We're not going to resize it, but rebuild the
510 table anyway to purge old dummy entries.
511 Subtle: This is *necessary* if fill==size,
512 as lookdict needs at least one virgin slot to
513 terminate failing searches. If fill < size, it's
514 merely desirable, as dummies slow searches. */
515 assert(mp->ma_fill > mp->ma_used);
Tim Peters0c6010b2001-05-23 23:33:57 +0000516 memcpy(small_copy, oldtable, sizeof(small_copy));
517 oldtable = small_copy;
518 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000519 }
520 else {
521 newtable = PyMem_NEW(dictentry, newsize);
522 if (newtable == NULL) {
523 PyErr_NoMemory();
524 return -1;
525 }
526 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000527
528 /* Make the dict empty, using the new table. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000529 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000530 mp->ma_table = newtable;
Tim Petersafb6ae82001-06-04 21:00:21 +0000531 mp->ma_mask = newsize - 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000532 memset(newtable, 0, sizeof(dictentry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000533 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000534 i = mp->ma_fill;
535 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000536
Tim Peters19283142001-05-17 22:25:34 +0000537 /* Copy the data over; this is refcount-neutral for active entries;
538 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000539 for (ep = oldtable; i > 0; ep++) {
540 if (ep->me_value != NULL) { /* active entry */
541 --i;
Armin Rigo35f6d362006-06-01 13:19:12 +0000542 insertdict_clean(mp, ep->me_key, (long)ep->me_hash,
543 ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000544 }
Tim Peters19283142001-05-17 22:25:34 +0000545 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000546 --i;
Tim Peters19283142001-05-17 22:25:34 +0000547 assert(ep->me_key == dummy);
548 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000549 }
Tim Peters19283142001-05-17 22:25:34 +0000550 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000551 }
552
Tim Peters0c6010b2001-05-23 23:33:57 +0000553 if (is_oldtable_malloced)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000554 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000555 return 0;
556}
557
Tim Petersd770ebd2006-06-01 15:50:44 +0000558/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
559 * that may occur (originally dicts supported only string keys, and exceptions
560 * weren't possible). So, while the original intent was that a NULL return
Andrew M. Kuchling0067b5f2006-08-04 20:37:43 +0000561 * meant the key wasn't present, in reality it can mean that, or that an error
Tim Petersd770ebd2006-06-01 15:50:44 +0000562 * (suppressed) occurred while computing the key's hash, or that some error
563 * (suppressed) occurred when comparing keys in the dict's internal probe
564 * sequence. A nasty example of the latter is when a Python-coded comparison
565 * function hits a stack-depth error, which can cause this to return NULL
566 * even if the key is present.
567 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000568PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000569PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000570{
571 long hash;
Fred Drake1bff34a2000-08-31 19:31:38 +0000572 dictobject *mp = (dictobject *)op;
Armin Rigo35f6d362006-06-01 13:19:12 +0000573 dictentry *ep;
574 PyThreadState *tstate;
Tim Petersd770ebd2006-06-01 15:50:44 +0000575 if (!PyDict_Check(op))
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000576 return NULL;
Tim Peters0ab085c2001-09-14 00:25:33 +0000577 if (!PyString_CheckExact(key) ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000578 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Guido van Rossumca756f21997-01-23 19:39:29 +0000579 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000580 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000581 if (hash == -1) {
582 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000583 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000584 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000585 }
Armin Rigo35f6d362006-06-01 13:19:12 +0000586
587 /* We can arrive here with a NULL tstate during initialization:
588 try running "python -Wi" for an example related to string
589 interning. Let's just hope that no exception occurs then... */
Armin Rigoacd0d6d2006-06-10 10:57:40 +0000590 tstate = _PyThreadState_Current;
Armin Rigo35f6d362006-06-01 13:19:12 +0000591 if (tstate != NULL && tstate->curexc_type != NULL) {
592 /* preserve the existing exception */
593 PyObject *err_type, *err_value, *err_tb;
594 PyErr_Fetch(&err_type, &err_value, &err_tb);
595 ep = (mp->ma_lookup)(mp, key, hash);
596 /* ignore errors */
597 PyErr_Restore(err_type, err_value, err_tb);
598 if (ep == NULL)
599 return NULL;
600 }
601 else {
602 ep = (mp->ma_lookup)(mp, key, hash);
603 if (ep == NULL) {
604 PyErr_Clear();
605 return NULL;
606 }
607 }
608 return ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000609}
610
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000611/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
Tim Peters60b29962006-01-01 01:19:23 +0000612 * dictionary if it's merely replacing the value for an existing key.
613 * This means that it's safe to loop over a dictionary with PyDict_Next()
614 * and occasionally replace a value -- but you can't insert new keys or
615 * remove them.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000616 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000617int
Tim Peters1f5871e2000-07-04 17:44:48 +0000618PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000619{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000620 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000621 register long hash;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000622 register Py_ssize_t n_used;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000623
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000624 if (!PyDict_Check(op)) {
625 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000626 return -1;
627 }
Neal Norwitz48808a12006-07-21 05:29:58 +0000628 assert(key);
629 assert(value);
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000630 mp = (dictobject *)op;
Tim Peters0ab085c2001-09-14 00:25:33 +0000631 if (PyString_CheckExact(key)) {
Guido van Rossum45ec02a2002-08-19 21:43:18 +0000632 hash = ((PyStringObject *)key)->ob_shash;
633 if (hash == -1)
634 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000635 }
Tim Peters1f7df352002-03-29 03:29:08 +0000636 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000637 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000638 if (hash == -1)
639 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000640 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000641 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000642 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000643 Py_INCREF(value);
644 Py_INCREF(key);
Armin Rigo35f6d362006-06-01 13:19:12 +0000645 if (insertdict(mp, key, hash, value) != 0)
646 return -1;
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000647 /* If we added a key, we can safely resize. Otherwise just return!
648 * If fill >= 2/3 size, adjust size. Normally, this doubles or
649 * quaduples the size, but it's also possible for the dict to shrink
Tim Peters60b29962006-01-01 01:19:23 +0000650 * (if ma_fill is much larger than ma_used, meaning a lot of dict
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000651 * keys have been * deleted).
Tim Peters60b29962006-01-01 01:19:23 +0000652 *
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000653 * Quadrupling the size improves average dictionary sparseness
654 * (reducing collisions) at the cost of some memory and iteration
655 * speed (which loops over every possible entry). It also halves
Neal Norwitz80af59c2006-05-30 04:19:21 +0000656 * the number of expensive resize operations in a growing dictionary.
Tim Peters60b29962006-01-01 01:19:23 +0000657 *
658 * Very large dictionaries (over 50K items) use doubling instead.
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000659 * This may help applications with severe memory constraints.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000660 */
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000661 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
662 return 0;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000663 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000664}
665
666int
Tim Peters1f5871e2000-07-04 17:44:48 +0000667PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000668{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000669 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000670 register long hash;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000671 register dictentry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000672 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000673
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000674 if (!PyDict_Check(op)) {
675 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000676 return -1;
677 }
Neal Norwitz48808a12006-07-21 05:29:58 +0000678 assert(key);
Tim Peters0ab085c2001-09-14 00:25:33 +0000679 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +0000680 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000681 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000682 if (hash == -1)
683 return -1;
684 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000685 mp = (dictobject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000686 ep = (mp->ma_lookup)(mp, key, hash);
Armin Rigo35f6d362006-06-01 13:19:12 +0000687 if (ep == NULL)
688 return -1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000689 if (ep->me_value == NULL) {
Georg Brandl5e9f94a2006-10-29 18:31:45 +0000690 set_key_error(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000691 return -1;
692 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000693 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000694 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000695 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000696 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000697 ep->me_value = NULL;
698 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000699 Py_DECREF(old_value);
700 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000701 return 0;
702}
703
Guido van Rossum25831651993-05-19 14:50:45 +0000704void
Tim Peters1f5871e2000-07-04 17:44:48 +0000705PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000706{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000707 dictobject *mp;
Tim Petersdea48ec2001-05-22 20:40:22 +0000708 dictentry *ep, *table;
709 int table_is_malloced;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000710 Py_ssize_t fill;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000711 dictentry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000712#ifdef Py_DEBUG
Tim Peters9b10f7e2006-05-30 04:16:25 +0000713 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +0000714#endif
715
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000716 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000717 return;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000718 mp = (dictobject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000719#ifdef Py_DEBUG
Tim Petersafb6ae82001-06-04 21:00:21 +0000720 n = mp->ma_mask + 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000721 i = 0;
722#endif
723
724 table = mp->ma_table;
725 assert(table != NULL);
726 table_is_malloced = table != mp->ma_smalltable;
727
728 /* This is delicate. During the process of clearing the dict,
729 * decrefs can cause the dict to mutate. To avoid fatal confusion
730 * (voice of experience), we have to make the dict empty before
731 * clearing the slots, and never refer to anything via mp->xxx while
732 * clearing.
733 */
734 fill = mp->ma_fill;
735 if (table_is_malloced)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000736 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000737
738 else if (fill > 0) {
739 /* It's a small table with something that needs to be cleared.
740 * Afraid the only safe way is to copy the dict entries into
741 * another small table first.
742 */
743 memcpy(small_copy, table, sizeof(small_copy));
744 table = small_copy;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000745 EMPTY_TO_MINSIZE(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000746 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000747 /* else it's a small table that's already empty */
748
749 /* Now we can finally clear things. If C had refcounts, we could
750 * assert that the refcount on table is 1 now, i.e. that this function
751 * has unique access to it, so decref side-effects can't alter it.
752 */
753 for (ep = table; fill > 0; ++ep) {
754#ifdef Py_DEBUG
755 assert(i < n);
756 ++i;
757#endif
758 if (ep->me_key) {
759 --fill;
760 Py_DECREF(ep->me_key);
761 Py_XDECREF(ep->me_value);
762 }
763#ifdef Py_DEBUG
764 else
765 assert(ep->me_value == NULL);
766#endif
767 }
768
769 if (table_is_malloced)
770 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000771}
772
Tim Peters080c88b2003-02-15 03:01:11 +0000773/*
774 * Iterate over a dict. Use like so:
775 *
Tim Peters9b10f7e2006-05-30 04:16:25 +0000776 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +0000777 * PyObject *key, *value;
778 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +0000779 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +0000780 * Refer to borrowed references in key and value.
781 * }
782 *
783 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +0000784 * mutates the dict. One exception: it is safe if the loop merely changes
785 * the values associated with the keys (but doesn't insert new keys or
786 * delete keys), via PyDict_SetItem().
787 */
Guido van Rossum25831651993-05-19 14:50:45 +0000788int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000789PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000790{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000791 register Py_ssize_t i;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000792 register Py_ssize_t mask;
Raymond Hettinger43442782004-03-17 21:55:03 +0000793 register dictentry *ep;
794
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000795 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000796 return 0;
Guido van Rossum25831651993-05-19 14:50:45 +0000797 i = *ppos;
798 if (i < 0)
799 return 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000800 ep = ((dictobject *)op)->ma_table;
801 mask = ((dictobject *)op)->ma_mask;
802 while (i <= mask && ep[i].me_value == NULL)
Guido van Rossum25831651993-05-19 14:50:45 +0000803 i++;
804 *ppos = i+1;
Raymond Hettinger43442782004-03-17 21:55:03 +0000805 if (i > mask)
Guido van Rossum25831651993-05-19 14:50:45 +0000806 return 0;
807 if (pkey)
Raymond Hettinger43442782004-03-17 21:55:03 +0000808 *pkey = ep[i].me_key;
Guido van Rossum25831651993-05-19 14:50:45 +0000809 if (pvalue)
Raymond Hettinger43442782004-03-17 21:55:03 +0000810 *pvalue = ep[i].me_value;
Guido van Rossum25831651993-05-19 14:50:45 +0000811 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000812}
813
Raymond Hettinger1bff7962007-02-19 03:04:45 +0000814/* Internal version of PyDict_Next that returns a hash value in addition to the key and value.*/
815int
816_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, long *phash)
817{
818 register Py_ssize_t i;
819 register Py_ssize_t mask;
820 register dictentry *ep;
821
822 if (!PyDict_Check(op))
823 return 0;
824 i = *ppos;
825 if (i < 0)
826 return 0;
827 ep = ((dictobject *)op)->ma_table;
828 mask = ((dictobject *)op)->ma_mask;
829 while (i <= mask && ep[i].me_value == NULL)
830 i++;
831 *ppos = i+1;
832 if (i > mask)
833 return 0;
834 *phash = (long)(ep[i].me_hash);
835 if (pkey)
836 *pkey = ep[i].me_key;
837 if (pvalue)
838 *pvalue = ep[i].me_value;
839 return 1;
840}
841
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000842/* Methods */
843
844static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000845dict_dealloc(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000846{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000847 register dictentry *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +0000848 Py_ssize_t fill = mp->ma_fill;
Guido van Rossumff413af2002-03-28 20:34:59 +0000849 PyObject_GC_UnTrack(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000850 Py_TRASHCAN_SAFE_BEGIN(mp)
Tim Petersdea48ec2001-05-22 20:40:22 +0000851 for (ep = mp->ma_table; fill > 0; ep++) {
852 if (ep->me_key) {
853 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000854 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000855 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000856 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000857 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000858 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000859 PyMem_DEL(mp->ma_table);
Raymond Hettinger43442782004-03-17 21:55:03 +0000860 if (num_free_dicts < MAXFREEDICTS && mp->ob_type == &PyDict_Type)
861 free_dicts[num_free_dicts++] = mp;
Tim Peters60b29962006-01-01 01:19:23 +0000862 else
Raymond Hettinger43442782004-03-17 21:55:03 +0000863 mp->ob_type->tp_free((PyObject *)mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000864 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000865}
866
867static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000868dict_print(register dictobject *mp, register FILE *fp, register int flags)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000869{
Tim Peters9b10f7e2006-05-30 04:16:25 +0000870 register Py_ssize_t i;
871 register Py_ssize_t any;
Tim Peters33f4a6a2006-05-30 05:23:59 +0000872 int status;
Guido van Rossum255443b1998-04-10 22:47:14 +0000873
Tim Peters33f4a6a2006-05-30 05:23:59 +0000874 status = Py_ReprEnter((PyObject*)mp);
875 if (status != 0) {
876 if (status < 0)
877 return status;
Guido van Rossum255443b1998-04-10 22:47:14 +0000878 fprintf(fp, "{...}");
879 return 0;
880 }
881
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000882 fprintf(fp, "{");
883 any = 0;
Tim Petersafb6ae82001-06-04 21:00:21 +0000884 for (i = 0; i <= mp->ma_mask; i++) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000885 dictentry *ep = mp->ma_table + i;
886 PyObject *pvalue = ep->me_value;
887 if (pvalue != NULL) {
888 /* Prevent PyObject_Repr from deleting value during
889 key format */
890 Py_INCREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000891 if (any++ > 0)
892 fprintf(fp, ", ");
Guido van Rossum255443b1998-04-10 22:47:14 +0000893 if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000894 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000895 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000896 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000897 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000898 fprintf(fp, ": ");
Tim Peters19b77cf2001-06-02 08:27:39 +0000899 if (PyObject_Print(pvalue, fp, 0) != 0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000900 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000901 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000902 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000903 }
Tim Peters23cf6be2001-06-02 08:02:56 +0000904 Py_DECREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000905 }
906 }
907 fprintf(fp, "}");
Guido van Rossum255443b1998-04-10 22:47:14 +0000908 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000909 return 0;
910}
911
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000912static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000913dict_repr(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000914{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000915 Py_ssize_t i;
Tim Petersa7259592001-06-16 05:11:17 +0000916 PyObject *s, *temp, *colon = NULL;
917 PyObject *pieces = NULL, *result = NULL;
918 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +0000919
Tim Petersa7259592001-06-16 05:11:17 +0000920 i = Py_ReprEnter((PyObject *)mp);
Guido van Rossum255443b1998-04-10 22:47:14 +0000921 if (i != 0) {
Tim Petersa7259592001-06-16 05:11:17 +0000922 return i > 0 ? PyString_FromString("{...}") : NULL;
Guido van Rossum255443b1998-04-10 22:47:14 +0000923 }
924
Tim Petersa7259592001-06-16 05:11:17 +0000925 if (mp->ma_used == 0) {
926 result = PyString_FromString("{}");
927 goto Done;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000928 }
Tim Petersa7259592001-06-16 05:11:17 +0000929
930 pieces = PyList_New(0);
931 if (pieces == NULL)
932 goto Done;
933
934 colon = PyString_FromString(": ");
935 if (colon == NULL)
936 goto Done;
937
938 /* Do repr() on each key+value pair, and insert ": " between them.
939 Note that repr may mutate the dict. */
Tim Petersc6057842001-06-16 07:52:53 +0000940 i = 0;
941 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Tim Petersa7259592001-06-16 05:11:17 +0000942 int status;
943 /* Prevent repr from deleting value during key format. */
944 Py_INCREF(value);
945 s = PyObject_Repr(key);
946 PyString_Concat(&s, colon);
947 PyString_ConcatAndDel(&s, PyObject_Repr(value));
948 Py_DECREF(value);
949 if (s == NULL)
950 goto Done;
951 status = PyList_Append(pieces, s);
952 Py_DECREF(s); /* append created a new ref */
953 if (status < 0)
954 goto Done;
955 }
956
957 /* Add "{}" decorations to the first and last items. */
958 assert(PyList_GET_SIZE(pieces) > 0);
959 s = PyString_FromString("{");
960 if (s == NULL)
961 goto Done;
962 temp = PyList_GET_ITEM(pieces, 0);
963 PyString_ConcatAndDel(&s, temp);
964 PyList_SET_ITEM(pieces, 0, s);
965 if (s == NULL)
966 goto Done;
967
968 s = PyString_FromString("}");
969 if (s == NULL)
970 goto Done;
971 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
972 PyString_ConcatAndDel(&temp, s);
973 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
974 if (temp == NULL)
975 goto Done;
976
977 /* Paste them all together with ", " between. */
978 s = PyString_FromString(", ");
979 if (s == NULL)
980 goto Done;
981 result = _PyString_Join(s, pieces);
Tim Peters0ab085c2001-09-14 00:25:33 +0000982 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +0000983
984Done:
985 Py_XDECREF(pieces);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000986 Py_XDECREF(colon);
Tim Petersa7259592001-06-16 05:11:17 +0000987 Py_ReprLeave((PyObject *)mp);
988 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000989}
990
Martin v. Löwis18e16552006-02-15 17:27:45 +0000991static Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +0000992dict_length(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000993{
994 return mp->ma_used;
995}
996
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000997static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000998dict_subscript(dictobject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000999{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001000 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001001 long hash;
Armin Rigo35f6d362006-06-01 13:19:12 +00001002 dictentry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +00001003 assert(mp->ma_table != NULL);
Tim Peters0ab085c2001-09-14 00:25:33 +00001004 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001005 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001006 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001007 if (hash == -1)
1008 return NULL;
1009 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001010 ep = (mp->ma_lookup)(mp, key, hash);
1011 if (ep == NULL)
1012 return NULL;
1013 v = ep->me_value;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001014 if (v == NULL) {
1015 if (!PyDict_CheckExact(mp)) {
1016 /* Look up __missing__ method if we're a subclass. */
Guido van Rossum4b92a822006-02-25 23:32:30 +00001017 PyObject *missing;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001018 static PyObject *missing_str = NULL;
1019 if (missing_str == NULL)
Tim Peters9b10f7e2006-05-30 04:16:25 +00001020 missing_str =
Guido van Rossum1968ad32006-02-25 22:38:04 +00001021 PyString_InternFromString("__missing__");
Guido van Rossum4b92a822006-02-25 23:32:30 +00001022 missing = _PyType_Lookup(mp->ob_type, missing_str);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001023 if (missing != NULL)
1024 return PyObject_CallFunctionObjArgs(missing,
1025 (PyObject *)mp, key, NULL);
1026 }
Georg Brandl5e9f94a2006-10-29 18:31:45 +00001027 set_key_error(key);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001028 return NULL;
1029 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001030 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001031 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001032 return v;
1033}
1034
1035static int
Tim Peters1f5871e2000-07-04 17:44:48 +00001036dict_ass_sub(dictobject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001037{
1038 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001039 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001040 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001041 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001042}
1043
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001044static PyMappingMethods dict_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001045 (lenfunc)dict_length, /*mp_length*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001046 (binaryfunc)dict_subscript, /*mp_subscript*/
1047 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001048};
1049
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001050static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001051dict_keys(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001052{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001053 register PyObject *v;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001054 register Py_ssize_t i, j;
Raymond Hettinger43442782004-03-17 21:55:03 +00001055 dictentry *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001056 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001057
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001058 again:
1059 n = mp->ma_used;
1060 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001061 if (v == NULL)
1062 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001063 if (n != mp->ma_used) {
1064 /* Durnit. The allocations caused the dict to resize.
1065 * Just start over, this shouldn't normally happen.
1066 */
1067 Py_DECREF(v);
1068 goto again;
1069 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001070 ep = mp->ma_table;
1071 mask = mp->ma_mask;
1072 for (i = 0, j = 0; i <= mask; i++) {
1073 if (ep[i].me_value != NULL) {
1074 PyObject *key = ep[i].me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001075 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001076 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001077 j++;
1078 }
1079 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001080 assert(j == n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001081 return v;
1082}
1083
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001084static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001085dict_values(register dictobject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001086{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001087 register PyObject *v;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001088 register Py_ssize_t i, j;
Raymond Hettinger43442782004-03-17 21:55:03 +00001089 dictentry *ep;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001090 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001091
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001092 again:
1093 n = mp->ma_used;
1094 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001095 if (v == NULL)
1096 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001097 if (n != mp->ma_used) {
1098 /* Durnit. The allocations caused the dict to resize.
1099 * Just start over, this shouldn't normally happen.
1100 */
1101 Py_DECREF(v);
1102 goto again;
1103 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001104 ep = mp->ma_table;
1105 mask = mp->ma_mask;
1106 for (i = 0, j = 0; i <= mask; i++) {
1107 if (ep[i].me_value != NULL) {
1108 PyObject *value = ep[i].me_value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001109 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001110 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001111 j++;
1112 }
1113 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001114 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001115 return v;
1116}
1117
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001118static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001119dict_items(register dictobject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001120{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001121 register PyObject *v;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001122 register Py_ssize_t i, j, n;
1123 Py_ssize_t mask;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001124 PyObject *item, *key, *value;
Raymond Hettinger43442782004-03-17 21:55:03 +00001125 dictentry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001126
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001127 /* Preallocate the list of tuples, to avoid allocations during
1128 * the loop over the items, which could trigger GC, which
1129 * could resize the dict. :-(
1130 */
1131 again:
1132 n = mp->ma_used;
1133 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001134 if (v == NULL)
1135 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001136 for (i = 0; i < n; i++) {
1137 item = PyTuple_New(2);
1138 if (item == NULL) {
1139 Py_DECREF(v);
1140 return NULL;
1141 }
1142 PyList_SET_ITEM(v, i, item);
1143 }
1144 if (n != mp->ma_used) {
1145 /* Durnit. The allocations caused the dict to resize.
1146 * Just start over, this shouldn't normally happen.
1147 */
1148 Py_DECREF(v);
1149 goto again;
1150 }
1151 /* Nothing we do below makes any function calls. */
Raymond Hettinger43442782004-03-17 21:55:03 +00001152 ep = mp->ma_table;
1153 mask = mp->ma_mask;
1154 for (i = 0, j = 0; i <= mask; i++) {
Raymond Hettinger06905122004-03-19 10:30:00 +00001155 if ((value=ep[i].me_value) != NULL) {
Raymond Hettinger43442782004-03-17 21:55:03 +00001156 key = ep[i].me_key;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001157 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001158 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001159 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001160 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001161 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001162 j++;
1163 }
1164 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001165 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001166 return v;
1167}
1168
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001169static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001170dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001171{
1172 PyObject *seq;
1173 PyObject *value = Py_None;
1174 PyObject *it; /* iter(seq) */
1175 PyObject *key;
1176 PyObject *d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001177 int status;
1178
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001179 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001180 return NULL;
1181
1182 d = PyObject_CallObject(cls, NULL);
1183 if (d == NULL)
1184 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001185
Raymond Hettingerf94e89c2007-03-20 21:45:04 +00001186 if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
1187 dictobject *mp = (dictobject *)d;
1188 Py_ssize_t pos = 0;
1189 PyObject *key;
1190 long hash;
1191
1192 if (dictresize(mp, PySet_GET_SIZE(seq)))
1193 return NULL;
1194
1195 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
1196 Py_INCREF(key);
Raymond Hettinger7ed0a652007-03-21 20:36:45 +00001197 Py_INCREF(value);
1198 if (insertdict(mp, key, hash, value))
Raymond Hettingerf94e89c2007-03-20 21:45:04 +00001199 return NULL;
1200 }
1201 return d;
1202 }
1203
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001204 it = PyObject_GetIter(seq);
1205 if (it == NULL){
1206 Py_DECREF(d);
1207 return NULL;
1208 }
1209
1210 for (;;) {
1211 key = PyIter_Next(it);
1212 if (key == NULL) {
1213 if (PyErr_Occurred())
1214 goto Fail;
1215 break;
1216 }
Raymond Hettingere03e5b12002-12-07 08:10:51 +00001217 status = PyObject_SetItem(d, key, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001218 Py_DECREF(key);
1219 if (status < 0)
1220 goto Fail;
1221 }
1222
1223 Py_DECREF(it);
1224 return d;
1225
1226Fail:
1227 Py_DECREF(it);
1228 Py_DECREF(d);
1229 return NULL;
1230}
1231
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001232static int
1233dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001234{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001235 PyObject *arg = NULL;
1236 int result = 0;
1237
1238 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1239 result = -1;
1240
1241 else if (arg != NULL) {
1242 if (PyObject_HasAttrString(arg, "keys"))
1243 result = PyDict_Merge(self, arg, 1);
1244 else
1245 result = PyDict_MergeFromSeq2(self, arg, 1);
1246 }
1247 if (result == 0 && kwds != NULL)
1248 result = PyDict_Merge(self, kwds, 1);
1249 return result;
1250}
1251
1252static PyObject *
1253dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1254{
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001255 if (dict_update_common(self, args, kwds, "update") != -1)
1256 Py_RETURN_NONE;
1257 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001258}
1259
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001260/* Update unconditionally replaces existing items.
1261 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001262 otherwise it leaves existing items unchanged.
1263
1264 PyDict_{Update,Merge} update/merge from a mapping object.
1265
Tim Petersf582b822001-12-11 18:51:08 +00001266 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001267 producing iterable objects of length 2.
1268*/
1269
Tim Petersf582b822001-12-11 18:51:08 +00001270int
Tim Peters1fc240e2001-10-26 05:06:50 +00001271PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1272{
1273 PyObject *it; /* iter(seq2) */
Tim Peters9b10f7e2006-05-30 04:16:25 +00001274 Py_ssize_t i; /* index into seq2 of current element */
Tim Peters1fc240e2001-10-26 05:06:50 +00001275 PyObject *item; /* seq2[i] */
1276 PyObject *fast; /* item as a 2-tuple or 2-list */
1277
1278 assert(d != NULL);
1279 assert(PyDict_Check(d));
1280 assert(seq2 != NULL);
1281
1282 it = PyObject_GetIter(seq2);
1283 if (it == NULL)
1284 return -1;
1285
1286 for (i = 0; ; ++i) {
1287 PyObject *key, *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001288 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001289
1290 fast = NULL;
1291 item = PyIter_Next(it);
1292 if (item == NULL) {
1293 if (PyErr_Occurred())
1294 goto Fail;
1295 break;
1296 }
1297
1298 /* Convert item to sequence, and verify length 2. */
1299 fast = PySequence_Fast(item, "");
1300 if (fast == NULL) {
1301 if (PyErr_ExceptionMatches(PyExc_TypeError))
1302 PyErr_Format(PyExc_TypeError,
1303 "cannot convert dictionary update "
Neal Norwitzd3881b02006-05-30 04:25:05 +00001304 "sequence element #%zd to a sequence",
Tim Peters1fc240e2001-10-26 05:06:50 +00001305 i);
1306 goto Fail;
1307 }
1308 n = PySequence_Fast_GET_SIZE(fast);
1309 if (n != 2) {
1310 PyErr_Format(PyExc_ValueError,
Neal Norwitzd3881b02006-05-30 04:25:05 +00001311 "dictionary update sequence element #%zd "
Martin v. Löwis2c95cc62006-02-16 06:54:25 +00001312 "has length %zd; 2 is required",
Martin v. Löwise0e89f72006-02-16 06:59:22 +00001313 i, n);
Tim Peters1fc240e2001-10-26 05:06:50 +00001314 goto Fail;
1315 }
1316
1317 /* Update/merge with this (key, value) pair. */
1318 key = PySequence_Fast_GET_ITEM(fast, 0);
1319 value = PySequence_Fast_GET_ITEM(fast, 1);
1320 if (override || PyDict_GetItem(d, key) == NULL) {
1321 int status = PyDict_SetItem(d, key, value);
1322 if (status < 0)
1323 goto Fail;
1324 }
1325 Py_DECREF(fast);
1326 Py_DECREF(item);
1327 }
1328
1329 i = 0;
1330 goto Return;
1331Fail:
1332 Py_XDECREF(item);
1333 Py_XDECREF(fast);
1334 i = -1;
1335Return:
1336 Py_DECREF(it);
Neal Norwitzd3881b02006-05-30 04:25:05 +00001337 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001338}
1339
Tim Peters6d6c1a32001-08-02 04:15:00 +00001340int
1341PyDict_Update(PyObject *a, PyObject *b)
1342{
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001343 return PyDict_Merge(a, b, 1);
1344}
1345
1346int
1347PyDict_Merge(PyObject *a, PyObject *b, int override)
1348{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001349 register PyDictObject *mp, *other;
Tim Peters9b10f7e2006-05-30 04:16:25 +00001350 register Py_ssize_t i;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001351 dictentry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001352
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001353 /* We accept for the argument either a concrete dictionary object,
1354 * or an abstract "mapping" object. For the former, we can do
1355 * things quite efficiently. For the latter, we only require that
1356 * PyMapping_Keys() and PyObject_GetItem() be supported.
1357 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001358 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1359 PyErr_BadInternalCall();
1360 return -1;
1361 }
1362 mp = (dictobject*)a;
Neal Norwitze6e383f2007-04-16 06:59:13 +00001363 if (PyDict_Check(b)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001364 other = (dictobject*)b;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001365 if (other == mp || other->ma_used == 0)
1366 /* a.update(a) or a.update({}); nothing to do */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001367 return 0;
Raymond Hettinger186e7392005-05-14 18:08:25 +00001368 if (mp->ma_used == 0)
1369 /* Since the target dict is empty, PyDict_GetItem()
1370 * always returns NULL. Setting override to 1
1371 * skips the unnecessary test.
1372 */
1373 override = 1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001374 /* Do one big resize at the start, rather than
1375 * incrementally resizing as we insert new items. Expect
1376 * that there will be no (or few) overlapping keys.
1377 */
1378 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
Raymond Hettingerc8d22902003-05-07 00:49:40 +00001379 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001380 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001381 }
1382 for (i = 0; i <= other->ma_mask; i++) {
1383 entry = &other->ma_table[i];
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001384 if (entry->me_value != NULL &&
1385 (override ||
1386 PyDict_GetItem(a, entry->me_key) == NULL)) {
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001387 Py_INCREF(entry->me_key);
1388 Py_INCREF(entry->me_value);
Armin Rigo35f6d362006-06-01 13:19:12 +00001389 if (insertdict(mp, entry->me_key,
1390 (long)entry->me_hash,
1391 entry->me_value) != 0)
1392 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001393 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001394 }
1395 }
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001396 else {
1397 /* Do it the generic, slower way */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001398 PyObject *keys = PyMapping_Keys(b);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001399 PyObject *iter;
1400 PyObject *key, *value;
1401 int status;
1402
1403 if (keys == NULL)
1404 /* Docstring says this is equivalent to E.keys() so
1405 * if E doesn't have a .keys() method we want
1406 * AttributeError to percolate up. Might as well
1407 * do the same for any other error.
1408 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001409 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001410
1411 iter = PyObject_GetIter(keys);
1412 Py_DECREF(keys);
1413 if (iter == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001414 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001415
1416 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001417 if (!override && PyDict_GetItem(a, key) != NULL) {
1418 Py_DECREF(key);
1419 continue;
1420 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001421 value = PyObject_GetItem(b, key);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001422 if (value == NULL) {
1423 Py_DECREF(iter);
1424 Py_DECREF(key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001425 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001426 }
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001427 status = PyDict_SetItem(a, key, value);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001428 Py_DECREF(key);
1429 Py_DECREF(value);
1430 if (status < 0) {
1431 Py_DECREF(iter);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001432 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001433 }
1434 }
1435 Py_DECREF(iter);
1436 if (PyErr_Occurred())
1437 /* Iterator completed, via error */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001438 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001439 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001440 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001441}
1442
1443static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001444dict_copy(register dictobject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001445{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001446 return PyDict_Copy((PyObject*)mp);
1447}
1448
1449PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001450PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001451{
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001452 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001453
1454 if (o == NULL || !PyDict_Check(o)) {
1455 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001456 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001457 }
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001458 copy = PyDict_New();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001459 if (copy == NULL)
1460 return NULL;
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001461 if (PyDict_Merge(copy, o, 1) == 0)
1462 return copy;
1463 Py_DECREF(copy);
Raymond Hettinger1356f782005-04-15 15:58:42 +00001464 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001465}
1466
Martin v. Löwis18e16552006-02-15 17:27:45 +00001467Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001468PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001469{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001470 if (mp == NULL || !PyDict_Check(mp)) {
1471 PyErr_BadInternalCall();
Jeremy Hylton7083bb72004-02-17 20:10:11 +00001472 return -1;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001473 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001474 return ((dictobject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001475}
1476
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001477PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001478PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001479{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001480 if (mp == NULL || !PyDict_Check(mp)) {
1481 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001482 return NULL;
1483 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001484 return dict_keys((dictobject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001485}
1486
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001487PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001488PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001489{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001490 if (mp == NULL || !PyDict_Check(mp)) {
1491 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001492 return NULL;
1493 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001494 return dict_values((dictobject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001495}
1496
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001497PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001498PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001499{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001500 if (mp == NULL || !PyDict_Check(mp)) {
1501 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001502 return NULL;
1503 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001504 return dict_items((dictobject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001505}
1506
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001507/* Subroutine which returns the smallest key in a for which b's value
1508 is different or absent. The value is returned too, through the
Tim Peters95bf9392001-05-10 08:32:44 +00001509 pval argument. Both are NULL if no key in a is found for which b's status
1510 differs. The refcounts on (and only on) non-NULL *pval and function return
1511 values must be decremented by the caller (characterize() increments them
1512 to ensure that mutating comparison and PyDict_GetItem calls can't delete
1513 them before the caller is done looking at them). */
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001514
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001515static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001516characterize(dictobject *a, dictobject *b, PyObject **pval)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001517{
Tim Peters95bf9392001-05-10 08:32:44 +00001518 PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
1519 PyObject *aval = NULL; /* a[akey] */
Tim Peters9b10f7e2006-05-30 04:16:25 +00001520 Py_ssize_t i;
1521 int cmp;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001522
Tim Petersafb6ae82001-06-04 21:00:21 +00001523 for (i = 0; i <= a->ma_mask; i++) {
Tim Peters95bf9392001-05-10 08:32:44 +00001524 PyObject *thiskey, *thisaval, *thisbval;
1525 if (a->ma_table[i].me_value == NULL)
1526 continue;
1527 thiskey = a->ma_table[i].me_key;
1528 Py_INCREF(thiskey); /* keep alive across compares */
1529 if (akey != NULL) {
1530 cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
1531 if (cmp < 0) {
1532 Py_DECREF(thiskey);
1533 goto Fail;
1534 }
1535 if (cmp > 0 ||
Tim Petersafb6ae82001-06-04 21:00:21 +00001536 i > a->ma_mask ||
Tim Peters95bf9392001-05-10 08:32:44 +00001537 a->ma_table[i].me_value == NULL)
1538 {
1539 /* Not the *smallest* a key; or maybe it is
1540 * but the compare shrunk the dict so we can't
1541 * find its associated value anymore; or
1542 * maybe it is but the compare deleted the
1543 * a[thiskey] entry.
Neal Norwitzd3881b02006-05-30 04:25:05 +00001544 */
Tim Peters95bf9392001-05-10 08:32:44 +00001545 Py_DECREF(thiskey);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001546 continue;
Guido van Rossumb9324202001-01-18 00:39:02 +00001547 }
Tim Peters95bf9392001-05-10 08:32:44 +00001548 }
1549
1550 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
1551 thisaval = a->ma_table[i].me_value;
1552 assert(thisaval);
1553 Py_INCREF(thisaval); /* keep alive */
1554 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
1555 if (thisbval == NULL)
1556 cmp = 0;
1557 else {
1558 /* both dicts have thiskey: same values? */
1559 cmp = PyObject_RichCompareBool(
1560 thisaval, thisbval, Py_EQ);
1561 if (cmp < 0) {
1562 Py_DECREF(thiskey);
1563 Py_DECREF(thisaval);
1564 goto Fail;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001565 }
1566 }
Tim Peters95bf9392001-05-10 08:32:44 +00001567 if (cmp == 0) {
1568 /* New winner. */
1569 Py_XDECREF(akey);
1570 Py_XDECREF(aval);
1571 akey = thiskey;
1572 aval = thisaval;
1573 }
1574 else {
1575 Py_DECREF(thiskey);
1576 Py_DECREF(thisaval);
1577 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001578 }
Tim Peters95bf9392001-05-10 08:32:44 +00001579 *pval = aval;
1580 return akey;
1581
1582Fail:
1583 Py_XDECREF(akey);
1584 Py_XDECREF(aval);
1585 *pval = NULL;
1586 return NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001587}
1588
1589static int
Tim Peters1f5871e2000-07-04 17:44:48 +00001590dict_compare(dictobject *a, dictobject *b)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001591{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001592 PyObject *adiff, *bdiff, *aval, *bval;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001593 int res;
1594
1595 /* Compare lengths first */
1596 if (a->ma_used < b->ma_used)
1597 return -1; /* a is shorter */
1598 else if (a->ma_used > b->ma_used)
1599 return 1; /* b is shorter */
Tim Peters95bf9392001-05-10 08:32:44 +00001600
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001601 /* Same length -- check all keys */
Tim Peters95bf9392001-05-10 08:32:44 +00001602 bdiff = bval = NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001603 adiff = characterize(a, b, &aval);
Tim Peters95bf9392001-05-10 08:32:44 +00001604 if (adiff == NULL) {
1605 assert(!aval);
Tim Peters3918fb22001-05-10 18:58:31 +00001606 /* Either an error, or a is a subset with the same length so
Tim Peters95bf9392001-05-10 08:32:44 +00001607 * must be equal.
1608 */
1609 res = PyErr_Occurred() ? -1 : 0;
1610 goto Finished;
1611 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001612 bdiff = characterize(b, a, &bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001613 if (bdiff == NULL && PyErr_Occurred()) {
1614 assert(!bval);
1615 res = -1;
1616 goto Finished;
1617 }
1618 res = 0;
1619 if (bdiff) {
1620 /* bdiff == NULL "should be" impossible now, but perhaps
1621 * the last comparison done by the characterize() on a had
1622 * the side effect of making the dicts equal!
1623 */
1624 res = PyObject_Compare(adiff, bdiff);
1625 }
1626 if (res == 0 && bval != NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001627 res = PyObject_Compare(aval, bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001628
1629Finished:
1630 Py_XDECREF(adiff);
1631 Py_XDECREF(bdiff);
1632 Py_XDECREF(aval);
1633 Py_XDECREF(bval);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001634 return res;
1635}
1636
Tim Peterse63415e2001-05-08 04:38:29 +00001637/* Return 1 if dicts equal, 0 if not, -1 if error.
1638 * Gets out as soon as any difference is detected.
1639 * Uses only Py_EQ comparison.
1640 */
1641static int
1642dict_equal(dictobject *a, dictobject *b)
1643{
Tim Peters9b10f7e2006-05-30 04:16:25 +00001644 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00001645
1646 if (a->ma_used != b->ma_used)
1647 /* can't be equal if # of entries differ */
1648 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001649
Tim Peterse63415e2001-05-08 04:38:29 +00001650 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Tim Petersafb6ae82001-06-04 21:00:21 +00001651 for (i = 0; i <= a->ma_mask; i++) {
Tim Peterse63415e2001-05-08 04:38:29 +00001652 PyObject *aval = a->ma_table[i].me_value;
1653 if (aval != NULL) {
1654 int cmp;
1655 PyObject *bval;
1656 PyObject *key = a->ma_table[i].me_key;
1657 /* temporarily bump aval's refcount to ensure it stays
1658 alive until we're done with it */
1659 Py_INCREF(aval);
Neal Norwitzd3da7d32006-09-05 01:54:06 +00001660 /* ditto for key */
1661 Py_INCREF(key);
Tim Peterse63415e2001-05-08 04:38:29 +00001662 bval = PyDict_GetItem((PyObject *)b, key);
Neal Norwitzd3da7d32006-09-05 01:54:06 +00001663 Py_DECREF(key);
Tim Peterse63415e2001-05-08 04:38:29 +00001664 if (bval == NULL) {
1665 Py_DECREF(aval);
1666 return 0;
1667 }
1668 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1669 Py_DECREF(aval);
1670 if (cmp <= 0) /* error or not equal */
1671 return cmp;
1672 }
1673 }
1674 return 1;
1675 }
1676
1677static PyObject *
1678dict_richcompare(PyObject *v, PyObject *w, int op)
1679{
1680 int cmp;
1681 PyObject *res;
1682
1683 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1684 res = Py_NotImplemented;
1685 }
1686 else if (op == Py_EQ || op == Py_NE) {
1687 cmp = dict_equal((dictobject *)v, (dictobject *)w);
1688 if (cmp < 0)
1689 return NULL;
1690 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1691 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001692 else
1693 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001694 Py_INCREF(res);
1695 return res;
1696 }
1697
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001698static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001699dict_has_key(register dictobject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001700{
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001701 long hash;
Armin Rigo35f6d362006-06-01 13:19:12 +00001702 dictentry *ep;
Tim Petersd770ebd2006-06-01 15:50:44 +00001703
Tim Peters0ab085c2001-09-14 00:25:33 +00001704 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001705 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001706 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001707 if (hash == -1)
1708 return NULL;
1709 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001710 ep = (mp->ma_lookup)(mp, key, hash);
1711 if (ep == NULL)
1712 return NULL;
Tim Petersd770ebd2006-06-01 15:50:44 +00001713 return PyBool_FromLong(ep->me_value != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001714}
1715
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001716static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001717dict_get(register dictobject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001718{
1719 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001720 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001721 PyObject *val = NULL;
1722 long hash;
Armin Rigo35f6d362006-06-01 13:19:12 +00001723 dictentry *ep;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001724
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001725 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001726 return NULL;
1727
Tim Peters0ab085c2001-09-14 00:25:33 +00001728 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001729 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Barry Warsawc38c5da1997-10-06 17:49:20 +00001730 hash = PyObject_Hash(key);
1731 if (hash == -1)
1732 return NULL;
1733 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001734 ep = (mp->ma_lookup)(mp, key, hash);
1735 if (ep == NULL)
1736 return NULL;
1737 val = ep->me_value;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001738 if (val == NULL)
1739 val = failobj;
1740 Py_INCREF(val);
1741 return val;
1742}
1743
1744
1745static PyObject *
Guido van Rossum164452c2000-08-08 16:12:54 +00001746dict_setdefault(register dictobject *mp, PyObject *args)
1747{
1748 PyObject *key;
1749 PyObject *failobj = Py_None;
1750 PyObject *val = NULL;
1751 long hash;
Armin Rigo35f6d362006-06-01 13:19:12 +00001752 dictentry *ep;
Guido van Rossum164452c2000-08-08 16:12:54 +00001753
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001754 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
Guido van Rossum164452c2000-08-08 16:12:54 +00001755 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001756
Tim Peters0ab085c2001-09-14 00:25:33 +00001757 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001758 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum164452c2000-08-08 16:12:54 +00001759 hash = PyObject_Hash(key);
1760 if (hash == -1)
1761 return NULL;
1762 }
Armin Rigo35f6d362006-06-01 13:19:12 +00001763 ep = (mp->ma_lookup)(mp, key, hash);
1764 if (ep == NULL)
1765 return NULL;
1766 val = ep->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001767 if (val == NULL) {
1768 val = failobj;
1769 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1770 val = NULL;
1771 }
1772 Py_XINCREF(val);
1773 return val;
1774}
1775
1776
1777static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001778dict_clear(register dictobject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001779{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001780 PyDict_Clear((PyObject *)mp);
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001781 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001782}
1783
Guido van Rossumba6ab842000-12-12 22:02:18 +00001784static PyObject *
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001785dict_pop(dictobject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00001786{
1787 long hash;
1788 dictentry *ep;
1789 PyObject *old_value, *old_key;
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001790 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001791
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001792 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1793 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001794 if (mp->ma_used == 0) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001795 if (deflt) {
1796 Py_INCREF(deflt);
1797 return deflt;
1798 }
Guido van Rossume027d982002-04-12 15:11:59 +00001799 PyErr_SetString(PyExc_KeyError,
1800 "pop(): dictionary is empty");
1801 return NULL;
1802 }
1803 if (!PyString_CheckExact(key) ||
1804 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1805 hash = PyObject_Hash(key);
1806 if (hash == -1)
1807 return NULL;
1808 }
1809 ep = (mp->ma_lookup)(mp, key, hash);
Armin Rigo35f6d362006-06-01 13:19:12 +00001810 if (ep == NULL)
1811 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001812 if (ep->me_value == NULL) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001813 if (deflt) {
1814 Py_INCREF(deflt);
1815 return deflt;
1816 }
Georg Brandl5e9f94a2006-10-29 18:31:45 +00001817 set_key_error(key);
Guido van Rossume027d982002-04-12 15:11:59 +00001818 return NULL;
1819 }
1820 old_key = ep->me_key;
1821 Py_INCREF(dummy);
1822 ep->me_key = dummy;
1823 old_value = ep->me_value;
1824 ep->me_value = NULL;
1825 mp->ma_used--;
1826 Py_DECREF(old_key);
1827 return old_value;
1828}
1829
1830static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001831dict_popitem(dictobject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001832{
Tim Peters9b10f7e2006-05-30 04:16:25 +00001833 Py_ssize_t i = 0;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001834 dictentry *ep;
1835 PyObject *res;
1836
Tim Petersf4b33f62001-06-02 05:42:29 +00001837 /* Allocate the result tuple before checking the size. Believe it
1838 * or not, this allocation could trigger a garbage collection which
1839 * could empty the dict, so if we checked the size first and that
1840 * happened, the result would be an infinite loop (searching for an
1841 * entry that no longer exists). Note that the usual popitem()
1842 * idiom is "while d: k, v = d.popitem()". so needing to throw the
Tim Peters9b10f7e2006-05-30 04:16:25 +00001843 * tuple away if the dict *is* empty isn't a significant
Tim Petersf4b33f62001-06-02 05:42:29 +00001844 * inefficiency -- possible, but unlikely in practice.
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001845 */
1846 res = PyTuple_New(2);
1847 if (res == NULL)
1848 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001849 if (mp->ma_used == 0) {
1850 Py_DECREF(res);
1851 PyErr_SetString(PyExc_KeyError,
1852 "popitem(): dictionary is empty");
1853 return NULL;
1854 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001855 /* Set ep to "the first" dict entry with a value. We abuse the hash
1856 * field of slot 0 to hold a search finger:
1857 * If slot 0 has a value, use slot 0.
1858 * Else slot 0 is being used to hold a search finger,
Neal Norwitzd3881b02006-05-30 04:25:05 +00001859 * and we use its hash value as the first index to look.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001860 */
1861 ep = &mp->ma_table[0];
1862 if (ep->me_value == NULL) {
Tim Peters9b10f7e2006-05-30 04:16:25 +00001863 i = ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001864 /* The hash field may be a real hash value, or it may be a
1865 * legit search finger, or it may be a once-legit search
1866 * finger that's out of bounds now because it wrapped around
1867 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001868 */
Tim Petersafb6ae82001-06-04 21:00:21 +00001869 if (i > mp->ma_mask || i < 1)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001870 i = 1; /* skip slot 0 */
1871 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1872 i++;
Tim Petersafb6ae82001-06-04 21:00:21 +00001873 if (i > mp->ma_mask)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001874 i = 1;
1875 }
1876 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001877 PyTuple_SET_ITEM(res, 0, ep->me_key);
1878 PyTuple_SET_ITEM(res, 1, ep->me_value);
1879 Py_INCREF(dummy);
1880 ep->me_key = dummy;
1881 ep->me_value = NULL;
1882 mp->ma_used--;
1883 assert(mp->ma_table[0].me_value == NULL);
1884 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001885 return res;
1886}
1887
Jeremy Hylton8caad492000-06-23 14:18:11 +00001888static int
1889dict_traverse(PyObject *op, visitproc visit, void *arg)
1890{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001891 Py_ssize_t i = 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001892 PyObject *pk;
1893 PyObject *pv;
1894
1895 while (PyDict_Next(op, &i, &pk, &pv)) {
Thomas Woutersc6e55062006-04-15 21:47:09 +00001896 Py_VISIT(pk);
1897 Py_VISIT(pv);
Jeremy Hylton8caad492000-06-23 14:18:11 +00001898 }
1899 return 0;
1900}
1901
1902static int
1903dict_tp_clear(PyObject *op)
1904{
1905 PyDict_Clear(op);
1906 return 0;
1907}
1908
Tim Petersf7f88b12000-12-13 23:18:45 +00001909
Raymond Hettinger019a1482004-03-18 02:41:19 +00001910extern PyTypeObject PyDictIterKey_Type; /* Forward */
1911extern PyTypeObject PyDictIterValue_Type; /* Forward */
1912extern PyTypeObject PyDictIterItem_Type; /* Forward */
1913static PyObject *dictiter_new(dictobject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001914
1915static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001916dict_iterkeys(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001917{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001918 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001919}
1920
1921static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001922dict_itervalues(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001923{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001924 return dictiter_new(dict, &PyDictIterValue_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001925}
1926
1927static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001928dict_iteritems(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001929{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001930 return dictiter_new(dict, &PyDictIterItem_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001931}
1932
1933
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001934PyDoc_STRVAR(has_key__doc__,
Raymond Hettinger574aa322003-09-01 22:12:08 +00001935"D.has_key(k) -> True if D has a key k, else False");
Tim Petersf7f88b12000-12-13 23:18:45 +00001936
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001937PyDoc_STRVAR(contains__doc__,
1938"D.__contains__(k) -> True if D has a key k, else False");
1939
1940PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
1941
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001942PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001943"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001944
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001945PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001946"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 +00001947
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001948PyDoc_STRVAR(pop__doc__,
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001949"D.pop(k[,d]) -> v, remove specified key and return the corresponding value\n\
1950If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00001951
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001952PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00001953"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000019542-tuple; but raise KeyError if D is empty");
Tim Petersf7f88b12000-12-13 23:18:45 +00001955
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001956PyDoc_STRVAR(keys__doc__,
1957"D.keys() -> list of D's keys");
Tim Petersf7f88b12000-12-13 23:18:45 +00001958
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001959PyDoc_STRVAR(items__doc__,
1960"D.items() -> list of D's (key, value) pairs, as 2-tuples");
Tim Petersf7f88b12000-12-13 23:18:45 +00001961
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001962PyDoc_STRVAR(values__doc__,
1963"D.values() -> list of D's values");
Tim Petersf7f88b12000-12-13 23:18:45 +00001964
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001965PyDoc_STRVAR(update__doc__,
Walter Dörwaldd70ad8a2004-05-28 20:59:21 +00001966"D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k]\n\
1967(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 +00001968
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001969PyDoc_STRVAR(fromkeys__doc__,
1970"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
1971v defaults to None.");
1972
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001973PyDoc_STRVAR(clear__doc__,
1974"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001975
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001976PyDoc_STRVAR(copy__doc__,
1977"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00001978
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001979PyDoc_STRVAR(iterkeys__doc__,
1980"D.iterkeys() -> an iterator over the keys of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00001981
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001982PyDoc_STRVAR(itervalues__doc__,
1983"D.itervalues() -> an iterator over the values of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00001984
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001985PyDoc_STRVAR(iteritems__doc__,
1986"D.iteritems() -> an iterator over the (key, value) items of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00001987
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001988static PyMethodDef mapp_methods[] = {
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001989 {"__contains__",(PyCFunction)dict_has_key, METH_O | METH_COEXIST,
1990 contains__doc__},
Raymond Hettinger0c669672003-12-13 13:31:55 +00001991 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001992 getitem__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001993 {"has_key", (PyCFunction)dict_has_key, METH_O,
Tim Petersf7f88b12000-12-13 23:18:45 +00001994 has_key__doc__},
1995 {"get", (PyCFunction)dict_get, METH_VARARGS,
1996 get__doc__},
1997 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1998 setdefault_doc__},
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001999 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
Just van Rossuma797d812002-11-23 09:45:04 +00002000 pop__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002001 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002002 popitem__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002003 {"keys", (PyCFunction)dict_keys, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002004 keys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002005 {"items", (PyCFunction)dict_items, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002006 items__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002007 {"values", (PyCFunction)dict_values, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002008 values__doc__},
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002009 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002010 update__doc__},
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002011 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
2012 fromkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002013 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002014 clear__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002015 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002016 copy__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002017 {"iterkeys", (PyCFunction)dict_iterkeys, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00002018 iterkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002019 {"itervalues", (PyCFunction)dict_itervalues, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00002020 itervalues__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002021 {"iteritems", (PyCFunction)dict_iteritems, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00002022 iteritems__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00002023 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002024};
2025
Tim Petersd770ebd2006-06-01 15:50:44 +00002026/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002027int
2028PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002029{
2030 long hash;
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002031 dictobject *mp = (dictobject *)op;
Armin Rigo35f6d362006-06-01 13:19:12 +00002032 dictentry *ep;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002033
Tim Peters0ab085c2001-09-14 00:25:33 +00002034 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00002035 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002036 hash = PyObject_Hash(key);
2037 if (hash == -1)
2038 return -1;
2039 }
Armin Rigo35f6d362006-06-01 13:19:12 +00002040 ep = (mp->ma_lookup)(mp, key, hash);
Tim Petersd770ebd2006-06-01 15:50:44 +00002041 return ep == NULL ? -1 : (ep->me_value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002042}
2043
Raymond Hettinger1bff7962007-02-19 03:04:45 +00002044/* Internal version of PyDict_Contains used when the hash value is already known */
2045int
2046_PyDict_Contains(PyObject *op, PyObject *key, long hash)
2047{
2048 dictobject *mp = (dictobject *)op;
2049 dictentry *ep;
2050
2051 ep = (mp->ma_lookup)(mp, key, hash);
2052 return ep == NULL ? -1 : (ep->me_value != NULL);
2053}
2054
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002055/* Hack to implement "key in dict" */
2056static PySequenceMethods dict_as_sequence = {
Georg Brandl347b3002006-03-30 11:57:00 +00002057 0, /* sq_length */
2058 0, /* sq_concat */
2059 0, /* sq_repeat */
2060 0, /* sq_item */
2061 0, /* sq_slice */
2062 0, /* sq_ass_item */
2063 0, /* sq_ass_slice */
2064 PyDict_Contains, /* sq_contains */
2065 0, /* sq_inplace_concat */
2066 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002067};
2068
Guido van Rossum09e563a2001-05-01 12:10:21 +00002069static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002070dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2071{
2072 PyObject *self;
2073
2074 assert(type != NULL && type->tp_alloc != NULL);
2075 self = type->tp_alloc(type, 0);
2076 if (self != NULL) {
2077 PyDictObject *d = (PyDictObject *)self;
2078 /* It's guaranteed that tp->alloc zeroed out the struct. */
2079 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
2080 INIT_NONZERO_DICT_SLOTS(d);
2081 d->ma_lookup = lookdict_string;
2082#ifdef SHOW_CONVERSION_COUNTS
2083 ++created;
2084#endif
2085 }
2086 return self;
2087}
2088
Tim Peters25786c02001-09-02 08:22:48 +00002089static int
2090dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2091{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002092 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002093}
2094
Guido van Rossumdbb53d92001-12-03 16:32:18 +00002095static long
2096dict_nohash(PyObject *self)
2097{
2098 PyErr_SetString(PyExc_TypeError, "dict objects are unhashable");
2099 return -1;
2100}
2101
Tim Peters6d6c1a32001-08-02 04:15:00 +00002102static PyObject *
Guido van Rossum09e563a2001-05-01 12:10:21 +00002103dict_iter(dictobject *dict)
2104{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002105 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002106}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002107
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002108PyDoc_STRVAR(dictionary_doc,
Tim Petersa427a2b2001-10-29 22:25:45 +00002109"dict() -> new empty dictionary.\n"
2110"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Tim Peters1fc240e2001-10-26 05:06:50 +00002111" (key, value) pairs.\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002112"dict(seq) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002113" d = {}\n"
2114" for k, v in seq:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002115" d[k] = v\n"
2116"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2117" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002118
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002119PyTypeObject PyDict_Type = {
2120 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002121 0,
Tim Petersa427a2b2001-10-29 22:25:45 +00002122 "dict",
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002123 sizeof(dictobject),
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002124 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00002125 (destructor)dict_dealloc, /* tp_dealloc */
2126 (printfunc)dict_print, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002127 0, /* tp_getattr */
Guido van Rossumb9324202001-01-18 00:39:02 +00002128 0, /* tp_setattr */
Tim Peters4fa58bf2001-05-10 21:45:19 +00002129 (cmpfunc)dict_compare, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00002130 (reprfunc)dict_repr, /* tp_repr */
2131 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002132 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00002133 &dict_as_mapping, /* tp_as_mapping */
Guido van Rossumdbb53d92001-12-03 16:32:18 +00002134 dict_nohash, /* tp_hash */
Guido van Rossumb9324202001-01-18 00:39:02 +00002135 0, /* tp_call */
2136 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002137 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumb9324202001-01-18 00:39:02 +00002138 0, /* tp_setattro */
2139 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002140 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Tim Peters6d6c1a32001-08-02 04:15:00 +00002141 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters25786c02001-09-02 08:22:48 +00002142 dictionary_doc, /* tp_doc */
Georg Brandl347b3002006-03-30 11:57:00 +00002143 dict_traverse, /* tp_traverse */
2144 dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00002145 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002146 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00002147 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002148 0, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002149 mapp_methods, /* tp_methods */
2150 0, /* tp_members */
2151 0, /* tp_getset */
2152 0, /* tp_base */
2153 0, /* tp_dict */
2154 0, /* tp_descr_get */
2155 0, /* tp_descr_set */
2156 0, /* tp_dictoffset */
Georg Brandl347b3002006-03-30 11:57:00 +00002157 dict_init, /* tp_init */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002158 PyType_GenericAlloc, /* tp_alloc */
2159 dict_new, /* tp_new */
Neil Schemenauer6189b892002-04-12 02:43:00 +00002160 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002161};
2162
Guido van Rossum3cca2451997-05-16 14:23:33 +00002163/* For backward compatibility with old dictionary interface */
2164
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002165PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002166PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002167{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002168 PyObject *kv, *rv;
2169 kv = PyString_FromString(key);
2170 if (kv == NULL)
2171 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002172 rv = PyDict_GetItem(v, kv);
2173 Py_DECREF(kv);
2174 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002175}
2176
2177int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002178PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002179{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002180 PyObject *kv;
2181 int err;
2182 kv = PyString_FromString(key);
2183 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002184 return -1;
Guido van Rossum4f3bf1e1997-09-29 23:31:11 +00002185 PyString_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00002186 err = PyDict_SetItem(v, kv, item);
2187 Py_DECREF(kv);
2188 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002189}
2190
2191int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002192PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002193{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002194 PyObject *kv;
2195 int err;
2196 kv = PyString_FromString(key);
2197 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002198 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002199 err = PyDict_DelItem(v, kv);
2200 Py_DECREF(kv);
2201 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002202}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002203
Raymond Hettinger019a1482004-03-18 02:41:19 +00002204/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002205
2206typedef struct {
2207 PyObject_HEAD
Guido van Rossum2147df72002-07-16 20:30:22 +00002208 dictobject *di_dict; /* Set to NULL when iterator is exhausted */
Tim Peters9b10f7e2006-05-30 04:16:25 +00002209 Py_ssize_t di_used;
2210 Py_ssize_t di_pos;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002211 PyObject* di_result; /* reusable result tuple for iteritems */
Tim Peters9b10f7e2006-05-30 04:16:25 +00002212 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002213} dictiterobject;
2214
2215static PyObject *
Raymond Hettinger019a1482004-03-18 02:41:19 +00002216dictiter_new(dictobject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002217{
2218 dictiterobject *di;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002219 di = PyObject_New(dictiterobject, itertype);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002220 if (di == NULL)
2221 return NULL;
2222 Py_INCREF(dict);
2223 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00002224 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002225 di->di_pos = 0;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002226 di->len = dict->ma_used;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002227 if (itertype == &PyDictIterItem_Type) {
2228 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2229 if (di->di_result == NULL) {
2230 Py_DECREF(di);
2231 return NULL;
2232 }
2233 }
2234 else
2235 di->di_result = NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002236 return (PyObject *)di;
2237}
2238
2239static void
2240dictiter_dealloc(dictiterobject *di)
2241{
Guido van Rossum2147df72002-07-16 20:30:22 +00002242 Py_XDECREF(di->di_dict);
Raymond Hettinger019a1482004-03-18 02:41:19 +00002243 Py_XDECREF(di->di_result);
Neil Schemenauer6189b892002-04-12 02:43:00 +00002244 PyObject_Del(di);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002245}
2246
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002247static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002248dictiter_len(dictiterobject *di)
2249{
Tim Peters9b10f7e2006-05-30 04:16:25 +00002250 Py_ssize_t len = 0;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00002251 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002252 len = di->len;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002253 return PyInt_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002254}
2255
Armin Rigof5b3e362006-02-11 21:32:43 +00002256PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002257
2258static PyMethodDef dictiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +00002259 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002260 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002261};
2262
Raymond Hettinger019a1482004-03-18 02:41:19 +00002263static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002264{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002265 PyObject *key;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002266 register Py_ssize_t i, mask;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002267 register dictentry *ep;
2268 dictobject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002269
Raymond Hettinger019a1482004-03-18 02:41:19 +00002270 if (d == NULL)
Guido van Rossum2147df72002-07-16 20:30:22 +00002271 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002272 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002273
Raymond Hettinger019a1482004-03-18 02:41:19 +00002274 if (di->di_used != d->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00002275 PyErr_SetString(PyExc_RuntimeError,
2276 "dictionary changed size during iteration");
Guido van Rossum2147df72002-07-16 20:30:22 +00002277 di->di_used = -1; /* Make this state sticky */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002278 return NULL;
2279 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002280
Raymond Hettinger019a1482004-03-18 02:41:19 +00002281 i = di->di_pos;
2282 if (i < 0)
2283 goto fail;
2284 ep = d->ma_table;
2285 mask = d->ma_mask;
2286 while (i <= mask && ep[i].me_value == NULL)
2287 i++;
2288 di->di_pos = i+1;
2289 if (i > mask)
2290 goto fail;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002291 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002292 key = ep[i].me_key;
2293 Py_INCREF(key);
2294 return key;
2295
2296fail:
2297 Py_DECREF(d);
Guido van Rossum2147df72002-07-16 20:30:22 +00002298 di->di_dict = NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002299 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002300}
2301
Raymond Hettinger019a1482004-03-18 02:41:19 +00002302PyTypeObject PyDictIterKey_Type = {
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002303 PyObject_HEAD_INIT(&PyType_Type)
2304 0, /* ob_size */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002305 "dictionary-keyiterator", /* tp_name */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002306 sizeof(dictiterobject), /* tp_basicsize */
2307 0, /* tp_itemsize */
2308 /* methods */
2309 (destructor)dictiter_dealloc, /* tp_dealloc */
2310 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002311 0, /* tp_getattr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002312 0, /* tp_setattr */
2313 0, /* tp_compare */
2314 0, /* tp_repr */
2315 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002316 0, /* tp_as_sequence */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002317 0, /* tp_as_mapping */
2318 0, /* tp_hash */
2319 0, /* tp_call */
2320 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002321 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002322 0, /* tp_setattro */
2323 0, /* tp_as_buffer */
2324 Py_TPFLAGS_DEFAULT, /* tp_flags */
2325 0, /* tp_doc */
2326 0, /* tp_traverse */
2327 0, /* tp_clear */
2328 0, /* tp_richcompare */
2329 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002330 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002331 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002332 dictiter_methods, /* tp_methods */
2333 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002334};
2335
2336static PyObject *dictiter_iternextvalue(dictiterobject *di)
2337{
2338 PyObject *value;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002339 register Py_ssize_t i, mask;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002340 register dictentry *ep;
2341 dictobject *d = di->di_dict;
2342
2343 if (d == NULL)
2344 return NULL;
2345 assert (PyDict_Check(d));
2346
2347 if (di->di_used != d->ma_used) {
2348 PyErr_SetString(PyExc_RuntimeError,
2349 "dictionary changed size during iteration");
2350 di->di_used = -1; /* Make this state sticky */
2351 return NULL;
2352 }
2353
2354 i = di->di_pos;
Guido van Rossum09240f62004-03-20 19:11:58 +00002355 mask = d->ma_mask;
2356 if (i < 0 || i > mask)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002357 goto fail;
2358 ep = d->ma_table;
Guido van Rossum09240f62004-03-20 19:11:58 +00002359 while ((value=ep[i].me_value) == NULL) {
Raymond Hettinger019a1482004-03-18 02:41:19 +00002360 i++;
Guido van Rossum09240f62004-03-20 19:11:58 +00002361 if (i > mask)
2362 goto fail;
2363 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002364 di->di_pos = i+1;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002365 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002366 Py_INCREF(value);
2367 return value;
2368
2369fail:
2370 Py_DECREF(d);
2371 di->di_dict = NULL;
2372 return NULL;
2373}
2374
2375PyTypeObject PyDictIterValue_Type = {
2376 PyObject_HEAD_INIT(&PyType_Type)
2377 0, /* ob_size */
2378 "dictionary-valueiterator", /* tp_name */
2379 sizeof(dictiterobject), /* tp_basicsize */
2380 0, /* tp_itemsize */
2381 /* methods */
2382 (destructor)dictiter_dealloc, /* tp_dealloc */
2383 0, /* tp_print */
2384 0, /* tp_getattr */
2385 0, /* tp_setattr */
2386 0, /* tp_compare */
2387 0, /* tp_repr */
2388 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002389 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002390 0, /* tp_as_mapping */
2391 0, /* tp_hash */
2392 0, /* tp_call */
2393 0, /* tp_str */
2394 PyObject_GenericGetAttr, /* tp_getattro */
2395 0, /* tp_setattro */
2396 0, /* tp_as_buffer */
2397 Py_TPFLAGS_DEFAULT, /* tp_flags */
2398 0, /* tp_doc */
2399 0, /* tp_traverse */
2400 0, /* tp_clear */
2401 0, /* tp_richcompare */
2402 0, /* tp_weaklistoffset */
2403 PyObject_SelfIter, /* tp_iter */
2404 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002405 dictiter_methods, /* tp_methods */
2406 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002407};
2408
2409static PyObject *dictiter_iternextitem(dictiterobject *di)
2410{
2411 PyObject *key, *value, *result = di->di_result;
Tim Peters9b10f7e2006-05-30 04:16:25 +00002412 register Py_ssize_t i, mask;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002413 register dictentry *ep;
2414 dictobject *d = di->di_dict;
2415
2416 if (d == NULL)
2417 return NULL;
2418 assert (PyDict_Check(d));
2419
2420 if (di->di_used != d->ma_used) {
2421 PyErr_SetString(PyExc_RuntimeError,
2422 "dictionary changed size during iteration");
2423 di->di_used = -1; /* Make this state sticky */
2424 return NULL;
2425 }
2426
2427 i = di->di_pos;
2428 if (i < 0)
2429 goto fail;
2430 ep = d->ma_table;
2431 mask = d->ma_mask;
2432 while (i <= mask && ep[i].me_value == NULL)
2433 i++;
2434 di->di_pos = i+1;
2435 if (i > mask)
2436 goto fail;
2437
2438 if (result->ob_refcnt == 1) {
2439 Py_INCREF(result);
2440 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2441 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2442 } else {
2443 result = PyTuple_New(2);
Tim Peters60b29962006-01-01 01:19:23 +00002444 if (result == NULL)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002445 return NULL;
2446 }
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002447 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002448 key = ep[i].me_key;
2449 value = ep[i].me_value;
2450 Py_INCREF(key);
2451 Py_INCREF(value);
2452 PyTuple_SET_ITEM(result, 0, key);
2453 PyTuple_SET_ITEM(result, 1, value);
2454 return result;
2455
2456fail:
2457 Py_DECREF(d);
2458 di->di_dict = NULL;
2459 return NULL;
2460}
2461
2462PyTypeObject PyDictIterItem_Type = {
2463 PyObject_HEAD_INIT(&PyType_Type)
2464 0, /* ob_size */
2465 "dictionary-itemiterator", /* tp_name */
2466 sizeof(dictiterobject), /* tp_basicsize */
2467 0, /* tp_itemsize */
2468 /* methods */
2469 (destructor)dictiter_dealloc, /* tp_dealloc */
2470 0, /* tp_print */
2471 0, /* tp_getattr */
2472 0, /* tp_setattr */
2473 0, /* tp_compare */
2474 0, /* tp_repr */
2475 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002476 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002477 0, /* tp_as_mapping */
2478 0, /* tp_hash */
2479 0, /* tp_call */
2480 0, /* tp_str */
2481 PyObject_GenericGetAttr, /* tp_getattro */
2482 0, /* tp_setattro */
2483 0, /* tp_as_buffer */
2484 Py_TPFLAGS_DEFAULT, /* tp_flags */
2485 0, /* tp_doc */
2486 0, /* tp_traverse */
2487 0, /* tp_clear */
2488 0, /* tp_richcompare */
2489 0, /* tp_weaklistoffset */
2490 PyObject_SelfIter, /* tp_iter */
2491 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002492 dictiter_methods, /* tp_methods */
2493 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002494};