blob: 8d8a882611e978dbddeeed60c5e1f1c511653a19 [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 *
Guido van Rossum89d8c602007-09-18 17:26:56 +0000152lookdict_unicode(dictobject *mp, PyObject *key, long hash);
Fred Drake1bff34a2000-08-31 19:31:38 +0000153
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 }
Guido van Rossum89d8c602007-09-18 17:26:56 +0000221 mp->ma_lookup = lookdict_unicode;
Fred Drake1bff34a2000-08-31 19:31:38 +0000222#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).
Guido van Rossum89d8c602007-09-18 17:26:56 +0000246lookdict_unicode() below is specialized to string keys, comparison of which can
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000247never 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
Guido van Rossum89d8c602007-09-18 17:26:56 +0000328/* Return 1 if two unicode objects are equal, 0 if not. */
329static int
330unicode_eq(PyObject *aa, PyObject *bb)
331{
332 PyUnicodeObject *a = (PyUnicodeObject *)aa;
333 PyUnicodeObject *b = (PyUnicodeObject *)bb;
334
335 if (a->length != b->length)
336 return 0;
337 if (a->length == 0)
338 return 1;
339 if (a->str[0] != b->str[0])
340 return 0;
341 if (a->length == 1)
342 return 1;
343 return PyUnicode_Compare(aa, bb) == 0;
344}
345
346
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000347/*
Guido van Rossum89d8c602007-09-18 17:26:56 +0000348 * Hacked up version of lookdict which can assume keys are always
349 * unicodes; this assumption allows testing for errors during
350 * PyObject_RichCompareBool() to be dropped; unicode-unicode
351 * comparisons never raise exceptions. This also means we don't need
352 * to go through PyObject_RichCompareBool(); we can always use
353 * unicode_eq() directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000354 *
Guido van Rossum89d8c602007-09-18 17:26:56 +0000355 * This is valuable because dicts with only unicode keys are very common.
Fred Drake1bff34a2000-08-31 19:31:38 +0000356 */
357static dictentry *
Guido van Rossum89d8c602007-09-18 17:26:56 +0000358lookdict_unicode(dictobject *mp, PyObject *key, register long hash)
Fred Drake1bff34a2000-08-31 19:31:38 +0000359{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000360 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000361 register size_t perturb;
Fred Drake1bff34a2000-08-31 19:31:38 +0000362 register dictentry *freeslot;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000363 register size_t mask = (size_t)mp->ma_mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000364 dictentry *ep0 = mp->ma_table;
365 register dictentry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000366
Guido van Rossum89d8c602007-09-18 17:26:56 +0000367 /* Make sure this function doesn't have to handle non-unicode keys,
Tim Peters0ab085c2001-09-14 00:25:33 +0000368 including subclasses of str; e.g., one reason to subclass
Guido van Rossum89d8c602007-09-18 17:26:56 +0000369 unicodes is to override __eq__, and for speed we don't cater to
Tim Peters0ab085c2001-09-14 00:25:33 +0000370 that here. */
Guido van Rossum89d8c602007-09-18 17:26:56 +0000371 if (!PyUnicode_CheckExact(key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000372#ifdef SHOW_CONVERSION_COUNTS
373 ++converted;
374#endif
375 mp->ma_lookup = lookdict;
376 return lookdict(mp, key, hash);
377 }
Tim Peters2f228e72001-05-13 00:19:31 +0000378 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000379 ep = &ep0[i];
380 if (ep->me_key == NULL || ep->me_key == key)
381 return ep;
382 if (ep->me_key == dummy)
383 freeslot = ep;
384 else {
Guido van Rossum89d8c602007-09-18 17:26:56 +0000385 if (ep->me_hash == hash && unicode_eq(ep->me_key, key))
Fred Drake1bff34a2000-08-31 19:31:38 +0000386 return ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000387 freeslot = NULL;
388 }
Tim Peters15d49292001-05-27 07:39:22 +0000389
Tim Peters342c65e2001-05-13 06:43:53 +0000390 /* In the loop, me_key == dummy is by far (factor of 100s) the
391 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000392 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
393 i = (i << 2) + i + perturb + 1;
394 ep = &ep0[i & mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000395 if (ep->me_key == NULL)
396 return freeslot == NULL ? ep : freeslot;
397 if (ep->me_key == key
398 || (ep->me_hash == hash
399 && ep->me_key != dummy
Guido van Rossum89d8c602007-09-18 17:26:56 +0000400 && unicode_eq(ep->me_key, key)))
Fred Drake1bff34a2000-08-31 19:31:38 +0000401 return ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000402 if (ep->me_key == dummy && freeslot == NULL)
Tim Peters342c65e2001-05-13 06:43:53 +0000403 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000404 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000405 assert(0); /* NOT REACHED */
406 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000407}
408
409/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000410Internal routine to insert a new item into the table.
411Used both by the internal resize routine and by the public insert routine.
412Eats a reference to key and one to value.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000413Returns -1 if an error occurred, or 0 on success.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000414*/
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000415static int
Tim Peters1f5871e2000-07-04 17:44:48 +0000416insertdict(register dictobject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000417{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000418 PyObject *old_value;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000419 register dictentry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000420 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
421
422 assert(mp->ma_lookup != NULL);
423 ep = mp->ma_lookup(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000424 if (ep == NULL) {
425 Py_DECREF(key);
426 Py_DECREF(value);
427 return -1;
428 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000429 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000430 old_value = ep->me_value;
431 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000432 Py_DECREF(old_value); /* which **CAN** re-enter */
433 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000434 }
435 else {
436 if (ep->me_key == NULL)
437 mp->ma_fill++;
Raymond Hettinger07ead172005-02-05 23:42:57 +0000438 else {
439 assert(ep->me_key == dummy);
440 Py_DECREF(dummy);
441 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000442 ep->me_key = key;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000443 ep->me_hash = (Py_ssize_t)hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000444 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000445 mp->ma_used++;
446 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000447 return 0;
448}
449
450/*
451Internal routine used by dictresize() to insert an item which is
452known to be absent from the dict. This routine also assumes that
453the dict contains no deleted entries. Besides the performance benefit,
454using insertdict() in dictresize() is dangerous (SF bug #1456209).
455Note that no refcounts are changed by this routine; if needed, the caller
456is responsible for incref'ing `key` and `value`.
457*/
458static void
459insertdict_clean(register dictobject *mp, PyObject *key, long hash,
460 PyObject *value)
461{
462 register size_t i;
463 register size_t perturb;
464 register size_t mask = (size_t)mp->ma_mask;
465 dictentry *ep0 = mp->ma_table;
466 register dictentry *ep;
467
468 i = hash & mask;
469 ep = &ep0[i];
470 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
471 i = (i << 2) + i + perturb + 1;
472 ep = &ep0[i & mask];
473 }
474 assert(ep->me_value == NULL);
475 mp->ma_fill++;
476 ep->me_key = key;
477 ep->me_hash = (Py_ssize_t)hash;
478 ep->me_value = value;
479 mp->ma_used++;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000480}
481
482/*
483Restructure the table by allocating a new table and reinserting all
484items again. When entries have been deleted, the new table may
485actually be smaller than the old one.
486*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000487static int
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000488dictresize(dictobject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000489{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000490 Py_ssize_t newsize;
Tim Peters0c6010b2001-05-23 23:33:57 +0000491 dictentry *oldtable, *newtable, *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000492 Py_ssize_t i;
Tim Peters0c6010b2001-05-23 23:33:57 +0000493 int is_oldtable_malloced;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000494 dictentry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000495
496 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000497
Tim Peterseb28ef22001-06-02 05:27:19 +0000498 /* Find the smallest table size > minused. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000499 for (newsize = PyDict_MINSIZE;
Tim Peterseb28ef22001-06-02 05:27:19 +0000500 newsize <= minused && newsize > 0;
501 newsize <<= 1)
502 ;
503 if (newsize <= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000504 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000505 return -1;
506 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000507
508 /* Get space for a new table. */
509 oldtable = mp->ma_table;
510 assert(oldtable != NULL);
511 is_oldtable_malloced = oldtable != mp->ma_smalltable;
512
Tim Peters6d6c1a32001-08-02 04:15:00 +0000513 if (newsize == PyDict_MINSIZE) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000514 /* A large table is shrinking, or we can't get any smaller. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000515 newtable = mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000516 if (newtable == oldtable) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000517 if (mp->ma_fill == mp->ma_used) {
518 /* No dummies, so no point doing anything. */
Tim Peters0c6010b2001-05-23 23:33:57 +0000519 return 0;
Tim Petersf8a548c2001-05-24 16:26:40 +0000520 }
521 /* We're not going to resize it, but rebuild the
522 table anyway to purge old dummy entries.
523 Subtle: This is *necessary* if fill==size,
524 as lookdict needs at least one virgin slot to
525 terminate failing searches. If fill < size, it's
526 merely desirable, as dummies slow searches. */
527 assert(mp->ma_fill > mp->ma_used);
Tim Peters0c6010b2001-05-23 23:33:57 +0000528 memcpy(small_copy, oldtable, sizeof(small_copy));
529 oldtable = small_copy;
530 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000531 }
532 else {
533 newtable = PyMem_NEW(dictentry, newsize);
534 if (newtable == NULL) {
535 PyErr_NoMemory();
536 return -1;
537 }
538 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000539
540 /* Make the dict empty, using the new table. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000541 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000542 mp->ma_table = newtable;
Tim Petersafb6ae82001-06-04 21:00:21 +0000543 mp->ma_mask = newsize - 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000544 memset(newtable, 0, sizeof(dictentry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000545 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000546 i = mp->ma_fill;
547 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000548
Tim Peters19283142001-05-17 22:25:34 +0000549 /* Copy the data over; this is refcount-neutral for active entries;
550 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000551 for (ep = oldtable; i > 0; ep++) {
552 if (ep->me_value != NULL) { /* active entry */
553 --i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000554 insertdict_clean(mp, ep->me_key, (long)ep->me_hash,
555 ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000556 }
Tim Peters19283142001-05-17 22:25:34 +0000557 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000558 --i;
Tim Peters19283142001-05-17 22:25:34 +0000559 assert(ep->me_key == dummy);
560 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000561 }
Tim Peters19283142001-05-17 22:25:34 +0000562 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000563 }
564
Tim Peters0c6010b2001-05-23 23:33:57 +0000565 if (is_oldtable_malloced)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000566 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000567 return 0;
568}
569
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000570/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
571 * that may occur (originally dicts supported only string keys, and exceptions
572 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000573 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000574 * (suppressed) occurred while computing the key's hash, or that some error
575 * (suppressed) occurred when comparing keys in the dict's internal probe
576 * sequence. A nasty example of the latter is when a Python-coded comparison
577 * function hits a stack-depth error, which can cause this to return NULL
578 * even if the key is present.
579 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000580PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000581PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000582{
583 long hash;
Fred Drake1bff34a2000-08-31 19:31:38 +0000584 dictobject *mp = (dictobject *)op;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000585 dictentry *ep;
586 PyThreadState *tstate;
587 if (!PyDict_Check(op))
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000588 return NULL;
Guido van Rossum4d027722007-09-18 04:30:42 +0000589 if (!PyUnicode_CheckExact(key) ||
590 (hash = ((PyUnicodeObject *) key)->hash) == -1)
Guido van Rossumca756f21997-01-23 19:39:29 +0000591 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000592 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000593 if (hash == -1) {
594 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000595 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000596 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000597 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000598
599 /* We can arrive here with a NULL tstate during initialization:
600 try running "python -Wi" for an example related to string
601 interning. Let's just hope that no exception occurs then... */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000602 tstate = _PyThreadState_Current;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000603 if (tstate != NULL && tstate->curexc_type != NULL) {
604 /* preserve the existing exception */
605 PyObject *err_type, *err_value, *err_tb;
606 PyErr_Fetch(&err_type, &err_value, &err_tb);
607 ep = (mp->ma_lookup)(mp, key, hash);
608 /* ignore errors */
609 PyErr_Restore(err_type, err_value, err_tb);
610 if (ep == NULL)
611 return NULL;
612 }
613 else {
614 ep = (mp->ma_lookup)(mp, key, hash);
615 if (ep == NULL) {
616 PyErr_Clear();
617 return NULL;
618 }
619 }
620 return ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000621}
622
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000623/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
624 This returns NULL *with* an exception set if an exception occurred.
625 It returns NULL *without* an exception set if the key wasn't present.
626*/
627PyObject *
628PyDict_GetItemWithError(PyObject *op, PyObject *key)
629{
630 long hash;
631 dictobject *mp = (dictobject *)op;
632 dictentry *ep;
633
634 if (!PyDict_Check(op)) {
635 PyErr_BadInternalCall();
636 return NULL;
637 }
Guido van Rossum89d8c602007-09-18 17:26:56 +0000638 if (!PyUnicode_CheckExact(key) ||
639 (hash = ((PyUnicodeObject *) key)->hash) == -1)
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000640 {
641 hash = PyObject_Hash(key);
642 if (hash == -1) {
643 return NULL;
644 }
645 }
646
647 ep = (mp->ma_lookup)(mp, key, hash);
648 if (ep == NULL)
649 return NULL;
650 return ep->me_value;
651}
652
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000653/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
Tim Peters60b29962006-01-01 01:19:23 +0000654 * dictionary if it's merely replacing the value for an existing key.
655 * This means that it's safe to loop over a dictionary with PyDict_Next()
656 * and occasionally replace a value -- but you can't insert new keys or
657 * remove them.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000658 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000659int
Tim Peters1f5871e2000-07-04 17:44:48 +0000660PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000661{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000662 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000663 register long hash;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000664 register Py_ssize_t n_used;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000665
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000666 if (!PyDict_Check(op)) {
667 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000668 return -1;
669 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000670 assert(key);
671 assert(value);
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000672 mp = (dictobject *)op;
Guido van Rossum4d027722007-09-18 04:30:42 +0000673 if (!PyUnicode_CheckExact(key) ||
674 (hash = ((PyUnicodeObject *) key)->hash) == -1)
675 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000676 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000677 if (hash == -1)
678 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000679 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000680 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000681 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000682 Py_INCREF(value);
683 Py_INCREF(key);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000684 if (insertdict(mp, key, hash, value) != 0)
685 return -1;
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000686 /* If we added a key, we can safely resize. Otherwise just return!
687 * If fill >= 2/3 size, adjust size. Normally, this doubles or
688 * quaduples the size, but it's also possible for the dict to shrink
Tim Peters60b29962006-01-01 01:19:23 +0000689 * (if ma_fill is much larger than ma_used, meaning a lot of dict
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000690 * keys have been * deleted).
Tim Peters60b29962006-01-01 01:19:23 +0000691 *
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000692 * Quadrupling the size improves average dictionary sparseness
693 * (reducing collisions) at the cost of some memory and iteration
694 * speed (which loops over every possible entry). It also halves
695 * the number of expensive resize operations in a growing dictionary.
Tim Peters60b29962006-01-01 01:19:23 +0000696 *
697 * Very large dictionaries (over 50K items) use doubling instead.
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000698 * This may help applications with severe memory constraints.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000699 */
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000700 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
701 return 0;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000702 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000703}
704
705int
Tim Peters1f5871e2000-07-04 17:44:48 +0000706PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000707{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000708 register dictobject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000709 register long hash;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000710 register dictentry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000711 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000712
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000713 if (!PyDict_Check(op)) {
714 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000715 return -1;
716 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000717 assert(key);
Guido van Rossum89d8c602007-09-18 17:26:56 +0000718 if (!PyUnicode_CheckExact(key) ||
719 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000720 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000721 if (hash == -1)
722 return -1;
723 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000724 mp = (dictobject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000725 ep = (mp->ma_lookup)(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000726 if (ep == NULL)
727 return -1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000728 if (ep->me_value == NULL) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000729 set_key_error(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000730 return -1;
731 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000732 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000733 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000734 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000735 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000736 ep->me_value = NULL;
737 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000738 Py_DECREF(old_value);
739 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000740 return 0;
741}
742
Guido van Rossum25831651993-05-19 14:50:45 +0000743void
Tim Peters1f5871e2000-07-04 17:44:48 +0000744PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000745{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000746 dictobject *mp;
Tim Petersdea48ec2001-05-22 20:40:22 +0000747 dictentry *ep, *table;
748 int table_is_malloced;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000749 Py_ssize_t fill;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000750 dictentry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000751#ifdef Py_DEBUG
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000752 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +0000753#endif
754
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000755 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000756 return;
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000757 mp = (dictobject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000758#ifdef Py_DEBUG
Tim Petersafb6ae82001-06-04 21:00:21 +0000759 n = mp->ma_mask + 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000760 i = 0;
761#endif
762
763 table = mp->ma_table;
764 assert(table != NULL);
765 table_is_malloced = table != mp->ma_smalltable;
766
767 /* This is delicate. During the process of clearing the dict,
768 * decrefs can cause the dict to mutate. To avoid fatal confusion
769 * (voice of experience), we have to make the dict empty before
770 * clearing the slots, and never refer to anything via mp->xxx while
771 * clearing.
772 */
773 fill = mp->ma_fill;
774 if (table_is_malloced)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000775 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000776
777 else if (fill > 0) {
778 /* It's a small table with something that needs to be cleared.
779 * Afraid the only safe way is to copy the dict entries into
780 * another small table first.
781 */
782 memcpy(small_copy, table, sizeof(small_copy));
783 table = small_copy;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000784 EMPTY_TO_MINSIZE(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000785 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000786 /* else it's a small table that's already empty */
787
788 /* Now we can finally clear things. If C had refcounts, we could
789 * assert that the refcount on table is 1 now, i.e. that this function
790 * has unique access to it, so decref side-effects can't alter it.
791 */
792 for (ep = table; fill > 0; ++ep) {
793#ifdef Py_DEBUG
794 assert(i < n);
795 ++i;
796#endif
797 if (ep->me_key) {
798 --fill;
799 Py_DECREF(ep->me_key);
800 Py_XDECREF(ep->me_value);
801 }
802#ifdef Py_DEBUG
803 else
804 assert(ep->me_value == NULL);
805#endif
806 }
807
808 if (table_is_malloced)
809 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000810}
811
Tim Peters080c88b2003-02-15 03:01:11 +0000812/*
813 * Iterate over a dict. Use like so:
814 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000815 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +0000816 * PyObject *key, *value;
817 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +0000818 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +0000819 * Refer to borrowed references in key and value.
820 * }
821 *
822 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +0000823 * mutates the dict. One exception: it is safe if the loop merely changes
824 * the values associated with the keys (but doesn't insert new keys or
825 * delete keys), via PyDict_SetItem().
826 */
Guido van Rossum25831651993-05-19 14:50:45 +0000827int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000828PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000829{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000830 register Py_ssize_t i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000831 register Py_ssize_t mask;
Raymond Hettinger43442782004-03-17 21:55:03 +0000832 register dictentry *ep;
833
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000834 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000835 return 0;
Guido van Rossum25831651993-05-19 14:50:45 +0000836 i = *ppos;
837 if (i < 0)
838 return 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000839 ep = ((dictobject *)op)->ma_table;
840 mask = ((dictobject *)op)->ma_mask;
841 while (i <= mask && ep[i].me_value == NULL)
Guido van Rossum25831651993-05-19 14:50:45 +0000842 i++;
843 *ppos = i+1;
Raymond Hettinger43442782004-03-17 21:55:03 +0000844 if (i > mask)
Guido van Rossum25831651993-05-19 14:50:45 +0000845 return 0;
846 if (pkey)
Raymond Hettinger43442782004-03-17 21:55:03 +0000847 *pkey = ep[i].me_key;
Guido van Rossum25831651993-05-19 14:50:45 +0000848 if (pvalue)
Raymond Hettinger43442782004-03-17 21:55:03 +0000849 *pvalue = ep[i].me_value;
Guido van Rossum25831651993-05-19 14:50:45 +0000850 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000851}
852
Thomas Wouterscf297e42007-02-23 15:07:44 +0000853/* Internal version of PyDict_Next that returns a hash value in addition to the key and value.*/
854int
855_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, long *phash)
856{
857 register Py_ssize_t i;
858 register Py_ssize_t mask;
859 register dictentry *ep;
860
861 if (!PyDict_Check(op))
862 return 0;
863 i = *ppos;
864 if (i < 0)
865 return 0;
866 ep = ((dictobject *)op)->ma_table;
867 mask = ((dictobject *)op)->ma_mask;
868 while (i <= mask && ep[i].me_value == NULL)
869 i++;
870 *ppos = i+1;
871 if (i > mask)
872 return 0;
873 *phash = (long)(ep[i].me_hash);
874 if (pkey)
875 *pkey = ep[i].me_key;
876 if (pvalue)
877 *pvalue = ep[i].me_value;
878 return 1;
879}
880
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000881/* Methods */
882
883static void
Tim Peters1f5871e2000-07-04 17:44:48 +0000884dict_dealloc(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000885{
Guido van Rossuma9e7a811997-05-13 21:02:11 +0000886 register dictentry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000887 Py_ssize_t fill = mp->ma_fill;
Guido van Rossumff413af2002-03-28 20:34:59 +0000888 PyObject_GC_UnTrack(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000889 Py_TRASHCAN_SAFE_BEGIN(mp)
Tim Petersdea48ec2001-05-22 20:40:22 +0000890 for (ep = mp->ma_table; fill > 0; ep++) {
891 if (ep->me_key) {
892 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000893 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000894 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000895 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000896 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000897 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000898 PyMem_DEL(mp->ma_table);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000899 if (num_free_dicts < MAXFREEDICTS && Py_Type(mp) == &PyDict_Type)
Raymond Hettinger43442782004-03-17 21:55:03 +0000900 free_dicts[num_free_dicts++] = mp;
Tim Peters60b29962006-01-01 01:19:23 +0000901 else
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000902 Py_Type(mp)->tp_free((PyObject *)mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000903 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000904}
905
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000906static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000907dict_repr(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000908{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000909 Py_ssize_t i;
Tim Petersa7259592001-06-16 05:11:17 +0000910 PyObject *s, *temp, *colon = NULL;
911 PyObject *pieces = NULL, *result = NULL;
912 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +0000913
Tim Petersa7259592001-06-16 05:11:17 +0000914 i = Py_ReprEnter((PyObject *)mp);
Guido van Rossum255443b1998-04-10 22:47:14 +0000915 if (i != 0) {
Walter Dörwald1ab83302007-05-18 17:15:44 +0000916 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
Guido van Rossum255443b1998-04-10 22:47:14 +0000917 }
918
Tim Petersa7259592001-06-16 05:11:17 +0000919 if (mp->ma_used == 0) {
Walter Dörwald1ab83302007-05-18 17:15:44 +0000920 result = PyUnicode_FromString("{}");
Tim Petersa7259592001-06-16 05:11:17 +0000921 goto Done;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000922 }
Tim Petersa7259592001-06-16 05:11:17 +0000923
924 pieces = PyList_New(0);
925 if (pieces == NULL)
926 goto Done;
927
Walter Dörwald1ab83302007-05-18 17:15:44 +0000928 colon = PyUnicode_FromString(": ");
Tim Petersa7259592001-06-16 05:11:17 +0000929 if (colon == NULL)
930 goto Done;
931
932 /* Do repr() on each key+value pair, and insert ": " between them.
933 Note that repr may mutate the dict. */
Tim Petersc6057842001-06-16 07:52:53 +0000934 i = 0;
935 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Tim Petersa7259592001-06-16 05:11:17 +0000936 int status;
937 /* Prevent repr from deleting value during key format. */
938 Py_INCREF(value);
939 s = PyObject_Repr(key);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000940 PyUnicode_Append(&s, colon);
941 PyUnicode_AppendAndDel(&s, PyObject_Repr(value));
Tim Petersa7259592001-06-16 05:11:17 +0000942 Py_DECREF(value);
943 if (s == NULL)
944 goto Done;
945 status = PyList_Append(pieces, s);
946 Py_DECREF(s); /* append created a new ref */
947 if (status < 0)
948 goto Done;
949 }
950
951 /* Add "{}" decorations to the first and last items. */
952 assert(PyList_GET_SIZE(pieces) > 0);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000953 s = PyUnicode_FromString("{");
Tim Petersa7259592001-06-16 05:11:17 +0000954 if (s == NULL)
955 goto Done;
956 temp = PyList_GET_ITEM(pieces, 0);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000957 PyUnicode_AppendAndDel(&s, temp);
Tim Petersa7259592001-06-16 05:11:17 +0000958 PyList_SET_ITEM(pieces, 0, s);
959 if (s == NULL)
960 goto Done;
961
Walter Dörwald1ab83302007-05-18 17:15:44 +0000962 s = PyUnicode_FromString("}");
Tim Petersa7259592001-06-16 05:11:17 +0000963 if (s == NULL)
964 goto Done;
965 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000966 PyUnicode_AppendAndDel(&temp, s);
Tim Petersa7259592001-06-16 05:11:17 +0000967 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
968 if (temp == NULL)
969 goto Done;
970
971 /* Paste them all together with ", " between. */
Walter Dörwald1ab83302007-05-18 17:15:44 +0000972 s = PyUnicode_FromString(", ");
Tim Petersa7259592001-06-16 05:11:17 +0000973 if (s == NULL)
974 goto Done;
Walter Dörwald1ab83302007-05-18 17:15:44 +0000975 result = PyUnicode_Join(s, pieces);
Tim Peters0ab085c2001-09-14 00:25:33 +0000976 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +0000977
978Done:
979 Py_XDECREF(pieces);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000980 Py_XDECREF(colon);
Tim Petersa7259592001-06-16 05:11:17 +0000981 Py_ReprLeave((PyObject *)mp);
982 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000983}
984
Martin v. Löwis18e16552006-02-15 17:27:45 +0000985static Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +0000986dict_length(dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000987{
988 return mp->ma_used;
989}
990
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000991static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000992dict_subscript(dictobject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000993{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000994 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000995 long hash;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000996 dictentry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000997 assert(mp->ma_table != NULL);
Guido van Rossum89d8c602007-09-18 17:26:56 +0000998 if (!PyUnicode_CheckExact(key) ||
999 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001000 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001001 if (hash == -1)
1002 return NULL;
1003 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001004 ep = (mp->ma_lookup)(mp, key, hash);
1005 if (ep == NULL)
1006 return NULL;
1007 v = ep->me_value;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001008 if (v == NULL) {
1009 if (!PyDict_CheckExact(mp)) {
1010 /* Look up __missing__ method if we're a subclass. */
Guido van Rossum4b92a822006-02-25 23:32:30 +00001011 PyObject *missing;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001012 static PyObject *missing_str = NULL;
1013 if (missing_str == NULL)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001014 missing_str =
Martin v. Löwis5b222132007-06-10 09:51:05 +00001015 PyUnicode_InternFromString("__missing__");
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001016 missing = _PyType_Lookup(Py_Type(mp), missing_str);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001017 if (missing != NULL)
1018 return PyObject_CallFunctionObjArgs(missing,
1019 (PyObject *)mp, key, NULL);
1020 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001021 set_key_error(key);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001022 return NULL;
1023 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001024 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001025 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001026 return v;
1027}
1028
1029static int
Tim Peters1f5871e2000-07-04 17:44:48 +00001030dict_ass_sub(dictobject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001031{
1032 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001033 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001034 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001035 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001036}
1037
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001038static PyMappingMethods dict_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001039 (lenfunc)dict_length, /*mp_length*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001040 (binaryfunc)dict_subscript, /*mp_subscript*/
1041 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001042};
1043
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001044static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001045dict_keys(register dictobject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001046{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001047 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001048 register Py_ssize_t i, j;
Raymond Hettinger43442782004-03-17 21:55:03 +00001049 dictentry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001050 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001051
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001052 again:
1053 n = mp->ma_used;
1054 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001055 if (v == NULL)
1056 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001057 if (n != mp->ma_used) {
1058 /* Durnit. The allocations caused the dict to resize.
1059 * Just start over, this shouldn't normally happen.
1060 */
1061 Py_DECREF(v);
1062 goto again;
1063 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001064 ep = mp->ma_table;
1065 mask = mp->ma_mask;
1066 for (i = 0, j = 0; i <= mask; i++) {
1067 if (ep[i].me_value != NULL) {
1068 PyObject *key = ep[i].me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001069 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001070 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001071 j++;
1072 }
1073 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001074 assert(j == n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001075 return v;
1076}
1077
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001078static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001079dict_values(register dictobject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001080{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001081 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001082 register Py_ssize_t i, j;
Raymond Hettinger43442782004-03-17 21:55:03 +00001083 dictentry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001084 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001085
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001086 again:
1087 n = mp->ma_used;
1088 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001089 if (v == NULL)
1090 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001091 if (n != mp->ma_used) {
1092 /* Durnit. The allocations caused the dict to resize.
1093 * Just start over, this shouldn't normally happen.
1094 */
1095 Py_DECREF(v);
1096 goto again;
1097 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001098 ep = mp->ma_table;
1099 mask = mp->ma_mask;
1100 for (i = 0, j = 0; i <= mask; i++) {
1101 if (ep[i].me_value != NULL) {
1102 PyObject *value = ep[i].me_value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001103 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001104 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001105 j++;
1106 }
1107 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001108 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001109 return v;
1110}
1111
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001112static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001113dict_items(register dictobject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001114{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001115 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001116 register Py_ssize_t i, j, n;
1117 Py_ssize_t mask;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001118 PyObject *item, *key, *value;
Raymond Hettinger43442782004-03-17 21:55:03 +00001119 dictentry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001120
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001121 /* Preallocate the list of tuples, to avoid allocations during
1122 * the loop over the items, which could trigger GC, which
1123 * could resize the dict. :-(
1124 */
1125 again:
1126 n = mp->ma_used;
1127 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001128 if (v == NULL)
1129 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001130 for (i = 0; i < n; i++) {
1131 item = PyTuple_New(2);
1132 if (item == NULL) {
1133 Py_DECREF(v);
1134 return NULL;
1135 }
1136 PyList_SET_ITEM(v, i, item);
1137 }
1138 if (n != mp->ma_used) {
1139 /* Durnit. The allocations caused the dict to resize.
1140 * Just start over, this shouldn't normally happen.
1141 */
1142 Py_DECREF(v);
1143 goto again;
1144 }
1145 /* Nothing we do below makes any function calls. */
Raymond Hettinger43442782004-03-17 21:55:03 +00001146 ep = mp->ma_table;
1147 mask = mp->ma_mask;
1148 for (i = 0, j = 0; i <= mask; i++) {
Raymond Hettinger06905122004-03-19 10:30:00 +00001149 if ((value=ep[i].me_value) != NULL) {
Raymond Hettinger43442782004-03-17 21:55:03 +00001150 key = ep[i].me_key;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001151 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001152 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001153 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001154 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001155 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001156 j++;
1157 }
1158 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001159 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001160 return v;
1161}
1162
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001163static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001164dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001165{
1166 PyObject *seq;
1167 PyObject *value = Py_None;
1168 PyObject *it; /* iter(seq) */
1169 PyObject *key;
1170 PyObject *d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001171 int status;
1172
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001173 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001174 return NULL;
1175
1176 d = PyObject_CallObject(cls, NULL);
1177 if (d == NULL)
1178 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001179
Guido van Rossumd8faa362007-04-27 19:54:29 +00001180 if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
1181 dictobject *mp = (dictobject *)d;
1182 Py_ssize_t pos = 0;
1183 PyObject *key;
1184 long hash;
1185
1186 if (dictresize(mp, PySet_GET_SIZE(seq)))
1187 return NULL;
1188
1189 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
1190 Py_INCREF(key);
1191 Py_INCREF(value);
1192 if (insertdict(mp, key, hash, value))
1193 return NULL;
1194 }
1195 return d;
1196 }
1197
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001198 it = PyObject_GetIter(seq);
1199 if (it == NULL){
1200 Py_DECREF(d);
1201 return NULL;
1202 }
1203
1204 for (;;) {
1205 key = PyIter_Next(it);
1206 if (key == NULL) {
1207 if (PyErr_Occurred())
1208 goto Fail;
1209 break;
1210 }
Raymond Hettingere03e5b12002-12-07 08:10:51 +00001211 status = PyObject_SetItem(d, key, value);
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001212 Py_DECREF(key);
1213 if (status < 0)
1214 goto Fail;
1215 }
1216
1217 Py_DECREF(it);
1218 return d;
1219
1220Fail:
1221 Py_DECREF(it);
1222 Py_DECREF(d);
1223 return NULL;
1224}
1225
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001226static int
1227dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001228{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001229 PyObject *arg = NULL;
1230 int result = 0;
1231
1232 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1233 result = -1;
1234
1235 else if (arg != NULL) {
1236 if (PyObject_HasAttrString(arg, "keys"))
1237 result = PyDict_Merge(self, arg, 1);
1238 else
1239 result = PyDict_MergeFromSeq2(self, arg, 1);
1240 }
1241 if (result == 0 && kwds != NULL)
1242 result = PyDict_Merge(self, kwds, 1);
1243 return result;
1244}
1245
1246static PyObject *
1247dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1248{
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001249 if (dict_update_common(self, args, kwds, "update") != -1)
1250 Py_RETURN_NONE;
1251 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001252}
1253
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001254/* Update unconditionally replaces existing items.
1255 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001256 otherwise it leaves existing items unchanged.
1257
1258 PyDict_{Update,Merge} update/merge from a mapping object.
1259
Tim Petersf582b822001-12-11 18:51:08 +00001260 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001261 producing iterable objects of length 2.
1262*/
1263
Tim Petersf582b822001-12-11 18:51:08 +00001264int
Tim Peters1fc240e2001-10-26 05:06:50 +00001265PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1266{
1267 PyObject *it; /* iter(seq2) */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001268 Py_ssize_t i; /* index into seq2 of current element */
Tim Peters1fc240e2001-10-26 05:06:50 +00001269 PyObject *item; /* seq2[i] */
1270 PyObject *fast; /* item as a 2-tuple or 2-list */
1271
1272 assert(d != NULL);
1273 assert(PyDict_Check(d));
1274 assert(seq2 != NULL);
1275
1276 it = PyObject_GetIter(seq2);
1277 if (it == NULL)
1278 return -1;
1279
1280 for (i = 0; ; ++i) {
1281 PyObject *key, *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001282 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001283
1284 fast = NULL;
1285 item = PyIter_Next(it);
1286 if (item == NULL) {
1287 if (PyErr_Occurred())
1288 goto Fail;
1289 break;
1290 }
1291
1292 /* Convert item to sequence, and verify length 2. */
1293 fast = PySequence_Fast(item, "");
1294 if (fast == NULL) {
1295 if (PyErr_ExceptionMatches(PyExc_TypeError))
1296 PyErr_Format(PyExc_TypeError,
1297 "cannot convert dictionary update "
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001298 "sequence element #%zd to a sequence",
Tim Peters1fc240e2001-10-26 05:06:50 +00001299 i);
1300 goto Fail;
1301 }
1302 n = PySequence_Fast_GET_SIZE(fast);
1303 if (n != 2) {
1304 PyErr_Format(PyExc_ValueError,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001305 "dictionary update sequence element #%zd "
Martin v. Löwis2c95cc62006-02-16 06:54:25 +00001306 "has length %zd; 2 is required",
Martin v. Löwise0e89f72006-02-16 06:59:22 +00001307 i, n);
Tim Peters1fc240e2001-10-26 05:06:50 +00001308 goto Fail;
1309 }
1310
1311 /* Update/merge with this (key, value) pair. */
1312 key = PySequence_Fast_GET_ITEM(fast, 0);
1313 value = PySequence_Fast_GET_ITEM(fast, 1);
1314 if (override || PyDict_GetItem(d, key) == NULL) {
1315 int status = PyDict_SetItem(d, key, value);
1316 if (status < 0)
1317 goto Fail;
1318 }
1319 Py_DECREF(fast);
1320 Py_DECREF(item);
1321 }
1322
1323 i = 0;
1324 goto Return;
1325Fail:
1326 Py_XDECREF(item);
1327 Py_XDECREF(fast);
1328 i = -1;
1329Return:
1330 Py_DECREF(it);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001331 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001332}
1333
Tim Peters6d6c1a32001-08-02 04:15:00 +00001334int
1335PyDict_Update(PyObject *a, PyObject *b)
1336{
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001337 return PyDict_Merge(a, b, 1);
1338}
1339
1340int
1341PyDict_Merge(PyObject *a, PyObject *b, int override)
1342{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001343 register PyDictObject *mp, *other;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001344 register Py_ssize_t i;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001345 dictentry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001346
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001347 /* We accept for the argument either a concrete dictionary object,
1348 * or an abstract "mapping" object. For the former, we can do
1349 * things quite efficiently. For the latter, we only require that
1350 * PyMapping_Keys() and PyObject_GetItem() be supported.
1351 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001352 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1353 PyErr_BadInternalCall();
1354 return -1;
1355 }
1356 mp = (dictobject*)a;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001357 if (PyDict_CheckExact(b)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001358 other = (dictobject*)b;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001359 if (other == mp || other->ma_used == 0)
1360 /* a.update(a) or a.update({}); nothing to do */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001361 return 0;
Raymond Hettinger186e7392005-05-14 18:08:25 +00001362 if (mp->ma_used == 0)
1363 /* Since the target dict is empty, PyDict_GetItem()
1364 * always returns NULL. Setting override to 1
1365 * skips the unnecessary test.
1366 */
1367 override = 1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001368 /* Do one big resize at the start, rather than
1369 * incrementally resizing as we insert new items. Expect
1370 * that there will be no (or few) overlapping keys.
1371 */
1372 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
Raymond Hettingerc8d22902003-05-07 00:49:40 +00001373 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001374 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001375 }
1376 for (i = 0; i <= other->ma_mask; i++) {
1377 entry = &other->ma_table[i];
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001378 if (entry->me_value != NULL &&
1379 (override ||
1380 PyDict_GetItem(a, entry->me_key) == NULL)) {
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001381 Py_INCREF(entry->me_key);
1382 Py_INCREF(entry->me_value);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001383 if (insertdict(mp, entry->me_key,
1384 (long)entry->me_hash,
1385 entry->me_value) != 0)
1386 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001387 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001388 }
1389 }
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001390 else {
1391 /* Do it the generic, slower way */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001392 PyObject *keys = PyMapping_Keys(b);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001393 PyObject *iter;
1394 PyObject *key, *value;
1395 int status;
1396
1397 if (keys == NULL)
1398 /* Docstring says this is equivalent to E.keys() so
1399 * if E doesn't have a .keys() method we want
1400 * AttributeError to percolate up. Might as well
1401 * do the same for any other error.
1402 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001403 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001404
1405 iter = PyObject_GetIter(keys);
1406 Py_DECREF(keys);
1407 if (iter == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001408 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001409
1410 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001411 if (!override && PyDict_GetItem(a, key) != NULL) {
1412 Py_DECREF(key);
1413 continue;
1414 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001415 value = PyObject_GetItem(b, key);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001416 if (value == NULL) {
1417 Py_DECREF(iter);
1418 Py_DECREF(key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001419 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001420 }
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001421 status = PyDict_SetItem(a, key, value);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001422 Py_DECREF(key);
1423 Py_DECREF(value);
1424 if (status < 0) {
1425 Py_DECREF(iter);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001426 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001427 }
1428 }
1429 Py_DECREF(iter);
1430 if (PyErr_Occurred())
1431 /* Iterator completed, via error */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001432 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001433 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001434 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001435}
1436
1437static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001438dict_copy(register dictobject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001439{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001440 return PyDict_Copy((PyObject*)mp);
1441}
1442
1443PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001444PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001445{
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001446 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001447
1448 if (o == NULL || !PyDict_Check(o)) {
1449 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001450 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001451 }
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001452 copy = PyDict_New();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001453 if (copy == NULL)
1454 return NULL;
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001455 if (PyDict_Merge(copy, o, 1) == 0)
1456 return copy;
1457 Py_DECREF(copy);
Raymond Hettinger1356f782005-04-15 15:58:42 +00001458 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001459}
1460
Martin v. Löwis18e16552006-02-15 17:27:45 +00001461Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001462PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001463{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001464 if (mp == NULL || !PyDict_Check(mp)) {
1465 PyErr_BadInternalCall();
Jeremy Hylton7083bb72004-02-17 20:10:11 +00001466 return -1;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001467 }
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001468 return ((dictobject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001469}
1470
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001471PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001472PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001473{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001474 if (mp == NULL || !PyDict_Check(mp)) {
1475 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001476 return NULL;
1477 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001478 return dict_keys((dictobject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001479}
1480
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001481PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001482PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001483{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001484 if (mp == NULL || !PyDict_Check(mp)) {
1485 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001486 return NULL;
1487 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001488 return dict_values((dictobject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001489}
1490
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001491PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001492PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001493{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001494 if (mp == NULL || !PyDict_Check(mp)) {
1495 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001496 return NULL;
1497 }
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001498 return dict_items((dictobject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001499}
1500
Tim Peterse63415e2001-05-08 04:38:29 +00001501/* Return 1 if dicts equal, 0 if not, -1 if error.
1502 * Gets out as soon as any difference is detected.
1503 * Uses only Py_EQ comparison.
1504 */
1505static int
1506dict_equal(dictobject *a, dictobject *b)
1507{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001508 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00001509
1510 if (a->ma_used != b->ma_used)
1511 /* can't be equal if # of entries differ */
1512 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001513
Tim Peterse63415e2001-05-08 04:38:29 +00001514 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Tim Petersafb6ae82001-06-04 21:00:21 +00001515 for (i = 0; i <= a->ma_mask; i++) {
Tim Peterse63415e2001-05-08 04:38:29 +00001516 PyObject *aval = a->ma_table[i].me_value;
1517 if (aval != NULL) {
1518 int cmp;
1519 PyObject *bval;
1520 PyObject *key = a->ma_table[i].me_key;
1521 /* temporarily bump aval's refcount to ensure it stays
1522 alive until we're done with it */
1523 Py_INCREF(aval);
Guido van Rossumdc5f6b22006-08-24 21:29:26 +00001524 /* ditto for key */
1525 Py_INCREF(key);
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001526 bval = PyDict_GetItemWithError((PyObject *)b, key);
Guido van Rossumdc5f6b22006-08-24 21:29:26 +00001527 Py_DECREF(key);
Tim Peterse63415e2001-05-08 04:38:29 +00001528 if (bval == NULL) {
1529 Py_DECREF(aval);
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001530 if (PyErr_Occurred())
1531 return -1;
Tim Peterse63415e2001-05-08 04:38:29 +00001532 return 0;
1533 }
1534 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1535 Py_DECREF(aval);
1536 if (cmp <= 0) /* error or not equal */
1537 return cmp;
1538 }
1539 }
1540 return 1;
1541 }
1542
1543static PyObject *
1544dict_richcompare(PyObject *v, PyObject *w, int op)
1545{
1546 int cmp;
1547 PyObject *res;
1548
1549 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1550 res = Py_NotImplemented;
1551 }
1552 else if (op == Py_EQ || op == Py_NE) {
1553 cmp = dict_equal((dictobject *)v, (dictobject *)w);
1554 if (cmp < 0)
1555 return NULL;
1556 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1557 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001558 else
1559 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001560 Py_INCREF(res);
1561 return res;
1562 }
1563
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001564static PyObject *
Guido van Rossume2b70bc2006-08-18 22:13:04 +00001565dict_contains(register dictobject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001566{
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001567 long hash;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001568 dictentry *ep;
1569
Guido van Rossum89d8c602007-09-18 17:26:56 +00001570 if (!PyUnicode_CheckExact(key) ||
1571 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001572 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001573 if (hash == -1)
1574 return NULL;
1575 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001576 ep = (mp->ma_lookup)(mp, key, hash);
1577 if (ep == NULL)
1578 return NULL;
1579 return PyBool_FromLong(ep->me_value != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001580}
1581
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001582static PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001583dict_get(register dictobject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001584{
1585 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001586 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001587 PyObject *val = NULL;
1588 long hash;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001589 dictentry *ep;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001590
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001591 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001592 return NULL;
1593
Guido van Rossum89d8c602007-09-18 17:26:56 +00001594 if (!PyUnicode_CheckExact(key) ||
1595 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Barry Warsawc38c5da1997-10-06 17:49:20 +00001596 hash = PyObject_Hash(key);
1597 if (hash == -1)
1598 return NULL;
1599 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001600 ep = (mp->ma_lookup)(mp, key, hash);
1601 if (ep == NULL)
1602 return NULL;
1603 val = ep->me_value;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001604 if (val == NULL)
1605 val = failobj;
1606 Py_INCREF(val);
1607 return val;
1608}
1609
1610
1611static PyObject *
Guido van Rossum164452c2000-08-08 16:12:54 +00001612dict_setdefault(register dictobject *mp, PyObject *args)
1613{
1614 PyObject *key;
1615 PyObject *failobj = Py_None;
1616 PyObject *val = NULL;
1617 long hash;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001618 dictentry *ep;
Guido van Rossum164452c2000-08-08 16:12:54 +00001619
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001620 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
Guido van Rossum164452c2000-08-08 16:12:54 +00001621 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001622
Guido van Rossum89d8c602007-09-18 17:26:56 +00001623 if (!PyUnicode_CheckExact(key) ||
1624 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossum164452c2000-08-08 16:12:54 +00001625 hash = PyObject_Hash(key);
1626 if (hash == -1)
1627 return NULL;
1628 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001629 ep = (mp->ma_lookup)(mp, key, hash);
1630 if (ep == NULL)
1631 return NULL;
1632 val = ep->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001633 if (val == NULL) {
1634 val = failobj;
1635 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1636 val = NULL;
1637 }
1638 Py_XINCREF(val);
1639 return val;
1640}
1641
1642
1643static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001644dict_clear(register dictobject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001645{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001646 PyDict_Clear((PyObject *)mp);
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001647 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001648}
1649
Guido van Rossumba6ab842000-12-12 22:02:18 +00001650static PyObject *
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001651dict_pop(dictobject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00001652{
1653 long hash;
1654 dictentry *ep;
1655 PyObject *old_value, *old_key;
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001656 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001657
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001658 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1659 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001660 if (mp->ma_used == 0) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001661 if (deflt) {
1662 Py_INCREF(deflt);
1663 return deflt;
1664 }
Guido van Rossume027d982002-04-12 15:11:59 +00001665 PyErr_SetString(PyExc_KeyError,
1666 "pop(): dictionary is empty");
1667 return NULL;
1668 }
Guido van Rossum89d8c602007-09-18 17:26:56 +00001669 if (!PyUnicode_CheckExact(key) ||
1670 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossume027d982002-04-12 15:11:59 +00001671 hash = PyObject_Hash(key);
1672 if (hash == -1)
1673 return NULL;
1674 }
1675 ep = (mp->ma_lookup)(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001676 if (ep == NULL)
1677 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001678 if (ep->me_value == NULL) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001679 if (deflt) {
1680 Py_INCREF(deflt);
1681 return deflt;
1682 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001683 set_key_error(key);
Guido van Rossume027d982002-04-12 15:11:59 +00001684 return NULL;
1685 }
1686 old_key = ep->me_key;
1687 Py_INCREF(dummy);
1688 ep->me_key = dummy;
1689 old_value = ep->me_value;
1690 ep->me_value = NULL;
1691 mp->ma_used--;
1692 Py_DECREF(old_key);
1693 return old_value;
1694}
1695
1696static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001697dict_popitem(dictobject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001698{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001699 Py_ssize_t i = 0;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001700 dictentry *ep;
1701 PyObject *res;
1702
Tim Petersf4b33f62001-06-02 05:42:29 +00001703 /* Allocate the result tuple before checking the size. Believe it
1704 * or not, this allocation could trigger a garbage collection which
1705 * could empty the dict, so if we checked the size first and that
1706 * happened, the result would be an infinite loop (searching for an
1707 * entry that no longer exists). Note that the usual popitem()
1708 * idiom is "while d: k, v = d.popitem()". so needing to throw the
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001709 * tuple away if the dict *is* empty isn't a significant
Tim Petersf4b33f62001-06-02 05:42:29 +00001710 * inefficiency -- possible, but unlikely in practice.
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001711 */
1712 res = PyTuple_New(2);
1713 if (res == NULL)
1714 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001715 if (mp->ma_used == 0) {
1716 Py_DECREF(res);
1717 PyErr_SetString(PyExc_KeyError,
1718 "popitem(): dictionary is empty");
1719 return NULL;
1720 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001721 /* Set ep to "the first" dict entry with a value. We abuse the hash
1722 * field of slot 0 to hold a search finger:
1723 * If slot 0 has a value, use slot 0.
1724 * Else slot 0 is being used to hold a search finger,
1725 * and we use its hash value as the first index to look.
1726 */
1727 ep = &mp->ma_table[0];
1728 if (ep->me_value == NULL) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001729 i = ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001730 /* The hash field may be a real hash value, or it may be a
1731 * legit search finger, or it may be a once-legit search
1732 * finger that's out of bounds now because it wrapped around
1733 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001734 */
Tim Petersafb6ae82001-06-04 21:00:21 +00001735 if (i > mp->ma_mask || i < 1)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001736 i = 1; /* skip slot 0 */
1737 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1738 i++;
Tim Petersafb6ae82001-06-04 21:00:21 +00001739 if (i > mp->ma_mask)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001740 i = 1;
1741 }
1742 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001743 PyTuple_SET_ITEM(res, 0, ep->me_key);
1744 PyTuple_SET_ITEM(res, 1, ep->me_value);
1745 Py_INCREF(dummy);
1746 ep->me_key = dummy;
1747 ep->me_value = NULL;
1748 mp->ma_used--;
1749 assert(mp->ma_table[0].me_value == NULL);
1750 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001751 return res;
1752}
1753
Jeremy Hylton8caad492000-06-23 14:18:11 +00001754static int
1755dict_traverse(PyObject *op, visitproc visit, void *arg)
1756{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001757 Py_ssize_t i = 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001758 PyObject *pk;
1759 PyObject *pv;
1760
1761 while (PyDict_Next(op, &i, &pk, &pv)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001762 Py_VISIT(pk);
1763 Py_VISIT(pv);
Jeremy Hylton8caad492000-06-23 14:18:11 +00001764 }
1765 return 0;
1766}
1767
1768static int
1769dict_tp_clear(PyObject *op)
1770{
1771 PyDict_Clear(op);
1772 return 0;
1773}
1774
Tim Petersf7f88b12000-12-13 23:18:45 +00001775
Raymond Hettinger019a1482004-03-18 02:41:19 +00001776extern PyTypeObject PyDictIterKey_Type; /* Forward */
1777extern PyTypeObject PyDictIterValue_Type; /* Forward */
1778extern PyTypeObject PyDictIterItem_Type; /* Forward */
1779static PyObject *dictiter_new(dictobject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001780
Guido van Rossum09e563a2001-05-01 12:10:21 +00001781
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001782PyDoc_STRVAR(contains__doc__,
1783"D.__contains__(k) -> True if D has a key k, else False");
1784
1785PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
1786
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001787PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001788"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001789
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001790PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001791"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 +00001792
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001793PyDoc_STRVAR(pop__doc__,
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001794"D.pop(k[,d]) -> v, remove specified key and return the corresponding value\n\
1795If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00001796
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001797PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00001798"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000017992-tuple; but raise KeyError if D is empty");
Tim Petersf7f88b12000-12-13 23:18:45 +00001800
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001801PyDoc_STRVAR(update__doc__,
Guido van Rossumb90c8482007-02-10 01:11:45 +00001802"D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k]\
1803\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 +00001804
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001805PyDoc_STRVAR(fromkeys__doc__,
1806"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
1807v defaults to None.");
1808
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001809PyDoc_STRVAR(clear__doc__,
1810"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001811
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001812PyDoc_STRVAR(copy__doc__,
1813"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00001814
Guido van Rossumb90c8482007-02-10 01:11:45 +00001815/* Forward */
1816static PyObject *dictkeys_new(PyObject *);
1817static PyObject *dictitems_new(PyObject *);
1818static PyObject *dictvalues_new(PyObject *);
1819
Guido van Rossum45c85d12007-07-27 16:31:40 +00001820PyDoc_STRVAR(keys__doc__,
1821 "D.keys() -> a set-like object providing a view on D's keys");
1822PyDoc_STRVAR(items__doc__,
1823 "D.items() -> a set-like object providing a view on D's items");
1824PyDoc_STRVAR(values__doc__,
1825 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00001826
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001827static PyMethodDef mapp_methods[] = {
Guido van Rossume2b70bc2006-08-18 22:13:04 +00001828 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001829 contains__doc__},
Raymond Hettinger0c669672003-12-13 13:31:55 +00001830 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001831 getitem__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00001832 {"get", (PyCFunction)dict_get, METH_VARARGS,
1833 get__doc__},
1834 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1835 setdefault_doc__},
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001836 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
Just van Rossuma797d812002-11-23 09:45:04 +00001837 pop__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001838 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001839 popitem__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001840 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00001841 keys__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001842 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00001843 items__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001844 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00001845 values__doc__},
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001846 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001847 update__doc__},
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001848 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
1849 fromkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001850 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001851 clear__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001852 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001853 copy__doc__},
1854 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001855};
1856
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001857/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001858int
1859PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001860{
1861 long hash;
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001862 dictobject *mp = (dictobject *)op;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001863 dictentry *ep;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001864
Guido van Rossum89d8c602007-09-18 17:26:56 +00001865 if (!PyUnicode_CheckExact(key) ||
1866 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001867 hash = PyObject_Hash(key);
1868 if (hash == -1)
1869 return -1;
1870 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001871 ep = (mp->ma_lookup)(mp, key, hash);
1872 return ep == NULL ? -1 : (ep->me_value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001873}
1874
Thomas Wouterscf297e42007-02-23 15:07:44 +00001875/* Internal version of PyDict_Contains used when the hash value is already known */
1876int
1877_PyDict_Contains(PyObject *op, PyObject *key, long hash)
1878{
1879 dictobject *mp = (dictobject *)op;
1880 dictentry *ep;
1881
1882 ep = (mp->ma_lookup)(mp, key, hash);
1883 return ep == NULL ? -1 : (ep->me_value != NULL);
1884}
1885
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001886/* Hack to implement "key in dict" */
1887static PySequenceMethods dict_as_sequence = {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001888 0, /* sq_length */
1889 0, /* sq_concat */
1890 0, /* sq_repeat */
1891 0, /* sq_item */
1892 0, /* sq_slice */
1893 0, /* sq_ass_item */
1894 0, /* sq_ass_slice */
1895 PyDict_Contains, /* sq_contains */
1896 0, /* sq_inplace_concat */
1897 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001898};
1899
Guido van Rossum09e563a2001-05-01 12:10:21 +00001900static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001901dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1902{
1903 PyObject *self;
1904
1905 assert(type != NULL && type->tp_alloc != NULL);
1906 self = type->tp_alloc(type, 0);
1907 if (self != NULL) {
1908 PyDictObject *d = (PyDictObject *)self;
1909 /* It's guaranteed that tp->alloc zeroed out the struct. */
1910 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
1911 INIT_NONZERO_DICT_SLOTS(d);
Guido van Rossum89d8c602007-09-18 17:26:56 +00001912 d->ma_lookup = lookdict_unicode;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001913#ifdef SHOW_CONVERSION_COUNTS
1914 ++created;
1915#endif
1916 }
1917 return self;
1918}
1919
Tim Peters25786c02001-09-02 08:22:48 +00001920static int
1921dict_init(PyObject *self, PyObject *args, PyObject *kwds)
1922{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001923 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00001924}
1925
Tim Peters6d6c1a32001-08-02 04:15:00 +00001926static PyObject *
Guido van Rossum09e563a2001-05-01 12:10:21 +00001927dict_iter(dictobject *dict)
1928{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001929 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001930}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001931
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001932PyDoc_STRVAR(dictionary_doc,
Tim Petersa427a2b2001-10-29 22:25:45 +00001933"dict() -> new empty dictionary.\n"
1934"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Tim Peters1fc240e2001-10-26 05:06:50 +00001935" (key, value) pairs.\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00001936"dict(seq) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00001937" d = {}\n"
1938" for k, v in seq:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00001939" d[k] = v\n"
1940"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
1941" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00001942
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001943PyTypeObject PyDict_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001944 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Petersa427a2b2001-10-29 22:25:45 +00001945 "dict",
Neil Schemenauere83c00e2001-08-29 23:54:21 +00001946 sizeof(dictobject),
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001947 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00001948 (destructor)dict_dealloc, /* tp_dealloc */
Guido van Rossum04dbf3b2007-08-07 19:51:00 +00001949 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001950 0, /* tp_getattr */
Guido van Rossumb9324202001-01-18 00:39:02 +00001951 0, /* tp_setattr */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001952 0, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00001953 (reprfunc)dict_repr, /* tp_repr */
1954 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001955 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00001956 &dict_as_mapping, /* tp_as_mapping */
Guido van Rossum50e9fb92006-08-17 05:42:55 +00001957 0, /* tp_hash */
Guido van Rossumb9324202001-01-18 00:39:02 +00001958 0, /* tp_call */
1959 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001960 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumb9324202001-01-18 00:39:02 +00001961 0, /* tp_setattro */
1962 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00001963 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Thomas Wouters27d517b2007-02-25 20:39:11 +00001964 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
Tim Peters25786c02001-09-02 08:22:48 +00001965 dictionary_doc, /* tp_doc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001966 dict_traverse, /* tp_traverse */
1967 dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00001968 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001969 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00001970 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001971 0, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001972 mapp_methods, /* tp_methods */
1973 0, /* tp_members */
1974 0, /* tp_getset */
1975 0, /* tp_base */
1976 0, /* tp_dict */
1977 0, /* tp_descr_get */
1978 0, /* tp_descr_set */
1979 0, /* tp_dictoffset */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001980 dict_init, /* tp_init */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001981 PyType_GenericAlloc, /* tp_alloc */
1982 dict_new, /* tp_new */
Neil Schemenauer6189b892002-04-12 02:43:00 +00001983 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001984};
1985
Guido van Rossum3cca2451997-05-16 14:23:33 +00001986/* For backward compatibility with old dictionary interface */
1987
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001988PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00001989PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001990{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001991 PyObject *kv, *rv;
Martin v. Löwis5b222132007-06-10 09:51:05 +00001992 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00001993 if (kv == NULL)
1994 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00001995 rv = PyDict_GetItem(v, kv);
1996 Py_DECREF(kv);
1997 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001998}
1999
2000int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002001PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002002{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002003 PyObject *kv;
2004 int err;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002005 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002006 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002007 return -1;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002008 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00002009 err = PyDict_SetItem(v, kv, item);
2010 Py_DECREF(kv);
2011 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002012}
2013
2014int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002015PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002016{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002017 PyObject *kv;
2018 int err;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002019 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002020 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002021 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002022 err = PyDict_DelItem(v, kv);
2023 Py_DECREF(kv);
2024 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002025}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002026
Raymond Hettinger019a1482004-03-18 02:41:19 +00002027/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002028
2029typedef struct {
2030 PyObject_HEAD
Guido van Rossum2147df72002-07-16 20:30:22 +00002031 dictobject *di_dict; /* Set to NULL when iterator is exhausted */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002032 Py_ssize_t di_used;
2033 Py_ssize_t di_pos;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002034 PyObject* di_result; /* reusable result tuple for iteritems */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002035 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002036} dictiterobject;
2037
2038static PyObject *
Raymond Hettinger019a1482004-03-18 02:41:19 +00002039dictiter_new(dictobject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002040{
2041 dictiterobject *di;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002042 di = PyObject_New(dictiterobject, itertype);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002043 if (di == NULL)
2044 return NULL;
2045 Py_INCREF(dict);
2046 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00002047 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002048 di->di_pos = 0;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002049 di->len = dict->ma_used;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002050 if (itertype == &PyDictIterItem_Type) {
2051 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2052 if (di->di_result == NULL) {
2053 Py_DECREF(di);
2054 return NULL;
2055 }
2056 }
2057 else
2058 di->di_result = NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002059 return (PyObject *)di;
2060}
2061
2062static void
2063dictiter_dealloc(dictiterobject *di)
2064{
Guido van Rossum2147df72002-07-16 20:30:22 +00002065 Py_XDECREF(di->di_dict);
Raymond Hettinger019a1482004-03-18 02:41:19 +00002066 Py_XDECREF(di->di_result);
Neil Schemenauer6189b892002-04-12 02:43:00 +00002067 PyObject_Del(di);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002068}
2069
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002070static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002071dictiter_len(dictiterobject *di)
2072{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002073 Py_ssize_t len = 0;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00002074 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002075 len = di->len;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002076 return PyInt_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002077}
2078
Guido van Rossumb90c8482007-02-10 01:11:45 +00002079PyDoc_STRVAR(length_hint_doc,
2080 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002081
2082static PyMethodDef dictiter_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002083 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
2084 length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002085 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002086};
2087
Raymond Hettinger019a1482004-03-18 02:41:19 +00002088static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002089{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002090 PyObject *key;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002091 register Py_ssize_t i, mask;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002092 register dictentry *ep;
2093 dictobject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002094
Raymond Hettinger019a1482004-03-18 02:41:19 +00002095 if (d == NULL)
Guido van Rossum2147df72002-07-16 20:30:22 +00002096 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002097 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002098
Raymond Hettinger019a1482004-03-18 02:41:19 +00002099 if (di->di_used != d->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00002100 PyErr_SetString(PyExc_RuntimeError,
2101 "dictionary changed size during iteration");
Guido van Rossum2147df72002-07-16 20:30:22 +00002102 di->di_used = -1; /* Make this state sticky */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002103 return NULL;
2104 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002105
Raymond Hettinger019a1482004-03-18 02:41:19 +00002106 i = di->di_pos;
2107 if (i < 0)
2108 goto fail;
2109 ep = d->ma_table;
2110 mask = d->ma_mask;
2111 while (i <= mask && ep[i].me_value == NULL)
2112 i++;
2113 di->di_pos = i+1;
2114 if (i > mask)
2115 goto fail;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002116 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002117 key = ep[i].me_key;
2118 Py_INCREF(key);
2119 return key;
2120
2121fail:
2122 Py_DECREF(d);
Guido van Rossum2147df72002-07-16 20:30:22 +00002123 di->di_dict = NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002124 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002125}
2126
Raymond Hettinger019a1482004-03-18 02:41:19 +00002127PyTypeObject PyDictIterKey_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002128 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002129 "dictionary-keyiterator", /* tp_name */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002130 sizeof(dictiterobject), /* tp_basicsize */
2131 0, /* tp_itemsize */
2132 /* methods */
2133 (destructor)dictiter_dealloc, /* tp_dealloc */
2134 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002135 0, /* tp_getattr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002136 0, /* tp_setattr */
2137 0, /* tp_compare */
2138 0, /* tp_repr */
2139 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002140 0, /* tp_as_sequence */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002141 0, /* tp_as_mapping */
2142 0, /* tp_hash */
2143 0, /* tp_call */
2144 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002145 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002146 0, /* tp_setattro */
2147 0, /* tp_as_buffer */
2148 Py_TPFLAGS_DEFAULT, /* tp_flags */
2149 0, /* tp_doc */
2150 0, /* tp_traverse */
2151 0, /* tp_clear */
2152 0, /* tp_richcompare */
2153 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002154 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002155 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002156 dictiter_methods, /* tp_methods */
2157 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002158};
2159
2160static PyObject *dictiter_iternextvalue(dictiterobject *di)
2161{
2162 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002163 register Py_ssize_t i, mask;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002164 register dictentry *ep;
2165 dictobject *d = di->di_dict;
2166
2167 if (d == NULL)
2168 return NULL;
2169 assert (PyDict_Check(d));
2170
2171 if (di->di_used != d->ma_used) {
2172 PyErr_SetString(PyExc_RuntimeError,
2173 "dictionary changed size during iteration");
2174 di->di_used = -1; /* Make this state sticky */
2175 return NULL;
2176 }
2177
2178 i = di->di_pos;
Guido van Rossum09240f62004-03-20 19:11:58 +00002179 mask = d->ma_mask;
2180 if (i < 0 || i > mask)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002181 goto fail;
2182 ep = d->ma_table;
Guido van Rossum09240f62004-03-20 19:11:58 +00002183 while ((value=ep[i].me_value) == NULL) {
Raymond Hettinger019a1482004-03-18 02:41:19 +00002184 i++;
Guido van Rossum09240f62004-03-20 19:11:58 +00002185 if (i > mask)
2186 goto fail;
2187 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002188 di->di_pos = i+1;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002189 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002190 Py_INCREF(value);
2191 return value;
2192
2193fail:
2194 Py_DECREF(d);
2195 di->di_dict = NULL;
2196 return NULL;
2197}
2198
2199PyTypeObject PyDictIterValue_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002200 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002201 "dictionary-valueiterator", /* tp_name */
2202 sizeof(dictiterobject), /* tp_basicsize */
2203 0, /* tp_itemsize */
2204 /* methods */
2205 (destructor)dictiter_dealloc, /* tp_dealloc */
2206 0, /* tp_print */
2207 0, /* tp_getattr */
2208 0, /* tp_setattr */
2209 0, /* tp_compare */
2210 0, /* tp_repr */
2211 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002212 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002213 0, /* tp_as_mapping */
2214 0, /* tp_hash */
2215 0, /* tp_call */
2216 0, /* tp_str */
2217 PyObject_GenericGetAttr, /* tp_getattro */
2218 0, /* tp_setattro */
2219 0, /* tp_as_buffer */
2220 Py_TPFLAGS_DEFAULT, /* tp_flags */
2221 0, /* tp_doc */
2222 0, /* tp_traverse */
2223 0, /* tp_clear */
2224 0, /* tp_richcompare */
2225 0, /* tp_weaklistoffset */
2226 PyObject_SelfIter, /* tp_iter */
2227 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002228 dictiter_methods, /* tp_methods */
2229 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002230};
2231
2232static PyObject *dictiter_iternextitem(dictiterobject *di)
2233{
2234 PyObject *key, *value, *result = di->di_result;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002235 register Py_ssize_t i, mask;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002236 register dictentry *ep;
2237 dictobject *d = di->di_dict;
2238
2239 if (d == NULL)
2240 return NULL;
2241 assert (PyDict_Check(d));
2242
2243 if (di->di_used != d->ma_used) {
2244 PyErr_SetString(PyExc_RuntimeError,
2245 "dictionary changed size during iteration");
2246 di->di_used = -1; /* Make this state sticky */
2247 return NULL;
2248 }
2249
2250 i = di->di_pos;
2251 if (i < 0)
2252 goto fail;
2253 ep = d->ma_table;
2254 mask = d->ma_mask;
2255 while (i <= mask && ep[i].me_value == NULL)
2256 i++;
2257 di->di_pos = i+1;
2258 if (i > mask)
2259 goto fail;
2260
2261 if (result->ob_refcnt == 1) {
2262 Py_INCREF(result);
2263 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2264 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2265 } else {
2266 result = PyTuple_New(2);
Tim Peters60b29962006-01-01 01:19:23 +00002267 if (result == NULL)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002268 return NULL;
2269 }
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002270 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002271 key = ep[i].me_key;
2272 value = ep[i].me_value;
2273 Py_INCREF(key);
2274 Py_INCREF(value);
2275 PyTuple_SET_ITEM(result, 0, key);
2276 PyTuple_SET_ITEM(result, 1, value);
2277 return result;
2278
2279fail:
2280 Py_DECREF(d);
2281 di->di_dict = NULL;
2282 return NULL;
2283}
2284
2285PyTypeObject PyDictIterItem_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002286 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002287 "dictionary-itemiterator", /* tp_name */
2288 sizeof(dictiterobject), /* tp_basicsize */
2289 0, /* tp_itemsize */
2290 /* methods */
2291 (destructor)dictiter_dealloc, /* tp_dealloc */
2292 0, /* tp_print */
2293 0, /* tp_getattr */
2294 0, /* tp_setattr */
2295 0, /* tp_compare */
2296 0, /* tp_repr */
2297 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002298 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002299 0, /* tp_as_mapping */
2300 0, /* tp_hash */
2301 0, /* tp_call */
2302 0, /* tp_str */
2303 PyObject_GenericGetAttr, /* tp_getattro */
2304 0, /* tp_setattro */
2305 0, /* tp_as_buffer */
2306 Py_TPFLAGS_DEFAULT, /* tp_flags */
2307 0, /* tp_doc */
2308 0, /* tp_traverse */
2309 0, /* tp_clear */
2310 0, /* tp_richcompare */
2311 0, /* tp_weaklistoffset */
2312 PyObject_SelfIter, /* tp_iter */
2313 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002314 dictiter_methods, /* tp_methods */
2315 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002316};
Guido van Rossumb90c8482007-02-10 01:11:45 +00002317
2318
Guido van Rossum3ac67412007-02-10 18:55:06 +00002319/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002320/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002321/***********************************************/
2322
Guido van Rossumb90c8482007-02-10 01:11:45 +00002323/* The instance lay-out is the same for all three; but the type differs. */
2324
2325typedef struct {
2326 PyObject_HEAD
Guido van Rossum3ac67412007-02-10 18:55:06 +00002327 dictobject *dv_dict;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002328} dictviewobject;
2329
2330
2331static void
Guido van Rossum3ac67412007-02-10 18:55:06 +00002332dictview_dealloc(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002333{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002334 Py_XDECREF(dv->dv_dict);
2335 PyObject_Del(dv);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002336}
2337
Guido van Rossum83825ac2007-02-10 04:54:19 +00002338static Py_ssize_t
Guido van Rossum3ac67412007-02-10 18:55:06 +00002339dictview_len(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002340{
2341 Py_ssize_t len = 0;
Guido van Rossum3ac67412007-02-10 18:55:06 +00002342 if (dv->dv_dict != NULL)
2343 len = dv->dv_dict->ma_used;
Guido van Rossum83825ac2007-02-10 04:54:19 +00002344 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002345}
2346
2347static PyObject *
2348dictview_new(PyObject *dict, PyTypeObject *type)
2349{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002350 dictviewobject *dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002351 if (dict == NULL) {
2352 PyErr_BadInternalCall();
2353 return NULL;
2354 }
2355 if (!PyDict_Check(dict)) {
2356 /* XXX Get rid of this restriction later */
2357 PyErr_Format(PyExc_TypeError,
2358 "%s() requires a dict argument, not '%s'",
2359 type->tp_name, dict->ob_type->tp_name);
2360 return NULL;
2361 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002362 dv = PyObject_New(dictviewobject, type);
2363 if (dv == NULL)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002364 return NULL;
2365 Py_INCREF(dict);
Guido van Rossum3ac67412007-02-10 18:55:06 +00002366 dv->dv_dict = (dictobject *)dict;
2367 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002368}
2369
Neal Norwitze36f2ba2007-02-26 23:12:28 +00002370/* TODO(guido): The views objects are not complete:
2371
2372 * support more set operations
2373 * support arbitrary mappings?
2374 - either these should be static or exported in dictobject.h
2375 - if public then they should probably be in builtins
2376*/
2377
Guido van Rossumd9214d12007-02-12 02:23:40 +00002378/* Forward */
2379PyTypeObject PyDictKeys_Type;
2380PyTypeObject PyDictItems_Type;
2381PyTypeObject PyDictValues_Type;
2382
2383#define PyDictKeys_Check(obj) ((obj)->ob_type == &PyDictKeys_Type)
2384#define PyDictItems_Check(obj) ((obj)->ob_type == &PyDictItems_Type)
2385#define PyDictValues_Check(obj) ((obj)->ob_type == &PyDictValues_Type)
2386
2387/* This excludes Values, since they are not sets. */
2388# define PyDictViewSet_Check(obj) \
2389 (PyDictKeys_Check(obj) || PyDictItems_Check(obj))
2390
Guido van Rossumaac530c2007-08-24 22:33:45 +00002391/* Return 1 if self is a subset of other, iterating over self;
2392 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002393static int
2394all_contained_in(PyObject *self, PyObject *other)
2395{
2396 PyObject *iter = PyObject_GetIter(self);
2397 int ok = 1;
2398
2399 if (iter == NULL)
2400 return -1;
2401 for (;;) {
2402 PyObject *next = PyIter_Next(iter);
2403 if (next == NULL) {
2404 if (PyErr_Occurred())
2405 ok = -1;
2406 break;
2407 }
2408 ok = PySequence_Contains(other, next);
2409 Py_DECREF(next);
2410 if (ok <= 0)
2411 break;
2412 }
2413 Py_DECREF(iter);
2414 return ok;
2415}
2416
2417static PyObject *
2418dictview_richcompare(PyObject *self, PyObject *other, int op)
2419{
Guido van Rossumaac530c2007-08-24 22:33:45 +00002420 Py_ssize_t len_self, len_other;
2421 int ok;
2422 PyObject *result;
2423
Guido van Rossumd9214d12007-02-12 02:23:40 +00002424 assert(self != NULL);
2425 assert(PyDictViewSet_Check(self));
2426 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00002427
Guido van Rossumaac530c2007-08-24 22:33:45 +00002428 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other)) {
Guido van Rossumd9214d12007-02-12 02:23:40 +00002429 Py_INCREF(Py_NotImplemented);
2430 return Py_NotImplemented;
2431 }
Guido van Rossumaac530c2007-08-24 22:33:45 +00002432
2433 len_self = PyObject_Size(self);
2434 if (len_self < 0)
2435 return NULL;
2436 len_other = PyObject_Size(other);
2437 if (len_other < 0)
2438 return NULL;
2439
2440 ok = 0;
2441 switch(op) {
2442
2443 case Py_NE:
2444 case Py_EQ:
2445 if (len_self == len_other)
2446 ok = all_contained_in(self, other);
2447 if (op == Py_NE && ok >= 0)
2448 ok = !ok;
2449 break;
2450
2451 case Py_LT:
2452 if (len_self < len_other)
2453 ok = all_contained_in(self, other);
2454 break;
2455
2456 case Py_LE:
2457 if (len_self <= len_other)
2458 ok = all_contained_in(self, other);
2459 break;
2460
2461 case Py_GT:
2462 if (len_self > len_other)
2463 ok = all_contained_in(other, self);
2464 break;
2465
2466 case Py_GE:
2467 if (len_self >= len_other)
2468 ok = all_contained_in(other, self);
2469 break;
2470
2471 }
2472 if (ok < 0)
2473 return NULL;
2474 result = ok ? Py_True : Py_False;
2475 Py_INCREF(result);
2476 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00002477}
2478
Guido van Rossum3ac67412007-02-10 18:55:06 +00002479/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002480
2481static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002482dictkeys_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002483{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002484 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002485 Py_RETURN_NONE;
2486 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002487 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
2488}
2489
2490static int
2491dictkeys_contains(dictviewobject *dv, PyObject *obj)
2492{
2493 if (dv->dv_dict == NULL)
2494 return 0;
2495 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002496}
2497
Guido van Rossum83825ac2007-02-10 04:54:19 +00002498static PySequenceMethods dictkeys_as_sequence = {
2499 (lenfunc)dictview_len, /* sq_length */
2500 0, /* sq_concat */
2501 0, /* sq_repeat */
2502 0, /* sq_item */
2503 0, /* sq_slice */
2504 0, /* sq_ass_item */
2505 0, /* sq_ass_slice */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002506 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002507};
2508
Guido van Rossum523259b2007-08-24 23:41:22 +00002509static PyObject*
2510dictviews_sub(PyObject* self, PyObject *other)
2511{
2512 PyObject *result = PySet_New(self);
2513 PyObject *tmp;
2514 if (result == NULL)
2515 return NULL;
2516
2517 tmp = PyObject_CallMethod(result, "difference_update", "O", other);
2518 if (tmp == NULL) {
2519 Py_DECREF(result);
2520 return NULL;
2521 }
2522
2523 Py_DECREF(tmp);
2524 return result;
2525}
2526
2527static PyObject*
2528dictviews_and(PyObject* self, PyObject *other)
2529{
2530 PyObject *result = PySet_New(self);
2531 PyObject *tmp;
2532 if (result == NULL)
2533 return NULL;
2534
2535 tmp = PyObject_CallMethod(result, "intersection_update", "O", other);
2536 if (tmp == NULL) {
2537 Py_DECREF(result);
2538 return NULL;
2539 }
2540
2541 Py_DECREF(tmp);
2542 return result;
2543}
2544
2545static PyObject*
2546dictviews_or(PyObject* self, PyObject *other)
2547{
2548 PyObject *result = PySet_New(self);
2549 PyObject *tmp;
2550 if (result == NULL)
2551 return NULL;
2552
2553 tmp = PyObject_CallMethod(result, "update", "O", other);
2554 if (tmp == NULL) {
2555 Py_DECREF(result);
2556 return NULL;
2557 }
2558
2559 Py_DECREF(tmp);
2560 return result;
2561}
2562
2563static PyObject*
2564dictviews_xor(PyObject* self, PyObject *other)
2565{
2566 PyObject *result = PySet_New(self);
2567 PyObject *tmp;
2568 if (result == NULL)
2569 return NULL;
2570
2571 tmp = PyObject_CallMethod(result, "symmetric_difference_update", "O",
2572 other);
2573 if (tmp == NULL) {
2574 Py_DECREF(result);
2575 return NULL;
2576 }
2577
2578 Py_DECREF(tmp);
2579 return result;
2580}
2581
2582static PyNumberMethods dictviews_as_number = {
2583 0, /*nb_add*/
2584 (binaryfunc)dictviews_sub, /*nb_subtract*/
2585 0, /*nb_multiply*/
2586 0, /*nb_remainder*/
2587 0, /*nb_divmod*/
2588 0, /*nb_power*/
2589 0, /*nb_negative*/
2590 0, /*nb_positive*/
2591 0, /*nb_absolute*/
2592 0, /*nb_bool*/
2593 0, /*nb_invert*/
2594 0, /*nb_lshift*/
2595 0, /*nb_rshift*/
2596 (binaryfunc)dictviews_and, /*nb_and*/
2597 (binaryfunc)dictviews_xor, /*nb_xor*/
2598 (binaryfunc)dictviews_or, /*nb_or*/
2599};
2600
Guido van Rossumb90c8482007-02-10 01:11:45 +00002601static PyMethodDef dictkeys_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002602 {NULL, NULL} /* sentinel */
2603};
2604
2605PyTypeObject PyDictKeys_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002606 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002607 "dict_keys", /* tp_name */
2608 sizeof(dictviewobject), /* tp_basicsize */
2609 0, /* tp_itemsize */
2610 /* methods */
2611 (destructor)dictview_dealloc, /* tp_dealloc */
2612 0, /* tp_print */
2613 0, /* tp_getattr */
2614 0, /* tp_setattr */
2615 0, /* tp_compare */
2616 0, /* tp_repr */
Guido van Rossum523259b2007-08-24 23:41:22 +00002617 &dictviews_as_number, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002618 &dictkeys_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002619 0, /* tp_as_mapping */
2620 0, /* tp_hash */
2621 0, /* tp_call */
2622 0, /* tp_str */
2623 PyObject_GenericGetAttr, /* tp_getattro */
2624 0, /* tp_setattro */
2625 0, /* tp_as_buffer */
2626 Py_TPFLAGS_DEFAULT, /* tp_flags */
2627 0, /* tp_doc */
2628 0, /* tp_traverse */
2629 0, /* tp_clear */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002630 dictview_richcompare, /* tp_richcompare */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002631 0, /* tp_weaklistoffset */
2632 (getiterfunc)dictkeys_iter, /* tp_iter */
2633 0, /* tp_iternext */
2634 dictkeys_methods, /* tp_methods */
2635 0,
2636};
2637
2638static PyObject *
2639dictkeys_new(PyObject *dict)
2640{
2641 return dictview_new(dict, &PyDictKeys_Type);
2642}
2643
Guido van Rossum3ac67412007-02-10 18:55:06 +00002644/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002645
2646static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002647dictitems_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002648{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002649 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002650 Py_RETURN_NONE;
2651 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002652 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
2653}
2654
2655static int
2656dictitems_contains(dictviewobject *dv, PyObject *obj)
2657{
2658 PyObject *key, *value, *found;
2659 if (dv->dv_dict == NULL)
2660 return 0;
2661 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
2662 return 0;
2663 key = PyTuple_GET_ITEM(obj, 0);
2664 value = PyTuple_GET_ITEM(obj, 1);
2665 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
2666 if (found == NULL) {
2667 if (PyErr_Occurred())
2668 return -1;
2669 return 0;
2670 }
2671 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002672}
2673
Guido van Rossum83825ac2007-02-10 04:54:19 +00002674static PySequenceMethods dictitems_as_sequence = {
2675 (lenfunc)dictview_len, /* sq_length */
2676 0, /* sq_concat */
2677 0, /* sq_repeat */
2678 0, /* sq_item */
2679 0, /* sq_slice */
2680 0, /* sq_ass_item */
2681 0, /* sq_ass_slice */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002682 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002683};
2684
Guido van Rossumb90c8482007-02-10 01:11:45 +00002685static PyMethodDef dictitems_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002686 {NULL, NULL} /* sentinel */
2687};
2688
2689PyTypeObject PyDictItems_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002690 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002691 "dict_items", /* tp_name */
2692 sizeof(dictviewobject), /* tp_basicsize */
2693 0, /* tp_itemsize */
2694 /* methods */
2695 (destructor)dictview_dealloc, /* tp_dealloc */
2696 0, /* tp_print */
2697 0, /* tp_getattr */
2698 0, /* tp_setattr */
2699 0, /* tp_compare */
2700 0, /* tp_repr */
Guido van Rossum523259b2007-08-24 23:41:22 +00002701 &dictviews_as_number, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002702 &dictitems_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002703 0, /* tp_as_mapping */
2704 0, /* tp_hash */
2705 0, /* tp_call */
2706 0, /* tp_str */
2707 PyObject_GenericGetAttr, /* tp_getattro */
2708 0, /* tp_setattro */
2709 0, /* tp_as_buffer */
2710 Py_TPFLAGS_DEFAULT, /* tp_flags */
2711 0, /* tp_doc */
2712 0, /* tp_traverse */
2713 0, /* tp_clear */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002714 dictview_richcompare, /* tp_richcompare */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002715 0, /* tp_weaklistoffset */
2716 (getiterfunc)dictitems_iter, /* tp_iter */
2717 0, /* tp_iternext */
2718 dictitems_methods, /* tp_methods */
2719 0,
2720};
2721
2722static PyObject *
2723dictitems_new(PyObject *dict)
2724{
2725 return dictview_new(dict, &PyDictItems_Type);
2726}
2727
Guido van Rossum3ac67412007-02-10 18:55:06 +00002728/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002729
2730static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002731dictvalues_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002732{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002733 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002734 Py_RETURN_NONE;
2735 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002736 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002737}
2738
Guido van Rossum83825ac2007-02-10 04:54:19 +00002739static PySequenceMethods dictvalues_as_sequence = {
2740 (lenfunc)dictview_len, /* sq_length */
2741 0, /* sq_concat */
2742 0, /* sq_repeat */
2743 0, /* sq_item */
2744 0, /* sq_slice */
2745 0, /* sq_ass_item */
2746 0, /* sq_ass_slice */
2747 (objobjproc)0, /* sq_contains */
2748};
2749
Guido van Rossumb90c8482007-02-10 01:11:45 +00002750static PyMethodDef dictvalues_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002751 {NULL, NULL} /* sentinel */
2752};
2753
2754PyTypeObject PyDictValues_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002755 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002756 "dict_values", /* tp_name */
2757 sizeof(dictviewobject), /* tp_basicsize */
2758 0, /* tp_itemsize */
2759 /* methods */
2760 (destructor)dictview_dealloc, /* tp_dealloc */
2761 0, /* tp_print */
2762 0, /* tp_getattr */
2763 0, /* tp_setattr */
2764 0, /* tp_compare */
2765 0, /* tp_repr */
2766 0, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002767 &dictvalues_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002768 0, /* tp_as_mapping */
2769 0, /* tp_hash */
2770 0, /* tp_call */
2771 0, /* tp_str */
2772 PyObject_GenericGetAttr, /* tp_getattro */
2773 0, /* tp_setattro */
2774 0, /* tp_as_buffer */
2775 Py_TPFLAGS_DEFAULT, /* tp_flags */
2776 0, /* tp_doc */
2777 0, /* tp_traverse */
2778 0, /* tp_clear */
2779 0, /* tp_richcompare */
2780 0, /* tp_weaklistoffset */
2781 (getiterfunc)dictvalues_iter, /* tp_iter */
2782 0, /* tp_iternext */
2783 dictvalues_methods, /* tp_methods */
2784 0,
2785};
2786
2787static PyObject *
2788dictvalues_new(PyObject *dict)
2789{
2790 return dictview_new(dict, &PyDictValues_Type);
2791}