blob: ce4c578f421c1b11f3a7bfffc707ec4e63adbc56 [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 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000161 mp = PyObject_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
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000169 PyObject_GC_Init(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 *
301 * This really only becomes meaningful if proper error handling in lookdict()
302 * is too expensive.
303 */
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
314 /* make sure this function doesn't have to handle non-string keys */
315 if (!PyString_Check(key)) {
316#ifdef SHOW_CONVERSION_COUNTS
317 ++converted;
318#endif
319 mp->ma_lookup = lookdict;
320 return lookdict(mp, key, hash);
321 }
Tim Peters2f228e72001-05-13 00:19:31 +0000322 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000323 ep = &ep0[i];
324 if (ep->me_key == NULL || ep->me_key == key)
325 return ep;
326 if (ep->me_key == dummy)
327 freeslot = ep;
328 else {
329 if (ep->me_hash == hash
Martin v. Löwiscd353062001-05-24 16:56:35 +0000330 && _PyString_Eq(ep->me_key, key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000331 return ep;
332 }
333 freeslot = NULL;
334 }
Tim Peters15d49292001-05-27 07:39:22 +0000335
Tim Peters342c65e2001-05-13 06:43:53 +0000336 /* In the loop, me_key == dummy is by far (factor of 100s) the
337 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000338 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
339 i = (i << 2) + i + perturb + 1;
340 ep = &ep0[i & mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000341 if (ep->me_key == NULL)
342 return freeslot == NULL ? ep : freeslot;
343 if (ep->me_key == key
344 || (ep->me_hash == hash
345 && ep->me_key != dummy
Martin v. Löwiscd353062001-05-24 16:56:35 +0000346 && _PyString_Eq(ep->me_key, key)))
Fred Drake1bff34a2000-08-31 19:31:38 +0000347 return ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000348 if (ep->me_key == dummy && freeslot == NULL)
Tim Peters342c65e2001-05-13 06:43:53 +0000349 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000350 }
351}
352
353/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000354Internal routine to insert a new item into the table.
355Used both by the internal resize routine and by the public insert routine.
356Eats a reference to key and one to value.
357*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000358static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000359insertdict(register dictobject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000360{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000361 PyObject *old_value;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000362 register dictentry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000363 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
364
365 assert(mp->ma_lookup != NULL);
366 ep = mp->ma_lookup(mp, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000367 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000368 old_value = ep->me_value;
369 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000370 Py_DECREF(old_value); /* which **CAN** re-enter */
371 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000372 }
373 else {
374 if (ep->me_key == NULL)
375 mp->ma_fill++;
376 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000377 Py_DECREF(ep->me_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000378 ep->me_key = key;
379 ep->me_hash = hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000380 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000381 mp->ma_used++;
382 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000383}
384
385/*
386Restructure the table by allocating a new table and reinserting all
387items again. When entries have been deleted, the new table may
388actually be smaller than the old one.
389*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000390static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000391dictresize(dictobject *mp, int minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000392{
Tim Peterseb28ef22001-06-02 05:27:19 +0000393 int newsize;
Tim Peters0c6010b2001-05-23 23:33:57 +0000394 dictentry *oldtable, *newtable, *ep;
395 int i;
396 int is_oldtable_malloced;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000397 dictentry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000398
399 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000400
Tim Peterseb28ef22001-06-02 05:27:19 +0000401 /* Find the smallest table size > minused. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000402 for (newsize = PyDict_MINSIZE;
Tim Peterseb28ef22001-06-02 05:27:19 +0000403 newsize <= minused && newsize > 0;
404 newsize <<= 1)
405 ;
406 if (newsize <= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000407 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000408 return -1;
409 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000410
411 /* Get space for a new table. */
412 oldtable = mp->ma_table;
413 assert(oldtable != NULL);
414 is_oldtable_malloced = oldtable != mp->ma_smalltable;
415
Tim Peters6d6c1a32001-08-02 04:15:00 +0000416 if (newsize == PyDict_MINSIZE) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000417 /* A large table is shrinking, or we can't get any smaller. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000418 newtable = mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000419 if (newtable == oldtable) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000420 if (mp->ma_fill == mp->ma_used) {
421 /* No dummies, so no point doing anything. */
Tim Peters0c6010b2001-05-23 23:33:57 +0000422 return 0;
Tim Petersf8a548c2001-05-24 16:26:40 +0000423 }
424 /* We're not going to resize it, but rebuild the
425 table anyway to purge old dummy entries.
426 Subtle: This is *necessary* if fill==size,
427 as lookdict needs at least one virgin slot to
428 terminate failing searches. If fill < size, it's
429 merely desirable, as dummies slow searches. */
430 assert(mp->ma_fill > mp->ma_used);
Tim Peters0c6010b2001-05-23 23:33:57 +0000431 memcpy(small_copy, oldtable, sizeof(small_copy));
432 oldtable = small_copy;
433 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000434 }
435 else {
436 newtable = PyMem_NEW(dictentry, newsize);
437 if (newtable == NULL) {
438 PyErr_NoMemory();
439 return -1;
440 }
441 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000442
443 /* Make the dict empty, using the new table. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000444 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000445 mp->ma_table = newtable;
Tim Petersafb6ae82001-06-04 21:00:21 +0000446 mp->ma_mask = newsize - 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000447 memset(newtable, 0, sizeof(dictentry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000448 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000449 i = mp->ma_fill;
450 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000451
Tim Peters19283142001-05-17 22:25:34 +0000452 /* Copy the data over; this is refcount-neutral for active entries;
453 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000454 for (ep = oldtable; i > 0; ep++) {
455 if (ep->me_value != NULL) { /* active entry */
456 --i;
Tim Peters19283142001-05-17 22:25:34 +0000457 insertdict(mp, ep->me_key, ep->me_hash, ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000458 }
Tim Peters19283142001-05-17 22:25:34 +0000459 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000460 --i;
Tim Peters19283142001-05-17 22:25:34 +0000461 assert(ep->me_key == dummy);
462 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000463 }
Tim Peters19283142001-05-17 22:25:34 +0000464 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000465 }
466
Tim Peters0c6010b2001-05-23 23:33:57 +0000467 if (is_oldtable_malloced)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000468 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000469 return 0;
470}
471
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000472PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000473PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000474{
475 long hash;
Fred Drake1bff34a2000-08-31 19:31:38 +0000476 dictobject *mp = (dictobject *)op;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000477 if (!PyDict_Check(op)) {
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000478 return NULL;
479 }
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000480#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000481 if (!PyString_Check(key) ||
482 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000483#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000484 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000485 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000486 if (hash == -1) {
487 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000488 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000489 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000490 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000491 return (mp->ma_lookup)(mp, key, hash)->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000492}
493
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000494/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
495 * dictionary if it is merely replacing the value for an existing key.
496 * This is means that it's safe to loop over a dictionary with
497 * PyDict_Next() and occasionally replace a value -- but you can't
498 * insert new keys or remove them.
499 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000500int
Tim Peters1f5871e2000-07-04 17:44:48 +0000501PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000502{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000503 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000504 register long hash;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000505 register int n_used;
506
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000507 if (!PyDict_Check(op)) {
508 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000509 return -1;
510 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000511 mp = (dictobject *)op;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000512#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000513 if (PyString_Check(key)) {
Guido van Rossum2a61e741997-01-18 07:55:05 +0000514#ifdef INTERN_STRINGS
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000515 if (((PyStringObject *)key)->ob_sinterned != NULL) {
516 key = ((PyStringObject *)key)->ob_sinterned;
517 hash = ((PyStringObject *)key)->ob_shash;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000518 }
519 else
520#endif
521 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000522 hash = ((PyStringObject *)key)->ob_shash;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000523 if (hash == -1)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000524 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000525 }
526 }
527 else
528#endif
529 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000530 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000531 if (hash == -1)
532 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000533 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000534 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000535 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000536 Py_INCREF(value);
537 Py_INCREF(key);
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000538 insertdict(mp, key, hash, value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000539 /* If we added a key, we can safely resize. Otherwise skip this!
540 * If fill >= 2/3 size, adjust size. Normally, this doubles the
541 * size, but it's also possible for the dict to shrink (if ma_fill is
542 * much larger than ma_used, meaning a lot of dict keys have been
543 * deleted).
544 */
Tim Petersafb6ae82001-06-04 21:00:21 +0000545 if (mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2) {
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000546 if (dictresize(mp, mp->ma_used*2) != 0)
547 return -1;
548 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000549 return 0;
550}
551
552int
Tim Peters1f5871e2000-07-04 17:44:48 +0000553PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000554{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000555 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000556 register long hash;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000557 register dictentry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000558 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000559
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000560 if (!PyDict_Check(op)) {
561 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000562 return -1;
563 }
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000564#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000565 if (!PyString_Check(key) ||
566 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000567#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000568 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000569 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000570 if (hash == -1)
571 return -1;
572 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000573 mp = (dictobject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000574 ep = (mp->ma_lookup)(mp, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000575 if (ep->me_value == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000576 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000577 return -1;
578 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000579 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000580 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000581 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000582 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000583 ep->me_value = NULL;
584 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000585 Py_DECREF(old_value);
586 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000587 return 0;
588}
589
Guido van Rossum25831651993-05-19 14:50:45 +0000590void
Tim Peters1f5871e2000-07-04 17:44:48 +0000591PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000592{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000593 dictobject *mp;
Tim Petersdea48ec2001-05-22 20:40:22 +0000594 dictentry *ep, *table;
595 int table_is_malloced;
596 int fill;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000597 dictentry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000598#ifdef Py_DEBUG
599 int i, n;
600#endif
601
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000602 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000603 return;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000604 mp = (dictobject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000605#ifdef Py_DEBUG
Tim Petersafb6ae82001-06-04 21:00:21 +0000606 n = mp->ma_mask + 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000607 i = 0;
608#endif
609
610 table = mp->ma_table;
611 assert(table != NULL);
612 table_is_malloced = table != mp->ma_smalltable;
613
614 /* This is delicate. During the process of clearing the dict,
615 * decrefs can cause the dict to mutate. To avoid fatal confusion
616 * (voice of experience), we have to make the dict empty before
617 * clearing the slots, and never refer to anything via mp->xxx while
618 * clearing.
619 */
620 fill = mp->ma_fill;
621 if (table_is_malloced)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000622 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000623
624 else if (fill > 0) {
625 /* It's a small table with something that needs to be cleared.
626 * Afraid the only safe way is to copy the dict entries into
627 * another small table first.
628 */
629 memcpy(small_copy, table, sizeof(small_copy));
630 table = small_copy;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000631 EMPTY_TO_MINSIZE(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000632 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000633 /* else it's a small table that's already empty */
634
635 /* Now we can finally clear things. If C had refcounts, we could
636 * assert that the refcount on table is 1 now, i.e. that this function
637 * has unique access to it, so decref side-effects can't alter it.
638 */
639 for (ep = table; fill > 0; ++ep) {
640#ifdef Py_DEBUG
641 assert(i < n);
642 ++i;
643#endif
644 if (ep->me_key) {
645 --fill;
646 Py_DECREF(ep->me_key);
647 Py_XDECREF(ep->me_value);
648 }
649#ifdef Py_DEBUG
650 else
651 assert(ep->me_value == NULL);
652#endif
653 }
654
655 if (table_is_malloced)
656 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000657}
658
Tim Peters67830702001-03-21 19:23:56 +0000659/* CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
660 * mutates the dict. One exception: it is safe if the loop merely changes
661 * the values associated with the keys (but doesn't insert new keys or
662 * delete keys), via PyDict_SetItem().
663 */
Guido van Rossum25831651993-05-19 14:50:45 +0000664int
Tim Peters1f5871e2000-07-04 17:44:48 +0000665PyDict_Next(PyObject *op, int *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000666{
Guido van Rossum25831651993-05-19 14:50:45 +0000667 int i;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000668 register dictobject *mp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000669 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000670 return 0;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000671 mp = (dictobject *)op;
Guido van Rossum25831651993-05-19 14:50:45 +0000672 i = *ppos;
673 if (i < 0)
674 return 0;
Tim Petersafb6ae82001-06-04 21:00:21 +0000675 while (i <= mp->ma_mask && mp->ma_table[i].me_value == NULL)
Guido van Rossum25831651993-05-19 14:50:45 +0000676 i++;
677 *ppos = i+1;
Tim Petersafb6ae82001-06-04 21:00:21 +0000678 if (i > mp->ma_mask)
Guido van Rossum25831651993-05-19 14:50:45 +0000679 return 0;
680 if (pkey)
681 *pkey = mp->ma_table[i].me_key;
682 if (pvalue)
683 *pvalue = mp->ma_table[i].me_value;
684 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000685}
686
687/* Methods */
688
689static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000690dict_dealloc(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000691{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000692 register dictentry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000693 int fill = mp->ma_fill;
Guido van Rossumd724b232000-03-13 16:01:29 +0000694 Py_TRASHCAN_SAFE_BEGIN(mp)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000695 PyObject_GC_Fini(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000696 for (ep = mp->ma_table; fill > 0; ep++) {
697 if (ep->me_key) {
698 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000699 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000700 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000701 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000702 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000703 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000704 PyMem_DEL(mp->ma_table);
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000705 mp = (dictobject *) PyObject_AS_GC(mp);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000706 PyObject_DEL(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000707 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000708}
709
710static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000711dict_print(register dictobject *mp, register FILE *fp, register int flags)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000712{
713 register int i;
714 register int any;
Guido van Rossum255443b1998-04-10 22:47:14 +0000715
716 i = Py_ReprEnter((PyObject*)mp);
717 if (i != 0) {
718 if (i < 0)
719 return i;
720 fprintf(fp, "{...}");
721 return 0;
722 }
723
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000724 fprintf(fp, "{");
725 any = 0;
Tim Petersafb6ae82001-06-04 21:00:21 +0000726 for (i = 0; i <= mp->ma_mask; i++) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000727 dictentry *ep = mp->ma_table + i;
728 PyObject *pvalue = ep->me_value;
729 if (pvalue != NULL) {
730 /* Prevent PyObject_Repr from deleting value during
731 key format */
732 Py_INCREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000733 if (any++ > 0)
734 fprintf(fp, ", ");
Guido van Rossum255443b1998-04-10 22:47:14 +0000735 if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000736 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000737 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000738 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000739 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000740 fprintf(fp, ": ");
Tim Peters19b77cf2001-06-02 08:27:39 +0000741 if (PyObject_Print(pvalue, fp, 0) != 0) {
Tim Peters23cf6be2001-06-02 08:02:56 +0000742 Py_DECREF(pvalue);
Guido van Rossum255443b1998-04-10 22:47:14 +0000743 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000744 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000745 }
Tim Peters23cf6be2001-06-02 08:02:56 +0000746 Py_DECREF(pvalue);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000747 }
748 }
749 fprintf(fp, "}");
Guido van Rossum255443b1998-04-10 22:47:14 +0000750 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000751 return 0;
752}
753
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000754static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000755dict_repr(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000756{
Tim Petersc6057842001-06-16 07:52:53 +0000757 int i;
Tim Petersa7259592001-06-16 05:11:17 +0000758 PyObject *s, *temp, *colon = NULL;
759 PyObject *pieces = NULL, *result = NULL;
760 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +0000761
Tim Petersa7259592001-06-16 05:11:17 +0000762 i = Py_ReprEnter((PyObject *)mp);
Guido van Rossum255443b1998-04-10 22:47:14 +0000763 if (i != 0) {
Tim Petersa7259592001-06-16 05:11:17 +0000764 return i > 0 ? PyString_FromString("{...}") : NULL;
Guido van Rossum255443b1998-04-10 22:47:14 +0000765 }
766
Tim Petersa7259592001-06-16 05:11:17 +0000767 if (mp->ma_used == 0) {
768 result = PyString_FromString("{}");
769 goto Done;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000770 }
Tim Petersa7259592001-06-16 05:11:17 +0000771
772 pieces = PyList_New(0);
773 if (pieces == NULL)
774 goto Done;
775
776 colon = PyString_FromString(": ");
777 if (colon == NULL)
778 goto Done;
779
780 /* Do repr() on each key+value pair, and insert ": " between them.
781 Note that repr may mutate the dict. */
Tim Petersc6057842001-06-16 07:52:53 +0000782 i = 0;
783 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Tim Petersa7259592001-06-16 05:11:17 +0000784 int status;
785 /* Prevent repr from deleting value during key format. */
786 Py_INCREF(value);
787 s = PyObject_Repr(key);
788 PyString_Concat(&s, colon);
789 PyString_ConcatAndDel(&s, PyObject_Repr(value));
790 Py_DECREF(value);
791 if (s == NULL)
792 goto Done;
793 status = PyList_Append(pieces, s);
794 Py_DECREF(s); /* append created a new ref */
795 if (status < 0)
796 goto Done;
797 }
798
799 /* Add "{}" decorations to the first and last items. */
800 assert(PyList_GET_SIZE(pieces) > 0);
801 s = PyString_FromString("{");
802 if (s == NULL)
803 goto Done;
804 temp = PyList_GET_ITEM(pieces, 0);
805 PyString_ConcatAndDel(&s, temp);
806 PyList_SET_ITEM(pieces, 0, s);
807 if (s == NULL)
808 goto Done;
809
810 s = PyString_FromString("}");
811 if (s == NULL)
812 goto Done;
813 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
814 PyString_ConcatAndDel(&temp, s);
815 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
816 if (temp == NULL)
817 goto Done;
818
819 /* Paste them all together with ", " between. */
820 s = PyString_FromString(", ");
821 if (s == NULL)
822 goto Done;
823 result = _PyString_Join(s, pieces);
824 Py_DECREF(s);
825
826Done:
827 Py_XDECREF(pieces);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000828 Py_XDECREF(colon);
Tim Petersa7259592001-06-16 05:11:17 +0000829 Py_ReprLeave((PyObject *)mp);
830 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000831}
832
833static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000834dict_length(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000835{
836 return mp->ma_used;
837}
838
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000839static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000840dict_subscript(dictobject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000841{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000842 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000843 long hash;
Tim Petersdea48ec2001-05-22 20:40:22 +0000844 assert(mp->ma_table != NULL);
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000845#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000846 if (!PyString_Check(key) ||
847 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000848#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000849 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000850 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000851 if (hash == -1)
852 return NULL;
853 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000854 v = (mp->ma_lookup)(mp, key, hash) -> me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000855 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000856 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000857 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000858 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000859 return v;
860}
861
862static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000863dict_ass_sub(dictobject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000864{
865 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000866 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000867 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000868 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000869}
870
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000871static PyMappingMethods dict_as_mapping = {
872 (inquiry)dict_length, /*mp_length*/
873 (binaryfunc)dict_subscript, /*mp_subscript*/
874 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000875};
876
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000877static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000878dict_keys(register dictobject *mp, PyObject *args)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000879{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000880 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000881 register int i, j, n;
882
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000883 if (!PyArg_NoArgs(args))
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000884 return NULL;
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 *
Tim Peters1f5871e2000-07-04 17:44:48 +0000909dict_values(register dictobject *mp, PyObject *args)
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 Rossumc0b618a1997-05-02 03:12:38 +0000914 if (!PyArg_NoArgs(args))
Guido van Rossum25831651993-05-19 14:50:45 +0000915 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000916 again:
917 n = mp->ma_used;
918 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +0000919 if (v == NULL)
920 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000921 if (n != mp->ma_used) {
922 /* Durnit. The allocations caused the dict to resize.
923 * Just start over, this shouldn't normally happen.
924 */
925 Py_DECREF(v);
926 goto again;
927 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000928 for (i = 0, j = 0; i <= mp->ma_mask; i++) {
Guido van Rossum25831651993-05-19 14:50:45 +0000929 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000930 PyObject *value = mp->ma_table[i].me_value;
931 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000932 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +0000933 j++;
934 }
935 }
936 return v;
937}
938
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000939static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000940dict_items(register dictobject *mp, PyObject *args)
Guido van Rossum25831651993-05-19 14:50:45 +0000941{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000942 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000943 register int i, j, n;
944 PyObject *item, *key, *value;
945
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000946 if (!PyArg_NoArgs(args))
Guido van Rossum25831651993-05-19 14:50:45 +0000947 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000948 /* Preallocate the list of tuples, to avoid allocations during
949 * the loop over the items, which could trigger GC, which
950 * could resize the dict. :-(
951 */
952 again:
953 n = mp->ma_used;
954 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +0000955 if (v == NULL)
956 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000957 for (i = 0; i < n; i++) {
958 item = PyTuple_New(2);
959 if (item == NULL) {
960 Py_DECREF(v);
961 return NULL;
962 }
963 PyList_SET_ITEM(v, i, item);
964 }
965 if (n != mp->ma_used) {
966 /* Durnit. The allocations caused the dict to resize.
967 * Just start over, this shouldn't normally happen.
968 */
969 Py_DECREF(v);
970 goto again;
971 }
972 /* Nothing we do below makes any function calls. */
Tim Petersafb6ae82001-06-04 21:00:21 +0000973 for (i = 0, j = 0; i <= mp->ma_mask; i++) {
Guido van Rossum25831651993-05-19 14:50:45 +0000974 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000975 key = mp->ma_table[i].me_key;
976 value = mp->ma_table[i].me_value;
977 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000978 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000979 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000980 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000981 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +0000982 j++;
983 }
984 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000985 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +0000986 return v;
987}
988
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000989static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000990dict_update(PyObject *mp, PyObject *args)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000991{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000992 PyObject *other;
993
994 if (!PyArg_ParseTuple(args, "O:update", &other))
995 return NULL;
996 if (PyDict_Update(mp, other) < 0)
997 return NULL;
998 Py_INCREF(Py_None);
999 return Py_None;
1000}
1001
1002int
1003PyDict_Update(PyObject *a, PyObject *b)
1004{
1005 register PyDictObject *mp, *other;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001006 register int i;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001007 dictentry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001008
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001009 /* We accept for the argument either a concrete dictionary object,
1010 * or an abstract "mapping" object. For the former, we can do
1011 * things quite efficiently. For the latter, we only require that
1012 * PyMapping_Keys() and PyObject_GetItem() be supported.
1013 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001014 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1015 PyErr_BadInternalCall();
1016 return -1;
1017 }
1018 mp = (dictobject*)a;
1019 if (PyDict_Check(b)) {
1020 other = (dictobject*)b;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001021 if (other == mp || other->ma_used == 0)
1022 /* a.update(a) or a.update({}); nothing to do */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001023 return 0;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001024 /* Do one big resize at the start, rather than
1025 * incrementally resizing as we insert new items. Expect
1026 * that there will be no (or few) overlapping keys.
1027 */
1028 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
1029 if (dictresize(mp, (mp->ma_used + other->ma_used)*3/2) != 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001030 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001031 }
1032 for (i = 0; i <= other->ma_mask; i++) {
1033 entry = &other->ma_table[i];
1034 if (entry->me_value != NULL) {
1035 Py_INCREF(entry->me_key);
1036 Py_INCREF(entry->me_value);
1037 insertdict(mp, entry->me_key, entry->me_hash,
1038 entry->me_value);
1039 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001040 }
1041 }
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001042 else {
1043 /* Do it the generic, slower way */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001044 PyObject *keys = PyMapping_Keys(b);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001045 PyObject *iter;
1046 PyObject *key, *value;
1047 int status;
1048
1049 if (keys == NULL)
1050 /* Docstring says this is equivalent to E.keys() so
1051 * if E doesn't have a .keys() method we want
1052 * AttributeError to percolate up. Might as well
1053 * do the same for any other error.
1054 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001055 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001056
1057 iter = PyObject_GetIter(keys);
1058 Py_DECREF(keys);
1059 if (iter == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001060 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001061
1062 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001063 value = PyObject_GetItem(b, key);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001064 if (value == NULL) {
1065 Py_DECREF(iter);
1066 Py_DECREF(key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001067 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001068 }
1069 status = PyDict_SetItem((PyObject*)mp, key, value);
1070 Py_DECREF(key);
1071 Py_DECREF(value);
1072 if (status < 0) {
1073 Py_DECREF(iter);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001074 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001075 }
1076 }
1077 Py_DECREF(iter);
1078 if (PyErr_Occurred())
1079 /* Iterator completed, via error */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001080 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001081 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001082 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001083}
1084
1085static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001086dict_copy(register dictobject *mp, PyObject *args)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001087{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001088 if (!PyArg_Parse(args, ""))
1089 return NULL;
1090 return PyDict_Copy((PyObject*)mp);
1091}
1092
1093PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001094PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001095{
1096 register dictobject *mp;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001097 register int i;
1098 dictobject *copy;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001099 dictentry *entry;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001100
1101 if (o == NULL || !PyDict_Check(o)) {
1102 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001103 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001104 }
1105 mp = (dictobject *)o;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001106 copy = (dictobject *)PyDict_New();
1107 if (copy == NULL)
1108 return NULL;
1109 if (mp->ma_used > 0) {
1110 if (dictresize(copy, mp->ma_used*3/2) != 0)
1111 return NULL;
Tim Petersafb6ae82001-06-04 21:00:21 +00001112 for (i = 0; i <= mp->ma_mask; i++) {
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001113 entry = &mp->ma_table[i];
1114 if (entry->me_value != NULL) {
1115 Py_INCREF(entry->me_key);
1116 Py_INCREF(entry->me_value);
1117 insertdict(copy, entry->me_key, entry->me_hash,
1118 entry->me_value);
1119 }
1120 }
1121 }
1122 return (PyObject *)copy;
1123}
1124
Guido van Rossum4199fac1993-11-05 10:18:44 +00001125int
Tim Peters1f5871e2000-07-04 17:44:48 +00001126PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001127{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001128 if (mp == NULL || !PyDict_Check(mp)) {
1129 PyErr_BadInternalCall();
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001130 return 0;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001131 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001132 return ((dictobject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001133}
1134
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001135PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001136PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001137{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001138 if (mp == NULL || !PyDict_Check(mp)) {
1139 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001140 return NULL;
1141 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001142 return dict_keys((dictobject *)mp, (PyObject *)NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001143}
1144
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001145PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001146PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001147{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001148 if (mp == NULL || !PyDict_Check(mp)) {
1149 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001150 return NULL;
1151 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001152 return dict_values((dictobject *)mp, (PyObject *)NULL);
Guido van Rossum25831651993-05-19 14:50:45 +00001153}
1154
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001155PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001156PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001157{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001158 if (mp == NULL || !PyDict_Check(mp)) {
1159 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001160 return NULL;
1161 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001162 return dict_items((dictobject *)mp, (PyObject *)NULL);
Guido van Rossum25831651993-05-19 14:50:45 +00001163}
1164
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001165/* Subroutine which returns the smallest key in a for which b's value
1166 is different or absent. The value is returned too, through the
Tim Peters95bf9392001-05-10 08:32:44 +00001167 pval argument. Both are NULL if no key in a is found for which b's status
1168 differs. The refcounts on (and only on) non-NULL *pval and function return
1169 values must be decremented by the caller (characterize() increments them
1170 to ensure that mutating comparison and PyDict_GetItem calls can't delete
1171 them before the caller is done looking at them). */
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001172
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001173static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001174characterize(dictobject *a, dictobject *b, PyObject **pval)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001175{
Tim Peters95bf9392001-05-10 08:32:44 +00001176 PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
1177 PyObject *aval = NULL; /* a[akey] */
Guido van Rossumb9324202001-01-18 00:39:02 +00001178 int i, cmp;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001179
Tim Petersafb6ae82001-06-04 21:00:21 +00001180 for (i = 0; i <= a->ma_mask; i++) {
Tim Peters95bf9392001-05-10 08:32:44 +00001181 PyObject *thiskey, *thisaval, *thisbval;
1182 if (a->ma_table[i].me_value == NULL)
1183 continue;
1184 thiskey = a->ma_table[i].me_key;
1185 Py_INCREF(thiskey); /* keep alive across compares */
1186 if (akey != NULL) {
1187 cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
1188 if (cmp < 0) {
1189 Py_DECREF(thiskey);
1190 goto Fail;
1191 }
1192 if (cmp > 0 ||
Tim Petersafb6ae82001-06-04 21:00:21 +00001193 i > a->ma_mask ||
Tim Peters95bf9392001-05-10 08:32:44 +00001194 a->ma_table[i].me_value == NULL)
1195 {
1196 /* Not the *smallest* a key; or maybe it is
1197 * but the compare shrunk the dict so we can't
1198 * find its associated value anymore; or
1199 * maybe it is but the compare deleted the
1200 * a[thiskey] entry.
1201 */
1202 Py_DECREF(thiskey);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001203 continue;
Guido van Rossumb9324202001-01-18 00:39:02 +00001204 }
Tim Peters95bf9392001-05-10 08:32:44 +00001205 }
1206
1207 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
1208 thisaval = a->ma_table[i].me_value;
1209 assert(thisaval);
1210 Py_INCREF(thisaval); /* keep alive */
1211 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
1212 if (thisbval == NULL)
1213 cmp = 0;
1214 else {
1215 /* both dicts have thiskey: same values? */
1216 cmp = PyObject_RichCompareBool(
1217 thisaval, thisbval, Py_EQ);
1218 if (cmp < 0) {
1219 Py_DECREF(thiskey);
1220 Py_DECREF(thisaval);
1221 goto Fail;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001222 }
1223 }
Tim Peters95bf9392001-05-10 08:32:44 +00001224 if (cmp == 0) {
1225 /* New winner. */
1226 Py_XDECREF(akey);
1227 Py_XDECREF(aval);
1228 akey = thiskey;
1229 aval = thisaval;
1230 }
1231 else {
1232 Py_DECREF(thiskey);
1233 Py_DECREF(thisaval);
1234 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001235 }
Tim Peters95bf9392001-05-10 08:32:44 +00001236 *pval = aval;
1237 return akey;
1238
1239Fail:
1240 Py_XDECREF(akey);
1241 Py_XDECREF(aval);
1242 *pval = NULL;
1243 return NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001244}
1245
1246static int
Tim Peters1f5871e2000-07-04 17:44:48 +00001247dict_compare(dictobject *a, dictobject *b)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001248{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001249 PyObject *adiff, *bdiff, *aval, *bval;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001250 int res;
1251
1252 /* Compare lengths first */
1253 if (a->ma_used < b->ma_used)
1254 return -1; /* a is shorter */
1255 else if (a->ma_used > b->ma_used)
1256 return 1; /* b is shorter */
Tim Peters95bf9392001-05-10 08:32:44 +00001257
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001258 /* Same length -- check all keys */
Tim Peters95bf9392001-05-10 08:32:44 +00001259 bdiff = bval = NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001260 adiff = characterize(a, b, &aval);
Tim Peters95bf9392001-05-10 08:32:44 +00001261 if (adiff == NULL) {
1262 assert(!aval);
Tim Peters3918fb22001-05-10 18:58:31 +00001263 /* Either an error, or a is a subset with the same length so
Tim Peters95bf9392001-05-10 08:32:44 +00001264 * must be equal.
1265 */
1266 res = PyErr_Occurred() ? -1 : 0;
1267 goto Finished;
1268 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001269 bdiff = characterize(b, a, &bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001270 if (bdiff == NULL && PyErr_Occurred()) {
1271 assert(!bval);
1272 res = -1;
1273 goto Finished;
1274 }
1275 res = 0;
1276 if (bdiff) {
1277 /* bdiff == NULL "should be" impossible now, but perhaps
1278 * the last comparison done by the characterize() on a had
1279 * the side effect of making the dicts equal!
1280 */
1281 res = PyObject_Compare(adiff, bdiff);
1282 }
1283 if (res == 0 && bval != NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001284 res = PyObject_Compare(aval, bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001285
1286Finished:
1287 Py_XDECREF(adiff);
1288 Py_XDECREF(bdiff);
1289 Py_XDECREF(aval);
1290 Py_XDECREF(bval);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001291 return res;
1292}
1293
Tim Peterse63415e2001-05-08 04:38:29 +00001294/* Return 1 if dicts equal, 0 if not, -1 if error.
1295 * Gets out as soon as any difference is detected.
1296 * Uses only Py_EQ comparison.
1297 */
1298static int
1299dict_equal(dictobject *a, dictobject *b)
1300{
1301 int i;
1302
1303 if (a->ma_used != b->ma_used)
1304 /* can't be equal if # of entries differ */
1305 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001306
Tim Peterse63415e2001-05-08 04:38:29 +00001307 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Tim Petersafb6ae82001-06-04 21:00:21 +00001308 for (i = 0; i <= a->ma_mask; i++) {
Tim Peterse63415e2001-05-08 04:38:29 +00001309 PyObject *aval = a->ma_table[i].me_value;
1310 if (aval != NULL) {
1311 int cmp;
1312 PyObject *bval;
1313 PyObject *key = a->ma_table[i].me_key;
1314 /* temporarily bump aval's refcount to ensure it stays
1315 alive until we're done with it */
1316 Py_INCREF(aval);
1317 bval = PyDict_GetItem((PyObject *)b, key);
1318 if (bval == NULL) {
1319 Py_DECREF(aval);
1320 return 0;
1321 }
1322 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1323 Py_DECREF(aval);
1324 if (cmp <= 0) /* error or not equal */
1325 return cmp;
1326 }
1327 }
1328 return 1;
1329 }
1330
1331static PyObject *
1332dict_richcompare(PyObject *v, PyObject *w, int op)
1333{
1334 int cmp;
1335 PyObject *res;
1336
1337 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1338 res = Py_NotImplemented;
1339 }
1340 else if (op == Py_EQ || op == Py_NE) {
1341 cmp = dict_equal((dictobject *)v, (dictobject *)w);
1342 if (cmp < 0)
1343 return NULL;
1344 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1345 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001346 else
1347 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001348 Py_INCREF(res);
1349 return res;
1350 }
1351
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001352static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001353dict_has_key(register dictobject *mp, PyObject *args)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001354{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001355 PyObject *key;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001356 long hash;
1357 register long ok;
Fred Drake52fccfd2000-02-23 15:47:16 +00001358 if (!PyArg_ParseTuple(args, "O:has_key", &key))
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001359 return NULL;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001360#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001361 if (!PyString_Check(key) ||
1362 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001363#endif
Guido van Rossumca756f21997-01-23 19:39:29 +00001364 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001365 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001366 if (hash == -1)
1367 return NULL;
1368 }
Tim Petersdea48ec2001-05-22 20:40:22 +00001369 ok = (mp->ma_lookup)(mp, key, hash)->me_value != NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001370 return PyInt_FromLong(ok);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001371}
1372
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001373static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001374dict_get(register dictobject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001375{
1376 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001377 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001378 PyObject *val = NULL;
1379 long hash;
1380
Fred Drake52fccfd2000-02-23 15:47:16 +00001381 if (!PyArg_ParseTuple(args, "O|O:get", &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001382 return NULL;
1383
Barry Warsawc38c5da1997-10-06 17:49:20 +00001384#ifdef CACHE_HASH
1385 if (!PyString_Check(key) ||
1386 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1387#endif
1388 {
1389 hash = PyObject_Hash(key);
1390 if (hash == -1)
1391 return NULL;
1392 }
Fred Drake1bff34a2000-08-31 19:31:38 +00001393 val = (mp->ma_lookup)(mp, key, hash)->me_value;
Barry Warsaw320ac331997-10-20 17:26:25 +00001394
Barry Warsawc38c5da1997-10-06 17:49:20 +00001395 if (val == NULL)
1396 val = failobj;
1397 Py_INCREF(val);
1398 return val;
1399}
1400
1401
1402static PyObject *
Guido van Rossum164452c2000-08-08 16:12:54 +00001403dict_setdefault(register dictobject *mp, PyObject *args)
1404{
1405 PyObject *key;
1406 PyObject *failobj = Py_None;
1407 PyObject *val = NULL;
1408 long hash;
1409
1410 if (!PyArg_ParseTuple(args, "O|O:setdefault", &key, &failobj))
1411 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001412
1413#ifdef CACHE_HASH
1414 if (!PyString_Check(key) ||
1415 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1416#endif
1417 {
1418 hash = PyObject_Hash(key);
1419 if (hash == -1)
1420 return NULL;
1421 }
Fred Drake1bff34a2000-08-31 19:31:38 +00001422 val = (mp->ma_lookup)(mp, key, hash)->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001423 if (val == NULL) {
1424 val = failobj;
1425 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1426 val = NULL;
1427 }
1428 Py_XINCREF(val);
1429 return val;
1430}
1431
1432
1433static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001434dict_clear(register dictobject *mp, PyObject *args)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001435{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001436 if (!PyArg_NoArgs(args))
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001437 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001438 PyDict_Clear((PyObject *)mp);
1439 Py_INCREF(Py_None);
1440 return Py_None;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001441}
1442
Guido van Rossumba6ab842000-12-12 22:02:18 +00001443static PyObject *
1444dict_popitem(dictobject *mp, PyObject *args)
1445{
1446 int i = 0;
1447 dictentry *ep;
1448 PyObject *res;
1449
1450 if (!PyArg_NoArgs(args))
1451 return NULL;
Tim Petersf4b33f62001-06-02 05:42:29 +00001452 /* Allocate the result tuple before checking the size. Believe it
1453 * or not, this allocation could trigger a garbage collection which
1454 * could empty the dict, so if we checked the size first and that
1455 * happened, the result would be an infinite loop (searching for an
1456 * entry that no longer exists). Note that the usual popitem()
1457 * idiom is "while d: k, v = d.popitem()". so needing to throw the
1458 * tuple away if the dict *is* empty isn't a significant
1459 * inefficiency -- possible, but unlikely in practice.
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001460 */
1461 res = PyTuple_New(2);
1462 if (res == NULL)
1463 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001464 if (mp->ma_used == 0) {
1465 Py_DECREF(res);
1466 PyErr_SetString(PyExc_KeyError,
1467 "popitem(): dictionary is empty");
1468 return NULL;
1469 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001470 /* Set ep to "the first" dict entry with a value. We abuse the hash
1471 * field of slot 0 to hold a search finger:
1472 * If slot 0 has a value, use slot 0.
1473 * Else slot 0 is being used to hold a search finger,
1474 * and we use its hash value as the first index to look.
1475 */
1476 ep = &mp->ma_table[0];
1477 if (ep->me_value == NULL) {
1478 i = (int)ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001479 /* The hash field may be a real hash value, or it may be a
1480 * legit search finger, or it may be a once-legit search
1481 * finger that's out of bounds now because it wrapped around
1482 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001483 */
Tim Petersafb6ae82001-06-04 21:00:21 +00001484 if (i > mp->ma_mask || i < 1)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001485 i = 1; /* skip slot 0 */
1486 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1487 i++;
Tim Petersafb6ae82001-06-04 21:00:21 +00001488 if (i > mp->ma_mask)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001489 i = 1;
1490 }
1491 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001492 PyTuple_SET_ITEM(res, 0, ep->me_key);
1493 PyTuple_SET_ITEM(res, 1, ep->me_value);
1494 Py_INCREF(dummy);
1495 ep->me_key = dummy;
1496 ep->me_value = NULL;
1497 mp->ma_used--;
1498 assert(mp->ma_table[0].me_value == NULL);
1499 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001500 return res;
1501}
1502
Jeremy Hylton8caad492000-06-23 14:18:11 +00001503static int
1504dict_traverse(PyObject *op, visitproc visit, void *arg)
1505{
1506 int i = 0, err;
1507 PyObject *pk;
1508 PyObject *pv;
1509
1510 while (PyDict_Next(op, &i, &pk, &pv)) {
1511 err = visit(pk, arg);
1512 if (err)
1513 return err;
1514 err = visit(pv, arg);
1515 if (err)
1516 return err;
1517 }
1518 return 0;
1519}
1520
1521static int
1522dict_tp_clear(PyObject *op)
1523{
1524 PyDict_Clear(op);
1525 return 0;
1526}
1527
Tim Petersf7f88b12000-12-13 23:18:45 +00001528
Guido van Rossum09e563a2001-05-01 12:10:21 +00001529staticforward PyObject *dictiter_new(dictobject *, binaryfunc);
1530
1531static PyObject *
1532select_key(PyObject *key, PyObject *value)
1533{
1534 Py_INCREF(key);
1535 return key;
1536}
1537
1538static PyObject *
1539select_value(PyObject *key, PyObject *value)
1540{
1541 Py_INCREF(value);
1542 return value;
1543}
1544
1545static PyObject *
1546select_item(PyObject *key, PyObject *value)
1547{
1548 PyObject *res = PyTuple_New(2);
1549
1550 if (res != NULL) {
1551 Py_INCREF(key);
1552 Py_INCREF(value);
1553 PyTuple_SET_ITEM(res, 0, key);
1554 PyTuple_SET_ITEM(res, 1, value);
1555 }
1556 return res;
1557}
1558
1559static PyObject *
1560dict_iterkeys(dictobject *dict, PyObject *args)
1561{
1562 if (!PyArg_ParseTuple(args, ""))
1563 return NULL;
1564 return dictiter_new(dict, select_key);
1565}
1566
1567static PyObject *
1568dict_itervalues(dictobject *dict, PyObject *args)
1569{
1570 if (!PyArg_ParseTuple(args, ""))
1571 return NULL;
1572 return dictiter_new(dict, select_value);
1573}
1574
1575static PyObject *
1576dict_iteritems(dictobject *dict, PyObject *args)
1577{
1578 if (!PyArg_ParseTuple(args, ""))
1579 return NULL;
1580 return dictiter_new(dict, select_item);
1581}
1582
1583
Tim Petersf7f88b12000-12-13 23:18:45 +00001584static char has_key__doc__[] =
1585"D.has_key(k) -> 1 if D has a key k, else 0";
1586
1587static char get__doc__[] =
1588"D.get(k[,d]) -> D[k] if D.has_key(k), else d. d defaults to None.";
1589
1590static char setdefault_doc__[] =
1591"D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if not D.has_key(k)";
1592
1593static char popitem__doc__[] =
1594"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
15952-tuple; but raise KeyError if D is empty";
1596
1597static char keys__doc__[] =
1598"D.keys() -> list of D's keys";
1599
1600static char items__doc__[] =
1601"D.items() -> list of D's (key, value) pairs, as 2-tuples";
1602
1603static char values__doc__[] =
1604"D.values() -> list of D's values";
1605
1606static char update__doc__[] =
1607"D.update(E) -> None. Update D from E: for k in E.keys(): D[k] = E[k]";
1608
1609static char clear__doc__[] =
1610"D.clear() -> None. Remove all items from D.";
1611
1612static char copy__doc__[] =
1613"D.copy() -> a shallow copy of D";
1614
Guido van Rossum09e563a2001-05-01 12:10:21 +00001615static char iterkeys__doc__[] =
1616"D.iterkeys() -> an iterator over the keys of D";
1617
1618static char itervalues__doc__[] =
1619"D.itervalues() -> an iterator over the values of D";
1620
1621static char iteritems__doc__[] =
1622"D.iteritems() -> an iterator over the (key, value) items of D";
1623
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001624static PyMethodDef mapp_methods[] = {
Tim Petersf7f88b12000-12-13 23:18:45 +00001625 {"has_key", (PyCFunction)dict_has_key, METH_VARARGS,
1626 has_key__doc__},
1627 {"get", (PyCFunction)dict_get, METH_VARARGS,
1628 get__doc__},
1629 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1630 setdefault_doc__},
1631 {"popitem", (PyCFunction)dict_popitem, METH_OLDARGS,
1632 popitem__doc__},
1633 {"keys", (PyCFunction)dict_keys, METH_OLDARGS,
1634 keys__doc__},
1635 {"items", (PyCFunction)dict_items, METH_OLDARGS,
1636 items__doc__},
1637 {"values", (PyCFunction)dict_values, METH_OLDARGS,
1638 values__doc__},
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001639 {"update", (PyCFunction)dict_update, METH_VARARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001640 update__doc__},
1641 {"clear", (PyCFunction)dict_clear, METH_OLDARGS,
1642 clear__doc__},
1643 {"copy", (PyCFunction)dict_copy, METH_OLDARGS,
1644 copy__doc__},
Guido van Rossum09e563a2001-05-01 12:10:21 +00001645 {"iterkeys", (PyCFunction)dict_iterkeys, METH_VARARGS,
1646 iterkeys__doc__},
1647 {"itervalues", (PyCFunction)dict_itervalues, METH_VARARGS,
1648 itervalues__doc__},
1649 {"iteritems", (PyCFunction)dict_iteritems, METH_VARARGS,
1650 iteritems__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00001651 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001652};
1653
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001654static int
1655dict_contains(dictobject *mp, PyObject *key)
1656{
1657 long hash;
1658
1659#ifdef CACHE_HASH
1660 if (!PyString_Check(key) ||
1661 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1662#endif
1663 {
1664 hash = PyObject_Hash(key);
1665 if (hash == -1)
1666 return -1;
1667 }
Tim Petersdea48ec2001-05-22 20:40:22 +00001668 return (mp->ma_lookup)(mp, key, hash)->me_value != NULL;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001669}
1670
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001671/* Hack to implement "key in dict" */
1672static PySequenceMethods dict_as_sequence = {
1673 0, /* sq_length */
1674 0, /* sq_concat */
1675 0, /* sq_repeat */
1676 0, /* sq_item */
1677 0, /* sq_slice */
1678 0, /* sq_ass_item */
1679 0, /* sq_ass_slice */
1680 (objobjproc)dict_contains, /* sq_contains */
1681 0, /* sq_inplace_concat */
1682 0, /* sq_inplace_repeat */
1683};
1684
Guido van Rossum09e563a2001-05-01 12:10:21 +00001685static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001686dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1687{
1688 PyObject *self;
1689
1690 assert(type != NULL && type->tp_alloc != NULL);
1691 self = type->tp_alloc(type, 0);
1692 if (self != NULL) {
1693 PyDictObject *d = (PyDictObject *)self;
1694 /* It's guaranteed that tp->alloc zeroed out the struct. */
1695 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
1696 INIT_NONZERO_DICT_SLOTS(d);
1697 d->ma_lookup = lookdict_string;
1698#ifdef SHOW_CONVERSION_COUNTS
1699 ++created;
1700#endif
1701 }
1702 return self;
1703}
1704
1705static PyObject *
Guido van Rossum09e563a2001-05-01 12:10:21 +00001706dict_iter(dictobject *dict)
1707{
1708 return dictiter_new(dict, select_key);
1709}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001710
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001711PyTypeObject PyDict_Type = {
1712 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001713 0,
Guido van Rossum9bfef441993-03-29 10:43:31 +00001714 "dictionary",
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001715 sizeof(dictobject) + PyGC_HEAD_SIZE,
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001716 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00001717 (destructor)dict_dealloc, /* tp_dealloc */
1718 (printfunc)dict_print, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001719 0, /* tp_getattr */
Guido van Rossumb9324202001-01-18 00:39:02 +00001720 0, /* tp_setattr */
Tim Peters4fa58bf2001-05-10 21:45:19 +00001721 (cmpfunc)dict_compare, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00001722 (reprfunc)dict_repr, /* tp_repr */
1723 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001724 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00001725 &dict_as_mapping, /* tp_as_mapping */
1726 0, /* tp_hash */
1727 0, /* tp_call */
1728 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001729 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumb9324202001-01-18 00:39:02 +00001730 0, /* tp_setattro */
1731 0, /* tp_as_buffer */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001732 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC |
1733 Py_TPFLAGS_BASETYPE, /* tp_flags */
1734 "dictionary type", /* tp_doc */
Guido van Rossumb9324202001-01-18 00:39:02 +00001735 (traverseproc)dict_traverse, /* tp_traverse */
1736 (inquiry)dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00001737 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001738 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00001739 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001740 0, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001741 mapp_methods, /* tp_methods */
1742 0, /* tp_members */
1743 0, /* tp_getset */
1744 0, /* tp_base */
1745 0, /* tp_dict */
1746 0, /* tp_descr_get */
1747 0, /* tp_descr_set */
1748 0, /* tp_dictoffset */
1749 0, /* tp_init */
1750 PyType_GenericAlloc, /* tp_alloc */
1751 dict_new, /* tp_new */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001752};
1753
Guido van Rossum3cca2451997-05-16 14:23:33 +00001754/* For backward compatibility with old dictionary interface */
1755
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001756PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001757PyDict_GetItemString(PyObject *v, char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001758{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001759 PyObject *kv, *rv;
1760 kv = PyString_FromString(key);
1761 if (kv == NULL)
1762 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00001763 rv = PyDict_GetItem(v, kv);
1764 Py_DECREF(kv);
1765 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001766}
1767
1768int
Tim Peters1f5871e2000-07-04 17:44:48 +00001769PyDict_SetItemString(PyObject *v, char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001770{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001771 PyObject *kv;
1772 int err;
1773 kv = PyString_FromString(key);
1774 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00001775 return -1;
Guido van Rossum4f3bf1e1997-09-29 23:31:11 +00001776 PyString_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00001777 err = PyDict_SetItem(v, kv, item);
1778 Py_DECREF(kv);
1779 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001780}
1781
1782int
Tim Peters1f5871e2000-07-04 17:44:48 +00001783PyDict_DelItemString(PyObject *v, char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001784{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001785 PyObject *kv;
1786 int err;
1787 kv = PyString_FromString(key);
1788 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00001789 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00001790 err = PyDict_DelItem(v, kv);
1791 Py_DECREF(kv);
1792 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001793}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001794
1795/* Dictionary iterator type */
1796
1797extern PyTypeObject PyDictIter_Type; /* Forward */
1798
1799typedef struct {
1800 PyObject_HEAD
1801 dictobject *di_dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001802 int di_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001803 int di_pos;
Guido van Rossum09e563a2001-05-01 12:10:21 +00001804 binaryfunc di_select;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001805} dictiterobject;
1806
1807static PyObject *
Guido van Rossum09e563a2001-05-01 12:10:21 +00001808dictiter_new(dictobject *dict, binaryfunc select)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001809{
1810 dictiterobject *di;
1811 di = PyObject_NEW(dictiterobject, &PyDictIter_Type);
1812 if (di == NULL)
1813 return NULL;
1814 Py_INCREF(dict);
1815 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001816 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001817 di->di_pos = 0;
Guido van Rossum09e563a2001-05-01 12:10:21 +00001818 di->di_select = select;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001819 return (PyObject *)di;
1820}
1821
1822static void
1823dictiter_dealloc(dictiterobject *di)
1824{
1825 Py_DECREF(di->di_dict);
1826 PyObject_DEL(di);
1827}
1828
1829static PyObject *
1830dictiter_next(dictiterobject *di, PyObject *args)
1831{
Guido van Rossum09e563a2001-05-01 12:10:21 +00001832 PyObject *key, *value;
Guido van Rossum213c7a62001-04-23 14:08:49 +00001833
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001834 if (di->di_used != di->di_dict->ma_used) {
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001835 PyErr_SetString(PyExc_RuntimeError,
1836 "dictionary changed size during iteration");
1837 return NULL;
1838 }
Guido van Rossum09e563a2001-05-01 12:10:21 +00001839 if (PyDict_Next((PyObject *)(di->di_dict), &di->di_pos, &key, &value)) {
1840 return (*di->di_select)(key, value);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001841 }
1842 PyErr_SetObject(PyExc_StopIteration, Py_None);
1843 return NULL;
1844}
1845
1846static PyObject *
1847dictiter_getiter(PyObject *it)
1848{
1849 Py_INCREF(it);
1850 return it;
1851}
1852
1853static PyMethodDef dictiter_methods[] = {
1854 {"next", (PyCFunction)dictiter_next, METH_VARARGS,
1855 "it.next() -- get the next value, or raise StopIteration"},
1856 {NULL, NULL} /* sentinel */
1857};
1858
Guido van Rossum213c7a62001-04-23 14:08:49 +00001859static PyObject *dictiter_iternext(dictiterobject *di)
1860{
Guido van Rossum09e563a2001-05-01 12:10:21 +00001861 PyObject *key, *value;
Guido van Rossum213c7a62001-04-23 14:08:49 +00001862
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001863 if (di->di_used != di->di_dict->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00001864 PyErr_SetString(PyExc_RuntimeError,
1865 "dictionary changed size during iteration");
1866 return NULL;
1867 }
Guido van Rossum09e563a2001-05-01 12:10:21 +00001868 if (PyDict_Next((PyObject *)(di->di_dict), &di->di_pos, &key, &value)) {
1869 return (*di->di_select)(key, value);
Guido van Rossum213c7a62001-04-23 14:08:49 +00001870 }
1871 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001872}
1873
1874PyTypeObject PyDictIter_Type = {
1875 PyObject_HEAD_INIT(&PyType_Type)
1876 0, /* ob_size */
1877 "dictionary-iterator", /* tp_name */
1878 sizeof(dictiterobject), /* tp_basicsize */
1879 0, /* tp_itemsize */
1880 /* methods */
1881 (destructor)dictiter_dealloc, /* tp_dealloc */
1882 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001883 0, /* tp_getattr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001884 0, /* tp_setattr */
1885 0, /* tp_compare */
1886 0, /* tp_repr */
1887 0, /* tp_as_number */
1888 0, /* tp_as_sequence */
1889 0, /* tp_as_mapping */
1890 0, /* tp_hash */
1891 0, /* tp_call */
1892 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001893 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001894 0, /* tp_setattro */
1895 0, /* tp_as_buffer */
1896 Py_TPFLAGS_DEFAULT, /* tp_flags */
1897 0, /* tp_doc */
1898 0, /* tp_traverse */
1899 0, /* tp_clear */
1900 0, /* tp_richcompare */
1901 0, /* tp_weaklistoffset */
1902 (getiterfunc)dictiter_getiter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001903 (iternextfunc)dictiter_iternext, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001904 dictiter_methods, /* tp_methods */
1905 0, /* tp_members */
1906 0, /* tp_getset */
1907 0, /* tp_base */
1908 0, /* tp_dict */
1909 0, /* tp_descr_get */
1910 0, /* tp_descr_set */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001911};