blob: f9e45fd86242bd62383afd77d215ef0342079b33 [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
Tim Peterseb28ef22001-06-02 05:27:19 +000015/* Define this out if you don't want conversion statistics on exit. */
Fred Drake1bff34a2000-08-31 19:31:38 +000016#undef SHOW_CONVERSION_COUNTS
17
Tim Peterseb28ef22001-06-02 05:27:19 +000018/* See large comment block below. This must be >= 1. */
19#define PERTURB_SHIFT 5
20
Guido van Rossum16e93a81997-01-28 00:00:11 +000021/*
Tim Peterseb28ef22001-06-02 05:27:19 +000022Major subtleties ahead: Most hash schemes depend on having a "good" hash
23function, in the sense of simulating randomness. Python doesn't: its most
24important hash functions (for strings and ints) are very regular in common
25cases:
Tim Peters15d49292001-05-27 07:39:22 +000026
Tim Peterseb28ef22001-06-02 05:27:19 +000027>>> map(hash, (0, 1, 2, 3))
28[0, 1, 2, 3]
29>>> map(hash, ("namea", "nameb", "namec", "named"))
30[-1658398457, -1658398460, -1658398459, -1658398462]
31>>>
Tim Peters15d49292001-05-27 07:39:22 +000032
Tim Peterseb28ef22001-06-02 05:27:19 +000033This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
34the low-order i bits as the initial table index is extremely fast, and there
35are no collisions at all for dicts indexed by a contiguous range of ints.
36The same is approximately true when keys are "consecutive" strings. So this
37gives better-than-random behavior in common cases, and that's very desirable.
Tim Peters15d49292001-05-27 07:39:22 +000038
Tim Peterseb28ef22001-06-02 05:27:19 +000039OTOH, when collisions occur, the tendency to fill contiguous slices of the
40hash table makes a good collision resolution strategy crucial. Taking only
41the last i bits of the hash code is also vulnerable: for example, consider
42[i << 16 for i in range(20000)] as a set of keys. Since ints are their own
43hash codes, and this fits in a dict of size 2**15, the last 15 bits of every
44hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +000045
Tim Peterseb28ef22001-06-02 05:27:19 +000046But catering to unusual cases should not slow the usual ones, so we just take
47the last i bits anyway. It's up to collision resolution to do the rest. If
48we *usually* find the key we're looking for on the first try (and, it turns
49out, we usually do -- the table load factor is kept under 2/3, so the odds
50are solidly in our favor), then it makes best sense to keep the initial index
51computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +000052
Tim Peterseb28ef22001-06-02 05:27:19 +000053The first half of collision resolution is to visit table indices via this
54recurrence:
Tim Peters15d49292001-05-27 07:39:22 +000055
Tim Peterseb28ef22001-06-02 05:27:19 +000056 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +000057
Tim Peterseb28ef22001-06-02 05:27:19 +000058For any initial j in range(2**i), repeating that 2**i times generates each
59int in range(2**i) exactly once (see any text on random-number generation for
60proof). By itself, this doesn't help much: like linear probing (setting
61j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
62order. This would be bad, except that's not the only thing we do, and it's
63actually *good* in the common cases where hash keys are consecutive. In an
64example that's really too small to make this entirely clear, for a table of
65size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +000066
Tim Peterseb28ef22001-06-02 05:27:19 +000067 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
68
69If two things come in at index 5, the first place we look after is index 2,
70not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
71Linear probing is deadly in this case because there the fixed probe order
72is the *same* as the order consecutive keys are likely to arrive. But it's
73extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
74and certain that consecutive hash codes do not.
75
76The other half of the strategy is to get the other bits of the hash code
77into play. This is done by initializing a (unsigned) vrbl "perturb" to the
78full hash code, and changing the recurrence to:
79
80 j = (5*j) + 1 + perturb;
81 perturb >>= PERTURB_SHIFT;
82 use j % 2**i as the next table index;
83
84Now the probe sequence depends (eventually) on every bit in the hash code,
85and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
86because it quickly magnifies small differences in the bits that didn't affect
87the initial index. Note that because perturb is unsigned, if the recurrence
88is executed often enough perturb eventually becomes and remains 0. At that
89point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
90that's certain to find an empty slot eventually (since it generates every int
91in range(2**i), and we make sure there's always at least one empty slot).
92
93Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
94small so that the high bits of the hash code continue to affect the probe
95sequence across iterations; but you want it large so that in really bad cases
96the high-order hash bits have an effect on early iterations. 5 was "the
97best" in minimizing total collisions across experiments Tim Peters ran (on
98both normal and pathological cases), but 4 and 6 weren't significantly worse.
99
100Historical: Reimer Behrends contributed the idea of using a polynomial-based
101approach, using repeated multiplication by x in GF(2**n) where an irreducible
102polynomial for each table size was chosen such that x was a primitive root.
103Christian Tismer later extended that to use division by x instead, as an
104efficient way to get the high bits of the hash code into play. This scheme
105also gave excellent collision statistics, but was more expensive: two
106if-tests were required inside the loop; computing "the next" index took about
107the same number of operations but without as much potential parallelism
108(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
109above, and then shifting perturb can be done while the table index is being
110masked); and the dictobject struct required a member to hold the table's
111polynomial. In Tim's experiments the current scheme ran faster, produced
112equally good collision statistics, needed less code & used less memory.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000113
114Theoretical Python 2.5 headache: hash codes are only C "long", but
115sizeof(Py_ssize_t) > sizeof(long) may be possible. In that case, and if a
116dict is genuinely huge, then only the slots directly reachable via indexing
117by a C long can be the first slot in a probe sequence. The probe sequence
118will still eventually reach every slot in the table, but the collision rate
119on initial probes may be much higher than this scheme was designed for.
120Getting a hash code as fat as Py_ssize_t is the only real cure. But in
121practice, this probably won't make a lick of difference for many years (at
122which point everyone will have terabytes of RAM on 64-bit boxes).
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000123*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000124
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000125/* Object used as dummy key to fill deleted entries */
Raymond Hettingerf81e4502005-08-17 02:19:36 +0000126static PyObject *dummy = NULL; /* Initialized by first call to newdictobject() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000127
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000128#ifdef Py_REF_DEBUG
129PyObject *
130_PyDict_Dummy(void)
131{
132 return dummy;
133}
134#endif
135
Fred Drake1bff34a2000-08-31 19:31:38 +0000136/* forward declarations */
137static dictentry *
138lookdict_string(dictobject *mp, PyObject *key, long hash);
139
140#ifdef SHOW_CONVERSION_COUNTS
141static long created = 0L;
142static long converted = 0L;
143
144static void
145show_counts(void)
146{
147 fprintf(stderr, "created %ld string dicts\n", created);
148 fprintf(stderr, "converted %ld to normal dicts\n", converted);
149 fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created);
150}
151#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000152
Tim Peters6d6c1a32001-08-02 04:15:00 +0000153/* Initialization macros.
154 There are two ways to create a dict: PyDict_New() is the main C API
155 function, and the tp_new slot maps to dict_new(). In the latter case we
156 can save a little time over what PyDict_New does because it's guaranteed
157 that the PyDictObject struct is already zeroed out.
158 Everyone except dict_new() should use EMPTY_TO_MINSIZE (unless they have
159 an excellent reason not to).
160*/
161
162#define INIT_NONZERO_DICT_SLOTS(mp) do { \
Tim Petersdea48ec2001-05-22 20:40:22 +0000163 (mp)->ma_table = (mp)->ma_smalltable; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000164 (mp)->ma_mask = PyDict_MINSIZE - 1; \
165 } while(0)
166
167#define EMPTY_TO_MINSIZE(mp) do { \
168 memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000169 (mp)->ma_used = (mp)->ma_fill = 0; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000170 INIT_NONZERO_DICT_SLOTS(mp); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000171 } while(0)
172
Raymond Hettinger43442782004-03-17 21:55:03 +0000173/* Dictionary reuse scheme to save calls to malloc, free, and memset */
174#define MAXFREEDICTS 80
175static PyDictObject *free_dicts[MAXFREEDICTS];
176static int num_free_dicts = 0;
177
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000178PyObject *
Thomas Wouters78890102000-07-22 19:25:51 +0000179PyDict_New(void)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000180{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000181 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000182 if (dummy == NULL) { /* Auto-initialize dummy */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000183 dummy = PyString_FromString("<dummy key>");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000184 if (dummy == NULL)
185 return NULL;
Fred Drake1bff34a2000-08-31 19:31:38 +0000186#ifdef SHOW_CONVERSION_COUNTS
187 Py_AtExit(show_counts);
188#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000189 }
Raymond Hettinger43442782004-03-17 21:55:03 +0000190 if (num_free_dicts) {
191 mp = free_dicts[--num_free_dicts];
192 assert (mp != NULL);
193 assert (mp->ob_type == &PyDict_Type);
194 _Py_NewReference((PyObject *)mp);
195 if (mp->ma_fill) {
196 EMPTY_TO_MINSIZE(mp);
197 }
198 assert (mp->ma_used == 0);
199 assert (mp->ma_table == mp->ma_smalltable);
200 assert (mp->ma_mask == PyDict_MINSIZE - 1);
201 } else {
202 mp = PyObject_GC_New(dictobject, &PyDict_Type);
203 if (mp == NULL)
204 return NULL;
205 EMPTY_TO_MINSIZE(mp);
206 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000207 mp->ma_lookup = lookdict_string;
208#ifdef SHOW_CONVERSION_COUNTS
209 ++created;
210#endif
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000211 _PyObject_GC_TRACK(mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000212 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000213}
214
215/*
216The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000217This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000218Open addressing is preferred over chaining since the link overhead for
219chaining would be substantial (100% with typical malloc overhead).
220
Tim Peterseb28ef22001-06-02 05:27:19 +0000221The initial probe index is computed as hash mod the table size. Subsequent
222probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000223
224All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000225
Tim Peterseb28ef22001-06-02 05:27:19 +0000226(The details in this version are due to Tim Peters, building on many past
227contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
228Christian Tismer).
Fred Drake1bff34a2000-08-31 19:31:38 +0000229
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000230lookdict() is general-purpose, and may return NULL if (and only if) a
231comparison raises an exception (this was new in Python 2.5).
232lookdict_string() below is specialized to string keys, comparison of which can
233never raise an exception; that function can never return NULL. For both, when
234the key isn't found a dictentry* is returned for which the me_value field is
235NULL; this is the slot in the dict at which the key would have been found, and
236the caller can (if it wishes) add the <key, value> pair to the returned
237dictentry*.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000238*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000239static dictentry *
Tim Peters1f5871e2000-07-04 17:44:48 +0000240lookdict(dictobject *mp, PyObject *key, register long hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000241{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000242 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000243 register size_t perturb;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000244 register dictentry *freeslot;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000245 register size_t mask = (size_t)mp->ma_mask;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000246 dictentry *ep0 = mp->ma_table;
247 register dictentry *ep;
Fred Drakec88b99c2000-08-31 19:04:07 +0000248 register int cmp;
Tim Peters453163d2001-06-03 04:54:32 +0000249 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000250
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000251 i = (size_t)hash & mask;
Guido van Rossumefb46091997-01-29 15:53:56 +0000252 ep = &ep0[i];
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000253 if (ep->me_key == NULL || ep->me_key == key)
Guido van Rossum7d181591997-01-16 21:06:45 +0000254 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000255
Guido van Rossum16e93a81997-01-28 00:00:11 +0000256 if (ep->me_key == dummy)
Guido van Rossum7d181591997-01-16 21:06:45 +0000257 freeslot = ep;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000258 else {
Fred Drakec88b99c2000-08-31 19:04:07 +0000259 if (ep->me_hash == hash) {
Tim Peters453163d2001-06-03 04:54:32 +0000260 startkey = ep->me_key;
261 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000262 if (cmp < 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000263 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000264 if (ep0 == mp->ma_table && ep->me_key == startkey) {
265 if (cmp > 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000266 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000267 }
268 else {
269 /* The compare did major nasty stuff to the
270 * dict: start over.
271 * XXX A clever adversary could prevent this
272 * XXX from terminating.
273 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000274 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000275 }
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000276 }
277 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000278 }
Tim Peters15d49292001-05-27 07:39:22 +0000279
Tim Peters342c65e2001-05-13 06:43:53 +0000280 /* In the loop, me_key == dummy is by far (factor of 100s) the
281 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000282 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
283 i = (i << 2) + i + perturb + 1;
284 ep = &ep0[i & mask];
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000285 if (ep->me_key == NULL)
286 return freeslot == NULL ? ep : freeslot;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000287 if (ep->me_key == key)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000288 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000289 if (ep->me_hash == hash && ep->me_key != dummy) {
Tim Peters453163d2001-06-03 04:54:32 +0000290 startkey = ep->me_key;
291 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000292 if (cmp < 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000293 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000294 if (ep0 == mp->ma_table && ep->me_key == startkey) {
295 if (cmp > 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000296 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000297 }
298 else {
299 /* The compare did major nasty stuff to the
300 * dict: start over.
301 * XXX A clever adversary could prevent this
302 * XXX from terminating.
303 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000304 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000305 }
Guido van Rossum16e93a81997-01-28 00:00:11 +0000306 }
Tim Peters342c65e2001-05-13 06:43:53 +0000307 else if (ep->me_key == dummy && freeslot == NULL)
308 freeslot = ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000309 }
310}
311
312/*
Fred Drake1bff34a2000-08-31 19:31:38 +0000313 * Hacked up version of lookdict which can assume keys are always strings;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000314 * this assumption allows testing for errors during PyObject_RichCompareBool()
315 * to be dropped; string-string comparisons never raise exceptions. This also
316 * means we don't need to go through PyObject_RichCompareBool(); we can always
317 * use _PyString_Eq() directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000318 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000319 * This is valuable because dicts with only string keys are very common.
Fred Drake1bff34a2000-08-31 19:31:38 +0000320 */
321static dictentry *
322lookdict_string(dictobject *mp, PyObject *key, register long hash)
323{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000324 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000325 register size_t perturb;
Fred Drake1bff34a2000-08-31 19:31:38 +0000326 register dictentry *freeslot;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000327 register size_t mask = (size_t)mp->ma_mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000328 dictentry *ep0 = mp->ma_table;
329 register dictentry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000330
Tim Peters0ab085c2001-09-14 00:25:33 +0000331 /* Make sure this function doesn't have to handle non-string keys,
332 including subclasses of str; e.g., one reason to subclass
333 strings is to override __eq__, and for speed we don't cater to
334 that here. */
335 if (!PyString_CheckExact(key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000336#ifdef SHOW_CONVERSION_COUNTS
337 ++converted;
338#endif
339 mp->ma_lookup = lookdict;
340 return lookdict(mp, key, hash);
341 }
Tim Peters2f228e72001-05-13 00:19:31 +0000342 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000343 ep = &ep0[i];
344 if (ep->me_key == NULL || ep->me_key == key)
345 return ep;
346 if (ep->me_key == dummy)
347 freeslot = ep;
348 else {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000349 if (ep->me_hash == hash && _PyString_Eq(ep->me_key, key))
Fred Drake1bff34a2000-08-31 19:31:38 +0000350 return ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000351 freeslot = NULL;
352 }
Tim Peters15d49292001-05-27 07:39:22 +0000353
Tim Peters342c65e2001-05-13 06:43:53 +0000354 /* In the loop, me_key == dummy is by far (factor of 100s) the
355 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000356 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
357 i = (i << 2) + i + perturb + 1;
358 ep = &ep0[i & mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000359 if (ep->me_key == NULL)
360 return freeslot == NULL ? ep : freeslot;
361 if (ep->me_key == key
362 || (ep->me_hash == hash
363 && ep->me_key != dummy
Martin v. Löwiscd353062001-05-24 16:56:35 +0000364 && _PyString_Eq(ep->me_key, key)))
Fred Drake1bff34a2000-08-31 19:31:38 +0000365 return ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000366 if (ep->me_key == dummy && freeslot == NULL)
Tim Peters342c65e2001-05-13 06:43:53 +0000367 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000368 }
369}
370
371/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000372Internal routine to insert a new item into the table.
373Used both by the internal resize routine and by the public insert routine.
374Eats a reference to key and one to value.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000375Returns -1 if an error occurred, or 0 on success.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000376*/
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000377static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000378insertdict(register dictobject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000379{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000380 PyObject *old_value;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000381 register dictentry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000382 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
383
384 assert(mp->ma_lookup != NULL);
385 ep = mp->ma_lookup(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000386 if (ep == NULL) {
387 Py_DECREF(key);
388 Py_DECREF(value);
389 return -1;
390 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000391 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000392 old_value = ep->me_value;
393 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000394 Py_DECREF(old_value); /* which **CAN** re-enter */
395 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000396 }
397 else {
398 if (ep->me_key == NULL)
399 mp->ma_fill++;
Raymond Hettinger07ead172005-02-05 23:42:57 +0000400 else {
401 assert(ep->me_key == dummy);
402 Py_DECREF(dummy);
403 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000404 ep->me_key = key;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000405 ep->me_hash = (Py_ssize_t)hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000406 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000407 mp->ma_used++;
408 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000409 return 0;
410}
411
412/*
413Internal routine used by dictresize() to insert an item which is
414known to be absent from the dict. This routine also assumes that
415the dict contains no deleted entries. Besides the performance benefit,
416using insertdict() in dictresize() is dangerous (SF bug #1456209).
417Note that no refcounts are changed by this routine; if needed, the caller
418is responsible for incref'ing `key` and `value`.
419*/
420static void
421insertdict_clean(register dictobject *mp, PyObject *key, long hash,
422 PyObject *value)
423{
424 register size_t i;
425 register size_t perturb;
426 register size_t mask = (size_t)mp->ma_mask;
427 dictentry *ep0 = mp->ma_table;
428 register dictentry *ep;
429
430 i = hash & mask;
431 ep = &ep0[i];
432 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
433 i = (i << 2) + i + perturb + 1;
434 ep = &ep0[i & mask];
435 }
436 assert(ep->me_value == NULL);
437 mp->ma_fill++;
438 ep->me_key = key;
439 ep->me_hash = (Py_ssize_t)hash;
440 ep->me_value = value;
441 mp->ma_used++;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000442}
443
444/*
445Restructure the table by allocating a new table and reinserting all
446items again. When entries have been deleted, the new table may
447actually be smaller than the old one.
448*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000449static int
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000450dictresize(dictobject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000451{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000452 Py_ssize_t newsize;
Tim Peters0c6010b2001-05-23 23:33:57 +0000453 dictentry *oldtable, *newtable, *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000454 Py_ssize_t i;
Tim Peters0c6010b2001-05-23 23:33:57 +0000455 int is_oldtable_malloced;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000456 dictentry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000457
458 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000459
Tim Peterseb28ef22001-06-02 05:27:19 +0000460 /* Find the smallest table size > minused. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000461 for (newsize = PyDict_MINSIZE;
Tim Peterseb28ef22001-06-02 05:27:19 +0000462 newsize <= minused && newsize > 0;
463 newsize <<= 1)
464 ;
465 if (newsize <= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000466 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000467 return -1;
468 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000469
470 /* Get space for a new table. */
471 oldtable = mp->ma_table;
472 assert(oldtable != NULL);
473 is_oldtable_malloced = oldtable != mp->ma_smalltable;
474
Tim Peters6d6c1a32001-08-02 04:15:00 +0000475 if (newsize == PyDict_MINSIZE) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000476 /* A large table is shrinking, or we can't get any smaller. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000477 newtable = mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000478 if (newtable == oldtable) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000479 if (mp->ma_fill == mp->ma_used) {
480 /* No dummies, so no point doing anything. */
Tim Peters0c6010b2001-05-23 23:33:57 +0000481 return 0;
Tim Petersf8a548c2001-05-24 16:26:40 +0000482 }
483 /* We're not going to resize it, but rebuild the
484 table anyway to purge old dummy entries.
485 Subtle: This is *necessary* if fill==size,
486 as lookdict needs at least one virgin slot to
487 terminate failing searches. If fill < size, it's
488 merely desirable, as dummies slow searches. */
489 assert(mp->ma_fill > mp->ma_used);
Tim Peters0c6010b2001-05-23 23:33:57 +0000490 memcpy(small_copy, oldtable, sizeof(small_copy));
491 oldtable = small_copy;
492 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000493 }
494 else {
495 newtable = PyMem_NEW(dictentry, newsize);
496 if (newtable == NULL) {
497 PyErr_NoMemory();
498 return -1;
499 }
500 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000501
502 /* Make the dict empty, using the new table. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000503 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000504 mp->ma_table = newtable;
Tim Petersafb6ae82001-06-04 21:00:21 +0000505 mp->ma_mask = newsize - 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000506 memset(newtable, 0, sizeof(dictentry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000507 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000508 i = mp->ma_fill;
509 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000510
Tim Peters19283142001-05-17 22:25:34 +0000511 /* Copy the data over; this is refcount-neutral for active entries;
512 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000513 for (ep = oldtable; i > 0; ep++) {
514 if (ep->me_value != NULL) { /* active entry */
515 --i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000516 insertdict_clean(mp, ep->me_key, (long)ep->me_hash,
517 ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000518 }
Tim Peters19283142001-05-17 22:25:34 +0000519 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000520 --i;
Tim Peters19283142001-05-17 22:25:34 +0000521 assert(ep->me_key == dummy);
522 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000523 }
Tim Peters19283142001-05-17 22:25:34 +0000524 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000525 }
526
Tim Peters0c6010b2001-05-23 23:33:57 +0000527 if (is_oldtable_malloced)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000528 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000529 return 0;
530}
531
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000532/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
533 * that may occur (originally dicts supported only string keys, and exceptions
534 * weren't possible). So, while the original intent was that a NULL return
535 * meant the key wasn't present, it reality it can mean that, or that an error
536 * (suppressed) occurred while computing the key's hash, or that some error
537 * (suppressed) occurred when comparing keys in the dict's internal probe
538 * sequence. A nasty example of the latter is when a Python-coded comparison
539 * function hits a stack-depth error, which can cause this to return NULL
540 * even if the key is present.
541 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000542PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000543PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000544{
545 long hash;
Fred Drake1bff34a2000-08-31 19:31:38 +0000546 dictobject *mp = (dictobject *)op;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000547 dictentry *ep;
548 PyThreadState *tstate;
549 if (!PyDict_Check(op))
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000550 return NULL;
Tim Peters0ab085c2001-09-14 00:25:33 +0000551 if (!PyString_CheckExact(key) ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000552 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Guido van Rossumca756f21997-01-23 19:39:29 +0000553 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000554 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000555 if (hash == -1) {
556 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000557 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000558 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000559 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000560
561 /* We can arrive here with a NULL tstate during initialization:
562 try running "python -Wi" for an example related to string
563 interning. Let's just hope that no exception occurs then... */
564 tstate = PyThreadState_GET();
565 if (tstate != NULL && tstate->curexc_type != NULL) {
566 /* preserve the existing exception */
567 PyObject *err_type, *err_value, *err_tb;
568 PyErr_Fetch(&err_type, &err_value, &err_tb);
569 ep = (mp->ma_lookup)(mp, key, hash);
570 /* ignore errors */
571 PyErr_Restore(err_type, err_value, err_tb);
572 if (ep == NULL)
573 return NULL;
574 }
575 else {
576 ep = (mp->ma_lookup)(mp, key, hash);
577 if (ep == NULL) {
578 PyErr_Clear();
579 return NULL;
580 }
581 }
582 return ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000583}
584
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000585/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
Tim Peters60b29962006-01-01 01:19:23 +0000586 * dictionary if it's merely replacing the value for an existing key.
587 * This means that it's safe to loop over a dictionary with PyDict_Next()
588 * and occasionally replace a value -- but you can't insert new keys or
589 * remove them.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000590 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000591int
Tim Peters1f5871e2000-07-04 17:44:48 +0000592PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000593{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000594 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000595 register long hash;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000596 register Py_ssize_t n_used;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000597
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000598 if (!PyDict_Check(op)) {
599 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000600 return -1;
601 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000602 mp = (dictobject *)op;
Tim Peters0ab085c2001-09-14 00:25:33 +0000603 if (PyString_CheckExact(key)) {
Guido van Rossum45ec02a2002-08-19 21:43:18 +0000604 hash = ((PyStringObject *)key)->ob_shash;
605 if (hash == -1)
606 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000607 }
Tim Peters1f7df352002-03-29 03:29:08 +0000608 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000609 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000610 if (hash == -1)
611 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000612 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000613 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000614 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000615 Py_INCREF(value);
616 Py_INCREF(key);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000617 if (insertdict(mp, key, hash, value) != 0)
618 return -1;
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000619 /* If we added a key, we can safely resize. Otherwise just return!
620 * If fill >= 2/3 size, adjust size. Normally, this doubles or
621 * quaduples the size, but it's also possible for the dict to shrink
Tim Peters60b29962006-01-01 01:19:23 +0000622 * (if ma_fill is much larger than ma_used, meaning a lot of dict
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000623 * keys have been * deleted).
Tim Peters60b29962006-01-01 01:19:23 +0000624 *
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000625 * Quadrupling the size improves average dictionary sparseness
626 * (reducing collisions) at the cost of some memory and iteration
627 * speed (which loops over every possible entry). It also halves
628 * the number of expensive resize operations in a growing dictionary.
Tim Peters60b29962006-01-01 01:19:23 +0000629 *
630 * Very large dictionaries (over 50K items) use doubling instead.
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000631 * This may help applications with severe memory constraints.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000632 */
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000633 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
634 return 0;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000635 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000636}
637
638int
Tim Peters1f5871e2000-07-04 17:44:48 +0000639PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000640{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000641 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000642 register long hash;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000643 register dictentry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000644 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000645
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000646 if (!PyDict_Check(op)) {
647 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000648 return -1;
649 }
Tim Peters0ab085c2001-09-14 00:25:33 +0000650 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +0000651 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000652 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000653 if (hash == -1)
654 return -1;
655 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000656 mp = (dictobject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000657 ep = (mp->ma_lookup)(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000658 if (ep == NULL)
659 return -1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000660 if (ep->me_value == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000661 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000662 return -1;
663 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000664 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000665 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000666 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000667 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000668 ep->me_value = NULL;
669 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000670 Py_DECREF(old_value);
671 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000672 return 0;
673}
674
Guido van Rossum25831651993-05-19 14:50:45 +0000675void
Tim Peters1f5871e2000-07-04 17:44:48 +0000676PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000677{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000678 dictobject *mp;
Tim Petersdea48ec2001-05-22 20:40:22 +0000679 dictentry *ep, *table;
680 int table_is_malloced;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000681 Py_ssize_t fill;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000682 dictentry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000683#ifdef Py_DEBUG
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000684 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +0000685#endif
686
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000687 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000688 return;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000689 mp = (dictobject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000690#ifdef Py_DEBUG
Tim Petersafb6ae82001-06-04 21:00:21 +0000691 n = mp->ma_mask + 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000692 i = 0;
693#endif
694
695 table = mp->ma_table;
696 assert(table != NULL);
697 table_is_malloced = table != mp->ma_smalltable;
698
699 /* This is delicate. During the process of clearing the dict,
700 * decrefs can cause the dict to mutate. To avoid fatal confusion
701 * (voice of experience), we have to make the dict empty before
702 * clearing the slots, and never refer to anything via mp->xxx while
703 * clearing.
704 */
705 fill = mp->ma_fill;
706 if (table_is_malloced)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000707 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000708
709 else if (fill > 0) {
710 /* It's a small table with something that needs to be cleared.
711 * Afraid the only safe way is to copy the dict entries into
712 * another small table first.
713 */
714 memcpy(small_copy, table, sizeof(small_copy));
715 table = small_copy;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000716 EMPTY_TO_MINSIZE(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000717 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000718 /* else it's a small table that's already empty */
719
720 /* Now we can finally clear things. If C had refcounts, we could
721 * assert that the refcount on table is 1 now, i.e. that this function
722 * has unique access to it, so decref side-effects can't alter it.
723 */
724 for (ep = table; fill > 0; ++ep) {
725#ifdef Py_DEBUG
726 assert(i < n);
727 ++i;
728#endif
729 if (ep->me_key) {
730 --fill;
731 Py_DECREF(ep->me_key);
732 Py_XDECREF(ep->me_value);
733 }
734#ifdef Py_DEBUG
735 else
736 assert(ep->me_value == NULL);
737#endif
738 }
739
740 if (table_is_malloced)
741 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000742}
743
Tim Peters080c88b2003-02-15 03:01:11 +0000744/*
745 * Iterate over a dict. Use like so:
746 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000747 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +0000748 * PyObject *key, *value;
749 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +0000750 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +0000751 * Refer to borrowed references in key and value.
752 * }
753 *
754 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +0000755 * mutates the dict. One exception: it is safe if the loop merely changes
756 * the values associated with the keys (but doesn't insert new keys or
757 * delete keys), via PyDict_SetItem().
758 */
Guido van Rossum25831651993-05-19 14:50:45 +0000759int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000760PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000761{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000762 register Py_ssize_t i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000763 register Py_ssize_t mask;
Raymond Hettinger43442782004-03-17 21:55:03 +0000764 register dictentry *ep;
765
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000766 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000767 return 0;
Guido van Rossum25831651993-05-19 14:50:45 +0000768 i = *ppos;
769 if (i < 0)
770 return 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000771 ep = ((dictobject *)op)->ma_table;
772 mask = ((dictobject *)op)->ma_mask;
773 while (i <= mask && ep[i].me_value == NULL)
Guido van Rossum25831651993-05-19 14:50:45 +0000774 i++;
775 *ppos = i+1;
Raymond Hettinger43442782004-03-17 21:55:03 +0000776 if (i > mask)
Guido van Rossum25831651993-05-19 14:50:45 +0000777 return 0;
778 if (pkey)
Raymond Hettinger43442782004-03-17 21:55:03 +0000779 *pkey = ep[i].me_key;
Guido van Rossum25831651993-05-19 14:50:45 +0000780 if (pvalue)
Raymond Hettinger43442782004-03-17 21:55:03 +0000781 *pvalue = ep[i].me_value;
Guido van Rossum25831651993-05-19 14:50:45 +0000782 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000783}
784
785/* Methods */
786
787static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000788dict_dealloc(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000789{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000790 register dictentry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000791 Py_ssize_t fill = mp->ma_fill;
Guido van Rossumff413af2002-03-28 20:34:59 +0000792 PyObject_GC_UnTrack(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000793 Py_TRASHCAN_SAFE_BEGIN(mp)
Tim Petersdea48ec2001-05-22 20:40:22 +0000794 for (ep = mp->ma_table; fill > 0; ep++) {
795 if (ep->me_key) {
796 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000797 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000798 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000799 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000800 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000801 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000802 PyMem_DEL(mp->ma_table);
Raymond Hettinger43442782004-03-17 21:55:03 +0000803 if (num_free_dicts < MAXFREEDICTS && mp->ob_type == &PyDict_Type)
804 free_dicts[num_free_dicts++] = mp;
Tim Peters60b29962006-01-01 01:19:23 +0000805 else
Raymond Hettinger43442782004-03-17 21:55:03 +0000806 mp->ob_type->tp_free((PyObject *)mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000807 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000808}
809
810static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000811dict_print(register dictobject *mp, register FILE *fp, register int flags)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000812{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000813 register Py_ssize_t i;
814 register Py_ssize_t any;
815 int status;
Guido van Rossum255443b1998-04-10 22:47:14 +0000816
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000817 status = Py_ReprEnter((PyObject*)mp);
818 if (status != 0) {
819 if (status < 0)
820 return status;
Guido van Rossum255443b1998-04-10 22:47:14 +0000821 fprintf(fp, "{...}");
822 return 0;
823 }
824
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000825 fprintf(fp, "{");
826 any = 0;
Tim Petersafb6ae82001-06-04 21:00:21 +0000827 for (i = 0; i <= mp->ma_mask; i++) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000828 dictentry *ep = mp->ma_table + i;
829 PyObject *pvalue = ep->me_value;
830 if (pvalue != NULL) {
831 /* Prevent PyObject_Repr from deleting value during
832 key format */
833 Py_INCREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000834 if (any++ > 0)
835 fprintf(fp, ", ");
Guido van Rossum255443b1998-04-10 22:47:14 +0000836 if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000837 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000838 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000839 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000840 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000841 fprintf(fp, ": ");
Tim Peters19b77cf2001-06-02 08:27:39 +0000842 if (PyObject_Print(pvalue, fp, 0) != 0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000843 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000844 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000845 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000846 }
Tim Peters23cf6be2001-06-02 08:02:56 +0000847 Py_DECREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000848 }
849 }
850 fprintf(fp, "}");
Guido van Rossum255443b1998-04-10 22:47:14 +0000851 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000852 return 0;
853}
854
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000855static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000856dict_repr(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000857{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000858 Py_ssize_t i;
Tim Petersa7259592001-06-16 05:11:17 +0000859 PyObject *s, *temp, *colon = NULL;
860 PyObject *pieces = NULL, *result = NULL;
861 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +0000862
Tim Petersa7259592001-06-16 05:11:17 +0000863 i = Py_ReprEnter((PyObject *)mp);
Guido van Rossum255443b1998-04-10 22:47:14 +0000864 if (i != 0) {
Tim Petersa7259592001-06-16 05:11:17 +0000865 return i > 0 ? PyString_FromString("{...}") : NULL;
Guido van Rossum255443b1998-04-10 22:47:14 +0000866 }
867
Tim Petersa7259592001-06-16 05:11:17 +0000868 if (mp->ma_used == 0) {
869 result = PyString_FromString("{}");
870 goto Done;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000871 }
Tim Petersa7259592001-06-16 05:11:17 +0000872
873 pieces = PyList_New(0);
874 if (pieces == NULL)
875 goto Done;
876
877 colon = PyString_FromString(": ");
878 if (colon == NULL)
879 goto Done;
880
881 /* Do repr() on each key+value pair, and insert ": " between them.
882 Note that repr may mutate the dict. */
Tim Petersc6057842001-06-16 07:52:53 +0000883 i = 0;
884 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Tim Petersa7259592001-06-16 05:11:17 +0000885 int status;
886 /* Prevent repr from deleting value during key format. */
887 Py_INCREF(value);
888 s = PyObject_Repr(key);
889 PyString_Concat(&s, colon);
890 PyString_ConcatAndDel(&s, PyObject_Repr(value));
891 Py_DECREF(value);
892 if (s == NULL)
893 goto Done;
894 status = PyList_Append(pieces, s);
895 Py_DECREF(s); /* append created a new ref */
896 if (status < 0)
897 goto Done;
898 }
899
900 /* Add "{}" decorations to the first and last items. */
901 assert(PyList_GET_SIZE(pieces) > 0);
902 s = PyString_FromString("{");
903 if (s == NULL)
904 goto Done;
905 temp = PyList_GET_ITEM(pieces, 0);
906 PyString_ConcatAndDel(&s, temp);
907 PyList_SET_ITEM(pieces, 0, s);
908 if (s == NULL)
909 goto Done;
910
911 s = PyString_FromString("}");
912 if (s == NULL)
913 goto Done;
914 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
915 PyString_ConcatAndDel(&temp, s);
916 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
917 if (temp == NULL)
918 goto Done;
919
920 /* Paste them all together with ", " between. */
921 s = PyString_FromString(", ");
922 if (s == NULL)
923 goto Done;
924 result = _PyString_Join(s, pieces);
Tim Peters0ab085c2001-09-14 00:25:33 +0000925 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +0000926
927Done:
928 Py_XDECREF(pieces);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000929 Py_XDECREF(colon);
Tim Petersa7259592001-06-16 05:11:17 +0000930 Py_ReprLeave((PyObject *)mp);
931 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000932}
933
Martin v. Löwis18e16552006-02-15 17:27:45 +0000934static Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +0000935dict_length(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000936{
937 return mp->ma_used;
938}
939
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000940static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000941dict_subscript(dictobject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000942{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000943 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000944 long hash;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000945 dictentry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000946 assert(mp->ma_table != NULL);
Tim Peters0ab085c2001-09-14 00:25:33 +0000947 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +0000948 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000949 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000950 if (hash == -1)
951 return NULL;
952 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000953 ep = (mp->ma_lookup)(mp, key, hash);
954 if (ep == NULL)
955 return NULL;
956 v = ep->me_value;
Guido van Rossum1968ad32006-02-25 22:38:04 +0000957 if (v == NULL) {
958 if (!PyDict_CheckExact(mp)) {
959 /* Look up __missing__ method if we're a subclass. */
Guido van Rossum4b92a822006-02-25 23:32:30 +0000960 PyObject *missing;
Guido van Rossum1968ad32006-02-25 22:38:04 +0000961 static PyObject *missing_str = NULL;
962 if (missing_str == NULL)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000963 missing_str =
Guido van Rossum1968ad32006-02-25 22:38:04 +0000964 PyString_InternFromString("__missing__");
Guido van Rossum4b92a822006-02-25 23:32:30 +0000965 missing = _PyType_Lookup(mp->ob_type, missing_str);
Guido van Rossum1968ad32006-02-25 22:38:04 +0000966 if (missing != NULL)
967 return PyObject_CallFunctionObjArgs(missing,
968 (PyObject *)mp, key, NULL);
969 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000970 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum1968ad32006-02-25 22:38:04 +0000971 return NULL;
972 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000973 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000974 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000975 return v;
976}
977
978static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000979dict_ass_sub(dictobject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000980{
981 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000982 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000983 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000984 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000985}
986
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000987static PyMappingMethods dict_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000988 (lenfunc)dict_length, /*mp_length*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000989 (binaryfunc)dict_subscript, /*mp_subscript*/
990 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000991};
992
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000993static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000994dict_keys(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000995{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000996 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000997 register Py_ssize_t i, j;
Raymond Hettinger43442782004-03-17 21:55:03 +0000998 dictentry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000999 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001000
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001001 again:
1002 n = mp->ma_used;
1003 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001004 if (v == NULL)
1005 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001006 if (n != mp->ma_used) {
1007 /* Durnit. The allocations caused the dict to resize.
1008 * Just start over, this shouldn't normally happen.
1009 */
1010 Py_DECREF(v);
1011 goto again;
1012 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001013 ep = mp->ma_table;
1014 mask = mp->ma_mask;
1015 for (i = 0, j = 0; i <= mask; i++) {
1016 if (ep[i].me_value != NULL) {
1017 PyObject *key = ep[i].me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001018 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001019 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001020 j++;
1021 }
1022 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001023 assert(j == n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001024 return v;
1025}
1026
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001027static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001028dict_values(register dictobject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001029{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001030 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001031 register Py_ssize_t i, j;
Raymond Hettinger43442782004-03-17 21:55:03 +00001032 dictentry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001033 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001034
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001035 again:
1036 n = mp->ma_used;
1037 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001038 if (v == NULL)
1039 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001040 if (n != mp->ma_used) {
1041 /* Durnit. The allocations caused the dict to resize.
1042 * Just start over, this shouldn't normally happen.
1043 */
1044 Py_DECREF(v);
1045 goto again;
1046 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001047 ep = mp->ma_table;
1048 mask = mp->ma_mask;
1049 for (i = 0, j = 0; i <= mask; i++) {
1050 if (ep[i].me_value != NULL) {
1051 PyObject *value = ep[i].me_value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001052 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001053 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001054 j++;
1055 }
1056 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001057 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001058 return v;
1059}
1060
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001061static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001062dict_items(register dictobject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001063{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001064 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001065 register Py_ssize_t i, j, n;
1066 Py_ssize_t mask;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001067 PyObject *item, *key, *value;
Raymond Hettinger43442782004-03-17 21:55:03 +00001068 dictentry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001069
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001070 /* Preallocate the list of tuples, to avoid allocations during
1071 * the loop over the items, which could trigger GC, which
1072 * could resize the dict. :-(
1073 */
1074 again:
1075 n = mp->ma_used;
1076 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001077 if (v == NULL)
1078 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001079 for (i = 0; i < n; i++) {
1080 item = PyTuple_New(2);
1081 if (item == NULL) {
1082 Py_DECREF(v);
1083 return NULL;
1084 }
1085 PyList_SET_ITEM(v, i, item);
1086 }
1087 if (n != mp->ma_used) {
1088 /* Durnit. The allocations caused the dict to resize.
1089 * Just start over, this shouldn't normally happen.
1090 */
1091 Py_DECREF(v);
1092 goto again;
1093 }
1094 /* Nothing we do below makes any function calls. */
Raymond Hettinger43442782004-03-17 21:55:03 +00001095 ep = mp->ma_table;
1096 mask = mp->ma_mask;
1097 for (i = 0, j = 0; i <= mask; i++) {
Raymond Hettinger06905122004-03-19 10:30:00 +00001098 if ((value=ep[i].me_value) != NULL) {
Raymond Hettinger43442782004-03-17 21:55:03 +00001099 key = ep[i].me_key;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001100 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001101 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001102 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001103 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001104 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001105 j++;
1106 }
1107 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001108 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001109 return v;
1110}
1111
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001112static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001113dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001114{
1115 PyObject *seq;
1116 PyObject *value = Py_None;
1117 PyObject *it; /* iter(seq) */
1118 PyObject *key;
1119 PyObject *d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001120 int status;
1121
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001122 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001123 return NULL;
1124
1125 d = PyObject_CallObject(cls, NULL);
1126 if (d == NULL)
1127 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001128
1129 it = PyObject_GetIter(seq);
1130 if (it == NULL){
1131 Py_DECREF(d);
1132 return NULL;
1133 }
1134
1135 for (;;) {
1136 key = PyIter_Next(it);
1137 if (key == NULL) {
1138 if (PyErr_Occurred())
1139 goto Fail;
1140 break;
1141 }
Raymond Hettingere03e5b12002-12-07 08:10:51 +00001142 status = PyObject_SetItem(d, key, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001143 Py_DECREF(key);
1144 if (status < 0)
1145 goto Fail;
1146 }
1147
1148 Py_DECREF(it);
1149 return d;
1150
1151Fail:
1152 Py_DECREF(it);
1153 Py_DECREF(d);
1154 return NULL;
1155}
1156
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001157static int
1158dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001159{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001160 PyObject *arg = NULL;
1161 int result = 0;
1162
1163 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1164 result = -1;
1165
1166 else if (arg != NULL) {
1167 if (PyObject_HasAttrString(arg, "keys"))
1168 result = PyDict_Merge(self, arg, 1);
1169 else
1170 result = PyDict_MergeFromSeq2(self, arg, 1);
1171 }
1172 if (result == 0 && kwds != NULL)
1173 result = PyDict_Merge(self, kwds, 1);
1174 return result;
1175}
1176
1177static PyObject *
1178dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1179{
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001180 if (dict_update_common(self, args, kwds, "update") != -1)
1181 Py_RETURN_NONE;
1182 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001183}
1184
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001185/* Update unconditionally replaces existing items.
1186 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001187 otherwise it leaves existing items unchanged.
1188
1189 PyDict_{Update,Merge} update/merge from a mapping object.
1190
Tim Petersf582b822001-12-11 18:51:08 +00001191 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001192 producing iterable objects of length 2.
1193*/
1194
Tim Petersf582b822001-12-11 18:51:08 +00001195int
Tim Peters1fc240e2001-10-26 05:06:50 +00001196PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1197{
1198 PyObject *it; /* iter(seq2) */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001199 Py_ssize_t i; /* index into seq2 of current element */
Tim Peters1fc240e2001-10-26 05:06:50 +00001200 PyObject *item; /* seq2[i] */
1201 PyObject *fast; /* item as a 2-tuple or 2-list */
1202
1203 assert(d != NULL);
1204 assert(PyDict_Check(d));
1205 assert(seq2 != NULL);
1206
1207 it = PyObject_GetIter(seq2);
1208 if (it == NULL)
1209 return -1;
1210
1211 for (i = 0; ; ++i) {
1212 PyObject *key, *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001213 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001214
1215 fast = NULL;
1216 item = PyIter_Next(it);
1217 if (item == NULL) {
1218 if (PyErr_Occurred())
1219 goto Fail;
1220 break;
1221 }
1222
1223 /* Convert item to sequence, and verify length 2. */
1224 fast = PySequence_Fast(item, "");
1225 if (fast == NULL) {
1226 if (PyErr_ExceptionMatches(PyExc_TypeError))
1227 PyErr_Format(PyExc_TypeError,
1228 "cannot convert dictionary update "
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001229 "sequence element #%zd to a sequence",
Tim Peters1fc240e2001-10-26 05:06:50 +00001230 i);
1231 goto Fail;
1232 }
1233 n = PySequence_Fast_GET_SIZE(fast);
1234 if (n != 2) {
1235 PyErr_Format(PyExc_ValueError,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001236 "dictionary update sequence element #%zd "
Martin v. Löwis2c95cc62006-02-16 06:54:25 +00001237 "has length %zd; 2 is required",
Martin v. Löwise0e89f72006-02-16 06:59:22 +00001238 i, n);
Tim Peters1fc240e2001-10-26 05:06:50 +00001239 goto Fail;
1240 }
1241
1242 /* Update/merge with this (key, value) pair. */
1243 key = PySequence_Fast_GET_ITEM(fast, 0);
1244 value = PySequence_Fast_GET_ITEM(fast, 1);
1245 if (override || PyDict_GetItem(d, key) == NULL) {
1246 int status = PyDict_SetItem(d, key, value);
1247 if (status < 0)
1248 goto Fail;
1249 }
1250 Py_DECREF(fast);
1251 Py_DECREF(item);
1252 }
1253
1254 i = 0;
1255 goto Return;
1256Fail:
1257 Py_XDECREF(item);
1258 Py_XDECREF(fast);
1259 i = -1;
1260Return:
1261 Py_DECREF(it);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001262 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001263}
1264
Tim Peters6d6c1a32001-08-02 04:15:00 +00001265int
1266PyDict_Update(PyObject *a, PyObject *b)
1267{
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001268 return PyDict_Merge(a, b, 1);
1269}
1270
1271int
1272PyDict_Merge(PyObject *a, PyObject *b, int override)
1273{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001274 register PyDictObject *mp, *other;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001275 register Py_ssize_t i;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001276 dictentry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001277
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001278 /* We accept for the argument either a concrete dictionary object,
1279 * or an abstract "mapping" object. For the former, we can do
1280 * things quite efficiently. For the latter, we only require that
1281 * PyMapping_Keys() and PyObject_GetItem() be supported.
1282 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001283 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1284 PyErr_BadInternalCall();
1285 return -1;
1286 }
1287 mp = (dictobject*)a;
1288 if (PyDict_Check(b)) {
1289 other = (dictobject*)b;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001290 if (other == mp || other->ma_used == 0)
1291 /* a.update(a) or a.update({}); nothing to do */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001292 return 0;
Raymond Hettinger186e7392005-05-14 18:08:25 +00001293 if (mp->ma_used == 0)
1294 /* Since the target dict is empty, PyDict_GetItem()
1295 * always returns NULL. Setting override to 1
1296 * skips the unnecessary test.
1297 */
1298 override = 1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001299 /* Do one big resize at the start, rather than
1300 * incrementally resizing as we insert new items. Expect
1301 * that there will be no (or few) overlapping keys.
1302 */
1303 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
Raymond Hettingerc8d22902003-05-07 00:49:40 +00001304 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001305 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001306 }
1307 for (i = 0; i <= other->ma_mask; i++) {
1308 entry = &other->ma_table[i];
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001309 if (entry->me_value != NULL &&
1310 (override ||
1311 PyDict_GetItem(a, entry->me_key) == NULL)) {
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001312 Py_INCREF(entry->me_key);
1313 Py_INCREF(entry->me_value);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001314 if (insertdict(mp, entry->me_key,
1315 (long)entry->me_hash,
1316 entry->me_value) != 0)
1317 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001318 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001319 }
1320 }
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001321 else {
1322 /* Do it the generic, slower way */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001323 PyObject *keys = PyMapping_Keys(b);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001324 PyObject *iter;
1325 PyObject *key, *value;
1326 int status;
1327
1328 if (keys == NULL)
1329 /* Docstring says this is equivalent to E.keys() so
1330 * if E doesn't have a .keys() method we want
1331 * AttributeError to percolate up. Might as well
1332 * do the same for any other error.
1333 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001334 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001335
1336 iter = PyObject_GetIter(keys);
1337 Py_DECREF(keys);
1338 if (iter == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001339 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001340
1341 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001342 if (!override && PyDict_GetItem(a, key) != NULL) {
1343 Py_DECREF(key);
1344 continue;
1345 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001346 value = PyObject_GetItem(b, key);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001347 if (value == NULL) {
1348 Py_DECREF(iter);
1349 Py_DECREF(key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001350 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001351 }
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001352 status = PyDict_SetItem(a, key, value);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001353 Py_DECREF(key);
1354 Py_DECREF(value);
1355 if (status < 0) {
1356 Py_DECREF(iter);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001357 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001358 }
1359 }
1360 Py_DECREF(iter);
1361 if (PyErr_Occurred())
1362 /* Iterator completed, via error */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001363 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001364 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001365 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001366}
1367
1368static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001369dict_copy(register dictobject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001370{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001371 return PyDict_Copy((PyObject*)mp);
1372}
1373
1374PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001375PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001376{
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001377 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001378
1379 if (o == NULL || !PyDict_Check(o)) {
1380 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001381 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001382 }
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001383 copy = PyDict_New();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001384 if (copy == NULL)
1385 return NULL;
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001386 if (PyDict_Merge(copy, o, 1) == 0)
1387 return copy;
1388 Py_DECREF(copy);
Raymond Hettinger1356f782005-04-15 15:58:42 +00001389 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001390}
1391
Martin v. Löwis18e16552006-02-15 17:27:45 +00001392Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001393PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001394{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001395 if (mp == NULL || !PyDict_Check(mp)) {
1396 PyErr_BadInternalCall();
Jeremy Hylton7083bb72004-02-17 20:10:11 +00001397 return -1;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001398 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001399 return ((dictobject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001400}
1401
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001402PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001403PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001404{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001405 if (mp == NULL || !PyDict_Check(mp)) {
1406 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001407 return NULL;
1408 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001409 return dict_keys((dictobject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001410}
1411
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001412PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001413PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001414{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001415 if (mp == NULL || !PyDict_Check(mp)) {
1416 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001417 return NULL;
1418 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001419 return dict_values((dictobject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001420}
1421
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001422PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001423PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001424{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001425 if (mp == NULL || !PyDict_Check(mp)) {
1426 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001427 return NULL;
1428 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001429 return dict_items((dictobject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001430}
1431
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001432/* Subroutine which returns the smallest key in a for which b's value
1433 is different or absent. The value is returned too, through the
Tim Peters95bf9392001-05-10 08:32:44 +00001434 pval argument. Both are NULL if no key in a is found for which b's status
1435 differs. The refcounts on (and only on) non-NULL *pval and function return
1436 values must be decremented by the caller (characterize() increments them
1437 to ensure that mutating comparison and PyDict_GetItem calls can't delete
1438 them before the caller is done looking at them). */
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001439
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001440static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001441characterize(dictobject *a, dictobject *b, PyObject **pval)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001442{
Tim Peters95bf9392001-05-10 08:32:44 +00001443 PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
1444 PyObject *aval = NULL; /* a[akey] */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001445 Py_ssize_t i;
1446 int cmp;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001447
Tim Petersafb6ae82001-06-04 21:00:21 +00001448 for (i = 0; i <= a->ma_mask; i++) {
Tim Peters95bf9392001-05-10 08:32:44 +00001449 PyObject *thiskey, *thisaval, *thisbval;
1450 if (a->ma_table[i].me_value == NULL)
1451 continue;
1452 thiskey = a->ma_table[i].me_key;
1453 Py_INCREF(thiskey); /* keep alive across compares */
1454 if (akey != NULL) {
1455 cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
1456 if (cmp < 0) {
1457 Py_DECREF(thiskey);
1458 goto Fail;
1459 }
1460 if (cmp > 0 ||
Tim Petersafb6ae82001-06-04 21:00:21 +00001461 i > a->ma_mask ||
Tim Peters95bf9392001-05-10 08:32:44 +00001462 a->ma_table[i].me_value == NULL)
1463 {
1464 /* Not the *smallest* a key; or maybe it is
1465 * but the compare shrunk the dict so we can't
1466 * find its associated value anymore; or
1467 * maybe it is but the compare deleted the
1468 * a[thiskey] entry.
1469 */
1470 Py_DECREF(thiskey);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001471 continue;
Guido van Rossumb9324202001-01-18 00:39:02 +00001472 }
Tim Peters95bf9392001-05-10 08:32:44 +00001473 }
1474
1475 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
1476 thisaval = a->ma_table[i].me_value;
1477 assert(thisaval);
1478 Py_INCREF(thisaval); /* keep alive */
1479 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
1480 if (thisbval == NULL)
1481 cmp = 0;
1482 else {
1483 /* both dicts have thiskey: same values? */
1484 cmp = PyObject_RichCompareBool(
1485 thisaval, thisbval, Py_EQ);
1486 if (cmp < 0) {
1487 Py_DECREF(thiskey);
1488 Py_DECREF(thisaval);
1489 goto Fail;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001490 }
1491 }
Tim Peters95bf9392001-05-10 08:32:44 +00001492 if (cmp == 0) {
1493 /* New winner. */
1494 Py_XDECREF(akey);
1495 Py_XDECREF(aval);
1496 akey = thiskey;
1497 aval = thisaval;
1498 }
1499 else {
1500 Py_DECREF(thiskey);
1501 Py_DECREF(thisaval);
1502 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001503 }
Tim Peters95bf9392001-05-10 08:32:44 +00001504 *pval = aval;
1505 return akey;
1506
1507Fail:
1508 Py_XDECREF(akey);
1509 Py_XDECREF(aval);
1510 *pval = NULL;
1511 return NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001512}
1513
1514static int
Tim Peters1f5871e2000-07-04 17:44:48 +00001515dict_compare(dictobject *a, dictobject *b)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001516{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001517 PyObject *adiff, *bdiff, *aval, *bval;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001518 int res;
1519
1520 /* Compare lengths first */
1521 if (a->ma_used < b->ma_used)
1522 return -1; /* a is shorter */
1523 else if (a->ma_used > b->ma_used)
1524 return 1; /* b is shorter */
Tim Peters95bf9392001-05-10 08:32:44 +00001525
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001526 /* Same length -- check all keys */
Tim Peters95bf9392001-05-10 08:32:44 +00001527 bdiff = bval = NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001528 adiff = characterize(a, b, &aval);
Tim Peters95bf9392001-05-10 08:32:44 +00001529 if (adiff == NULL) {
1530 assert(!aval);
Tim Peters3918fb22001-05-10 18:58:31 +00001531 /* Either an error, or a is a subset with the same length so
Tim Peters95bf9392001-05-10 08:32:44 +00001532 * must be equal.
1533 */
1534 res = PyErr_Occurred() ? -1 : 0;
1535 goto Finished;
1536 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001537 bdiff = characterize(b, a, &bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001538 if (bdiff == NULL && PyErr_Occurred()) {
1539 assert(!bval);
1540 res = -1;
1541 goto Finished;
1542 }
1543 res = 0;
1544 if (bdiff) {
1545 /* bdiff == NULL "should be" impossible now, but perhaps
1546 * the last comparison done by the characterize() on a had
1547 * the side effect of making the dicts equal!
1548 */
1549 res = PyObject_Compare(adiff, bdiff);
1550 }
1551 if (res == 0 && bval != NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001552 res = PyObject_Compare(aval, bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001553
1554Finished:
1555 Py_XDECREF(adiff);
1556 Py_XDECREF(bdiff);
1557 Py_XDECREF(aval);
1558 Py_XDECREF(bval);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001559 return res;
1560}
1561
Tim Peterse63415e2001-05-08 04:38:29 +00001562/* Return 1 if dicts equal, 0 if not, -1 if error.
1563 * Gets out as soon as any difference is detected.
1564 * Uses only Py_EQ comparison.
1565 */
1566static int
1567dict_equal(dictobject *a, dictobject *b)
1568{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001569 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00001570
1571 if (a->ma_used != b->ma_used)
1572 /* can't be equal if # of entries differ */
1573 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001574
Tim Peterse63415e2001-05-08 04:38:29 +00001575 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Tim Petersafb6ae82001-06-04 21:00:21 +00001576 for (i = 0; i <= a->ma_mask; i++) {
Tim Peterse63415e2001-05-08 04:38:29 +00001577 PyObject *aval = a->ma_table[i].me_value;
1578 if (aval != NULL) {
1579 int cmp;
1580 PyObject *bval;
1581 PyObject *key = a->ma_table[i].me_key;
1582 /* temporarily bump aval's refcount to ensure it stays
1583 alive until we're done with it */
1584 Py_INCREF(aval);
1585 bval = PyDict_GetItem((PyObject *)b, key);
1586 if (bval == NULL) {
1587 Py_DECREF(aval);
1588 return 0;
1589 }
1590 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1591 Py_DECREF(aval);
1592 if (cmp <= 0) /* error or not equal */
1593 return cmp;
1594 }
1595 }
1596 return 1;
1597 }
1598
1599static PyObject *
1600dict_richcompare(PyObject *v, PyObject *w, int op)
1601{
1602 int cmp;
1603 PyObject *res;
1604
1605 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1606 res = Py_NotImplemented;
1607 }
1608 else if (op == Py_EQ || op == Py_NE) {
1609 cmp = dict_equal((dictobject *)v, (dictobject *)w);
1610 if (cmp < 0)
1611 return NULL;
1612 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1613 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001614 else
1615 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001616 Py_INCREF(res);
1617 return res;
1618 }
1619
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001620static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001621dict_has_key(register dictobject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001622{
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001623 long hash;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001624 dictentry *ep;
1625
Tim Peters0ab085c2001-09-14 00:25:33 +00001626 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001627 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001628 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001629 if (hash == -1)
1630 return NULL;
1631 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001632 ep = (mp->ma_lookup)(mp, key, hash);
1633 if (ep == NULL)
1634 return NULL;
1635 return PyBool_FromLong(ep->me_value != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001636}
1637
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001638static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001639dict_get(register dictobject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001640{
1641 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001642 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001643 PyObject *val = NULL;
1644 long hash;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001645 dictentry *ep;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001646
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001647 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001648 return NULL;
1649
Tim Peters0ab085c2001-09-14 00:25:33 +00001650 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001651 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Barry Warsawc38c5da1997-10-06 17:49:20 +00001652 hash = PyObject_Hash(key);
1653 if (hash == -1)
1654 return NULL;
1655 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001656 ep = (mp->ma_lookup)(mp, key, hash);
1657 if (ep == NULL)
1658 return NULL;
1659 val = ep->me_value;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001660 if (val == NULL)
1661 val = failobj;
1662 Py_INCREF(val);
1663 return val;
1664}
1665
1666
1667static PyObject *
Guido van Rossum164452c2000-08-08 16:12:54 +00001668dict_setdefault(register dictobject *mp, PyObject *args)
1669{
1670 PyObject *key;
1671 PyObject *failobj = Py_None;
1672 PyObject *val = NULL;
1673 long hash;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001674 dictentry *ep;
Guido van Rossum164452c2000-08-08 16:12:54 +00001675
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001676 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
Guido van Rossum164452c2000-08-08 16:12:54 +00001677 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001678
Tim Peters0ab085c2001-09-14 00:25:33 +00001679 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001680 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum164452c2000-08-08 16:12:54 +00001681 hash = PyObject_Hash(key);
1682 if (hash == -1)
1683 return NULL;
1684 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001685 ep = (mp->ma_lookup)(mp, key, hash);
1686 if (ep == NULL)
1687 return NULL;
1688 val = ep->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001689 if (val == NULL) {
1690 val = failobj;
1691 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1692 val = NULL;
1693 }
1694 Py_XINCREF(val);
1695 return val;
1696}
1697
1698
1699static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001700dict_clear(register dictobject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001701{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001702 PyDict_Clear((PyObject *)mp);
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001703 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001704}
1705
Guido van Rossumba6ab842000-12-12 22:02:18 +00001706static PyObject *
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001707dict_pop(dictobject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00001708{
1709 long hash;
1710 dictentry *ep;
1711 PyObject *old_value, *old_key;
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001712 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001713
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001714 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1715 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001716 if (mp->ma_used == 0) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001717 if (deflt) {
1718 Py_INCREF(deflt);
1719 return deflt;
1720 }
Guido van Rossume027d982002-04-12 15:11:59 +00001721 PyErr_SetString(PyExc_KeyError,
1722 "pop(): dictionary is empty");
1723 return NULL;
1724 }
1725 if (!PyString_CheckExact(key) ||
1726 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1727 hash = PyObject_Hash(key);
1728 if (hash == -1)
1729 return NULL;
1730 }
1731 ep = (mp->ma_lookup)(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001732 if (ep == NULL)
1733 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001734 if (ep->me_value == NULL) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001735 if (deflt) {
1736 Py_INCREF(deflt);
1737 return deflt;
1738 }
Guido van Rossume027d982002-04-12 15:11:59 +00001739 PyErr_SetObject(PyExc_KeyError, key);
1740 return NULL;
1741 }
1742 old_key = ep->me_key;
1743 Py_INCREF(dummy);
1744 ep->me_key = dummy;
1745 old_value = ep->me_value;
1746 ep->me_value = NULL;
1747 mp->ma_used--;
1748 Py_DECREF(old_key);
1749 return old_value;
1750}
1751
1752static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001753dict_popitem(dictobject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001754{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001755 Py_ssize_t i = 0;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001756 dictentry *ep;
1757 PyObject *res;
1758
Tim Petersf4b33f62001-06-02 05:42:29 +00001759 /* Allocate the result tuple before checking the size. Believe it
1760 * or not, this allocation could trigger a garbage collection which
1761 * could empty the dict, so if we checked the size first and that
1762 * happened, the result would be an infinite loop (searching for an
1763 * entry that no longer exists). Note that the usual popitem()
1764 * idiom is "while d: k, v = d.popitem()". so needing to throw the
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001765 * tuple away if the dict *is* empty isn't a significant
Tim Petersf4b33f62001-06-02 05:42:29 +00001766 * inefficiency -- possible, but unlikely in practice.
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001767 */
1768 res = PyTuple_New(2);
1769 if (res == NULL)
1770 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001771 if (mp->ma_used == 0) {
1772 Py_DECREF(res);
1773 PyErr_SetString(PyExc_KeyError,
1774 "popitem(): dictionary is empty");
1775 return NULL;
1776 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001777 /* Set ep to "the first" dict entry with a value. We abuse the hash
1778 * field of slot 0 to hold a search finger:
1779 * If slot 0 has a value, use slot 0.
1780 * Else slot 0 is being used to hold a search finger,
1781 * and we use its hash value as the first index to look.
1782 */
1783 ep = &mp->ma_table[0];
1784 if (ep->me_value == NULL) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001785 i = ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001786 /* The hash field may be a real hash value, or it may be a
1787 * legit search finger, or it may be a once-legit search
1788 * finger that's out of bounds now because it wrapped around
1789 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001790 */
Tim Petersafb6ae82001-06-04 21:00:21 +00001791 if (i > mp->ma_mask || i < 1)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001792 i = 1; /* skip slot 0 */
1793 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1794 i++;
Tim Petersafb6ae82001-06-04 21:00:21 +00001795 if (i > mp->ma_mask)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001796 i = 1;
1797 }
1798 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001799 PyTuple_SET_ITEM(res, 0, ep->me_key);
1800 PyTuple_SET_ITEM(res, 1, ep->me_value);
1801 Py_INCREF(dummy);
1802 ep->me_key = dummy;
1803 ep->me_value = NULL;
1804 mp->ma_used--;
1805 assert(mp->ma_table[0].me_value == NULL);
1806 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001807 return res;
1808}
1809
Jeremy Hylton8caad492000-06-23 14:18:11 +00001810static int
1811dict_traverse(PyObject *op, visitproc visit, void *arg)
1812{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001813 Py_ssize_t i = 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001814 PyObject *pk;
1815 PyObject *pv;
1816
1817 while (PyDict_Next(op, &i, &pk, &pv)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001818 Py_VISIT(pk);
1819 Py_VISIT(pv);
Jeremy Hylton8caad492000-06-23 14:18:11 +00001820 }
1821 return 0;
1822}
1823
1824static int
1825dict_tp_clear(PyObject *op)
1826{
1827 PyDict_Clear(op);
1828 return 0;
1829}
1830
Tim Petersf7f88b12000-12-13 23:18:45 +00001831
Raymond Hettinger019a1482004-03-18 02:41:19 +00001832extern PyTypeObject PyDictIterKey_Type; /* Forward */
1833extern PyTypeObject PyDictIterValue_Type; /* Forward */
1834extern PyTypeObject PyDictIterItem_Type; /* Forward */
1835static PyObject *dictiter_new(dictobject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001836
1837static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001838dict_iterkeys(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001839{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001840 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001841}
1842
1843static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001844dict_itervalues(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001845{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001846 return dictiter_new(dict, &PyDictIterValue_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001847}
1848
1849static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001850dict_iteritems(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001851{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001852 return dictiter_new(dict, &PyDictIterItem_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001853}
1854
1855
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001856PyDoc_STRVAR(has_key__doc__,
Raymond Hettinger574aa322003-09-01 22:12:08 +00001857"D.has_key(k) -> True if D has a key k, else False");
Tim Petersf7f88b12000-12-13 23:18:45 +00001858
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001859PyDoc_STRVAR(contains__doc__,
1860"D.__contains__(k) -> True if D has a key k, else False");
1861
1862PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
1863
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001864PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001865"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001866
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001867PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001868"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 +00001869
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001870PyDoc_STRVAR(pop__doc__,
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001871"D.pop(k[,d]) -> v, remove specified key and return the corresponding value\n\
1872If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00001873
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001874PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00001875"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000018762-tuple; but raise KeyError if D is empty");
Tim Petersf7f88b12000-12-13 23:18:45 +00001877
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001878PyDoc_STRVAR(keys__doc__,
1879"D.keys() -> list of D's keys");
Tim Petersf7f88b12000-12-13 23:18:45 +00001880
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001881PyDoc_STRVAR(items__doc__,
1882"D.items() -> list of D's (key, value) pairs, as 2-tuples");
Tim Petersf7f88b12000-12-13 23:18:45 +00001883
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001884PyDoc_STRVAR(values__doc__,
1885"D.values() -> list of D's values");
Tim Petersf7f88b12000-12-13 23:18:45 +00001886
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001887PyDoc_STRVAR(update__doc__,
Walter Dörwaldd70ad8a2004-05-28 20:59:21 +00001888"D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k]\n\
1889(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 +00001890
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001891PyDoc_STRVAR(fromkeys__doc__,
1892"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
1893v defaults to None.");
1894
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001895PyDoc_STRVAR(clear__doc__,
1896"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001897
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001898PyDoc_STRVAR(copy__doc__,
1899"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00001900
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001901PyDoc_STRVAR(iterkeys__doc__,
1902"D.iterkeys() -> an iterator over the keys of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00001903
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001904PyDoc_STRVAR(itervalues__doc__,
1905"D.itervalues() -> an iterator over the values of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00001906
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001907PyDoc_STRVAR(iteritems__doc__,
1908"D.iteritems() -> an iterator over the (key, value) items of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00001909
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001910static PyMethodDef mapp_methods[] = {
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001911 {"__contains__",(PyCFunction)dict_has_key, METH_O | METH_COEXIST,
1912 contains__doc__},
Raymond Hettinger0c669672003-12-13 13:31:55 +00001913 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001914 getitem__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001915 {"has_key", (PyCFunction)dict_has_key, METH_O,
Tim Petersf7f88b12000-12-13 23:18:45 +00001916 has_key__doc__},
1917 {"get", (PyCFunction)dict_get, METH_VARARGS,
1918 get__doc__},
1919 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1920 setdefault_doc__},
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001921 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
Just van Rossuma797d812002-11-23 09:45:04 +00001922 pop__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001923 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001924 popitem__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001925 {"keys", (PyCFunction)dict_keys, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001926 keys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001927 {"items", (PyCFunction)dict_items, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001928 items__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001929 {"values", (PyCFunction)dict_values, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001930 values__doc__},
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001931 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001932 update__doc__},
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001933 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
1934 fromkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001935 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001936 clear__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001937 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001938 copy__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001939 {"iterkeys", (PyCFunction)dict_iterkeys, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00001940 iterkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001941 {"itervalues", (PyCFunction)dict_itervalues, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00001942 itervalues__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001943 {"iteritems", (PyCFunction)dict_iteritems, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00001944 iteritems__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00001945 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001946};
1947
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001948/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001949int
1950PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001951{
1952 long hash;
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001953 dictobject *mp = (dictobject *)op;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001954 dictentry *ep;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001955
Tim Peters0ab085c2001-09-14 00:25:33 +00001956 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001957 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001958 hash = PyObject_Hash(key);
1959 if (hash == -1)
1960 return -1;
1961 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001962 ep = (mp->ma_lookup)(mp, key, hash);
1963 return ep == NULL ? -1 : (ep->me_value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001964}
1965
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001966/* Hack to implement "key in dict" */
1967static PySequenceMethods dict_as_sequence = {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001968 0, /* sq_length */
1969 0, /* sq_concat */
1970 0, /* sq_repeat */
1971 0, /* sq_item */
1972 0, /* sq_slice */
1973 0, /* sq_ass_item */
1974 0, /* sq_ass_slice */
1975 PyDict_Contains, /* sq_contains */
1976 0, /* sq_inplace_concat */
1977 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001978};
1979
Guido van Rossum09e563a2001-05-01 12:10:21 +00001980static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001981dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1982{
1983 PyObject *self;
1984
1985 assert(type != NULL && type->tp_alloc != NULL);
1986 self = type->tp_alloc(type, 0);
1987 if (self != NULL) {
1988 PyDictObject *d = (PyDictObject *)self;
1989 /* It's guaranteed that tp->alloc zeroed out the struct. */
1990 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
1991 INIT_NONZERO_DICT_SLOTS(d);
1992 d->ma_lookup = lookdict_string;
1993#ifdef SHOW_CONVERSION_COUNTS
1994 ++created;
1995#endif
1996 }
1997 return self;
1998}
1999
Tim Peters25786c02001-09-02 08:22:48 +00002000static int
2001dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2002{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002003 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002004}
2005
Guido van Rossumdbb53d92001-12-03 16:32:18 +00002006static long
2007dict_nohash(PyObject *self)
2008{
2009 PyErr_SetString(PyExc_TypeError, "dict objects are unhashable");
2010 return -1;
2011}
2012
Tim Peters6d6c1a32001-08-02 04:15:00 +00002013static PyObject *
Guido van Rossum09e563a2001-05-01 12:10:21 +00002014dict_iter(dictobject *dict)
2015{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002016 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002017}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002018
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002019PyDoc_STRVAR(dictionary_doc,
Tim Petersa427a2b2001-10-29 22:25:45 +00002020"dict() -> new empty dictionary.\n"
2021"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Tim Peters1fc240e2001-10-26 05:06:50 +00002022" (key, value) pairs.\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002023"dict(seq) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002024" d = {}\n"
2025" for k, v in seq:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002026" d[k] = v\n"
2027"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2028" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002029
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002030PyTypeObject PyDict_Type = {
2031 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002032 0,
Tim Petersa427a2b2001-10-29 22:25:45 +00002033 "dict",
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002034 sizeof(dictobject),
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002035 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00002036 (destructor)dict_dealloc, /* tp_dealloc */
2037 (printfunc)dict_print, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002038 0, /* tp_getattr */
Guido van Rossumb9324202001-01-18 00:39:02 +00002039 0, /* tp_setattr */
Tim Peters4fa58bf2001-05-10 21:45:19 +00002040 (cmpfunc)dict_compare, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00002041 (reprfunc)dict_repr, /* tp_repr */
2042 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002043 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00002044 &dict_as_mapping, /* tp_as_mapping */
Guido van Rossumdbb53d92001-12-03 16:32:18 +00002045 dict_nohash, /* tp_hash */
Guido van Rossumb9324202001-01-18 00:39:02 +00002046 0, /* tp_call */
2047 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002048 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumb9324202001-01-18 00:39:02 +00002049 0, /* tp_setattro */
2050 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002051 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Tim Peters6d6c1a32001-08-02 04:15:00 +00002052 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters25786c02001-09-02 08:22:48 +00002053 dictionary_doc, /* tp_doc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002054 dict_traverse, /* tp_traverse */
2055 dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00002056 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002057 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00002058 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002059 0, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002060 mapp_methods, /* tp_methods */
2061 0, /* tp_members */
2062 0, /* tp_getset */
2063 0, /* tp_base */
2064 0, /* tp_dict */
2065 0, /* tp_descr_get */
2066 0, /* tp_descr_set */
2067 0, /* tp_dictoffset */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002068 dict_init, /* tp_init */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002069 PyType_GenericAlloc, /* tp_alloc */
2070 dict_new, /* tp_new */
Neil Schemenauer6189b892002-04-12 02:43:00 +00002071 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002072};
2073
Guido van Rossum3cca2451997-05-16 14:23:33 +00002074/* For backward compatibility with old dictionary interface */
2075
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002076PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002077PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002078{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002079 PyObject *kv, *rv;
2080 kv = PyString_FromString(key);
2081 if (kv == NULL)
2082 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002083 rv = PyDict_GetItem(v, kv);
2084 Py_DECREF(kv);
2085 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002086}
2087
2088int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002089PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002090{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002091 PyObject *kv;
2092 int err;
2093 kv = PyString_FromString(key);
2094 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002095 return -1;
Guido van Rossum4f3bf1e1997-09-29 23:31:11 +00002096 PyString_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00002097 err = PyDict_SetItem(v, kv, item);
2098 Py_DECREF(kv);
2099 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002100}
2101
2102int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002103PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002104{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002105 PyObject *kv;
2106 int err;
2107 kv = PyString_FromString(key);
2108 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002109 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002110 err = PyDict_DelItem(v, kv);
2111 Py_DECREF(kv);
2112 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002113}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002114
Raymond Hettinger019a1482004-03-18 02:41:19 +00002115/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002116
2117typedef struct {
2118 PyObject_HEAD
Guido van Rossum2147df72002-07-16 20:30:22 +00002119 dictobject *di_dict; /* Set to NULL when iterator is exhausted */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002120 Py_ssize_t di_used;
2121 Py_ssize_t di_pos;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002122 PyObject* di_result; /* reusable result tuple for iteritems */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002123 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002124} dictiterobject;
2125
2126static PyObject *
Raymond Hettinger019a1482004-03-18 02:41:19 +00002127dictiter_new(dictobject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002128{
2129 dictiterobject *di;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002130 di = PyObject_New(dictiterobject, itertype);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002131 if (di == NULL)
2132 return NULL;
2133 Py_INCREF(dict);
2134 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00002135 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002136 di->di_pos = 0;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002137 di->len = dict->ma_used;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002138 if (itertype == &PyDictIterItem_Type) {
2139 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2140 if (di->di_result == NULL) {
2141 Py_DECREF(di);
2142 return NULL;
2143 }
2144 }
2145 else
2146 di->di_result = NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002147 return (PyObject *)di;
2148}
2149
2150static void
2151dictiter_dealloc(dictiterobject *di)
2152{
Guido van Rossum2147df72002-07-16 20:30:22 +00002153 Py_XDECREF(di->di_dict);
Raymond Hettinger019a1482004-03-18 02:41:19 +00002154 Py_XDECREF(di->di_result);
Neil Schemenauer6189b892002-04-12 02:43:00 +00002155 PyObject_Del(di);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002156}
2157
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002158static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002159dictiter_len(dictiterobject *di)
2160{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002161 Py_ssize_t len = 0;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00002162 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002163 len = di->len;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002164 return PyInt_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002165}
2166
Armin Rigof5b3e362006-02-11 21:32:43 +00002167PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002168
2169static PyMethodDef dictiter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +00002170 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002171 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002172};
2173
Raymond Hettinger019a1482004-03-18 02:41:19 +00002174static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002175{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002176 PyObject *key;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002177 register Py_ssize_t i, mask;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002178 register dictentry *ep;
2179 dictobject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002180
Raymond Hettinger019a1482004-03-18 02:41:19 +00002181 if (d == NULL)
Guido van Rossum2147df72002-07-16 20:30:22 +00002182 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002183 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002184
Raymond Hettinger019a1482004-03-18 02:41:19 +00002185 if (di->di_used != d->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00002186 PyErr_SetString(PyExc_RuntimeError,
2187 "dictionary changed size during iteration");
Guido van Rossum2147df72002-07-16 20:30:22 +00002188 di->di_used = -1; /* Make this state sticky */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002189 return NULL;
2190 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002191
Raymond Hettinger019a1482004-03-18 02:41:19 +00002192 i = di->di_pos;
2193 if (i < 0)
2194 goto fail;
2195 ep = d->ma_table;
2196 mask = d->ma_mask;
2197 while (i <= mask && ep[i].me_value == NULL)
2198 i++;
2199 di->di_pos = i+1;
2200 if (i > mask)
2201 goto fail;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002202 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002203 key = ep[i].me_key;
2204 Py_INCREF(key);
2205 return key;
2206
2207fail:
2208 Py_DECREF(d);
Guido van Rossum2147df72002-07-16 20:30:22 +00002209 di->di_dict = NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002210 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002211}
2212
Raymond Hettinger019a1482004-03-18 02:41:19 +00002213PyTypeObject PyDictIterKey_Type = {
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002214 PyObject_HEAD_INIT(&PyType_Type)
2215 0, /* ob_size */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002216 "dictionary-keyiterator", /* tp_name */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002217 sizeof(dictiterobject), /* tp_basicsize */
2218 0, /* tp_itemsize */
2219 /* methods */
2220 (destructor)dictiter_dealloc, /* tp_dealloc */
2221 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002222 0, /* tp_getattr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002223 0, /* tp_setattr */
2224 0, /* tp_compare */
2225 0, /* tp_repr */
2226 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002227 0, /* tp_as_sequence */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002228 0, /* tp_as_mapping */
2229 0, /* tp_hash */
2230 0, /* tp_call */
2231 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002232 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002233 0, /* tp_setattro */
2234 0, /* tp_as_buffer */
2235 Py_TPFLAGS_DEFAULT, /* tp_flags */
2236 0, /* tp_doc */
2237 0, /* tp_traverse */
2238 0, /* tp_clear */
2239 0, /* tp_richcompare */
2240 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002241 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002242 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002243 dictiter_methods, /* tp_methods */
2244 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002245};
2246
2247static PyObject *dictiter_iternextvalue(dictiterobject *di)
2248{
2249 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002250 register Py_ssize_t i, mask;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002251 register dictentry *ep;
2252 dictobject *d = di->di_dict;
2253
2254 if (d == NULL)
2255 return NULL;
2256 assert (PyDict_Check(d));
2257
2258 if (di->di_used != d->ma_used) {
2259 PyErr_SetString(PyExc_RuntimeError,
2260 "dictionary changed size during iteration");
2261 di->di_used = -1; /* Make this state sticky */
2262 return NULL;
2263 }
2264
2265 i = di->di_pos;
Guido van Rossum09240f62004-03-20 19:11:58 +00002266 mask = d->ma_mask;
2267 if (i < 0 || i > mask)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002268 goto fail;
2269 ep = d->ma_table;
Guido van Rossum09240f62004-03-20 19:11:58 +00002270 while ((value=ep[i].me_value) == NULL) {
Raymond Hettinger019a1482004-03-18 02:41:19 +00002271 i++;
Guido van Rossum09240f62004-03-20 19:11:58 +00002272 if (i > mask)
2273 goto fail;
2274 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002275 di->di_pos = i+1;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002276 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002277 Py_INCREF(value);
2278 return value;
2279
2280fail:
2281 Py_DECREF(d);
2282 di->di_dict = NULL;
2283 return NULL;
2284}
2285
2286PyTypeObject PyDictIterValue_Type = {
2287 PyObject_HEAD_INIT(&PyType_Type)
2288 0, /* ob_size */
2289 "dictionary-valueiterator", /* tp_name */
2290 sizeof(dictiterobject), /* tp_basicsize */
2291 0, /* tp_itemsize */
2292 /* methods */
2293 (destructor)dictiter_dealloc, /* tp_dealloc */
2294 0, /* tp_print */
2295 0, /* tp_getattr */
2296 0, /* tp_setattr */
2297 0, /* tp_compare */
2298 0, /* tp_repr */
2299 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002300 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002301 0, /* tp_as_mapping */
2302 0, /* tp_hash */
2303 0, /* tp_call */
2304 0, /* tp_str */
2305 PyObject_GenericGetAttr, /* tp_getattro */
2306 0, /* tp_setattro */
2307 0, /* tp_as_buffer */
2308 Py_TPFLAGS_DEFAULT, /* tp_flags */
2309 0, /* tp_doc */
2310 0, /* tp_traverse */
2311 0, /* tp_clear */
2312 0, /* tp_richcompare */
2313 0, /* tp_weaklistoffset */
2314 PyObject_SelfIter, /* tp_iter */
2315 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002316 dictiter_methods, /* tp_methods */
2317 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002318};
2319
2320static PyObject *dictiter_iternextitem(dictiterobject *di)
2321{
2322 PyObject *key, *value, *result = di->di_result;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002323 register Py_ssize_t i, mask;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002324 register dictentry *ep;
2325 dictobject *d = di->di_dict;
2326
2327 if (d == NULL)
2328 return NULL;
2329 assert (PyDict_Check(d));
2330
2331 if (di->di_used != d->ma_used) {
2332 PyErr_SetString(PyExc_RuntimeError,
2333 "dictionary changed size during iteration");
2334 di->di_used = -1; /* Make this state sticky */
2335 return NULL;
2336 }
2337
2338 i = di->di_pos;
2339 if (i < 0)
2340 goto fail;
2341 ep = d->ma_table;
2342 mask = d->ma_mask;
2343 while (i <= mask && ep[i].me_value == NULL)
2344 i++;
2345 di->di_pos = i+1;
2346 if (i > mask)
2347 goto fail;
2348
2349 if (result->ob_refcnt == 1) {
2350 Py_INCREF(result);
2351 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2352 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2353 } else {
2354 result = PyTuple_New(2);
Tim Peters60b29962006-01-01 01:19:23 +00002355 if (result == NULL)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002356 return NULL;
2357 }
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002358 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002359 key = ep[i].me_key;
2360 value = ep[i].me_value;
2361 Py_INCREF(key);
2362 Py_INCREF(value);
2363 PyTuple_SET_ITEM(result, 0, key);
2364 PyTuple_SET_ITEM(result, 1, value);
2365 return result;
2366
2367fail:
2368 Py_DECREF(d);
2369 di->di_dict = NULL;
2370 return NULL;
2371}
2372
2373PyTypeObject PyDictIterItem_Type = {
2374 PyObject_HEAD_INIT(&PyType_Type)
2375 0, /* ob_size */
2376 "dictionary-itemiterator", /* tp_name */
2377 sizeof(dictiterobject), /* tp_basicsize */
2378 0, /* tp_itemsize */
2379 /* methods */
2380 (destructor)dictiter_dealloc, /* tp_dealloc */
2381 0, /* tp_print */
2382 0, /* tp_getattr */
2383 0, /* tp_setattr */
2384 0, /* tp_compare */
2385 0, /* tp_repr */
2386 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002387 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002388 0, /* tp_as_mapping */
2389 0, /* tp_hash */
2390 0, /* tp_call */
2391 0, /* tp_str */
2392 PyObject_GenericGetAttr, /* tp_getattro */
2393 0, /* tp_setattro */
2394 0, /* tp_as_buffer */
2395 Py_TPFLAGS_DEFAULT, /* tp_flags */
2396 0, /* tp_doc */
2397 0, /* tp_traverse */
2398 0, /* tp_clear */
2399 0, /* tp_richcompare */
2400 0, /* tp_weaklistoffset */
2401 PyObject_SelfIter, /* tp_iter */
2402 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002403 dictiter_methods, /* tp_methods */
2404 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002405};