blob: eca86770a657fa61bb4bd9793932619bd5380b70 [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
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Guido van Rossum4b1302b1993-03-27 18:11:32 +00005
Tim Peters6d6c1a32001-08-02 04:15:00 +00006typedef PyDictEntry dictentry;
7typedef PyDictObject dictobject;
Guido van Rossum16e93a81997-01-28 00:00:11 +00008
Tim Peterseb28ef22001-06-02 05:27:19 +00009/* Define this out if you don't want conversion statistics on exit. */
Fred Drake1bff34a2000-08-31 19:31:38 +000010#undef SHOW_CONVERSION_COUNTS
11
Tim Peterseb28ef22001-06-02 05:27:19 +000012/* See large comment block below. This must be >= 1. */
13#define PERTURB_SHIFT 5
14
Guido van Rossum16e93a81997-01-28 00:00:11 +000015/*
Tim Peterseb28ef22001-06-02 05:27:19 +000016Major subtleties ahead: Most hash schemes depend on having a "good" hash
17function, in the sense of simulating randomness. Python doesn't: its most
18important hash functions (for strings and ints) are very regular in common
19cases:
Tim Peters15d49292001-05-27 07:39:22 +000020
Tim Peterseb28ef22001-06-02 05:27:19 +000021>>> map(hash, (0, 1, 2, 3))
22[0, 1, 2, 3]
23>>> map(hash, ("namea", "nameb", "namec", "named"))
24[-1658398457, -1658398460, -1658398459, -1658398462]
25>>>
Tim Peters15d49292001-05-27 07:39:22 +000026
Tim Peterseb28ef22001-06-02 05:27:19 +000027This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
28the low-order i bits as the initial table index is extremely fast, and there
29are no collisions at all for dicts indexed by a contiguous range of ints.
30The same is approximately true when keys are "consecutive" strings. So this
31gives better-than-random behavior in common cases, and that's very desirable.
Tim Peters15d49292001-05-27 07:39:22 +000032
Tim Peterseb28ef22001-06-02 05:27:19 +000033OTOH, when collisions occur, the tendency to fill contiguous slices of the
34hash table makes a good collision resolution strategy crucial. Taking only
35the last i bits of the hash code is also vulnerable: for example, consider
36[i << 16 for i in range(20000)] as a set of keys. Since ints are their own
37hash codes, and this fits in a dict of size 2**15, the last 15 bits of every
38hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +000039
Tim Peterseb28ef22001-06-02 05:27:19 +000040But catering to unusual cases should not slow the usual ones, so we just take
41the last i bits anyway. It's up to collision resolution to do the rest. If
42we *usually* find the key we're looking for on the first try (and, it turns
43out, we usually do -- the table load factor is kept under 2/3, so the odds
44are solidly in our favor), then it makes best sense to keep the initial index
45computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +000046
Tim Peterseb28ef22001-06-02 05:27:19 +000047The first half of collision resolution is to visit table indices via this
48recurrence:
Tim Peters15d49292001-05-27 07:39:22 +000049
Tim Peterseb28ef22001-06-02 05:27:19 +000050 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +000051
Tim Peterseb28ef22001-06-02 05:27:19 +000052For any initial j in range(2**i), repeating that 2**i times generates each
53int in range(2**i) exactly once (see any text on random-number generation for
54proof). By itself, this doesn't help much: like linear probing (setting
55j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
56order. This would be bad, except that's not the only thing we do, and it's
57actually *good* in the common cases where hash keys are consecutive. In an
58example that's really too small to make this entirely clear, for a table of
59size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +000060
Tim Peterseb28ef22001-06-02 05:27:19 +000061 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
62
63If two things come in at index 5, the first place we look after is index 2,
64not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
65Linear probing is deadly in this case because there the fixed probe order
66is the *same* as the order consecutive keys are likely to arrive. But it's
67extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
68and certain that consecutive hash codes do not.
69
70The other half of the strategy is to get the other bits of the hash code
71into play. This is done by initializing a (unsigned) vrbl "perturb" to the
72full hash code, and changing the recurrence to:
73
74 j = (5*j) + 1 + perturb;
75 perturb >>= PERTURB_SHIFT;
76 use j % 2**i as the next table index;
77
78Now the probe sequence depends (eventually) on every bit in the hash code,
79and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
80because it quickly magnifies small differences in the bits that didn't affect
81the initial index. Note that because perturb is unsigned, if the recurrence
82is executed often enough perturb eventually becomes and remains 0. At that
83point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
84that's certain to find an empty slot eventually (since it generates every int
85in range(2**i), and we make sure there's always at least one empty slot).
86
87Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
88small so that the high bits of the hash code continue to affect the probe
89sequence across iterations; but you want it large so that in really bad cases
90the high-order hash bits have an effect on early iterations. 5 was "the
91best" in minimizing total collisions across experiments Tim Peters ran (on
92both normal and pathological cases), but 4 and 6 weren't significantly worse.
93
94Historical: Reimer Behrends contributed the idea of using a polynomial-based
95approach, using repeated multiplication by x in GF(2**n) where an irreducible
96polynomial for each table size was chosen such that x was a primitive root.
97Christian Tismer later extended that to use division by x instead, as an
98efficient way to get the high bits of the hash code into play. This scheme
99also gave excellent collision statistics, but was more expensive: two
100if-tests were required inside the loop; computing "the next" index took about
101the same number of operations but without as much potential parallelism
102(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
103above, and then shifting perturb can be done while the table index is being
104masked); and the dictobject struct required a member to hold the table's
105polynomial. In Tim's experiments the current scheme ran faster, produced
106equally good collision statistics, needed less code & used less memory.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000107*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000108
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000109/* Object used as dummy key to fill deleted entries */
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000110static PyObject *dummy; /* Initialized by first call to newdictobject() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000111
Fred Drake1bff34a2000-08-31 19:31:38 +0000112/* forward declarations */
113static dictentry *
114lookdict_string(dictobject *mp, PyObject *key, long hash);
115
116#ifdef SHOW_CONVERSION_COUNTS
117static long created = 0L;
118static long converted = 0L;
119
120static void
121show_counts(void)
122{
123 fprintf(stderr, "created %ld string dicts\n", created);
124 fprintf(stderr, "converted %ld to normal dicts\n", converted);
125 fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created);
126}
127#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000128
Tim Peters6d6c1a32001-08-02 04:15:00 +0000129/* Initialization macros.
130 There are two ways to create a dict: PyDict_New() is the main C API
131 function, and the tp_new slot maps to dict_new(). In the latter case we
132 can save a little time over what PyDict_New does because it's guaranteed
133 that the PyDictObject struct is already zeroed out.
134 Everyone except dict_new() should use EMPTY_TO_MINSIZE (unless they have
135 an excellent reason not to).
136*/
137
138#define INIT_NONZERO_DICT_SLOTS(mp) do { \
Tim Petersdea48ec2001-05-22 20:40:22 +0000139 (mp)->ma_table = (mp)->ma_smalltable; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000140 (mp)->ma_mask = PyDict_MINSIZE - 1; \
141 } while(0)
142
143#define EMPTY_TO_MINSIZE(mp) do { \
144 memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000145 (mp)->ma_used = (mp)->ma_fill = 0; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000146 INIT_NONZERO_DICT_SLOTS(mp); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000147 } while(0)
148
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000149PyObject *
Thomas Wouters78890102000-07-22 19:25:51 +0000150PyDict_New(void)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000151{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000152 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000153 if (dummy == NULL) { /* Auto-initialize dummy */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000154 dummy = PyString_FromString("<dummy key>");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000155 if (dummy == NULL)
156 return NULL;
Fred Drake1bff34a2000-08-31 19:31:38 +0000157#ifdef SHOW_CONVERSION_COUNTS
158 Py_AtExit(show_counts);
159#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000160 }
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000161 mp = PyObject_GC_New(dictobject, &PyDict_Type);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000162 if (mp == NULL)
163 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000164 EMPTY_TO_MINSIZE(mp);
Fred Drake1bff34a2000-08-31 19:31:38 +0000165 mp->ma_lookup = lookdict_string;
166#ifdef SHOW_CONVERSION_COUNTS
167 ++created;
168#endif
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000169 _PyObject_GC_TRACK(mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000170 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000171}
172
173/*
174The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000175This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000176Open addressing is preferred over chaining since the link overhead for
177chaining would be substantial (100% with typical malloc overhead).
178
Tim Peterseb28ef22001-06-02 05:27:19 +0000179The initial probe index is computed as hash mod the table size. Subsequent
180probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000181
182All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000183
Tim Peterseb28ef22001-06-02 05:27:19 +0000184(The details in this version are due to Tim Peters, building on many past
185contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
186Christian Tismer).
Fred Drake1bff34a2000-08-31 19:31:38 +0000187
188This function must never return NULL; failures are indicated by returning
189a dictentry* for which the me_value field is NULL. Exceptions are never
190reported by this function, and outstanding exceptions are maintained.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000191*/
Tim Peterseb28ef22001-06-02 05:27:19 +0000192
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000193static dictentry *
Tim Peters1f5871e2000-07-04 17:44:48 +0000194lookdict(dictobject *mp, PyObject *key, register long hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000195{
Guido van Rossum16e93a81997-01-28 00:00:11 +0000196 register int i;
Tim Peterseb28ef22001-06-02 05:27:19 +0000197 register unsigned int perturb;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000198 register dictentry *freeslot;
Tim Petersafb6ae82001-06-04 21:00:21 +0000199 register unsigned int mask = mp->ma_mask;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000200 dictentry *ep0 = mp->ma_table;
201 register dictentry *ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000202 register int restore_error;
203 register int checked_error;
Fred Drakec88b99c2000-08-31 19:04:07 +0000204 register int cmp;
205 PyObject *err_type, *err_value, *err_tb;
Tim Peters453163d2001-06-03 04:54:32 +0000206 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000207
Tim Peters2f228e72001-05-13 00:19:31 +0000208 i = hash & mask;
Guido van Rossumefb46091997-01-29 15:53:56 +0000209 ep = &ep0[i];
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000210 if (ep->me_key == NULL || ep->me_key == key)
Guido van Rossum7d181591997-01-16 21:06:45 +0000211 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000212
213 restore_error = checked_error = 0;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000214 if (ep->me_key == dummy)
Guido van Rossum7d181591997-01-16 21:06:45 +0000215 freeslot = ep;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000216 else {
Fred Drakec88b99c2000-08-31 19:04:07 +0000217 if (ep->me_hash == hash) {
218 /* error can't have been checked yet */
219 checked_error = 1;
220 if (PyErr_Occurred()) {
221 restore_error = 1;
222 PyErr_Fetch(&err_type, &err_value, &err_tb);
223 }
Tim Peters453163d2001-06-03 04:54:32 +0000224 startkey = ep->me_key;
225 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000226 if (cmp < 0)
Guido van Rossumb9324202001-01-18 00:39:02 +0000227 PyErr_Clear();
Tim Peters453163d2001-06-03 04:54:32 +0000228 if (ep0 == mp->ma_table && ep->me_key == startkey) {
229 if (cmp > 0)
230 goto Done;
231 }
232 else {
233 /* The compare did major nasty stuff to the
234 * dict: start over.
235 * XXX A clever adversary could prevent this
236 * XXX from terminating.
237 */
238 ep = lookdict(mp, key, hash);
239 goto Done;
240 }
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000241 }
242 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000243 }
Tim Peters15d49292001-05-27 07:39:22 +0000244
Tim Peters342c65e2001-05-13 06:43:53 +0000245 /* In the loop, me_key == dummy is by far (factor of 100s) the
246 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000247 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
248 i = (i << 2) + i + perturb + 1;
249 ep = &ep0[i & mask];
Guido van Rossum16e93a81997-01-28 00:00:11 +0000250 if (ep->me_key == NULL) {
Tim Peters7b5d0af2001-06-03 04:14:43 +0000251 if (freeslot != NULL)
252 ep = freeslot;
253 break;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000254 }
Tim Peters7b5d0af2001-06-03 04:14:43 +0000255 if (ep->me_key == key)
256 break;
257 if (ep->me_hash == hash && ep->me_key != dummy) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000258 if (!checked_error) {
259 checked_error = 1;
260 if (PyErr_Occurred()) {
261 restore_error = 1;
262 PyErr_Fetch(&err_type, &err_value,
263 &err_tb);
264 }
265 }
Tim Peters453163d2001-06-03 04:54:32 +0000266 startkey = ep->me_key;
267 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000268 if (cmp < 0)
Guido van Rossumb9324202001-01-18 00:39:02 +0000269 PyErr_Clear();
Tim Peters453163d2001-06-03 04:54:32 +0000270 if (ep0 == mp->ma_table && ep->me_key == startkey) {
271 if (cmp > 0)
272 break;
273 }
274 else {
275 /* The compare did major nasty stuff to the
276 * dict: start over.
277 * XXX A clever adversary could prevent this
278 * XXX from terminating.
279 */
280 ep = lookdict(mp, key, hash);
281 break;
282 }
Guido van Rossum16e93a81997-01-28 00:00:11 +0000283 }
Tim Peters342c65e2001-05-13 06:43:53 +0000284 else if (ep->me_key == dummy && freeslot == NULL)
285 freeslot = ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000286 }
Tim Peters7b5d0af2001-06-03 04:14:43 +0000287
288Done:
289 if (restore_error)
290 PyErr_Restore(err_type, err_value, err_tb);
291 return ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000292}
293
294/*
Fred Drake1bff34a2000-08-31 19:31:38 +0000295 * Hacked up version of lookdict which can assume keys are always strings;
296 * this assumption allows testing for errors during PyObject_Compare() to
297 * be dropped; string-string comparisons never raise exceptions. This also
298 * means we don't need to go through PyObject_Compare(); we can always use
Martin v. Löwiscd353062001-05-24 16:56:35 +0000299 * _PyString_Eq directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000300 *
Tim Peters0ab085c2001-09-14 00:25:33 +0000301 * This is valuable because the general-case error handling in lookdict() is
302 * expensive, and dicts with pure-string keys are very common.
Fred Drake1bff34a2000-08-31 19:31:38 +0000303 */
304static dictentry *
305lookdict_string(dictobject *mp, PyObject *key, register long hash)
306{
307 register int i;
Tim Peterseb28ef22001-06-02 05:27:19 +0000308 register unsigned int perturb;
Fred Drake1bff34a2000-08-31 19:31:38 +0000309 register dictentry *freeslot;
Tim Petersafb6ae82001-06-04 21:00:21 +0000310 register unsigned int mask = mp->ma_mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000311 dictentry *ep0 = mp->ma_table;
312 register dictentry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000313
Tim Peters0ab085c2001-09-14 00:25:33 +0000314 /* Make sure this function doesn't have to handle non-string keys,
315 including subclasses of str; e.g., one reason to subclass
316 strings is to override __eq__, and for speed we don't cater to
317 that here. */
318 if (!PyString_CheckExact(key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000319#ifdef SHOW_CONVERSION_COUNTS
320 ++converted;
321#endif
322 mp->ma_lookup = lookdict;
323 return lookdict(mp, key, hash);
324 }
Tim Peters2f228e72001-05-13 00:19:31 +0000325 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000326 ep = &ep0[i];
327 if (ep->me_key == NULL || ep->me_key == key)
328 return ep;
329 if (ep->me_key == dummy)
330 freeslot = ep;
331 else {
332 if (ep->me_hash == hash
Martin v. Löwiscd353062001-05-24 16:56:35 +0000333 && _PyString_Eq(ep->me_key, key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000334 return ep;
335 }
336 freeslot = NULL;
337 }
Tim Peters15d49292001-05-27 07:39:22 +0000338
Tim Peters342c65e2001-05-13 06:43:53 +0000339 /* In the loop, me_key == dummy is by far (factor of 100s) the
340 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000341 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
342 i = (i << 2) + i + perturb + 1;
343 ep = &ep0[i & mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000344 if (ep->me_key == NULL)
345 return freeslot == NULL ? ep : freeslot;
346 if (ep->me_key == key
347 || (ep->me_hash == hash
348 && ep->me_key != dummy
Martin v. Löwiscd353062001-05-24 16:56:35 +0000349 && _PyString_Eq(ep->me_key, key)))
Fred Drake1bff34a2000-08-31 19:31:38 +0000350 return ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000351 if (ep->me_key == dummy && freeslot == NULL)
Tim Peters342c65e2001-05-13 06:43:53 +0000352 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000353 }
354}
355
356/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000357Internal routine to insert a new item into the table.
358Used both by the internal resize routine and by the public insert routine.
359Eats a reference to key and one to value.
360*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000361static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000362insertdict(register dictobject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000363{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000364 PyObject *old_value;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000365 register dictentry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000366 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
367
368 assert(mp->ma_lookup != NULL);
369 ep = mp->ma_lookup(mp, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000370 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000371 old_value = ep->me_value;
372 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000373 Py_DECREF(old_value); /* which **CAN** re-enter */
374 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000375 }
376 else {
377 if (ep->me_key == NULL)
378 mp->ma_fill++;
379 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000380 Py_DECREF(ep->me_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000381 ep->me_key = key;
382 ep->me_hash = hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000383 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000384 mp->ma_used++;
385 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000386}
387
388/*
389Restructure the table by allocating a new table and reinserting all
390items again. When entries have been deleted, the new table may
391actually be smaller than the old one.
392*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000393static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000394dictresize(dictobject *mp, int minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000395{
Tim Peterseb28ef22001-06-02 05:27:19 +0000396 int newsize;
Tim Peters0c6010b2001-05-23 23:33:57 +0000397 dictentry *oldtable, *newtable, *ep;
398 int i;
399 int is_oldtable_malloced;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000400 dictentry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000401
402 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000403
Tim Peterseb28ef22001-06-02 05:27:19 +0000404 /* Find the smallest table size > minused. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000405 for (newsize = PyDict_MINSIZE;
Tim Peterseb28ef22001-06-02 05:27:19 +0000406 newsize <= minused && newsize > 0;
407 newsize <<= 1)
408 ;
409 if (newsize <= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000410 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000411 return -1;
412 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000413
414 /* Get space for a new table. */
415 oldtable = mp->ma_table;
416 assert(oldtable != NULL);
417 is_oldtable_malloced = oldtable != mp->ma_smalltable;
418
Tim Peters6d6c1a32001-08-02 04:15:00 +0000419 if (newsize == PyDict_MINSIZE) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000420 /* A large table is shrinking, or we can't get any smaller. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000421 newtable = mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000422 if (newtable == oldtable) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000423 if (mp->ma_fill == mp->ma_used) {
424 /* No dummies, so no point doing anything. */
Tim Peters0c6010b2001-05-23 23:33:57 +0000425 return 0;
Tim Petersf8a548c2001-05-24 16:26:40 +0000426 }
427 /* We're not going to resize it, but rebuild the
428 table anyway to purge old dummy entries.
429 Subtle: This is *necessary* if fill==size,
430 as lookdict needs at least one virgin slot to
431 terminate failing searches. If fill < size, it's
432 merely desirable, as dummies slow searches. */
433 assert(mp->ma_fill > mp->ma_used);
Tim Peters0c6010b2001-05-23 23:33:57 +0000434 memcpy(small_copy, oldtable, sizeof(small_copy));
435 oldtable = small_copy;
436 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000437 }
438 else {
439 newtable = PyMem_NEW(dictentry, newsize);
440 if (newtable == NULL) {
441 PyErr_NoMemory();
442 return -1;
443 }
444 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000445
446 /* Make the dict empty, using the new table. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000447 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000448 mp->ma_table = newtable;
Tim Petersafb6ae82001-06-04 21:00:21 +0000449 mp->ma_mask = newsize - 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000450 memset(newtable, 0, sizeof(dictentry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000451 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000452 i = mp->ma_fill;
453 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000454
Tim Peters19283142001-05-17 22:25:34 +0000455 /* Copy the data over; this is refcount-neutral for active entries;
456 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000457 for (ep = oldtable; i > 0; ep++) {
458 if (ep->me_value != NULL) { /* active entry */
459 --i;
Tim Peters19283142001-05-17 22:25:34 +0000460 insertdict(mp, ep->me_key, ep->me_hash, ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000461 }
Tim Peters19283142001-05-17 22:25:34 +0000462 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000463 --i;
Tim Peters19283142001-05-17 22:25:34 +0000464 assert(ep->me_key == dummy);
465 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000466 }
Tim Peters19283142001-05-17 22:25:34 +0000467 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000468 }
469
Tim Peters0c6010b2001-05-23 23:33:57 +0000470 if (is_oldtable_malloced)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000471 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000472 return 0;
473}
474
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000475PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000476PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000477{
478 long hash;
Fred Drake1bff34a2000-08-31 19:31:38 +0000479 dictobject *mp = (dictobject *)op;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000480 if (!PyDict_Check(op)) {
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000481 return NULL;
482 }
Tim Peters0ab085c2001-09-14 00:25:33 +0000483 if (!PyString_CheckExact(key) ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000484 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Guido van Rossumca756f21997-01-23 19:39:29 +0000485 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000486 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000487 if (hash == -1) {
488 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000489 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000490 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000491 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000492 return (mp->ma_lookup)(mp, key, hash)->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000493}
494
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000495/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
496 * dictionary if it is merely replacing the value for an existing key.
497 * This is means that it's safe to loop over a dictionary with
498 * PyDict_Next() and occasionally replace a value -- but you can't
499 * insert new keys or remove them.
500 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000501int
Tim Peters1f5871e2000-07-04 17:44:48 +0000502PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000503{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000504 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000505 register long hash;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000506 register int n_used;
507
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000508 if (!PyDict_Check(op)) {
509 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000510 return -1;
511 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000512 mp = (dictobject *)op;
Tim Peters0ab085c2001-09-14 00:25:33 +0000513 if (PyString_CheckExact(key)) {
Guido van Rossum45ec02a2002-08-19 21:43:18 +0000514 hash = ((PyStringObject *)key)->ob_shash;
515 if (hash == -1)
516 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000517 }
Tim Peters1f7df352002-03-29 03:29:08 +0000518 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000519 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000520 if (hash == -1)
521 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000522 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000523 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000524 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000525 Py_INCREF(value);
526 Py_INCREF(key);
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000527 insertdict(mp, key, hash, value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000528 /* If we added a key, we can safely resize. Otherwise skip this!
529 * If fill >= 2/3 size, adjust size. Normally, this doubles the
530 * size, but it's also possible for the dict to shrink (if ma_fill is
531 * much larger than ma_used, meaning a lot of dict keys have been
532 * deleted).
533 */
Tim Petersafb6ae82001-06-04 21:00:21 +0000534 if (mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2) {
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000535 if (dictresize(mp, mp->ma_used*2) != 0)
536 return -1;
537 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000538 return 0;
539}
540
541int
Tim Peters1f5871e2000-07-04 17:44:48 +0000542PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000543{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000544 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000545 register long hash;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000546 register dictentry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000547 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000548
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000549 if (!PyDict_Check(op)) {
550 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000551 return -1;
552 }
Tim Peters0ab085c2001-09-14 00:25:33 +0000553 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +0000554 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000555 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000556 if (hash == -1)
557 return -1;
558 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000559 mp = (dictobject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000560 ep = (mp->ma_lookup)(mp, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000561 if (ep->me_value == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000562 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000563 return -1;
564 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000565 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000566 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000567 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000568 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000569 ep->me_value = NULL;
570 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000571 Py_DECREF(old_value);
572 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000573 return 0;
574}
575
Guido van Rossum25831651993-05-19 14:50:45 +0000576void
Tim Peters1f5871e2000-07-04 17:44:48 +0000577PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000578{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000579 dictobject *mp;
Tim Petersdea48ec2001-05-22 20:40:22 +0000580 dictentry *ep, *table;
581 int table_is_malloced;
582 int fill;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000583 dictentry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000584#ifdef Py_DEBUG
585 int i, n;
586#endif
587
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000588 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000589 return;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000590 mp = (dictobject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000591#ifdef Py_DEBUG
Tim Petersafb6ae82001-06-04 21:00:21 +0000592 n = mp->ma_mask + 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000593 i = 0;
594#endif
595
596 table = mp->ma_table;
597 assert(table != NULL);
598 table_is_malloced = table != mp->ma_smalltable;
599
600 /* This is delicate. During the process of clearing the dict,
601 * decrefs can cause the dict to mutate. To avoid fatal confusion
602 * (voice of experience), we have to make the dict empty before
603 * clearing the slots, and never refer to anything via mp->xxx while
604 * clearing.
605 */
606 fill = mp->ma_fill;
607 if (table_is_malloced)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000608 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000609
610 else if (fill > 0) {
611 /* It's a small table with something that needs to be cleared.
612 * Afraid the only safe way is to copy the dict entries into
613 * another small table first.
614 */
615 memcpy(small_copy, table, sizeof(small_copy));
616 table = small_copy;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000617 EMPTY_TO_MINSIZE(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000618 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000619 /* else it's a small table that's already empty */
620
621 /* Now we can finally clear things. If C had refcounts, we could
622 * assert that the refcount on table is 1 now, i.e. that this function
623 * has unique access to it, so decref side-effects can't alter it.
624 */
625 for (ep = table; fill > 0; ++ep) {
626#ifdef Py_DEBUG
627 assert(i < n);
628 ++i;
629#endif
630 if (ep->me_key) {
631 --fill;
632 Py_DECREF(ep->me_key);
633 Py_XDECREF(ep->me_value);
634 }
635#ifdef Py_DEBUG
636 else
637 assert(ep->me_value == NULL);
638#endif
639 }
640
641 if (table_is_malloced)
642 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000643}
644
Tim Peters67830702001-03-21 19:23:56 +0000645/* CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
646 * mutates the dict. One exception: it is safe if the loop merely changes
647 * the values associated with the keys (but doesn't insert new keys or
648 * delete keys), via PyDict_SetItem().
649 */
Guido van Rossum25831651993-05-19 14:50:45 +0000650int
Tim Peters1f5871e2000-07-04 17:44:48 +0000651PyDict_Next(PyObject *op, int *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000652{
Guido van Rossum25831651993-05-19 14:50:45 +0000653 int i;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000654 register dictobject *mp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000655 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000656 return 0;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000657 mp = (dictobject *)op;
Guido van Rossum25831651993-05-19 14:50:45 +0000658 i = *ppos;
659 if (i < 0)
660 return 0;
Tim Petersafb6ae82001-06-04 21:00:21 +0000661 while (i <= mp->ma_mask && mp->ma_table[i].me_value == NULL)
Guido van Rossum25831651993-05-19 14:50:45 +0000662 i++;
663 *ppos = i+1;
Tim Petersafb6ae82001-06-04 21:00:21 +0000664 if (i > mp->ma_mask)
Guido van Rossum25831651993-05-19 14:50:45 +0000665 return 0;
666 if (pkey)
667 *pkey = mp->ma_table[i].me_key;
668 if (pvalue)
669 *pvalue = mp->ma_table[i].me_value;
670 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000671}
672
673/* Methods */
674
675static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000676dict_dealloc(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000677{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000678 register dictentry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000679 int fill = mp->ma_fill;
Guido van Rossumff413af2002-03-28 20:34:59 +0000680 PyObject_GC_UnTrack(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000681 Py_TRASHCAN_SAFE_BEGIN(mp)
Tim Petersdea48ec2001-05-22 20:40:22 +0000682 for (ep = mp->ma_table; fill > 0; ep++) {
683 if (ep->me_key) {
684 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000685 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000686 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000687 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000688 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000689 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000690 PyMem_DEL(mp->ma_table);
Guido van Rossum9475a232001-10-05 20:51:39 +0000691 mp->ob_type->tp_free((PyObject *)mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000692 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000693}
694
695static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000696dict_print(register dictobject *mp, register FILE *fp, register int flags)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000697{
698 register int i;
699 register int any;
Guido van Rossum255443b1998-04-10 22:47:14 +0000700
701 i = Py_ReprEnter((PyObject*)mp);
702 if (i != 0) {
703 if (i < 0)
704 return i;
705 fprintf(fp, "{...}");
706 return 0;
707 }
708
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000709 fprintf(fp, "{");
710 any = 0;
Tim Petersafb6ae82001-06-04 21:00:21 +0000711 for (i = 0; i <= mp->ma_mask; i++) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000712 dictentry *ep = mp->ma_table + i;
713 PyObject *pvalue = ep->me_value;
714 if (pvalue != NULL) {
715 /* Prevent PyObject_Repr from deleting value during
716 key format */
717 Py_INCREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000718 if (any++ > 0)
719 fprintf(fp, ", ");
Guido van Rossum255443b1998-04-10 22:47:14 +0000720 if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000721 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000722 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000723 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000724 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000725 fprintf(fp, ": ");
Tim Peters19b77cf2001-06-02 08:27:39 +0000726 if (PyObject_Print(pvalue, fp, 0) != 0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000727 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000728 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000729 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000730 }
Tim Peters23cf6be2001-06-02 08:02:56 +0000731 Py_DECREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000732 }
733 }
734 fprintf(fp, "}");
Guido van Rossum255443b1998-04-10 22:47:14 +0000735 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000736 return 0;
737}
738
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000739static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000740dict_repr(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000741{
Tim Petersc6057842001-06-16 07:52:53 +0000742 int i;
Tim Petersa7259592001-06-16 05:11:17 +0000743 PyObject *s, *temp, *colon = NULL;
744 PyObject *pieces = NULL, *result = NULL;
745 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +0000746
Tim Petersa7259592001-06-16 05:11:17 +0000747 i = Py_ReprEnter((PyObject *)mp);
Guido van Rossum255443b1998-04-10 22:47:14 +0000748 if (i != 0) {
Tim Petersa7259592001-06-16 05:11:17 +0000749 return i > 0 ? PyString_FromString("{...}") : NULL;
Guido van Rossum255443b1998-04-10 22:47:14 +0000750 }
751
Tim Petersa7259592001-06-16 05:11:17 +0000752 if (mp->ma_used == 0) {
753 result = PyString_FromString("{}");
754 goto Done;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000755 }
Tim Petersa7259592001-06-16 05:11:17 +0000756
757 pieces = PyList_New(0);
758 if (pieces == NULL)
759 goto Done;
760
761 colon = PyString_FromString(": ");
762 if (colon == NULL)
763 goto Done;
764
765 /* Do repr() on each key+value pair, and insert ": " between them.
766 Note that repr may mutate the dict. */
Tim Petersc6057842001-06-16 07:52:53 +0000767 i = 0;
768 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Tim Petersa7259592001-06-16 05:11:17 +0000769 int status;
770 /* Prevent repr from deleting value during key format. */
771 Py_INCREF(value);
772 s = PyObject_Repr(key);
773 PyString_Concat(&s, colon);
774 PyString_ConcatAndDel(&s, PyObject_Repr(value));
775 Py_DECREF(value);
776 if (s == NULL)
777 goto Done;
778 status = PyList_Append(pieces, s);
779 Py_DECREF(s); /* append created a new ref */
780 if (status < 0)
781 goto Done;
782 }
783
784 /* Add "{}" decorations to the first and last items. */
785 assert(PyList_GET_SIZE(pieces) > 0);
786 s = PyString_FromString("{");
787 if (s == NULL)
788 goto Done;
789 temp = PyList_GET_ITEM(pieces, 0);
790 PyString_ConcatAndDel(&s, temp);
791 PyList_SET_ITEM(pieces, 0, s);
792 if (s == NULL)
793 goto Done;
794
795 s = PyString_FromString("}");
796 if (s == NULL)
797 goto Done;
798 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
799 PyString_ConcatAndDel(&temp, s);
800 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
801 if (temp == NULL)
802 goto Done;
803
804 /* Paste them all together with ", " between. */
805 s = PyString_FromString(", ");
806 if (s == NULL)
807 goto Done;
808 result = _PyString_Join(s, pieces);
Tim Peters0ab085c2001-09-14 00:25:33 +0000809 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +0000810
811Done:
812 Py_XDECREF(pieces);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000813 Py_XDECREF(colon);
Tim Petersa7259592001-06-16 05:11:17 +0000814 Py_ReprLeave((PyObject *)mp);
815 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000816}
817
818static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000819dict_length(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000820{
821 return mp->ma_used;
822}
823
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000824static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000825dict_subscript(dictobject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000826{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000827 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000828 long hash;
Tim Petersdea48ec2001-05-22 20:40:22 +0000829 assert(mp->ma_table != NULL);
Tim Peters0ab085c2001-09-14 00:25:33 +0000830 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +0000831 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000832 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000833 if (hash == -1)
834 return NULL;
835 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000836 v = (mp->ma_lookup)(mp, key, hash) -> me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000837 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000838 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000839 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000840 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000841 return v;
842}
843
844static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000845dict_ass_sub(dictobject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000846{
847 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000848 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000849 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000850 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000851}
852
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000853static PyMappingMethods dict_as_mapping = {
854 (inquiry)dict_length, /*mp_length*/
855 (binaryfunc)dict_subscript, /*mp_subscript*/
856 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000857};
858
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000859static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000860dict_keys(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000861{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000862 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000863 register int i, j, n;
864
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000865 again:
866 n = mp->ma_used;
867 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000868 if (v == NULL)
869 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000870 if (n != mp->ma_used) {
871 /* Durnit. The allocations caused the dict to resize.
872 * Just start over, this shouldn't normally happen.
873 */
874 Py_DECREF(v);
875 goto again;
876 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000877 for (i = 0, j = 0; i <= mp->ma_mask; i++) {
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000878 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000879 PyObject *key = mp->ma_table[i].me_key;
880 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000881 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000882 j++;
883 }
884 }
885 return v;
886}
887
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000888static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000889dict_values(register dictobject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +0000890{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000891 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000892 register int i, j, n;
893
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000894 again:
895 n = mp->ma_used;
896 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +0000897 if (v == NULL)
898 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000899 if (n != mp->ma_used) {
900 /* Durnit. The allocations caused the dict to resize.
901 * Just start over, this shouldn't normally happen.
902 */
903 Py_DECREF(v);
904 goto again;
905 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000906 for (i = 0, j = 0; i <= mp->ma_mask; i++) {
Guido van Rossum25831651993-05-19 14:50:45 +0000907 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000908 PyObject *value = mp->ma_table[i].me_value;
909 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000910 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +0000911 j++;
912 }
913 }
914 return v;
915}
916
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000917static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000918dict_items(register dictobject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +0000919{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000920 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000921 register int i, j, n;
922 PyObject *item, *key, *value;
923
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000924 /* Preallocate the list of tuples, to avoid allocations during
925 * the loop over the items, which could trigger GC, which
926 * could resize the dict. :-(
927 */
928 again:
929 n = mp->ma_used;
930 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +0000931 if (v == NULL)
932 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000933 for (i = 0; i < n; i++) {
934 item = PyTuple_New(2);
935 if (item == NULL) {
936 Py_DECREF(v);
937 return NULL;
938 }
939 PyList_SET_ITEM(v, i, item);
940 }
941 if (n != mp->ma_used) {
942 /* Durnit. The allocations caused the dict to resize.
943 * Just start over, this shouldn't normally happen.
944 */
945 Py_DECREF(v);
946 goto again;
947 }
948 /* Nothing we do below makes any function calls. */
Tim Petersafb6ae82001-06-04 21:00:21 +0000949 for (i = 0, j = 0; i <= mp->ma_mask; i++) {
Guido van Rossum25831651993-05-19 14:50:45 +0000950 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000951 key = mp->ma_table[i].me_key;
952 value = mp->ma_table[i].me_value;
953 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000954 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000955 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000956 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000957 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +0000958 j++;
959 }
960 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000961 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +0000962 return v;
963}
964
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000965static PyObject *
Raymond Hettingere33d3df2002-11-27 07:29:33 +0000966dict_fromkeys(PyObject *mp, PyObject *args)
967{
968 PyObject *seq;
969 PyObject *value = Py_None;
970 PyObject *it; /* iter(seq) */
971 PyObject *key;
972 PyObject *d;
973 PyObject *cls;
974 int status;
975
976 if (!PyArg_ParseTuple(args, "OO|O:fromkeys", &cls, &seq, &value))
977 return NULL;
978
979 d = PyObject_CallObject(cls, NULL);
980 if (d == NULL)
981 return NULL;
982 if (!PyDict_Check(d)) {
983 PyErr_BadInternalCall();
Neal Norwitzef786ae2002-11-27 19:38:00 +0000984 Py_DECREF(d);
Raymond Hettingere33d3df2002-11-27 07:29:33 +0000985 return NULL;
986 }
987
988 it = PyObject_GetIter(seq);
989 if (it == NULL){
990 Py_DECREF(d);
991 return NULL;
992 }
993
994 for (;;) {
995 key = PyIter_Next(it);
996 if (key == NULL) {
997 if (PyErr_Occurred())
998 goto Fail;
999 break;
1000 }
1001 status = PyDict_SetItem(d, key, value);
1002 Py_DECREF(key);
1003 if (status < 0)
1004 goto Fail;
1005 }
1006
1007 Py_DECREF(it);
1008 return d;
1009
1010Fail:
1011 Py_DECREF(it);
1012 Py_DECREF(d);
1013 return NULL;
1014}
1015
1016static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001017dict_update(PyObject *mp, PyObject *other)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001018{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001019 if (PyDict_Update(mp, other) < 0)
1020 return NULL;
1021 Py_INCREF(Py_None);
1022 return Py_None;
1023}
1024
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001025/* Update unconditionally replaces existing items.
1026 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001027 otherwise it leaves existing items unchanged.
1028
1029 PyDict_{Update,Merge} update/merge from a mapping object.
1030
Tim Petersf582b822001-12-11 18:51:08 +00001031 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001032 producing iterable objects of length 2.
1033*/
1034
Tim Petersf582b822001-12-11 18:51:08 +00001035int
Tim Peters1fc240e2001-10-26 05:06:50 +00001036PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1037{
1038 PyObject *it; /* iter(seq2) */
1039 int i; /* index into seq2 of current element */
1040 PyObject *item; /* seq2[i] */
1041 PyObject *fast; /* item as a 2-tuple or 2-list */
1042
1043 assert(d != NULL);
1044 assert(PyDict_Check(d));
1045 assert(seq2 != NULL);
1046
1047 it = PyObject_GetIter(seq2);
1048 if (it == NULL)
1049 return -1;
1050
1051 for (i = 0; ; ++i) {
1052 PyObject *key, *value;
1053 int n;
1054
1055 fast = NULL;
1056 item = PyIter_Next(it);
1057 if (item == NULL) {
1058 if (PyErr_Occurred())
1059 goto Fail;
1060 break;
1061 }
1062
1063 /* Convert item to sequence, and verify length 2. */
1064 fast = PySequence_Fast(item, "");
1065 if (fast == NULL) {
1066 if (PyErr_ExceptionMatches(PyExc_TypeError))
1067 PyErr_Format(PyExc_TypeError,
1068 "cannot convert dictionary update "
1069 "sequence element #%d to a sequence",
1070 i);
1071 goto Fail;
1072 }
1073 n = PySequence_Fast_GET_SIZE(fast);
1074 if (n != 2) {
1075 PyErr_Format(PyExc_ValueError,
1076 "dictionary update sequence element #%d "
1077 "has length %d; 2 is required",
1078 i, n);
1079 goto Fail;
1080 }
1081
1082 /* Update/merge with this (key, value) pair. */
1083 key = PySequence_Fast_GET_ITEM(fast, 0);
1084 value = PySequence_Fast_GET_ITEM(fast, 1);
1085 if (override || PyDict_GetItem(d, key) == NULL) {
1086 int status = PyDict_SetItem(d, key, value);
1087 if (status < 0)
1088 goto Fail;
1089 }
1090 Py_DECREF(fast);
1091 Py_DECREF(item);
1092 }
1093
1094 i = 0;
1095 goto Return;
1096Fail:
1097 Py_XDECREF(item);
1098 Py_XDECREF(fast);
1099 i = -1;
1100Return:
1101 Py_DECREF(it);
1102 return i;
1103}
1104
Tim Peters6d6c1a32001-08-02 04:15:00 +00001105int
1106PyDict_Update(PyObject *a, PyObject *b)
1107{
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001108 return PyDict_Merge(a, b, 1);
1109}
1110
1111int
1112PyDict_Merge(PyObject *a, PyObject *b, int override)
1113{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001114 register PyDictObject *mp, *other;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001115 register int i;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001116 dictentry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001117
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001118 /* We accept for the argument either a concrete dictionary object,
1119 * or an abstract "mapping" object. For the former, we can do
1120 * things quite efficiently. For the latter, we only require that
1121 * PyMapping_Keys() and PyObject_GetItem() be supported.
1122 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001123 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1124 PyErr_BadInternalCall();
1125 return -1;
1126 }
1127 mp = (dictobject*)a;
1128 if (PyDict_Check(b)) {
1129 other = (dictobject*)b;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001130 if (other == mp || other->ma_used == 0)
1131 /* a.update(a) or a.update({}); nothing to do */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001132 return 0;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001133 /* Do one big resize at the start, rather than
1134 * incrementally resizing as we insert new items. Expect
1135 * that there will be no (or few) overlapping keys.
1136 */
1137 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
1138 if (dictresize(mp, (mp->ma_used + other->ma_used)*3/2) != 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001139 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001140 }
1141 for (i = 0; i <= other->ma_mask; i++) {
1142 entry = &other->ma_table[i];
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001143 if (entry->me_value != NULL &&
1144 (override ||
1145 PyDict_GetItem(a, entry->me_key) == NULL)) {
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001146 Py_INCREF(entry->me_key);
1147 Py_INCREF(entry->me_value);
1148 insertdict(mp, entry->me_key, entry->me_hash,
1149 entry->me_value);
1150 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001151 }
1152 }
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001153 else {
1154 /* Do it the generic, slower way */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001155 PyObject *keys = PyMapping_Keys(b);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001156 PyObject *iter;
1157 PyObject *key, *value;
1158 int status;
1159
1160 if (keys == NULL)
1161 /* Docstring says this is equivalent to E.keys() so
1162 * if E doesn't have a .keys() method we want
1163 * AttributeError to percolate up. Might as well
1164 * do the same for any other error.
1165 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001166 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001167
1168 iter = PyObject_GetIter(keys);
1169 Py_DECREF(keys);
1170 if (iter == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001171 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001172
1173 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001174 if (!override && PyDict_GetItem(a, key) != NULL) {
1175 Py_DECREF(key);
1176 continue;
1177 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001178 value = PyObject_GetItem(b, key);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001179 if (value == NULL) {
1180 Py_DECREF(iter);
1181 Py_DECREF(key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001182 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001183 }
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001184 status = PyDict_SetItem(a, key, value);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001185 Py_DECREF(key);
1186 Py_DECREF(value);
1187 if (status < 0) {
1188 Py_DECREF(iter);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001189 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001190 }
1191 }
1192 Py_DECREF(iter);
1193 if (PyErr_Occurred())
1194 /* Iterator completed, via error */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001195 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001196 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001197 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001198}
1199
1200static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001201dict_copy(register dictobject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001202{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001203 return PyDict_Copy((PyObject*)mp);
1204}
1205
1206PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001207PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001208{
1209 register dictobject *mp;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001210 register int i;
1211 dictobject *copy;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001212 dictentry *entry;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001213
1214 if (o == NULL || !PyDict_Check(o)) {
1215 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001216 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001217 }
1218 mp = (dictobject *)o;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001219 copy = (dictobject *)PyDict_New();
1220 if (copy == NULL)
1221 return NULL;
1222 if (mp->ma_used > 0) {
1223 if (dictresize(copy, mp->ma_used*3/2) != 0)
1224 return NULL;
Tim Petersafb6ae82001-06-04 21:00:21 +00001225 for (i = 0; i <= mp->ma_mask; i++) {
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001226 entry = &mp->ma_table[i];
1227 if (entry->me_value != NULL) {
1228 Py_INCREF(entry->me_key);
1229 Py_INCREF(entry->me_value);
1230 insertdict(copy, entry->me_key, entry->me_hash,
1231 entry->me_value);
1232 }
1233 }
1234 }
1235 return (PyObject *)copy;
1236}
1237
Guido van Rossum4199fac1993-11-05 10:18:44 +00001238int
Tim Peters1f5871e2000-07-04 17:44:48 +00001239PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001240{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001241 if (mp == NULL || !PyDict_Check(mp)) {
1242 PyErr_BadInternalCall();
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001243 return 0;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001244 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001245 return ((dictobject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001246}
1247
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001248PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001249PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001250{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001251 if (mp == NULL || !PyDict_Check(mp)) {
1252 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001253 return NULL;
1254 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001255 return dict_keys((dictobject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001256}
1257
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001258PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001259PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001260{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001261 if (mp == NULL || !PyDict_Check(mp)) {
1262 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001263 return NULL;
1264 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001265 return dict_values((dictobject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001266}
1267
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001268PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001269PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001270{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001271 if (mp == NULL || !PyDict_Check(mp)) {
1272 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001273 return NULL;
1274 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001275 return dict_items((dictobject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001276}
1277
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001278/* Subroutine which returns the smallest key in a for which b's value
1279 is different or absent. The value is returned too, through the
Tim Peters95bf9392001-05-10 08:32:44 +00001280 pval argument. Both are NULL if no key in a is found for which b's status
1281 differs. The refcounts on (and only on) non-NULL *pval and function return
1282 values must be decremented by the caller (characterize() increments them
1283 to ensure that mutating comparison and PyDict_GetItem calls can't delete
1284 them before the caller is done looking at them). */
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001285
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001286static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001287characterize(dictobject *a, dictobject *b, PyObject **pval)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001288{
Tim Peters95bf9392001-05-10 08:32:44 +00001289 PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
1290 PyObject *aval = NULL; /* a[akey] */
Guido van Rossumb9324202001-01-18 00:39:02 +00001291 int i, cmp;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001292
Tim Petersafb6ae82001-06-04 21:00:21 +00001293 for (i = 0; i <= a->ma_mask; i++) {
Tim Peters95bf9392001-05-10 08:32:44 +00001294 PyObject *thiskey, *thisaval, *thisbval;
1295 if (a->ma_table[i].me_value == NULL)
1296 continue;
1297 thiskey = a->ma_table[i].me_key;
1298 Py_INCREF(thiskey); /* keep alive across compares */
1299 if (akey != NULL) {
1300 cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
1301 if (cmp < 0) {
1302 Py_DECREF(thiskey);
1303 goto Fail;
1304 }
1305 if (cmp > 0 ||
Tim Petersafb6ae82001-06-04 21:00:21 +00001306 i > a->ma_mask ||
Tim Peters95bf9392001-05-10 08:32:44 +00001307 a->ma_table[i].me_value == NULL)
1308 {
1309 /* Not the *smallest* a key; or maybe it is
1310 * but the compare shrunk the dict so we can't
1311 * find its associated value anymore; or
1312 * maybe it is but the compare deleted the
1313 * a[thiskey] entry.
1314 */
1315 Py_DECREF(thiskey);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001316 continue;
Guido van Rossumb9324202001-01-18 00:39:02 +00001317 }
Tim Peters95bf9392001-05-10 08:32:44 +00001318 }
1319
1320 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
1321 thisaval = a->ma_table[i].me_value;
1322 assert(thisaval);
1323 Py_INCREF(thisaval); /* keep alive */
1324 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
1325 if (thisbval == NULL)
1326 cmp = 0;
1327 else {
1328 /* both dicts have thiskey: same values? */
1329 cmp = PyObject_RichCompareBool(
1330 thisaval, thisbval, Py_EQ);
1331 if (cmp < 0) {
1332 Py_DECREF(thiskey);
1333 Py_DECREF(thisaval);
1334 goto Fail;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001335 }
1336 }
Tim Peters95bf9392001-05-10 08:32:44 +00001337 if (cmp == 0) {
1338 /* New winner. */
1339 Py_XDECREF(akey);
1340 Py_XDECREF(aval);
1341 akey = thiskey;
1342 aval = thisaval;
1343 }
1344 else {
1345 Py_DECREF(thiskey);
1346 Py_DECREF(thisaval);
1347 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001348 }
Tim Peters95bf9392001-05-10 08:32:44 +00001349 *pval = aval;
1350 return akey;
1351
1352Fail:
1353 Py_XDECREF(akey);
1354 Py_XDECREF(aval);
1355 *pval = NULL;
1356 return NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001357}
1358
1359static int
Tim Peters1f5871e2000-07-04 17:44:48 +00001360dict_compare(dictobject *a, dictobject *b)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001361{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001362 PyObject *adiff, *bdiff, *aval, *bval;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001363 int res;
1364
1365 /* Compare lengths first */
1366 if (a->ma_used < b->ma_used)
1367 return -1; /* a is shorter */
1368 else if (a->ma_used > b->ma_used)
1369 return 1; /* b is shorter */
Tim Peters95bf9392001-05-10 08:32:44 +00001370
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001371 /* Same length -- check all keys */
Tim Peters95bf9392001-05-10 08:32:44 +00001372 bdiff = bval = NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001373 adiff = characterize(a, b, &aval);
Tim Peters95bf9392001-05-10 08:32:44 +00001374 if (adiff == NULL) {
1375 assert(!aval);
Tim Peters3918fb22001-05-10 18:58:31 +00001376 /* Either an error, or a is a subset with the same length so
Tim Peters95bf9392001-05-10 08:32:44 +00001377 * must be equal.
1378 */
1379 res = PyErr_Occurred() ? -1 : 0;
1380 goto Finished;
1381 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001382 bdiff = characterize(b, a, &bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001383 if (bdiff == NULL && PyErr_Occurred()) {
1384 assert(!bval);
1385 res = -1;
1386 goto Finished;
1387 }
1388 res = 0;
1389 if (bdiff) {
1390 /* bdiff == NULL "should be" impossible now, but perhaps
1391 * the last comparison done by the characterize() on a had
1392 * the side effect of making the dicts equal!
1393 */
1394 res = PyObject_Compare(adiff, bdiff);
1395 }
1396 if (res == 0 && bval != NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001397 res = PyObject_Compare(aval, bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001398
1399Finished:
1400 Py_XDECREF(adiff);
1401 Py_XDECREF(bdiff);
1402 Py_XDECREF(aval);
1403 Py_XDECREF(bval);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001404 return res;
1405}
1406
Tim Peterse63415e2001-05-08 04:38:29 +00001407/* Return 1 if dicts equal, 0 if not, -1 if error.
1408 * Gets out as soon as any difference is detected.
1409 * Uses only Py_EQ comparison.
1410 */
1411static int
1412dict_equal(dictobject *a, dictobject *b)
1413{
1414 int i;
1415
1416 if (a->ma_used != b->ma_used)
1417 /* can't be equal if # of entries differ */
1418 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001419
Tim Peterse63415e2001-05-08 04:38:29 +00001420 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Tim Petersafb6ae82001-06-04 21:00:21 +00001421 for (i = 0; i <= a->ma_mask; i++) {
Tim Peterse63415e2001-05-08 04:38:29 +00001422 PyObject *aval = a->ma_table[i].me_value;
1423 if (aval != NULL) {
1424 int cmp;
1425 PyObject *bval;
1426 PyObject *key = a->ma_table[i].me_key;
1427 /* temporarily bump aval's refcount to ensure it stays
1428 alive until we're done with it */
1429 Py_INCREF(aval);
1430 bval = PyDict_GetItem((PyObject *)b, key);
1431 if (bval == NULL) {
1432 Py_DECREF(aval);
1433 return 0;
1434 }
1435 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1436 Py_DECREF(aval);
1437 if (cmp <= 0) /* error or not equal */
1438 return cmp;
1439 }
1440 }
1441 return 1;
1442 }
1443
1444static PyObject *
1445dict_richcompare(PyObject *v, PyObject *w, int op)
1446{
1447 int cmp;
1448 PyObject *res;
1449
1450 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1451 res = Py_NotImplemented;
1452 }
1453 else if (op == Py_EQ || op == Py_NE) {
1454 cmp = dict_equal((dictobject *)v, (dictobject *)w);
1455 if (cmp < 0)
1456 return NULL;
1457 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1458 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001459 else
1460 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001461 Py_INCREF(res);
1462 return res;
1463 }
1464
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001465static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001466dict_has_key(register dictobject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001467{
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001468 long hash;
1469 register long ok;
Tim Peters0ab085c2001-09-14 00:25:33 +00001470 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001471 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001472 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001473 if (hash == -1)
1474 return NULL;
1475 }
Tim Petersdea48ec2001-05-22 20:40:22 +00001476 ok = (mp->ma_lookup)(mp, key, hash)->me_value != NULL;
Guido van Rossum77f6a652002-04-03 22:41:51 +00001477 return PyBool_FromLong(ok);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001478}
1479
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001480static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001481dict_get(register dictobject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001482{
1483 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001484 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001485 PyObject *val = NULL;
1486 long hash;
1487
Fred Drake52fccfd2000-02-23 15:47:16 +00001488 if (!PyArg_ParseTuple(args, "O|O:get", &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001489 return NULL;
1490
Tim Peters0ab085c2001-09-14 00:25:33 +00001491 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001492 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Barry Warsawc38c5da1997-10-06 17:49:20 +00001493 hash = PyObject_Hash(key);
1494 if (hash == -1)
1495 return NULL;
1496 }
Fred Drake1bff34a2000-08-31 19:31:38 +00001497 val = (mp->ma_lookup)(mp, key, hash)->me_value;
Barry Warsaw320ac331997-10-20 17:26:25 +00001498
Barry Warsawc38c5da1997-10-06 17:49:20 +00001499 if (val == NULL)
1500 val = failobj;
1501 Py_INCREF(val);
1502 return val;
1503}
1504
1505
1506static PyObject *
Guido van Rossum164452c2000-08-08 16:12:54 +00001507dict_setdefault(register dictobject *mp, PyObject *args)
1508{
1509 PyObject *key;
1510 PyObject *failobj = Py_None;
1511 PyObject *val = NULL;
1512 long hash;
1513
1514 if (!PyArg_ParseTuple(args, "O|O:setdefault", &key, &failobj))
1515 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001516
Tim Peters0ab085c2001-09-14 00:25:33 +00001517 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001518 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum164452c2000-08-08 16:12:54 +00001519 hash = PyObject_Hash(key);
1520 if (hash == -1)
1521 return NULL;
1522 }
Fred Drake1bff34a2000-08-31 19:31:38 +00001523 val = (mp->ma_lookup)(mp, key, hash)->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001524 if (val == NULL) {
1525 val = failobj;
1526 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1527 val = NULL;
1528 }
1529 Py_XINCREF(val);
1530 return val;
1531}
1532
1533
1534static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001535dict_clear(register dictobject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001536{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001537 PyDict_Clear((PyObject *)mp);
1538 Py_INCREF(Py_None);
1539 return Py_None;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001540}
1541
Guido van Rossumba6ab842000-12-12 22:02:18 +00001542static PyObject *
Guido van Rossume027d982002-04-12 15:11:59 +00001543dict_pop(dictobject *mp, PyObject *key)
1544{
1545 long hash;
1546 dictentry *ep;
1547 PyObject *old_value, *old_key;
1548
1549 if (mp->ma_used == 0) {
1550 PyErr_SetString(PyExc_KeyError,
1551 "pop(): dictionary is empty");
1552 return NULL;
1553 }
1554 if (!PyString_CheckExact(key) ||
1555 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1556 hash = PyObject_Hash(key);
1557 if (hash == -1)
1558 return NULL;
1559 }
1560 ep = (mp->ma_lookup)(mp, key, hash);
1561 if (ep->me_value == NULL) {
1562 PyErr_SetObject(PyExc_KeyError, key);
1563 return NULL;
1564 }
1565 old_key = ep->me_key;
1566 Py_INCREF(dummy);
1567 ep->me_key = dummy;
1568 old_value = ep->me_value;
1569 ep->me_value = NULL;
1570 mp->ma_used--;
1571 Py_DECREF(old_key);
1572 return old_value;
1573}
1574
1575static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001576dict_popitem(dictobject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001577{
1578 int i = 0;
1579 dictentry *ep;
1580 PyObject *res;
1581
Tim Petersf4b33f62001-06-02 05:42:29 +00001582 /* Allocate the result tuple before checking the size. Believe it
1583 * or not, this allocation could trigger a garbage collection which
1584 * could empty the dict, so if we checked the size first and that
1585 * happened, the result would be an infinite loop (searching for an
1586 * entry that no longer exists). Note that the usual popitem()
1587 * idiom is "while d: k, v = d.popitem()". so needing to throw the
1588 * tuple away if the dict *is* empty isn't a significant
1589 * inefficiency -- possible, but unlikely in practice.
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001590 */
1591 res = PyTuple_New(2);
1592 if (res == NULL)
1593 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001594 if (mp->ma_used == 0) {
1595 Py_DECREF(res);
1596 PyErr_SetString(PyExc_KeyError,
1597 "popitem(): dictionary is empty");
1598 return NULL;
1599 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001600 /* Set ep to "the first" dict entry with a value. We abuse the hash
1601 * field of slot 0 to hold a search finger:
1602 * If slot 0 has a value, use slot 0.
1603 * Else slot 0 is being used to hold a search finger,
1604 * and we use its hash value as the first index to look.
1605 */
1606 ep = &mp->ma_table[0];
1607 if (ep->me_value == NULL) {
1608 i = (int)ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001609 /* The hash field may be a real hash value, or it may be a
1610 * legit search finger, or it may be a once-legit search
1611 * finger that's out of bounds now because it wrapped around
1612 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001613 */
Tim Petersafb6ae82001-06-04 21:00:21 +00001614 if (i > mp->ma_mask || i < 1)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001615 i = 1; /* skip slot 0 */
1616 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1617 i++;
Tim Petersafb6ae82001-06-04 21:00:21 +00001618 if (i > mp->ma_mask)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001619 i = 1;
1620 }
1621 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001622 PyTuple_SET_ITEM(res, 0, ep->me_key);
1623 PyTuple_SET_ITEM(res, 1, ep->me_value);
1624 Py_INCREF(dummy);
1625 ep->me_key = dummy;
1626 ep->me_value = NULL;
1627 mp->ma_used--;
1628 assert(mp->ma_table[0].me_value == NULL);
1629 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001630 return res;
1631}
1632
Jeremy Hylton8caad492000-06-23 14:18:11 +00001633static int
1634dict_traverse(PyObject *op, visitproc visit, void *arg)
1635{
1636 int i = 0, err;
1637 PyObject *pk;
1638 PyObject *pv;
1639
1640 while (PyDict_Next(op, &i, &pk, &pv)) {
1641 err = visit(pk, arg);
1642 if (err)
1643 return err;
1644 err = visit(pv, arg);
1645 if (err)
1646 return err;
1647 }
1648 return 0;
1649}
1650
1651static int
1652dict_tp_clear(PyObject *op)
1653{
1654 PyDict_Clear(op);
1655 return 0;
1656}
1657
Tim Petersf7f88b12000-12-13 23:18:45 +00001658
Jeremy Hylton938ace62002-07-17 16:30:39 +00001659static PyObject *dictiter_new(dictobject *, binaryfunc);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001660
1661static PyObject *
1662select_key(PyObject *key, PyObject *value)
1663{
1664 Py_INCREF(key);
1665 return key;
1666}
1667
1668static PyObject *
1669select_value(PyObject *key, PyObject *value)
1670{
1671 Py_INCREF(value);
1672 return value;
1673}
1674
1675static PyObject *
1676select_item(PyObject *key, PyObject *value)
1677{
1678 PyObject *res = PyTuple_New(2);
1679
1680 if (res != NULL) {
1681 Py_INCREF(key);
1682 Py_INCREF(value);
1683 PyTuple_SET_ITEM(res, 0, key);
1684 PyTuple_SET_ITEM(res, 1, value);
1685 }
1686 return res;
1687}
1688
1689static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001690dict_iterkeys(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001691{
Guido van Rossum09e563a2001-05-01 12:10:21 +00001692 return dictiter_new(dict, select_key);
1693}
1694
1695static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001696dict_itervalues(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001697{
Guido van Rossum09e563a2001-05-01 12:10:21 +00001698 return dictiter_new(dict, select_value);
1699}
1700
1701static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001702dict_iteritems(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001703{
Guido van Rossum09e563a2001-05-01 12:10:21 +00001704 return dictiter_new(dict, select_item);
1705}
1706
1707
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001708PyDoc_STRVAR(has_key__doc__,
1709"D.has_key(k) -> 1 if D has a key k, else 0");
Tim Petersf7f88b12000-12-13 23:18:45 +00001710
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001711PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001712"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001713
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001714PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001715"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 +00001716
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001717PyDoc_STRVAR(pop__doc__,
1718"D.pop(k) -> v, remove specified key and return the corresponding value");
Guido van Rossume027d982002-04-12 15:11:59 +00001719
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001720PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00001721"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000017222-tuple; but raise KeyError if D is empty");
Tim Petersf7f88b12000-12-13 23:18:45 +00001723
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001724PyDoc_STRVAR(keys__doc__,
1725"D.keys() -> list of D's keys");
Tim Petersf7f88b12000-12-13 23:18:45 +00001726
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001727PyDoc_STRVAR(items__doc__,
1728"D.items() -> list of D's (key, value) pairs, as 2-tuples");
Tim Petersf7f88b12000-12-13 23:18:45 +00001729
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001730PyDoc_STRVAR(values__doc__,
1731"D.values() -> list of D's values");
Tim Petersf7f88b12000-12-13 23:18:45 +00001732
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001733PyDoc_STRVAR(update__doc__,
1734"D.update(E) -> None. Update D from E: for k in E.keys(): D[k] = E[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00001735
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001736PyDoc_STRVAR(fromkeys__doc__,
1737"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
1738v defaults to None.");
1739
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001740PyDoc_STRVAR(clear__doc__,
1741"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001742
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001743PyDoc_STRVAR(copy__doc__,
1744"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00001745
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001746PyDoc_STRVAR(iterkeys__doc__,
1747"D.iterkeys() -> an iterator over the keys of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00001748
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001749PyDoc_STRVAR(itervalues__doc__,
1750"D.itervalues() -> an iterator over the values of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00001751
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001752PyDoc_STRVAR(iteritems__doc__,
1753"D.iteritems() -> an iterator over the (key, value) items of D");
Guido van Rossum09e563a2001-05-01 12:10:21 +00001754
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001755static PyMethodDef mapp_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001756 {"has_key", (PyCFunction)dict_has_key, METH_O,
Tim Petersf7f88b12000-12-13 23:18:45 +00001757 has_key__doc__},
1758 {"get", (PyCFunction)dict_get, METH_VARARGS,
1759 get__doc__},
1760 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1761 setdefault_doc__},
Guido van Rossume027d982002-04-12 15:11:59 +00001762 {"pop", (PyCFunction)dict_pop, METH_O,
Just van Rossuma797d812002-11-23 09:45:04 +00001763 pop__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001764 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001765 popitem__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001766 {"keys", (PyCFunction)dict_keys, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001767 keys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001768 {"items", (PyCFunction)dict_items, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001769 items__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001770 {"values", (PyCFunction)dict_values, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001771 values__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001772 {"update", (PyCFunction)dict_update, METH_O,
Tim Petersf7f88b12000-12-13 23:18:45 +00001773 update__doc__},
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001774 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
1775 fromkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001776 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001777 clear__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001778 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001779 copy__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001780 {"iterkeys", (PyCFunction)dict_iterkeys, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00001781 iterkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001782 {"itervalues", (PyCFunction)dict_itervalues, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00001783 itervalues__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001784 {"iteritems", (PyCFunction)dict_iteritems, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00001785 iteritems__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00001786 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001787};
1788
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001789static int
1790dict_contains(dictobject *mp, PyObject *key)
1791{
1792 long hash;
1793
Tim Peters0ab085c2001-09-14 00:25:33 +00001794 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001795 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001796 hash = PyObject_Hash(key);
1797 if (hash == -1)
1798 return -1;
1799 }
Tim Petersdea48ec2001-05-22 20:40:22 +00001800 return (mp->ma_lookup)(mp, key, hash)->me_value != NULL;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001801}
1802
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001803/* Hack to implement "key in dict" */
1804static PySequenceMethods dict_as_sequence = {
1805 0, /* sq_length */
1806 0, /* sq_concat */
1807 0, /* sq_repeat */
1808 0, /* sq_item */
1809 0, /* sq_slice */
1810 0, /* sq_ass_item */
1811 0, /* sq_ass_slice */
1812 (objobjproc)dict_contains, /* sq_contains */
1813 0, /* sq_inplace_concat */
1814 0, /* sq_inplace_repeat */
1815};
1816
Guido van Rossum09e563a2001-05-01 12:10:21 +00001817static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001818dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1819{
1820 PyObject *self;
1821
1822 assert(type != NULL && type->tp_alloc != NULL);
1823 self = type->tp_alloc(type, 0);
1824 if (self != NULL) {
1825 PyDictObject *d = (PyDictObject *)self;
1826 /* It's guaranteed that tp->alloc zeroed out the struct. */
1827 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
1828 INIT_NONZERO_DICT_SLOTS(d);
1829 d->ma_lookup = lookdict_string;
1830#ifdef SHOW_CONVERSION_COUNTS
1831 ++created;
1832#endif
1833 }
1834 return self;
1835}
1836
Tim Peters25786c02001-09-02 08:22:48 +00001837static int
1838dict_init(PyObject *self, PyObject *args, PyObject *kwds)
1839{
1840 PyObject *arg = NULL;
Tim Peters1fc240e2001-10-26 05:06:50 +00001841 int result = 0;
Tim Peters25786c02001-09-02 08:22:48 +00001842
Just van Rossuma797d812002-11-23 09:45:04 +00001843 if (!PyArg_ParseTuple(args, "|O:dict", &arg))
Tim Peters1fc240e2001-10-26 05:06:50 +00001844 result = -1;
1845
1846 else if (arg != NULL) {
1847 if (PyObject_HasAttrString(arg, "keys"))
1848 result = PyDict_Merge(self, arg, 1);
1849 else
1850 result = PyDict_MergeFromSeq2(self, arg, 1);
Tim Peters25786c02001-09-02 08:22:48 +00001851 }
Just van Rossuma797d812002-11-23 09:45:04 +00001852 if (result == 0 && kwds != NULL)
1853 result = PyDict_Merge(self, kwds, 1);
Tim Peters1fc240e2001-10-26 05:06:50 +00001854 return result;
Tim Peters25786c02001-09-02 08:22:48 +00001855}
1856
Guido van Rossumdbb53d92001-12-03 16:32:18 +00001857static long
1858dict_nohash(PyObject *self)
1859{
1860 PyErr_SetString(PyExc_TypeError, "dict objects are unhashable");
1861 return -1;
1862}
1863
Tim Peters6d6c1a32001-08-02 04:15:00 +00001864static PyObject *
Guido van Rossum09e563a2001-05-01 12:10:21 +00001865dict_iter(dictobject *dict)
1866{
1867 return dictiter_new(dict, select_key);
1868}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001869
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001870PyDoc_STRVAR(dictionary_doc,
Tim Petersa427a2b2001-10-29 22:25:45 +00001871"dict() -> new empty dictionary.\n"
1872"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Tim Peters1fc240e2001-10-26 05:06:50 +00001873" (key, value) pairs.\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00001874"dict(seq) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00001875" d = {}\n"
1876" for k, v in seq:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00001877" d[k] = v\n"
1878"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
1879" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00001880
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001881PyTypeObject PyDict_Type = {
1882 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001883 0,
Tim Petersa427a2b2001-10-29 22:25:45 +00001884 "dict",
Neil Schemenauere83c00e2001-08-29 23:54:21 +00001885 sizeof(dictobject),
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001886 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00001887 (destructor)dict_dealloc, /* tp_dealloc */
1888 (printfunc)dict_print, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001889 0, /* tp_getattr */
Guido van Rossumb9324202001-01-18 00:39:02 +00001890 0, /* tp_setattr */
Tim Peters4fa58bf2001-05-10 21:45:19 +00001891 (cmpfunc)dict_compare, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00001892 (reprfunc)dict_repr, /* tp_repr */
1893 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001894 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00001895 &dict_as_mapping, /* tp_as_mapping */
Guido van Rossumdbb53d92001-12-03 16:32:18 +00001896 dict_nohash, /* tp_hash */
Guido van Rossumb9324202001-01-18 00:39:02 +00001897 0, /* tp_call */
1898 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001899 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumb9324202001-01-18 00:39:02 +00001900 0, /* tp_setattro */
1901 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00001902 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Tim Peters6d6c1a32001-08-02 04:15:00 +00001903 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters25786c02001-09-02 08:22:48 +00001904 dictionary_doc, /* tp_doc */
Guido van Rossumb9324202001-01-18 00:39:02 +00001905 (traverseproc)dict_traverse, /* tp_traverse */
1906 (inquiry)dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00001907 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001908 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00001909 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001910 0, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001911 mapp_methods, /* tp_methods */
1912 0, /* tp_members */
1913 0, /* tp_getset */
1914 0, /* tp_base */
1915 0, /* tp_dict */
1916 0, /* tp_descr_get */
1917 0, /* tp_descr_set */
1918 0, /* tp_dictoffset */
Tim Peters25786c02001-09-02 08:22:48 +00001919 (initproc)dict_init, /* tp_init */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001920 PyType_GenericAlloc, /* tp_alloc */
1921 dict_new, /* tp_new */
Neil Schemenauer6189b892002-04-12 02:43:00 +00001922 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001923};
1924
Guido van Rossum3cca2451997-05-16 14:23:33 +00001925/* For backward compatibility with old dictionary interface */
1926
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001927PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001928PyDict_GetItemString(PyObject *v, char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001929{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001930 PyObject *kv, *rv;
1931 kv = PyString_FromString(key);
1932 if (kv == NULL)
1933 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00001934 rv = PyDict_GetItem(v, kv);
1935 Py_DECREF(kv);
1936 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001937}
1938
1939int
Tim Peters1f5871e2000-07-04 17:44:48 +00001940PyDict_SetItemString(PyObject *v, char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001941{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001942 PyObject *kv;
1943 int err;
1944 kv = PyString_FromString(key);
1945 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00001946 return -1;
Guido van Rossum4f3bf1e1997-09-29 23:31:11 +00001947 PyString_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00001948 err = PyDict_SetItem(v, kv, item);
1949 Py_DECREF(kv);
1950 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001951}
1952
1953int
Tim Peters1f5871e2000-07-04 17:44:48 +00001954PyDict_DelItemString(PyObject *v, char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001955{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001956 PyObject *kv;
1957 int err;
1958 kv = PyString_FromString(key);
1959 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00001960 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00001961 err = PyDict_DelItem(v, kv);
1962 Py_DECREF(kv);
1963 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001964}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001965
1966/* Dictionary iterator type */
1967
1968extern PyTypeObject PyDictIter_Type; /* Forward */
1969
1970typedef struct {
1971 PyObject_HEAD
Guido van Rossum2147df72002-07-16 20:30:22 +00001972 dictobject *di_dict; /* Set to NULL when iterator is exhausted */
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001973 int di_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001974 int di_pos;
Guido van Rossum09e563a2001-05-01 12:10:21 +00001975 binaryfunc di_select;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001976} dictiterobject;
1977
1978static PyObject *
Guido van Rossum09e563a2001-05-01 12:10:21 +00001979dictiter_new(dictobject *dict, binaryfunc select)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001980{
1981 dictiterobject *di;
Neil Schemenauer6189b892002-04-12 02:43:00 +00001982 di = PyObject_New(dictiterobject, &PyDictIter_Type);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001983 if (di == NULL)
1984 return NULL;
1985 Py_INCREF(dict);
1986 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001987 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001988 di->di_pos = 0;
Guido van Rossum09e563a2001-05-01 12:10:21 +00001989 di->di_select = select;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001990 return (PyObject *)di;
1991}
1992
1993static void
1994dictiter_dealloc(dictiterobject *di)
1995{
Guido van Rossum2147df72002-07-16 20:30:22 +00001996 Py_XDECREF(di->di_dict);
Neil Schemenauer6189b892002-04-12 02:43:00 +00001997 PyObject_Del(di);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001998}
1999
2000static PyObject *
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002001dictiter_getiter(PyObject *it)
2002{
2003 Py_INCREF(it);
2004 return it;
2005}
2006
Guido van Rossum213c7a62001-04-23 14:08:49 +00002007static PyObject *dictiter_iternext(dictiterobject *di)
2008{
Guido van Rossum09e563a2001-05-01 12:10:21 +00002009 PyObject *key, *value;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002010
Guido van Rossum2147df72002-07-16 20:30:22 +00002011 if (di->di_dict == NULL)
2012 return NULL;
2013
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00002014 if (di->di_used != di->di_dict->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00002015 PyErr_SetString(PyExc_RuntimeError,
2016 "dictionary changed size during iteration");
Guido van Rossum2147df72002-07-16 20:30:22 +00002017 di->di_used = -1; /* Make this state sticky */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002018 return NULL;
2019 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002020 if (PyDict_Next((PyObject *)(di->di_dict), &di->di_pos, &key, &value))
Guido van Rossum09e563a2001-05-01 12:10:21 +00002021 return (*di->di_select)(key, value);
Guido van Rossum2147df72002-07-16 20:30:22 +00002022
2023 Py_DECREF(di->di_dict);
2024 di->di_dict = NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002025 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002026}
2027
2028PyTypeObject PyDictIter_Type = {
2029 PyObject_HEAD_INIT(&PyType_Type)
2030 0, /* ob_size */
2031 "dictionary-iterator", /* tp_name */
2032 sizeof(dictiterobject), /* tp_basicsize */
2033 0, /* tp_itemsize */
2034 /* methods */
2035 (destructor)dictiter_dealloc, /* tp_dealloc */
2036 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002037 0, /* tp_getattr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002038 0, /* tp_setattr */
2039 0, /* tp_compare */
2040 0, /* tp_repr */
2041 0, /* tp_as_number */
2042 0, /* tp_as_sequence */
2043 0, /* tp_as_mapping */
2044 0, /* tp_hash */
2045 0, /* tp_call */
2046 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002047 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002048 0, /* tp_setattro */
2049 0, /* tp_as_buffer */
2050 Py_TPFLAGS_DEFAULT, /* tp_flags */
2051 0, /* tp_doc */
2052 0, /* tp_traverse */
2053 0, /* tp_clear */
2054 0, /* tp_richcompare */
2055 0, /* tp_weaklistoffset */
2056 (getiterfunc)dictiter_getiter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002057 (iternextfunc)dictiter_iternext, /* tp_iternext */
Guido van Rossum2147df72002-07-16 20:30:22 +00002058 0, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002059 0, /* tp_members */
2060 0, /* tp_getset */
2061 0, /* tp_base */
2062 0, /* tp_dict */
2063 0, /* tp_descr_get */
2064 0, /* tp_descr_set */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002065};