blob: 96089a16af0641a5db92d1706760bb7f8b625389 [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"
Guido van Rossum4b1302b1993-03-27 18:11:32 +000011
Tim Peters6d6c1a32001-08-02 04:15:00 +000012typedef PyDictEntry dictentry;
13typedef PyDictObject dictobject;
Guido van Rossum16e93a81997-01-28 00:00:11 +000014
Thomas Wouters89f507f2006-12-13 04:49:30 +000015/* Set a key error with the specified argument, wrapping it in a
16 * tuple automatically so that tuple keys are not unpacked as the
17 * exception arguments. */
18static void
19set_key_error(PyObject *arg)
20{
21 PyObject *tup;
22 tup = PyTuple_Pack(1, arg);
23 if (!tup)
24 return; /* caller will expect error to be set anyway */
25 PyErr_SetObject(PyExc_KeyError, tup);
26 Py_DECREF(tup);
27}
28
Tim Peterseb28ef22001-06-02 05:27:19 +000029/* Define this out if you don't want conversion statistics on exit. */
Fred Drake1bff34a2000-08-31 19:31:38 +000030#undef SHOW_CONVERSION_COUNTS
31
Tim Peterseb28ef22001-06-02 05:27:19 +000032/* See large comment block below. This must be >= 1. */
33#define PERTURB_SHIFT 5
34
Guido van Rossum16e93a81997-01-28 00:00:11 +000035/*
Tim Peterseb28ef22001-06-02 05:27:19 +000036Major subtleties ahead: Most hash schemes depend on having a "good" hash
37function, in the sense of simulating randomness. Python doesn't: its most
38important hash functions (for strings and ints) are very regular in common
39cases:
Tim Peters15d49292001-05-27 07:39:22 +000040
Guido van Rossumdc5f6b22006-08-24 21:29:26 +000041 >>> map(hash, (0, 1, 2, 3))
42 [0, 1, 2, 3]
43 >>> map(hash, ("namea", "nameb", "namec", "named"))
44 [-1658398457, -1658398460, -1658398459, -1658398462]
45 >>>
Tim Peters15d49292001-05-27 07:39:22 +000046
Tim Peterseb28ef22001-06-02 05:27:19 +000047This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
48the low-order i bits as the initial table index is extremely fast, and there
49are no collisions at all for dicts indexed by a contiguous range of ints.
50The same is approximately true when keys are "consecutive" strings. So this
51gives better-than-random behavior in common cases, and that's very desirable.
Tim Peters15d49292001-05-27 07:39:22 +000052
Tim Peterseb28ef22001-06-02 05:27:19 +000053OTOH, when collisions occur, the tendency to fill contiguous slices of the
54hash table makes a good collision resolution strategy crucial. Taking only
55the last i bits of the hash code is also vulnerable: for example, consider
Guido van Rossumdc5f6b22006-08-24 21:29:26 +000056the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
57their own hash codes, and this fits in a dict of size 2**15, the last 15 bits
58 of every hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +000059
Tim Peterseb28ef22001-06-02 05:27:19 +000060But catering to unusual cases should not slow the usual ones, so we just take
61the last i bits anyway. It's up to collision resolution to do the rest. If
62we *usually* find the key we're looking for on the first try (and, it turns
63out, we usually do -- the table load factor is kept under 2/3, so the odds
64are solidly in our favor), then it makes best sense to keep the initial index
65computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +000066
Tim Peterseb28ef22001-06-02 05:27:19 +000067The first half of collision resolution is to visit table indices via this
68recurrence:
Tim Peters15d49292001-05-27 07:39:22 +000069
Tim Peterseb28ef22001-06-02 05:27:19 +000070 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +000071
Tim Peterseb28ef22001-06-02 05:27:19 +000072For any initial j in range(2**i), repeating that 2**i times generates each
73int in range(2**i) exactly once (see any text on random-number generation for
74proof). By itself, this doesn't help much: like linear probing (setting
75j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
76order. This would be bad, except that's not the only thing we do, and it's
77actually *good* in the common cases where hash keys are consecutive. In an
78example that's really too small to make this entirely clear, for a table of
79size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +000080
Tim Peterseb28ef22001-06-02 05:27:19 +000081 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
82
83If two things come in at index 5, the first place we look after is index 2,
84not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
85Linear probing is deadly in this case because there the fixed probe order
86is the *same* as the order consecutive keys are likely to arrive. But it's
87extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
88and certain that consecutive hash codes do not.
89
90The other half of the strategy is to get the other bits of the hash code
91into play. This is done by initializing a (unsigned) vrbl "perturb" to the
92full hash code, and changing the recurrence to:
93
94 j = (5*j) + 1 + perturb;
95 perturb >>= PERTURB_SHIFT;
96 use j % 2**i as the next table index;
97
98Now the probe sequence depends (eventually) on every bit in the hash code,
99and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
100because it quickly magnifies small differences in the bits that didn't affect
101the initial index. Note that because perturb is unsigned, if the recurrence
102is executed often enough perturb eventually becomes and remains 0. At that
103point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
104that's certain to find an empty slot eventually (since it generates every int
105in range(2**i), and we make sure there's always at least one empty slot).
106
107Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
108small so that the high bits of the hash code continue to affect the probe
109sequence across iterations; but you want it large so that in really bad cases
110the high-order hash bits have an effect on early iterations. 5 was "the
111best" in minimizing total collisions across experiments Tim Peters ran (on
112both normal and pathological cases), but 4 and 6 weren't significantly worse.
113
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000114Historical: Reimer Behrends contributed the idea of using a polynomial-based
Tim Peterseb28ef22001-06-02 05:27:19 +0000115approach, using repeated multiplication by x in GF(2**n) where an irreducible
116polynomial for each table size was chosen such that x was a primitive root.
117Christian Tismer later extended that to use division by x instead, as an
118efficient way to get the high bits of the hash code into play. This scheme
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000119also gave excellent collision statistics, but was more expensive: two if-tests
120were required inside the loop; computing "the next" index took about the same
121number of operations but without as much potential parallelism (e.g.,
122computing 5*j can go on at the same time as computing 1+perturb in the above,
123and then shifting perturb can be done while the table index is being masked);
124and the dictobject struct required a member to hold the table's polynomial.
125In Tim's experiments the current scheme ran faster, produced equally good
126collision statistics, needed less code & used less memory.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000127
128Theoretical Python 2.5 headache: hash codes are only C "long", but
129sizeof(Py_ssize_t) > sizeof(long) may be possible. In that case, and if a
130dict is genuinely huge, then only the slots directly reachable via indexing
131by a C long can be the first slot in a probe sequence. The probe sequence
132will still eventually reach every slot in the table, but the collision rate
133on initial probes may be much higher than this scheme was designed for.
134Getting a hash code as fat as Py_ssize_t is the only real cure. But in
135practice, this probably won't make a lick of difference for many years (at
136which point everyone will have terabytes of RAM on 64-bit boxes).
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000137*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000138
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000139/* Object used as dummy key to fill deleted entries */
Raymond Hettingerf81e4502005-08-17 02:19:36 +0000140static PyObject *dummy = NULL; /* Initialized by first call to newdictobject() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000141
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000142#ifdef Py_REF_DEBUG
143PyObject *
144_PyDict_Dummy(void)
145{
146 return dummy;
147}
148#endif
149
Fred Drake1bff34a2000-08-31 19:31:38 +0000150/* forward declarations */
151static dictentry *
152lookdict_string(dictobject *mp, PyObject *key, long hash);
153
154#ifdef SHOW_CONVERSION_COUNTS
155static long created = 0L;
156static long converted = 0L;
157
158static void
159show_counts(void)
160{
161 fprintf(stderr, "created %ld string dicts\n", created);
162 fprintf(stderr, "converted %ld to normal dicts\n", converted);
163 fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created);
164}
165#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000166
Tim Peters6d6c1a32001-08-02 04:15:00 +0000167/* Initialization macros.
168 There are two ways to create a dict: PyDict_New() is the main C API
169 function, and the tp_new slot maps to dict_new(). In the latter case we
170 can save a little time over what PyDict_New does because it's guaranteed
171 that the PyDictObject struct is already zeroed out.
172 Everyone except dict_new() should use EMPTY_TO_MINSIZE (unless they have
173 an excellent reason not to).
174*/
175
176#define INIT_NONZERO_DICT_SLOTS(mp) do { \
Tim Petersdea48ec2001-05-22 20:40:22 +0000177 (mp)->ma_table = (mp)->ma_smalltable; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000178 (mp)->ma_mask = PyDict_MINSIZE - 1; \
179 } while(0)
180
181#define EMPTY_TO_MINSIZE(mp) do { \
182 memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000183 (mp)->ma_used = (mp)->ma_fill = 0; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000184 INIT_NONZERO_DICT_SLOTS(mp); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000185 } while(0)
186
Raymond Hettinger43442782004-03-17 21:55:03 +0000187/* Dictionary reuse scheme to save calls to malloc, free, and memset */
188#define MAXFREEDICTS 80
189static PyDictObject *free_dicts[MAXFREEDICTS];
190static int num_free_dicts = 0;
191
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000192PyObject *
Thomas Wouters78890102000-07-22 19:25:51 +0000193PyDict_New(void)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000194{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000195 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000196 if (dummy == NULL) { /* Auto-initialize dummy */
Neal Norwitz53cbdaa2007-08-23 21:42:55 +0000197 dummy = PyUnicode_FromString("<dummy key>");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000198 if (dummy == NULL)
199 return NULL;
Fred Drake1bff34a2000-08-31 19:31:38 +0000200#ifdef SHOW_CONVERSION_COUNTS
201 Py_AtExit(show_counts);
202#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000203 }
Raymond Hettinger43442782004-03-17 21:55:03 +0000204 if (num_free_dicts) {
205 mp = free_dicts[--num_free_dicts];
206 assert (mp != NULL);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000207 assert (Py_Type(mp) == &PyDict_Type);
Raymond Hettinger43442782004-03-17 21:55:03 +0000208 _Py_NewReference((PyObject *)mp);
209 if (mp->ma_fill) {
210 EMPTY_TO_MINSIZE(mp);
211 }
212 assert (mp->ma_used == 0);
213 assert (mp->ma_table == mp->ma_smalltable);
214 assert (mp->ma_mask == PyDict_MINSIZE - 1);
215 } else {
216 mp = PyObject_GC_New(dictobject, &PyDict_Type);
217 if (mp == NULL)
218 return NULL;
219 EMPTY_TO_MINSIZE(mp);
220 }
Fred Drake1bff34a2000-08-31 19:31:38 +0000221 mp->ma_lookup = lookdict_string;
222#ifdef SHOW_CONVERSION_COUNTS
223 ++created;
224#endif
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000225 _PyObject_GC_TRACK(mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000226 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000227}
228
229/*
230The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000231This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000232Open addressing is preferred over chaining since the link overhead for
233chaining would be substantial (100% with typical malloc overhead).
234
Tim Peterseb28ef22001-06-02 05:27:19 +0000235The initial probe index is computed as hash mod the table size. Subsequent
236probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000237
238All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000239
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000240The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000241contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000242Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000243
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000244lookdict() is general-purpose, and may return NULL if (and only if) a
245comparison raises an exception (this was new in Python 2.5).
246lookdict_string() below is specialized to string keys, comparison of which can
247never raise an exception; that function can never return NULL. For both, when
248the key isn't found a dictentry* is returned for which the me_value field is
249NULL; this is the slot in the dict at which the key would have been found, and
250the caller can (if it wishes) add the <key, value> pair to the returned
251dictentry*.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000252*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000253static dictentry *
Tim Peters1f5871e2000-07-04 17:44:48 +0000254lookdict(dictobject *mp, PyObject *key, register long hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000255{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000256 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000257 register size_t perturb;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000258 register dictentry *freeslot;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000259 register size_t mask = (size_t)mp->ma_mask;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000260 dictentry *ep0 = mp->ma_table;
261 register dictentry *ep;
Fred Drakec88b99c2000-08-31 19:04:07 +0000262 register int cmp;
Tim Peters453163d2001-06-03 04:54:32 +0000263 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000264
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000265 i = (size_t)hash & mask;
Guido van Rossumefb46091997-01-29 15:53:56 +0000266 ep = &ep0[i];
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000267 if (ep->me_key == NULL || ep->me_key == key)
Guido van Rossum7d181591997-01-16 21:06:45 +0000268 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000269
Guido van Rossum16e93a81997-01-28 00:00:11 +0000270 if (ep->me_key == dummy)
Guido van Rossum7d181591997-01-16 21:06:45 +0000271 freeslot = ep;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000272 else {
Fred Drakec88b99c2000-08-31 19:04:07 +0000273 if (ep->me_hash == hash) {
Tim Peters453163d2001-06-03 04:54:32 +0000274 startkey = ep->me_key;
275 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000276 if (cmp < 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000277 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000278 if (ep0 == mp->ma_table && ep->me_key == startkey) {
279 if (cmp > 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000280 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000281 }
282 else {
283 /* The compare did major nasty stuff to the
284 * dict: start over.
285 * XXX A clever adversary could prevent this
286 * XXX from terminating.
287 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000288 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000289 }
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000290 }
291 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000292 }
Tim Peters15d49292001-05-27 07:39:22 +0000293
Tim Peters342c65e2001-05-13 06:43:53 +0000294 /* In the loop, me_key == dummy is by far (factor of 100s) the
295 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000296 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
297 i = (i << 2) + i + perturb + 1;
298 ep = &ep0[i & mask];
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000299 if (ep->me_key == NULL)
300 return freeslot == NULL ? ep : freeslot;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000301 if (ep->me_key == key)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000302 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000303 if (ep->me_hash == hash && ep->me_key != dummy) {
Tim Peters453163d2001-06-03 04:54:32 +0000304 startkey = ep->me_key;
305 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000306 if (cmp < 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000307 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000308 if (ep0 == mp->ma_table && ep->me_key == startkey) {
309 if (cmp > 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000310 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000311 }
312 else {
313 /* The compare did major nasty stuff to the
314 * dict: start over.
315 * XXX A clever adversary could prevent this
316 * XXX from terminating.
317 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000318 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000319 }
Guido van Rossum16e93a81997-01-28 00:00:11 +0000320 }
Tim Peters342c65e2001-05-13 06:43:53 +0000321 else if (ep->me_key == dummy && freeslot == NULL)
322 freeslot = ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000323 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000324 assert(0); /* NOT REACHED */
325 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000326}
327
328/*
Fred Drake1bff34a2000-08-31 19:31:38 +0000329 * Hacked up version of lookdict which can assume keys are always strings;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000330 * this assumption allows testing for errors during PyObject_RichCompareBool()
331 * to be dropped; string-string comparisons never raise exceptions. This also
332 * means we don't need to go through PyObject_RichCompareBool(); we can always
333 * use _PyString_Eq() directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000334 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000335 * This is valuable because dicts with only string keys are very common.
Fred Drake1bff34a2000-08-31 19:31:38 +0000336 */
337static dictentry *
338lookdict_string(dictobject *mp, PyObject *key, register long hash)
339{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000340 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000341 register size_t perturb;
Fred Drake1bff34a2000-08-31 19:31:38 +0000342 register dictentry *freeslot;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000343 register size_t mask = (size_t)mp->ma_mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000344 dictentry *ep0 = mp->ma_table;
345 register dictentry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000346
Tim Peters0ab085c2001-09-14 00:25:33 +0000347 /* Make sure this function doesn't have to handle non-string keys,
348 including subclasses of str; e.g., one reason to subclass
349 strings is to override __eq__, and for speed we don't cater to
350 that here. */
351 if (!PyString_CheckExact(key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000352#ifdef SHOW_CONVERSION_COUNTS
353 ++converted;
354#endif
355 mp->ma_lookup = lookdict;
356 return lookdict(mp, key, hash);
357 }
Tim Peters2f228e72001-05-13 00:19:31 +0000358 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000359 ep = &ep0[i];
360 if (ep->me_key == NULL || ep->me_key == key)
361 return ep;
362 if (ep->me_key == dummy)
363 freeslot = ep;
364 else {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000365 if (ep->me_hash == hash && _PyString_Eq(ep->me_key, key))
Fred Drake1bff34a2000-08-31 19:31:38 +0000366 return ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000367 freeslot = NULL;
368 }
Tim Peters15d49292001-05-27 07:39:22 +0000369
Tim Peters342c65e2001-05-13 06:43:53 +0000370 /* In the loop, me_key == dummy is by far (factor of 100s) the
371 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000372 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
373 i = (i << 2) + i + perturb + 1;
374 ep = &ep0[i & mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000375 if (ep->me_key == NULL)
376 return freeslot == NULL ? ep : freeslot;
377 if (ep->me_key == key
378 || (ep->me_hash == hash
379 && ep->me_key != dummy
Martin v. Löwiscd353062001-05-24 16:56:35 +0000380 && _PyString_Eq(ep->me_key, key)))
Fred Drake1bff34a2000-08-31 19:31:38 +0000381 return ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000382 if (ep->me_key == dummy && freeslot == NULL)
Tim Peters342c65e2001-05-13 06:43:53 +0000383 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000384 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000385 assert(0); /* NOT REACHED */
386 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000387}
388
389/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000390Internal routine to insert a new item into the table.
391Used both by the internal resize routine and by the public insert routine.
392Eats a reference to key and one to value.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000393Returns -1 if an error occurred, or 0 on success.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000394*/
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000395static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000396insertdict(register dictobject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000397{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000398 PyObject *old_value;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000399 register dictentry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000400 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
401
402 assert(mp->ma_lookup != NULL);
403 ep = mp->ma_lookup(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000404 if (ep == NULL) {
405 Py_DECREF(key);
406 Py_DECREF(value);
407 return -1;
408 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000409 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000410 old_value = ep->me_value;
411 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000412 Py_DECREF(old_value); /* which **CAN** re-enter */
413 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000414 }
415 else {
416 if (ep->me_key == NULL)
417 mp->ma_fill++;
Raymond Hettinger07ead172005-02-05 23:42:57 +0000418 else {
419 assert(ep->me_key == dummy);
420 Py_DECREF(dummy);
421 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000422 ep->me_key = key;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000423 ep->me_hash = (Py_ssize_t)hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000424 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000425 mp->ma_used++;
426 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000427 return 0;
428}
429
430/*
431Internal routine used by dictresize() to insert an item which is
432known to be absent from the dict. This routine also assumes that
433the dict contains no deleted entries. Besides the performance benefit,
434using insertdict() in dictresize() is dangerous (SF bug #1456209).
435Note that no refcounts are changed by this routine; if needed, the caller
436is responsible for incref'ing `key` and `value`.
437*/
438static void
439insertdict_clean(register dictobject *mp, PyObject *key, long hash,
440 PyObject *value)
441{
442 register size_t i;
443 register size_t perturb;
444 register size_t mask = (size_t)mp->ma_mask;
445 dictentry *ep0 = mp->ma_table;
446 register dictentry *ep;
447
448 i = hash & mask;
449 ep = &ep0[i];
450 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
451 i = (i << 2) + i + perturb + 1;
452 ep = &ep0[i & mask];
453 }
454 assert(ep->me_value == NULL);
455 mp->ma_fill++;
456 ep->me_key = key;
457 ep->me_hash = (Py_ssize_t)hash;
458 ep->me_value = value;
459 mp->ma_used++;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000460}
461
462/*
463Restructure the table by allocating a new table and reinserting all
464items again. When entries have been deleted, the new table may
465actually be smaller than the old one.
466*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000467static int
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000468dictresize(dictobject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000469{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000470 Py_ssize_t newsize;
Tim Peters0c6010b2001-05-23 23:33:57 +0000471 dictentry *oldtable, *newtable, *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000472 Py_ssize_t i;
Tim Peters0c6010b2001-05-23 23:33:57 +0000473 int is_oldtable_malloced;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000474 dictentry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000475
476 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000477
Tim Peterseb28ef22001-06-02 05:27:19 +0000478 /* Find the smallest table size > minused. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000479 for (newsize = PyDict_MINSIZE;
Tim Peterseb28ef22001-06-02 05:27:19 +0000480 newsize <= minused && newsize > 0;
481 newsize <<= 1)
482 ;
483 if (newsize <= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000484 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000485 return -1;
486 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000487
488 /* Get space for a new table. */
489 oldtable = mp->ma_table;
490 assert(oldtable != NULL);
491 is_oldtable_malloced = oldtable != mp->ma_smalltable;
492
Tim Peters6d6c1a32001-08-02 04:15:00 +0000493 if (newsize == PyDict_MINSIZE) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000494 /* A large table is shrinking, or we can't get any smaller. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000495 newtable = mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000496 if (newtable == oldtable) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000497 if (mp->ma_fill == mp->ma_used) {
498 /* No dummies, so no point doing anything. */
Tim Peters0c6010b2001-05-23 23:33:57 +0000499 return 0;
Tim Petersf8a548c2001-05-24 16:26:40 +0000500 }
501 /* We're not going to resize it, but rebuild the
502 table anyway to purge old dummy entries.
503 Subtle: This is *necessary* if fill==size,
504 as lookdict needs at least one virgin slot to
505 terminate failing searches. If fill < size, it's
506 merely desirable, as dummies slow searches. */
507 assert(mp->ma_fill > mp->ma_used);
Tim Peters0c6010b2001-05-23 23:33:57 +0000508 memcpy(small_copy, oldtable, sizeof(small_copy));
509 oldtable = small_copy;
510 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000511 }
512 else {
513 newtable = PyMem_NEW(dictentry, newsize);
514 if (newtable == NULL) {
515 PyErr_NoMemory();
516 return -1;
517 }
518 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000519
520 /* Make the dict empty, using the new table. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000521 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000522 mp->ma_table = newtable;
Tim Petersafb6ae82001-06-04 21:00:21 +0000523 mp->ma_mask = newsize - 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000524 memset(newtable, 0, sizeof(dictentry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000525 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000526 i = mp->ma_fill;
527 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000528
Tim Peters19283142001-05-17 22:25:34 +0000529 /* Copy the data over; this is refcount-neutral for active entries;
530 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000531 for (ep = oldtable; i > 0; ep++) {
532 if (ep->me_value != NULL) { /* active entry */
533 --i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000534 insertdict_clean(mp, ep->me_key, (long)ep->me_hash,
535 ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000536 }
Tim Peters19283142001-05-17 22:25:34 +0000537 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000538 --i;
Tim Peters19283142001-05-17 22:25:34 +0000539 assert(ep->me_key == dummy);
540 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000541 }
Tim Peters19283142001-05-17 22:25:34 +0000542 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000543 }
544
Tim Peters0c6010b2001-05-23 23:33:57 +0000545 if (is_oldtable_malloced)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000546 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000547 return 0;
548}
549
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000550/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
551 * that may occur (originally dicts supported only string keys, and exceptions
552 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000553 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000554 * (suppressed) occurred while computing the key's hash, or that some error
555 * (suppressed) occurred when comparing keys in the dict's internal probe
556 * sequence. A nasty example of the latter is when a Python-coded comparison
557 * function hits a stack-depth error, which can cause this to return NULL
558 * even if the key is present.
559 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000560PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000561PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000562{
563 long hash;
Fred Drake1bff34a2000-08-31 19:31:38 +0000564 dictobject *mp = (dictobject *)op;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000565 dictentry *ep;
566 PyThreadState *tstate;
567 if (!PyDict_Check(op))
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000568 return NULL;
Guido van Rossum4d027722007-09-18 04:30:42 +0000569 if (!PyUnicode_CheckExact(key) ||
570 (hash = ((PyUnicodeObject *) key)->hash) == -1)
Guido van Rossumca756f21997-01-23 19:39:29 +0000571 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000572 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000573 if (hash == -1) {
574 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000575 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000576 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000577 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000578
579 /* We can arrive here with a NULL tstate during initialization:
580 try running "python -Wi" for an example related to string
581 interning. Let's just hope that no exception occurs then... */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000582 tstate = _PyThreadState_Current;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000583 if (tstate != NULL && tstate->curexc_type != NULL) {
584 /* preserve the existing exception */
585 PyObject *err_type, *err_value, *err_tb;
586 PyErr_Fetch(&err_type, &err_value, &err_tb);
587 ep = (mp->ma_lookup)(mp, key, hash);
588 /* ignore errors */
589 PyErr_Restore(err_type, err_value, err_tb);
590 if (ep == NULL)
591 return NULL;
592 }
593 else {
594 ep = (mp->ma_lookup)(mp, key, hash);
595 if (ep == NULL) {
596 PyErr_Clear();
597 return NULL;
598 }
599 }
600 return ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000601}
602
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000603/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
604 This returns NULL *with* an exception set if an exception occurred.
605 It returns NULL *without* an exception set if the key wasn't present.
606*/
607PyObject *
608PyDict_GetItemWithError(PyObject *op, PyObject *key)
609{
610 long hash;
611 dictobject *mp = (dictobject *)op;
612 dictentry *ep;
613
614 if (!PyDict_Check(op)) {
615 PyErr_BadInternalCall();
616 return NULL;
617 }
618 if (!PyString_CheckExact(key) ||
619 (hash = ((PyStringObject *) key)->ob_shash) == -1)
620 {
621 hash = PyObject_Hash(key);
622 if (hash == -1) {
623 return NULL;
624 }
625 }
626
627 ep = (mp->ma_lookup)(mp, key, hash);
628 if (ep == NULL)
629 return NULL;
630 return ep->me_value;
631}
632
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000633/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
Tim Peters60b29962006-01-01 01:19:23 +0000634 * dictionary if it's merely replacing the value for an existing key.
635 * This means that it's safe to loop over a dictionary with PyDict_Next()
636 * and occasionally replace a value -- but you can't insert new keys or
637 * remove them.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000638 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000639int
Tim Peters1f5871e2000-07-04 17:44:48 +0000640PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000641{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000642 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000643 register long hash;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000644 register Py_ssize_t n_used;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000645
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000646 if (!PyDict_Check(op)) {
647 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000648 return -1;
649 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000650 assert(key);
651 assert(value);
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000652 mp = (dictobject *)op;
Guido van Rossum4d027722007-09-18 04:30:42 +0000653 if (!PyUnicode_CheckExact(key) ||
654 (hash = ((PyUnicodeObject *) key)->hash) == -1)
655 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000656 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000657 if (hash == -1)
658 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000659 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000660 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000661 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000662 Py_INCREF(value);
663 Py_INCREF(key);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000664 if (insertdict(mp, key, hash, value) != 0)
665 return -1;
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000666 /* If we added a key, we can safely resize. Otherwise just return!
667 * If fill >= 2/3 size, adjust size. Normally, this doubles or
668 * quaduples the size, but it's also possible for the dict to shrink
Tim Peters60b29962006-01-01 01:19:23 +0000669 * (if ma_fill is much larger than ma_used, meaning a lot of dict
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000670 * keys have been * deleted).
Tim Peters60b29962006-01-01 01:19:23 +0000671 *
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000672 * Quadrupling the size improves average dictionary sparseness
673 * (reducing collisions) at the cost of some memory and iteration
674 * speed (which loops over every possible entry). It also halves
675 * the number of expensive resize operations in a growing dictionary.
Tim Peters60b29962006-01-01 01:19:23 +0000676 *
677 * Very large dictionaries (over 50K items) use doubling instead.
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000678 * This may help applications with severe memory constraints.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000679 */
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000680 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
681 return 0;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000682 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000683}
684
685int
Tim Peters1f5871e2000-07-04 17:44:48 +0000686PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000687{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000688 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000689 register long hash;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000690 register dictentry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000691 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000692
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000693 if (!PyDict_Check(op)) {
694 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000695 return -1;
696 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000697 assert(key);
Tim Peters0ab085c2001-09-14 00:25:33 +0000698 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +0000699 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000700 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000701 if (hash == -1)
702 return -1;
703 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000704 mp = (dictobject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000705 ep = (mp->ma_lookup)(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000706 if (ep == NULL)
707 return -1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000708 if (ep->me_value == NULL) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000709 set_key_error(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000710 return -1;
711 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000712 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000713 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000714 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000715 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000716 ep->me_value = NULL;
717 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000718 Py_DECREF(old_value);
719 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000720 return 0;
721}
722
Guido van Rossum25831651993-05-19 14:50:45 +0000723void
Tim Peters1f5871e2000-07-04 17:44:48 +0000724PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000725{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000726 dictobject *mp;
Tim Petersdea48ec2001-05-22 20:40:22 +0000727 dictentry *ep, *table;
728 int table_is_malloced;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000729 Py_ssize_t fill;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000730 dictentry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000731#ifdef Py_DEBUG
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000732 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +0000733#endif
734
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000735 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000736 return;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000737 mp = (dictobject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000738#ifdef Py_DEBUG
Tim Petersafb6ae82001-06-04 21:00:21 +0000739 n = mp->ma_mask + 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000740 i = 0;
741#endif
742
743 table = mp->ma_table;
744 assert(table != NULL);
745 table_is_malloced = table != mp->ma_smalltable;
746
747 /* This is delicate. During the process of clearing the dict,
748 * decrefs can cause the dict to mutate. To avoid fatal confusion
749 * (voice of experience), we have to make the dict empty before
750 * clearing the slots, and never refer to anything via mp->xxx while
751 * clearing.
752 */
753 fill = mp->ma_fill;
754 if (table_is_malloced)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000755 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000756
757 else if (fill > 0) {
758 /* It's a small table with something that needs to be cleared.
759 * Afraid the only safe way is to copy the dict entries into
760 * another small table first.
761 */
762 memcpy(small_copy, table, sizeof(small_copy));
763 table = small_copy;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000764 EMPTY_TO_MINSIZE(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000765 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000766 /* else it's a small table that's already empty */
767
768 /* Now we can finally clear things. If C had refcounts, we could
769 * assert that the refcount on table is 1 now, i.e. that this function
770 * has unique access to it, so decref side-effects can't alter it.
771 */
772 for (ep = table; fill > 0; ++ep) {
773#ifdef Py_DEBUG
774 assert(i < n);
775 ++i;
776#endif
777 if (ep->me_key) {
778 --fill;
779 Py_DECREF(ep->me_key);
780 Py_XDECREF(ep->me_value);
781 }
782#ifdef Py_DEBUG
783 else
784 assert(ep->me_value == NULL);
785#endif
786 }
787
788 if (table_is_malloced)
789 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000790}
791
Tim Peters080c88b2003-02-15 03:01:11 +0000792/*
793 * Iterate over a dict. Use like so:
794 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000795 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +0000796 * PyObject *key, *value;
797 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +0000798 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +0000799 * Refer to borrowed references in key and value.
800 * }
801 *
802 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +0000803 * mutates the dict. One exception: it is safe if the loop merely changes
804 * the values associated with the keys (but doesn't insert new keys or
805 * delete keys), via PyDict_SetItem().
806 */
Guido van Rossum25831651993-05-19 14:50:45 +0000807int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000808PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000809{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000810 register Py_ssize_t i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000811 register Py_ssize_t mask;
Raymond Hettinger43442782004-03-17 21:55:03 +0000812 register dictentry *ep;
813
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000814 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000815 return 0;
Guido van Rossum25831651993-05-19 14:50:45 +0000816 i = *ppos;
817 if (i < 0)
818 return 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000819 ep = ((dictobject *)op)->ma_table;
820 mask = ((dictobject *)op)->ma_mask;
821 while (i <= mask && ep[i].me_value == NULL)
Guido van Rossum25831651993-05-19 14:50:45 +0000822 i++;
823 *ppos = i+1;
Raymond Hettinger43442782004-03-17 21:55:03 +0000824 if (i > mask)
Guido van Rossum25831651993-05-19 14:50:45 +0000825 return 0;
826 if (pkey)
Raymond Hettinger43442782004-03-17 21:55:03 +0000827 *pkey = ep[i].me_key;
Guido van Rossum25831651993-05-19 14:50:45 +0000828 if (pvalue)
Raymond Hettinger43442782004-03-17 21:55:03 +0000829 *pvalue = ep[i].me_value;
Guido van Rossum25831651993-05-19 14:50:45 +0000830 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000831}
832
Thomas Wouterscf297e42007-02-23 15:07:44 +0000833/* Internal version of PyDict_Next that returns a hash value in addition to the key and value.*/
834int
835_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, long *phash)
836{
837 register Py_ssize_t i;
838 register Py_ssize_t mask;
839 register dictentry *ep;
840
841 if (!PyDict_Check(op))
842 return 0;
843 i = *ppos;
844 if (i < 0)
845 return 0;
846 ep = ((dictobject *)op)->ma_table;
847 mask = ((dictobject *)op)->ma_mask;
848 while (i <= mask && ep[i].me_value == NULL)
849 i++;
850 *ppos = i+1;
851 if (i > mask)
852 return 0;
853 *phash = (long)(ep[i].me_hash);
854 if (pkey)
855 *pkey = ep[i].me_key;
856 if (pvalue)
857 *pvalue = ep[i].me_value;
858 return 1;
859}
860
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000861/* Methods */
862
863static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000864dict_dealloc(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000865{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000866 register dictentry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000867 Py_ssize_t fill = mp->ma_fill;
Guido van Rossumff413af2002-03-28 20:34:59 +0000868 PyObject_GC_UnTrack(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000869 Py_TRASHCAN_SAFE_BEGIN(mp)
Tim Petersdea48ec2001-05-22 20:40:22 +0000870 for (ep = mp->ma_table; fill > 0; ep++) {
871 if (ep->me_key) {
872 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000873 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000874 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000875 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000876 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000877 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000878 PyMem_DEL(mp->ma_table);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000879 if (num_free_dicts < MAXFREEDICTS && Py_Type(mp) == &PyDict_Type)
Raymond Hettinger43442782004-03-17 21:55:03 +0000880 free_dicts[num_free_dicts++] = mp;
Tim Peters60b29962006-01-01 01:19:23 +0000881 else
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000882 Py_Type(mp)->tp_free((PyObject *)mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000883 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000884}
885
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000886static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000887dict_repr(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000888{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000889 Py_ssize_t i;
Tim Petersa7259592001-06-16 05:11:17 +0000890 PyObject *s, *temp, *colon = NULL;
891 PyObject *pieces = NULL, *result = NULL;
892 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +0000893
Tim Petersa7259592001-06-16 05:11:17 +0000894 i = Py_ReprEnter((PyObject *)mp);
Guido van Rossum255443b1998-04-10 22:47:14 +0000895 if (i != 0) {
Walter Dörwald1ab83302007-05-18 17:15:44 +0000896 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
Guido van Rossum255443b1998-04-10 22:47:14 +0000897 }
898
Tim Petersa7259592001-06-16 05:11:17 +0000899 if (mp->ma_used == 0) {
Walter Dörwald1ab83302007-05-18 17:15:44 +0000900 result = PyUnicode_FromString("{}");
Tim Petersa7259592001-06-16 05:11:17 +0000901 goto Done;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000902 }
Tim Petersa7259592001-06-16 05:11:17 +0000903
904 pieces = PyList_New(0);
905 if (pieces == NULL)
906 goto Done;
907
Walter Dörwald1ab83302007-05-18 17:15:44 +0000908 colon = PyUnicode_FromString(": ");
Tim Petersa7259592001-06-16 05:11:17 +0000909 if (colon == NULL)
910 goto Done;
911
912 /* Do repr() on each key+value pair, and insert ": " between them.
913 Note that repr may mutate the dict. */
Tim Petersc6057842001-06-16 07:52:53 +0000914 i = 0;
915 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Tim Petersa7259592001-06-16 05:11:17 +0000916 int status;
917 /* Prevent repr from deleting value during key format. */
918 Py_INCREF(value);
919 s = PyObject_Repr(key);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000920 PyUnicode_Append(&s, colon);
921 PyUnicode_AppendAndDel(&s, PyObject_Repr(value));
Tim Petersa7259592001-06-16 05:11:17 +0000922 Py_DECREF(value);
923 if (s == NULL)
924 goto Done;
925 status = PyList_Append(pieces, s);
926 Py_DECREF(s); /* append created a new ref */
927 if (status < 0)
928 goto Done;
929 }
930
931 /* Add "{}" decorations to the first and last items. */
932 assert(PyList_GET_SIZE(pieces) > 0);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000933 s = PyUnicode_FromString("{");
Tim Petersa7259592001-06-16 05:11:17 +0000934 if (s == NULL)
935 goto Done;
936 temp = PyList_GET_ITEM(pieces, 0);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000937 PyUnicode_AppendAndDel(&s, temp);
Tim Petersa7259592001-06-16 05:11:17 +0000938 PyList_SET_ITEM(pieces, 0, s);
939 if (s == NULL)
940 goto Done;
941
Walter Dörwald1ab83302007-05-18 17:15:44 +0000942 s = PyUnicode_FromString("}");
Tim Petersa7259592001-06-16 05:11:17 +0000943 if (s == NULL)
944 goto Done;
945 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000946 PyUnicode_AppendAndDel(&temp, s);
Tim Petersa7259592001-06-16 05:11:17 +0000947 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
948 if (temp == NULL)
949 goto Done;
950
951 /* Paste them all together with ", " between. */
Walter Dörwald1ab83302007-05-18 17:15:44 +0000952 s = PyUnicode_FromString(", ");
Tim Petersa7259592001-06-16 05:11:17 +0000953 if (s == NULL)
954 goto Done;
Walter Dörwald1ab83302007-05-18 17:15:44 +0000955 result = PyUnicode_Join(s, pieces);
Tim Peters0ab085c2001-09-14 00:25:33 +0000956 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +0000957
958Done:
959 Py_XDECREF(pieces);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000960 Py_XDECREF(colon);
Tim Petersa7259592001-06-16 05:11:17 +0000961 Py_ReprLeave((PyObject *)mp);
962 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000963}
964
Martin v. Löwis18e16552006-02-15 17:27:45 +0000965static Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +0000966dict_length(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000967{
968 return mp->ma_used;
969}
970
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000971static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000972dict_subscript(dictobject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000973{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000974 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000975 long hash;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000976 dictentry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000977 assert(mp->ma_table != NULL);
Tim Peters0ab085c2001-09-14 00:25:33 +0000978 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +0000979 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000980 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000981 if (hash == -1)
982 return NULL;
983 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000984 ep = (mp->ma_lookup)(mp, key, hash);
985 if (ep == NULL)
986 return NULL;
987 v = ep->me_value;
Guido van Rossum1968ad32006-02-25 22:38:04 +0000988 if (v == NULL) {
989 if (!PyDict_CheckExact(mp)) {
990 /* Look up __missing__ method if we're a subclass. */
Guido van Rossum4b92a822006-02-25 23:32:30 +0000991 PyObject *missing;
Guido van Rossum1968ad32006-02-25 22:38:04 +0000992 static PyObject *missing_str = NULL;
993 if (missing_str == NULL)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000994 missing_str =
Martin v. Löwis5b222132007-06-10 09:51:05 +0000995 PyUnicode_InternFromString("__missing__");
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000996 missing = _PyType_Lookup(Py_Type(mp), missing_str);
Guido van Rossum1968ad32006-02-25 22:38:04 +0000997 if (missing != NULL)
998 return PyObject_CallFunctionObjArgs(missing,
999 (PyObject *)mp, key, NULL);
1000 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001001 set_key_error(key);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001002 return NULL;
1003 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001004 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001005 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001006 return v;
1007}
1008
1009static int
Tim Peters1f5871e2000-07-04 17:44:48 +00001010dict_ass_sub(dictobject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001011{
1012 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001013 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001014 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001015 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001016}
1017
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001018static PyMappingMethods dict_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001019 (lenfunc)dict_length, /*mp_length*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001020 (binaryfunc)dict_subscript, /*mp_subscript*/
1021 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001022};
1023
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001024static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001025dict_keys(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001026{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001027 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001028 register Py_ssize_t i, j;
Raymond Hettinger43442782004-03-17 21:55:03 +00001029 dictentry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001030 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001031
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001032 again:
1033 n = mp->ma_used;
1034 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001035 if (v == NULL)
1036 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001037 if (n != mp->ma_used) {
1038 /* Durnit. The allocations caused the dict to resize.
1039 * Just start over, this shouldn't normally happen.
1040 */
1041 Py_DECREF(v);
1042 goto again;
1043 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001044 ep = mp->ma_table;
1045 mask = mp->ma_mask;
1046 for (i = 0, j = 0; i <= mask; i++) {
1047 if (ep[i].me_value != NULL) {
1048 PyObject *key = ep[i].me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001049 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001050 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001051 j++;
1052 }
1053 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001054 assert(j == n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001055 return v;
1056}
1057
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001058static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001059dict_values(register dictobject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001060{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001061 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001062 register Py_ssize_t i, j;
Raymond Hettinger43442782004-03-17 21:55:03 +00001063 dictentry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001064 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001065
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001066 again:
1067 n = mp->ma_used;
1068 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001069 if (v == NULL)
1070 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001071 if (n != mp->ma_used) {
1072 /* Durnit. The allocations caused the dict to resize.
1073 * Just start over, this shouldn't normally happen.
1074 */
1075 Py_DECREF(v);
1076 goto again;
1077 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001078 ep = mp->ma_table;
1079 mask = mp->ma_mask;
1080 for (i = 0, j = 0; i <= mask; i++) {
1081 if (ep[i].me_value != NULL) {
1082 PyObject *value = ep[i].me_value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001083 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001084 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001085 j++;
1086 }
1087 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001088 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001089 return v;
1090}
1091
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001092static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001093dict_items(register dictobject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001094{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001095 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001096 register Py_ssize_t i, j, n;
1097 Py_ssize_t mask;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001098 PyObject *item, *key, *value;
Raymond Hettinger43442782004-03-17 21:55:03 +00001099 dictentry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001100
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001101 /* Preallocate the list of tuples, to avoid allocations during
1102 * the loop over the items, which could trigger GC, which
1103 * could resize the dict. :-(
1104 */
1105 again:
1106 n = mp->ma_used;
1107 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001108 if (v == NULL)
1109 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001110 for (i = 0; i < n; i++) {
1111 item = PyTuple_New(2);
1112 if (item == NULL) {
1113 Py_DECREF(v);
1114 return NULL;
1115 }
1116 PyList_SET_ITEM(v, i, item);
1117 }
1118 if (n != mp->ma_used) {
1119 /* Durnit. The allocations caused the dict to resize.
1120 * Just start over, this shouldn't normally happen.
1121 */
1122 Py_DECREF(v);
1123 goto again;
1124 }
1125 /* Nothing we do below makes any function calls. */
Raymond Hettinger43442782004-03-17 21:55:03 +00001126 ep = mp->ma_table;
1127 mask = mp->ma_mask;
1128 for (i = 0, j = 0; i <= mask; i++) {
Raymond Hettinger06905122004-03-19 10:30:00 +00001129 if ((value=ep[i].me_value) != NULL) {
Raymond Hettinger43442782004-03-17 21:55:03 +00001130 key = ep[i].me_key;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001131 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001132 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001133 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001134 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001135 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001136 j++;
1137 }
1138 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001139 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001140 return v;
1141}
1142
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001143static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001144dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001145{
1146 PyObject *seq;
1147 PyObject *value = Py_None;
1148 PyObject *it; /* iter(seq) */
1149 PyObject *key;
1150 PyObject *d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001151 int status;
1152
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001153 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001154 return NULL;
1155
1156 d = PyObject_CallObject(cls, NULL);
1157 if (d == NULL)
1158 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001159
Guido van Rossumd8faa362007-04-27 19:54:29 +00001160 if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
1161 dictobject *mp = (dictobject *)d;
1162 Py_ssize_t pos = 0;
1163 PyObject *key;
1164 long hash;
1165
1166 if (dictresize(mp, PySet_GET_SIZE(seq)))
1167 return NULL;
1168
1169 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
1170 Py_INCREF(key);
1171 Py_INCREF(value);
1172 if (insertdict(mp, key, hash, value))
1173 return NULL;
1174 }
1175 return d;
1176 }
1177
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001178 it = PyObject_GetIter(seq);
1179 if (it == NULL){
1180 Py_DECREF(d);
1181 return NULL;
1182 }
1183
1184 for (;;) {
1185 key = PyIter_Next(it);
1186 if (key == NULL) {
1187 if (PyErr_Occurred())
1188 goto Fail;
1189 break;
1190 }
Raymond Hettingere03e5b12002-12-07 08:10:51 +00001191 status = PyObject_SetItem(d, key, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001192 Py_DECREF(key);
1193 if (status < 0)
1194 goto Fail;
1195 }
1196
1197 Py_DECREF(it);
1198 return d;
1199
1200Fail:
1201 Py_DECREF(it);
1202 Py_DECREF(d);
1203 return NULL;
1204}
1205
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001206static int
1207dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001208{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001209 PyObject *arg = NULL;
1210 int result = 0;
1211
1212 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1213 result = -1;
1214
1215 else if (arg != NULL) {
1216 if (PyObject_HasAttrString(arg, "keys"))
1217 result = PyDict_Merge(self, arg, 1);
1218 else
1219 result = PyDict_MergeFromSeq2(self, arg, 1);
1220 }
1221 if (result == 0 && kwds != NULL)
1222 result = PyDict_Merge(self, kwds, 1);
1223 return result;
1224}
1225
1226static PyObject *
1227dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1228{
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001229 if (dict_update_common(self, args, kwds, "update") != -1)
1230 Py_RETURN_NONE;
1231 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001232}
1233
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001234/* Update unconditionally replaces existing items.
1235 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001236 otherwise it leaves existing items unchanged.
1237
1238 PyDict_{Update,Merge} update/merge from a mapping object.
1239
Tim Petersf582b822001-12-11 18:51:08 +00001240 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001241 producing iterable objects of length 2.
1242*/
1243
Tim Petersf582b822001-12-11 18:51:08 +00001244int
Tim Peters1fc240e2001-10-26 05:06:50 +00001245PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1246{
1247 PyObject *it; /* iter(seq2) */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001248 Py_ssize_t i; /* index into seq2 of current element */
Tim Peters1fc240e2001-10-26 05:06:50 +00001249 PyObject *item; /* seq2[i] */
1250 PyObject *fast; /* item as a 2-tuple or 2-list */
1251
1252 assert(d != NULL);
1253 assert(PyDict_Check(d));
1254 assert(seq2 != NULL);
1255
1256 it = PyObject_GetIter(seq2);
1257 if (it == NULL)
1258 return -1;
1259
1260 for (i = 0; ; ++i) {
1261 PyObject *key, *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001262 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001263
1264 fast = NULL;
1265 item = PyIter_Next(it);
1266 if (item == NULL) {
1267 if (PyErr_Occurred())
1268 goto Fail;
1269 break;
1270 }
1271
1272 /* Convert item to sequence, and verify length 2. */
1273 fast = PySequence_Fast(item, "");
1274 if (fast == NULL) {
1275 if (PyErr_ExceptionMatches(PyExc_TypeError))
1276 PyErr_Format(PyExc_TypeError,
1277 "cannot convert dictionary update "
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001278 "sequence element #%zd to a sequence",
Tim Peters1fc240e2001-10-26 05:06:50 +00001279 i);
1280 goto Fail;
1281 }
1282 n = PySequence_Fast_GET_SIZE(fast);
1283 if (n != 2) {
1284 PyErr_Format(PyExc_ValueError,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001285 "dictionary update sequence element #%zd "
Martin v. Löwis2c95cc62006-02-16 06:54:25 +00001286 "has length %zd; 2 is required",
Martin v. Löwise0e89f72006-02-16 06:59:22 +00001287 i, n);
Tim Peters1fc240e2001-10-26 05:06:50 +00001288 goto Fail;
1289 }
1290
1291 /* Update/merge with this (key, value) pair. */
1292 key = PySequence_Fast_GET_ITEM(fast, 0);
1293 value = PySequence_Fast_GET_ITEM(fast, 1);
1294 if (override || PyDict_GetItem(d, key) == NULL) {
1295 int status = PyDict_SetItem(d, key, value);
1296 if (status < 0)
1297 goto Fail;
1298 }
1299 Py_DECREF(fast);
1300 Py_DECREF(item);
1301 }
1302
1303 i = 0;
1304 goto Return;
1305Fail:
1306 Py_XDECREF(item);
1307 Py_XDECREF(fast);
1308 i = -1;
1309Return:
1310 Py_DECREF(it);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001311 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001312}
1313
Tim Peters6d6c1a32001-08-02 04:15:00 +00001314int
1315PyDict_Update(PyObject *a, PyObject *b)
1316{
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001317 return PyDict_Merge(a, b, 1);
1318}
1319
1320int
1321PyDict_Merge(PyObject *a, PyObject *b, int override)
1322{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001323 register PyDictObject *mp, *other;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001324 register Py_ssize_t i;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001325 dictentry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001326
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001327 /* We accept for the argument either a concrete dictionary object,
1328 * or an abstract "mapping" object. For the former, we can do
1329 * things quite efficiently. For the latter, we only require that
1330 * PyMapping_Keys() and PyObject_GetItem() be supported.
1331 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001332 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1333 PyErr_BadInternalCall();
1334 return -1;
1335 }
1336 mp = (dictobject*)a;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001337 if (PyDict_CheckExact(b)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001338 other = (dictobject*)b;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001339 if (other == mp || other->ma_used == 0)
1340 /* a.update(a) or a.update({}); nothing to do */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001341 return 0;
Raymond Hettinger186e7392005-05-14 18:08:25 +00001342 if (mp->ma_used == 0)
1343 /* Since the target dict is empty, PyDict_GetItem()
1344 * always returns NULL. Setting override to 1
1345 * skips the unnecessary test.
1346 */
1347 override = 1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001348 /* Do one big resize at the start, rather than
1349 * incrementally resizing as we insert new items. Expect
1350 * that there will be no (or few) overlapping keys.
1351 */
1352 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
Raymond Hettingerc8d22902003-05-07 00:49:40 +00001353 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001354 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001355 }
1356 for (i = 0; i <= other->ma_mask; i++) {
1357 entry = &other->ma_table[i];
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001358 if (entry->me_value != NULL &&
1359 (override ||
1360 PyDict_GetItem(a, entry->me_key) == NULL)) {
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001361 Py_INCREF(entry->me_key);
1362 Py_INCREF(entry->me_value);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001363 if (insertdict(mp, entry->me_key,
1364 (long)entry->me_hash,
1365 entry->me_value) != 0)
1366 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001367 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001368 }
1369 }
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001370 else {
1371 /* Do it the generic, slower way */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001372 PyObject *keys = PyMapping_Keys(b);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001373 PyObject *iter;
1374 PyObject *key, *value;
1375 int status;
1376
1377 if (keys == NULL)
1378 /* Docstring says this is equivalent to E.keys() so
1379 * if E doesn't have a .keys() method we want
1380 * AttributeError to percolate up. Might as well
1381 * do the same for any other error.
1382 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001383 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001384
1385 iter = PyObject_GetIter(keys);
1386 Py_DECREF(keys);
1387 if (iter == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001388 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001389
1390 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001391 if (!override && PyDict_GetItem(a, key) != NULL) {
1392 Py_DECREF(key);
1393 continue;
1394 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001395 value = PyObject_GetItem(b, key);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001396 if (value == NULL) {
1397 Py_DECREF(iter);
1398 Py_DECREF(key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001399 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001400 }
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001401 status = PyDict_SetItem(a, key, value);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001402 Py_DECREF(key);
1403 Py_DECREF(value);
1404 if (status < 0) {
1405 Py_DECREF(iter);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001406 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001407 }
1408 }
1409 Py_DECREF(iter);
1410 if (PyErr_Occurred())
1411 /* Iterator completed, via error */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001412 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001413 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001414 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001415}
1416
1417static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001418dict_copy(register dictobject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001419{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001420 return PyDict_Copy((PyObject*)mp);
1421}
1422
1423PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001424PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001425{
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001426 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001427
1428 if (o == NULL || !PyDict_Check(o)) {
1429 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001430 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001431 }
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001432 copy = PyDict_New();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001433 if (copy == NULL)
1434 return NULL;
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001435 if (PyDict_Merge(copy, o, 1) == 0)
1436 return copy;
1437 Py_DECREF(copy);
Raymond Hettinger1356f782005-04-15 15:58:42 +00001438 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001439}
1440
Martin v. Löwis18e16552006-02-15 17:27:45 +00001441Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001442PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001443{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001444 if (mp == NULL || !PyDict_Check(mp)) {
1445 PyErr_BadInternalCall();
Jeremy Hylton7083bb72004-02-17 20:10:11 +00001446 return -1;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001447 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001448 return ((dictobject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001449}
1450
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001451PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001452PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001453{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001454 if (mp == NULL || !PyDict_Check(mp)) {
1455 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001456 return NULL;
1457 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001458 return dict_keys((dictobject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001459}
1460
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001461PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001462PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001463{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001464 if (mp == NULL || !PyDict_Check(mp)) {
1465 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001466 return NULL;
1467 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001468 return dict_values((dictobject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001469}
1470
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001471PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001472PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001473{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001474 if (mp == NULL || !PyDict_Check(mp)) {
1475 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001476 return NULL;
1477 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001478 return dict_items((dictobject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001479}
1480
Tim Peterse63415e2001-05-08 04:38:29 +00001481/* Return 1 if dicts equal, 0 if not, -1 if error.
1482 * Gets out as soon as any difference is detected.
1483 * Uses only Py_EQ comparison.
1484 */
1485static int
1486dict_equal(dictobject *a, dictobject *b)
1487{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001488 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00001489
1490 if (a->ma_used != b->ma_used)
1491 /* can't be equal if # of entries differ */
1492 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001493
Tim Peterse63415e2001-05-08 04:38:29 +00001494 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Tim Petersafb6ae82001-06-04 21:00:21 +00001495 for (i = 0; i <= a->ma_mask; i++) {
Tim Peterse63415e2001-05-08 04:38:29 +00001496 PyObject *aval = a->ma_table[i].me_value;
1497 if (aval != NULL) {
1498 int cmp;
1499 PyObject *bval;
1500 PyObject *key = a->ma_table[i].me_key;
1501 /* temporarily bump aval's refcount to ensure it stays
1502 alive until we're done with it */
1503 Py_INCREF(aval);
Guido van Rossumdc5f6b22006-08-24 21:29:26 +00001504 /* ditto for key */
1505 Py_INCREF(key);
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001506 bval = PyDict_GetItemWithError((PyObject *)b, key);
Guido van Rossumdc5f6b22006-08-24 21:29:26 +00001507 Py_DECREF(key);
Tim Peterse63415e2001-05-08 04:38:29 +00001508 if (bval == NULL) {
1509 Py_DECREF(aval);
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001510 if (PyErr_Occurred())
1511 return -1;
Tim Peterse63415e2001-05-08 04:38:29 +00001512 return 0;
1513 }
1514 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1515 Py_DECREF(aval);
1516 if (cmp <= 0) /* error or not equal */
1517 return cmp;
1518 }
1519 }
1520 return 1;
1521 }
1522
1523static PyObject *
1524dict_richcompare(PyObject *v, PyObject *w, int op)
1525{
1526 int cmp;
1527 PyObject *res;
1528
1529 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1530 res = Py_NotImplemented;
1531 }
1532 else if (op == Py_EQ || op == Py_NE) {
1533 cmp = dict_equal((dictobject *)v, (dictobject *)w);
1534 if (cmp < 0)
1535 return NULL;
1536 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1537 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001538 else
1539 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001540 Py_INCREF(res);
1541 return res;
1542 }
1543
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001544static PyObject *
Guido van Rossume2b70bc2006-08-18 22:13:04 +00001545dict_contains(register dictobject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001546{
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001547 long hash;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001548 dictentry *ep;
1549
Tim Peters0ab085c2001-09-14 00:25:33 +00001550 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001551 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001552 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001553 if (hash == -1)
1554 return NULL;
1555 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001556 ep = (mp->ma_lookup)(mp, key, hash);
1557 if (ep == NULL)
1558 return NULL;
1559 return PyBool_FromLong(ep->me_value != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001560}
1561
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001562static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001563dict_get(register dictobject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001564{
1565 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001566 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001567 PyObject *val = NULL;
1568 long hash;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001569 dictentry *ep;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001570
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001571 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001572 return NULL;
1573
Tim Peters0ab085c2001-09-14 00:25:33 +00001574 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001575 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Barry Warsawc38c5da1997-10-06 17:49:20 +00001576 hash = PyObject_Hash(key);
1577 if (hash == -1)
1578 return NULL;
1579 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001580 ep = (mp->ma_lookup)(mp, key, hash);
1581 if (ep == NULL)
1582 return NULL;
1583 val = ep->me_value;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001584 if (val == NULL)
1585 val = failobj;
1586 Py_INCREF(val);
1587 return val;
1588}
1589
1590
1591static PyObject *
Guido van Rossum164452c2000-08-08 16:12:54 +00001592dict_setdefault(register dictobject *mp, PyObject *args)
1593{
1594 PyObject *key;
1595 PyObject *failobj = Py_None;
1596 PyObject *val = NULL;
1597 long hash;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001598 dictentry *ep;
Guido van Rossum164452c2000-08-08 16:12:54 +00001599
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001600 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
Guido van Rossum164452c2000-08-08 16:12:54 +00001601 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001602
Tim Peters0ab085c2001-09-14 00:25:33 +00001603 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001604 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum164452c2000-08-08 16:12:54 +00001605 hash = PyObject_Hash(key);
1606 if (hash == -1)
1607 return NULL;
1608 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001609 ep = (mp->ma_lookup)(mp, key, hash);
1610 if (ep == NULL)
1611 return NULL;
1612 val = ep->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001613 if (val == NULL) {
1614 val = failobj;
1615 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1616 val = NULL;
1617 }
1618 Py_XINCREF(val);
1619 return val;
1620}
1621
1622
1623static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001624dict_clear(register dictobject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001625{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001626 PyDict_Clear((PyObject *)mp);
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001627 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001628}
1629
Guido van Rossumba6ab842000-12-12 22:02:18 +00001630static PyObject *
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001631dict_pop(dictobject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00001632{
1633 long hash;
1634 dictentry *ep;
1635 PyObject *old_value, *old_key;
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001636 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001637
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001638 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1639 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001640 if (mp->ma_used == 0) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001641 if (deflt) {
1642 Py_INCREF(deflt);
1643 return deflt;
1644 }
Guido van Rossume027d982002-04-12 15:11:59 +00001645 PyErr_SetString(PyExc_KeyError,
1646 "pop(): dictionary is empty");
1647 return NULL;
1648 }
1649 if (!PyString_CheckExact(key) ||
1650 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
1651 hash = PyObject_Hash(key);
1652 if (hash == -1)
1653 return NULL;
1654 }
1655 ep = (mp->ma_lookup)(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001656 if (ep == NULL)
1657 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001658 if (ep->me_value == NULL) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001659 if (deflt) {
1660 Py_INCREF(deflt);
1661 return deflt;
1662 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001663 set_key_error(key);
Guido van Rossume027d982002-04-12 15:11:59 +00001664 return NULL;
1665 }
1666 old_key = ep->me_key;
1667 Py_INCREF(dummy);
1668 ep->me_key = dummy;
1669 old_value = ep->me_value;
1670 ep->me_value = NULL;
1671 mp->ma_used--;
1672 Py_DECREF(old_key);
1673 return old_value;
1674}
1675
1676static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001677dict_popitem(dictobject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001678{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001679 Py_ssize_t i = 0;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001680 dictentry *ep;
1681 PyObject *res;
1682
Tim Petersf4b33f62001-06-02 05:42:29 +00001683 /* Allocate the result tuple before checking the size. Believe it
1684 * or not, this allocation could trigger a garbage collection which
1685 * could empty the dict, so if we checked the size first and that
1686 * happened, the result would be an infinite loop (searching for an
1687 * entry that no longer exists). Note that the usual popitem()
1688 * idiom is "while d: k, v = d.popitem()". so needing to throw the
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001689 * tuple away if the dict *is* empty isn't a significant
Tim Petersf4b33f62001-06-02 05:42:29 +00001690 * inefficiency -- possible, but unlikely in practice.
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001691 */
1692 res = PyTuple_New(2);
1693 if (res == NULL)
1694 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001695 if (mp->ma_used == 0) {
1696 Py_DECREF(res);
1697 PyErr_SetString(PyExc_KeyError,
1698 "popitem(): dictionary is empty");
1699 return NULL;
1700 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001701 /* Set ep to "the first" dict entry with a value. We abuse the hash
1702 * field of slot 0 to hold a search finger:
1703 * If slot 0 has a value, use slot 0.
1704 * Else slot 0 is being used to hold a search finger,
1705 * and we use its hash value as the first index to look.
1706 */
1707 ep = &mp->ma_table[0];
1708 if (ep->me_value == NULL) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001709 i = ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001710 /* The hash field may be a real hash value, or it may be a
1711 * legit search finger, or it may be a once-legit search
1712 * finger that's out of bounds now because it wrapped around
1713 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001714 */
Tim Petersafb6ae82001-06-04 21:00:21 +00001715 if (i > mp->ma_mask || i < 1)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001716 i = 1; /* skip slot 0 */
1717 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1718 i++;
Tim Petersafb6ae82001-06-04 21:00:21 +00001719 if (i > mp->ma_mask)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001720 i = 1;
1721 }
1722 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001723 PyTuple_SET_ITEM(res, 0, ep->me_key);
1724 PyTuple_SET_ITEM(res, 1, ep->me_value);
1725 Py_INCREF(dummy);
1726 ep->me_key = dummy;
1727 ep->me_value = NULL;
1728 mp->ma_used--;
1729 assert(mp->ma_table[0].me_value == NULL);
1730 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001731 return res;
1732}
1733
Jeremy Hylton8caad492000-06-23 14:18:11 +00001734static int
1735dict_traverse(PyObject *op, visitproc visit, void *arg)
1736{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001737 Py_ssize_t i = 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001738 PyObject *pk;
1739 PyObject *pv;
1740
1741 while (PyDict_Next(op, &i, &pk, &pv)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001742 Py_VISIT(pk);
1743 Py_VISIT(pv);
Jeremy Hylton8caad492000-06-23 14:18:11 +00001744 }
1745 return 0;
1746}
1747
1748static int
1749dict_tp_clear(PyObject *op)
1750{
1751 PyDict_Clear(op);
1752 return 0;
1753}
1754
Tim Petersf7f88b12000-12-13 23:18:45 +00001755
Raymond Hettinger019a1482004-03-18 02:41:19 +00001756extern PyTypeObject PyDictIterKey_Type; /* Forward */
1757extern PyTypeObject PyDictIterValue_Type; /* Forward */
1758extern PyTypeObject PyDictIterItem_Type; /* Forward */
1759static PyObject *dictiter_new(dictobject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001760
Guido van Rossum09e563a2001-05-01 12:10:21 +00001761
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001762PyDoc_STRVAR(contains__doc__,
1763"D.__contains__(k) -> True if D has a key k, else False");
1764
1765PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
1766
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001767PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001768"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001769
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001770PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001771"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 +00001772
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001773PyDoc_STRVAR(pop__doc__,
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001774"D.pop(k[,d]) -> v, remove specified key and return the corresponding value\n\
1775If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00001776
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001777PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00001778"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000017792-tuple; but raise KeyError if D is empty");
Tim Petersf7f88b12000-12-13 23:18:45 +00001780
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001781PyDoc_STRVAR(update__doc__,
Guido van Rossumb90c8482007-02-10 01:11:45 +00001782"D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k]\
1783\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 +00001784
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001785PyDoc_STRVAR(fromkeys__doc__,
1786"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
1787v defaults to None.");
1788
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001789PyDoc_STRVAR(clear__doc__,
1790"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001791
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001792PyDoc_STRVAR(copy__doc__,
1793"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00001794
Guido van Rossumb90c8482007-02-10 01:11:45 +00001795/* Forward */
1796static PyObject *dictkeys_new(PyObject *);
1797static PyObject *dictitems_new(PyObject *);
1798static PyObject *dictvalues_new(PyObject *);
1799
Guido van Rossum45c85d12007-07-27 16:31:40 +00001800PyDoc_STRVAR(keys__doc__,
1801 "D.keys() -> a set-like object providing a view on D's keys");
1802PyDoc_STRVAR(items__doc__,
1803 "D.items() -> a set-like object providing a view on D's items");
1804PyDoc_STRVAR(values__doc__,
1805 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00001806
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001807static PyMethodDef mapp_methods[] = {
Guido van Rossume2b70bc2006-08-18 22:13:04 +00001808 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001809 contains__doc__},
Raymond Hettinger0c669672003-12-13 13:31:55 +00001810 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001811 getitem__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00001812 {"get", (PyCFunction)dict_get, METH_VARARGS,
1813 get__doc__},
1814 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1815 setdefault_doc__},
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001816 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
Just van Rossuma797d812002-11-23 09:45:04 +00001817 pop__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001818 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001819 popitem__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001820 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00001821 keys__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001822 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00001823 items__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001824 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00001825 values__doc__},
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001826 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001827 update__doc__},
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001828 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
1829 fromkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001830 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001831 clear__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001832 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001833 copy__doc__},
1834 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001835};
1836
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001837/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001838int
1839PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001840{
1841 long hash;
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001842 dictobject *mp = (dictobject *)op;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001843 dictentry *ep;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001844
Tim Peters0ab085c2001-09-14 00:25:33 +00001845 if (!PyString_CheckExact(key) ||
Tim Peters1f7df352002-03-29 03:29:08 +00001846 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001847 hash = PyObject_Hash(key);
1848 if (hash == -1)
1849 return -1;
1850 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001851 ep = (mp->ma_lookup)(mp, key, hash);
1852 return ep == NULL ? -1 : (ep->me_value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001853}
1854
Thomas Wouterscf297e42007-02-23 15:07:44 +00001855/* Internal version of PyDict_Contains used when the hash value is already known */
1856int
1857_PyDict_Contains(PyObject *op, PyObject *key, long hash)
1858{
1859 dictobject *mp = (dictobject *)op;
1860 dictentry *ep;
1861
1862 ep = (mp->ma_lookup)(mp, key, hash);
1863 return ep == NULL ? -1 : (ep->me_value != NULL);
1864}
1865
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001866/* Hack to implement "key in dict" */
1867static PySequenceMethods dict_as_sequence = {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001868 0, /* sq_length */
1869 0, /* sq_concat */
1870 0, /* sq_repeat */
1871 0, /* sq_item */
1872 0, /* sq_slice */
1873 0, /* sq_ass_item */
1874 0, /* sq_ass_slice */
1875 PyDict_Contains, /* sq_contains */
1876 0, /* sq_inplace_concat */
1877 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001878};
1879
Guido van Rossum09e563a2001-05-01 12:10:21 +00001880static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001881dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1882{
1883 PyObject *self;
1884
1885 assert(type != NULL && type->tp_alloc != NULL);
1886 self = type->tp_alloc(type, 0);
1887 if (self != NULL) {
1888 PyDictObject *d = (PyDictObject *)self;
1889 /* It's guaranteed that tp->alloc zeroed out the struct. */
1890 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
1891 INIT_NONZERO_DICT_SLOTS(d);
1892 d->ma_lookup = lookdict_string;
1893#ifdef SHOW_CONVERSION_COUNTS
1894 ++created;
1895#endif
1896 }
1897 return self;
1898}
1899
Tim Peters25786c02001-09-02 08:22:48 +00001900static int
1901dict_init(PyObject *self, PyObject *args, PyObject *kwds)
1902{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001903 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00001904}
1905
Tim Peters6d6c1a32001-08-02 04:15:00 +00001906static PyObject *
Guido van Rossum09e563a2001-05-01 12:10:21 +00001907dict_iter(dictobject *dict)
1908{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001909 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001910}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001911
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001912PyDoc_STRVAR(dictionary_doc,
Tim Petersa427a2b2001-10-29 22:25:45 +00001913"dict() -> new empty dictionary.\n"
1914"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Tim Peters1fc240e2001-10-26 05:06:50 +00001915" (key, value) pairs.\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00001916"dict(seq) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00001917" d = {}\n"
1918" for k, v in seq:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00001919" d[k] = v\n"
1920"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
1921" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00001922
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001923PyTypeObject PyDict_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001924 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Petersa427a2b2001-10-29 22:25:45 +00001925 "dict",
Neil Schemenauere83c00e2001-08-29 23:54:21 +00001926 sizeof(dictobject),
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001927 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00001928 (destructor)dict_dealloc, /* tp_dealloc */
Guido van Rossum04dbf3b2007-08-07 19:51:00 +00001929 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001930 0, /* tp_getattr */
Guido van Rossumb9324202001-01-18 00:39:02 +00001931 0, /* tp_setattr */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001932 0, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00001933 (reprfunc)dict_repr, /* tp_repr */
1934 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001935 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00001936 &dict_as_mapping, /* tp_as_mapping */
Guido van Rossum50e9fb92006-08-17 05:42:55 +00001937 0, /* tp_hash */
Guido van Rossumb9324202001-01-18 00:39:02 +00001938 0, /* tp_call */
1939 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001940 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumb9324202001-01-18 00:39:02 +00001941 0, /* tp_setattro */
1942 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00001943 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Thomas Wouters27d517b2007-02-25 20:39:11 +00001944 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
Tim Peters25786c02001-09-02 08:22:48 +00001945 dictionary_doc, /* tp_doc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001946 dict_traverse, /* tp_traverse */
1947 dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00001948 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001949 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00001950 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001951 0, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001952 mapp_methods, /* tp_methods */
1953 0, /* tp_members */
1954 0, /* tp_getset */
1955 0, /* tp_base */
1956 0, /* tp_dict */
1957 0, /* tp_descr_get */
1958 0, /* tp_descr_set */
1959 0, /* tp_dictoffset */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001960 dict_init, /* tp_init */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001961 PyType_GenericAlloc, /* tp_alloc */
1962 dict_new, /* tp_new */
Neil Schemenauer6189b892002-04-12 02:43:00 +00001963 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001964};
1965
Guido van Rossum3cca2451997-05-16 14:23:33 +00001966/* For backward compatibility with old dictionary interface */
1967
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001968PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00001969PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001970{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001971 PyObject *kv, *rv;
Martin v. Löwis5b222132007-06-10 09:51:05 +00001972 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00001973 if (kv == NULL)
1974 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00001975 rv = PyDict_GetItem(v, kv);
1976 Py_DECREF(kv);
1977 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001978}
1979
1980int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00001981PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001982{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001983 PyObject *kv;
1984 int err;
Martin v. Löwis5b222132007-06-10 09:51:05 +00001985 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00001986 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00001987 return -1;
Martin v. Löwis5b222132007-06-10 09:51:05 +00001988 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00001989 err = PyDict_SetItem(v, kv, item);
1990 Py_DECREF(kv);
1991 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001992}
1993
1994int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00001995PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001996{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001997 PyObject *kv;
1998 int err;
Martin v. Löwis5b222132007-06-10 09:51:05 +00001999 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002000 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002001 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002002 err = PyDict_DelItem(v, kv);
2003 Py_DECREF(kv);
2004 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002005}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002006
Raymond Hettinger019a1482004-03-18 02:41:19 +00002007/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002008
2009typedef struct {
2010 PyObject_HEAD
Guido van Rossum2147df72002-07-16 20:30:22 +00002011 dictobject *di_dict; /* Set to NULL when iterator is exhausted */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002012 Py_ssize_t di_used;
2013 Py_ssize_t di_pos;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002014 PyObject* di_result; /* reusable result tuple for iteritems */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002015 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002016} dictiterobject;
2017
2018static PyObject *
Raymond Hettinger019a1482004-03-18 02:41:19 +00002019dictiter_new(dictobject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002020{
2021 dictiterobject *di;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002022 di = PyObject_New(dictiterobject, itertype);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002023 if (di == NULL)
2024 return NULL;
2025 Py_INCREF(dict);
2026 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00002027 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002028 di->di_pos = 0;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002029 di->len = dict->ma_used;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002030 if (itertype == &PyDictIterItem_Type) {
2031 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2032 if (di->di_result == NULL) {
2033 Py_DECREF(di);
2034 return NULL;
2035 }
2036 }
2037 else
2038 di->di_result = NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002039 return (PyObject *)di;
2040}
2041
2042static void
2043dictiter_dealloc(dictiterobject *di)
2044{
Guido van Rossum2147df72002-07-16 20:30:22 +00002045 Py_XDECREF(di->di_dict);
Raymond Hettinger019a1482004-03-18 02:41:19 +00002046 Py_XDECREF(di->di_result);
Neil Schemenauer6189b892002-04-12 02:43:00 +00002047 PyObject_Del(di);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002048}
2049
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002050static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002051dictiter_len(dictiterobject *di)
2052{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002053 Py_ssize_t len = 0;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00002054 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002055 len = di->len;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002056 return PyInt_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002057}
2058
Guido van Rossumb90c8482007-02-10 01:11:45 +00002059PyDoc_STRVAR(length_hint_doc,
2060 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002061
2062static PyMethodDef dictiter_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002063 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
2064 length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002065 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002066};
2067
Raymond Hettinger019a1482004-03-18 02:41:19 +00002068static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002069{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002070 PyObject *key;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002071 register Py_ssize_t i, mask;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002072 register dictentry *ep;
2073 dictobject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002074
Raymond Hettinger019a1482004-03-18 02:41:19 +00002075 if (d == NULL)
Guido van Rossum2147df72002-07-16 20:30:22 +00002076 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002077 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002078
Raymond Hettinger019a1482004-03-18 02:41:19 +00002079 if (di->di_used != d->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00002080 PyErr_SetString(PyExc_RuntimeError,
2081 "dictionary changed size during iteration");
Guido van Rossum2147df72002-07-16 20:30:22 +00002082 di->di_used = -1; /* Make this state sticky */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002083 return NULL;
2084 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002085
Raymond Hettinger019a1482004-03-18 02:41:19 +00002086 i = di->di_pos;
2087 if (i < 0)
2088 goto fail;
2089 ep = d->ma_table;
2090 mask = d->ma_mask;
2091 while (i <= mask && ep[i].me_value == NULL)
2092 i++;
2093 di->di_pos = i+1;
2094 if (i > mask)
2095 goto fail;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002096 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002097 key = ep[i].me_key;
2098 Py_INCREF(key);
2099 return key;
2100
2101fail:
2102 Py_DECREF(d);
Guido van Rossum2147df72002-07-16 20:30:22 +00002103 di->di_dict = NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002104 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002105}
2106
Raymond Hettinger019a1482004-03-18 02:41:19 +00002107PyTypeObject PyDictIterKey_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002108 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002109 "dictionary-keyiterator", /* tp_name */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002110 sizeof(dictiterobject), /* tp_basicsize */
2111 0, /* tp_itemsize */
2112 /* methods */
2113 (destructor)dictiter_dealloc, /* tp_dealloc */
2114 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002115 0, /* tp_getattr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002116 0, /* tp_setattr */
2117 0, /* tp_compare */
2118 0, /* tp_repr */
2119 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002120 0, /* tp_as_sequence */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002121 0, /* tp_as_mapping */
2122 0, /* tp_hash */
2123 0, /* tp_call */
2124 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002125 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002126 0, /* tp_setattro */
2127 0, /* tp_as_buffer */
2128 Py_TPFLAGS_DEFAULT, /* tp_flags */
2129 0, /* tp_doc */
2130 0, /* tp_traverse */
2131 0, /* tp_clear */
2132 0, /* tp_richcompare */
2133 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002134 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002135 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002136 dictiter_methods, /* tp_methods */
2137 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002138};
2139
2140static PyObject *dictiter_iternextvalue(dictiterobject *di)
2141{
2142 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002143 register Py_ssize_t i, mask;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002144 register dictentry *ep;
2145 dictobject *d = di->di_dict;
2146
2147 if (d == NULL)
2148 return NULL;
2149 assert (PyDict_Check(d));
2150
2151 if (di->di_used != d->ma_used) {
2152 PyErr_SetString(PyExc_RuntimeError,
2153 "dictionary changed size during iteration");
2154 di->di_used = -1; /* Make this state sticky */
2155 return NULL;
2156 }
2157
2158 i = di->di_pos;
Guido van Rossum09240f62004-03-20 19:11:58 +00002159 mask = d->ma_mask;
2160 if (i < 0 || i > mask)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002161 goto fail;
2162 ep = d->ma_table;
Guido van Rossum09240f62004-03-20 19:11:58 +00002163 while ((value=ep[i].me_value) == NULL) {
Raymond Hettinger019a1482004-03-18 02:41:19 +00002164 i++;
Guido van Rossum09240f62004-03-20 19:11:58 +00002165 if (i > mask)
2166 goto fail;
2167 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002168 di->di_pos = i+1;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002169 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002170 Py_INCREF(value);
2171 return value;
2172
2173fail:
2174 Py_DECREF(d);
2175 di->di_dict = NULL;
2176 return NULL;
2177}
2178
2179PyTypeObject PyDictIterValue_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002180 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002181 "dictionary-valueiterator", /* tp_name */
2182 sizeof(dictiterobject), /* tp_basicsize */
2183 0, /* tp_itemsize */
2184 /* methods */
2185 (destructor)dictiter_dealloc, /* tp_dealloc */
2186 0, /* tp_print */
2187 0, /* tp_getattr */
2188 0, /* tp_setattr */
2189 0, /* tp_compare */
2190 0, /* tp_repr */
2191 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002192 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002193 0, /* tp_as_mapping */
2194 0, /* tp_hash */
2195 0, /* tp_call */
2196 0, /* tp_str */
2197 PyObject_GenericGetAttr, /* tp_getattro */
2198 0, /* tp_setattro */
2199 0, /* tp_as_buffer */
2200 Py_TPFLAGS_DEFAULT, /* tp_flags */
2201 0, /* tp_doc */
2202 0, /* tp_traverse */
2203 0, /* tp_clear */
2204 0, /* tp_richcompare */
2205 0, /* tp_weaklistoffset */
2206 PyObject_SelfIter, /* tp_iter */
2207 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002208 dictiter_methods, /* tp_methods */
2209 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002210};
2211
2212static PyObject *dictiter_iternextitem(dictiterobject *di)
2213{
2214 PyObject *key, *value, *result = di->di_result;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002215 register Py_ssize_t i, mask;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002216 register dictentry *ep;
2217 dictobject *d = di->di_dict;
2218
2219 if (d == NULL)
2220 return NULL;
2221 assert (PyDict_Check(d));
2222
2223 if (di->di_used != d->ma_used) {
2224 PyErr_SetString(PyExc_RuntimeError,
2225 "dictionary changed size during iteration");
2226 di->di_used = -1; /* Make this state sticky */
2227 return NULL;
2228 }
2229
2230 i = di->di_pos;
2231 if (i < 0)
2232 goto fail;
2233 ep = d->ma_table;
2234 mask = d->ma_mask;
2235 while (i <= mask && ep[i].me_value == NULL)
2236 i++;
2237 di->di_pos = i+1;
2238 if (i > mask)
2239 goto fail;
2240
2241 if (result->ob_refcnt == 1) {
2242 Py_INCREF(result);
2243 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2244 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2245 } else {
2246 result = PyTuple_New(2);
Tim Peters60b29962006-01-01 01:19:23 +00002247 if (result == NULL)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002248 return NULL;
2249 }
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002250 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002251 key = ep[i].me_key;
2252 value = ep[i].me_value;
2253 Py_INCREF(key);
2254 Py_INCREF(value);
2255 PyTuple_SET_ITEM(result, 0, key);
2256 PyTuple_SET_ITEM(result, 1, value);
2257 return result;
2258
2259fail:
2260 Py_DECREF(d);
2261 di->di_dict = NULL;
2262 return NULL;
2263}
2264
2265PyTypeObject PyDictIterItem_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002266 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002267 "dictionary-itemiterator", /* tp_name */
2268 sizeof(dictiterobject), /* tp_basicsize */
2269 0, /* tp_itemsize */
2270 /* methods */
2271 (destructor)dictiter_dealloc, /* tp_dealloc */
2272 0, /* tp_print */
2273 0, /* tp_getattr */
2274 0, /* tp_setattr */
2275 0, /* tp_compare */
2276 0, /* tp_repr */
2277 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002278 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002279 0, /* tp_as_mapping */
2280 0, /* tp_hash */
2281 0, /* tp_call */
2282 0, /* tp_str */
2283 PyObject_GenericGetAttr, /* tp_getattro */
2284 0, /* tp_setattro */
2285 0, /* tp_as_buffer */
2286 Py_TPFLAGS_DEFAULT, /* tp_flags */
2287 0, /* tp_doc */
2288 0, /* tp_traverse */
2289 0, /* tp_clear */
2290 0, /* tp_richcompare */
2291 0, /* tp_weaklistoffset */
2292 PyObject_SelfIter, /* tp_iter */
2293 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002294 dictiter_methods, /* tp_methods */
2295 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002296};
Guido van Rossumb90c8482007-02-10 01:11:45 +00002297
2298
Guido van Rossum3ac67412007-02-10 18:55:06 +00002299/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002300/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002301/***********************************************/
2302
Guido van Rossumb90c8482007-02-10 01:11:45 +00002303/* The instance lay-out is the same for all three; but the type differs. */
2304
2305typedef struct {
2306 PyObject_HEAD
Guido van Rossum3ac67412007-02-10 18:55:06 +00002307 dictobject *dv_dict;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002308} dictviewobject;
2309
2310
2311static void
Guido van Rossum3ac67412007-02-10 18:55:06 +00002312dictview_dealloc(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002313{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002314 Py_XDECREF(dv->dv_dict);
2315 PyObject_Del(dv);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002316}
2317
Guido van Rossum83825ac2007-02-10 04:54:19 +00002318static Py_ssize_t
Guido van Rossum3ac67412007-02-10 18:55:06 +00002319dictview_len(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002320{
2321 Py_ssize_t len = 0;
Guido van Rossum3ac67412007-02-10 18:55:06 +00002322 if (dv->dv_dict != NULL)
2323 len = dv->dv_dict->ma_used;
Guido van Rossum83825ac2007-02-10 04:54:19 +00002324 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002325}
2326
2327static PyObject *
2328dictview_new(PyObject *dict, PyTypeObject *type)
2329{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002330 dictviewobject *dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002331 if (dict == NULL) {
2332 PyErr_BadInternalCall();
2333 return NULL;
2334 }
2335 if (!PyDict_Check(dict)) {
2336 /* XXX Get rid of this restriction later */
2337 PyErr_Format(PyExc_TypeError,
2338 "%s() requires a dict argument, not '%s'",
2339 type->tp_name, dict->ob_type->tp_name);
2340 return NULL;
2341 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002342 dv = PyObject_New(dictviewobject, type);
2343 if (dv == NULL)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002344 return NULL;
2345 Py_INCREF(dict);
Guido van Rossum3ac67412007-02-10 18:55:06 +00002346 dv->dv_dict = (dictobject *)dict;
2347 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002348}
2349
Neal Norwitze36f2ba2007-02-26 23:12:28 +00002350/* TODO(guido): The views objects are not complete:
2351
2352 * support more set operations
2353 * support arbitrary mappings?
2354 - either these should be static or exported in dictobject.h
2355 - if public then they should probably be in builtins
2356*/
2357
Guido van Rossumd9214d12007-02-12 02:23:40 +00002358/* Forward */
2359PyTypeObject PyDictKeys_Type;
2360PyTypeObject PyDictItems_Type;
2361PyTypeObject PyDictValues_Type;
2362
2363#define PyDictKeys_Check(obj) ((obj)->ob_type == &PyDictKeys_Type)
2364#define PyDictItems_Check(obj) ((obj)->ob_type == &PyDictItems_Type)
2365#define PyDictValues_Check(obj) ((obj)->ob_type == &PyDictValues_Type)
2366
2367/* This excludes Values, since they are not sets. */
2368# define PyDictViewSet_Check(obj) \
2369 (PyDictKeys_Check(obj) || PyDictItems_Check(obj))
2370
Guido van Rossumaac530c2007-08-24 22:33:45 +00002371/* Return 1 if self is a subset of other, iterating over self;
2372 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002373static int
2374all_contained_in(PyObject *self, PyObject *other)
2375{
2376 PyObject *iter = PyObject_GetIter(self);
2377 int ok = 1;
2378
2379 if (iter == NULL)
2380 return -1;
2381 for (;;) {
2382 PyObject *next = PyIter_Next(iter);
2383 if (next == NULL) {
2384 if (PyErr_Occurred())
2385 ok = -1;
2386 break;
2387 }
2388 ok = PySequence_Contains(other, next);
2389 Py_DECREF(next);
2390 if (ok <= 0)
2391 break;
2392 }
2393 Py_DECREF(iter);
2394 return ok;
2395}
2396
2397static PyObject *
2398dictview_richcompare(PyObject *self, PyObject *other, int op)
2399{
Guido van Rossumaac530c2007-08-24 22:33:45 +00002400 Py_ssize_t len_self, len_other;
2401 int ok;
2402 PyObject *result;
2403
Guido van Rossumd9214d12007-02-12 02:23:40 +00002404 assert(self != NULL);
2405 assert(PyDictViewSet_Check(self));
2406 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00002407
Guido van Rossumaac530c2007-08-24 22:33:45 +00002408 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other)) {
Guido van Rossumd9214d12007-02-12 02:23:40 +00002409 Py_INCREF(Py_NotImplemented);
2410 return Py_NotImplemented;
2411 }
Guido van Rossumaac530c2007-08-24 22:33:45 +00002412
2413 len_self = PyObject_Size(self);
2414 if (len_self < 0)
2415 return NULL;
2416 len_other = PyObject_Size(other);
2417 if (len_other < 0)
2418 return NULL;
2419
2420 ok = 0;
2421 switch(op) {
2422
2423 case Py_NE:
2424 case Py_EQ:
2425 if (len_self == len_other)
2426 ok = all_contained_in(self, other);
2427 if (op == Py_NE && ok >= 0)
2428 ok = !ok;
2429 break;
2430
2431 case Py_LT:
2432 if (len_self < len_other)
2433 ok = all_contained_in(self, other);
2434 break;
2435
2436 case Py_LE:
2437 if (len_self <= len_other)
2438 ok = all_contained_in(self, other);
2439 break;
2440
2441 case Py_GT:
2442 if (len_self > len_other)
2443 ok = all_contained_in(other, self);
2444 break;
2445
2446 case Py_GE:
2447 if (len_self >= len_other)
2448 ok = all_contained_in(other, self);
2449 break;
2450
2451 }
2452 if (ok < 0)
2453 return NULL;
2454 result = ok ? Py_True : Py_False;
2455 Py_INCREF(result);
2456 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00002457}
2458
Guido van Rossum3ac67412007-02-10 18:55:06 +00002459/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002460
2461static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002462dictkeys_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002463{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002464 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002465 Py_RETURN_NONE;
2466 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002467 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
2468}
2469
2470static int
2471dictkeys_contains(dictviewobject *dv, PyObject *obj)
2472{
2473 if (dv->dv_dict == NULL)
2474 return 0;
2475 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002476}
2477
Guido van Rossum83825ac2007-02-10 04:54:19 +00002478static PySequenceMethods dictkeys_as_sequence = {
2479 (lenfunc)dictview_len, /* sq_length */
2480 0, /* sq_concat */
2481 0, /* sq_repeat */
2482 0, /* sq_item */
2483 0, /* sq_slice */
2484 0, /* sq_ass_item */
2485 0, /* sq_ass_slice */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002486 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002487};
2488
Guido van Rossum523259b2007-08-24 23:41:22 +00002489static PyObject*
2490dictviews_sub(PyObject* self, PyObject *other)
2491{
2492 PyObject *result = PySet_New(self);
2493 PyObject *tmp;
2494 if (result == NULL)
2495 return NULL;
2496
2497 tmp = PyObject_CallMethod(result, "difference_update", "O", other);
2498 if (tmp == NULL) {
2499 Py_DECREF(result);
2500 return NULL;
2501 }
2502
2503 Py_DECREF(tmp);
2504 return result;
2505}
2506
2507static PyObject*
2508dictviews_and(PyObject* self, PyObject *other)
2509{
2510 PyObject *result = PySet_New(self);
2511 PyObject *tmp;
2512 if (result == NULL)
2513 return NULL;
2514
2515 tmp = PyObject_CallMethod(result, "intersection_update", "O", other);
2516 if (tmp == NULL) {
2517 Py_DECREF(result);
2518 return NULL;
2519 }
2520
2521 Py_DECREF(tmp);
2522 return result;
2523}
2524
2525static PyObject*
2526dictviews_or(PyObject* self, PyObject *other)
2527{
2528 PyObject *result = PySet_New(self);
2529 PyObject *tmp;
2530 if (result == NULL)
2531 return NULL;
2532
2533 tmp = PyObject_CallMethod(result, "update", "O", other);
2534 if (tmp == NULL) {
2535 Py_DECREF(result);
2536 return NULL;
2537 }
2538
2539 Py_DECREF(tmp);
2540 return result;
2541}
2542
2543static PyObject*
2544dictviews_xor(PyObject* self, PyObject *other)
2545{
2546 PyObject *result = PySet_New(self);
2547 PyObject *tmp;
2548 if (result == NULL)
2549 return NULL;
2550
2551 tmp = PyObject_CallMethod(result, "symmetric_difference_update", "O",
2552 other);
2553 if (tmp == NULL) {
2554 Py_DECREF(result);
2555 return NULL;
2556 }
2557
2558 Py_DECREF(tmp);
2559 return result;
2560}
2561
2562static PyNumberMethods dictviews_as_number = {
2563 0, /*nb_add*/
2564 (binaryfunc)dictviews_sub, /*nb_subtract*/
2565 0, /*nb_multiply*/
2566 0, /*nb_remainder*/
2567 0, /*nb_divmod*/
2568 0, /*nb_power*/
2569 0, /*nb_negative*/
2570 0, /*nb_positive*/
2571 0, /*nb_absolute*/
2572 0, /*nb_bool*/
2573 0, /*nb_invert*/
2574 0, /*nb_lshift*/
2575 0, /*nb_rshift*/
2576 (binaryfunc)dictviews_and, /*nb_and*/
2577 (binaryfunc)dictviews_xor, /*nb_xor*/
2578 (binaryfunc)dictviews_or, /*nb_or*/
2579};
2580
Guido van Rossumb90c8482007-02-10 01:11:45 +00002581static PyMethodDef dictkeys_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002582 {NULL, NULL} /* sentinel */
2583};
2584
2585PyTypeObject PyDictKeys_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002586 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002587 "dict_keys", /* tp_name */
2588 sizeof(dictviewobject), /* tp_basicsize */
2589 0, /* tp_itemsize */
2590 /* methods */
2591 (destructor)dictview_dealloc, /* tp_dealloc */
2592 0, /* tp_print */
2593 0, /* tp_getattr */
2594 0, /* tp_setattr */
2595 0, /* tp_compare */
2596 0, /* tp_repr */
Guido van Rossum523259b2007-08-24 23:41:22 +00002597 &dictviews_as_number, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002598 &dictkeys_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002599 0, /* tp_as_mapping */
2600 0, /* tp_hash */
2601 0, /* tp_call */
2602 0, /* tp_str */
2603 PyObject_GenericGetAttr, /* tp_getattro */
2604 0, /* tp_setattro */
2605 0, /* tp_as_buffer */
2606 Py_TPFLAGS_DEFAULT, /* tp_flags */
2607 0, /* tp_doc */
2608 0, /* tp_traverse */
2609 0, /* tp_clear */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002610 dictview_richcompare, /* tp_richcompare */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002611 0, /* tp_weaklistoffset */
2612 (getiterfunc)dictkeys_iter, /* tp_iter */
2613 0, /* tp_iternext */
2614 dictkeys_methods, /* tp_methods */
2615 0,
2616};
2617
2618static PyObject *
2619dictkeys_new(PyObject *dict)
2620{
2621 return dictview_new(dict, &PyDictKeys_Type);
2622}
2623
Guido van Rossum3ac67412007-02-10 18:55:06 +00002624/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002625
2626static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002627dictitems_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002628{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002629 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002630 Py_RETURN_NONE;
2631 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002632 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
2633}
2634
2635static int
2636dictitems_contains(dictviewobject *dv, PyObject *obj)
2637{
2638 PyObject *key, *value, *found;
2639 if (dv->dv_dict == NULL)
2640 return 0;
2641 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
2642 return 0;
2643 key = PyTuple_GET_ITEM(obj, 0);
2644 value = PyTuple_GET_ITEM(obj, 1);
2645 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
2646 if (found == NULL) {
2647 if (PyErr_Occurred())
2648 return -1;
2649 return 0;
2650 }
2651 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002652}
2653
Guido van Rossum83825ac2007-02-10 04:54:19 +00002654static PySequenceMethods dictitems_as_sequence = {
2655 (lenfunc)dictview_len, /* sq_length */
2656 0, /* sq_concat */
2657 0, /* sq_repeat */
2658 0, /* sq_item */
2659 0, /* sq_slice */
2660 0, /* sq_ass_item */
2661 0, /* sq_ass_slice */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002662 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002663};
2664
Guido van Rossumb90c8482007-02-10 01:11:45 +00002665static PyMethodDef dictitems_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002666 {NULL, NULL} /* sentinel */
2667};
2668
2669PyTypeObject PyDictItems_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002670 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002671 "dict_items", /* tp_name */
2672 sizeof(dictviewobject), /* tp_basicsize */
2673 0, /* tp_itemsize */
2674 /* methods */
2675 (destructor)dictview_dealloc, /* tp_dealloc */
2676 0, /* tp_print */
2677 0, /* tp_getattr */
2678 0, /* tp_setattr */
2679 0, /* tp_compare */
2680 0, /* tp_repr */
Guido van Rossum523259b2007-08-24 23:41:22 +00002681 &dictviews_as_number, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002682 &dictitems_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002683 0, /* tp_as_mapping */
2684 0, /* tp_hash */
2685 0, /* tp_call */
2686 0, /* tp_str */
2687 PyObject_GenericGetAttr, /* tp_getattro */
2688 0, /* tp_setattro */
2689 0, /* tp_as_buffer */
2690 Py_TPFLAGS_DEFAULT, /* tp_flags */
2691 0, /* tp_doc */
2692 0, /* tp_traverse */
2693 0, /* tp_clear */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002694 dictview_richcompare, /* tp_richcompare */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002695 0, /* tp_weaklistoffset */
2696 (getiterfunc)dictitems_iter, /* tp_iter */
2697 0, /* tp_iternext */
2698 dictitems_methods, /* tp_methods */
2699 0,
2700};
2701
2702static PyObject *
2703dictitems_new(PyObject *dict)
2704{
2705 return dictview_new(dict, &PyDictItems_Type);
2706}
2707
Guido van Rossum3ac67412007-02-10 18:55:06 +00002708/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002709
2710static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002711dictvalues_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002712{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002713 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002714 Py_RETURN_NONE;
2715 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002716 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002717}
2718
Guido van Rossum83825ac2007-02-10 04:54:19 +00002719static PySequenceMethods dictvalues_as_sequence = {
2720 (lenfunc)dictview_len, /* sq_length */
2721 0, /* sq_concat */
2722 0, /* sq_repeat */
2723 0, /* sq_item */
2724 0, /* sq_slice */
2725 0, /* sq_ass_item */
2726 0, /* sq_ass_slice */
2727 (objobjproc)0, /* sq_contains */
2728};
2729
Guido van Rossumb90c8482007-02-10 01:11:45 +00002730static PyMethodDef dictvalues_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002731 {NULL, NULL} /* sentinel */
2732};
2733
2734PyTypeObject PyDictValues_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002735 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002736 "dict_values", /* tp_name */
2737 sizeof(dictviewobject), /* tp_basicsize */
2738 0, /* tp_itemsize */
2739 /* methods */
2740 (destructor)dictview_dealloc, /* tp_dealloc */
2741 0, /* tp_print */
2742 0, /* tp_getattr */
2743 0, /* tp_setattr */
2744 0, /* tp_compare */
2745 0, /* tp_repr */
2746 0, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002747 &dictvalues_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002748 0, /* tp_as_mapping */
2749 0, /* tp_hash */
2750 0, /* tp_call */
2751 0, /* tp_str */
2752 PyObject_GenericGetAttr, /* tp_getattro */
2753 0, /* tp_setattro */
2754 0, /* tp_as_buffer */
2755 Py_TPFLAGS_DEFAULT, /* tp_flags */
2756 0, /* tp_doc */
2757 0, /* tp_traverse */
2758 0, /* tp_clear */
2759 0, /* tp_richcompare */
2760 0, /* tp_weaklistoffset */
2761 (getiterfunc)dictvalues_iter, /* tp_iter */
2762 0, /* tp_iternext */
2763 dictvalues_methods, /* tp_methods */
2764 0,
2765};
2766
2767static PyObject *
2768dictvalues_new(PyObject *dict)
2769{
2770 return dictview_new(dict, &PyDictValues_Type);
2771}