blob: 11963ec1d9fae3f38738f2335a29eb51c922b050 [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{
Tim Peters0c6010b2001-05-23 23:33:57 +0000398 int newsize, newpoly;
399 dictentry *oldtable, *newtable, *ep;
400 int i;
401 int is_oldtable_malloced;
402 dictentry small_copy[MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000403
404 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000405
406 /* Find the smallest table size > minused, and its poly[] entry. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000407 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 Peters0c6010b2001-05-23 23:33:57 +0000423
424 /* Get space for a new table. */
425 oldtable = mp->ma_table;
426 assert(oldtable != NULL);
427 is_oldtable_malloced = oldtable != mp->ma_smalltable;
428
Tim Petersdea48ec2001-05-22 20:40:22 +0000429 if (newsize == MINSIZE) {
Tim Peters0c6010b2001-05-23 23:33:57 +0000430 /* Either a large table is shrinking, or we can't get any
431 smaller. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000432 newtable = mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000433 if (newtable == oldtable) {
434 if (mp->ma_fill < mp->ma_size)
435 return 0;
436 /* The small table is entirely full. We're not
437 going to resise it, but need to rebuild it
438 anyway to purge old dummy entries. */
439 assert(mp->ma_fill > mp->ma_used); /* a dummy exists */
440 memcpy(small_copy, oldtable, sizeof(small_copy));
441 oldtable = small_copy;
442 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000443 }
444 else {
445 newtable = PyMem_NEW(dictentry, newsize);
446 if (newtable == NULL) {
447 PyErr_NoMemory();
448 return -1;
449 }
450 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000451
452 /* Make the dict empty, using the new table. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000453 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000454 mp->ma_table = newtable;
Tim Petersdea48ec2001-05-22 20:40:22 +0000455 mp->ma_size = newsize;
456 memset(newtable, 0, sizeof(dictentry) * newsize);
457 mp->ma_poly = newpoly;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000458 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000459 i = mp->ma_fill;
460 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000461
Tim Peters19283142001-05-17 22:25:34 +0000462 /* Copy the data over; this is refcount-neutral for active entries;
463 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000464 for (ep = oldtable; i > 0; ep++) {
465 if (ep->me_value != NULL) { /* active entry */
466 --i;
Tim Peters19283142001-05-17 22:25:34 +0000467 insertdict(mp, ep->me_key, ep->me_hash, ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000468 }
Tim Peters19283142001-05-17 22:25:34 +0000469 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000470 --i;
Tim Peters19283142001-05-17 22:25:34 +0000471 assert(ep->me_key == dummy);
472 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000473 }
Tim Peters19283142001-05-17 22:25:34 +0000474 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000475 }
476
Tim Peters0c6010b2001-05-23 23:33:57 +0000477 if (is_oldtable_malloced)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000478 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000479 return 0;
480}
481
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000482PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000483PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000484{
485 long hash;
Fred Drake1bff34a2000-08-31 19:31:38 +0000486 dictobject *mp = (dictobject *)op;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000487 if (!PyDict_Check(op)) {
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000488 return NULL;
489 }
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000490#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000491 if (!PyString_Check(key) ||
492 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000493#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000494 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000495 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000496 if (hash == -1) {
497 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000498 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000499 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000500 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000501 return (mp->ma_lookup)(mp, key, hash)->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000502}
503
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000504/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
505 * dictionary if it is merely replacing the value for an existing key.
506 * This is means that it's safe to loop over a dictionary with
507 * PyDict_Next() and occasionally replace a value -- but you can't
508 * insert new keys or remove them.
509 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000510int
Tim Peters1f5871e2000-07-04 17:44:48 +0000511PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000512{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000513 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000514 register long hash;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000515 register int n_used;
516
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000517 if (!PyDict_Check(op)) {
518 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000519 return -1;
520 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000521 mp = (dictobject *)op;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000522#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000523 if (PyString_Check(key)) {
Guido van Rossum2a61e741997-01-18 07:55:05 +0000524#ifdef INTERN_STRINGS
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000525 if (((PyStringObject *)key)->ob_sinterned != NULL) {
526 key = ((PyStringObject *)key)->ob_sinterned;
527 hash = ((PyStringObject *)key)->ob_shash;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000528 }
529 else
530#endif
531 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000532 hash = ((PyStringObject *)key)->ob_shash;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000533 if (hash == -1)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000534 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000535 }
536 }
537 else
538#endif
539 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000540 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000541 if (hash == -1)
542 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000543 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000544 assert(mp->ma_fill < mp->ma_size);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000545 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000546 Py_INCREF(value);
547 Py_INCREF(key);
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000548 insertdict(mp, key, hash, value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000549 /* If we added a key, we can safely resize. Otherwise skip this!
550 * If fill >= 2/3 size, adjust size. Normally, this doubles the
551 * size, but it's also possible for the dict to shrink (if ma_fill is
552 * much larger than ma_used, meaning a lot of dict keys have been
553 * deleted).
554 */
555 if (mp->ma_used > n_used && mp->ma_fill*3 >= mp->ma_size*2) {
556 if (dictresize(mp, mp->ma_used*2) != 0)
557 return -1;
558 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000559 return 0;
560}
561
562int
Tim Peters1f5871e2000-07-04 17:44:48 +0000563PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000564{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000565 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000566 register long hash;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000567 register dictentry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000568 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000569
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000570 if (!PyDict_Check(op)) {
571 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000572 return -1;
573 }
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000574#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000575 if (!PyString_Check(key) ||
576 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000577#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000578 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000579 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000580 if (hash == -1)
581 return -1;
582 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000583 mp = (dictobject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000584 ep = (mp->ma_lookup)(mp, key, hash);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000585 if (ep->me_value == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000586 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000587 return -1;
588 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000589 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000590 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000591 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000592 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000593 ep->me_value = NULL;
594 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000595 Py_DECREF(old_value);
596 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000597 return 0;
598}
599
Guido van Rossum25831651993-05-19 14:50:45 +0000600void
Tim Peters1f5871e2000-07-04 17:44:48 +0000601PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000602{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000603 dictobject *mp;
Tim Petersdea48ec2001-05-22 20:40:22 +0000604 dictentry *ep, *table;
605 int table_is_malloced;
606 int fill;
607 dictentry small_copy[MINSIZE];
608#ifdef Py_DEBUG
609 int i, n;
610#endif
611
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000612 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000613 return;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000614 mp = (dictobject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000615#ifdef Py_DEBUG
Guido van Rossumd7047b31995-01-02 19:07:15 +0000616 n = mp->ma_size;
Tim Petersdea48ec2001-05-22 20:40:22 +0000617 i = 0;
618#endif
619
620 table = mp->ma_table;
621 assert(table != NULL);
622 table_is_malloced = table != mp->ma_smalltable;
623
624 /* This is delicate. During the process of clearing the dict,
625 * decrefs can cause the dict to mutate. To avoid fatal confusion
626 * (voice of experience), we have to make the dict empty before
627 * clearing the slots, and never refer to anything via mp->xxx while
628 * clearing.
629 */
630 fill = mp->ma_fill;
631 if (table_is_malloced)
632 empty_to_minsize(mp);
633
634 else if (fill > 0) {
635 /* It's a small table with something that needs to be cleared.
636 * Afraid the only safe way is to copy the dict entries into
637 * another small table first.
638 */
639 memcpy(small_copy, table, sizeof(small_copy));
640 table = small_copy;
641 empty_to_minsize(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000642 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000643 /* else it's a small table that's already empty */
644
645 /* Now we can finally clear things. If C had refcounts, we could
646 * assert that the refcount on table is 1 now, i.e. that this function
647 * has unique access to it, so decref side-effects can't alter it.
648 */
649 for (ep = table; fill > 0; ++ep) {
650#ifdef Py_DEBUG
651 assert(i < n);
652 ++i;
653#endif
654 if (ep->me_key) {
655 --fill;
656 Py_DECREF(ep->me_key);
657 Py_XDECREF(ep->me_value);
658 }
659#ifdef Py_DEBUG
660 else
661 assert(ep->me_value == NULL);
662#endif
663 }
664
665 if (table_is_malloced)
666 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000667}
668
Tim Peters67830702001-03-21 19:23:56 +0000669/* CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
670 * mutates the dict. One exception: it is safe if the loop merely changes
671 * the values associated with the keys (but doesn't insert new keys or
672 * delete keys), via PyDict_SetItem().
673 */
Guido van Rossum25831651993-05-19 14:50:45 +0000674int
Tim Peters1f5871e2000-07-04 17:44:48 +0000675PyDict_Next(PyObject *op, int *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000676{
Guido van Rossum25831651993-05-19 14:50:45 +0000677 int i;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000678 register dictobject *mp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000679 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000680 return 0;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000681 mp = (dictobject *)op;
Guido van Rossum25831651993-05-19 14:50:45 +0000682 i = *ppos;
683 if (i < 0)
684 return 0;
685 while (i < mp->ma_size && mp->ma_table[i].me_value == NULL)
686 i++;
687 *ppos = i+1;
688 if (i >= mp->ma_size)
689 return 0;
690 if (pkey)
691 *pkey = mp->ma_table[i].me_key;
692 if (pvalue)
693 *pvalue = mp->ma_table[i].me_value;
694 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000695}
696
697/* Methods */
698
699static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000700dict_dealloc(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000701{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000702 register dictentry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000703 int fill = mp->ma_fill;
Guido van Rossumd724b232000-03-13 16:01:29 +0000704 Py_TRASHCAN_SAFE_BEGIN(mp)
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000705 PyObject_GC_Fini(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000706 for (ep = mp->ma_table; fill > 0; ep++) {
707 if (ep->me_key) {
708 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000709 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000710 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000711 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000712 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000713 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000714 PyMem_DEL(mp->ma_table);
Guido van Rossum4cc6ac72000-07-01 01:00:38 +0000715 mp = (dictobject *) PyObject_AS_GC(mp);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000716 PyObject_DEL(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000717 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000718}
719
720static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000721dict_print(register dictobject *mp, register FILE *fp, register int flags)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000722{
723 register int i;
724 register int any;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000725 register dictentry *ep;
Guido van Rossum255443b1998-04-10 22:47:14 +0000726
727 i = Py_ReprEnter((PyObject*)mp);
728 if (i != 0) {
729 if (i < 0)
730 return i;
731 fprintf(fp, "{...}");
732 return 0;
733 }
734
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000735 fprintf(fp, "{");
736 any = 0;
737 for (i = 0, ep = mp->ma_table; i < mp->ma_size; i++, ep++) {
738 if (ep->me_value != NULL) {
739 if (any++ > 0)
740 fprintf(fp, ", ");
Guido van Rossum255443b1998-04-10 22:47:14 +0000741 if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
742 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000743 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000744 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000745 fprintf(fp, ": ");
Guido van Rossum255443b1998-04-10 22:47:14 +0000746 if (PyObject_Print(ep->me_value, fp, 0) != 0) {
747 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000748 return -1;
Guido van Rossum255443b1998-04-10 22:47:14 +0000749 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000750 }
751 }
752 fprintf(fp, "}");
Guido van Rossum255443b1998-04-10 22:47:14 +0000753 Py_ReprLeave((PyObject*)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000754 return 0;
755}
756
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000757static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000758dict_repr(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000759{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000760 auto PyObject *v;
761 PyObject *sepa, *colon;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000762 register int i;
763 register int any;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000764 register dictentry *ep;
Guido van Rossum255443b1998-04-10 22:47:14 +0000765
766 i = Py_ReprEnter((PyObject*)mp);
767 if (i != 0) {
768 if (i > 0)
769 return PyString_FromString("{...}");
770 return NULL;
771 }
772
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000773 v = PyString_FromString("{");
774 sepa = PyString_FromString(", ");
775 colon = PyString_FromString(": ");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000776 any = 0;
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000777 for (i = 0, ep = mp->ma_table; i < mp->ma_size && v; i++, ep++) {
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000778 if (ep->me_value != NULL) {
779 if (any++)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000780 PyString_Concat(&v, sepa);
781 PyString_ConcatAndDel(&v, PyObject_Repr(ep->me_key));
782 PyString_Concat(&v, colon);
783 PyString_ConcatAndDel(&v, PyObject_Repr(ep->me_value));
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000784 }
785 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000786 PyString_ConcatAndDel(&v, PyString_FromString("}"));
Guido van Rossum255443b1998-04-10 22:47:14 +0000787 Py_ReprLeave((PyObject*)mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000788 Py_XDECREF(sepa);
789 Py_XDECREF(colon);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000790 return v;
791}
792
793static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000794dict_length(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000795{
796 return mp->ma_used;
797}
798
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000799static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000800dict_subscript(dictobject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000801{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000802 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000803 long hash;
Tim Petersdea48ec2001-05-22 20:40:22 +0000804 assert(mp->ma_table != NULL);
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000805#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000806 if (!PyString_Check(key) ||
807 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000808#endif
Guido van Rossumca756f21997-01-23 19:39:29 +0000809 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000810 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000811 if (hash == -1)
812 return NULL;
813 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000814 v = (mp->ma_lookup)(mp, key, hash) -> me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000815 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000816 PyErr_SetObject(PyExc_KeyError, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000817 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000818 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000819 return v;
820}
821
822static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000823dict_ass_sub(dictobject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000824{
825 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000826 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000827 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000828 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000829}
830
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000831static PyMappingMethods dict_as_mapping = {
832 (inquiry)dict_length, /*mp_length*/
833 (binaryfunc)dict_subscript, /*mp_subscript*/
834 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000835};
836
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000837static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000838dict_keys(register dictobject *mp, PyObject *args)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000839{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000840 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000841 register int i, j, n;
842
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000843 if (!PyArg_NoArgs(args))
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000844 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000845 again:
846 n = mp->ma_used;
847 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000848 if (v == NULL)
849 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000850 if (n != mp->ma_used) {
851 /* Durnit. The allocations caused the dict to resize.
852 * Just start over, this shouldn't normally happen.
853 */
854 Py_DECREF(v);
855 goto again;
856 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000857 for (i = 0, j = 0; i < mp->ma_size; i++) {
858 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000859 PyObject *key = mp->ma_table[i].me_key;
860 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000861 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000862 j++;
863 }
864 }
865 return v;
866}
867
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000868static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000869dict_values(register dictobject *mp, PyObject *args)
Guido van Rossum25831651993-05-19 14:50:45 +0000870{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000871 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000872 register int i, j, n;
873
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000874 if (!PyArg_NoArgs(args))
Guido van Rossum25831651993-05-19 14:50:45 +0000875 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000876 again:
877 n = mp->ma_used;
878 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +0000879 if (v == NULL)
880 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000881 if (n != mp->ma_used) {
882 /* Durnit. The allocations caused the dict to resize.
883 * Just start over, this shouldn't normally happen.
884 */
885 Py_DECREF(v);
886 goto again;
887 }
Guido van Rossum25831651993-05-19 14:50:45 +0000888 for (i = 0, j = 0; i < mp->ma_size; i++) {
889 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000890 PyObject *value = mp->ma_table[i].me_value;
891 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000892 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +0000893 j++;
894 }
895 }
896 return v;
897}
898
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000899static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000900dict_items(register dictobject *mp, PyObject *args)
Guido van Rossum25831651993-05-19 14:50:45 +0000901{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000902 register PyObject *v;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000903 register int i, j, n;
904 PyObject *item, *key, *value;
905
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000906 if (!PyArg_NoArgs(args))
Guido van Rossum25831651993-05-19 14:50:45 +0000907 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000908 /* Preallocate the list of tuples, to avoid allocations during
909 * the loop over the items, which could trigger GC, which
910 * could resize the dict. :-(
911 */
912 again:
913 n = mp->ma_used;
914 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +0000915 if (v == NULL)
916 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000917 for (i = 0; i < n; i++) {
918 item = PyTuple_New(2);
919 if (item == NULL) {
920 Py_DECREF(v);
921 return NULL;
922 }
923 PyList_SET_ITEM(v, i, item);
924 }
925 if (n != mp->ma_used) {
926 /* Durnit. The allocations caused the dict to resize.
927 * Just start over, this shouldn't normally happen.
928 */
929 Py_DECREF(v);
930 goto again;
931 }
932 /* Nothing we do below makes any function calls. */
Guido van Rossum25831651993-05-19 14:50:45 +0000933 for (i = 0, j = 0; i < mp->ma_size; i++) {
934 if (mp->ma_table[i].me_value != NULL) {
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000935 key = mp->ma_table[i].me_key;
936 value = mp->ma_table[i].me_value;
937 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000938 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000939 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000940 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000941 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +0000942 j++;
943 }
944 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000945 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +0000946 return v;
947}
948
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000949static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000950dict_update(register dictobject *mp, PyObject *args)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000951{
952 register int i;
953 dictobject *other;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000954 dictentry *entry;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000955 if (!PyArg_Parse(args, "O!", &PyDict_Type, &other))
956 return NULL;
Jeremy Hylton1fb60882001-01-03 22:34:59 +0000957 if (other == mp || other->ma_used == 0)
958 goto done; /* a.update(a) or a.update({}); nothing to do */
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000959 /* Do one big resize at the start, rather than incrementally
960 resizing as we insert new items. Expect that there will be
961 no (or few) overlapping keys. */
962 if ((mp->ma_fill + other->ma_used)*3 >= mp->ma_size*2) {
963 if (dictresize(mp, (mp->ma_used + other->ma_used)*3/2) != 0)
964 return NULL;
965 }
966 for (i = 0; i < other->ma_size; i++) {
967 entry = &other->ma_table[i];
968 if (entry->me_value != NULL) {
969 Py_INCREF(entry->me_key);
970 Py_INCREF(entry->me_value);
971 insertdict(mp, entry->me_key, entry->me_hash,
972 entry->me_value);
973 }
974 }
975 done:
976 Py_INCREF(Py_None);
977 return Py_None;
978}
979
980static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000981dict_copy(register dictobject *mp, PyObject *args)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000982{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +0000983 if (!PyArg_Parse(args, ""))
984 return NULL;
985 return PyDict_Copy((PyObject*)mp);
986}
987
988PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000989PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +0000990{
991 register dictobject *mp;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000992 register int i;
993 dictobject *copy;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000994 dictentry *entry;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +0000995
996 if (o == NULL || !PyDict_Check(o)) {
997 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +0000998 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +0000999 }
1000 mp = (dictobject *)o;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001001 copy = (dictobject *)PyDict_New();
1002 if (copy == NULL)
1003 return NULL;
1004 if (mp->ma_used > 0) {
1005 if (dictresize(copy, mp->ma_used*3/2) != 0)
1006 return NULL;
1007 for (i = 0; i < mp->ma_size; i++) {
1008 entry = &mp->ma_table[i];
1009 if (entry->me_value != NULL) {
1010 Py_INCREF(entry->me_key);
1011 Py_INCREF(entry->me_value);
1012 insertdict(copy, entry->me_key, entry->me_hash,
1013 entry->me_value);
1014 }
1015 }
1016 }
1017 return (PyObject *)copy;
1018}
1019
Guido van Rossum4199fac1993-11-05 10:18:44 +00001020int
Tim Peters1f5871e2000-07-04 17:44:48 +00001021PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001022{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001023 if (mp == NULL || !PyDict_Check(mp)) {
1024 PyErr_BadInternalCall();
Guido van Rossumb376a4a1993-11-23 17:53:17 +00001025 return 0;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001026 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001027 return ((dictobject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001028}
1029
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001030PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001031PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001032{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001033 if (mp == NULL || !PyDict_Check(mp)) {
1034 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001035 return NULL;
1036 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001037 return dict_keys((dictobject *)mp, (PyObject *)NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001038}
1039
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001040PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001041PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001042{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001043 if (mp == NULL || !PyDict_Check(mp)) {
1044 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001045 return NULL;
1046 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001047 return dict_values((dictobject *)mp, (PyObject *)NULL);
Guido van Rossum25831651993-05-19 14:50:45 +00001048}
1049
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001050PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001051PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001052{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001053 if (mp == NULL || !PyDict_Check(mp)) {
1054 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001055 return NULL;
1056 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001057 return dict_items((dictobject *)mp, (PyObject *)NULL);
Guido van Rossum25831651993-05-19 14:50:45 +00001058}
1059
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001060/* Subroutine which returns the smallest key in a for which b's value
1061 is different or absent. The value is returned too, through the
Tim Peters95bf9392001-05-10 08:32:44 +00001062 pval argument. Both are NULL if no key in a is found for which b's status
1063 differs. The refcounts on (and only on) non-NULL *pval and function return
1064 values must be decremented by the caller (characterize() increments them
1065 to ensure that mutating comparison and PyDict_GetItem calls can't delete
1066 them before the caller is done looking at them). */
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001067
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001068static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001069characterize(dictobject *a, dictobject *b, PyObject **pval)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001070{
Tim Peters95bf9392001-05-10 08:32:44 +00001071 PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
1072 PyObject *aval = NULL; /* a[akey] */
Guido van Rossumb9324202001-01-18 00:39:02 +00001073 int i, cmp;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001074
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001075 for (i = 0; i < a->ma_size; i++) {
Tim Peters95bf9392001-05-10 08:32:44 +00001076 PyObject *thiskey, *thisaval, *thisbval;
1077 if (a->ma_table[i].me_value == NULL)
1078 continue;
1079 thiskey = a->ma_table[i].me_key;
1080 Py_INCREF(thiskey); /* keep alive across compares */
1081 if (akey != NULL) {
1082 cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
1083 if (cmp < 0) {
1084 Py_DECREF(thiskey);
1085 goto Fail;
1086 }
1087 if (cmp > 0 ||
1088 i >= a->ma_size ||
1089 a->ma_table[i].me_value == NULL)
1090 {
1091 /* Not the *smallest* a key; or maybe it is
1092 * but the compare shrunk the dict so we can't
1093 * find its associated value anymore; or
1094 * maybe it is but the compare deleted the
1095 * a[thiskey] entry.
1096 */
1097 Py_DECREF(thiskey);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001098 continue;
Guido van Rossumb9324202001-01-18 00:39:02 +00001099 }
Tim Peters95bf9392001-05-10 08:32:44 +00001100 }
1101
1102 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
1103 thisaval = a->ma_table[i].me_value;
1104 assert(thisaval);
1105 Py_INCREF(thisaval); /* keep alive */
1106 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
1107 if (thisbval == NULL)
1108 cmp = 0;
1109 else {
1110 /* both dicts have thiskey: same values? */
1111 cmp = PyObject_RichCompareBool(
1112 thisaval, thisbval, Py_EQ);
1113 if (cmp < 0) {
1114 Py_DECREF(thiskey);
1115 Py_DECREF(thisaval);
1116 goto Fail;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001117 }
1118 }
Tim Peters95bf9392001-05-10 08:32:44 +00001119 if (cmp == 0) {
1120 /* New winner. */
1121 Py_XDECREF(akey);
1122 Py_XDECREF(aval);
1123 akey = thiskey;
1124 aval = thisaval;
1125 }
1126 else {
1127 Py_DECREF(thiskey);
1128 Py_DECREF(thisaval);
1129 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001130 }
Tim Peters95bf9392001-05-10 08:32:44 +00001131 *pval = aval;
1132 return akey;
1133
1134Fail:
1135 Py_XDECREF(akey);
1136 Py_XDECREF(aval);
1137 *pval = NULL;
1138 return NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001139}
1140
1141static int
Tim Peters1f5871e2000-07-04 17:44:48 +00001142dict_compare(dictobject *a, dictobject *b)
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001143{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001144 PyObject *adiff, *bdiff, *aval, *bval;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001145 int res;
1146
1147 /* Compare lengths first */
1148 if (a->ma_used < b->ma_used)
1149 return -1; /* a is shorter */
1150 else if (a->ma_used > b->ma_used)
1151 return 1; /* b is shorter */
Tim Peters95bf9392001-05-10 08:32:44 +00001152
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001153 /* Same length -- check all keys */
Tim Peters95bf9392001-05-10 08:32:44 +00001154 bdiff = bval = NULL;
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001155 adiff = characterize(a, b, &aval);
Tim Peters95bf9392001-05-10 08:32:44 +00001156 if (adiff == NULL) {
1157 assert(!aval);
Tim Peters3918fb22001-05-10 18:58:31 +00001158 /* Either an error, or a is a subset with the same length so
Tim Peters95bf9392001-05-10 08:32:44 +00001159 * must be equal.
1160 */
1161 res = PyErr_Occurred() ? -1 : 0;
1162 goto Finished;
1163 }
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001164 bdiff = characterize(b, a, &bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001165 if (bdiff == NULL && PyErr_Occurred()) {
1166 assert(!bval);
1167 res = -1;
1168 goto Finished;
1169 }
1170 res = 0;
1171 if (bdiff) {
1172 /* bdiff == NULL "should be" impossible now, but perhaps
1173 * the last comparison done by the characterize() on a had
1174 * the side effect of making the dicts equal!
1175 */
1176 res = PyObject_Compare(adiff, bdiff);
1177 }
1178 if (res == 0 && bval != NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001179 res = PyObject_Compare(aval, bval);
Tim Peters95bf9392001-05-10 08:32:44 +00001180
1181Finished:
1182 Py_XDECREF(adiff);
1183 Py_XDECREF(bdiff);
1184 Py_XDECREF(aval);
1185 Py_XDECREF(bval);
Guido van Rossuma0a69b81996-12-05 21:55:55 +00001186 return res;
1187}
1188
Tim Peterse63415e2001-05-08 04:38:29 +00001189/* Return 1 if dicts equal, 0 if not, -1 if error.
1190 * Gets out as soon as any difference is detected.
1191 * Uses only Py_EQ comparison.
1192 */
1193static int
1194dict_equal(dictobject *a, dictobject *b)
1195{
1196 int i;
1197
1198 if (a->ma_used != b->ma_used)
1199 /* can't be equal if # of entries differ */
1200 return 0;
1201
1202 /* Same # of entries -- check all of 'em. Exit early on any diff. */
1203 for (i = 0; i < a->ma_size; i++) {
1204 PyObject *aval = a->ma_table[i].me_value;
1205 if (aval != NULL) {
1206 int cmp;
1207 PyObject *bval;
1208 PyObject *key = a->ma_table[i].me_key;
1209 /* temporarily bump aval's refcount to ensure it stays
1210 alive until we're done with it */
1211 Py_INCREF(aval);
1212 bval = PyDict_GetItem((PyObject *)b, key);
1213 if (bval == NULL) {
1214 Py_DECREF(aval);
1215 return 0;
1216 }
1217 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1218 Py_DECREF(aval);
1219 if (cmp <= 0) /* error or not equal */
1220 return cmp;
1221 }
1222 }
1223 return 1;
1224 }
1225
1226static PyObject *
1227dict_richcompare(PyObject *v, PyObject *w, int op)
1228{
1229 int cmp;
1230 PyObject *res;
1231
1232 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1233 res = Py_NotImplemented;
1234 }
1235 else if (op == Py_EQ || op == Py_NE) {
1236 cmp = dict_equal((dictobject *)v, (dictobject *)w);
1237 if (cmp < 0)
1238 return NULL;
1239 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1240 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001241 else
1242 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001243 Py_INCREF(res);
1244 return res;
1245 }
1246
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001247static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001248dict_has_key(register dictobject *mp, PyObject *args)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001249{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001250 PyObject *key;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001251 long hash;
1252 register long ok;
Fred Drake52fccfd2000-02-23 15:47:16 +00001253 if (!PyArg_ParseTuple(args, "O:has_key", &key))
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001254 return NULL;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001255#ifdef CACHE_HASH
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001256 if (!PyString_Check(key) ||
1257 (hash = ((PyStringObject *) key)->ob_shash) == -1)
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001258#endif
Guido van Rossumca756f21997-01-23 19:39:29 +00001259 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001260 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001261 if (hash == -1)
1262 return NULL;
1263 }
Tim Petersdea48ec2001-05-22 20:40:22 +00001264 ok = (mp->ma_lookup)(mp, key, hash)->me_value != NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001265 return PyInt_FromLong(ok);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001266}
1267
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001268static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001269dict_get(register dictobject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001270{
1271 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001272 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001273 PyObject *val = NULL;
1274 long hash;
1275
Fred Drake52fccfd2000-02-23 15:47:16 +00001276 if (!PyArg_ParseTuple(args, "O|O:get", &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001277 return NULL;
1278
Barry Warsawc38c5da1997-10-06 17:49:20 +00001279#ifdef CACHE_HASH
1280 if (!PyString_Check(key) ||
1281 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1282#endif
1283 {
1284 hash = PyObject_Hash(key);
1285 if (hash == -1)
1286 return NULL;
1287 }
Fred Drake1bff34a2000-08-31 19:31:38 +00001288 val = (mp->ma_lookup)(mp, key, hash)->me_value;
Barry Warsaw320ac331997-10-20 17:26:25 +00001289
Barry Warsawc38c5da1997-10-06 17:49:20 +00001290 if (val == NULL)
1291 val = failobj;
1292 Py_INCREF(val);
1293 return val;
1294}
1295
1296
1297static PyObject *
Guido van Rossum164452c2000-08-08 16:12:54 +00001298dict_setdefault(register dictobject *mp, PyObject *args)
1299{
1300 PyObject *key;
1301 PyObject *failobj = Py_None;
1302 PyObject *val = NULL;
1303 long hash;
1304
1305 if (!PyArg_ParseTuple(args, "O|O:setdefault", &key, &failobj))
1306 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001307
1308#ifdef CACHE_HASH
1309 if (!PyString_Check(key) ||
1310 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1311#endif
1312 {
1313 hash = PyObject_Hash(key);
1314 if (hash == -1)
1315 return NULL;
1316 }
Fred Drake1bff34a2000-08-31 19:31:38 +00001317 val = (mp->ma_lookup)(mp, key, hash)->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001318 if (val == NULL) {
1319 val = failobj;
1320 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1321 val = NULL;
1322 }
1323 Py_XINCREF(val);
1324 return val;
1325}
1326
1327
1328static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001329dict_clear(register dictobject *mp, PyObject *args)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001330{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001331 if (!PyArg_NoArgs(args))
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001332 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001333 PyDict_Clear((PyObject *)mp);
1334 Py_INCREF(Py_None);
1335 return Py_None;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001336}
1337
Guido van Rossumba6ab842000-12-12 22:02:18 +00001338static PyObject *
1339dict_popitem(dictobject *mp, PyObject *args)
1340{
1341 int i = 0;
1342 dictentry *ep;
1343 PyObject *res;
1344
1345 if (!PyArg_NoArgs(args))
1346 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001347 /* Allocate the result tuple first. Believe it or not,
1348 * this allocation could trigger a garbage collection which
1349 * could resize the dict, which would invalidate the pointer
1350 * (ep) into the dict calculated below.
1351 * So we have to do this first.
1352 */
1353 res = PyTuple_New(2);
1354 if (res == NULL)
1355 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001356 if (mp->ma_used == 0) {
1357 Py_DECREF(res);
1358 PyErr_SetString(PyExc_KeyError,
1359 "popitem(): dictionary is empty");
1360 return NULL;
1361 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001362 /* Set ep to "the first" dict entry with a value. We abuse the hash
1363 * field of slot 0 to hold a search finger:
1364 * If slot 0 has a value, use slot 0.
1365 * Else slot 0 is being used to hold a search finger,
1366 * and we use its hash value as the first index to look.
1367 */
1368 ep = &mp->ma_table[0];
1369 if (ep->me_value == NULL) {
1370 i = (int)ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001371 /* The hash field may be a real hash value, or it may be a
1372 * legit search finger, or it may be a once-legit search
1373 * finger that's out of bounds now because it wrapped around
1374 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001375 */
1376 if (i >= mp->ma_size || i < 1)
1377 i = 1; /* skip slot 0 */
1378 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1379 i++;
1380 if (i >= mp->ma_size)
1381 i = 1;
1382 }
1383 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001384 PyTuple_SET_ITEM(res, 0, ep->me_key);
1385 PyTuple_SET_ITEM(res, 1, ep->me_value);
1386 Py_INCREF(dummy);
1387 ep->me_key = dummy;
1388 ep->me_value = NULL;
1389 mp->ma_used--;
1390 assert(mp->ma_table[0].me_value == NULL);
1391 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001392 return res;
1393}
1394
Jeremy Hylton8caad492000-06-23 14:18:11 +00001395static int
1396dict_traverse(PyObject *op, visitproc visit, void *arg)
1397{
1398 int i = 0, err;
1399 PyObject *pk;
1400 PyObject *pv;
1401
1402 while (PyDict_Next(op, &i, &pk, &pv)) {
1403 err = visit(pk, arg);
1404 if (err)
1405 return err;
1406 err = visit(pv, arg);
1407 if (err)
1408 return err;
1409 }
1410 return 0;
1411}
1412
1413static int
1414dict_tp_clear(PyObject *op)
1415{
1416 PyDict_Clear(op);
1417 return 0;
1418}
1419
Tim Petersf7f88b12000-12-13 23:18:45 +00001420
Guido van Rossum09e563a2001-05-01 12:10:21 +00001421staticforward PyObject *dictiter_new(dictobject *, binaryfunc);
1422
1423static PyObject *
1424select_key(PyObject *key, PyObject *value)
1425{
1426 Py_INCREF(key);
1427 return key;
1428}
1429
1430static PyObject *
1431select_value(PyObject *key, PyObject *value)
1432{
1433 Py_INCREF(value);
1434 return value;
1435}
1436
1437static PyObject *
1438select_item(PyObject *key, PyObject *value)
1439{
1440 PyObject *res = PyTuple_New(2);
1441
1442 if (res != NULL) {
1443 Py_INCREF(key);
1444 Py_INCREF(value);
1445 PyTuple_SET_ITEM(res, 0, key);
1446 PyTuple_SET_ITEM(res, 1, value);
1447 }
1448 return res;
1449}
1450
1451static PyObject *
1452dict_iterkeys(dictobject *dict, PyObject *args)
1453{
1454 if (!PyArg_ParseTuple(args, ""))
1455 return NULL;
1456 return dictiter_new(dict, select_key);
1457}
1458
1459static PyObject *
1460dict_itervalues(dictobject *dict, PyObject *args)
1461{
1462 if (!PyArg_ParseTuple(args, ""))
1463 return NULL;
1464 return dictiter_new(dict, select_value);
1465}
1466
1467static PyObject *
1468dict_iteritems(dictobject *dict, PyObject *args)
1469{
1470 if (!PyArg_ParseTuple(args, ""))
1471 return NULL;
1472 return dictiter_new(dict, select_item);
1473}
1474
1475
Tim Petersf7f88b12000-12-13 23:18:45 +00001476static char has_key__doc__[] =
1477"D.has_key(k) -> 1 if D has a key k, else 0";
1478
1479static char get__doc__[] =
1480"D.get(k[,d]) -> D[k] if D.has_key(k), else d. d defaults to None.";
1481
1482static char setdefault_doc__[] =
1483"D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if not D.has_key(k)";
1484
1485static char popitem__doc__[] =
1486"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
14872-tuple; but raise KeyError if D is empty";
1488
1489static char keys__doc__[] =
1490"D.keys() -> list of D's keys";
1491
1492static char items__doc__[] =
1493"D.items() -> list of D's (key, value) pairs, as 2-tuples";
1494
1495static char values__doc__[] =
1496"D.values() -> list of D's values";
1497
1498static char update__doc__[] =
1499"D.update(E) -> None. Update D from E: for k in E.keys(): D[k] = E[k]";
1500
1501static char clear__doc__[] =
1502"D.clear() -> None. Remove all items from D.";
1503
1504static char copy__doc__[] =
1505"D.copy() -> a shallow copy of D";
1506
Guido van Rossum09e563a2001-05-01 12:10:21 +00001507static char iterkeys__doc__[] =
1508"D.iterkeys() -> an iterator over the keys of D";
1509
1510static char itervalues__doc__[] =
1511"D.itervalues() -> an iterator over the values of D";
1512
1513static char iteritems__doc__[] =
1514"D.iteritems() -> an iterator over the (key, value) items of D";
1515
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001516static PyMethodDef mapp_methods[] = {
Tim Petersf7f88b12000-12-13 23:18:45 +00001517 {"has_key", (PyCFunction)dict_has_key, METH_VARARGS,
1518 has_key__doc__},
1519 {"get", (PyCFunction)dict_get, METH_VARARGS,
1520 get__doc__},
1521 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1522 setdefault_doc__},
1523 {"popitem", (PyCFunction)dict_popitem, METH_OLDARGS,
1524 popitem__doc__},
1525 {"keys", (PyCFunction)dict_keys, METH_OLDARGS,
1526 keys__doc__},
1527 {"items", (PyCFunction)dict_items, METH_OLDARGS,
1528 items__doc__},
1529 {"values", (PyCFunction)dict_values, METH_OLDARGS,
1530 values__doc__},
1531 {"update", (PyCFunction)dict_update, METH_OLDARGS,
1532 update__doc__},
1533 {"clear", (PyCFunction)dict_clear, METH_OLDARGS,
1534 clear__doc__},
1535 {"copy", (PyCFunction)dict_copy, METH_OLDARGS,
1536 copy__doc__},
Guido van Rossum09e563a2001-05-01 12:10:21 +00001537 {"iterkeys", (PyCFunction)dict_iterkeys, METH_VARARGS,
1538 iterkeys__doc__},
1539 {"itervalues", (PyCFunction)dict_itervalues, METH_VARARGS,
1540 itervalues__doc__},
1541 {"iteritems", (PyCFunction)dict_iteritems, METH_VARARGS,
1542 iteritems__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00001543 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001544};
1545
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001546static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001547dict_getattr(dictobject *mp, char *name)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001548{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001549 return Py_FindMethod(mapp_methods, (PyObject *)mp, name);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001550}
1551
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001552static int
1553dict_contains(dictobject *mp, PyObject *key)
1554{
1555 long hash;
1556
1557#ifdef CACHE_HASH
1558 if (!PyString_Check(key) ||
1559 (hash = ((PyStringObject *) key)->ob_shash) == -1)
1560#endif
1561 {
1562 hash = PyObject_Hash(key);
1563 if (hash == -1)
1564 return -1;
1565 }
Tim Petersdea48ec2001-05-22 20:40:22 +00001566 return (mp->ma_lookup)(mp, key, hash)->me_value != NULL;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001567}
1568
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001569/* Hack to implement "key in dict" */
1570static PySequenceMethods dict_as_sequence = {
1571 0, /* sq_length */
1572 0, /* sq_concat */
1573 0, /* sq_repeat */
1574 0, /* sq_item */
1575 0, /* sq_slice */
1576 0, /* sq_ass_item */
1577 0, /* sq_ass_slice */
1578 (objobjproc)dict_contains, /* sq_contains */
1579 0, /* sq_inplace_concat */
1580 0, /* sq_inplace_repeat */
1581};
1582
Guido van Rossum09e563a2001-05-01 12:10:21 +00001583static PyObject *
1584dict_iter(dictobject *dict)
1585{
1586 return dictiter_new(dict, select_key);
1587}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001588
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001589PyTypeObject PyDict_Type = {
1590 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001591 0,
Guido van Rossum9bfef441993-03-29 10:43:31 +00001592 "dictionary",
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +00001593 sizeof(dictobject) + PyGC_HEAD_SIZE,
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001594 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00001595 (destructor)dict_dealloc, /* tp_dealloc */
1596 (printfunc)dict_print, /* tp_print */
1597 (getattrfunc)dict_getattr, /* tp_getattr */
1598 0, /* tp_setattr */
Tim Peters4fa58bf2001-05-10 21:45:19 +00001599 (cmpfunc)dict_compare, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00001600 (reprfunc)dict_repr, /* tp_repr */
1601 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001602 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00001603 &dict_as_mapping, /* tp_as_mapping */
1604 0, /* tp_hash */
1605 0, /* tp_call */
1606 0, /* tp_str */
1607 0, /* tp_getattro */
1608 0, /* tp_setattro */
1609 0, /* tp_as_buffer */
1610 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /* tp_flags */
1611 0, /* tp_doc */
1612 (traverseproc)dict_traverse, /* tp_traverse */
1613 (inquiry)dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00001614 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001615 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00001616 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001617 0, /* tp_iternext */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001618};
1619
Guido van Rossum3cca2451997-05-16 14:23:33 +00001620/* For backward compatibility with old dictionary interface */
1621
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001622PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001623PyDict_GetItemString(PyObject *v, char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001624{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001625 PyObject *kv, *rv;
1626 kv = PyString_FromString(key);
1627 if (kv == NULL)
1628 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00001629 rv = PyDict_GetItem(v, kv);
1630 Py_DECREF(kv);
1631 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001632}
1633
1634int
Tim Peters1f5871e2000-07-04 17:44:48 +00001635PyDict_SetItemString(PyObject *v, char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001636{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001637 PyObject *kv;
1638 int err;
1639 kv = PyString_FromString(key);
1640 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00001641 return -1;
Guido van Rossum4f3bf1e1997-09-29 23:31:11 +00001642 PyString_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00001643 err = PyDict_SetItem(v, kv, item);
1644 Py_DECREF(kv);
1645 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001646}
1647
1648int
Tim Peters1f5871e2000-07-04 17:44:48 +00001649PyDict_DelItemString(PyObject *v, char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001650{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001651 PyObject *kv;
1652 int err;
1653 kv = PyString_FromString(key);
1654 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00001655 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00001656 err = PyDict_DelItem(v, kv);
1657 Py_DECREF(kv);
1658 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001659}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001660
1661/* Dictionary iterator type */
1662
1663extern PyTypeObject PyDictIter_Type; /* Forward */
1664
1665typedef struct {
1666 PyObject_HEAD
1667 dictobject *di_dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001668 int di_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001669 int di_pos;
Guido van Rossum09e563a2001-05-01 12:10:21 +00001670 binaryfunc di_select;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001671} dictiterobject;
1672
1673static PyObject *
Guido van Rossum09e563a2001-05-01 12:10:21 +00001674dictiter_new(dictobject *dict, binaryfunc select)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001675{
1676 dictiterobject *di;
1677 di = PyObject_NEW(dictiterobject, &PyDictIter_Type);
1678 if (di == NULL)
1679 return NULL;
1680 Py_INCREF(dict);
1681 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001682 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001683 di->di_pos = 0;
Guido van Rossum09e563a2001-05-01 12:10:21 +00001684 di->di_select = select;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001685 return (PyObject *)di;
1686}
1687
1688static void
1689dictiter_dealloc(dictiterobject *di)
1690{
1691 Py_DECREF(di->di_dict);
1692 PyObject_DEL(di);
1693}
1694
1695static PyObject *
1696dictiter_next(dictiterobject *di, PyObject *args)
1697{
Guido van Rossum09e563a2001-05-01 12:10:21 +00001698 PyObject *key, *value;
Guido van Rossum213c7a62001-04-23 14:08:49 +00001699
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001700 if (di->di_used != di->di_dict->ma_used) {
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001701 PyErr_SetString(PyExc_RuntimeError,
1702 "dictionary changed size during iteration");
1703 return NULL;
1704 }
Guido van Rossum09e563a2001-05-01 12:10:21 +00001705 if (PyDict_Next((PyObject *)(di->di_dict), &di->di_pos, &key, &value)) {
1706 return (*di->di_select)(key, value);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001707 }
1708 PyErr_SetObject(PyExc_StopIteration, Py_None);
1709 return NULL;
1710}
1711
1712static PyObject *
1713dictiter_getiter(PyObject *it)
1714{
1715 Py_INCREF(it);
1716 return it;
1717}
1718
1719static PyMethodDef dictiter_methods[] = {
1720 {"next", (PyCFunction)dictiter_next, METH_VARARGS,
1721 "it.next() -- get the next value, or raise StopIteration"},
1722 {NULL, NULL} /* sentinel */
1723};
1724
1725static PyObject *
Guido van Rossum213c7a62001-04-23 14:08:49 +00001726dictiter_getattr(dictiterobject *di, char *name)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001727{
Guido van Rossum213c7a62001-04-23 14:08:49 +00001728 return Py_FindMethod(dictiter_methods, (PyObject *)di, name);
1729}
1730
1731static PyObject *dictiter_iternext(dictiterobject *di)
1732{
Guido van Rossum09e563a2001-05-01 12:10:21 +00001733 PyObject *key, *value;
Guido van Rossum213c7a62001-04-23 14:08:49 +00001734
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00001735 if (di->di_used != di->di_dict->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00001736 PyErr_SetString(PyExc_RuntimeError,
1737 "dictionary changed size during iteration");
1738 return NULL;
1739 }
Guido van Rossum09e563a2001-05-01 12:10:21 +00001740 if (PyDict_Next((PyObject *)(di->di_dict), &di->di_pos, &key, &value)) {
1741 return (*di->di_select)(key, value);
Guido van Rossum213c7a62001-04-23 14:08:49 +00001742 }
1743 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001744}
1745
1746PyTypeObject PyDictIter_Type = {
1747 PyObject_HEAD_INIT(&PyType_Type)
1748 0, /* ob_size */
1749 "dictionary-iterator", /* tp_name */
1750 sizeof(dictiterobject), /* tp_basicsize */
1751 0, /* tp_itemsize */
1752 /* methods */
1753 (destructor)dictiter_dealloc, /* tp_dealloc */
1754 0, /* tp_print */
1755 (getattrfunc)dictiter_getattr, /* tp_getattr */
1756 0, /* tp_setattr */
1757 0, /* tp_compare */
1758 0, /* tp_repr */
1759 0, /* tp_as_number */
1760 0, /* tp_as_sequence */
1761 0, /* tp_as_mapping */
1762 0, /* tp_hash */
1763 0, /* tp_call */
1764 0, /* tp_str */
1765 0, /* tp_getattro */
1766 0, /* tp_setattro */
1767 0, /* tp_as_buffer */
1768 Py_TPFLAGS_DEFAULT, /* tp_flags */
1769 0, /* tp_doc */
1770 0, /* tp_traverse */
1771 0, /* tp_clear */
1772 0, /* tp_richcompare */
1773 0, /* tp_weaklistoffset */
1774 (getiterfunc)dictiter_getiter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001775 (iternextfunc)dictiter_iternext, /* tp_iternext */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001776};