blob: 81bdf598c46456f01da21836d3618bbe1d0d3211 [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
6
7/*
Tim Petersdea48ec2001-05-22 20:40:22 +00008 * MINSIZE is the minimum size of a dictionary. This many slots are
9 * allocated directly in the dict object (in the ma_smalltable member).
10 * This must be a power of 2, and the first entry in the polys[] vector must
11 * match.
Guido van Rossum16e93a81997-01-28 00:00:11 +000012 */
Tim Petersdea48ec2001-05-22 20:40:22 +000013#define MINSIZE 8
Guido van Rossum16e93a81997-01-28 00:00:11 +000014
Fred Drake1bff34a2000-08-31 19:31:38 +000015/* define this out if you don't want conversion statistics on exit */
16#undef SHOW_CONVERSION_COUNTS
17
Guido van Rossum16e93a81997-01-28 00:00:11 +000018/*
19Table of irreducible polynomials to efficiently cycle through
Tim Petersea8f2bf2000-12-13 01:02:46 +000020GF(2^n)-{0}, 2<=n<=30. A table size is always a power of 2.
Tim Petersdea48ec2001-05-22 20:40:22 +000021For a table size of 2**i, the polys entry is 2**i + j for some j in 1 thru
222**i-1 inclusive. The polys[] entries here happen to add in the smallest j
23values "that work". Work means this: given any integer k in 1 thru 2**i-1
24inclusive, a poly works if & only if repeating this code:
25 print k
26 k <<= 1
27 if k >= 2**i:
28 k ^= poly
29prints every integer in 1 thru 2**i-1 inclusive exactly once before printing
30k a second time. Theory can be used to find such polys efficiently, but the
31operational defn. of "works" is sufficient to find them in reasonable time
32via brute force program (hint: any poly that has an even number of 1 bits
33cannot work; ditto any poly with low bit 0; exploit those).
Guido van Rossum4b1302b1993-03-27 18:11:32 +000034*/
Tim Petersdea48ec2001-05-22 20:40:22 +000035
Guido van Rossum16e93a81997-01-28 00:00:11 +000036static long polys[] = {
Tim Petersdea48ec2001-05-22 20:40:22 +000037/* 4 + 3, */ /* first active entry if MINSIZE == 4 */
38 8 + 3, /* first active entry if MINSIZE == 8 */
Guido van Rossum9e5656c1997-01-29 04:45:16 +000039 16 + 3,
40 32 + 5,
41 64 + 3,
42 128 + 3,
43 256 + 29,
44 512 + 17,
45 1024 + 9,
46 2048 + 5,
47 4096 + 83,
48 8192 + 27,
49 16384 + 43,
50 32768 + 3,
51 65536 + 45,
52 131072 + 9,
53 262144 + 39,
54 524288 + 39,
55 1048576 + 9,
56 2097152 + 5,
57 4194304 + 3,
58 8388608 + 33,
59 16777216 + 27,
60 33554432 + 9,
61 67108864 + 71,
62 134217728 + 39,
63 268435456 + 9,
64 536870912 + 5,
Tim Petersdea48ec2001-05-22 20:40:22 +000065 1073741824 + 83
66 /* 2147483648 + 9 -- if we ever boost this to unsigned long */
Guido van Rossum4b1302b1993-03-27 18:11:32 +000067};
68
69/* Object used as dummy key to fill deleted entries */
Guido van Rossuma9e7a811997-05-13 21:02:11 +000070static PyObject *dummy; /* Initialized by first call to newdictobject() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +000071
72/*
Tim Petersea8f2bf2000-12-13 01:02:46 +000073There are three kinds of slots in the table:
74
751. Unused. me_key == me_value == NULL
76 Does not hold an active (key, value) pair now and never did. Unused can
77 transition to Active upon key insertion. This is the only case in which
78 me_key is NULL, and is each slot's initial state.
79
802. Active. me_key != NULL and me_key != dummy and me_value != NULL
81 Holds an active (key, value) pair. Active can transition to Dummy upon
82 key deletion. This is the only case in which me_value != NULL.
83
Tim Petersf1c7c882000-12-13 19:58:25 +0000843. Dummy. me_key == dummy and me_value == NULL
Tim Petersea8f2bf2000-12-13 01:02:46 +000085 Previously held an active (key, value) pair, but that was deleted and an
86 active pair has not yet overwritten the slot. Dummy can transition to
87 Active upon key insertion. Dummy slots cannot be made Unused again
88 (cannot have me_key set to NULL), else the probe sequence in case of
89 collision would have no way to know they were once active.
Tim Petersf1c7c882000-12-13 19:58:25 +000090
91Note: .popitem() abuses the me_hash field of an Unused or Dummy slot to
92hold a search finger. The me_hash field of Unused or Dummy slots has no
93meaning otherwise.
Guido van Rossum4b1302b1993-03-27 18:11:32 +000094*/
95typedef struct {
Tim Petersea8f2bf2000-12-13 01:02:46 +000096 long me_hash; /* cached hash code of me_key */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000097 PyObject *me_key;
98 PyObject *me_value;
Guido van Rossum36488841997-04-11 19:14:07 +000099#ifdef USE_CACHE_ALIGNED
100 long aligner;
101#endif
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000102} dictentry;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000103
104/*
Tim Petersf1c7c882000-12-13 19:58:25 +0000105To ensure the lookup algorithm terminates, there must be at least one Unused
Tim Petersea8f2bf2000-12-13 01:02:46 +0000106slot (NULL key) in the table.
107The value ma_fill is the number of non-NULL keys (sum of Active and Dummy);
108ma_used is the number of non-NULL, non-dummy keys (== the number of non-NULL
109values == the number of Active items).
110To avoid slowing down lookups on a near-full table, we resize the table when
Tim Peters67830702001-03-21 19:23:56 +0000111it's two-thirds full.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000112*/
Fred Drake1bff34a2000-08-31 19:31:38 +0000113typedef struct dictobject dictobject;
114struct dictobject {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000115 PyObject_HEAD
Tim Petersea8f2bf2000-12-13 01:02:46 +0000116 int ma_fill; /* # Active + # Dummy */
117 int ma_used; /* # Active */
118 int ma_size; /* total # slots in ma_table */
119 int ma_poly; /* appopriate entry from polys vector */
Tim Petersdea48ec2001-05-22 20:40:22 +0000120 /* ma_table points to ma_smalltable for small tables, else to
121 * additional malloc'ed memory. ma_table is never NULL! This rule
122 * saves repeated runtime null-tests in the workhorse getitem and
123 * setitem calls.
124 */
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000125 dictentry *ma_table;
Fred Drake1bff34a2000-08-31 19:31:38 +0000126 dictentry *(*ma_lookup)(dictobject *mp, PyObject *key, long hash);
Tim Petersdea48ec2001-05-22 20:40:22 +0000127 dictentry ma_smalltable[MINSIZE];
Fred Drake1bff34a2000-08-31 19:31:38 +0000128};
129
130/* forward declarations */
131static dictentry *
132lookdict_string(dictobject *mp, PyObject *key, long hash);
133
134#ifdef SHOW_CONVERSION_COUNTS
135static long created = 0L;
136static long converted = 0L;
137
138static void
139show_counts(void)
140{
141 fprintf(stderr, "created %ld string dicts\n", created);
142 fprintf(stderr, "converted %ld to normal dicts\n", converted);
143 fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created);
144}
145#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000146
Tim Petersdea48ec2001-05-22 20:40:22 +0000147/* Set dictobject* mp to empty but w/ MINSIZE slots, using ma_smalltable. */
148#define empty_to_minsize(mp) do { \
149 memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \
150 (mp)->ma_table = (mp)->ma_smalltable; \
151 (mp)->ma_size = MINSIZE; \
152 (mp)->ma_used = (mp)->ma_fill = 0; \
153 (mp)->ma_poly = polys[0]; \
154 assert(MINSIZE < (mp)->ma_poly && (mp)->ma_poly < MINSIZE*2); \
155 } while(0)
156
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000157PyObject *
Thomas Wouters78890102000-07-22 19:25:51 +0000158PyDict_New(void)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000159{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000160 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000161 if (dummy == NULL) { /* Auto-initialize dummy */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000162 dummy = PyString_FromString("<dummy key>");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000163 if (dummy == NULL)
164 return NULL;
Fred Drake1bff34a2000-08-31 19:31:38 +0000165#ifdef SHOW_CONVERSION_COUNTS
166 Py_AtExit(show_counts);
167#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000168 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000169 mp = PyObject_NEW(dictobject, &PyDict_Type);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000170 if (mp == NULL)
171 return NULL;
Tim Petersdea48ec2001-05-22 20:40:22 +0000172 empty_to_minsize(mp);
Fred Drake1bff34a2000-08-31 19:31:38 +0000173 mp->ma_lookup = lookdict_string;
174#ifdef SHOW_CONVERSION_COUNTS
175 ++created;
176#endif
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000177 PyObject_GC_Init(mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000178 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000179}
180
181/*
182The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000183This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000184Open addressing is preferred over chaining since the link overhead for
185chaining would be substantial (100% with typical malloc overhead).
Guido van Rossum16e93a81997-01-28 00:00:11 +0000186However, instead of going through the table at constant steps, we cycle
Tim Petersea8f2bf2000-12-13 01:02:46 +0000187through the values of GF(2^n). This avoids modulo computations, being
Guido van Rossum16e93a81997-01-28 00:00:11 +0000188much cheaper on RISC machines, without leading to clustering.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000189
Guido van Rossum2bc13791999-03-24 19:06:42 +0000190The initial probe index is computed as hash mod the table size.
Tim Petersea8f2bf2000-12-13 01:02:46 +0000191Subsequent probe indices use the values of x^i in GF(2^n)-{0} as an offset,
192where x is a root. The initial offset is derived from hash, too.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000193
194All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000195
196(This version is due to Reimer Behrends, some ideas are also due to
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000197Jyrki Alakuijala and Vladimir Marangozov.)
Fred Drake1bff34a2000-08-31 19:31:38 +0000198
199This function must never return NULL; failures are indicated by returning
200a dictentry* for which the me_value field is NULL. Exceptions are never
201reported by this function, and outstanding exceptions are maintained.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000202*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000203static dictentry *
Tim Peters1f5871e2000-07-04 17:44:48 +0000204lookdict(dictobject *mp, PyObject *key, register long hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000205{
Guido van Rossum16e93a81997-01-28 00:00:11 +0000206 register int i;
207 register unsigned incr;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000208 register dictentry *freeslot;
Guido van Rossum2095d241997-04-09 19:41:24 +0000209 register unsigned int mask = mp->ma_size-1;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000210 dictentry *ep0 = mp->ma_table;
211 register dictentry *ep;
Fred Drakec88b99c2000-08-31 19:04:07 +0000212 register int restore_error = 0;
213 register int checked_error = 0;
214 register int cmp;
215 PyObject *err_type, *err_value, *err_tb;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000216 /* We must come up with (i, incr) such that 0 <= i < ma_size
Tim Petersea8f2bf2000-12-13 01:02:46 +0000217 and 0 < incr < ma_size and both are a function of hash.
218 i is the initial table index and incr the initial probe offset. */
Tim Peters2f228e72001-05-13 00:19:31 +0000219 i = hash & mask;
Guido van Rossumefb46091997-01-29 15:53:56 +0000220 ep = &ep0[i];
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000221 if (ep->me_key == NULL || ep->me_key == key)
Guido van Rossum7d181591997-01-16 21:06:45 +0000222 return ep;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000223 if (ep->me_key == dummy)
Guido van Rossum7d181591997-01-16 21:06:45 +0000224 freeslot = ep;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000225 else {
Fred Drakec88b99c2000-08-31 19:04:07 +0000226 if (ep->me_hash == hash) {
227 /* error can't have been checked yet */
228 checked_error = 1;
229 if (PyErr_Occurred()) {
230 restore_error = 1;
231 PyErr_Fetch(&err_type, &err_value, &err_tb);
232 }
Guido van Rossumb9324202001-01-18 00:39:02 +0000233 cmp = PyObject_RichCompareBool(ep->me_key, key, Py_EQ);
234 if (cmp > 0) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000235 if (restore_error)
236 PyErr_Restore(err_type, err_value,
237 err_tb);
238 return ep;
239 }
Guido van Rossumb9324202001-01-18 00:39:02 +0000240 else if (cmp < 0)
241 PyErr_Clear();
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000242 }
243 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000244 }
Guido van Rossum2bc13791999-03-24 19:06:42 +0000245 /* Derive incr from hash, just to make it more arbitrary. Note that
Guido van Rossum16e93a81997-01-28 00:00:11 +0000246 incr must not be 0, or we will get into an infinite loop.*/
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000247 incr = (hash ^ ((unsigned long)hash >> 3)) & mask;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000248 if (!incr)
249 incr = mask;
Tim Peters342c65e2001-05-13 06:43:53 +0000250 /* In the loop, me_key == dummy is by far (factor of 100s) the
251 least likely outcome, so test for that last. */
Guido van Rossum16e93a81997-01-28 00:00:11 +0000252 for (;;) {
Guido van Rossumefb46091997-01-29 15:53:56 +0000253 ep = &ep0[(i+incr)&mask];
Guido van Rossum16e93a81997-01-28 00:00:11 +0000254 if (ep->me_key == NULL) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000255 if (restore_error)
256 PyErr_Restore(err_type, err_value, err_tb);
Tim Peters342c65e2001-05-13 06:43:53 +0000257 return freeslot == NULL ? ep : freeslot;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000258 }
Tim Peters342c65e2001-05-13 06:43:53 +0000259 if (ep->me_key == key) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000260 if (restore_error)
261 PyErr_Restore(err_type, err_value, err_tb);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000262 return ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000263 }
Tim Peters342c65e2001-05-13 06:43:53 +0000264 else if (ep->me_hash == hash && ep->me_key != dummy) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000265 if (!checked_error) {
266 checked_error = 1;
267 if (PyErr_Occurred()) {
268 restore_error = 1;
269 PyErr_Fetch(&err_type, &err_value,
270 &err_tb);
271 }
272 }
Guido van Rossumb9324202001-01-18 00:39:02 +0000273 cmp = PyObject_RichCompareBool(ep->me_key, key, Py_EQ);
274 if (cmp > 0) {
Fred Drakec88b99c2000-08-31 19:04:07 +0000275 if (restore_error)
276 PyErr_Restore(err_type, err_value,
277 err_tb);
278 return ep;
279 }
Guido van Rossumb9324202001-01-18 00:39:02 +0000280 else if (cmp < 0)
281 PyErr_Clear();
Guido van Rossum16e93a81997-01-28 00:00:11 +0000282 }
Tim Peters342c65e2001-05-13 06:43:53 +0000283 else if (ep->me_key == dummy && freeslot == NULL)
284 freeslot = ep;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000285 /* Cycle through GF(2^n)-{0} */
Tim Peters342c65e2001-05-13 06:43:53 +0000286 incr <<= 1;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000287 if (incr > mask)
Tim Peters342c65e2001-05-13 06:43:53 +0000288 incr ^= mp->ma_poly; /* clears the highest bit */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000289 }
290}
291
292/*
Fred Drake1bff34a2000-08-31 19:31:38 +0000293 * Hacked up version of lookdict which can assume keys are always strings;
294 * this assumption allows testing for errors during PyObject_Compare() to
295 * be dropped; string-string comparisons never raise exceptions. This also
296 * means we don't need to go through PyObject_Compare(); we can always use
297 * the tp_compare slot of the string type object directly.
298 *
299 * This really only becomes meaningful if proper error handling in lookdict()
300 * is too expensive.
301 */
302static dictentry *
303lookdict_string(dictobject *mp, PyObject *key, register long hash)
304{
305 register int i;
306 register unsigned incr;
307 register dictentry *freeslot;
308 register unsigned int mask = mp->ma_size-1;
309 dictentry *ep0 = mp->ma_table;
310 register dictentry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000311 cmpfunc compare = PyString_Type.tp_compare;
Fred Drake1bff34a2000-08-31 19:31:38 +0000312
313 /* make sure this function doesn't have to handle non-string keys */
314 if (!PyString_Check(key)) {
315#ifdef SHOW_CONVERSION_COUNTS
316 ++converted;
317#endif
318 mp->ma_lookup = lookdict;
319 return lookdict(mp, key, hash);
320 }
321 /* We must come up with (i, incr) such that 0 <= i < ma_size
322 and 0 < incr < ma_size and both are a function of hash */
Tim Peters2f228e72001-05-13 00:19:31 +0000323 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000324 ep = &ep0[i];
325 if (ep->me_key == NULL || ep->me_key == key)
326 return ep;
327 if (ep->me_key == dummy)
328 freeslot = ep;
329 else {
330 if (ep->me_hash == hash
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000331 && compare(ep->me_key, key) == 0) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000332 return ep;
333 }
334 freeslot = NULL;
335 }
336 /* Derive incr from hash, just to make it more arbitrary. Note that
337 incr must not be 0, or we will get into an infinite loop.*/
338 incr = (hash ^ ((unsigned long)hash >> 3)) & mask;
339 if (!incr)
340 incr = mask;
Tim Peters342c65e2001-05-13 06:43:53 +0000341 /* In the loop, me_key == dummy is by far (factor of 100s) the
342 least likely outcome, so test for that last. */
Fred Drake1bff34a2000-08-31 19:31:38 +0000343 for (;;) {
344 ep = &ep0[(i+incr)&mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000345 if (ep->me_key == NULL)
346 return freeslot == NULL ? ep : freeslot;
347 if (ep->me_key == key
348 || (ep->me_hash == hash
349 && ep->me_key != dummy
350 && compare(ep->me_key, key) == 0))
Fred Drake1bff34a2000-08-31 19:31:38 +0000351 return ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000352 if (ep->me_key == dummy && freeslot == NULL)
Tim Peters342c65e2001-05-13 06:43:53 +0000353 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000354 /* Cycle through GF(2^n)-{0} */
Tim Peters342c65e2001-05-13 06:43:53 +0000355 incr <<= 1;
Fred Drake1bff34a2000-08-31 19:31:38 +0000356 if (incr > mask)
Tim Peters342c65e2001-05-13 06:43:53 +0000357 incr ^= mp->ma_poly; /* clears the highest bit */
Fred Drake1bff34a2000-08-31 19:31:38 +0000358 }
359}
360
361/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000362Internal routine to insert a new item into the table.
363Used both by the internal resize routine and by the public insert routine.
364Eats a reference to key and one to value.
365*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000366static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000367insertdict(register dictobject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000368{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000369 PyObject *old_value;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000370 register dictentry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000371 ep = (mp->ma_lookup)(mp, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000372 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000373 old_value = ep->me_value;
374 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000375 Py_DECREF(old_value); /* which **CAN** re-enter */
376 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000377 }
378 else {
379 if (ep->me_key == NULL)
380 mp->ma_fill++;
381 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000382 Py_DECREF(ep->me_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000383 ep->me_key = key;
384 ep->me_hash = hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000385 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000386 mp->ma_used++;
387 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000388}
389
390/*
391Restructure the table by allocating a new table and reinserting all
392items again. When entries have been deleted, the new table may
393actually be smaller than the old one.
394*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000395static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000396dictresize(dictobject *mp, int minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000397{
398 register int oldsize = mp->ma_size;
Guido van Rossum16e93a81997-01-28 00:00:11 +0000399 register int newsize, newpoly;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000400 register dictentry *oldtable = mp->ma_table;
401 register dictentry *newtable;
402 register dictentry *ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000403 register int i;
Tim Peters91a364d2001-05-19 07:04:38 +0000404
405 assert(minused >= 0);
Tim Petersdea48ec2001-05-22 20:40:22 +0000406 assert(oldtable != NULL);
407 newpoly = 0;
408 newsize = MINSIZE;
409 for (i = 0; i < sizeof(polys)/sizeof(polys[0]); ++i) {
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000410 if (newsize > minused) {
Guido van Rossum16e93a81997-01-28 00:00:11 +0000411 newpoly = polys[i];
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000412 break;
413 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000414 newsize <<= 1;
415 if (newsize < 0) /* overflow */
416 break;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000417 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000418 if (newpoly == 0) {
419 /* Ran out of polynomials or newsize overflowed. */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000420 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000421 return -1;
422 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000423 if (newsize == MINSIZE) {
424 newtable = mp->ma_smalltable;
425 if (newtable == oldtable)
426 return 0;
427 }
428 else {
429 newtable = PyMem_NEW(dictentry, newsize);
430 if (newtable == NULL) {
431 PyErr_NoMemory();
432 return -1;
433 }
434 }
435 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000436 mp->ma_table = newtable;
Tim Petersdea48ec2001-05-22 20:40:22 +0000437 mp->ma_size = newsize;
438 memset(newtable, 0, sizeof(dictentry) * newsize);
439 mp->ma_poly = newpoly;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000440 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000441 i = mp->ma_fill;
442 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000443
Tim Peters19283142001-05-17 22:25:34 +0000444 /* Copy the data over; this is refcount-neutral for active entries;
445 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000446 for (ep = oldtable; i > 0; ep++) {
447 if (ep->me_value != NULL) { /* active entry */
448 --i;
Tim Peters19283142001-05-17 22:25:34 +0000449 insertdict(mp, ep->me_key, ep->me_hash, ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000450 }
Tim Peters19283142001-05-17 22:25:34 +0000451 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000452 --i;
Tim Peters19283142001-05-17 22:25:34 +0000453 assert(ep->me_key == dummy);
454 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000455 }
Tim Peters19283142001-05-17 22:25:34 +0000456 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000457 }
458
Tim Petersdea48ec2001-05-22 20:40:22 +0000459 if (oldtable != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000460 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000461 return 0;
462}
463
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000464PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000465PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000466{
467 long hash;
Fred Drake1bff34a2000-08-31 19:31:38 +0000468 dictobject *mp = (dictobject *)op;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000469 if (!PyDict_Check(op)) {
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000470 return NULL;
471 }
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000472#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000473 if (!PyString_Check(key) ||
474 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000475#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000476 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000477 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000478 if (hash == -1) {
479 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000480 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000481 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000482 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000483 return (mp->ma_lookup)(mp, key, hash)->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000484}
485
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000486/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
487 * dictionary if it is merely replacing the value for an existing key.
488 * This is means that it's safe to loop over a dictionary with
489 * PyDict_Next() and occasionally replace a value -- but you can't
490 * insert new keys or remove them.
491 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000492int
Tim Peters1f5871e2000-07-04 17:44:48 +0000493PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000494{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000495 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000496 register long hash;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000497 register int n_used;
498
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000499 if (!PyDict_Check(op)) {
500 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000501 return -1;
502 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000503 mp = (dictobject *)op;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000504#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000505 if (PyString_Check(key)) {
Guido van Rossum2a61e741997-01-18 07:55:05 +0000506#ifdef INTERN_STRINGS
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000507 if (((PyStringObject *)key)->ob_sinterned != NULL) {
508 key = ((PyStringObject *)key)->ob_sinterned;
509 hash = ((PyStringObject *)key)->ob_shash;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000510 }
511 else
512#endif
513 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000514 hash = ((PyStringObject *)key)->ob_shash;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000515 if (hash == -1)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000516 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000517 }
518 }
519 else
520#endif
521 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000522 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000523 if (hash == -1)
524 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000525 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000526 assert(mp->ma_fill < mp->ma_size);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000527 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000528 Py_INCREF(value);
529 Py_INCREF(key);
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000530 insertdict(mp, key, hash, value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000531 /* If we added a key, we can safely resize. Otherwise skip this!
532 * If fill >= 2/3 size, adjust size. Normally, this doubles the
533 * size, but it's also possible for the dict to shrink (if ma_fill is
534 * much larger than ma_used, meaning a lot of dict keys have been
535 * deleted).
536 */
537 if (mp->ma_used > n_used && mp->ma_fill*3 >= mp->ma_size*2) {
538 if (dictresize(mp, mp->ma_used*2) != 0)
539 return -1;
540 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000541 return 0;
542}
543
544int
Tim Peters1f5871e2000-07-04 17:44:48 +0000545PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000546{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000547 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000548 register long hash;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000549 register dictentry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000550 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000551
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000552 if (!PyDict_Check(op)) {
553 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000554 return -1;
555 }
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000556#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000557 if (!PyString_Check(key) ||
558 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000559#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000560 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000561 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000562 if (hash == -1)
563 return -1;
564 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000565 mp = (dictobject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000566 ep = (mp->ma_lookup)(mp, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000567 if (ep->me_value == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000568 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000569 return -1;
570 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000571 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000572 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000573 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000574 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000575 ep->me_value = NULL;
576 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000577 Py_DECREF(old_value);
578 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000579 return 0;
580}
581
Guido van Rossum25831651993-05-19 14:50:45 +0000582void
Tim Peters1f5871e2000-07-04 17:44:48 +0000583PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000584{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000585 dictobject *mp;
Tim Petersdea48ec2001-05-22 20:40:22 +0000586 dictentry *ep, *table;
587 int table_is_malloced;
588 int fill;
589 dictentry small_copy[MINSIZE];
590#ifdef Py_DEBUG
591 int i, n;
592#endif
593
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000594 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000595 return;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000596 mp = (dictobject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000597#ifdef Py_DEBUG
Guido van Rossumd7047b31995-01-02 19:07:15 +0000598 n = mp->ma_size;
Tim Petersdea48ec2001-05-22 20:40:22 +0000599 i = 0;
600#endif
601
602 table = mp->ma_table;
603 assert(table != NULL);
604 table_is_malloced = table != mp->ma_smalltable;
605
606 /* This is delicate. During the process of clearing the dict,
607 * decrefs can cause the dict to mutate. To avoid fatal confusion
608 * (voice of experience), we have to make the dict empty before
609 * clearing the slots, and never refer to anything via mp->xxx while
610 * clearing.
611 */
612 fill = mp->ma_fill;
613 if (table_is_malloced)
614 empty_to_minsize(mp);
615
616 else if (fill > 0) {
617 /* It's a small table with something that needs to be cleared.
618 * Afraid the only safe way is to copy the dict entries into
619 * another small table first.
620 */
621 memcpy(small_copy, table, sizeof(small_copy));
622 table = small_copy;
623 empty_to_minsize(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000624 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000625 /* else it's a small table that's already empty */
626
627 /* Now we can finally clear things. If C had refcounts, we could
628 * assert that the refcount on table is 1 now, i.e. that this function
629 * has unique access to it, so decref side-effects can't alter it.
630 */
631 for (ep = table; fill > 0; ++ep) {
632#ifdef Py_DEBUG
633 assert(i < n);
634 ++i;
635#endif
636 if (ep->me_key) {
637 --fill;
638 Py_DECREF(ep->me_key);
639 Py_XDECREF(ep->me_value);
640 }
641#ifdef Py_DEBUG
642 else
643 assert(ep->me_value == NULL);
644#endif
645 }
646
647 if (table_is_malloced)
648 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000649}
650
Tim Peters67830702001-03-21 19:23:56 +0000651/* CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
652 * mutates the dict. One exception: it is safe if the loop merely changes
653 * the values associated with the keys (but doesn't insert new keys or
654 * delete keys), via PyDict_SetItem().
655 */
Guido van Rossum25831651993-05-19 14:50:45 +0000656int
Tim Peters1f5871e2000-07-04 17:44:48 +0000657PyDict_Next(PyObject *op, int *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000658{
Guido van Rossum25831651993-05-19 14:50:45 +0000659 int i;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000660 register dictobject *mp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000661 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000662 return 0;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000663 mp = (dictobject *)op;
Guido van Rossum25831651993-05-19 14:50:45 +0000664 i = *ppos;
665 if (i < 0)
666 return 0;
667 while (i < mp->ma_size && mp->ma_table[i].me_value == NULL)
668 i++;
669 *ppos = i+1;
670 if (i >= mp->ma_size)
671 return 0;
672 if (pkey)
673 *pkey = mp->ma_table[i].me_key;
674 if (pvalue)
675 *pvalue = mp->ma_table[i].me_value;
676 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000677}
678
679/* Methods */
680
681static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000682dict_dealloc(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000683{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000684 register dictentry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000685 int fill = mp->ma_fill;
Guido van Rossumd724b232000-03-13 16:01:29 +0000686 Py_TRASHCAN_SAFE_BEGIN(mp)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000687 PyObject_GC_Fini(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000688 for (ep = mp->ma_table; fill > 0; ep++) {
689 if (ep->me_key) {
690 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000691 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000692 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000693 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000694 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000695 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000696 PyMem_DEL(mp->ma_table);
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000697 mp = (dictobject *) PyObject_AS_GC(mp);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000698 PyObject_DEL(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000699 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000700}
701
702static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000703dict_print(register dictobject *mp, register FILE *fp, register int flags)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000704{
705 register int i;
706 register int any;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000707 register dictentry *ep;
Guido van Rossum255443b1998-04-10 22:47:14 +0000708
709 i = Py_ReprEnter((PyObject*)mp);
710 if (i != 0) {
711 if (i < 0)
712 return i;
713 fprintf(fp, "{...}");
714 return 0;
715 }
716
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000717 fprintf(fp, "{");
718 any = 0;
719 for (i = 0, ep = mp->ma_table; i < mp->ma_size; i++, ep++) {
720 if (ep->me_value != NULL) {
721 if (any++ > 0)
722 fprintf(fp, ", ");
Guido van Rossum255443b1998-04-10 22:47:14 +0000723 if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
724 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000725 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000726 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000727 fprintf(fp, ": ");
Guido van Rossum255443b1998-04-10 22:47:14 +0000728 if (PyObject_Print(ep->me_value, fp, 0) != 0) {
729 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000730 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000731 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000732 }
733 }
734 fprintf(fp, "}");
Guido van Rossum255443b1998-04-10 22:47:14 +0000735 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000736 return 0;
737}
738
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000739static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000740dict_repr(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000741{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000742 auto PyObject *v;
743 PyObject *sepa, *colon;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000744 register int i;
745 register int any;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000746 register dictentry *ep;
Guido van Rossum255443b1998-04-10 22:47:14 +0000747
748 i = Py_ReprEnter((PyObject*)mp);
749 if (i != 0) {
750 if (i > 0)
751 return PyString_FromString("{...}");
752 return NULL;
753 }
754
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000755 v = PyString_FromString("{");
756 sepa = PyString_FromString(", ");
757 colon = PyString_FromString(": ");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000758 any = 0;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000759 for (i = 0, ep = mp->ma_table; i < mp->ma_size && v; i++, ep++) {
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000760 if (ep->me_value != NULL) {
761 if (any++)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000762 PyString_Concat(&v, sepa);
763 PyString_ConcatAndDel(&v, PyObject_Repr(ep->me_key));
764 PyString_Concat(&v, colon);
765 PyString_ConcatAndDel(&v, PyObject_Repr(ep->me_value));
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000766 }
767 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000768 PyString_ConcatAndDel(&v, PyString_FromString("}"));
Guido van Rossum255443b1998-04-10 22:47:14 +0000769 Py_ReprLeave((PyObject*)mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000770 Py_XDECREF(sepa);
771 Py_XDECREF(colon);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000772 return v;
773}
774
775static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000776dict_length(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000777{
778 return mp->ma_used;
779}
780
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000781static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000782dict_subscript(dictobject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000783{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000784 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000785 long hash;
Tim Petersdea48ec2001-05-22 20:40:22 +0000786 assert(mp->ma_table != NULL);
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000787#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000788 if (!PyString_Check(key) ||
789 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000790#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000791 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000792 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000793 if (hash == -1)
794 return NULL;
795 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000796 v = (mp->ma_lookup)(mp, key, hash) -> me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000797 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000798 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000799 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000800 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000801 return v;
802}
803
804static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000805dict_ass_sub(dictobject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000806{
807 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000808 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000809 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000810 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000811}
812
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000813static PyMappingMethods dict_as_mapping = {
814 (inquiry)dict_length, /*mp_length*/
815 (binaryfunc)dict_subscript, /*mp_subscript*/
816 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000817};
818
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000819static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000820dict_keys(register dictobject *mp, PyObject *args)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000821{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000822 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000823 register int i, j, n;
824
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000825 if (!PyArg_NoArgs(args))
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000826 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000827 again:
828 n = mp->ma_used;
829 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000830 if (v == NULL)
831 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000832 if (n != mp->ma_used) {
833 /* Durnit. The allocations caused the dict to resize.
834 * Just start over, this shouldn't normally happen.
835 */
836 Py_DECREF(v);
837 goto again;
838 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000839 for (i = 0, j = 0; i < mp->ma_size; i++) {
840 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000841 PyObject *key = mp->ma_table[i].me_key;
842 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000843 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000844 j++;
845 }
846 }
847 return v;
848}
849
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000850static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000851dict_values(register dictobject *mp, PyObject *args)
Guido van Rossum25831651993-05-19 14:50:45 +0000852{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000853 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000854 register int i, j, n;
855
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000856 if (!PyArg_NoArgs(args))
Guido van Rossum25831651993-05-19 14:50:45 +0000857 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000858 again:
859 n = mp->ma_used;
860 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +0000861 if (v == NULL)
862 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000863 if (n != mp->ma_used) {
864 /* Durnit. The allocations caused the dict to resize.
865 * Just start over, this shouldn't normally happen.
866 */
867 Py_DECREF(v);
868 goto again;
869 }
Guido van Rossum25831651993-05-19 14:50:45 +0000870 for (i = 0, j = 0; i < mp->ma_size; i++) {
871 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000872 PyObject *value = mp->ma_table[i].me_value;
873 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000874 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +0000875 j++;
876 }
877 }
878 return v;
879}
880
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000881static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000882dict_items(register dictobject *mp, PyObject *args)
Guido van Rossum25831651993-05-19 14:50:45 +0000883{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000884 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000885 register int i, j, n;
886 PyObject *item, *key, *value;
887
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000888 if (!PyArg_NoArgs(args))
Guido van Rossum25831651993-05-19 14:50:45 +0000889 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000890 /* Preallocate the list of tuples, to avoid allocations during
891 * the loop over the items, which could trigger GC, which
892 * could resize the dict. :-(
893 */
894 again:
895 n = mp->ma_used;
896 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +0000897 if (v == NULL)
898 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000899 for (i = 0; i < n; i++) {
900 item = PyTuple_New(2);
901 if (item == NULL) {
902 Py_DECREF(v);
903 return NULL;
904 }
905 PyList_SET_ITEM(v, i, item);
906 }
907 if (n != mp->ma_used) {
908 /* Durnit. The allocations caused the dict to resize.
909 * Just start over, this shouldn't normally happen.
910 */
911 Py_DECREF(v);
912 goto again;
913 }
914 /* Nothing we do below makes any function calls. */
Guido van Rossum25831651993-05-19 14:50:45 +0000915 for (i = 0, j = 0; i < mp->ma_size; i++) {
916 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000917 key = mp->ma_table[i].me_key;
918 value = mp->ma_table[i].me_value;
919 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000920 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000921 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000922 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000923 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +0000924 j++;
925 }
926 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000927 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +0000928 return v;
929}
930
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000931static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000932dict_update(register dictobject *mp, PyObject *args)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000933{
934 register int i;
935 dictobject *other;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000936 dictentry *entry;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000937 if (!PyArg_Parse(args, "O!", &PyDict_Type, &other))
938 return NULL;
Jeremy Hylton1fb60882001-01-03 22:34:59 +0000939 if (other == mp || other->ma_used == 0)
940 goto done; /* a.update(a) or a.update({}); nothing to do */
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000941 /* Do one big resize at the start, rather than incrementally
942 resizing as we insert new items. Expect that there will be
943 no (or few) overlapping keys. */
944 if ((mp->ma_fill + other->ma_used)*3 >= mp->ma_size*2) {
945 if (dictresize(mp, (mp->ma_used + other->ma_used)*3/2) != 0)
946 return NULL;
947 }
948 for (i = 0; i < other->ma_size; i++) {
949 entry = &other->ma_table[i];
950 if (entry->me_value != NULL) {
951 Py_INCREF(entry->me_key);
952 Py_INCREF(entry->me_value);
953 insertdict(mp, entry->me_key, entry->me_hash,
954 entry->me_value);
955 }
956 }
957 done:
958 Py_INCREF(Py_None);
959 return Py_None;
960}
961
962static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000963dict_copy(register dictobject *mp, PyObject *args)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000964{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +0000965 if (!PyArg_Parse(args, ""))
966 return NULL;
967 return PyDict_Copy((PyObject*)mp);
968}
969
970PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000971PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +0000972{
973 register dictobject *mp;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000974 register int i;
975 dictobject *copy;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000976 dictentry *entry;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +0000977
978 if (o == NULL || !PyDict_Check(o)) {
979 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000980 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +0000981 }
982 mp = (dictobject *)o;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000983 copy = (dictobject *)PyDict_New();
984 if (copy == NULL)
985 return NULL;
986 if (mp->ma_used > 0) {
987 if (dictresize(copy, mp->ma_used*3/2) != 0)
988 return NULL;
989 for (i = 0; i < mp->ma_size; i++) {
990 entry = &mp->ma_table[i];
991 if (entry->me_value != NULL) {
992 Py_INCREF(entry->me_key);
993 Py_INCREF(entry->me_value);
994 insertdict(copy, entry->me_key, entry->me_hash,
995 entry->me_value);
996 }
997 }
998 }
999 return (PyObject *)copy;
1000}
1001
Guido van Rossum4199fac1993-11-05 10:18:44 +00001002int
Tim Peters1f5871e2000-07-04 17:44:48 +00001003PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001004{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001005 if (mp == NULL || !PyDict_Check(mp)) {
1006 PyErr_BadInternalCall();
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001007 return 0;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001008 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001009 return ((dictobject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001010}
1011
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001012PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001013PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001014{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001015 if (mp == NULL || !PyDict_Check(mp)) {
1016 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001017 return NULL;
1018 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001019 return dict_keys((dictobject *)mp, (PyObject *)NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001020}
1021
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001022PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001023PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001024{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001025 if (mp == NULL || !PyDict_Check(mp)) {
1026 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001027 return NULL;
1028 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001029 return dict_values((dictobject *)mp, (PyObject *)NULL);
Guido van Rossum25831651993-05-19 14:50:45 +00001030}
1031
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001032PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001033PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001034{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001035 if (mp == NULL || !PyDict_Check(mp)) {
1036 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001037 return NULL;
1038 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001039 return dict_items((dictobject *)mp, (PyObject *)NULL);
Guido van Rossum25831651993-05-19 14:50:45 +00001040}
1041
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001042/* Subroutine which returns the smallest key in a for which b's value
1043 is different or absent. The value is returned too, through the
Tim Peters95bf9392001-05-10 08:32:44 +00001044 pval argument. Both are NULL if no key in a is found for which b's status
1045 differs. The refcounts on (and only on) non-NULL *pval and function return
1046 values must be decremented by the caller (characterize() increments them
1047 to ensure that mutating comparison and PyDict_GetItem calls can't delete
1048 them before the caller is done looking at them). */
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001049
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001050static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001051characterize(dictobject *a, dictobject *b, PyObject **pval)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001052{
Tim Peters95bf9392001-05-10 08:32:44 +00001053 PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
1054 PyObject *aval = NULL; /* a[akey] */
Guido van Rossumb9324202001-01-18 00:39:02 +00001055 int i, cmp;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001056
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001057 for (i = 0; i < a->ma_size; i++) {
Tim Peters95bf9392001-05-10 08:32:44 +00001058 PyObject *thiskey, *thisaval, *thisbval;
1059 if (a->ma_table[i].me_value == NULL)
1060 continue;
1061 thiskey = a->ma_table[i].me_key;
1062 Py_INCREF(thiskey); /* keep alive across compares */
1063 if (akey != NULL) {
1064 cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
1065 if (cmp < 0) {
1066 Py_DECREF(thiskey);
1067 goto Fail;
1068 }
1069 if (cmp > 0 ||
1070 i >= a->ma_size ||
1071 a->ma_table[i].me_value == NULL)
1072 {
1073 /* Not the *smallest* a key; or maybe it is
1074 * but the compare shrunk the dict so we can't
1075 * find its associated value anymore; or
1076 * maybe it is but the compare deleted the
1077 * a[thiskey] entry.
1078 */
1079 Py_DECREF(thiskey);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001080 continue;
Guido van Rossumb9324202001-01-18 00:39:02 +00001081 }
Tim Peters95bf9392001-05-10 08:32:44 +00001082 }
1083
1084 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
1085 thisaval = a->ma_table[i].me_value;
1086 assert(thisaval);
1087 Py_INCREF(thisaval); /* keep alive */
1088 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
1089 if (thisbval == NULL)
1090 cmp = 0;
1091 else {
1092 /* both dicts have thiskey: same values? */
1093 cmp = PyObject_RichCompareBool(
1094 thisaval, thisbval, Py_EQ);
1095 if (cmp < 0) {
1096 Py_DECREF(thiskey);
1097 Py_DECREF(thisaval);
1098 goto Fail;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001099 }
1100 }
Tim Peters95bf9392001-05-10 08:32:44 +00001101 if (cmp == 0) {
1102 /* New winner. */
1103 Py_XDECREF(akey);
1104 Py_XDECREF(aval);
1105 akey = thiskey;
1106 aval = thisaval;
1107 }
1108 else {
1109 Py_DECREF(thiskey);
1110 Py_DECREF(thisaval);
1111 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001112 }
Tim Peters95bf9392001-05-10 08:32:44 +00001113 *pval = aval;
1114 return akey;
1115
1116Fail:
1117 Py_XDECREF(akey);
1118 Py_XDECREF(aval);
1119 *pval = NULL;
1120 return NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001121}
1122
1123static int
Tim Peters1f5871e2000-07-04 17:44:48 +00001124dict_compare(dictobject *a, dictobject *b)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001125{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001126 PyObject *adiff, *bdiff, *aval, *bval;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001127 int res;
1128
1129 /* Compare lengths first */
1130 if (a->ma_used < b->ma_used)
1131 return -1; /* a is shorter */
1132 else if (a->ma_used > b->ma_used)
1133 return 1; /* b is shorter */
Tim Peters95bf9392001-05-10 08:32:44 +00001134
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001135 /* Same length -- check all keys */
Tim Peters95bf9392001-05-10 08:32:44 +00001136 bdiff = bval = NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001137 adiff = characterize(a, b, &aval);
Tim Peters95bf9392001-05-10 08:32:44 +00001138 if (adiff == NULL) {
1139 assert(!aval);
Tim Peters3918fb22001-05-10 18:58:31 +00001140 /* Either an error, or a is a subset with the same length so
Tim Peters95bf9392001-05-10 08:32:44 +00001141 * must be equal.
1142 */
1143 res = PyErr_Occurred() ? -1 : 0;
1144 goto Finished;
1145 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001146 bdiff = characterize(b, a, &bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001147 if (bdiff == NULL && PyErr_Occurred()) {
1148 assert(!bval);
1149 res = -1;
1150 goto Finished;
1151 }
1152 res = 0;
1153 if (bdiff) {
1154 /* bdiff == NULL "should be" impossible now, but perhaps
1155 * the last comparison done by the characterize() on a had
1156 * the side effect of making the dicts equal!
1157 */
1158 res = PyObject_Compare(adiff, bdiff);
1159 }
1160 if (res == 0 && bval != NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001161 res = PyObject_Compare(aval, bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001162
1163Finished:
1164 Py_XDECREF(adiff);
1165 Py_XDECREF(bdiff);
1166 Py_XDECREF(aval);
1167 Py_XDECREF(bval);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001168 return res;
1169}
1170
Tim Peterse63415e2001-05-08 04:38:29 +00001171/* Return 1 if dicts equal, 0 if not, -1 if error.
1172 * Gets out as soon as any difference is detected.
1173 * Uses only Py_EQ comparison.
1174 */
1175static int
1176dict_equal(dictobject *a, dictobject *b)
1177{
1178 int i;
1179
1180 if (a->ma_used != b->ma_used)
1181 /* can't be equal if # of entries differ */
1182 return 0;
1183
1184 /* Same # of entries -- check all of 'em. Exit early on any diff. */
1185 for (i = 0; i < a->ma_size; i++) {
1186 PyObject *aval = a->ma_table[i].me_value;
1187 if (aval != NULL) {
1188 int cmp;
1189 PyObject *bval;
1190 PyObject *key = a->ma_table[i].me_key;
1191 /* temporarily bump aval's refcount to ensure it stays
1192 alive until we're done with it */
1193 Py_INCREF(aval);
1194 bval = PyDict_GetItem((PyObject *)b, key);
1195 if (bval == NULL) {
1196 Py_DECREF(aval);
1197 return 0;
1198 }
1199 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1200 Py_DECREF(aval);
1201 if (cmp <= 0) /* error or not equal */
1202 return cmp;
1203 }
1204 }
1205 return 1;
1206 }
1207
1208static PyObject *
1209dict_richcompare(PyObject *v, PyObject *w, int op)
1210{
1211 int cmp;
1212 PyObject *res;
1213
1214 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1215 res = Py_NotImplemented;
1216 }
1217 else if (op == Py_EQ || op == Py_NE) {
1218 cmp = dict_equal((dictobject *)v, (dictobject *)w);
1219 if (cmp < 0)
1220 return NULL;
1221 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1222 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001223 else
1224 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001225 Py_INCREF(res);
1226 return res;
1227 }
1228
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001229static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001230dict_has_key(register dictobject *mp, PyObject *args)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001231{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001232 PyObject *key;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001233 long hash;
1234 register long ok;
Fred Drake52fccfd2000-02-23 15:47:16 +00001235 if (!PyArg_ParseTuple(args, "O:has_key", &key))
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001236 return NULL;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001237#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001238 if (!PyString_Check(key) ||
1239 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001240#endif
Guido van Rossumca756f21997-01-23 19:39:29 +00001241 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001242 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001243 if (hash == -1)
1244 return NULL;
1245 }
Tim Petersdea48ec2001-05-22 20:40:22 +00001246 ok = (mp->ma_lookup)(mp, key, hash)->me_value != NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001247 return PyInt_FromLong(ok);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001248}
1249
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001250static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001251dict_get(register dictobject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001252{
1253 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001254 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001255 PyObject *val = NULL;
1256 long hash;
1257
Fred Drake52fccfd2000-02-23 15:47:16 +00001258 if (!PyArg_ParseTuple(args, "O|O:get", &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001259 return NULL;
1260
Barry Warsawc38c5da1997-10-06 17:49:20 +00001261#ifdef CACHE_HASH
1262 if (!PyString_Check(key) ||
1263 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1264#endif
1265 {
1266 hash = PyObject_Hash(key);
1267 if (hash == -1)
1268 return NULL;
1269 }
Fred Drake1bff34a2000-08-31 19:31:38 +00001270 val = (mp->ma_lookup)(mp, key, hash)->me_value;
Barry Warsaw320ac331997-10-20 17:26:25 +00001271
Barry Warsawc38c5da1997-10-06 17:49:20 +00001272 if (val == NULL)
1273 val = failobj;
1274 Py_INCREF(val);
1275 return val;
1276}
1277
1278
1279static PyObject *
Guido van Rossum164452c2000-08-08 16:12:54 +00001280dict_setdefault(register dictobject *mp, PyObject *args)
1281{
1282 PyObject *key;
1283 PyObject *failobj = Py_None;
1284 PyObject *val = NULL;
1285 long hash;
1286
1287 if (!PyArg_ParseTuple(args, "O|O:setdefault", &key, &failobj))
1288 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001289
1290#ifdef CACHE_HASH
1291 if (!PyString_Check(key) ||
1292 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1293#endif
1294 {
1295 hash = PyObject_Hash(key);
1296 if (hash == -1)
1297 return NULL;
1298 }
Fred Drake1bff34a2000-08-31 19:31:38 +00001299 val = (mp->ma_lookup)(mp, key, hash)->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001300 if (val == NULL) {
1301 val = failobj;
1302 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1303 val = NULL;
1304 }
1305 Py_XINCREF(val);
1306 return val;
1307}
1308
1309
1310static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001311dict_clear(register dictobject *mp, PyObject *args)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001312{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001313 if (!PyArg_NoArgs(args))
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001314 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001315 PyDict_Clear((PyObject *)mp);
1316 Py_INCREF(Py_None);
1317 return Py_None;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001318}
1319
Guido van Rossumba6ab842000-12-12 22:02:18 +00001320static PyObject *
1321dict_popitem(dictobject *mp, PyObject *args)
1322{
1323 int i = 0;
1324 dictentry *ep;
1325 PyObject *res;
1326
1327 if (!PyArg_NoArgs(args))
1328 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001329 /* Allocate the result tuple first. Believe it or not,
1330 * this allocation could trigger a garbage collection which
1331 * could resize the dict, which would invalidate the pointer
1332 * (ep) into the dict calculated below.
1333 * So we have to do this first.
1334 */
1335 res = PyTuple_New(2);
1336 if (res == NULL)
1337 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001338 if (mp->ma_used == 0) {
1339 Py_DECREF(res);
1340 PyErr_SetString(PyExc_KeyError,
1341 "popitem(): dictionary is empty");
1342 return NULL;
1343 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001344 /* Set ep to "the first" dict entry with a value. We abuse the hash
1345 * field of slot 0 to hold a search finger:
1346 * If slot 0 has a value, use slot 0.
1347 * Else slot 0 is being used to hold a search finger,
1348 * and we use its hash value as the first index to look.
1349 */
1350 ep = &mp->ma_table[0];
1351 if (ep->me_value == NULL) {
1352 i = (int)ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001353 /* The hash field may be a real hash value, or it may be a
1354 * legit search finger, or it may be a once-legit search
1355 * finger that's out of bounds now because it wrapped around
1356 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001357 */
1358 if (i >= mp->ma_size || i < 1)
1359 i = 1; /* skip slot 0 */
1360 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1361 i++;
1362 if (i >= mp->ma_size)
1363 i = 1;
1364 }
1365 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001366 PyTuple_SET_ITEM(res, 0, ep->me_key);
1367 PyTuple_SET_ITEM(res, 1, ep->me_value);
1368 Py_INCREF(dummy);
1369 ep->me_key = dummy;
1370 ep->me_value = NULL;
1371 mp->ma_used--;
1372 assert(mp->ma_table[0].me_value == NULL);
1373 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001374 return res;
1375}
1376
Jeremy Hylton8caad492000-06-23 14:18:11 +00001377static int
1378dict_traverse(PyObject *op, visitproc visit, void *arg)
1379{
1380 int i = 0, err;
1381 PyObject *pk;
1382 PyObject *pv;
1383
1384 while (PyDict_Next(op, &i, &pk, &pv)) {
1385 err = visit(pk, arg);
1386 if (err)
1387 return err;
1388 err = visit(pv, arg);
1389 if (err)
1390 return err;
1391 }
1392 return 0;
1393}
1394
1395static int
1396dict_tp_clear(PyObject *op)
1397{
1398 PyDict_Clear(op);
1399 return 0;
1400}
1401
Tim Petersf7f88b12000-12-13 23:18:45 +00001402
Guido van Rossum09e563a2001-05-01 12:10:21 +00001403staticforward PyObject *dictiter_new(dictobject *, binaryfunc);
1404
1405static PyObject *
1406select_key(PyObject *key, PyObject *value)
1407{
1408 Py_INCREF(key);
1409 return key;
1410}
1411
1412static PyObject *
1413select_value(PyObject *key, PyObject *value)
1414{
1415 Py_INCREF(value);
1416 return value;
1417}
1418
1419static PyObject *
1420select_item(PyObject *key, PyObject *value)
1421{
1422 PyObject *res = PyTuple_New(2);
1423
1424 if (res != NULL) {
1425 Py_INCREF(key);
1426 Py_INCREF(value);
1427 PyTuple_SET_ITEM(res, 0, key);
1428 PyTuple_SET_ITEM(res, 1, value);
1429 }
1430 return res;
1431}
1432
1433static PyObject *
1434dict_iterkeys(dictobject *dict, PyObject *args)
1435{
1436 if (!PyArg_ParseTuple(args, ""))
1437 return NULL;
1438 return dictiter_new(dict, select_key);
1439}
1440
1441static PyObject *
1442dict_itervalues(dictobject *dict, PyObject *args)
1443{
1444 if (!PyArg_ParseTuple(args, ""))
1445 return NULL;
1446 return dictiter_new(dict, select_value);
1447}
1448
1449static PyObject *
1450dict_iteritems(dictobject *dict, PyObject *args)
1451{
1452 if (!PyArg_ParseTuple(args, ""))
1453 return NULL;
1454 return dictiter_new(dict, select_item);
1455}
1456
1457
Tim Petersf7f88b12000-12-13 23:18:45 +00001458static char has_key__doc__[] =
1459"D.has_key(k) -> 1 if D has a key k, else 0";
1460
1461static char get__doc__[] =
1462"D.get(k[,d]) -> D[k] if D.has_key(k), else d. d defaults to None.";
1463
1464static char setdefault_doc__[] =
1465"D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if not D.has_key(k)";
1466
1467static char popitem__doc__[] =
1468"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
14692-tuple; but raise KeyError if D is empty";
1470
1471static char keys__doc__[] =
1472"D.keys() -> list of D's keys";
1473
1474static char items__doc__[] =
1475"D.items() -> list of D's (key, value) pairs, as 2-tuples";
1476
1477static char values__doc__[] =
1478"D.values() -> list of D's values";
1479
1480static char update__doc__[] =
1481"D.update(E) -> None. Update D from E: for k in E.keys(): D[k] = E[k]";
1482
1483static char clear__doc__[] =
1484"D.clear() -> None. Remove all items from D.";
1485
1486static char copy__doc__[] =
1487"D.copy() -> a shallow copy of D";
1488
Guido van Rossum09e563a2001-05-01 12:10:21 +00001489static char iterkeys__doc__[] =
1490"D.iterkeys() -> an iterator over the keys of D";
1491
1492static char itervalues__doc__[] =
1493"D.itervalues() -> an iterator over the values of D";
1494
1495static char iteritems__doc__[] =
1496"D.iteritems() -> an iterator over the (key, value) items of D";
1497
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001498static PyMethodDef mapp_methods[] = {
Tim Petersf7f88b12000-12-13 23:18:45 +00001499 {"has_key", (PyCFunction)dict_has_key, METH_VARARGS,
1500 has_key__doc__},
1501 {"get", (PyCFunction)dict_get, METH_VARARGS,
1502 get__doc__},
1503 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1504 setdefault_doc__},
1505 {"popitem", (PyCFunction)dict_popitem, METH_OLDARGS,
1506 popitem__doc__},
1507 {"keys", (PyCFunction)dict_keys, METH_OLDARGS,
1508 keys__doc__},
1509 {"items", (PyCFunction)dict_items, METH_OLDARGS,
1510 items__doc__},
1511 {"values", (PyCFunction)dict_values, METH_OLDARGS,
1512 values__doc__},
1513 {"update", (PyCFunction)dict_update, METH_OLDARGS,
1514 update__doc__},
1515 {"clear", (PyCFunction)dict_clear, METH_OLDARGS,
1516 clear__doc__},
1517 {"copy", (PyCFunction)dict_copy, METH_OLDARGS,
1518 copy__doc__},
Guido van Rossum09e563a2001-05-01 12:10:21 +00001519 {"iterkeys", (PyCFunction)dict_iterkeys, METH_VARARGS,
1520 iterkeys__doc__},
1521 {"itervalues", (PyCFunction)dict_itervalues, METH_VARARGS,
1522 itervalues__doc__},
1523 {"iteritems", (PyCFunction)dict_iteritems, METH_VARARGS,
1524 iteritems__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00001525 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001526};
1527
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001528static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001529dict_getattr(dictobject *mp, char *name)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001530{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001531 return Py_FindMethod(mapp_methods, (PyObject *)mp, name);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001532}
1533
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001534static int
1535dict_contains(dictobject *mp, PyObject *key)
1536{
1537 long hash;
1538
1539#ifdef CACHE_HASH
1540 if (!PyString_Check(key) ||
1541 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1542#endif
1543 {
1544 hash = PyObject_Hash(key);
1545 if (hash == -1)
1546 return -1;
1547 }
Tim Petersdea48ec2001-05-22 20:40:22 +00001548 return (mp->ma_lookup)(mp, key, hash)->me_value != NULL;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001549}
1550
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001551/* Hack to implement "key in dict" */
1552static PySequenceMethods dict_as_sequence = {
1553 0, /* sq_length */
1554 0, /* sq_concat */
1555 0, /* sq_repeat */
1556 0, /* sq_item */
1557 0, /* sq_slice */
1558 0, /* sq_ass_item */
1559 0, /* sq_ass_slice */
1560 (objobjproc)dict_contains, /* sq_contains */
1561 0, /* sq_inplace_concat */
1562 0, /* sq_inplace_repeat */
1563};
1564
Guido van Rossum09e563a2001-05-01 12:10:21 +00001565static PyObject *
1566dict_iter(dictobject *dict)
1567{
1568 return dictiter_new(dict, select_key);
1569}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001570
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001571PyTypeObject PyDict_Type = {
1572 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001573 0,
Guido van Rossum9bfef441993-03-29 10:43:31 +00001574 "dictionary",
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001575 sizeof(dictobject) + PyGC_HEAD_SIZE,
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001576 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00001577 (destructor)dict_dealloc, /* tp_dealloc */
1578 (printfunc)dict_print, /* tp_print */
1579 (getattrfunc)dict_getattr, /* tp_getattr */
1580 0, /* tp_setattr */
Tim Peters4fa58bf2001-05-10 21:45:19 +00001581 (cmpfunc)dict_compare, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00001582 (reprfunc)dict_repr, /* tp_repr */
1583 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001584 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00001585 &dict_as_mapping, /* tp_as_mapping */
1586 0, /* tp_hash */
1587 0, /* tp_call */
1588 0, /* tp_str */
1589 0, /* tp_getattro */
1590 0, /* tp_setattro */
1591 0, /* tp_as_buffer */
1592 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /* tp_flags */
1593 0, /* tp_doc */
1594 (traverseproc)dict_traverse, /* tp_traverse */
1595 (inquiry)dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00001596 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001597 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00001598 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001599 0, /* tp_iternext */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001600};
1601
Guido van Rossum3cca2451997-05-16 14:23:33 +00001602/* For backward compatibility with old dictionary interface */
1603
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001604PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001605PyDict_GetItemString(PyObject *v, char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001606{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001607 PyObject *kv, *rv;
1608 kv = PyString_FromString(key);
1609 if (kv == NULL)
1610 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00001611 rv = PyDict_GetItem(v, kv);
1612 Py_DECREF(kv);
1613 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001614}
1615
1616int
Tim Peters1f5871e2000-07-04 17:44:48 +00001617PyDict_SetItemString(PyObject *v, char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001618{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001619 PyObject *kv;
1620 int err;
1621 kv = PyString_FromString(key);
1622 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00001623 return -1;
Guido van Rossum4f3bf1e1997-09-29 23:31:11 +00001624 PyString_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00001625 err = PyDict_SetItem(v, kv, item);
1626 Py_DECREF(kv);
1627 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001628}
1629
1630int
Tim Peters1f5871e2000-07-04 17:44:48 +00001631PyDict_DelItemString(PyObject *v, char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001632{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001633 PyObject *kv;
1634 int err;
1635 kv = PyString_FromString(key);
1636 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00001637 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00001638 err = PyDict_DelItem(v, kv);
1639 Py_DECREF(kv);
1640 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001641}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001642
1643/* Dictionary iterator type */
1644
1645extern PyTypeObject PyDictIter_Type; /* Forward */
1646
1647typedef struct {
1648 PyObject_HEAD
1649 dictobject *di_dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001650 int di_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001651 int di_pos;
Guido van Rossum09e563a2001-05-01 12:10:21 +00001652 binaryfunc di_select;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001653} dictiterobject;
1654
1655static PyObject *
Guido van Rossum09e563a2001-05-01 12:10:21 +00001656dictiter_new(dictobject *dict, binaryfunc select)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001657{
1658 dictiterobject *di;
1659 di = PyObject_NEW(dictiterobject, &PyDictIter_Type);
1660 if (di == NULL)
1661 return NULL;
1662 Py_INCREF(dict);
1663 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001664 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001665 di->di_pos = 0;
Guido van Rossum09e563a2001-05-01 12:10:21 +00001666 di->di_select = select;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001667 return (PyObject *)di;
1668}
1669
1670static void
1671dictiter_dealloc(dictiterobject *di)
1672{
1673 Py_DECREF(di->di_dict);
1674 PyObject_DEL(di);
1675}
1676
1677static PyObject *
1678dictiter_next(dictiterobject *di, PyObject *args)
1679{
Guido van Rossum09e563a2001-05-01 12:10:21 +00001680 PyObject *key, *value;
Guido van Rossum213c7a62001-04-23 14:08:49 +00001681
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001682 if (di->di_used != di->di_dict->ma_used) {
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001683 PyErr_SetString(PyExc_RuntimeError,
1684 "dictionary changed size during iteration");
1685 return NULL;
1686 }
Guido van Rossum09e563a2001-05-01 12:10:21 +00001687 if (PyDict_Next((PyObject *)(di->di_dict), &di->di_pos, &key, &value)) {
1688 return (*di->di_select)(key, value);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001689 }
1690 PyErr_SetObject(PyExc_StopIteration, Py_None);
1691 return NULL;
1692}
1693
1694static PyObject *
1695dictiter_getiter(PyObject *it)
1696{
1697 Py_INCREF(it);
1698 return it;
1699}
1700
1701static PyMethodDef dictiter_methods[] = {
1702 {"next", (PyCFunction)dictiter_next, METH_VARARGS,
1703 "it.next() -- get the next value, or raise StopIteration"},
1704 {NULL, NULL} /* sentinel */
1705};
1706
1707static PyObject *
Guido van Rossum213c7a62001-04-23 14:08:49 +00001708dictiter_getattr(dictiterobject *di, char *name)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001709{
Guido van Rossum213c7a62001-04-23 14:08:49 +00001710 return Py_FindMethod(dictiter_methods, (PyObject *)di, name);
1711}
1712
1713static PyObject *dictiter_iternext(dictiterobject *di)
1714{
Guido van Rossum09e563a2001-05-01 12:10:21 +00001715 PyObject *key, *value;
Guido van Rossum213c7a62001-04-23 14:08:49 +00001716
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001717 if (di->di_used != di->di_dict->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00001718 PyErr_SetString(PyExc_RuntimeError,
1719 "dictionary changed size during iteration");
1720 return NULL;
1721 }
Guido van Rossum09e563a2001-05-01 12:10:21 +00001722 if (PyDict_Next((PyObject *)(di->di_dict), &di->di_pos, &key, &value)) {
1723 return (*di->di_select)(key, value);
Guido van Rossum213c7a62001-04-23 14:08:49 +00001724 }
1725 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001726}
1727
1728PyTypeObject PyDictIter_Type = {
1729 PyObject_HEAD_INIT(&PyType_Type)
1730 0, /* ob_size */
1731 "dictionary-iterator", /* tp_name */
1732 sizeof(dictiterobject), /* tp_basicsize */
1733 0, /* tp_itemsize */
1734 /* methods */
1735 (destructor)dictiter_dealloc, /* tp_dealloc */
1736 0, /* tp_print */
1737 (getattrfunc)dictiter_getattr, /* tp_getattr */
1738 0, /* tp_setattr */
1739 0, /* tp_compare */
1740 0, /* tp_repr */
1741 0, /* tp_as_number */
1742 0, /* tp_as_sequence */
1743 0, /* tp_as_mapping */
1744 0, /* tp_hash */
1745 0, /* tp_call */
1746 0, /* tp_str */
1747 0, /* tp_getattro */
1748 0, /* tp_setattro */
1749 0, /* tp_as_buffer */
1750 Py_TPFLAGS_DEFAULT, /* tp_flags */
1751 0, /* tp_doc */
1752 0, /* tp_traverse */
1753 0, /* tp_clear */
1754 0, /* tp_richcompare */
1755 0, /* tp_weaklistoffset */
1756 (getiterfunc)dictiter_getiter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001757 (iternextfunc)dictiter_iternext, /* tp_iternext */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001758};