blob: f68a9648f42bd17840142e2f0eca276d08e9c145 [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 }
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000483#ifdef CACHE_HASH
Tim Peters0ab085c2001-09-14 00:25:33 +0000484 if (!PyString_CheckExact(key) ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000485 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000486#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000487 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000488 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000489 if (hash == -1) {
490 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000491 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000492 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000493 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000494 return (mp->ma_lookup)(mp, key, hash)->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000495}
496
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000497/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
498 * dictionary if it is merely replacing the value for an existing key.
499 * This is means that it's safe to loop over a dictionary with
500 * PyDict_Next() and occasionally replace a value -- but you can't
501 * insert new keys or remove them.
502 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000503int
Tim Peters1f5871e2000-07-04 17:44:48 +0000504PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000505{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000506 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000507 register long hash;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000508 register int n_used;
509
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000510 if (!PyDict_Check(op)) {
511 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000512 return -1;
513 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000514 mp = (dictobject *)op;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000515#ifdef CACHE_HASH
Tim Peters0ab085c2001-09-14 00:25:33 +0000516 if (PyString_CheckExact(key)) {
Guido van Rossum2a61e741997-01-18 07:55:05 +0000517#ifdef INTERN_STRINGS
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000518 if (((PyStringObject *)key)->ob_sinterned != NULL) {
519 key = ((PyStringObject *)key)->ob_sinterned;
520 hash = ((PyStringObject *)key)->ob_shash;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000521 }
522 else
523#endif
524 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000525 hash = ((PyStringObject *)key)->ob_shash;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000526 if (hash == -1)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000527 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000528 }
529 }
530 else
531#endif
532 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000533 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000534 if (hash == -1)
535 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000536 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000537 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000538 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000539 Py_INCREF(value);
540 Py_INCREF(key);
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000541 insertdict(mp, key, hash, value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000542 /* If we added a key, we can safely resize. Otherwise skip this!
543 * If fill >= 2/3 size, adjust size. Normally, this doubles the
544 * size, but it's also possible for the dict to shrink (if ma_fill is
545 * much larger than ma_used, meaning a lot of dict keys have been
546 * deleted).
547 */
Tim Petersafb6ae82001-06-04 21:00:21 +0000548 if (mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2) {
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000549 if (dictresize(mp, mp->ma_used*2) != 0)
550 return -1;
551 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000552 return 0;
553}
554
555int
Tim Peters1f5871e2000-07-04 17:44:48 +0000556PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000557{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000558 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000559 register long hash;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000560 register dictentry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000561 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000562
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000563 if (!PyDict_Check(op)) {
564 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000565 return -1;
566 }
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000567#ifdef CACHE_HASH
Tim Peters0ab085c2001-09-14 00:25:33 +0000568 if (!PyString_CheckExact(key) ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000569 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000570#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000571 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000572 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000573 if (hash == -1)
574 return -1;
575 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000576 mp = (dictobject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000577 ep = (mp->ma_lookup)(mp, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000578 if (ep->me_value == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000579 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000580 return -1;
581 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000582 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000583 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000584 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000585 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000586 ep->me_value = NULL;
587 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000588 Py_DECREF(old_value);
589 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000590 return 0;
591}
592
Guido van Rossum25831651993-05-19 14:50:45 +0000593void
Tim Peters1f5871e2000-07-04 17:44:48 +0000594PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000595{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000596 dictobject *mp;
Tim Petersdea48ec2001-05-22 20:40:22 +0000597 dictentry *ep, *table;
598 int table_is_malloced;
599 int fill;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000600 dictentry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000601#ifdef Py_DEBUG
602 int i, n;
603#endif
604
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000605 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000606 return;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000607 mp = (dictobject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000608#ifdef Py_DEBUG
Tim Petersafb6ae82001-06-04 21:00:21 +0000609 n = mp->ma_mask + 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000610 i = 0;
611#endif
612
613 table = mp->ma_table;
614 assert(table != NULL);
615 table_is_malloced = table != mp->ma_smalltable;
616
617 /* This is delicate. During the process of clearing the dict,
618 * decrefs can cause the dict to mutate. To avoid fatal confusion
619 * (voice of experience), we have to make the dict empty before
620 * clearing the slots, and never refer to anything via mp->xxx while
621 * clearing.
622 */
623 fill = mp->ma_fill;
624 if (table_is_malloced)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000625 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000626
627 else if (fill > 0) {
628 /* It's a small table with something that needs to be cleared.
629 * Afraid the only safe way is to copy the dict entries into
630 * another small table first.
631 */
632 memcpy(small_copy, table, sizeof(small_copy));
633 table = small_copy;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000634 EMPTY_TO_MINSIZE(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000635 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000636 /* else it's a small table that's already empty */
637
638 /* Now we can finally clear things. If C had refcounts, we could
639 * assert that the refcount on table is 1 now, i.e. that this function
640 * has unique access to it, so decref side-effects can't alter it.
641 */
642 for (ep = table; fill > 0; ++ep) {
643#ifdef Py_DEBUG
644 assert(i < n);
645 ++i;
646#endif
647 if (ep->me_key) {
648 --fill;
649 Py_DECREF(ep->me_key);
650 Py_XDECREF(ep->me_value);
651 }
652#ifdef Py_DEBUG
653 else
654 assert(ep->me_value == NULL);
655#endif
656 }
657
658 if (table_is_malloced)
659 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000660}
661
Tim Peters67830702001-03-21 19:23:56 +0000662/* CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
663 * mutates the dict. One exception: it is safe if the loop merely changes
664 * the values associated with the keys (but doesn't insert new keys or
665 * delete keys), via PyDict_SetItem().
666 */
Guido van Rossum25831651993-05-19 14:50:45 +0000667int
Tim Peters1f5871e2000-07-04 17:44:48 +0000668PyDict_Next(PyObject *op, int *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000669{
Guido van Rossum25831651993-05-19 14:50:45 +0000670 int i;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000671 register dictobject *mp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000672 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000673 return 0;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000674 mp = (dictobject *)op;
Guido van Rossum25831651993-05-19 14:50:45 +0000675 i = *ppos;
676 if (i < 0)
677 return 0;
Tim Petersafb6ae82001-06-04 21:00:21 +0000678 while (i <= mp->ma_mask && mp->ma_table[i].me_value == NULL)
Guido van Rossum25831651993-05-19 14:50:45 +0000679 i++;
680 *ppos = i+1;
Tim Petersafb6ae82001-06-04 21:00:21 +0000681 if (i > mp->ma_mask)
Guido van Rossum25831651993-05-19 14:50:45 +0000682 return 0;
683 if (pkey)
684 *pkey = mp->ma_table[i].me_key;
685 if (pvalue)
686 *pvalue = mp->ma_table[i].me_value;
687 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000688}
689
690/* Methods */
691
692static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000693dict_dealloc(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000694{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000695 register dictentry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000696 int fill = mp->ma_fill;
Guido van Rossumd724b232000-03-13 16:01:29 +0000697 Py_TRASHCAN_SAFE_BEGIN(mp)
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000698 _PyObject_GC_UNTRACK(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000699 for (ep = mp->ma_table; fill > 0; ep++) {
700 if (ep->me_key) {
701 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000702 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000703 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000704 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000705 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000706 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000707 PyMem_DEL(mp->ma_table);
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000708 PyObject_GC_Del(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000709 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000710}
711
712static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000713dict_print(register dictobject *mp, register FILE *fp, register int flags)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000714{
715 register int i;
716 register int any;
Guido van Rossum255443b1998-04-10 22:47:14 +0000717
718 i = Py_ReprEnter((PyObject*)mp);
719 if (i != 0) {
720 if (i < 0)
721 return i;
722 fprintf(fp, "{...}");
723 return 0;
724 }
725
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000726 fprintf(fp, "{");
727 any = 0;
Tim Petersafb6ae82001-06-04 21:00:21 +0000728 for (i = 0; i <= mp->ma_mask; i++) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000729 dictentry *ep = mp->ma_table + i;
730 PyObject *pvalue = ep->me_value;
731 if (pvalue != NULL) {
732 /* Prevent PyObject_Repr from deleting value during
733 key format */
734 Py_INCREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000735 if (any++ > 0)
736 fprintf(fp, ", ");
Guido van Rossum255443b1998-04-10 22:47:14 +0000737 if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000738 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000739 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000740 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000741 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000742 fprintf(fp, ": ");
Tim Peters19b77cf2001-06-02 08:27:39 +0000743 if (PyObject_Print(pvalue, fp, 0) != 0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000744 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000745 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000746 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000747 }
Tim Peters23cf6be2001-06-02 08:02:56 +0000748 Py_DECREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000749 }
750 }
751 fprintf(fp, "}");
Guido van Rossum255443b1998-04-10 22:47:14 +0000752 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000753 return 0;
754}
755
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000756static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000757dict_repr(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000758{
Tim Petersc6057842001-06-16 07:52:53 +0000759 int i;
Tim Petersa7259592001-06-16 05:11:17 +0000760 PyObject *s, *temp, *colon = NULL;
761 PyObject *pieces = NULL, *result = NULL;
762 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +0000763
Tim Petersa7259592001-06-16 05:11:17 +0000764 i = Py_ReprEnter((PyObject *)mp);
Guido van Rossum255443b1998-04-10 22:47:14 +0000765 if (i != 0) {
Tim Petersa7259592001-06-16 05:11:17 +0000766 return i > 0 ? PyString_FromString("{...}") : NULL;
Guido van Rossum255443b1998-04-10 22:47:14 +0000767 }
768
Tim Petersa7259592001-06-16 05:11:17 +0000769 if (mp->ma_used == 0) {
770 result = PyString_FromString("{}");
771 goto Done;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000772 }
Tim Petersa7259592001-06-16 05:11:17 +0000773
774 pieces = PyList_New(0);
775 if (pieces == NULL)
776 goto Done;
777
778 colon = PyString_FromString(": ");
779 if (colon == NULL)
780 goto Done;
781
782 /* Do repr() on each key+value pair, and insert ": " between them.
783 Note that repr may mutate the dict. */
Tim Petersc6057842001-06-16 07:52:53 +0000784 i = 0;
785 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Tim Petersa7259592001-06-16 05:11:17 +0000786 int status;
787 /* Prevent repr from deleting value during key format. */
788 Py_INCREF(value);
789 s = PyObject_Repr(key);
790 PyString_Concat(&s, colon);
791 PyString_ConcatAndDel(&s, PyObject_Repr(value));
792 Py_DECREF(value);
793 if (s == NULL)
794 goto Done;
795 status = PyList_Append(pieces, s);
796 Py_DECREF(s); /* append created a new ref */
797 if (status < 0)
798 goto Done;
799 }
800
801 /* Add "{}" decorations to the first and last items. */
802 assert(PyList_GET_SIZE(pieces) > 0);
803 s = PyString_FromString("{");
804 if (s == NULL)
805 goto Done;
806 temp = PyList_GET_ITEM(pieces, 0);
807 PyString_ConcatAndDel(&s, temp);
808 PyList_SET_ITEM(pieces, 0, s);
809 if (s == NULL)
810 goto Done;
811
812 s = PyString_FromString("}");
813 if (s == NULL)
814 goto Done;
815 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
816 PyString_ConcatAndDel(&temp, s);
817 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
818 if (temp == NULL)
819 goto Done;
820
821 /* Paste them all together with ", " between. */
822 s = PyString_FromString(", ");
823 if (s == NULL)
824 goto Done;
825 result = _PyString_Join(s, pieces);
Tim Peters0ab085c2001-09-14 00:25:33 +0000826 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +0000827
828Done:
829 Py_XDECREF(pieces);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000830 Py_XDECREF(colon);
Tim Petersa7259592001-06-16 05:11:17 +0000831 Py_ReprLeave((PyObject *)mp);
832 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000833}
834
835static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000836dict_length(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000837{
838 return mp->ma_used;
839}
840
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000841static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000842dict_subscript(dictobject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000843{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000844 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000845 long hash;
Tim Petersdea48ec2001-05-22 20:40:22 +0000846 assert(mp->ma_table != NULL);
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000847#ifdef CACHE_HASH
Tim Peters0ab085c2001-09-14 00:25:33 +0000848 if (!PyString_CheckExact(key) ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000849 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000850#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000851 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000852 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000853 if (hash == -1)
854 return NULL;
855 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000856 v = (mp->ma_lookup)(mp, key, hash) -> me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000857 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000858 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000859 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000860 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000861 return v;
862}
863
864static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000865dict_ass_sub(dictobject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000866{
867 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000868 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000869 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000870 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000871}
872
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000873static PyMappingMethods dict_as_mapping = {
874 (inquiry)dict_length, /*mp_length*/
875 (binaryfunc)dict_subscript, /*mp_subscript*/
876 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000877};
878
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000879static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000880dict_keys(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000881{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000882 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000883 register int i, j, n;
884
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000885 again:
886 n = mp->ma_used;
887 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000888 if (v == NULL)
889 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000890 if (n != mp->ma_used) {
891 /* Durnit. The allocations caused the dict to resize.
892 * Just start over, this shouldn't normally happen.
893 */
894 Py_DECREF(v);
895 goto again;
896 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000897 for (i = 0, j = 0; i <= mp->ma_mask; i++) {
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000898 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000899 PyObject *key = mp->ma_table[i].me_key;
900 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000901 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000902 j++;
903 }
904 }
905 return v;
906}
907
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000908static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000909dict_values(register dictobject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +0000910{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000911 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000912 register int i, j, n;
913
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000914 again:
915 n = mp->ma_used;
916 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +0000917 if (v == NULL)
918 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000919 if (n != mp->ma_used) {
920 /* Durnit. The allocations caused the dict to resize.
921 * Just start over, this shouldn't normally happen.
922 */
923 Py_DECREF(v);
924 goto again;
925 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000926 for (i = 0, j = 0; i <= mp->ma_mask; i++) {
Guido van Rossum25831651993-05-19 14:50:45 +0000927 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000928 PyObject *value = mp->ma_table[i].me_value;
929 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000930 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +0000931 j++;
932 }
933 }
934 return v;
935}
936
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000937static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000938dict_items(register dictobject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +0000939{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000940 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000941 register int i, j, n;
942 PyObject *item, *key, *value;
943
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000944 /* Preallocate the list of tuples, to avoid allocations during
945 * the loop over the items, which could trigger GC, which
946 * could resize the dict. :-(
947 */
948 again:
949 n = mp->ma_used;
950 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +0000951 if (v == NULL)
952 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000953 for (i = 0; i < n; i++) {
954 item = PyTuple_New(2);
955 if (item == NULL) {
956 Py_DECREF(v);
957 return NULL;
958 }
959 PyList_SET_ITEM(v, i, item);
960 }
961 if (n != mp->ma_used) {
962 /* Durnit. The allocations caused the dict to resize.
963 * Just start over, this shouldn't normally happen.
964 */
965 Py_DECREF(v);
966 goto again;
967 }
968 /* Nothing we do below makes any function calls. */
Tim Petersafb6ae82001-06-04 21:00:21 +0000969 for (i = 0, j = 0; i <= mp->ma_mask; i++) {
Guido van Rossum25831651993-05-19 14:50:45 +0000970 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000971 key = mp->ma_table[i].me_key;
972 value = mp->ma_table[i].me_value;
973 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000974 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000975 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000976 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000977 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +0000978 j++;
979 }
980 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000981 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +0000982 return v;
983}
984
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000985static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000986dict_update(PyObject *mp, PyObject *other)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000987{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000988 if (PyDict_Update(mp, other) < 0)
989 return NULL;
990 Py_INCREF(Py_None);
991 return Py_None;
992}
993
Guido van Rossum05ac6de2001-08-10 20:28:28 +0000994/* Update unconditionally replaces existing items.
995 Merge has a 3rd argument 'override'; if set, it acts like Update,
996 otherwise it leaves existing items unchanged. */
997
Tim Peters6d6c1a32001-08-02 04:15:00 +0000998int
999PyDict_Update(PyObject *a, PyObject *b)
1000{
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001001 return PyDict_Merge(a, b, 1);
1002}
1003
1004int
1005PyDict_Merge(PyObject *a, PyObject *b, int override)
1006{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001007 register PyDictObject *mp, *other;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001008 register int i;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001009 dictentry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001010
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001011 /* We accept for the argument either a concrete dictionary object,
1012 * or an abstract "mapping" object. For the former, we can do
1013 * things quite efficiently. For the latter, we only require that
1014 * PyMapping_Keys() and PyObject_GetItem() be supported.
1015 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001016 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1017 PyErr_BadInternalCall();
1018 return -1;
1019 }
1020 mp = (dictobject*)a;
1021 if (PyDict_Check(b)) {
1022 other = (dictobject*)b;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001023 if (other == mp || other->ma_used == 0)
1024 /* a.update(a) or a.update({}); nothing to do */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001025 return 0;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001026 /* Do one big resize at the start, rather than
1027 * incrementally resizing as we insert new items. Expect
1028 * that there will be no (or few) overlapping keys.
1029 */
1030 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
1031 if (dictresize(mp, (mp->ma_used + other->ma_used)*3/2) != 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001032 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001033 }
1034 for (i = 0; i <= other->ma_mask; i++) {
1035 entry = &other->ma_table[i];
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001036 if (entry->me_value != NULL &&
1037 (override ||
1038 PyDict_GetItem(a, entry->me_key) == NULL)) {
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001039 Py_INCREF(entry->me_key);
1040 Py_INCREF(entry->me_value);
1041 insertdict(mp, entry->me_key, entry->me_hash,
1042 entry->me_value);
1043 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001044 }
1045 }
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001046 else {
1047 /* Do it the generic, slower way */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001048 PyObject *keys = PyMapping_Keys(b);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001049 PyObject *iter;
1050 PyObject *key, *value;
1051 int status;
1052
1053 if (keys == NULL)
1054 /* Docstring says this is equivalent to E.keys() so
1055 * if E doesn't have a .keys() method we want
1056 * AttributeError to percolate up. Might as well
1057 * do the same for any other error.
1058 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001059 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001060
1061 iter = PyObject_GetIter(keys);
1062 Py_DECREF(keys);
1063 if (iter == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001064 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001065
1066 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001067 if (!override && PyDict_GetItem(a, key) != NULL) {
1068 Py_DECREF(key);
1069 continue;
1070 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001071 value = PyObject_GetItem(b, key);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001072 if (value == NULL) {
1073 Py_DECREF(iter);
1074 Py_DECREF(key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001075 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001076 }
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001077 status = PyDict_SetItem(a, key, value);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001078 Py_DECREF(key);
1079 Py_DECREF(value);
1080 if (status < 0) {
1081 Py_DECREF(iter);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001082 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001083 }
1084 }
1085 Py_DECREF(iter);
1086 if (PyErr_Occurred())
1087 /* Iterator completed, via error */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001088 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001089 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001090 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001091}
1092
1093static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001094dict_copy(register dictobject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001095{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001096 return PyDict_Copy((PyObject*)mp);
1097}
1098
1099PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001100PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001101{
1102 register dictobject *mp;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001103 register int i;
1104 dictobject *copy;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001105 dictentry *entry;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001106
1107 if (o == NULL || !PyDict_Check(o)) {
1108 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001109 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001110 }
1111 mp = (dictobject *)o;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001112 copy = (dictobject *)PyDict_New();
1113 if (copy == NULL)
1114 return NULL;
1115 if (mp->ma_used > 0) {
1116 if (dictresize(copy, mp->ma_used*3/2) != 0)
1117 return NULL;
Tim Petersafb6ae82001-06-04 21:00:21 +00001118 for (i = 0; i <= mp->ma_mask; i++) {
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001119 entry = &mp->ma_table[i];
1120 if (entry->me_value != NULL) {
1121 Py_INCREF(entry->me_key);
1122 Py_INCREF(entry->me_value);
1123 insertdict(copy, entry->me_key, entry->me_hash,
1124 entry->me_value);
1125 }
1126 }
1127 }
1128 return (PyObject *)copy;
1129}
1130
Guido van Rossum4199fac1993-11-05 10:18:44 +00001131int
Tim Peters1f5871e2000-07-04 17:44:48 +00001132PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001133{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001134 if (mp == NULL || !PyDict_Check(mp)) {
1135 PyErr_BadInternalCall();
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001136 return 0;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001137 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001138 return ((dictobject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001139}
1140
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001141PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001142PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001143{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001144 if (mp == NULL || !PyDict_Check(mp)) {
1145 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001146 return NULL;
1147 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001148 return dict_keys((dictobject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001149}
1150
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001151PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001152PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001153{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001154 if (mp == NULL || !PyDict_Check(mp)) {
1155 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001156 return NULL;
1157 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001158 return dict_values((dictobject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001159}
1160
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001161PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001162PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001163{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001164 if (mp == NULL || !PyDict_Check(mp)) {
1165 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001166 return NULL;
1167 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001168 return dict_items((dictobject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001169}
1170
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001171/* Subroutine which returns the smallest key in a for which b's value
1172 is different or absent. The value is returned too, through the
Tim Peters95bf9392001-05-10 08:32:44 +00001173 pval argument. Both are NULL if no key in a is found for which b's status
1174 differs. The refcounts on (and only on) non-NULL *pval and function return
1175 values must be decremented by the caller (characterize() increments them
1176 to ensure that mutating comparison and PyDict_GetItem calls can't delete
1177 them before the caller is done looking at them). */
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001178
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001179static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001180characterize(dictobject *a, dictobject *b, PyObject **pval)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001181{
Tim Peters95bf9392001-05-10 08:32:44 +00001182 PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
1183 PyObject *aval = NULL; /* a[akey] */
Guido van Rossumb9324202001-01-18 00:39:02 +00001184 int i, cmp;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001185
Tim Petersafb6ae82001-06-04 21:00:21 +00001186 for (i = 0; i <= a->ma_mask; i++) {
Tim Peters95bf9392001-05-10 08:32:44 +00001187 PyObject *thiskey, *thisaval, *thisbval;
1188 if (a->ma_table[i].me_value == NULL)
1189 continue;
1190 thiskey = a->ma_table[i].me_key;
1191 Py_INCREF(thiskey); /* keep alive across compares */
1192 if (akey != NULL) {
1193 cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
1194 if (cmp < 0) {
1195 Py_DECREF(thiskey);
1196 goto Fail;
1197 }
1198 if (cmp > 0 ||
Tim Petersafb6ae82001-06-04 21:00:21 +00001199 i > a->ma_mask ||
Tim Peters95bf9392001-05-10 08:32:44 +00001200 a->ma_table[i].me_value == NULL)
1201 {
1202 /* Not the *smallest* a key; or maybe it is
1203 * but the compare shrunk the dict so we can't
1204 * find its associated value anymore; or
1205 * maybe it is but the compare deleted the
1206 * a[thiskey] entry.
1207 */
1208 Py_DECREF(thiskey);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001209 continue;
Guido van Rossumb9324202001-01-18 00:39:02 +00001210 }
Tim Peters95bf9392001-05-10 08:32:44 +00001211 }
1212
1213 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
1214 thisaval = a->ma_table[i].me_value;
1215 assert(thisaval);
1216 Py_INCREF(thisaval); /* keep alive */
1217 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
1218 if (thisbval == NULL)
1219 cmp = 0;
1220 else {
1221 /* both dicts have thiskey: same values? */
1222 cmp = PyObject_RichCompareBool(
1223 thisaval, thisbval, Py_EQ);
1224 if (cmp < 0) {
1225 Py_DECREF(thiskey);
1226 Py_DECREF(thisaval);
1227 goto Fail;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001228 }
1229 }
Tim Peters95bf9392001-05-10 08:32:44 +00001230 if (cmp == 0) {
1231 /* New winner. */
1232 Py_XDECREF(akey);
1233 Py_XDECREF(aval);
1234 akey = thiskey;
1235 aval = thisaval;
1236 }
1237 else {
1238 Py_DECREF(thiskey);
1239 Py_DECREF(thisaval);
1240 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001241 }
Tim Peters95bf9392001-05-10 08:32:44 +00001242 *pval = aval;
1243 return akey;
1244
1245Fail:
1246 Py_XDECREF(akey);
1247 Py_XDECREF(aval);
1248 *pval = NULL;
1249 return NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001250}
1251
1252static int
Tim Peters1f5871e2000-07-04 17:44:48 +00001253dict_compare(dictobject *a, dictobject *b)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001254{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001255 PyObject *adiff, *bdiff, *aval, *bval;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001256 int res;
1257
1258 /* Compare lengths first */
1259 if (a->ma_used < b->ma_used)
1260 return -1; /* a is shorter */
1261 else if (a->ma_used > b->ma_used)
1262 return 1; /* b is shorter */
Tim Peters95bf9392001-05-10 08:32:44 +00001263
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001264 /* Same length -- check all keys */
Tim Peters95bf9392001-05-10 08:32:44 +00001265 bdiff = bval = NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001266 adiff = characterize(a, b, &aval);
Tim Peters95bf9392001-05-10 08:32:44 +00001267 if (adiff == NULL) {
1268 assert(!aval);
Tim Peters3918fb22001-05-10 18:58:31 +00001269 /* Either an error, or a is a subset with the same length so
Tim Peters95bf9392001-05-10 08:32:44 +00001270 * must be equal.
1271 */
1272 res = PyErr_Occurred() ? -1 : 0;
1273 goto Finished;
1274 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001275 bdiff = characterize(b, a, &bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001276 if (bdiff == NULL && PyErr_Occurred()) {
1277 assert(!bval);
1278 res = -1;
1279 goto Finished;
1280 }
1281 res = 0;
1282 if (bdiff) {
1283 /* bdiff == NULL "should be" impossible now, but perhaps
1284 * the last comparison done by the characterize() on a had
1285 * the side effect of making the dicts equal!
1286 */
1287 res = PyObject_Compare(adiff, bdiff);
1288 }
1289 if (res == 0 && bval != NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001290 res = PyObject_Compare(aval, bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001291
1292Finished:
1293 Py_XDECREF(adiff);
1294 Py_XDECREF(bdiff);
1295 Py_XDECREF(aval);
1296 Py_XDECREF(bval);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001297 return res;
1298}
1299
Tim Peterse63415e2001-05-08 04:38:29 +00001300/* Return 1 if dicts equal, 0 if not, -1 if error.
1301 * Gets out as soon as any difference is detected.
1302 * Uses only Py_EQ comparison.
1303 */
1304static int
1305dict_equal(dictobject *a, dictobject *b)
1306{
1307 int i;
1308
1309 if (a->ma_used != b->ma_used)
1310 /* can't be equal if # of entries differ */
1311 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001312
Tim Peterse63415e2001-05-08 04:38:29 +00001313 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Tim Petersafb6ae82001-06-04 21:00:21 +00001314 for (i = 0; i <= a->ma_mask; i++) {
Tim Peterse63415e2001-05-08 04:38:29 +00001315 PyObject *aval = a->ma_table[i].me_value;
1316 if (aval != NULL) {
1317 int cmp;
1318 PyObject *bval;
1319 PyObject *key = a->ma_table[i].me_key;
1320 /* temporarily bump aval's refcount to ensure it stays
1321 alive until we're done with it */
1322 Py_INCREF(aval);
1323 bval = PyDict_GetItem((PyObject *)b, key);
1324 if (bval == NULL) {
1325 Py_DECREF(aval);
1326 return 0;
1327 }
1328 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1329 Py_DECREF(aval);
1330 if (cmp <= 0) /* error or not equal */
1331 return cmp;
1332 }
1333 }
1334 return 1;
1335 }
1336
1337static PyObject *
1338dict_richcompare(PyObject *v, PyObject *w, int op)
1339{
1340 int cmp;
1341 PyObject *res;
1342
1343 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1344 res = Py_NotImplemented;
1345 }
1346 else if (op == Py_EQ || op == Py_NE) {
1347 cmp = dict_equal((dictobject *)v, (dictobject *)w);
1348 if (cmp < 0)
1349 return NULL;
1350 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1351 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001352 else
1353 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001354 Py_INCREF(res);
1355 return res;
1356 }
1357
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001358static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001359dict_has_key(register dictobject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001360{
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001361 long hash;
1362 register long ok;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001363#ifdef CACHE_HASH
Tim Peters0ab085c2001-09-14 00:25:33 +00001364 if (!PyString_CheckExact(key) ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001365 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001366#endif
Guido van Rossumca756f21997-01-23 19:39:29 +00001367 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001368 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001369 if (hash == -1)
1370 return NULL;
1371 }
Tim Petersdea48ec2001-05-22 20:40:22 +00001372 ok = (mp->ma_lookup)(mp, key, hash)->me_value != NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001373 return PyInt_FromLong(ok);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001374}
1375
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001376static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001377dict_get(register dictobject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001378{
1379 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001380 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001381 PyObject *val = NULL;
1382 long hash;
1383
Fred Drake52fccfd2000-02-23 15:47:16 +00001384 if (!PyArg_ParseTuple(args, "O|O:get", &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001385 return NULL;
1386
Barry Warsawc38c5da1997-10-06 17:49:20 +00001387#ifdef CACHE_HASH
Tim Peters0ab085c2001-09-14 00:25:33 +00001388 if (!PyString_CheckExact(key) ||
Barry Warsawc38c5da1997-10-06 17:49:20 +00001389 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1390#endif
1391 {
1392 hash = PyObject_Hash(key);
1393 if (hash == -1)
1394 return NULL;
1395 }
Fred Drake1bff34a2000-08-31 19:31:38 +00001396 val = (mp->ma_lookup)(mp, key, hash)->me_value;
Barry Warsaw320ac331997-10-20 17:26:25 +00001397
Barry Warsawc38c5da1997-10-06 17:49:20 +00001398 if (val == NULL)
1399 val = failobj;
1400 Py_INCREF(val);
1401 return val;
1402}
1403
1404
1405static PyObject *
Guido van Rossum164452c2000-08-08 16:12:54 +00001406dict_setdefault(register dictobject *mp, PyObject *args)
1407{
1408 PyObject *key;
1409 PyObject *failobj = Py_None;
1410 PyObject *val = NULL;
1411 long hash;
1412
1413 if (!PyArg_ParseTuple(args, "O|O:setdefault", &key, &failobj))
1414 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001415
1416#ifdef CACHE_HASH
Tim Peters0ab085c2001-09-14 00:25:33 +00001417 if (!PyString_CheckExact(key) ||
Guido van Rossum164452c2000-08-08 16:12:54 +00001418 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1419#endif
1420 {
1421 hash = PyObject_Hash(key);
1422 if (hash == -1)
1423 return NULL;
1424 }
Fred Drake1bff34a2000-08-31 19:31:38 +00001425 val = (mp->ma_lookup)(mp, key, hash)->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001426 if (val == NULL) {
1427 val = failobj;
1428 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1429 val = NULL;
1430 }
1431 Py_XINCREF(val);
1432 return val;
1433}
1434
1435
1436static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001437dict_clear(register dictobject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001438{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001439 PyDict_Clear((PyObject *)mp);
1440 Py_INCREF(Py_None);
1441 return Py_None;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001442}
1443
Guido van Rossumba6ab842000-12-12 22:02:18 +00001444static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001445dict_popitem(dictobject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001446{
1447 int i = 0;
1448 dictentry *ep;
1449 PyObject *res;
1450
Tim Petersf4b33f62001-06-02 05:42:29 +00001451 /* Allocate the result tuple before checking the size. Believe it
1452 * or not, this allocation could trigger a garbage collection which
1453 * could empty the dict, so if we checked the size first and that
1454 * happened, the result would be an infinite loop (searching for an
1455 * entry that no longer exists). Note that the usual popitem()
1456 * idiom is "while d: k, v = d.popitem()". so needing to throw the
1457 * tuple away if the dict *is* empty isn't a significant
1458 * inefficiency -- possible, but unlikely in practice.
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001459 */
1460 res = PyTuple_New(2);
1461 if (res == NULL)
1462 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001463 if (mp->ma_used == 0) {
1464 Py_DECREF(res);
1465 PyErr_SetString(PyExc_KeyError,
1466 "popitem(): dictionary is empty");
1467 return NULL;
1468 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001469 /* Set ep to "the first" dict entry with a value. We abuse the hash
1470 * field of slot 0 to hold a search finger:
1471 * If slot 0 has a value, use slot 0.
1472 * Else slot 0 is being used to hold a search finger,
1473 * and we use its hash value as the first index to look.
1474 */
1475 ep = &mp->ma_table[0];
1476 if (ep->me_value == NULL) {
1477 i = (int)ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001478 /* The hash field may be a real hash value, or it may be a
1479 * legit search finger, or it may be a once-legit search
1480 * finger that's out of bounds now because it wrapped around
1481 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001482 */
Tim Petersafb6ae82001-06-04 21:00:21 +00001483 if (i > mp->ma_mask || i < 1)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001484 i = 1; /* skip slot 0 */
1485 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1486 i++;
Tim Petersafb6ae82001-06-04 21:00:21 +00001487 if (i > mp->ma_mask)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001488 i = 1;
1489 }
1490 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001491 PyTuple_SET_ITEM(res, 0, ep->me_key);
1492 PyTuple_SET_ITEM(res, 1, ep->me_value);
1493 Py_INCREF(dummy);
1494 ep->me_key = dummy;
1495 ep->me_value = NULL;
1496 mp->ma_used--;
1497 assert(mp->ma_table[0].me_value == NULL);
1498 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001499 return res;
1500}
1501
Jeremy Hylton8caad492000-06-23 14:18:11 +00001502static int
1503dict_traverse(PyObject *op, visitproc visit, void *arg)
1504{
1505 int i = 0, err;
1506 PyObject *pk;
1507 PyObject *pv;
1508
1509 while (PyDict_Next(op, &i, &pk, &pv)) {
1510 err = visit(pk, arg);
1511 if (err)
1512 return err;
1513 err = visit(pv, arg);
1514 if (err)
1515 return err;
1516 }
1517 return 0;
1518}
1519
1520static int
1521dict_tp_clear(PyObject *op)
1522{
1523 PyDict_Clear(op);
1524 return 0;
1525}
1526
Tim Petersf7f88b12000-12-13 23:18:45 +00001527
Guido van Rossum09e563a2001-05-01 12:10:21 +00001528staticforward PyObject *dictiter_new(dictobject *, binaryfunc);
1529
1530static PyObject *
1531select_key(PyObject *key, PyObject *value)
1532{
1533 Py_INCREF(key);
1534 return key;
1535}
1536
1537static PyObject *
1538select_value(PyObject *key, PyObject *value)
1539{
1540 Py_INCREF(value);
1541 return value;
1542}
1543
1544static PyObject *
1545select_item(PyObject *key, PyObject *value)
1546{
1547 PyObject *res = PyTuple_New(2);
1548
1549 if (res != NULL) {
1550 Py_INCREF(key);
1551 Py_INCREF(value);
1552 PyTuple_SET_ITEM(res, 0, key);
1553 PyTuple_SET_ITEM(res, 1, value);
1554 }
1555 return res;
1556}
1557
1558static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001559dict_iterkeys(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001560{
Guido van Rossum09e563a2001-05-01 12:10:21 +00001561 return dictiter_new(dict, select_key);
1562}
1563
1564static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001565dict_itervalues(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001566{
Guido van Rossum09e563a2001-05-01 12:10:21 +00001567 return dictiter_new(dict, select_value);
1568}
1569
1570static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001571dict_iteritems(dictobject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001572{
Guido van Rossum09e563a2001-05-01 12:10:21 +00001573 return dictiter_new(dict, select_item);
1574}
1575
1576
Tim Petersf7f88b12000-12-13 23:18:45 +00001577static char has_key__doc__[] =
1578"D.has_key(k) -> 1 if D has a key k, else 0";
1579
1580static char get__doc__[] =
1581"D.get(k[,d]) -> D[k] if D.has_key(k), else d. d defaults to None.";
1582
1583static char setdefault_doc__[] =
1584"D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if not D.has_key(k)";
1585
1586static char popitem__doc__[] =
1587"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
15882-tuple; but raise KeyError if D is empty";
1589
1590static char keys__doc__[] =
1591"D.keys() -> list of D's keys";
1592
1593static char items__doc__[] =
1594"D.items() -> list of D's (key, value) pairs, as 2-tuples";
1595
1596static char values__doc__[] =
1597"D.values() -> list of D's values";
1598
1599static char update__doc__[] =
1600"D.update(E) -> None. Update D from E: for k in E.keys(): D[k] = E[k]";
1601
1602static char clear__doc__[] =
1603"D.clear() -> None. Remove all items from D.";
1604
1605static char copy__doc__[] =
1606"D.copy() -> a shallow copy of D";
1607
Guido van Rossum09e563a2001-05-01 12:10:21 +00001608static char iterkeys__doc__[] =
1609"D.iterkeys() -> an iterator over the keys of D";
1610
1611static char itervalues__doc__[] =
1612"D.itervalues() -> an iterator over the values of D";
1613
1614static char iteritems__doc__[] =
1615"D.iteritems() -> an iterator over the (key, value) items of D";
1616
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001617static PyMethodDef mapp_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001618 {"has_key", (PyCFunction)dict_has_key, METH_O,
Tim Petersf7f88b12000-12-13 23:18:45 +00001619 has_key__doc__},
1620 {"get", (PyCFunction)dict_get, METH_VARARGS,
1621 get__doc__},
1622 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1623 setdefault_doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001624 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001625 popitem__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001626 {"keys", (PyCFunction)dict_keys, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001627 keys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001628 {"items", (PyCFunction)dict_items, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001629 items__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001630 {"values", (PyCFunction)dict_values, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001631 values__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001632 {"update", (PyCFunction)dict_update, METH_O,
Tim Petersf7f88b12000-12-13 23:18:45 +00001633 update__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001634 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001635 clear__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001636 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001637 copy__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001638 {"iterkeys", (PyCFunction)dict_iterkeys, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00001639 iterkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001640 {"itervalues", (PyCFunction)dict_itervalues, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00001641 itervalues__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001642 {"iteritems", (PyCFunction)dict_iteritems, METH_NOARGS,
Guido van Rossum09e563a2001-05-01 12:10:21 +00001643 iteritems__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00001644 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001645};
1646
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001647static int
1648dict_contains(dictobject *mp, PyObject *key)
1649{
1650 long hash;
1651
1652#ifdef CACHE_HASH
Tim Peters0ab085c2001-09-14 00:25:33 +00001653 if (!PyString_CheckExact(key) ||
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001654 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1655#endif
1656 {
1657 hash = PyObject_Hash(key);
1658 if (hash == -1)
1659 return -1;
1660 }
Tim Petersdea48ec2001-05-22 20:40:22 +00001661 return (mp->ma_lookup)(mp, key, hash)->me_value != NULL;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001662}
1663
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001664/* Hack to implement "key in dict" */
1665static PySequenceMethods dict_as_sequence = {
1666 0, /* sq_length */
1667 0, /* sq_concat */
1668 0, /* sq_repeat */
1669 0, /* sq_item */
1670 0, /* sq_slice */
1671 0, /* sq_ass_item */
1672 0, /* sq_ass_slice */
1673 (objobjproc)dict_contains, /* sq_contains */
1674 0, /* sq_inplace_concat */
1675 0, /* sq_inplace_repeat */
1676};
1677
Guido van Rossum09e563a2001-05-01 12:10:21 +00001678static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001679dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1680{
1681 PyObject *self;
1682
1683 assert(type != NULL && type->tp_alloc != NULL);
1684 self = type->tp_alloc(type, 0);
1685 if (self != NULL) {
1686 PyDictObject *d = (PyDictObject *)self;
1687 /* It's guaranteed that tp->alloc zeroed out the struct. */
1688 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
1689 INIT_NONZERO_DICT_SLOTS(d);
1690 d->ma_lookup = lookdict_string;
1691#ifdef SHOW_CONVERSION_COUNTS
1692 ++created;
1693#endif
1694 }
1695 return self;
1696}
1697
Tim Peters25786c02001-09-02 08:22:48 +00001698static int
1699dict_init(PyObject *self, PyObject *args, PyObject *kwds)
1700{
1701 PyObject *arg = NULL;
1702 static char *kwlist[] = {"mapping", 0};
1703
1704 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:dictionary",
1705 kwlist, &arg))
1706 return -1;
1707 if (arg != NULL) {
1708 if (PyDict_Merge(self, arg, 1) < 0) {
Tim Petersb95ec092001-09-02 18:35:54 +00001709 /* An error like "AttributeError: keys" is too
Tim Peters25786c02001-09-02 08:22:48 +00001710 cryptic in this context. */
1711 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1712 PyErr_SetString(PyExc_TypeError,
1713 "argument must be of a mapping type");
1714 }
1715 return -1;
1716 }
1717 }
1718 return 0;
1719}
1720
Tim Peters6d6c1a32001-08-02 04:15:00 +00001721static PyObject *
Guido van Rossum09e563a2001-05-01 12:10:21 +00001722dict_iter(dictobject *dict)
1723{
1724 return dictiter_new(dict, select_key);
1725}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001726
Tim Peters25786c02001-09-02 08:22:48 +00001727static char dictionary_doc[] =
1728"dictionary() -> new empty dictionary\n"
1729"dictionary(mapping) -> new dict initialized from mapping's key+value pairs";
1730
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001731PyTypeObject PyDict_Type = {
1732 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001733 0,
Guido van Rossum9bfef441993-03-29 10:43:31 +00001734 "dictionary",
Neil Schemenauere83c00e2001-08-29 23:54:21 +00001735 sizeof(dictobject),
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001736 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00001737 (destructor)dict_dealloc, /* tp_dealloc */
1738 (printfunc)dict_print, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001739 0, /* tp_getattr */
Guido van Rossumb9324202001-01-18 00:39:02 +00001740 0, /* tp_setattr */
Tim Peters4fa58bf2001-05-10 21:45:19 +00001741 (cmpfunc)dict_compare, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00001742 (reprfunc)dict_repr, /* tp_repr */
1743 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001744 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00001745 &dict_as_mapping, /* tp_as_mapping */
1746 0, /* tp_hash */
1747 0, /* tp_call */
1748 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001749 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumb9324202001-01-18 00:39:02 +00001750 0, /* tp_setattro */
1751 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00001752 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Tim Peters6d6c1a32001-08-02 04:15:00 +00001753 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters25786c02001-09-02 08:22:48 +00001754 dictionary_doc, /* tp_doc */
Guido van Rossumb9324202001-01-18 00:39:02 +00001755 (traverseproc)dict_traverse, /* tp_traverse */
1756 (inquiry)dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00001757 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001758 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00001759 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001760 0, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001761 mapp_methods, /* tp_methods */
1762 0, /* tp_members */
1763 0, /* tp_getset */
1764 0, /* tp_base */
1765 0, /* tp_dict */
1766 0, /* tp_descr_get */
1767 0, /* tp_descr_set */
1768 0, /* tp_dictoffset */
Tim Peters25786c02001-09-02 08:22:48 +00001769 (initproc)dict_init, /* tp_init */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001770 PyType_GenericAlloc, /* tp_alloc */
1771 dict_new, /* tp_new */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001772};
1773
Guido van Rossum3cca2451997-05-16 14:23:33 +00001774/* For backward compatibility with old dictionary interface */
1775
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001776PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001777PyDict_GetItemString(PyObject *v, char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001778{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001779 PyObject *kv, *rv;
1780 kv = PyString_FromString(key);
1781 if (kv == NULL)
1782 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00001783 rv = PyDict_GetItem(v, kv);
1784 Py_DECREF(kv);
1785 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001786}
1787
1788int
Tim Peters1f5871e2000-07-04 17:44:48 +00001789PyDict_SetItemString(PyObject *v, char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001790{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001791 PyObject *kv;
1792 int err;
1793 kv = PyString_FromString(key);
1794 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00001795 return -1;
Guido van Rossum4f3bf1e1997-09-29 23:31:11 +00001796 PyString_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00001797 err = PyDict_SetItem(v, kv, item);
1798 Py_DECREF(kv);
1799 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001800}
1801
1802int
Tim Peters1f5871e2000-07-04 17:44:48 +00001803PyDict_DelItemString(PyObject *v, char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001804{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001805 PyObject *kv;
1806 int err;
1807 kv = PyString_FromString(key);
1808 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00001809 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00001810 err = PyDict_DelItem(v, kv);
1811 Py_DECREF(kv);
1812 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001813}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001814
1815/* Dictionary iterator type */
1816
1817extern PyTypeObject PyDictIter_Type; /* Forward */
1818
1819typedef struct {
1820 PyObject_HEAD
1821 dictobject *di_dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001822 int di_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001823 int di_pos;
Guido van Rossum09e563a2001-05-01 12:10:21 +00001824 binaryfunc di_select;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001825} dictiterobject;
1826
1827static PyObject *
Guido van Rossum09e563a2001-05-01 12:10:21 +00001828dictiter_new(dictobject *dict, binaryfunc select)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001829{
1830 dictiterobject *di;
1831 di = PyObject_NEW(dictiterobject, &PyDictIter_Type);
1832 if (di == NULL)
1833 return NULL;
1834 Py_INCREF(dict);
1835 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001836 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001837 di->di_pos = 0;
Guido van Rossum09e563a2001-05-01 12:10:21 +00001838 di->di_select = select;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001839 return (PyObject *)di;
1840}
1841
1842static void
1843dictiter_dealloc(dictiterobject *di)
1844{
1845 Py_DECREF(di->di_dict);
1846 PyObject_DEL(di);
1847}
1848
1849static PyObject *
1850dictiter_next(dictiterobject *di, PyObject *args)
1851{
Guido van Rossum09e563a2001-05-01 12:10:21 +00001852 PyObject *key, *value;
Guido van Rossum213c7a62001-04-23 14:08:49 +00001853
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001854 if (di->di_used != di->di_dict->ma_used) {
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001855 PyErr_SetString(PyExc_RuntimeError,
1856 "dictionary changed size during iteration");
1857 return NULL;
1858 }
Guido van Rossum09e563a2001-05-01 12:10:21 +00001859 if (PyDict_Next((PyObject *)(di->di_dict), &di->di_pos, &key, &value)) {
1860 return (*di->di_select)(key, value);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001861 }
1862 PyErr_SetObject(PyExc_StopIteration, Py_None);
1863 return NULL;
1864}
1865
1866static PyObject *
1867dictiter_getiter(PyObject *it)
1868{
1869 Py_INCREF(it);
1870 return it;
1871}
1872
1873static PyMethodDef dictiter_methods[] = {
1874 {"next", (PyCFunction)dictiter_next, METH_VARARGS,
1875 "it.next() -- get the next value, or raise StopIteration"},
1876 {NULL, NULL} /* sentinel */
1877};
1878
Guido van Rossum213c7a62001-04-23 14:08:49 +00001879static PyObject *dictiter_iternext(dictiterobject *di)
1880{
Guido van Rossum09e563a2001-05-01 12:10:21 +00001881 PyObject *key, *value;
Guido van Rossum213c7a62001-04-23 14:08:49 +00001882
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001883 if (di->di_used != di->di_dict->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00001884 PyErr_SetString(PyExc_RuntimeError,
1885 "dictionary changed size during iteration");
1886 return NULL;
1887 }
Guido van Rossum09e563a2001-05-01 12:10:21 +00001888 if (PyDict_Next((PyObject *)(di->di_dict), &di->di_pos, &key, &value)) {
1889 return (*di->di_select)(key, value);
Guido van Rossum213c7a62001-04-23 14:08:49 +00001890 }
1891 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001892}
1893
1894PyTypeObject PyDictIter_Type = {
1895 PyObject_HEAD_INIT(&PyType_Type)
1896 0, /* ob_size */
1897 "dictionary-iterator", /* tp_name */
1898 sizeof(dictiterobject), /* tp_basicsize */
1899 0, /* tp_itemsize */
1900 /* methods */
1901 (destructor)dictiter_dealloc, /* tp_dealloc */
1902 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001903 0, /* tp_getattr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001904 0, /* tp_setattr */
1905 0, /* tp_compare */
1906 0, /* tp_repr */
1907 0, /* tp_as_number */
1908 0, /* tp_as_sequence */
1909 0, /* tp_as_mapping */
1910 0, /* tp_hash */
1911 0, /* tp_call */
1912 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001913 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001914 0, /* tp_setattro */
1915 0, /* tp_as_buffer */
1916 Py_TPFLAGS_DEFAULT, /* tp_flags */
1917 0, /* tp_doc */
1918 0, /* tp_traverse */
1919 0, /* tp_clear */
1920 0, /* tp_richcompare */
1921 0, /* tp_weaklistoffset */
1922 (getiterfunc)dictiter_getiter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001923 (iternextfunc)dictiter_iternext, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001924 dictiter_methods, /* tp_methods */
1925 0, /* tp_members */
1926 0, /* tp_getset */
1927 0, /* tp_base */
1928 0, /* tp_dict */
1929 0, /* tp_descr_get */
1930 0, /* tp_descr_set */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001931};