blob: 3365c4757c15394ba647d0d7171ab6896c03f74a [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
Raymond Hettinger930427b2003-05-03 06:51:59 +00004/* The distribution includes a separate file, Objects/dictnotes.txt,
Tim Peters60b29962006-01-01 01:19:23 +00005 describing explorations into dictionary design and optimization.
Raymond Hettinger930427b2003-05-03 06:51:59 +00006 It covers typical dictionary use patterns, the parameters for
7 tuning dictionaries, and several ideas for possible optimizations.
8*/
9
Guido van Rossumc0b618a1997-05-02 03:12:38 +000010#include "Python.h"
Christian Heimes0ded5b52007-12-10 15:50:56 +000011#include "stringlib/eq.h"
Guido van Rossum4b1302b1993-03-27 18:11:32 +000012
Guido van Rossum16e93a81997-01-28 00:00:11 +000013
Thomas Wouters89f507f2006-12-13 04:49:30 +000014/* Set a key error with the specified argument, wrapping it in a
15 * tuple automatically so that tuple keys are not unpacked as the
16 * exception arguments. */
17static void
18set_key_error(PyObject *arg)
19{
20 PyObject *tup;
21 tup = PyTuple_Pack(1, arg);
22 if (!tup)
23 return; /* caller will expect error to be set anyway */
24 PyErr_SetObject(PyExc_KeyError, tup);
25 Py_DECREF(tup);
26}
27
Tim Peterseb28ef22001-06-02 05:27:19 +000028/* Define this out if you don't want conversion statistics on exit. */
Fred Drake1bff34a2000-08-31 19:31:38 +000029#undef SHOW_CONVERSION_COUNTS
30
Tim Peterseb28ef22001-06-02 05:27:19 +000031/* See large comment block below. This must be >= 1. */
32#define PERTURB_SHIFT 5
33
Guido van Rossum16e93a81997-01-28 00:00:11 +000034/*
Tim Peterseb28ef22001-06-02 05:27:19 +000035Major subtleties ahead: Most hash schemes depend on having a "good" hash
36function, in the sense of simulating randomness. Python doesn't: its most
37important hash functions (for strings and ints) are very regular in common
38cases:
Tim Peters15d49292001-05-27 07:39:22 +000039
Guido van Rossumdc5f6b22006-08-24 21:29:26 +000040 >>> map(hash, (0, 1, 2, 3))
41 [0, 1, 2, 3]
42 >>> map(hash, ("namea", "nameb", "namec", "named"))
43 [-1658398457, -1658398460, -1658398459, -1658398462]
44 >>>
Tim Peters15d49292001-05-27 07:39:22 +000045
Tim Peterseb28ef22001-06-02 05:27:19 +000046This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
47the low-order i bits as the initial table index is extremely fast, and there
48are no collisions at all for dicts indexed by a contiguous range of ints.
49The same is approximately true when keys are "consecutive" strings. So this
50gives better-than-random behavior in common cases, and that's very desirable.
Tim Peters15d49292001-05-27 07:39:22 +000051
Tim Peterseb28ef22001-06-02 05:27:19 +000052OTOH, when collisions occur, the tendency to fill contiguous slices of the
53hash table makes a good collision resolution strategy crucial. Taking only
54the last i bits of the hash code is also vulnerable: for example, consider
Guido van Rossumdc5f6b22006-08-24 21:29:26 +000055the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
56their own hash codes, and this fits in a dict of size 2**15, the last 15 bits
57 of every hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +000058
Tim Peterseb28ef22001-06-02 05:27:19 +000059But catering to unusual cases should not slow the usual ones, so we just take
60the last i bits anyway. It's up to collision resolution to do the rest. If
61we *usually* find the key we're looking for on the first try (and, it turns
62out, we usually do -- the table load factor is kept under 2/3, so the odds
63are solidly in our favor), then it makes best sense to keep the initial index
64computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +000065
Tim Peterseb28ef22001-06-02 05:27:19 +000066The first half of collision resolution is to visit table indices via this
67recurrence:
Tim Peters15d49292001-05-27 07:39:22 +000068
Tim Peterseb28ef22001-06-02 05:27:19 +000069 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +000070
Tim Peterseb28ef22001-06-02 05:27:19 +000071For any initial j in range(2**i), repeating that 2**i times generates each
72int in range(2**i) exactly once (see any text on random-number generation for
73proof). By itself, this doesn't help much: like linear probing (setting
74j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
75order. This would be bad, except that's not the only thing we do, and it's
76actually *good* in the common cases where hash keys are consecutive. In an
77example that's really too small to make this entirely clear, for a table of
78size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +000079
Tim Peterseb28ef22001-06-02 05:27:19 +000080 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
81
82If two things come in at index 5, the first place we look after is index 2,
83not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
84Linear probing is deadly in this case because there the fixed probe order
85is the *same* as the order consecutive keys are likely to arrive. But it's
86extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
87and certain that consecutive hash codes do not.
88
89The other half of the strategy is to get the other bits of the hash code
90into play. This is done by initializing a (unsigned) vrbl "perturb" to the
91full hash code, and changing the recurrence to:
92
93 j = (5*j) + 1 + perturb;
94 perturb >>= PERTURB_SHIFT;
95 use j % 2**i as the next table index;
96
97Now the probe sequence depends (eventually) on every bit in the hash code,
98and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
99because it quickly magnifies small differences in the bits that didn't affect
100the initial index. Note that because perturb is unsigned, if the recurrence
101is executed often enough perturb eventually becomes and remains 0. At that
102point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
103that's certain to find an empty slot eventually (since it generates every int
104in range(2**i), and we make sure there's always at least one empty slot).
105
106Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
107small so that the high bits of the hash code continue to affect the probe
108sequence across iterations; but you want it large so that in really bad cases
109the high-order hash bits have an effect on early iterations. 5 was "the
110best" in minimizing total collisions across experiments Tim Peters ran (on
111both normal and pathological cases), but 4 and 6 weren't significantly worse.
112
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000113Historical: Reimer Behrends contributed the idea of using a polynomial-based
Tim Peterseb28ef22001-06-02 05:27:19 +0000114approach, using repeated multiplication by x in GF(2**n) where an irreducible
115polynomial for each table size was chosen such that x was a primitive root.
116Christian Tismer later extended that to use division by x instead, as an
117efficient way to get the high bits of the hash code into play. This scheme
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000118also gave excellent collision statistics, but was more expensive: two
119if-tests were required inside the loop; computing "the next" index took about
120the same number of operations but without as much potential parallelism
121(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
122above, and then shifting perturb can be done while the table index is being
123masked); and the PyDictObject struct required a member to hold the table's
124polynomial. In Tim's experiments the current scheme ran faster, produced
125equally good collision statistics, needed less code & used less memory.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000126
127Theoretical Python 2.5 headache: hash codes are only C "long", but
128sizeof(Py_ssize_t) > sizeof(long) may be possible. In that case, and if a
129dict is genuinely huge, then only the slots directly reachable via indexing
130by a C long can be the first slot in a probe sequence. The probe sequence
131will still eventually reach every slot in the table, but the collision rate
132on initial probes may be much higher than this scheme was designed for.
133Getting a hash code as fat as Py_ssize_t is the only real cure. But in
134practice, this probably won't make a lick of difference for many years (at
135which point everyone will have terabytes of RAM on 64-bit boxes).
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000136*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000137
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000138/* Object used as dummy key to fill deleted entries */
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000139static PyObject *dummy = NULL; /* Initialized by first call to newPyDictObject() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000140
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000141#ifdef Py_REF_DEBUG
142PyObject *
143_PyDict_Dummy(void)
144{
145 return dummy;
146}
147#endif
148
Fred Drake1bff34a2000-08-31 19:31:38 +0000149/* forward declarations */
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000150static PyDictEntry *
151lookdict_unicode(PyDictObject *mp, PyObject *key, long hash);
Fred Drake1bff34a2000-08-31 19:31:38 +0000152
153#ifdef SHOW_CONVERSION_COUNTS
154static long created = 0L;
155static long converted = 0L;
156
157static void
158show_counts(void)
159{
160 fprintf(stderr, "created %ld string dicts\n", created);
161 fprintf(stderr, "converted %ld to normal dicts\n", converted);
162 fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created);
163}
164#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000165
Christian Heimes77c02eb2008-02-09 02:18:51 +0000166/* Debug statistic to compare allocations with reuse through the free list */
167#undef SHOW_ALLOC_COUNT
168#ifdef SHOW_ALLOC_COUNT
169static size_t count_alloc = 0;
170static size_t count_reuse = 0;
171
172static void
173show_alloc(void)
174{
175 fprintf(stderr, "Dict allocations: %zd\n", count_alloc);
176 fprintf(stderr, "Dict reuse through freelist: %zd\n", count_reuse);
177 fprintf(stderr, "%.2f%% reuse rate\n\n",
178 (100.0*count_reuse/(count_alloc+count_reuse)));
179}
180#endif
181
Tim Peters6d6c1a32001-08-02 04:15:00 +0000182/* Initialization macros.
183 There are two ways to create a dict: PyDict_New() is the main C API
184 function, and the tp_new slot maps to dict_new(). In the latter case we
185 can save a little time over what PyDict_New does because it's guaranteed
186 that the PyDictObject struct is already zeroed out.
187 Everyone except dict_new() should use EMPTY_TO_MINSIZE (unless they have
188 an excellent reason not to).
189*/
190
191#define INIT_NONZERO_DICT_SLOTS(mp) do { \
Tim Petersdea48ec2001-05-22 20:40:22 +0000192 (mp)->ma_table = (mp)->ma_smalltable; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000193 (mp)->ma_mask = PyDict_MINSIZE - 1; \
194 } while(0)
195
196#define EMPTY_TO_MINSIZE(mp) do { \
197 memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000198 (mp)->ma_used = (mp)->ma_fill = 0; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000199 INIT_NONZERO_DICT_SLOTS(mp); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000200 } while(0)
201
Raymond Hettinger43442782004-03-17 21:55:03 +0000202/* Dictionary reuse scheme to save calls to malloc, free, and memset */
Christian Heimes2202f872008-02-06 14:31:34 +0000203#ifndef PyDict_MAXFREELIST
204#define PyDict_MAXFREELIST 80
205#endif
206static PyDictObject *free_list[PyDict_MAXFREELIST];
207static int numfree = 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000208
Christian Heimes77c02eb2008-02-09 02:18:51 +0000209void
210PyDict_Fini(void)
211{
212 PyDictObject *op;
213
214 while (numfree) {
215 op = free_list[--numfree];
216 assert(PyDict_CheckExact(op));
217 PyObject_GC_Del(op);
218 }
219}
220
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000221PyObject *
Thomas Wouters78890102000-07-22 19:25:51 +0000222PyDict_New(void)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000223{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000224 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000225 if (dummy == NULL) { /* Auto-initialize dummy */
Neal Norwitz53cbdaa2007-08-23 21:42:55 +0000226 dummy = PyUnicode_FromString("<dummy key>");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000227 if (dummy == NULL)
228 return NULL;
Fred Drake1bff34a2000-08-31 19:31:38 +0000229#ifdef SHOW_CONVERSION_COUNTS
230 Py_AtExit(show_counts);
231#endif
Christian Heimes77c02eb2008-02-09 02:18:51 +0000232#ifdef SHOW_ALLOC_COUNT
233 Py_AtExit(show_alloc);
234#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000235 }
Christian Heimes2202f872008-02-06 14:31:34 +0000236 if (numfree) {
237 mp = free_list[--numfree];
Raymond Hettinger43442782004-03-17 21:55:03 +0000238 assert (mp != NULL);
Christian Heimes90aa7642007-12-19 02:45:37 +0000239 assert (Py_TYPE(mp) == &PyDict_Type);
Raymond Hettinger43442782004-03-17 21:55:03 +0000240 _Py_NewReference((PyObject *)mp);
241 if (mp->ma_fill) {
242 EMPTY_TO_MINSIZE(mp);
243 }
244 assert (mp->ma_used == 0);
245 assert (mp->ma_table == mp->ma_smalltable);
246 assert (mp->ma_mask == PyDict_MINSIZE - 1);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000247#ifdef SHOW_ALLOC_COUNT
248 count_reuse++;
249#endif
Raymond Hettinger43442782004-03-17 21:55:03 +0000250 } else {
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000251 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
Raymond Hettinger43442782004-03-17 21:55:03 +0000252 if (mp == NULL)
253 return NULL;
254 EMPTY_TO_MINSIZE(mp);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000255#ifdef SHOW_ALLOC_COUNT
256 count_alloc++;
257#endif
Raymond Hettinger43442782004-03-17 21:55:03 +0000258 }
Guido van Rossum89d8c602007-09-18 17:26:56 +0000259 mp->ma_lookup = lookdict_unicode;
Fred Drake1bff34a2000-08-31 19:31:38 +0000260#ifdef SHOW_CONVERSION_COUNTS
261 ++created;
262#endif
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000263 _PyObject_GC_TRACK(mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000264 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000265}
266
267/*
268The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000269This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000270Open addressing is preferred over chaining since the link overhead for
271chaining would be substantial (100% with typical malloc overhead).
272
Tim Peterseb28ef22001-06-02 05:27:19 +0000273The initial probe index is computed as hash mod the table size. Subsequent
274probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000275
276All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000277
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000278The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000279contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000280Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000281
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000282lookdict() is general-purpose, and may return NULL if (and only if) a
283comparison raises an exception (this was new in Python 2.5).
Guido van Rossum89d8c602007-09-18 17:26:56 +0000284lookdict_unicode() below is specialized to string keys, comparison of which can
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000285never raise an exception; that function can never return NULL. For both, when
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000286the key isn't found a PyDictEntry* is returned for which the me_value field is
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000287NULL; this is the slot in the dict at which the key would have been found, and
288the caller can (if it wishes) add the <key, value> pair to the returned
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000289PyDictEntry*.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000290*/
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000291static PyDictEntry *
292lookdict(PyDictObject *mp, PyObject *key, register long hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000293{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000294 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000295 register size_t perturb;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000296 register PyDictEntry *freeslot;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000297 register size_t mask = (size_t)mp->ma_mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000298 PyDictEntry *ep0 = mp->ma_table;
299 register PyDictEntry *ep;
Fred Drakec88b99c2000-08-31 19:04:07 +0000300 register int cmp;
Tim Peters453163d2001-06-03 04:54:32 +0000301 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000302
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000303 i = (size_t)hash & mask;
Guido van Rossumefb46091997-01-29 15:53:56 +0000304 ep = &ep0[i];
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000305 if (ep->me_key == NULL || ep->me_key == key)
Guido van Rossum7d181591997-01-16 21:06:45 +0000306 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000307
Guido van Rossum16e93a81997-01-28 00:00:11 +0000308 if (ep->me_key == dummy)
Guido van Rossum7d181591997-01-16 21:06:45 +0000309 freeslot = ep;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000310 else {
Fred Drakec88b99c2000-08-31 19:04:07 +0000311 if (ep->me_hash == hash) {
Tim Peters453163d2001-06-03 04:54:32 +0000312 startkey = ep->me_key;
Guido van Rossum2fd4f372007-11-29 18:43:05 +0000313 Py_INCREF(startkey);
Tim Peters453163d2001-06-03 04:54:32 +0000314 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Guido van Rossum2fd4f372007-11-29 18:43:05 +0000315 Py_DECREF(startkey);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000316 if (cmp < 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000317 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000318 if (ep0 == mp->ma_table && ep->me_key == startkey) {
319 if (cmp > 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000320 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000321 }
322 else {
323 /* The compare did major nasty stuff to the
324 * dict: start over.
325 * XXX A clever adversary could prevent this
326 * XXX from terminating.
327 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000328 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000329 }
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000330 }
331 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000332 }
Tim Peters15d49292001-05-27 07:39:22 +0000333
Tim Peters342c65e2001-05-13 06:43:53 +0000334 /* In the loop, me_key == dummy is by far (factor of 100s) the
335 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000336 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
337 i = (i << 2) + i + perturb + 1;
338 ep = &ep0[i & mask];
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000339 if (ep->me_key == NULL)
340 return freeslot == NULL ? ep : freeslot;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000341 if (ep->me_key == key)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000342 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000343 if (ep->me_hash == hash && ep->me_key != dummy) {
Tim Peters453163d2001-06-03 04:54:32 +0000344 startkey = ep->me_key;
Guido van Rossum2fd4f372007-11-29 18:43:05 +0000345 Py_INCREF(startkey);
Tim Peters453163d2001-06-03 04:54:32 +0000346 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Guido van Rossum2fd4f372007-11-29 18:43:05 +0000347 Py_DECREF(startkey);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000348 if (cmp < 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000349 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000350 if (ep0 == mp->ma_table && ep->me_key == startkey) {
351 if (cmp > 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000352 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000353 }
354 else {
355 /* The compare did major nasty stuff to the
356 * dict: start over.
357 * XXX A clever adversary could prevent this
358 * XXX from terminating.
359 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000360 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000361 }
Guido van Rossum16e93a81997-01-28 00:00:11 +0000362 }
Tim Peters342c65e2001-05-13 06:43:53 +0000363 else if (ep->me_key == dummy && freeslot == NULL)
364 freeslot = ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000365 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000366 assert(0); /* NOT REACHED */
367 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000368}
369
370/*
Guido van Rossum89d8c602007-09-18 17:26:56 +0000371 * Hacked up version of lookdict which can assume keys are always
372 * unicodes; this assumption allows testing for errors during
373 * PyObject_RichCompareBool() to be dropped; unicode-unicode
374 * comparisons never raise exceptions. This also means we don't need
375 * to go through PyObject_RichCompareBool(); we can always use
376 * unicode_eq() directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000377 *
Guido van Rossum89d8c602007-09-18 17:26:56 +0000378 * This is valuable because dicts with only unicode keys are very common.
Fred Drake1bff34a2000-08-31 19:31:38 +0000379 */
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000380static PyDictEntry *
381lookdict_unicode(PyDictObject *mp, PyObject *key, register long hash)
Fred Drake1bff34a2000-08-31 19:31:38 +0000382{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000383 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000384 register size_t perturb;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000385 register PyDictEntry *freeslot;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000386 register size_t mask = (size_t)mp->ma_mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000387 PyDictEntry *ep0 = mp->ma_table;
388 register PyDictEntry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000389
Guido van Rossum89d8c602007-09-18 17:26:56 +0000390 /* Make sure this function doesn't have to handle non-unicode keys,
Tim Peters0ab085c2001-09-14 00:25:33 +0000391 including subclasses of str; e.g., one reason to subclass
Guido van Rossum89d8c602007-09-18 17:26:56 +0000392 unicodes is to override __eq__, and for speed we don't cater to
Tim Peters0ab085c2001-09-14 00:25:33 +0000393 that here. */
Guido van Rossum89d8c602007-09-18 17:26:56 +0000394 if (!PyUnicode_CheckExact(key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000395#ifdef SHOW_CONVERSION_COUNTS
396 ++converted;
397#endif
398 mp->ma_lookup = lookdict;
399 return lookdict(mp, key, hash);
400 }
Tim Peters2f228e72001-05-13 00:19:31 +0000401 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000402 ep = &ep0[i];
403 if (ep->me_key == NULL || ep->me_key == key)
404 return ep;
405 if (ep->me_key == dummy)
406 freeslot = ep;
407 else {
Guido van Rossum89d8c602007-09-18 17:26:56 +0000408 if (ep->me_hash == hash && unicode_eq(ep->me_key, key))
Fred Drake1bff34a2000-08-31 19:31:38 +0000409 return ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000410 freeslot = NULL;
411 }
Tim Peters15d49292001-05-27 07:39:22 +0000412
Tim Peters342c65e2001-05-13 06:43:53 +0000413 /* In the loop, me_key == dummy is by far (factor of 100s) the
414 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000415 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
416 i = (i << 2) + i + perturb + 1;
417 ep = &ep0[i & mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000418 if (ep->me_key == NULL)
419 return freeslot == NULL ? ep : freeslot;
420 if (ep->me_key == key
421 || (ep->me_hash == hash
422 && ep->me_key != dummy
Guido van Rossum89d8c602007-09-18 17:26:56 +0000423 && unicode_eq(ep->me_key, key)))
Fred Drake1bff34a2000-08-31 19:31:38 +0000424 return ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000425 if (ep->me_key == dummy && freeslot == NULL)
Tim Peters342c65e2001-05-13 06:43:53 +0000426 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000427 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000428 assert(0); /* NOT REACHED */
429 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000430}
431
432/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000433Internal routine to insert a new item into the table.
434Used both by the internal resize routine and by the public insert routine.
435Eats a reference to key and one to value.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000436Returns -1 if an error occurred, or 0 on success.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000437*/
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000438static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000439insertdict(register PyDictObject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000440{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000441 PyObject *old_value;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000442 register PyDictEntry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000443 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
444
445 assert(mp->ma_lookup != NULL);
446 ep = mp->ma_lookup(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000447 if (ep == NULL) {
448 Py_DECREF(key);
449 Py_DECREF(value);
450 return -1;
451 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000452 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000453 old_value = ep->me_value;
454 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000455 Py_DECREF(old_value); /* which **CAN** re-enter */
456 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000457 }
458 else {
459 if (ep->me_key == NULL)
460 mp->ma_fill++;
Raymond Hettinger07ead172005-02-05 23:42:57 +0000461 else {
462 assert(ep->me_key == dummy);
463 Py_DECREF(dummy);
464 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000465 ep->me_key = key;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000466 ep->me_hash = (Py_ssize_t)hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000467 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000468 mp->ma_used++;
469 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000470 return 0;
471}
472
473/*
474Internal routine used by dictresize() to insert an item which is
475known to be absent from the dict. This routine also assumes that
476the dict contains no deleted entries. Besides the performance benefit,
477using insertdict() in dictresize() is dangerous (SF bug #1456209).
478Note that no refcounts are changed by this routine; if needed, the caller
479is responsible for incref'ing `key` and `value`.
480*/
481static void
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000482insertdict_clean(register PyDictObject *mp, PyObject *key, long hash,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000483 PyObject *value)
484{
485 register size_t i;
486 register size_t perturb;
487 register size_t mask = (size_t)mp->ma_mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000488 PyDictEntry *ep0 = mp->ma_table;
489 register PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000490
491 i = hash & mask;
492 ep = &ep0[i];
493 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
494 i = (i << 2) + i + perturb + 1;
495 ep = &ep0[i & mask];
496 }
497 assert(ep->me_value == NULL);
498 mp->ma_fill++;
499 ep->me_key = key;
500 ep->me_hash = (Py_ssize_t)hash;
501 ep->me_value = value;
502 mp->ma_used++;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000503}
504
505/*
506Restructure the table by allocating a new table and reinserting all
507items again. When entries have been deleted, the new table may
508actually be smaller than the old one.
509*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000510static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000511dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000512{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000513 Py_ssize_t newsize;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000514 PyDictEntry *oldtable, *newtable, *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000515 Py_ssize_t i;
Tim Peters0c6010b2001-05-23 23:33:57 +0000516 int is_oldtable_malloced;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000517 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000518
519 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000520
Tim Peterseb28ef22001-06-02 05:27:19 +0000521 /* Find the smallest table size > minused. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000522 for (newsize = PyDict_MINSIZE;
Tim Peterseb28ef22001-06-02 05:27:19 +0000523 newsize <= minused && newsize > 0;
524 newsize <<= 1)
525 ;
526 if (newsize <= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000527 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000528 return -1;
529 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000530
531 /* Get space for a new table. */
532 oldtable = mp->ma_table;
533 assert(oldtable != NULL);
534 is_oldtable_malloced = oldtable != mp->ma_smalltable;
535
Tim Peters6d6c1a32001-08-02 04:15:00 +0000536 if (newsize == PyDict_MINSIZE) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000537 /* A large table is shrinking, or we can't get any smaller. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000538 newtable = mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000539 if (newtable == oldtable) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000540 if (mp->ma_fill == mp->ma_used) {
541 /* No dummies, so no point doing anything. */
Tim Peters0c6010b2001-05-23 23:33:57 +0000542 return 0;
Tim Petersf8a548c2001-05-24 16:26:40 +0000543 }
544 /* We're not going to resize it, but rebuild the
545 table anyway to purge old dummy entries.
546 Subtle: This is *necessary* if fill==size,
547 as lookdict needs at least one virgin slot to
548 terminate failing searches. If fill < size, it's
549 merely desirable, as dummies slow searches. */
550 assert(mp->ma_fill > mp->ma_used);
Tim Peters0c6010b2001-05-23 23:33:57 +0000551 memcpy(small_copy, oldtable, sizeof(small_copy));
552 oldtable = small_copy;
553 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000554 }
555 else {
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000556 newtable = PyMem_NEW(PyDictEntry, newsize);
Tim Petersdea48ec2001-05-22 20:40:22 +0000557 if (newtable == NULL) {
558 PyErr_NoMemory();
559 return -1;
560 }
561 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000562
563 /* Make the dict empty, using the new table. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000564 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000565 mp->ma_table = newtable;
Tim Petersafb6ae82001-06-04 21:00:21 +0000566 mp->ma_mask = newsize - 1;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000567 memset(newtable, 0, sizeof(PyDictEntry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000568 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000569 i = mp->ma_fill;
570 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000571
Tim Peters19283142001-05-17 22:25:34 +0000572 /* Copy the data over; this is refcount-neutral for active entries;
573 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000574 for (ep = oldtable; i > 0; ep++) {
575 if (ep->me_value != NULL) { /* active entry */
576 --i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000577 insertdict_clean(mp, ep->me_key, (long)ep->me_hash,
578 ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000579 }
Tim Peters19283142001-05-17 22:25:34 +0000580 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000581 --i;
Tim Peters19283142001-05-17 22:25:34 +0000582 assert(ep->me_key == dummy);
583 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000584 }
Tim Peters19283142001-05-17 22:25:34 +0000585 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000586 }
587
Tim Peters0c6010b2001-05-23 23:33:57 +0000588 if (is_oldtable_malloced)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000589 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000590 return 0;
591}
592
Christian Heimes99170a52007-12-19 02:07:34 +0000593/* Create a new dictionary pre-sized to hold an estimated number of elements.
594 Underestimates are okay because the dictionary will resize as necessary.
595 Overestimates just mean the dictionary will be more sparse than usual.
596*/
597
598PyObject *
599_PyDict_NewPresized(Py_ssize_t minused)
600{
601 PyObject *op = PyDict_New();
602
603 if (minused>5 && op != NULL && dictresize((PyDictObject *)op, minused) == -1) {
604 Py_DECREF(op);
605 return NULL;
606 }
607 return op;
608}
609
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000610/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
611 * that may occur (originally dicts supported only string keys, and exceptions
612 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000613 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000614 * (suppressed) occurred while computing the key's hash, or that some error
615 * (suppressed) occurred when comparing keys in the dict's internal probe
616 * sequence. A nasty example of the latter is when a Python-coded comparison
617 * function hits a stack-depth error, which can cause this to return NULL
618 * even if the key is present.
619 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000620PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000621PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000622{
623 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000624 PyDictObject *mp = (PyDictObject *)op;
625 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000626 PyThreadState *tstate;
627 if (!PyDict_Check(op))
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000628 return NULL;
Guido van Rossum4d027722007-09-18 04:30:42 +0000629 if (!PyUnicode_CheckExact(key) ||
630 (hash = ((PyUnicodeObject *) key)->hash) == -1)
Guido van Rossumca756f21997-01-23 19:39:29 +0000631 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000632 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000633 if (hash == -1) {
634 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000635 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000636 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000637 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000638
639 /* We can arrive here with a NULL tstate during initialization:
640 try running "python -Wi" for an example related to string
641 interning. Let's just hope that no exception occurs then... */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000642 tstate = _PyThreadState_Current;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000643 if (tstate != NULL && tstate->curexc_type != NULL) {
644 /* preserve the existing exception */
645 PyObject *err_type, *err_value, *err_tb;
646 PyErr_Fetch(&err_type, &err_value, &err_tb);
647 ep = (mp->ma_lookup)(mp, key, hash);
648 /* ignore errors */
649 PyErr_Restore(err_type, err_value, err_tb);
650 if (ep == NULL)
651 return NULL;
652 }
653 else {
654 ep = (mp->ma_lookup)(mp, key, hash);
655 if (ep == NULL) {
656 PyErr_Clear();
657 return NULL;
658 }
659 }
660 return ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000661}
662
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000663/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
664 This returns NULL *with* an exception set if an exception occurred.
665 It returns NULL *without* an exception set if the key wasn't present.
666*/
667PyObject *
668PyDict_GetItemWithError(PyObject *op, PyObject *key)
669{
670 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000671 PyDictObject*mp = (PyDictObject *)op;
672 PyDictEntry *ep;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000673
674 if (!PyDict_Check(op)) {
675 PyErr_BadInternalCall();
676 return NULL;
677 }
Guido van Rossum89d8c602007-09-18 17:26:56 +0000678 if (!PyUnicode_CheckExact(key) ||
679 (hash = ((PyUnicodeObject *) key)->hash) == -1)
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000680 {
681 hash = PyObject_Hash(key);
682 if (hash == -1) {
683 return NULL;
684 }
685 }
686
687 ep = (mp->ma_lookup)(mp, key, hash);
688 if (ep == NULL)
689 return NULL;
690 return ep->me_value;
691}
692
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000693/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
Tim Peters60b29962006-01-01 01:19:23 +0000694 * dictionary if it's merely replacing the value for an existing key.
695 * This means that it's safe to loop over a dictionary with PyDict_Next()
696 * and occasionally replace a value -- but you can't insert new keys or
697 * remove them.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000698 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000699int
Tim Peters1f5871e2000-07-04 17:44:48 +0000700PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000701{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000702 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000703 register long hash;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000704 register Py_ssize_t n_used;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000705
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000706 if (!PyDict_Check(op)) {
707 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000708 return -1;
709 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000710 assert(key);
711 assert(value);
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000712 mp = (PyDictObject *)op;
Guido van Rossum4d027722007-09-18 04:30:42 +0000713 if (!PyUnicode_CheckExact(key) ||
714 (hash = ((PyUnicodeObject *) key)->hash) == -1)
715 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000716 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000717 if (hash == -1)
718 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000719 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000720 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000721 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000722 Py_INCREF(value);
723 Py_INCREF(key);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000724 if (insertdict(mp, key, hash, value) != 0)
725 return -1;
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000726 /* If we added a key, we can safely resize. Otherwise just return!
727 * If fill >= 2/3 size, adjust size. Normally, this doubles or
728 * quaduples the size, but it's also possible for the dict to shrink
Tim Peters60b29962006-01-01 01:19:23 +0000729 * (if ma_fill is much larger than ma_used, meaning a lot of dict
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000730 * keys have been * deleted).
Tim Peters60b29962006-01-01 01:19:23 +0000731 *
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000732 * Quadrupling the size improves average dictionary sparseness
733 * (reducing collisions) at the cost of some memory and iteration
734 * speed (which loops over every possible entry). It also halves
735 * the number of expensive resize operations in a growing dictionary.
Tim Peters60b29962006-01-01 01:19:23 +0000736 *
737 * Very large dictionaries (over 50K items) use doubling instead.
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000738 * This may help applications with severe memory constraints.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000739 */
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000740 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
741 return 0;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000742 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000743}
744
745int
Tim Peters1f5871e2000-07-04 17:44:48 +0000746PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000747{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000748 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000749 register long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000750 register PyDictEntry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000751 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000752
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000753 if (!PyDict_Check(op)) {
754 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000755 return -1;
756 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000757 assert(key);
Guido van Rossum89d8c602007-09-18 17:26:56 +0000758 if (!PyUnicode_CheckExact(key) ||
759 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000760 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000761 if (hash == -1)
762 return -1;
763 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000764 mp = (PyDictObject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000765 ep = (mp->ma_lookup)(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000766 if (ep == NULL)
767 return -1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000768 if (ep->me_value == NULL) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000769 set_key_error(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000770 return -1;
771 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000772 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000773 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000774 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000775 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000776 ep->me_value = NULL;
777 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000778 Py_DECREF(old_value);
779 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000780 return 0;
781}
782
Guido van Rossum25831651993-05-19 14:50:45 +0000783void
Tim Peters1f5871e2000-07-04 17:44:48 +0000784PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000785{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000786 PyDictObject *mp;
787 PyDictEntry *ep, *table;
Tim Petersdea48ec2001-05-22 20:40:22 +0000788 int table_is_malloced;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000789 Py_ssize_t fill;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000790 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000791#ifdef Py_DEBUG
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000792 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +0000793#endif
794
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000795 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000796 return;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000797 mp = (PyDictObject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000798#ifdef Py_DEBUG
Tim Petersafb6ae82001-06-04 21:00:21 +0000799 n = mp->ma_mask + 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000800 i = 0;
801#endif
802
803 table = mp->ma_table;
804 assert(table != NULL);
805 table_is_malloced = table != mp->ma_smalltable;
806
807 /* This is delicate. During the process of clearing the dict,
808 * decrefs can cause the dict to mutate. To avoid fatal confusion
809 * (voice of experience), we have to make the dict empty before
810 * clearing the slots, and never refer to anything via mp->xxx while
811 * clearing.
812 */
813 fill = mp->ma_fill;
814 if (table_is_malloced)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000815 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000816
817 else if (fill > 0) {
818 /* It's a small table with something that needs to be cleared.
819 * Afraid the only safe way is to copy the dict entries into
820 * another small table first.
821 */
822 memcpy(small_copy, table, sizeof(small_copy));
823 table = small_copy;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000824 EMPTY_TO_MINSIZE(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000825 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000826 /* else it's a small table that's already empty */
827
828 /* Now we can finally clear things. If C had refcounts, we could
829 * assert that the refcount on table is 1 now, i.e. that this function
830 * has unique access to it, so decref side-effects can't alter it.
831 */
832 for (ep = table; fill > 0; ++ep) {
833#ifdef Py_DEBUG
834 assert(i < n);
835 ++i;
836#endif
837 if (ep->me_key) {
838 --fill;
839 Py_DECREF(ep->me_key);
840 Py_XDECREF(ep->me_value);
841 }
842#ifdef Py_DEBUG
843 else
844 assert(ep->me_value == NULL);
845#endif
846 }
847
848 if (table_is_malloced)
849 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000850}
851
Tim Peters080c88b2003-02-15 03:01:11 +0000852/*
853 * Iterate over a dict. Use like so:
854 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000855 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +0000856 * PyObject *key, *value;
857 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +0000858 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +0000859 * Refer to borrowed references in key and value.
860 * }
861 *
862 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +0000863 * mutates the dict. One exception: it is safe if the loop merely changes
864 * the values associated with the keys (but doesn't insert new keys or
865 * delete keys), via PyDict_SetItem().
866 */
Guido van Rossum25831651993-05-19 14:50:45 +0000867int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000868PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000869{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000870 register Py_ssize_t i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000871 register Py_ssize_t mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000872 register PyDictEntry *ep;
Raymond Hettinger43442782004-03-17 21:55:03 +0000873
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000874 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000875 return 0;
Guido van Rossum25831651993-05-19 14:50:45 +0000876 i = *ppos;
877 if (i < 0)
878 return 0;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000879 ep = ((PyDictObject *)op)->ma_table;
880 mask = ((PyDictObject *)op)->ma_mask;
Raymond Hettinger43442782004-03-17 21:55:03 +0000881 while (i <= mask && ep[i].me_value == NULL)
Guido van Rossum25831651993-05-19 14:50:45 +0000882 i++;
883 *ppos = i+1;
Raymond Hettinger43442782004-03-17 21:55:03 +0000884 if (i > mask)
Guido van Rossum25831651993-05-19 14:50:45 +0000885 return 0;
886 if (pkey)
Raymond Hettinger43442782004-03-17 21:55:03 +0000887 *pkey = ep[i].me_key;
Guido van Rossum25831651993-05-19 14:50:45 +0000888 if (pvalue)
Raymond Hettinger43442782004-03-17 21:55:03 +0000889 *pvalue = ep[i].me_value;
Guido van Rossum25831651993-05-19 14:50:45 +0000890 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000891}
892
Thomas Wouterscf297e42007-02-23 15:07:44 +0000893/* Internal version of PyDict_Next that returns a hash value in addition to the key and value.*/
894int
895_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, long *phash)
896{
897 register Py_ssize_t i;
898 register Py_ssize_t mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000899 register PyDictEntry *ep;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000900
901 if (!PyDict_Check(op))
902 return 0;
903 i = *ppos;
904 if (i < 0)
905 return 0;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000906 ep = ((PyDictObject *)op)->ma_table;
907 mask = ((PyDictObject *)op)->ma_mask;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000908 while (i <= mask && ep[i].me_value == NULL)
909 i++;
910 *ppos = i+1;
911 if (i > mask)
912 return 0;
913 *phash = (long)(ep[i].me_hash);
914 if (pkey)
915 *pkey = ep[i].me_key;
916 if (pvalue)
917 *pvalue = ep[i].me_value;
918 return 1;
919}
920
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000921/* Methods */
922
923static void
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000924dict_dealloc(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000925{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000926 register PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000927 Py_ssize_t fill = mp->ma_fill;
Guido van Rossumff413af2002-03-28 20:34:59 +0000928 PyObject_GC_UnTrack(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000929 Py_TRASHCAN_SAFE_BEGIN(mp)
Tim Petersdea48ec2001-05-22 20:40:22 +0000930 for (ep = mp->ma_table; fill > 0; ep++) {
931 if (ep->me_key) {
932 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000933 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000934 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000935 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000936 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000937 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000938 PyMem_DEL(mp->ma_table);
Christian Heimes2202f872008-02-06 14:31:34 +0000939 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
940 free_list[numfree++] = mp;
Tim Peters60b29962006-01-01 01:19:23 +0000941 else
Christian Heimes90aa7642007-12-19 02:45:37 +0000942 Py_TYPE(mp)->tp_free((PyObject *)mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000943 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000944}
945
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000946static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000947dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000948{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000949 Py_ssize_t i;
Tim Petersa7259592001-06-16 05:11:17 +0000950 PyObject *s, *temp, *colon = NULL;
951 PyObject *pieces = NULL, *result = NULL;
952 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +0000953
Tim Petersa7259592001-06-16 05:11:17 +0000954 i = Py_ReprEnter((PyObject *)mp);
Guido van Rossum255443b1998-04-10 22:47:14 +0000955 if (i != 0) {
Walter Dörwald1ab83302007-05-18 17:15:44 +0000956 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
Guido van Rossum255443b1998-04-10 22:47:14 +0000957 }
958
Tim Petersa7259592001-06-16 05:11:17 +0000959 if (mp->ma_used == 0) {
Walter Dörwald1ab83302007-05-18 17:15:44 +0000960 result = PyUnicode_FromString("{}");
Tim Petersa7259592001-06-16 05:11:17 +0000961 goto Done;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000962 }
Tim Petersa7259592001-06-16 05:11:17 +0000963
964 pieces = PyList_New(0);
965 if (pieces == NULL)
966 goto Done;
967
Walter Dörwald1ab83302007-05-18 17:15:44 +0000968 colon = PyUnicode_FromString(": ");
Tim Petersa7259592001-06-16 05:11:17 +0000969 if (colon == NULL)
970 goto Done;
971
972 /* Do repr() on each key+value pair, and insert ": " between them.
973 Note that repr may mutate the dict. */
Tim Petersc6057842001-06-16 07:52:53 +0000974 i = 0;
975 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Tim Petersa7259592001-06-16 05:11:17 +0000976 int status;
977 /* Prevent repr from deleting value during key format. */
978 Py_INCREF(value);
979 s = PyObject_Repr(key);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000980 PyUnicode_Append(&s, colon);
981 PyUnicode_AppendAndDel(&s, PyObject_Repr(value));
Tim Petersa7259592001-06-16 05:11:17 +0000982 Py_DECREF(value);
983 if (s == NULL)
984 goto Done;
985 status = PyList_Append(pieces, s);
986 Py_DECREF(s); /* append created a new ref */
987 if (status < 0)
988 goto Done;
989 }
990
991 /* Add "{}" decorations to the first and last items. */
992 assert(PyList_GET_SIZE(pieces) > 0);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000993 s = PyUnicode_FromString("{");
Tim Petersa7259592001-06-16 05:11:17 +0000994 if (s == NULL)
995 goto Done;
996 temp = PyList_GET_ITEM(pieces, 0);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000997 PyUnicode_AppendAndDel(&s, temp);
Tim Petersa7259592001-06-16 05:11:17 +0000998 PyList_SET_ITEM(pieces, 0, s);
999 if (s == NULL)
1000 goto Done;
1001
Walter Dörwald1ab83302007-05-18 17:15:44 +00001002 s = PyUnicode_FromString("}");
Tim Petersa7259592001-06-16 05:11:17 +00001003 if (s == NULL)
1004 goto Done;
1005 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
Walter Dörwald1ab83302007-05-18 17:15:44 +00001006 PyUnicode_AppendAndDel(&temp, s);
Tim Petersa7259592001-06-16 05:11:17 +00001007 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
1008 if (temp == NULL)
1009 goto Done;
1010
1011 /* Paste them all together with ", " between. */
Walter Dörwald1ab83302007-05-18 17:15:44 +00001012 s = PyUnicode_FromString(", ");
Tim Petersa7259592001-06-16 05:11:17 +00001013 if (s == NULL)
1014 goto Done;
Walter Dörwald1ab83302007-05-18 17:15:44 +00001015 result = PyUnicode_Join(s, pieces);
Tim Peters0ab085c2001-09-14 00:25:33 +00001016 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +00001017
1018Done:
1019 Py_XDECREF(pieces);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001020 Py_XDECREF(colon);
Tim Petersa7259592001-06-16 05:11:17 +00001021 Py_ReprLeave((PyObject *)mp);
1022 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001023}
1024
Martin v. Löwis18e16552006-02-15 17:27:45 +00001025static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001026dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001027{
1028 return mp->ma_used;
1029}
1030
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001031static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001032dict_subscript(PyDictObject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001033{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001034 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001035 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001036 PyDictEntry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +00001037 assert(mp->ma_table != NULL);
Guido van Rossum89d8c602007-09-18 17:26:56 +00001038 if (!PyUnicode_CheckExact(key) ||
1039 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001040 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001041 if (hash == -1)
1042 return NULL;
1043 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001044 ep = (mp->ma_lookup)(mp, key, hash);
1045 if (ep == NULL)
1046 return NULL;
1047 v = ep->me_value;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001048 if (v == NULL) {
1049 if (!PyDict_CheckExact(mp)) {
1050 /* Look up __missing__ method if we're a subclass. */
Guido van Rossum4b92a822006-02-25 23:32:30 +00001051 PyObject *missing;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001052 static PyObject *missing_str = NULL;
1053 if (missing_str == NULL)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001054 missing_str =
Martin v. Löwis5b222132007-06-10 09:51:05 +00001055 PyUnicode_InternFromString("__missing__");
Christian Heimes90aa7642007-12-19 02:45:37 +00001056 missing = _PyType_Lookup(Py_TYPE(mp), missing_str);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001057 if (missing != NULL)
1058 return PyObject_CallFunctionObjArgs(missing,
1059 (PyObject *)mp, key, NULL);
1060 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001061 set_key_error(key);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001062 return NULL;
1063 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001064 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001065 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001066 return v;
1067}
1068
1069static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001070dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001071{
1072 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001073 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001074 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001075 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001076}
1077
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001078static PyMappingMethods dict_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001079 (lenfunc)dict_length, /*mp_length*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001080 (binaryfunc)dict_subscript, /*mp_subscript*/
1081 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001082};
1083
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001084static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001085dict_keys(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001086{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001087 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001088 register Py_ssize_t i, j;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001089 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001090 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001091
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001092 again:
1093 n = mp->ma_used;
1094 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001095 if (v == NULL)
1096 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001097 if (n != mp->ma_used) {
1098 /* Durnit. The allocations caused the dict to resize.
1099 * Just start over, this shouldn't normally happen.
1100 */
1101 Py_DECREF(v);
1102 goto again;
1103 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001104 ep = mp->ma_table;
1105 mask = mp->ma_mask;
1106 for (i = 0, j = 0; i <= mask; i++) {
1107 if (ep[i].me_value != NULL) {
1108 PyObject *key = ep[i].me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001109 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001110 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001111 j++;
1112 }
1113 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001114 assert(j == n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001115 return v;
1116}
1117
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001118static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001119dict_values(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001120{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001121 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001122 register Py_ssize_t i, j;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001123 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001124 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001125
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001126 again:
1127 n = mp->ma_used;
1128 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001129 if (v == NULL)
1130 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001131 if (n != mp->ma_used) {
1132 /* Durnit. The allocations caused the dict to resize.
1133 * Just start over, this shouldn't normally happen.
1134 */
1135 Py_DECREF(v);
1136 goto again;
1137 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001138 ep = mp->ma_table;
1139 mask = mp->ma_mask;
1140 for (i = 0, j = 0; i <= mask; i++) {
1141 if (ep[i].me_value != NULL) {
1142 PyObject *value = ep[i].me_value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001143 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001144 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001145 j++;
1146 }
1147 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001148 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001149 return v;
1150}
1151
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001152static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001153dict_items(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001154{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001155 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001156 register Py_ssize_t i, j, n;
1157 Py_ssize_t mask;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001158 PyObject *item, *key, *value;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001159 PyDictEntry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001160
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001161 /* Preallocate the list of tuples, to avoid allocations during
1162 * the loop over the items, which could trigger GC, which
1163 * could resize the dict. :-(
1164 */
1165 again:
1166 n = mp->ma_used;
1167 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001168 if (v == NULL)
1169 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001170 for (i = 0; i < n; i++) {
1171 item = PyTuple_New(2);
1172 if (item == NULL) {
1173 Py_DECREF(v);
1174 return NULL;
1175 }
1176 PyList_SET_ITEM(v, i, item);
1177 }
1178 if (n != mp->ma_used) {
1179 /* Durnit. The allocations caused the dict to resize.
1180 * Just start over, this shouldn't normally happen.
1181 */
1182 Py_DECREF(v);
1183 goto again;
1184 }
1185 /* Nothing we do below makes any function calls. */
Raymond Hettinger43442782004-03-17 21:55:03 +00001186 ep = mp->ma_table;
1187 mask = mp->ma_mask;
1188 for (i = 0, j = 0; i <= mask; i++) {
Raymond Hettinger06905122004-03-19 10:30:00 +00001189 if ((value=ep[i].me_value) != NULL) {
Raymond Hettinger43442782004-03-17 21:55:03 +00001190 key = ep[i].me_key;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001191 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001192 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001193 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001194 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001195 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001196 j++;
1197 }
1198 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001199 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001200 return v;
1201}
1202
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001203static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001204dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001205{
1206 PyObject *seq;
1207 PyObject *value = Py_None;
1208 PyObject *it; /* iter(seq) */
1209 PyObject *key;
1210 PyObject *d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001211 int status;
1212
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001213 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001214 return NULL;
1215
1216 d = PyObject_CallObject(cls, NULL);
1217 if (d == NULL)
1218 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001219
Guido van Rossum58da9312007-11-10 23:39:45 +00001220 if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) {
1221 PyDictObject *mp = (PyDictObject *)d;
1222 PyObject *oldvalue;
1223 Py_ssize_t pos = 0;
1224 PyObject *key;
1225 long hash;
1226
1227 if (dictresize(mp, PySet_GET_SIZE(seq)))
1228 return NULL;
1229
1230 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
1231 Py_INCREF(key);
1232 Py_INCREF(value);
1233 if (insertdict(mp, key, hash, value))
1234 return NULL;
1235 }
1236 return d;
1237 }
1238
Guido van Rossumd8faa362007-04-27 19:54:29 +00001239 if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001240 PyDictObject *mp = (PyDictObject *)d;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001241 Py_ssize_t pos = 0;
1242 PyObject *key;
1243 long hash;
1244
1245 if (dictresize(mp, PySet_GET_SIZE(seq)))
1246 return NULL;
1247
1248 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
1249 Py_INCREF(key);
1250 Py_INCREF(value);
1251 if (insertdict(mp, key, hash, value))
1252 return NULL;
1253 }
1254 return d;
1255 }
1256
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001257 it = PyObject_GetIter(seq);
1258 if (it == NULL){
1259 Py_DECREF(d);
1260 return NULL;
1261 }
1262
Guido van Rossum58da9312007-11-10 23:39:45 +00001263 if (PyDict_CheckExact(d)) {
1264 while ((key = PyIter_Next(it)) != NULL) {
1265 status = PyDict_SetItem(d, key, value);
1266 Py_DECREF(key);
1267 if (status < 0)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001268 goto Fail;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001269 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001270 } else {
1271 while ((key = PyIter_Next(it)) != NULL) {
1272 status = PyObject_SetItem(d, key, value);
1273 Py_DECREF(key);
1274 if (status < 0)
1275 goto Fail;
1276 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001277 }
1278
Guido van Rossum58da9312007-11-10 23:39:45 +00001279 if (PyErr_Occurred())
1280 goto Fail;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001281 Py_DECREF(it);
1282 return d;
1283
1284Fail:
1285 Py_DECREF(it);
1286 Py_DECREF(d);
1287 return NULL;
1288}
1289
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001290static int
1291dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001292{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001293 PyObject *arg = NULL;
1294 int result = 0;
1295
1296 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1297 result = -1;
1298
1299 else if (arg != NULL) {
1300 if (PyObject_HasAttrString(arg, "keys"))
1301 result = PyDict_Merge(self, arg, 1);
1302 else
1303 result = PyDict_MergeFromSeq2(self, arg, 1);
1304 }
1305 if (result == 0 && kwds != NULL)
1306 result = PyDict_Merge(self, kwds, 1);
1307 return result;
1308}
1309
1310static PyObject *
1311dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1312{
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001313 if (dict_update_common(self, args, kwds, "update") != -1)
1314 Py_RETURN_NONE;
1315 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001316}
1317
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001318/* Update unconditionally replaces existing items.
1319 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001320 otherwise it leaves existing items unchanged.
1321
1322 PyDict_{Update,Merge} update/merge from a mapping object.
1323
Tim Petersf582b822001-12-11 18:51:08 +00001324 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001325 producing iterable objects of length 2.
1326*/
1327
Tim Petersf582b822001-12-11 18:51:08 +00001328int
Tim Peters1fc240e2001-10-26 05:06:50 +00001329PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1330{
1331 PyObject *it; /* iter(seq2) */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001332 Py_ssize_t i; /* index into seq2 of current element */
Tim Peters1fc240e2001-10-26 05:06:50 +00001333 PyObject *item; /* seq2[i] */
1334 PyObject *fast; /* item as a 2-tuple or 2-list */
1335
1336 assert(d != NULL);
1337 assert(PyDict_Check(d));
1338 assert(seq2 != NULL);
1339
1340 it = PyObject_GetIter(seq2);
1341 if (it == NULL)
1342 return -1;
1343
1344 for (i = 0; ; ++i) {
1345 PyObject *key, *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001346 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001347
1348 fast = NULL;
1349 item = PyIter_Next(it);
1350 if (item == NULL) {
1351 if (PyErr_Occurred())
1352 goto Fail;
1353 break;
1354 }
1355
1356 /* Convert item to sequence, and verify length 2. */
1357 fast = PySequence_Fast(item, "");
1358 if (fast == NULL) {
1359 if (PyErr_ExceptionMatches(PyExc_TypeError))
1360 PyErr_Format(PyExc_TypeError,
1361 "cannot convert dictionary update "
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001362 "sequence element #%zd to a sequence",
Tim Peters1fc240e2001-10-26 05:06:50 +00001363 i);
1364 goto Fail;
1365 }
1366 n = PySequence_Fast_GET_SIZE(fast);
1367 if (n != 2) {
1368 PyErr_Format(PyExc_ValueError,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001369 "dictionary update sequence element #%zd "
Martin v. Löwis2c95cc62006-02-16 06:54:25 +00001370 "has length %zd; 2 is required",
Martin v. Löwise0e89f72006-02-16 06:59:22 +00001371 i, n);
Tim Peters1fc240e2001-10-26 05:06:50 +00001372 goto Fail;
1373 }
1374
1375 /* Update/merge with this (key, value) pair. */
1376 key = PySequence_Fast_GET_ITEM(fast, 0);
1377 value = PySequence_Fast_GET_ITEM(fast, 1);
1378 if (override || PyDict_GetItem(d, key) == NULL) {
1379 int status = PyDict_SetItem(d, key, value);
1380 if (status < 0)
1381 goto Fail;
1382 }
1383 Py_DECREF(fast);
1384 Py_DECREF(item);
1385 }
1386
1387 i = 0;
1388 goto Return;
1389Fail:
1390 Py_XDECREF(item);
1391 Py_XDECREF(fast);
1392 i = -1;
1393Return:
1394 Py_DECREF(it);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001395 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001396}
1397
Tim Peters6d6c1a32001-08-02 04:15:00 +00001398int
1399PyDict_Update(PyObject *a, PyObject *b)
1400{
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001401 return PyDict_Merge(a, b, 1);
1402}
1403
1404int
1405PyDict_Merge(PyObject *a, PyObject *b, int override)
1406{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001407 register PyDictObject *mp, *other;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001408 register Py_ssize_t i;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001409 PyDictEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001410
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001411 /* We accept for the argument either a concrete dictionary object,
1412 * or an abstract "mapping" object. For the former, we can do
1413 * things quite efficiently. For the latter, we only require that
1414 * PyMapping_Keys() and PyObject_GetItem() be supported.
1415 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001416 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1417 PyErr_BadInternalCall();
1418 return -1;
1419 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001420 mp = (PyDictObject*)a;
Christian Heimesaf98da12008-01-27 15:18:18 +00001421 if (PyDict_Check(b)) {
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001422 other = (PyDictObject*)b;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001423 if (other == mp || other->ma_used == 0)
1424 /* a.update(a) or a.update({}); nothing to do */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001425 return 0;
Raymond Hettinger186e7392005-05-14 18:08:25 +00001426 if (mp->ma_used == 0)
1427 /* Since the target dict is empty, PyDict_GetItem()
1428 * always returns NULL. Setting override to 1
1429 * skips the unnecessary test.
1430 */
1431 override = 1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001432 /* Do one big resize at the start, rather than
1433 * incrementally resizing as we insert new items. Expect
1434 * that there will be no (or few) overlapping keys.
1435 */
1436 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
Raymond Hettingerc8d22902003-05-07 00:49:40 +00001437 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001438 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001439 }
1440 for (i = 0; i <= other->ma_mask; i++) {
1441 entry = &other->ma_table[i];
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001442 if (entry->me_value != NULL &&
1443 (override ||
1444 PyDict_GetItem(a, entry->me_key) == NULL)) {
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001445 Py_INCREF(entry->me_key);
1446 Py_INCREF(entry->me_value);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001447 if (insertdict(mp, entry->me_key,
1448 (long)entry->me_hash,
1449 entry->me_value) != 0)
1450 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001451 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001452 }
1453 }
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001454 else {
1455 /* Do it the generic, slower way */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001456 PyObject *keys = PyMapping_Keys(b);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001457 PyObject *iter;
1458 PyObject *key, *value;
1459 int status;
1460
1461 if (keys == NULL)
1462 /* Docstring says this is equivalent to E.keys() so
1463 * if E doesn't have a .keys() method we want
1464 * AttributeError to percolate up. Might as well
1465 * do the same for any other error.
1466 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001467 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001468
1469 iter = PyObject_GetIter(keys);
1470 Py_DECREF(keys);
1471 if (iter == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001472 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001473
1474 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001475 if (!override && PyDict_GetItem(a, key) != NULL) {
1476 Py_DECREF(key);
1477 continue;
1478 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001479 value = PyObject_GetItem(b, key);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001480 if (value == NULL) {
1481 Py_DECREF(iter);
1482 Py_DECREF(key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001483 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001484 }
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001485 status = PyDict_SetItem(a, key, value);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001486 Py_DECREF(key);
1487 Py_DECREF(value);
1488 if (status < 0) {
1489 Py_DECREF(iter);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001490 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001491 }
1492 }
1493 Py_DECREF(iter);
1494 if (PyErr_Occurred())
1495 /* Iterator completed, via error */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001496 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001497 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001498 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001499}
1500
1501static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001502dict_copy(register PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001503{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001504 return PyDict_Copy((PyObject*)mp);
1505}
1506
1507PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001508PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001509{
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001510 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001511
1512 if (o == NULL || !PyDict_Check(o)) {
1513 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001514 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001515 }
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001516 copy = PyDict_New();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001517 if (copy == NULL)
1518 return NULL;
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001519 if (PyDict_Merge(copy, o, 1) == 0)
1520 return copy;
1521 Py_DECREF(copy);
Raymond Hettinger1356f782005-04-15 15:58:42 +00001522 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001523}
1524
Martin v. Löwis18e16552006-02-15 17:27:45 +00001525Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001526PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001527{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001528 if (mp == NULL || !PyDict_Check(mp)) {
1529 PyErr_BadInternalCall();
Jeremy Hylton7083bb72004-02-17 20:10:11 +00001530 return -1;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001531 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001532 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001533}
1534
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001535PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001536PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001537{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001538 if (mp == NULL || !PyDict_Check(mp)) {
1539 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001540 return NULL;
1541 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001542 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001543}
1544
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001545PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001546PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001547{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001548 if (mp == NULL || !PyDict_Check(mp)) {
1549 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001550 return NULL;
1551 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001552 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001553}
1554
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001555PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001556PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001557{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001558 if (mp == NULL || !PyDict_Check(mp)) {
1559 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001560 return NULL;
1561 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001562 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001563}
1564
Tim Peterse63415e2001-05-08 04:38:29 +00001565/* Return 1 if dicts equal, 0 if not, -1 if error.
1566 * Gets out as soon as any difference is detected.
1567 * Uses only Py_EQ comparison.
1568 */
1569static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001570dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00001571{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001572 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00001573
1574 if (a->ma_used != b->ma_used)
1575 /* can't be equal if # of entries differ */
1576 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001577
Tim Peterse63415e2001-05-08 04:38:29 +00001578 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Tim Petersafb6ae82001-06-04 21:00:21 +00001579 for (i = 0; i <= a->ma_mask; i++) {
Tim Peterse63415e2001-05-08 04:38:29 +00001580 PyObject *aval = a->ma_table[i].me_value;
1581 if (aval != NULL) {
1582 int cmp;
1583 PyObject *bval;
1584 PyObject *key = a->ma_table[i].me_key;
1585 /* temporarily bump aval's refcount to ensure it stays
1586 alive until we're done with it */
1587 Py_INCREF(aval);
Guido van Rossumdc5f6b22006-08-24 21:29:26 +00001588 /* ditto for key */
1589 Py_INCREF(key);
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001590 bval = PyDict_GetItemWithError((PyObject *)b, key);
Guido van Rossumdc5f6b22006-08-24 21:29:26 +00001591 Py_DECREF(key);
Tim Peterse63415e2001-05-08 04:38:29 +00001592 if (bval == NULL) {
1593 Py_DECREF(aval);
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001594 if (PyErr_Occurred())
1595 return -1;
Tim Peterse63415e2001-05-08 04:38:29 +00001596 return 0;
1597 }
1598 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1599 Py_DECREF(aval);
1600 if (cmp <= 0) /* error or not equal */
1601 return cmp;
1602 }
1603 }
1604 return 1;
1605 }
1606
1607static PyObject *
1608dict_richcompare(PyObject *v, PyObject *w, int op)
1609{
1610 int cmp;
1611 PyObject *res;
1612
1613 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1614 res = Py_NotImplemented;
1615 }
1616 else if (op == Py_EQ || op == Py_NE) {
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001617 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
Tim Peterse63415e2001-05-08 04:38:29 +00001618 if (cmp < 0)
1619 return NULL;
1620 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1621 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001622 else
1623 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001624 Py_INCREF(res);
1625 return res;
1626 }
1627
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001628static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001629dict_contains(register PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001630{
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001631 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001632 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001633
Guido van Rossum89d8c602007-09-18 17:26:56 +00001634 if (!PyUnicode_CheckExact(key) ||
1635 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001636 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001637 if (hash == -1)
1638 return NULL;
1639 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001640 ep = (mp->ma_lookup)(mp, key, hash);
1641 if (ep == NULL)
1642 return NULL;
1643 return PyBool_FromLong(ep->me_value != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001644}
1645
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001646static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001647dict_get(register PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001648{
1649 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001650 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001651 PyObject *val = NULL;
1652 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001653 PyDictEntry *ep;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001654
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001655 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001656 return NULL;
1657
Guido van Rossum89d8c602007-09-18 17:26:56 +00001658 if (!PyUnicode_CheckExact(key) ||
1659 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Barry Warsawc38c5da1997-10-06 17:49:20 +00001660 hash = PyObject_Hash(key);
1661 if (hash == -1)
1662 return NULL;
1663 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001664 ep = (mp->ma_lookup)(mp, key, hash);
1665 if (ep == NULL)
1666 return NULL;
1667 val = ep->me_value;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001668 if (val == NULL)
1669 val = failobj;
1670 Py_INCREF(val);
1671 return val;
1672}
1673
1674
1675static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001676dict_setdefault(register PyDictObject *mp, PyObject *args)
Guido van Rossum164452c2000-08-08 16:12:54 +00001677{
1678 PyObject *key;
1679 PyObject *failobj = Py_None;
1680 PyObject *val = NULL;
1681 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001682 PyDictEntry *ep;
Guido van Rossum164452c2000-08-08 16:12:54 +00001683
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001684 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
Guido van Rossum164452c2000-08-08 16:12:54 +00001685 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001686
Guido van Rossum89d8c602007-09-18 17:26:56 +00001687 if (!PyUnicode_CheckExact(key) ||
1688 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossum164452c2000-08-08 16:12:54 +00001689 hash = PyObject_Hash(key);
1690 if (hash == -1)
1691 return NULL;
1692 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001693 ep = (mp->ma_lookup)(mp, key, hash);
1694 if (ep == NULL)
1695 return NULL;
1696 val = ep->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001697 if (val == NULL) {
1698 val = failobj;
1699 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1700 val = NULL;
1701 }
1702 Py_XINCREF(val);
1703 return val;
1704}
1705
1706
1707static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001708dict_clear(register PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001709{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001710 PyDict_Clear((PyObject *)mp);
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001711 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001712}
1713
Guido van Rossumba6ab842000-12-12 22:02:18 +00001714static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001715dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00001716{
1717 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001718 PyDictEntry *ep;
Guido van Rossume027d982002-04-12 15:11:59 +00001719 PyObject *old_value, *old_key;
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001720 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001721
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001722 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1723 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001724 if (mp->ma_used == 0) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001725 if (deflt) {
1726 Py_INCREF(deflt);
1727 return deflt;
1728 }
Guido van Rossume027d982002-04-12 15:11:59 +00001729 PyErr_SetString(PyExc_KeyError,
1730 "pop(): dictionary is empty");
1731 return NULL;
1732 }
Guido van Rossum89d8c602007-09-18 17:26:56 +00001733 if (!PyUnicode_CheckExact(key) ||
1734 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossume027d982002-04-12 15:11:59 +00001735 hash = PyObject_Hash(key);
1736 if (hash == -1)
1737 return NULL;
1738 }
1739 ep = (mp->ma_lookup)(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001740 if (ep == NULL)
1741 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001742 if (ep->me_value == NULL) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001743 if (deflt) {
1744 Py_INCREF(deflt);
1745 return deflt;
1746 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001747 set_key_error(key);
Guido van Rossume027d982002-04-12 15:11:59 +00001748 return NULL;
1749 }
1750 old_key = ep->me_key;
1751 Py_INCREF(dummy);
1752 ep->me_key = dummy;
1753 old_value = ep->me_value;
1754 ep->me_value = NULL;
1755 mp->ma_used--;
1756 Py_DECREF(old_key);
1757 return old_value;
1758}
1759
1760static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001761dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001762{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001763 Py_ssize_t i = 0;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001764 PyDictEntry *ep;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001765 PyObject *res;
1766
Tim Petersf4b33f62001-06-02 05:42:29 +00001767 /* Allocate the result tuple before checking the size. Believe it
1768 * or not, this allocation could trigger a garbage collection which
1769 * could empty the dict, so if we checked the size first and that
1770 * happened, the result would be an infinite loop (searching for an
1771 * entry that no longer exists). Note that the usual popitem()
1772 * idiom is "while d: k, v = d.popitem()". so needing to throw the
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001773 * tuple away if the dict *is* empty isn't a significant
Tim Petersf4b33f62001-06-02 05:42:29 +00001774 * inefficiency -- possible, but unlikely in practice.
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001775 */
1776 res = PyTuple_New(2);
1777 if (res == NULL)
1778 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001779 if (mp->ma_used == 0) {
1780 Py_DECREF(res);
1781 PyErr_SetString(PyExc_KeyError,
1782 "popitem(): dictionary is empty");
1783 return NULL;
1784 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001785 /* Set ep to "the first" dict entry with a value. We abuse the hash
1786 * field of slot 0 to hold a search finger:
1787 * If slot 0 has a value, use slot 0.
1788 * Else slot 0 is being used to hold a search finger,
1789 * and we use its hash value as the first index to look.
1790 */
1791 ep = &mp->ma_table[0];
1792 if (ep->me_value == NULL) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001793 i = ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001794 /* The hash field may be a real hash value, or it may be a
1795 * legit search finger, or it may be a once-legit search
1796 * finger that's out of bounds now because it wrapped around
1797 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001798 */
Tim Petersafb6ae82001-06-04 21:00:21 +00001799 if (i > mp->ma_mask || i < 1)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001800 i = 1; /* skip slot 0 */
1801 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1802 i++;
Tim Petersafb6ae82001-06-04 21:00:21 +00001803 if (i > mp->ma_mask)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001804 i = 1;
1805 }
1806 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001807 PyTuple_SET_ITEM(res, 0, ep->me_key);
1808 PyTuple_SET_ITEM(res, 1, ep->me_value);
1809 Py_INCREF(dummy);
1810 ep->me_key = dummy;
1811 ep->me_value = NULL;
1812 mp->ma_used--;
1813 assert(mp->ma_table[0].me_value == NULL);
1814 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001815 return res;
1816}
1817
Jeremy Hylton8caad492000-06-23 14:18:11 +00001818static int
1819dict_traverse(PyObject *op, visitproc visit, void *arg)
1820{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001821 Py_ssize_t i = 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001822 PyObject *pk;
1823 PyObject *pv;
1824
1825 while (PyDict_Next(op, &i, &pk, &pv)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001826 Py_VISIT(pk);
1827 Py_VISIT(pv);
Jeremy Hylton8caad492000-06-23 14:18:11 +00001828 }
1829 return 0;
1830}
1831
1832static int
1833dict_tp_clear(PyObject *op)
1834{
1835 PyDict_Clear(op);
1836 return 0;
1837}
1838
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001839static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001840
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001841PyDoc_STRVAR(contains__doc__,
1842"D.__contains__(k) -> True if D has a key k, else False");
1843
1844PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
1845
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001846PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001847"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001848
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001849PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001850"D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D");
Tim Petersf7f88b12000-12-13 23:18:45 +00001851
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001852PyDoc_STRVAR(pop__doc__,
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001853"D.pop(k[,d]) -> v, remove specified key and return the corresponding value\n\
1854If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00001855
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001856PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00001857"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000018582-tuple; but raise KeyError if D is empty");
Tim Petersf7f88b12000-12-13 23:18:45 +00001859
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001860PyDoc_STRVAR(update__doc__,
Guido van Rossumb90c8482007-02-10 01:11:45 +00001861"D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k]\
1862\n(if E has keys else: for (k, v) in E: D[k] = v) then: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00001863
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001864PyDoc_STRVAR(fromkeys__doc__,
1865"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
1866v defaults to None.");
1867
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001868PyDoc_STRVAR(clear__doc__,
1869"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001870
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001871PyDoc_STRVAR(copy__doc__,
1872"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00001873
Guido van Rossumb90c8482007-02-10 01:11:45 +00001874/* Forward */
1875static PyObject *dictkeys_new(PyObject *);
1876static PyObject *dictitems_new(PyObject *);
1877static PyObject *dictvalues_new(PyObject *);
1878
Guido van Rossum45c85d12007-07-27 16:31:40 +00001879PyDoc_STRVAR(keys__doc__,
1880 "D.keys() -> a set-like object providing a view on D's keys");
1881PyDoc_STRVAR(items__doc__,
1882 "D.items() -> a set-like object providing a view on D's items");
1883PyDoc_STRVAR(values__doc__,
1884 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00001885
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001886static PyMethodDef mapp_methods[] = {
Guido van Rossume2b70bc2006-08-18 22:13:04 +00001887 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001888 contains__doc__},
Raymond Hettinger0c669672003-12-13 13:31:55 +00001889 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001890 getitem__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00001891 {"get", (PyCFunction)dict_get, METH_VARARGS,
1892 get__doc__},
1893 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1894 setdefault_doc__},
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001895 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
Just van Rossuma797d812002-11-23 09:45:04 +00001896 pop__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001897 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001898 popitem__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001899 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00001900 keys__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001901 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00001902 items__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001903 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00001904 values__doc__},
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001905 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001906 update__doc__},
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001907 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
1908 fromkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001909 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001910 clear__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001911 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001912 copy__doc__},
1913 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001914};
1915
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001916/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001917int
1918PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001919{
1920 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001921 PyDictObject *mp = (PyDictObject *)op;
1922 PyDictEntry *ep;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001923
Guido van Rossum89d8c602007-09-18 17:26:56 +00001924 if (!PyUnicode_CheckExact(key) ||
1925 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001926 hash = PyObject_Hash(key);
1927 if (hash == -1)
1928 return -1;
1929 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001930 ep = (mp->ma_lookup)(mp, key, hash);
1931 return ep == NULL ? -1 : (ep->me_value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001932}
1933
Thomas Wouterscf297e42007-02-23 15:07:44 +00001934/* Internal version of PyDict_Contains used when the hash value is already known */
1935int
1936_PyDict_Contains(PyObject *op, PyObject *key, long hash)
1937{
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001938 PyDictObject *mp = (PyDictObject *)op;
1939 PyDictEntry *ep;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001940
1941 ep = (mp->ma_lookup)(mp, key, hash);
1942 return ep == NULL ? -1 : (ep->me_value != NULL);
1943}
1944
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001945/* Hack to implement "key in dict" */
1946static PySequenceMethods dict_as_sequence = {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001947 0, /* sq_length */
1948 0, /* sq_concat */
1949 0, /* sq_repeat */
1950 0, /* sq_item */
1951 0, /* sq_slice */
1952 0, /* sq_ass_item */
1953 0, /* sq_ass_slice */
1954 PyDict_Contains, /* sq_contains */
1955 0, /* sq_inplace_concat */
1956 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001957};
1958
Guido van Rossum09e563a2001-05-01 12:10:21 +00001959static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001960dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1961{
1962 PyObject *self;
1963
1964 assert(type != NULL && type->tp_alloc != NULL);
1965 self = type->tp_alloc(type, 0);
1966 if (self != NULL) {
1967 PyDictObject *d = (PyDictObject *)self;
1968 /* It's guaranteed that tp->alloc zeroed out the struct. */
1969 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
1970 INIT_NONZERO_DICT_SLOTS(d);
Guido van Rossum89d8c602007-09-18 17:26:56 +00001971 d->ma_lookup = lookdict_unicode;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001972#ifdef SHOW_CONVERSION_COUNTS
1973 ++created;
1974#endif
1975 }
1976 return self;
1977}
1978
Tim Peters25786c02001-09-02 08:22:48 +00001979static int
1980dict_init(PyObject *self, PyObject *args, PyObject *kwds)
1981{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001982 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00001983}
1984
Tim Peters6d6c1a32001-08-02 04:15:00 +00001985static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001986dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001987{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001988 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001989}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001990
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001991PyDoc_STRVAR(dictionary_doc,
Tim Petersa427a2b2001-10-29 22:25:45 +00001992"dict() -> new empty dictionary.\n"
1993"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Tim Peters1fc240e2001-10-26 05:06:50 +00001994" (key, value) pairs.\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00001995"dict(seq) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00001996" d = {}\n"
1997" for k, v in seq:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00001998" d[k] = v\n"
1999"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2000" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002001
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002002PyTypeObject PyDict_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002003 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Petersa427a2b2001-10-29 22:25:45 +00002004 "dict",
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002005 sizeof(PyDictObject),
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002006 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00002007 (destructor)dict_dealloc, /* tp_dealloc */
Guido van Rossum04dbf3b2007-08-07 19:51:00 +00002008 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002009 0, /* tp_getattr */
Guido van Rossumb9324202001-01-18 00:39:02 +00002010 0, /* tp_setattr */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00002011 0, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00002012 (reprfunc)dict_repr, /* tp_repr */
2013 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002014 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00002015 &dict_as_mapping, /* tp_as_mapping */
Guido van Rossum50e9fb92006-08-17 05:42:55 +00002016 0, /* tp_hash */
Guido van Rossumb9324202001-01-18 00:39:02 +00002017 0, /* tp_call */
2018 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002019 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumb9324202001-01-18 00:39:02 +00002020 0, /* tp_setattro */
2021 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002022 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Thomas Wouters27d517b2007-02-25 20:39:11 +00002023 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
Tim Peters25786c02001-09-02 08:22:48 +00002024 dictionary_doc, /* tp_doc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002025 dict_traverse, /* tp_traverse */
2026 dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00002027 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002028 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00002029 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002030 0, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002031 mapp_methods, /* tp_methods */
2032 0, /* tp_members */
2033 0, /* tp_getset */
2034 0, /* tp_base */
2035 0, /* tp_dict */
2036 0, /* tp_descr_get */
2037 0, /* tp_descr_set */
2038 0, /* tp_dictoffset */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002039 dict_init, /* tp_init */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002040 PyType_GenericAlloc, /* tp_alloc */
2041 dict_new, /* tp_new */
Neil Schemenauer6189b892002-04-12 02:43:00 +00002042 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002043};
2044
Guido van Rossum3cca2451997-05-16 14:23:33 +00002045/* For backward compatibility with old dictionary interface */
2046
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002047PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002048PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002049{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002050 PyObject *kv, *rv;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002051 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002052 if (kv == NULL)
2053 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002054 rv = PyDict_GetItem(v, kv);
2055 Py_DECREF(kv);
2056 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002057}
2058
2059int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002060PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002061{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002062 PyObject *kv;
2063 int err;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002064 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002065 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002066 return -1;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002067 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00002068 err = PyDict_SetItem(v, kv, item);
2069 Py_DECREF(kv);
2070 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002071}
2072
2073int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002074PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002075{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002076 PyObject *kv;
2077 int err;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002078 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002079 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002080 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002081 err = PyDict_DelItem(v, kv);
2082 Py_DECREF(kv);
2083 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002084}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002085
Raymond Hettinger019a1482004-03-18 02:41:19 +00002086/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002087
2088typedef struct {
2089 PyObject_HEAD
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002090 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002091 Py_ssize_t di_used;
2092 Py_ssize_t di_pos;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002093 PyObject* di_result; /* reusable result tuple for iteritems */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002094 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002095} dictiterobject;
2096
2097static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002098dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002099{
2100 dictiterobject *di;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002101 di = PyObject_New(dictiterobject, itertype);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002102 if (di == NULL)
2103 return NULL;
2104 Py_INCREF(dict);
2105 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00002106 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002107 di->di_pos = 0;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002108 di->len = dict->ma_used;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002109 if (itertype == &PyDictIterItem_Type) {
2110 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2111 if (di->di_result == NULL) {
2112 Py_DECREF(di);
2113 return NULL;
2114 }
2115 }
2116 else
2117 di->di_result = NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002118 return (PyObject *)di;
2119}
2120
2121static void
2122dictiter_dealloc(dictiterobject *di)
2123{
Guido van Rossum2147df72002-07-16 20:30:22 +00002124 Py_XDECREF(di->di_dict);
Raymond Hettinger019a1482004-03-18 02:41:19 +00002125 Py_XDECREF(di->di_result);
Neil Schemenauer6189b892002-04-12 02:43:00 +00002126 PyObject_Del(di);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002127}
2128
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002129static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002130dictiter_len(dictiterobject *di)
2131{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002132 Py_ssize_t len = 0;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00002133 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002134 len = di->len;
Christian Heimes217cfd12007-12-02 14:31:20 +00002135 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002136}
2137
Guido van Rossumb90c8482007-02-10 01:11:45 +00002138PyDoc_STRVAR(length_hint_doc,
2139 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002140
2141static PyMethodDef dictiter_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002142 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
2143 length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002144 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002145};
2146
Raymond Hettinger019a1482004-03-18 02:41:19 +00002147static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002148{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002149 PyObject *key;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002150 register Py_ssize_t i, mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002151 register PyDictEntry *ep;
2152 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002153
Raymond Hettinger019a1482004-03-18 02:41:19 +00002154 if (d == NULL)
Guido van Rossum2147df72002-07-16 20:30:22 +00002155 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002156 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002157
Raymond Hettinger019a1482004-03-18 02:41:19 +00002158 if (di->di_used != d->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00002159 PyErr_SetString(PyExc_RuntimeError,
2160 "dictionary changed size during iteration");
Guido van Rossum2147df72002-07-16 20:30:22 +00002161 di->di_used = -1; /* Make this state sticky */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002162 return NULL;
2163 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002164
Raymond Hettinger019a1482004-03-18 02:41:19 +00002165 i = di->di_pos;
2166 if (i < 0)
2167 goto fail;
2168 ep = d->ma_table;
2169 mask = d->ma_mask;
2170 while (i <= mask && ep[i].me_value == NULL)
2171 i++;
2172 di->di_pos = i+1;
2173 if (i > mask)
2174 goto fail;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002175 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002176 key = ep[i].me_key;
2177 Py_INCREF(key);
2178 return key;
2179
2180fail:
2181 Py_DECREF(d);
Guido van Rossum2147df72002-07-16 20:30:22 +00002182 di->di_dict = NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002183 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002184}
2185
Raymond Hettinger019a1482004-03-18 02:41:19 +00002186PyTypeObject PyDictIterKey_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002187 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Christian Heimesf83be4e2007-11-28 09:44:38 +00002188 "dict_keyiterator", /* tp_name */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002189 sizeof(dictiterobject), /* tp_basicsize */
2190 0, /* tp_itemsize */
2191 /* methods */
2192 (destructor)dictiter_dealloc, /* tp_dealloc */
2193 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002194 0, /* tp_getattr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002195 0, /* tp_setattr */
2196 0, /* tp_compare */
2197 0, /* tp_repr */
2198 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002199 0, /* tp_as_sequence */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002200 0, /* tp_as_mapping */
2201 0, /* tp_hash */
2202 0, /* tp_call */
2203 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002204 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002205 0, /* tp_setattro */
2206 0, /* tp_as_buffer */
2207 Py_TPFLAGS_DEFAULT, /* tp_flags */
2208 0, /* tp_doc */
2209 0, /* tp_traverse */
2210 0, /* tp_clear */
2211 0, /* tp_richcompare */
2212 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002213 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002214 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002215 dictiter_methods, /* tp_methods */
2216 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002217};
2218
2219static PyObject *dictiter_iternextvalue(dictiterobject *di)
2220{
2221 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002222 register Py_ssize_t i, mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002223 register PyDictEntry *ep;
2224 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002225
2226 if (d == NULL)
2227 return NULL;
2228 assert (PyDict_Check(d));
2229
2230 if (di->di_used != d->ma_used) {
2231 PyErr_SetString(PyExc_RuntimeError,
2232 "dictionary changed size during iteration");
2233 di->di_used = -1; /* Make this state sticky */
2234 return NULL;
2235 }
2236
2237 i = di->di_pos;
Guido van Rossum09240f62004-03-20 19:11:58 +00002238 mask = d->ma_mask;
2239 if (i < 0 || i > mask)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002240 goto fail;
2241 ep = d->ma_table;
Guido van Rossum09240f62004-03-20 19:11:58 +00002242 while ((value=ep[i].me_value) == NULL) {
Raymond Hettinger019a1482004-03-18 02:41:19 +00002243 i++;
Guido van Rossum09240f62004-03-20 19:11:58 +00002244 if (i > mask)
2245 goto fail;
2246 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002247 di->di_pos = i+1;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002248 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002249 Py_INCREF(value);
2250 return value;
2251
2252fail:
2253 Py_DECREF(d);
2254 di->di_dict = NULL;
2255 return NULL;
2256}
2257
2258PyTypeObject PyDictIterValue_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002259 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Christian Heimesf83be4e2007-11-28 09:44:38 +00002260 "dict_valueiterator", /* tp_name */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002261 sizeof(dictiterobject), /* tp_basicsize */
2262 0, /* tp_itemsize */
2263 /* methods */
2264 (destructor)dictiter_dealloc, /* tp_dealloc */
2265 0, /* tp_print */
2266 0, /* tp_getattr */
2267 0, /* tp_setattr */
2268 0, /* tp_compare */
2269 0, /* tp_repr */
2270 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002271 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002272 0, /* tp_as_mapping */
2273 0, /* tp_hash */
2274 0, /* tp_call */
2275 0, /* tp_str */
2276 PyObject_GenericGetAttr, /* tp_getattro */
2277 0, /* tp_setattro */
2278 0, /* tp_as_buffer */
2279 Py_TPFLAGS_DEFAULT, /* tp_flags */
2280 0, /* tp_doc */
2281 0, /* tp_traverse */
2282 0, /* tp_clear */
2283 0, /* tp_richcompare */
2284 0, /* tp_weaklistoffset */
2285 PyObject_SelfIter, /* tp_iter */
2286 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002287 dictiter_methods, /* tp_methods */
2288 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002289};
2290
2291static PyObject *dictiter_iternextitem(dictiterobject *di)
2292{
2293 PyObject *key, *value, *result = di->di_result;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002294 register Py_ssize_t i, mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002295 register PyDictEntry *ep;
2296 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002297
2298 if (d == NULL)
2299 return NULL;
2300 assert (PyDict_Check(d));
2301
2302 if (di->di_used != d->ma_used) {
2303 PyErr_SetString(PyExc_RuntimeError,
2304 "dictionary changed size during iteration");
2305 di->di_used = -1; /* Make this state sticky */
2306 return NULL;
2307 }
2308
2309 i = di->di_pos;
2310 if (i < 0)
2311 goto fail;
2312 ep = d->ma_table;
2313 mask = d->ma_mask;
2314 while (i <= mask && ep[i].me_value == NULL)
2315 i++;
2316 di->di_pos = i+1;
2317 if (i > mask)
2318 goto fail;
2319
2320 if (result->ob_refcnt == 1) {
2321 Py_INCREF(result);
2322 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2323 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2324 } else {
2325 result = PyTuple_New(2);
Tim Peters60b29962006-01-01 01:19:23 +00002326 if (result == NULL)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002327 return NULL;
2328 }
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002329 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002330 key = ep[i].me_key;
2331 value = ep[i].me_value;
2332 Py_INCREF(key);
2333 Py_INCREF(value);
2334 PyTuple_SET_ITEM(result, 0, key);
2335 PyTuple_SET_ITEM(result, 1, value);
2336 return result;
2337
2338fail:
2339 Py_DECREF(d);
2340 di->di_dict = NULL;
2341 return NULL;
2342}
2343
2344PyTypeObject PyDictIterItem_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002345 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Christian Heimesf83be4e2007-11-28 09:44:38 +00002346 "dict_itemiterator", /* tp_name */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002347 sizeof(dictiterobject), /* tp_basicsize */
2348 0, /* tp_itemsize */
2349 /* methods */
2350 (destructor)dictiter_dealloc, /* tp_dealloc */
2351 0, /* tp_print */
2352 0, /* tp_getattr */
2353 0, /* tp_setattr */
2354 0, /* tp_compare */
2355 0, /* tp_repr */
2356 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002357 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002358 0, /* tp_as_mapping */
2359 0, /* tp_hash */
2360 0, /* tp_call */
2361 0, /* tp_str */
2362 PyObject_GenericGetAttr, /* tp_getattro */
2363 0, /* tp_setattro */
2364 0, /* tp_as_buffer */
2365 Py_TPFLAGS_DEFAULT, /* tp_flags */
2366 0, /* tp_doc */
2367 0, /* tp_traverse */
2368 0, /* tp_clear */
2369 0, /* tp_richcompare */
2370 0, /* tp_weaklistoffset */
2371 PyObject_SelfIter, /* tp_iter */
2372 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002373 dictiter_methods, /* tp_methods */
2374 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002375};
Guido van Rossumb90c8482007-02-10 01:11:45 +00002376
2377
Guido van Rossum3ac67412007-02-10 18:55:06 +00002378/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002379/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002380/***********************************************/
2381
Guido van Rossumb90c8482007-02-10 01:11:45 +00002382/* The instance lay-out is the same for all three; but the type differs. */
2383
2384typedef struct {
2385 PyObject_HEAD
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002386 PyDictObject *dv_dict;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002387} dictviewobject;
2388
2389
2390static void
Guido van Rossum3ac67412007-02-10 18:55:06 +00002391dictview_dealloc(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002392{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002393 Py_XDECREF(dv->dv_dict);
2394 PyObject_Del(dv);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002395}
2396
Guido van Rossum83825ac2007-02-10 04:54:19 +00002397static Py_ssize_t
Guido van Rossum3ac67412007-02-10 18:55:06 +00002398dictview_len(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002399{
2400 Py_ssize_t len = 0;
Guido van Rossum3ac67412007-02-10 18:55:06 +00002401 if (dv->dv_dict != NULL)
2402 len = dv->dv_dict->ma_used;
Guido van Rossum83825ac2007-02-10 04:54:19 +00002403 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002404}
2405
2406static PyObject *
2407dictview_new(PyObject *dict, PyTypeObject *type)
2408{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002409 dictviewobject *dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002410 if (dict == NULL) {
2411 PyErr_BadInternalCall();
2412 return NULL;
2413 }
2414 if (!PyDict_Check(dict)) {
2415 /* XXX Get rid of this restriction later */
2416 PyErr_Format(PyExc_TypeError,
2417 "%s() requires a dict argument, not '%s'",
2418 type->tp_name, dict->ob_type->tp_name);
2419 return NULL;
2420 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002421 dv = PyObject_New(dictviewobject, type);
2422 if (dv == NULL)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002423 return NULL;
2424 Py_INCREF(dict);
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002425 dv->dv_dict = (PyDictObject *)dict;
Guido van Rossum3ac67412007-02-10 18:55:06 +00002426 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002427}
2428
Neal Norwitze36f2ba2007-02-26 23:12:28 +00002429/* TODO(guido): The views objects are not complete:
2430
2431 * support more set operations
2432 * support arbitrary mappings?
2433 - either these should be static or exported in dictobject.h
2434 - if public then they should probably be in builtins
2435*/
2436
Guido van Rossumaac530c2007-08-24 22:33:45 +00002437/* Return 1 if self is a subset of other, iterating over self;
2438 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002439static int
2440all_contained_in(PyObject *self, PyObject *other)
2441{
2442 PyObject *iter = PyObject_GetIter(self);
2443 int ok = 1;
2444
2445 if (iter == NULL)
2446 return -1;
2447 for (;;) {
2448 PyObject *next = PyIter_Next(iter);
2449 if (next == NULL) {
2450 if (PyErr_Occurred())
2451 ok = -1;
2452 break;
2453 }
2454 ok = PySequence_Contains(other, next);
2455 Py_DECREF(next);
2456 if (ok <= 0)
2457 break;
2458 }
2459 Py_DECREF(iter);
2460 return ok;
2461}
2462
2463static PyObject *
2464dictview_richcompare(PyObject *self, PyObject *other, int op)
2465{
Guido van Rossumaac530c2007-08-24 22:33:45 +00002466 Py_ssize_t len_self, len_other;
2467 int ok;
2468 PyObject *result;
2469
Guido van Rossumd9214d12007-02-12 02:23:40 +00002470 assert(self != NULL);
2471 assert(PyDictViewSet_Check(self));
2472 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00002473
Guido van Rossumaac530c2007-08-24 22:33:45 +00002474 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other)) {
Guido van Rossumd9214d12007-02-12 02:23:40 +00002475 Py_INCREF(Py_NotImplemented);
2476 return Py_NotImplemented;
2477 }
Guido van Rossumaac530c2007-08-24 22:33:45 +00002478
2479 len_self = PyObject_Size(self);
2480 if (len_self < 0)
2481 return NULL;
2482 len_other = PyObject_Size(other);
2483 if (len_other < 0)
2484 return NULL;
2485
2486 ok = 0;
2487 switch(op) {
2488
2489 case Py_NE:
2490 case Py_EQ:
2491 if (len_self == len_other)
2492 ok = all_contained_in(self, other);
2493 if (op == Py_NE && ok >= 0)
2494 ok = !ok;
2495 break;
2496
2497 case Py_LT:
2498 if (len_self < len_other)
2499 ok = all_contained_in(self, other);
2500 break;
2501
2502 case Py_LE:
2503 if (len_self <= len_other)
2504 ok = all_contained_in(self, other);
2505 break;
2506
2507 case Py_GT:
2508 if (len_self > len_other)
2509 ok = all_contained_in(other, self);
2510 break;
2511
2512 case Py_GE:
2513 if (len_self >= len_other)
2514 ok = all_contained_in(other, self);
2515 break;
2516
2517 }
2518 if (ok < 0)
2519 return NULL;
2520 result = ok ? Py_True : Py_False;
2521 Py_INCREF(result);
2522 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00002523}
2524
Guido van Rossum3ac67412007-02-10 18:55:06 +00002525/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002526
2527static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002528dictkeys_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002529{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002530 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002531 Py_RETURN_NONE;
2532 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002533 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
2534}
2535
2536static int
2537dictkeys_contains(dictviewobject *dv, PyObject *obj)
2538{
2539 if (dv->dv_dict == NULL)
2540 return 0;
2541 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002542}
2543
Guido van Rossum83825ac2007-02-10 04:54:19 +00002544static PySequenceMethods dictkeys_as_sequence = {
2545 (lenfunc)dictview_len, /* sq_length */
2546 0, /* sq_concat */
2547 0, /* sq_repeat */
2548 0, /* sq_item */
2549 0, /* sq_slice */
2550 0, /* sq_ass_item */
2551 0, /* sq_ass_slice */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002552 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002553};
2554
Guido van Rossum523259b2007-08-24 23:41:22 +00002555static PyObject*
2556dictviews_sub(PyObject* self, PyObject *other)
2557{
2558 PyObject *result = PySet_New(self);
2559 PyObject *tmp;
2560 if (result == NULL)
2561 return NULL;
2562
2563 tmp = PyObject_CallMethod(result, "difference_update", "O", other);
2564 if (tmp == NULL) {
2565 Py_DECREF(result);
2566 return NULL;
2567 }
2568
2569 Py_DECREF(tmp);
2570 return result;
2571}
2572
2573static PyObject*
2574dictviews_and(PyObject* self, PyObject *other)
2575{
2576 PyObject *result = PySet_New(self);
2577 PyObject *tmp;
2578 if (result == NULL)
2579 return NULL;
2580
2581 tmp = PyObject_CallMethod(result, "intersection_update", "O", other);
2582 if (tmp == NULL) {
2583 Py_DECREF(result);
2584 return NULL;
2585 }
2586
2587 Py_DECREF(tmp);
2588 return result;
2589}
2590
2591static PyObject*
2592dictviews_or(PyObject* self, PyObject *other)
2593{
2594 PyObject *result = PySet_New(self);
2595 PyObject *tmp;
2596 if (result == NULL)
2597 return NULL;
2598
2599 tmp = PyObject_CallMethod(result, "update", "O", other);
2600 if (tmp == NULL) {
2601 Py_DECREF(result);
2602 return NULL;
2603 }
2604
2605 Py_DECREF(tmp);
2606 return result;
2607}
2608
2609static PyObject*
2610dictviews_xor(PyObject* self, PyObject *other)
2611{
2612 PyObject *result = PySet_New(self);
2613 PyObject *tmp;
2614 if (result == NULL)
2615 return NULL;
2616
2617 tmp = PyObject_CallMethod(result, "symmetric_difference_update", "O",
2618 other);
2619 if (tmp == NULL) {
2620 Py_DECREF(result);
2621 return NULL;
2622 }
2623
2624 Py_DECREF(tmp);
2625 return result;
2626}
2627
2628static PyNumberMethods dictviews_as_number = {
2629 0, /*nb_add*/
2630 (binaryfunc)dictviews_sub, /*nb_subtract*/
2631 0, /*nb_multiply*/
2632 0, /*nb_remainder*/
2633 0, /*nb_divmod*/
2634 0, /*nb_power*/
2635 0, /*nb_negative*/
2636 0, /*nb_positive*/
2637 0, /*nb_absolute*/
2638 0, /*nb_bool*/
2639 0, /*nb_invert*/
2640 0, /*nb_lshift*/
2641 0, /*nb_rshift*/
2642 (binaryfunc)dictviews_and, /*nb_and*/
2643 (binaryfunc)dictviews_xor, /*nb_xor*/
2644 (binaryfunc)dictviews_or, /*nb_or*/
2645};
2646
Guido van Rossumb90c8482007-02-10 01:11:45 +00002647static PyMethodDef dictkeys_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002648 {NULL, NULL} /* sentinel */
2649};
2650
2651PyTypeObject PyDictKeys_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002652 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002653 "dict_keys", /* tp_name */
2654 sizeof(dictviewobject), /* tp_basicsize */
2655 0, /* tp_itemsize */
2656 /* methods */
2657 (destructor)dictview_dealloc, /* tp_dealloc */
2658 0, /* tp_print */
2659 0, /* tp_getattr */
2660 0, /* tp_setattr */
2661 0, /* tp_compare */
2662 0, /* tp_repr */
Guido van Rossum523259b2007-08-24 23:41:22 +00002663 &dictviews_as_number, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002664 &dictkeys_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002665 0, /* tp_as_mapping */
2666 0, /* tp_hash */
2667 0, /* tp_call */
2668 0, /* tp_str */
2669 PyObject_GenericGetAttr, /* tp_getattro */
2670 0, /* tp_setattro */
2671 0, /* tp_as_buffer */
2672 Py_TPFLAGS_DEFAULT, /* tp_flags */
2673 0, /* tp_doc */
2674 0, /* tp_traverse */
2675 0, /* tp_clear */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002676 dictview_richcompare, /* tp_richcompare */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002677 0, /* tp_weaklistoffset */
2678 (getiterfunc)dictkeys_iter, /* tp_iter */
2679 0, /* tp_iternext */
2680 dictkeys_methods, /* tp_methods */
2681 0,
2682};
2683
2684static PyObject *
2685dictkeys_new(PyObject *dict)
2686{
2687 return dictview_new(dict, &PyDictKeys_Type);
2688}
2689
Guido van Rossum3ac67412007-02-10 18:55:06 +00002690/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002691
2692static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002693dictitems_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002694{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002695 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002696 Py_RETURN_NONE;
2697 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002698 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
2699}
2700
2701static int
2702dictitems_contains(dictviewobject *dv, PyObject *obj)
2703{
2704 PyObject *key, *value, *found;
2705 if (dv->dv_dict == NULL)
2706 return 0;
2707 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
2708 return 0;
2709 key = PyTuple_GET_ITEM(obj, 0);
2710 value = PyTuple_GET_ITEM(obj, 1);
2711 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
2712 if (found == NULL) {
2713 if (PyErr_Occurred())
2714 return -1;
2715 return 0;
2716 }
2717 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002718}
2719
Guido van Rossum83825ac2007-02-10 04:54:19 +00002720static PySequenceMethods dictitems_as_sequence = {
2721 (lenfunc)dictview_len, /* sq_length */
2722 0, /* sq_concat */
2723 0, /* sq_repeat */
2724 0, /* sq_item */
2725 0, /* sq_slice */
2726 0, /* sq_ass_item */
2727 0, /* sq_ass_slice */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002728 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002729};
2730
Guido van Rossumb90c8482007-02-10 01:11:45 +00002731static PyMethodDef dictitems_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002732 {NULL, NULL} /* sentinel */
2733};
2734
2735PyTypeObject PyDictItems_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002736 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002737 "dict_items", /* tp_name */
2738 sizeof(dictviewobject), /* tp_basicsize */
2739 0, /* tp_itemsize */
2740 /* methods */
2741 (destructor)dictview_dealloc, /* tp_dealloc */
2742 0, /* tp_print */
2743 0, /* tp_getattr */
2744 0, /* tp_setattr */
2745 0, /* tp_compare */
2746 0, /* tp_repr */
Guido van Rossum523259b2007-08-24 23:41:22 +00002747 &dictviews_as_number, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002748 &dictitems_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002749 0, /* tp_as_mapping */
2750 0, /* tp_hash */
2751 0, /* tp_call */
2752 0, /* tp_str */
2753 PyObject_GenericGetAttr, /* tp_getattro */
2754 0, /* tp_setattro */
2755 0, /* tp_as_buffer */
2756 Py_TPFLAGS_DEFAULT, /* tp_flags */
2757 0, /* tp_doc */
2758 0, /* tp_traverse */
2759 0, /* tp_clear */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002760 dictview_richcompare, /* tp_richcompare */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002761 0, /* tp_weaklistoffset */
2762 (getiterfunc)dictitems_iter, /* tp_iter */
2763 0, /* tp_iternext */
2764 dictitems_methods, /* tp_methods */
2765 0,
2766};
2767
2768static PyObject *
2769dictitems_new(PyObject *dict)
2770{
2771 return dictview_new(dict, &PyDictItems_Type);
2772}
2773
Guido van Rossum3ac67412007-02-10 18:55:06 +00002774/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002775
2776static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002777dictvalues_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002778{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002779 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002780 Py_RETURN_NONE;
2781 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002782 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002783}
2784
Guido van Rossum83825ac2007-02-10 04:54:19 +00002785static PySequenceMethods dictvalues_as_sequence = {
2786 (lenfunc)dictview_len, /* sq_length */
2787 0, /* sq_concat */
2788 0, /* sq_repeat */
2789 0, /* sq_item */
2790 0, /* sq_slice */
2791 0, /* sq_ass_item */
2792 0, /* sq_ass_slice */
2793 (objobjproc)0, /* sq_contains */
2794};
2795
Guido van Rossumb90c8482007-02-10 01:11:45 +00002796static PyMethodDef dictvalues_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002797 {NULL, NULL} /* sentinel */
2798};
2799
2800PyTypeObject PyDictValues_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002801 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002802 "dict_values", /* tp_name */
2803 sizeof(dictviewobject), /* tp_basicsize */
2804 0, /* tp_itemsize */
2805 /* methods */
2806 (destructor)dictview_dealloc, /* tp_dealloc */
2807 0, /* tp_print */
2808 0, /* tp_getattr */
2809 0, /* tp_setattr */
2810 0, /* tp_compare */
2811 0, /* tp_repr */
2812 0, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002813 &dictvalues_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002814 0, /* tp_as_mapping */
2815 0, /* tp_hash */
2816 0, /* tp_call */
2817 0, /* tp_str */
2818 PyObject_GenericGetAttr, /* tp_getattro */
2819 0, /* tp_setattro */
2820 0, /* tp_as_buffer */
2821 Py_TPFLAGS_DEFAULT, /* tp_flags */
2822 0, /* tp_doc */
2823 0, /* tp_traverse */
2824 0, /* tp_clear */
2825 0, /* tp_richcompare */
2826 0, /* tp_weaklistoffset */
2827 (getiterfunc)dictvalues_iter, /* tp_iter */
2828 0, /* tp_iternext */
2829 dictvalues_methods, /* tp_methods */
2830 0,
2831};
2832
2833static PyObject *
2834dictvalues_new(PyObject *dict)
2835{
2836 return dictview_new(dict, &PyDictValues_Type);
2837}