blob: 05252d7f53dbab44361f70e9c500632732457d93 [file] [log] [blame]
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001
Guido van Rossum2bc13791999-03-24 19:06:42 +00002/* Dictionary object implementation using a hash table */
Guido van Rossum9bfef441993-03-29 10:43:31 +00003
Raymond Hettinger930427b2003-05-03 06:51:59 +00004/* The distribution includes a separate file, Objects/dictnotes.txt,
Tim Peters60b29962006-01-01 01:19:23 +00005 describing explorations into dictionary design and optimization.
Raymond Hettinger930427b2003-05-03 06:51:59 +00006 It covers typical dictionary use patterns, the parameters for
7 tuning dictionaries, and several ideas for possible optimizations.
8*/
9
Guido van Rossumc0b618a1997-05-02 03:12:38 +000010#include "Python.h"
Christian Heimes0ded5b52007-12-10 15:50:56 +000011#include "stringlib/eq.h"
Guido van Rossum4b1302b1993-03-27 18:11:32 +000012
Guido van Rossum16e93a81997-01-28 00:00:11 +000013
Thomas Wouters89f507f2006-12-13 04:49:30 +000014/* Set a key error with the specified argument, wrapping it in a
15 * tuple automatically so that tuple keys are not unpacked as the
16 * exception arguments. */
17static void
18set_key_error(PyObject *arg)
19{
20 PyObject *tup;
21 tup = PyTuple_Pack(1, arg);
22 if (!tup)
23 return; /* caller will expect error to be set anyway */
24 PyErr_SetObject(PyExc_KeyError, tup);
25 Py_DECREF(tup);
26}
27
Tim Peterseb28ef22001-06-02 05:27:19 +000028/* Define this out if you don't want conversion statistics on exit. */
Fred Drake1bff34a2000-08-31 19:31:38 +000029#undef SHOW_CONVERSION_COUNTS
30
Tim Peterseb28ef22001-06-02 05:27:19 +000031/* See large comment block below. This must be >= 1. */
32#define PERTURB_SHIFT 5
33
Guido van Rossum16e93a81997-01-28 00:00:11 +000034/*
Tim Peterseb28ef22001-06-02 05:27:19 +000035Major subtleties ahead: Most hash schemes depend on having a "good" hash
36function, in the sense of simulating randomness. Python doesn't: its most
37important hash functions (for strings and ints) are very regular in common
38cases:
Tim Peters15d49292001-05-27 07:39:22 +000039
Guido van Rossumdc5f6b22006-08-24 21:29:26 +000040 >>> map(hash, (0, 1, 2, 3))
41 [0, 1, 2, 3]
42 >>> map(hash, ("namea", "nameb", "namec", "named"))
43 [-1658398457, -1658398460, -1658398459, -1658398462]
44 >>>
Tim Peters15d49292001-05-27 07:39:22 +000045
Tim Peterseb28ef22001-06-02 05:27:19 +000046This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
47the low-order i bits as the initial table index is extremely fast, and there
48are no collisions at all for dicts indexed by a contiguous range of ints.
49The same is approximately true when keys are "consecutive" strings. So this
50gives better-than-random behavior in common cases, and that's very desirable.
Tim Peters15d49292001-05-27 07:39:22 +000051
Tim Peterseb28ef22001-06-02 05:27:19 +000052OTOH, when collisions occur, the tendency to fill contiguous slices of the
53hash table makes a good collision resolution strategy crucial. Taking only
54the last i bits of the hash code is also vulnerable: for example, consider
Guido van Rossumdc5f6b22006-08-24 21:29:26 +000055the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
56their own hash codes, and this fits in a dict of size 2**15, the last 15 bits
57 of every hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +000058
Tim Peterseb28ef22001-06-02 05:27:19 +000059But catering to unusual cases should not slow the usual ones, so we just take
60the last i bits anyway. It's up to collision resolution to do the rest. If
61we *usually* find the key we're looking for on the first try (and, it turns
62out, we usually do -- the table load factor is kept under 2/3, so the odds
63are solidly in our favor), then it makes best sense to keep the initial index
64computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +000065
Tim Peterseb28ef22001-06-02 05:27:19 +000066The first half of collision resolution is to visit table indices via this
67recurrence:
Tim Peters15d49292001-05-27 07:39:22 +000068
Tim Peterseb28ef22001-06-02 05:27:19 +000069 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +000070
Tim Peterseb28ef22001-06-02 05:27:19 +000071For any initial j in range(2**i), repeating that 2**i times generates each
72int in range(2**i) exactly once (see any text on random-number generation for
73proof). By itself, this doesn't help much: like linear probing (setting
74j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
75order. This would be bad, except that's not the only thing we do, and it's
76actually *good* in the common cases where hash keys are consecutive. In an
77example that's really too small to make this entirely clear, for a table of
78size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +000079
Tim Peterseb28ef22001-06-02 05:27:19 +000080 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
81
82If two things come in at index 5, the first place we look after is index 2,
83not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
84Linear probing is deadly in this case because there the fixed probe order
85is the *same* as the order consecutive keys are likely to arrive. But it's
86extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
87and certain that consecutive hash codes do not.
88
89The other half of the strategy is to get the other bits of the hash code
90into play. This is done by initializing a (unsigned) vrbl "perturb" to the
91full hash code, and changing the recurrence to:
92
93 j = (5*j) + 1 + perturb;
94 perturb >>= PERTURB_SHIFT;
95 use j % 2**i as the next table index;
96
97Now the probe sequence depends (eventually) on every bit in the hash code,
98and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
99because it quickly magnifies small differences in the bits that didn't affect
100the initial index. Note that because perturb is unsigned, if the recurrence
101is executed often enough perturb eventually becomes and remains 0. At that
102point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
103that's certain to find an empty slot eventually (since it generates every int
104in range(2**i), and we make sure there's always at least one empty slot).
105
106Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
107small so that the high bits of the hash code continue to affect the probe
108sequence across iterations; but you want it large so that in really bad cases
109the high-order hash bits have an effect on early iterations. 5 was "the
110best" in minimizing total collisions across experiments Tim Peters ran (on
111both normal and pathological cases), but 4 and 6 weren't significantly worse.
112
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000113Historical: Reimer Behrends contributed the idea of using a polynomial-based
Tim Peterseb28ef22001-06-02 05:27:19 +0000114approach, using repeated multiplication by x in GF(2**n) where an irreducible
115polynomial for each table size was chosen such that x was a primitive root.
116Christian Tismer later extended that to use division by x instead, as an
117efficient way to get the high bits of the hash code into play. This scheme
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000118also gave excellent collision statistics, but was more expensive: two
119if-tests were required inside the loop; computing "the next" index took about
120the same number of operations but without as much potential parallelism
121(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
122above, and then shifting perturb can be done while the table index is being
123masked); and the PyDictObject struct required a member to hold the table's
124polynomial. In Tim's experiments the current scheme ran faster, produced
125equally good collision statistics, needed less code & used less memory.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000126
127Theoretical Python 2.5 headache: hash codes are only C "long", but
128sizeof(Py_ssize_t) > sizeof(long) may be possible. In that case, and if a
129dict is genuinely huge, then only the slots directly reachable via indexing
130by a C long can be the first slot in a probe sequence. The probe sequence
131will still eventually reach every slot in the table, but the collision rate
132on initial probes may be much higher than this scheme was designed for.
133Getting a hash code as fat as Py_ssize_t is the only real cure. But in
134practice, this probably won't make a lick of difference for many years (at
135which point everyone will have terabytes of RAM on 64-bit boxes).
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000136*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000137
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000138/* Object used as dummy key to fill deleted entries */
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000139static PyObject *dummy = NULL; /* Initialized by first call to newPyDictObject() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000140
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000141#ifdef Py_REF_DEBUG
142PyObject *
143_PyDict_Dummy(void)
144{
145 return dummy;
146}
147#endif
148
Fred Drake1bff34a2000-08-31 19:31:38 +0000149/* forward declarations */
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000150static PyDictEntry *
151lookdict_unicode(PyDictObject *mp, PyObject *key, long hash);
Fred Drake1bff34a2000-08-31 19:31:38 +0000152
153#ifdef SHOW_CONVERSION_COUNTS
154static long created = 0L;
155static long converted = 0L;
156
157static void
158show_counts(void)
159{
160 fprintf(stderr, "created %ld string dicts\n", created);
161 fprintf(stderr, "converted %ld to normal dicts\n", converted);
162 fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created);
163}
164#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000165
Tim Peters6d6c1a32001-08-02 04:15:00 +0000166/* Initialization macros.
167 There are two ways to create a dict: PyDict_New() is the main C API
168 function, and the tp_new slot maps to dict_new(). In the latter case we
169 can save a little time over what PyDict_New does because it's guaranteed
170 that the PyDictObject struct is already zeroed out.
171 Everyone except dict_new() should use EMPTY_TO_MINSIZE (unless they have
172 an excellent reason not to).
173*/
174
175#define INIT_NONZERO_DICT_SLOTS(mp) do { \
Tim Petersdea48ec2001-05-22 20:40:22 +0000176 (mp)->ma_table = (mp)->ma_smalltable; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000177 (mp)->ma_mask = PyDict_MINSIZE - 1; \
178 } while(0)
179
180#define EMPTY_TO_MINSIZE(mp) do { \
181 memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000182 (mp)->ma_used = (mp)->ma_fill = 0; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000183 INIT_NONZERO_DICT_SLOTS(mp); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000184 } while(0)
185
Raymond Hettinger43442782004-03-17 21:55:03 +0000186/* Dictionary reuse scheme to save calls to malloc, free, and memset */
187#define MAXFREEDICTS 80
188static PyDictObject *free_dicts[MAXFREEDICTS];
189static int num_free_dicts = 0;
190
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000191PyObject *
Thomas Wouters78890102000-07-22 19:25:51 +0000192PyDict_New(void)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000193{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000194 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000195 if (dummy == NULL) { /* Auto-initialize dummy */
Neal Norwitz53cbdaa2007-08-23 21:42:55 +0000196 dummy = PyUnicode_FromString("<dummy key>");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000197 if (dummy == NULL)
198 return NULL;
Fred Drake1bff34a2000-08-31 19:31:38 +0000199#ifdef SHOW_CONVERSION_COUNTS
200 Py_AtExit(show_counts);
201#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000202 }
Raymond Hettinger43442782004-03-17 21:55:03 +0000203 if (num_free_dicts) {
204 mp = free_dicts[--num_free_dicts];
205 assert (mp != NULL);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000206 assert (Py_Type(mp) == &PyDict_Type);
Raymond Hettinger43442782004-03-17 21:55:03 +0000207 _Py_NewReference((PyObject *)mp);
208 if (mp->ma_fill) {
209 EMPTY_TO_MINSIZE(mp);
210 }
211 assert (mp->ma_used == 0);
212 assert (mp->ma_table == mp->ma_smalltable);
213 assert (mp->ma_mask == PyDict_MINSIZE - 1);
214 } else {
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000215 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
Raymond Hettinger43442782004-03-17 21:55:03 +0000216 if (mp == NULL)
217 return NULL;
218 EMPTY_TO_MINSIZE(mp);
219 }
Guido van Rossum89d8c602007-09-18 17:26:56 +0000220 mp->ma_lookup = lookdict_unicode;
Fred Drake1bff34a2000-08-31 19:31:38 +0000221#ifdef SHOW_CONVERSION_COUNTS
222 ++created;
223#endif
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000224 _PyObject_GC_TRACK(mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000225 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000226}
227
228/*
229The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000230This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000231Open addressing is preferred over chaining since the link overhead for
232chaining would be substantial (100% with typical malloc overhead).
233
Tim Peterseb28ef22001-06-02 05:27:19 +0000234The initial probe index is computed as hash mod the table size. Subsequent
235probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000236
237All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000238
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000239The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000240contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000241Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000242
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000243lookdict() is general-purpose, and may return NULL if (and only if) a
244comparison raises an exception (this was new in Python 2.5).
Guido van Rossum89d8c602007-09-18 17:26:56 +0000245lookdict_unicode() below is specialized to string keys, comparison of which can
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000246never raise an exception; that function can never return NULL. For both, when
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000247the key isn't found a PyDictEntry* is returned for which the me_value field is
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000248NULL; this is the slot in the dict at which the key would have been found, and
249the caller can (if it wishes) add the <key, value> pair to the returned
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000250PyDictEntry*.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000251*/
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000252static PyDictEntry *
253lookdict(PyDictObject *mp, PyObject *key, register long hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000254{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000255 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000256 register size_t perturb;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000257 register PyDictEntry *freeslot;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000258 register size_t mask = (size_t)mp->ma_mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000259 PyDictEntry *ep0 = mp->ma_table;
260 register PyDictEntry *ep;
Fred Drakec88b99c2000-08-31 19:04:07 +0000261 register int cmp;
Tim Peters453163d2001-06-03 04:54:32 +0000262 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000263
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000264 i = (size_t)hash & mask;
Guido van Rossumefb46091997-01-29 15:53:56 +0000265 ep = &ep0[i];
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000266 if (ep->me_key == NULL || ep->me_key == key)
Guido van Rossum7d181591997-01-16 21:06:45 +0000267 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000268
Guido van Rossum16e93a81997-01-28 00:00:11 +0000269 if (ep->me_key == dummy)
Guido van Rossum7d181591997-01-16 21:06:45 +0000270 freeslot = ep;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000271 else {
Fred Drakec88b99c2000-08-31 19:04:07 +0000272 if (ep->me_hash == hash) {
Tim Peters453163d2001-06-03 04:54:32 +0000273 startkey = ep->me_key;
Guido van Rossum2fd4f372007-11-29 18:43:05 +0000274 Py_INCREF(startkey);
Tim Peters453163d2001-06-03 04:54:32 +0000275 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Guido van Rossum2fd4f372007-11-29 18:43:05 +0000276 Py_DECREF(startkey);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000277 if (cmp < 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000278 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000279 if (ep0 == mp->ma_table && ep->me_key == startkey) {
280 if (cmp > 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000281 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000282 }
283 else {
284 /* The compare did major nasty stuff to the
285 * dict: start over.
286 * XXX A clever adversary could prevent this
287 * XXX from terminating.
288 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000289 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000290 }
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000291 }
292 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000293 }
Tim Peters15d49292001-05-27 07:39:22 +0000294
Tim Peters342c65e2001-05-13 06:43:53 +0000295 /* In the loop, me_key == dummy is by far (factor of 100s) the
296 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000297 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
298 i = (i << 2) + i + perturb + 1;
299 ep = &ep0[i & mask];
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000300 if (ep->me_key == NULL)
301 return freeslot == NULL ? ep : freeslot;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000302 if (ep->me_key == key)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000303 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000304 if (ep->me_hash == hash && ep->me_key != dummy) {
Tim Peters453163d2001-06-03 04:54:32 +0000305 startkey = ep->me_key;
Guido van Rossum2fd4f372007-11-29 18:43:05 +0000306 Py_INCREF(startkey);
Tim Peters453163d2001-06-03 04:54:32 +0000307 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Guido van Rossum2fd4f372007-11-29 18:43:05 +0000308 Py_DECREF(startkey);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000309 if (cmp < 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000310 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000311 if (ep0 == mp->ma_table && ep->me_key == startkey) {
312 if (cmp > 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000313 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000314 }
315 else {
316 /* The compare did major nasty stuff to the
317 * dict: start over.
318 * XXX A clever adversary could prevent this
319 * XXX from terminating.
320 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000321 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000322 }
Guido van Rossum16e93a81997-01-28 00:00:11 +0000323 }
Tim Peters342c65e2001-05-13 06:43:53 +0000324 else if (ep->me_key == dummy && freeslot == NULL)
325 freeslot = ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000326 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000327 assert(0); /* NOT REACHED */
328 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000329}
330
331/*
Guido van Rossum89d8c602007-09-18 17:26:56 +0000332 * Hacked up version of lookdict which can assume keys are always
333 * unicodes; this assumption allows testing for errors during
334 * PyObject_RichCompareBool() to be dropped; unicode-unicode
335 * comparisons never raise exceptions. This also means we don't need
336 * to go through PyObject_RichCompareBool(); we can always use
337 * unicode_eq() directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000338 *
Guido van Rossum89d8c602007-09-18 17:26:56 +0000339 * This is valuable because dicts with only unicode keys are very common.
Fred Drake1bff34a2000-08-31 19:31:38 +0000340 */
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000341static PyDictEntry *
342lookdict_unicode(PyDictObject *mp, PyObject *key, register long hash)
Fred Drake1bff34a2000-08-31 19:31:38 +0000343{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000344 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000345 register size_t perturb;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000346 register PyDictEntry *freeslot;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000347 register size_t mask = (size_t)mp->ma_mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000348 PyDictEntry *ep0 = mp->ma_table;
349 register PyDictEntry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000350
Guido van Rossum89d8c602007-09-18 17:26:56 +0000351 /* Make sure this function doesn't have to handle non-unicode keys,
Tim Peters0ab085c2001-09-14 00:25:33 +0000352 including subclasses of str; e.g., one reason to subclass
Guido van Rossum89d8c602007-09-18 17:26:56 +0000353 unicodes is to override __eq__, and for speed we don't cater to
Tim Peters0ab085c2001-09-14 00:25:33 +0000354 that here. */
Guido van Rossum89d8c602007-09-18 17:26:56 +0000355 if (!PyUnicode_CheckExact(key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000356#ifdef SHOW_CONVERSION_COUNTS
357 ++converted;
358#endif
359 mp->ma_lookup = lookdict;
360 return lookdict(mp, key, hash);
361 }
Tim Peters2f228e72001-05-13 00:19:31 +0000362 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000363 ep = &ep0[i];
364 if (ep->me_key == NULL || ep->me_key == key)
365 return ep;
366 if (ep->me_key == dummy)
367 freeslot = ep;
368 else {
Guido van Rossum89d8c602007-09-18 17:26:56 +0000369 if (ep->me_hash == hash && unicode_eq(ep->me_key, key))
Fred Drake1bff34a2000-08-31 19:31:38 +0000370 return ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000371 freeslot = NULL;
372 }
Tim Peters15d49292001-05-27 07:39:22 +0000373
Tim Peters342c65e2001-05-13 06:43:53 +0000374 /* In the loop, me_key == dummy is by far (factor of 100s) the
375 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000376 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
377 i = (i << 2) + i + perturb + 1;
378 ep = &ep0[i & mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000379 if (ep->me_key == NULL)
380 return freeslot == NULL ? ep : freeslot;
381 if (ep->me_key == key
382 || (ep->me_hash == hash
383 && ep->me_key != dummy
Guido van Rossum89d8c602007-09-18 17:26:56 +0000384 && unicode_eq(ep->me_key, key)))
Fred Drake1bff34a2000-08-31 19:31:38 +0000385 return ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000386 if (ep->me_key == dummy && freeslot == NULL)
Tim Peters342c65e2001-05-13 06:43:53 +0000387 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000388 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000389 assert(0); /* NOT REACHED */
390 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000391}
392
393/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000394Internal routine to insert a new item into the table.
395Used both by the internal resize routine and by the public insert routine.
396Eats a reference to key and one to value.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000397Returns -1 if an error occurred, or 0 on success.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000398*/
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000399static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000400insertdict(register PyDictObject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000401{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000402 PyObject *old_value;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000403 register PyDictEntry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000404 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
405
406 assert(mp->ma_lookup != NULL);
407 ep = mp->ma_lookup(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000408 if (ep == NULL) {
409 Py_DECREF(key);
410 Py_DECREF(value);
411 return -1;
412 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000413 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000414 old_value = ep->me_value;
415 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000416 Py_DECREF(old_value); /* which **CAN** re-enter */
417 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000418 }
419 else {
420 if (ep->me_key == NULL)
421 mp->ma_fill++;
Raymond Hettinger07ead172005-02-05 23:42:57 +0000422 else {
423 assert(ep->me_key == dummy);
424 Py_DECREF(dummy);
425 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000426 ep->me_key = key;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000427 ep->me_hash = (Py_ssize_t)hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000428 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000429 mp->ma_used++;
430 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000431 return 0;
432}
433
434/*
435Internal routine used by dictresize() to insert an item which is
436known to be absent from the dict. This routine also assumes that
437the dict contains no deleted entries. Besides the performance benefit,
438using insertdict() in dictresize() is dangerous (SF bug #1456209).
439Note that no refcounts are changed by this routine; if needed, the caller
440is responsible for incref'ing `key` and `value`.
441*/
442static void
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000443insertdict_clean(register PyDictObject *mp, PyObject *key, long hash,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000444 PyObject *value)
445{
446 register size_t i;
447 register size_t perturb;
448 register size_t mask = (size_t)mp->ma_mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000449 PyDictEntry *ep0 = mp->ma_table;
450 register PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000451
452 i = hash & mask;
453 ep = &ep0[i];
454 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
455 i = (i << 2) + i + perturb + 1;
456 ep = &ep0[i & mask];
457 }
458 assert(ep->me_value == NULL);
459 mp->ma_fill++;
460 ep->me_key = key;
461 ep->me_hash = (Py_ssize_t)hash;
462 ep->me_value = value;
463 mp->ma_used++;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000464}
465
466/*
467Restructure the table by allocating a new table and reinserting all
468items again. When entries have been deleted, the new table may
469actually be smaller than the old one.
470*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000471static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000472dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000473{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000474 Py_ssize_t newsize;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000475 PyDictEntry *oldtable, *newtable, *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000476 Py_ssize_t i;
Tim Peters0c6010b2001-05-23 23:33:57 +0000477 int is_oldtable_malloced;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000478 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000479
480 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000481
Tim Peterseb28ef22001-06-02 05:27:19 +0000482 /* Find the smallest table size > minused. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000483 for (newsize = PyDict_MINSIZE;
Tim Peterseb28ef22001-06-02 05:27:19 +0000484 newsize <= minused && newsize > 0;
485 newsize <<= 1)
486 ;
487 if (newsize <= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000488 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000489 return -1;
490 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000491
492 /* Get space for a new table. */
493 oldtable = mp->ma_table;
494 assert(oldtable != NULL);
495 is_oldtable_malloced = oldtable != mp->ma_smalltable;
496
Tim Peters6d6c1a32001-08-02 04:15:00 +0000497 if (newsize == PyDict_MINSIZE) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000498 /* A large table is shrinking, or we can't get any smaller. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000499 newtable = mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000500 if (newtable == oldtable) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000501 if (mp->ma_fill == mp->ma_used) {
502 /* No dummies, so no point doing anything. */
Tim Peters0c6010b2001-05-23 23:33:57 +0000503 return 0;
Tim Petersf8a548c2001-05-24 16:26:40 +0000504 }
505 /* We're not going to resize it, but rebuild the
506 table anyway to purge old dummy entries.
507 Subtle: This is *necessary* if fill==size,
508 as lookdict needs at least one virgin slot to
509 terminate failing searches. If fill < size, it's
510 merely desirable, as dummies slow searches. */
511 assert(mp->ma_fill > mp->ma_used);
Tim Peters0c6010b2001-05-23 23:33:57 +0000512 memcpy(small_copy, oldtable, sizeof(small_copy));
513 oldtable = small_copy;
514 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000515 }
516 else {
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000517 newtable = PyMem_NEW(PyDictEntry, newsize);
Tim Petersdea48ec2001-05-22 20:40:22 +0000518 if (newtable == NULL) {
519 PyErr_NoMemory();
520 return -1;
521 }
522 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000523
524 /* Make the dict empty, using the new table. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000525 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000526 mp->ma_table = newtable;
Tim Petersafb6ae82001-06-04 21:00:21 +0000527 mp->ma_mask = newsize - 1;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000528 memset(newtable, 0, sizeof(PyDictEntry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000529 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000530 i = mp->ma_fill;
531 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000532
Tim Peters19283142001-05-17 22:25:34 +0000533 /* Copy the data over; this is refcount-neutral for active entries;
534 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000535 for (ep = oldtable; i > 0; ep++) {
536 if (ep->me_value != NULL) { /* active entry */
537 --i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000538 insertdict_clean(mp, ep->me_key, (long)ep->me_hash,
539 ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000540 }
Tim Peters19283142001-05-17 22:25:34 +0000541 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000542 --i;
Tim Peters19283142001-05-17 22:25:34 +0000543 assert(ep->me_key == dummy);
544 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000545 }
Tim Peters19283142001-05-17 22:25:34 +0000546 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000547 }
548
Tim Peters0c6010b2001-05-23 23:33:57 +0000549 if (is_oldtable_malloced)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000550 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000551 return 0;
552}
553
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000554/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
555 * that may occur (originally dicts supported only string keys, and exceptions
556 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000557 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000558 * (suppressed) occurred while computing the key's hash, or that some error
559 * (suppressed) occurred when comparing keys in the dict's internal probe
560 * sequence. A nasty example of the latter is when a Python-coded comparison
561 * function hits a stack-depth error, which can cause this to return NULL
562 * even if the key is present.
563 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000564PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000565PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000566{
567 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000568 PyDictObject *mp = (PyDictObject *)op;
569 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000570 PyThreadState *tstate;
571 if (!PyDict_Check(op))
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000572 return NULL;
Guido van Rossum4d027722007-09-18 04:30:42 +0000573 if (!PyUnicode_CheckExact(key) ||
574 (hash = ((PyUnicodeObject *) key)->hash) == -1)
Guido van Rossumca756f21997-01-23 19:39:29 +0000575 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000576 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000577 if (hash == -1) {
578 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000579 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000580 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000581 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000582
583 /* We can arrive here with a NULL tstate during initialization:
584 try running "python -Wi" for an example related to string
585 interning. Let's just hope that no exception occurs then... */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000586 tstate = _PyThreadState_Current;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000587 if (tstate != NULL && tstate->curexc_type != NULL) {
588 /* preserve the existing exception */
589 PyObject *err_type, *err_value, *err_tb;
590 PyErr_Fetch(&err_type, &err_value, &err_tb);
591 ep = (mp->ma_lookup)(mp, key, hash);
592 /* ignore errors */
593 PyErr_Restore(err_type, err_value, err_tb);
594 if (ep == NULL)
595 return NULL;
596 }
597 else {
598 ep = (mp->ma_lookup)(mp, key, hash);
599 if (ep == NULL) {
600 PyErr_Clear();
601 return NULL;
602 }
603 }
604 return ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000605}
606
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000607/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
608 This returns NULL *with* an exception set if an exception occurred.
609 It returns NULL *without* an exception set if the key wasn't present.
610*/
611PyObject *
612PyDict_GetItemWithError(PyObject *op, PyObject *key)
613{
614 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000615 PyDictObject*mp = (PyDictObject *)op;
616 PyDictEntry *ep;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000617
618 if (!PyDict_Check(op)) {
619 PyErr_BadInternalCall();
620 return NULL;
621 }
Guido van Rossum89d8c602007-09-18 17:26:56 +0000622 if (!PyUnicode_CheckExact(key) ||
623 (hash = ((PyUnicodeObject *) key)->hash) == -1)
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000624 {
625 hash = PyObject_Hash(key);
626 if (hash == -1) {
627 return NULL;
628 }
629 }
630
631 ep = (mp->ma_lookup)(mp, key, hash);
632 if (ep == NULL)
633 return NULL;
634 return ep->me_value;
635}
636
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000637/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
Tim Peters60b29962006-01-01 01:19:23 +0000638 * dictionary if it's merely replacing the value for an existing key.
639 * This means that it's safe to loop over a dictionary with PyDict_Next()
640 * and occasionally replace a value -- but you can't insert new keys or
641 * remove them.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000642 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000643int
Tim Peters1f5871e2000-07-04 17:44:48 +0000644PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000645{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000646 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000647 register long hash;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000648 register Py_ssize_t n_used;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000649
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000650 if (!PyDict_Check(op)) {
651 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000652 return -1;
653 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000654 assert(key);
655 assert(value);
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000656 mp = (PyDictObject *)op;
Guido van Rossum4d027722007-09-18 04:30:42 +0000657 if (!PyUnicode_CheckExact(key) ||
658 (hash = ((PyUnicodeObject *) key)->hash) == -1)
659 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000660 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000661 if (hash == -1)
662 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000663 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000664 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000665 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000666 Py_INCREF(value);
667 Py_INCREF(key);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000668 if (insertdict(mp, key, hash, value) != 0)
669 return -1;
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000670 /* If we added a key, we can safely resize. Otherwise just return!
671 * If fill >= 2/3 size, adjust size. Normally, this doubles or
672 * quaduples the size, but it's also possible for the dict to shrink
Tim Peters60b29962006-01-01 01:19:23 +0000673 * (if ma_fill is much larger than ma_used, meaning a lot of dict
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000674 * keys have been * deleted).
Tim Peters60b29962006-01-01 01:19:23 +0000675 *
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000676 * Quadrupling the size improves average dictionary sparseness
677 * (reducing collisions) at the cost of some memory and iteration
678 * speed (which loops over every possible entry). It also halves
679 * the number of expensive resize operations in a growing dictionary.
Tim Peters60b29962006-01-01 01:19:23 +0000680 *
681 * Very large dictionaries (over 50K items) use doubling instead.
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000682 * This may help applications with severe memory constraints.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000683 */
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000684 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
685 return 0;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000686 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000687}
688
689int
Tim Peters1f5871e2000-07-04 17:44:48 +0000690PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000691{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000692 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000693 register long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000694 register PyDictEntry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000695 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000696
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000697 if (!PyDict_Check(op)) {
698 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000699 return -1;
700 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000701 assert(key);
Guido van Rossum89d8c602007-09-18 17:26:56 +0000702 if (!PyUnicode_CheckExact(key) ||
703 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000704 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000705 if (hash == -1)
706 return -1;
707 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000708 mp = (PyDictObject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000709 ep = (mp->ma_lookup)(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000710 if (ep == NULL)
711 return -1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000712 if (ep->me_value == NULL) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000713 set_key_error(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000714 return -1;
715 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000716 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000717 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000718 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000719 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000720 ep->me_value = NULL;
721 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000722 Py_DECREF(old_value);
723 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000724 return 0;
725}
726
Guido van Rossum25831651993-05-19 14:50:45 +0000727void
Tim Peters1f5871e2000-07-04 17:44:48 +0000728PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000729{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000730 PyDictObject *mp;
731 PyDictEntry *ep, *table;
Tim Petersdea48ec2001-05-22 20:40:22 +0000732 int table_is_malloced;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000733 Py_ssize_t fill;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000734 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000735#ifdef Py_DEBUG
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000736 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +0000737#endif
738
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000739 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000740 return;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000741 mp = (PyDictObject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000742#ifdef Py_DEBUG
Tim Petersafb6ae82001-06-04 21:00:21 +0000743 n = mp->ma_mask + 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000744 i = 0;
745#endif
746
747 table = mp->ma_table;
748 assert(table != NULL);
749 table_is_malloced = table != mp->ma_smalltable;
750
751 /* This is delicate. During the process of clearing the dict,
752 * decrefs can cause the dict to mutate. To avoid fatal confusion
753 * (voice of experience), we have to make the dict empty before
754 * clearing the slots, and never refer to anything via mp->xxx while
755 * clearing.
756 */
757 fill = mp->ma_fill;
758 if (table_is_malloced)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000759 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000760
761 else if (fill > 0) {
762 /* It's a small table with something that needs to be cleared.
763 * Afraid the only safe way is to copy the dict entries into
764 * another small table first.
765 */
766 memcpy(small_copy, table, sizeof(small_copy));
767 table = small_copy;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000768 EMPTY_TO_MINSIZE(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000769 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000770 /* else it's a small table that's already empty */
771
772 /* Now we can finally clear things. If C had refcounts, we could
773 * assert that the refcount on table is 1 now, i.e. that this function
774 * has unique access to it, so decref side-effects can't alter it.
775 */
776 for (ep = table; fill > 0; ++ep) {
777#ifdef Py_DEBUG
778 assert(i < n);
779 ++i;
780#endif
781 if (ep->me_key) {
782 --fill;
783 Py_DECREF(ep->me_key);
784 Py_XDECREF(ep->me_value);
785 }
786#ifdef Py_DEBUG
787 else
788 assert(ep->me_value == NULL);
789#endif
790 }
791
792 if (table_is_malloced)
793 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000794}
795
Tim Peters080c88b2003-02-15 03:01:11 +0000796/*
797 * Iterate over a dict. Use like so:
798 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000799 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +0000800 * PyObject *key, *value;
801 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +0000802 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +0000803 * Refer to borrowed references in key and value.
804 * }
805 *
806 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +0000807 * mutates the dict. One exception: it is safe if the loop merely changes
808 * the values associated with the keys (but doesn't insert new keys or
809 * delete keys), via PyDict_SetItem().
810 */
Guido van Rossum25831651993-05-19 14:50:45 +0000811int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000812PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000813{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000814 register Py_ssize_t i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000815 register Py_ssize_t mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000816 register PyDictEntry *ep;
Raymond Hettinger43442782004-03-17 21:55:03 +0000817
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000818 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000819 return 0;
Guido van Rossum25831651993-05-19 14:50:45 +0000820 i = *ppos;
821 if (i < 0)
822 return 0;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000823 ep = ((PyDictObject *)op)->ma_table;
824 mask = ((PyDictObject *)op)->ma_mask;
Raymond Hettinger43442782004-03-17 21:55:03 +0000825 while (i <= mask && ep[i].me_value == NULL)
Guido van Rossum25831651993-05-19 14:50:45 +0000826 i++;
827 *ppos = i+1;
Raymond Hettinger43442782004-03-17 21:55:03 +0000828 if (i > mask)
Guido van Rossum25831651993-05-19 14:50:45 +0000829 return 0;
830 if (pkey)
Raymond Hettinger43442782004-03-17 21:55:03 +0000831 *pkey = ep[i].me_key;
Guido van Rossum25831651993-05-19 14:50:45 +0000832 if (pvalue)
Raymond Hettinger43442782004-03-17 21:55:03 +0000833 *pvalue = ep[i].me_value;
Guido van Rossum25831651993-05-19 14:50:45 +0000834 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000835}
836
Thomas Wouterscf297e42007-02-23 15:07:44 +0000837/* Internal version of PyDict_Next that returns a hash value in addition to the key and value.*/
838int
839_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, long *phash)
840{
841 register Py_ssize_t i;
842 register Py_ssize_t mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000843 register PyDictEntry *ep;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000844
845 if (!PyDict_Check(op))
846 return 0;
847 i = *ppos;
848 if (i < 0)
849 return 0;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000850 ep = ((PyDictObject *)op)->ma_table;
851 mask = ((PyDictObject *)op)->ma_mask;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000852 while (i <= mask && ep[i].me_value == NULL)
853 i++;
854 *ppos = i+1;
855 if (i > mask)
856 return 0;
857 *phash = (long)(ep[i].me_hash);
858 if (pkey)
859 *pkey = ep[i].me_key;
860 if (pvalue)
861 *pvalue = ep[i].me_value;
862 return 1;
863}
864
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000865/* Methods */
866
867static void
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000868dict_dealloc(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000869{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000870 register PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000871 Py_ssize_t fill = mp->ma_fill;
Guido van Rossumff413af2002-03-28 20:34:59 +0000872 PyObject_GC_UnTrack(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000873 Py_TRASHCAN_SAFE_BEGIN(mp)
Tim Petersdea48ec2001-05-22 20:40:22 +0000874 for (ep = mp->ma_table; fill > 0; ep++) {
875 if (ep->me_key) {
876 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000877 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000878 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000879 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000880 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000881 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000882 PyMem_DEL(mp->ma_table);
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000883 if (num_free_dicts < MAXFREEDICTS && Py_Type(mp) == &PyDict_Type)
Raymond Hettinger43442782004-03-17 21:55:03 +0000884 free_dicts[num_free_dicts++] = mp;
Tim Peters60b29962006-01-01 01:19:23 +0000885 else
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000886 Py_Type(mp)->tp_free((PyObject *)mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000887 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000888}
889
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000890static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000891dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000892{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000893 Py_ssize_t i;
Tim Petersa7259592001-06-16 05:11:17 +0000894 PyObject *s, *temp, *colon = NULL;
895 PyObject *pieces = NULL, *result = NULL;
896 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +0000897
Tim Petersa7259592001-06-16 05:11:17 +0000898 i = Py_ReprEnter((PyObject *)mp);
Guido van Rossum255443b1998-04-10 22:47:14 +0000899 if (i != 0) {
Walter Dörwald1ab83302007-05-18 17:15:44 +0000900 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
Guido van Rossum255443b1998-04-10 22:47:14 +0000901 }
902
Tim Petersa7259592001-06-16 05:11:17 +0000903 if (mp->ma_used == 0) {
Walter Dörwald1ab83302007-05-18 17:15:44 +0000904 result = PyUnicode_FromString("{}");
Tim Petersa7259592001-06-16 05:11:17 +0000905 goto Done;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000906 }
Tim Petersa7259592001-06-16 05:11:17 +0000907
908 pieces = PyList_New(0);
909 if (pieces == NULL)
910 goto Done;
911
Walter Dörwald1ab83302007-05-18 17:15:44 +0000912 colon = PyUnicode_FromString(": ");
Tim Petersa7259592001-06-16 05:11:17 +0000913 if (colon == NULL)
914 goto Done;
915
916 /* Do repr() on each key+value pair, and insert ": " between them.
917 Note that repr may mutate the dict. */
Tim Petersc6057842001-06-16 07:52:53 +0000918 i = 0;
919 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Tim Petersa7259592001-06-16 05:11:17 +0000920 int status;
921 /* Prevent repr from deleting value during key format. */
922 Py_INCREF(value);
923 s = PyObject_Repr(key);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000924 PyUnicode_Append(&s, colon);
925 PyUnicode_AppendAndDel(&s, PyObject_Repr(value));
Tim Petersa7259592001-06-16 05:11:17 +0000926 Py_DECREF(value);
927 if (s == NULL)
928 goto Done;
929 status = PyList_Append(pieces, s);
930 Py_DECREF(s); /* append created a new ref */
931 if (status < 0)
932 goto Done;
933 }
934
935 /* Add "{}" decorations to the first and last items. */
936 assert(PyList_GET_SIZE(pieces) > 0);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000937 s = PyUnicode_FromString("{");
Tim Petersa7259592001-06-16 05:11:17 +0000938 if (s == NULL)
939 goto Done;
940 temp = PyList_GET_ITEM(pieces, 0);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000941 PyUnicode_AppendAndDel(&s, temp);
Tim Petersa7259592001-06-16 05:11:17 +0000942 PyList_SET_ITEM(pieces, 0, s);
943 if (s == NULL)
944 goto Done;
945
Walter Dörwald1ab83302007-05-18 17:15:44 +0000946 s = PyUnicode_FromString("}");
Tim Petersa7259592001-06-16 05:11:17 +0000947 if (s == NULL)
948 goto Done;
949 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000950 PyUnicode_AppendAndDel(&temp, s);
Tim Petersa7259592001-06-16 05:11:17 +0000951 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
952 if (temp == NULL)
953 goto Done;
954
955 /* Paste them all together with ", " between. */
Walter Dörwald1ab83302007-05-18 17:15:44 +0000956 s = PyUnicode_FromString(", ");
Tim Petersa7259592001-06-16 05:11:17 +0000957 if (s == NULL)
958 goto Done;
Walter Dörwald1ab83302007-05-18 17:15:44 +0000959 result = PyUnicode_Join(s, pieces);
Tim Peters0ab085c2001-09-14 00:25:33 +0000960 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +0000961
962Done:
963 Py_XDECREF(pieces);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000964 Py_XDECREF(colon);
Tim Petersa7259592001-06-16 05:11:17 +0000965 Py_ReprLeave((PyObject *)mp);
966 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000967}
968
Martin v. Löwis18e16552006-02-15 17:27:45 +0000969static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000970dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000971{
972 return mp->ma_used;
973}
974
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000975static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000976dict_subscript(PyDictObject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000977{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000978 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000979 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000980 PyDictEntry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000981 assert(mp->ma_table != NULL);
Guido van Rossum89d8c602007-09-18 17:26:56 +0000982 if (!PyUnicode_CheckExact(key) ||
983 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000984 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000985 if (hash == -1)
986 return NULL;
987 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000988 ep = (mp->ma_lookup)(mp, key, hash);
989 if (ep == NULL)
990 return NULL;
991 v = ep->me_value;
Guido van Rossum1968ad32006-02-25 22:38:04 +0000992 if (v == NULL) {
993 if (!PyDict_CheckExact(mp)) {
994 /* Look up __missing__ method if we're a subclass. */
Guido van Rossum4b92a822006-02-25 23:32:30 +0000995 PyObject *missing;
Guido van Rossum1968ad32006-02-25 22:38:04 +0000996 static PyObject *missing_str = NULL;
997 if (missing_str == NULL)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000998 missing_str =
Martin v. Löwis5b222132007-06-10 09:51:05 +0000999 PyUnicode_InternFromString("__missing__");
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001000 missing = _PyType_Lookup(Py_Type(mp), missing_str);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001001 if (missing != NULL)
1002 return PyObject_CallFunctionObjArgs(missing,
1003 (PyObject *)mp, key, NULL);
1004 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001005 set_key_error(key);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001006 return NULL;
1007 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001008 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001009 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001010 return v;
1011}
1012
1013static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001014dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001015{
1016 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001017 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001018 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001019 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001020}
1021
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001022static PyMappingMethods dict_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001023 (lenfunc)dict_length, /*mp_length*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001024 (binaryfunc)dict_subscript, /*mp_subscript*/
1025 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001026};
1027
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001028static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001029dict_keys(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001030{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001031 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001032 register Py_ssize_t i, j;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001033 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001034 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001035
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001036 again:
1037 n = mp->ma_used;
1038 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001039 if (v == NULL)
1040 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001041 if (n != mp->ma_used) {
1042 /* Durnit. The allocations caused the dict to resize.
1043 * Just start over, this shouldn't normally happen.
1044 */
1045 Py_DECREF(v);
1046 goto again;
1047 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001048 ep = mp->ma_table;
1049 mask = mp->ma_mask;
1050 for (i = 0, j = 0; i <= mask; i++) {
1051 if (ep[i].me_value != NULL) {
1052 PyObject *key = ep[i].me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001053 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001054 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001055 j++;
1056 }
1057 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001058 assert(j == n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001059 return v;
1060}
1061
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001062static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001063dict_values(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001064{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001065 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001066 register Py_ssize_t i, j;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001067 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001068 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001069
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001070 again:
1071 n = mp->ma_used;
1072 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001073 if (v == NULL)
1074 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001075 if (n != mp->ma_used) {
1076 /* Durnit. The allocations caused the dict to resize.
1077 * Just start over, this shouldn't normally happen.
1078 */
1079 Py_DECREF(v);
1080 goto again;
1081 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001082 ep = mp->ma_table;
1083 mask = mp->ma_mask;
1084 for (i = 0, j = 0; i <= mask; i++) {
1085 if (ep[i].me_value != NULL) {
1086 PyObject *value = ep[i].me_value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001087 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001088 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001089 j++;
1090 }
1091 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001092 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001093 return v;
1094}
1095
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001096static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001097dict_items(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001098{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001099 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001100 register Py_ssize_t i, j, n;
1101 Py_ssize_t mask;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001102 PyObject *item, *key, *value;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001103 PyDictEntry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001104
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001105 /* Preallocate the list of tuples, to avoid allocations during
1106 * the loop over the items, which could trigger GC, which
1107 * could resize the dict. :-(
1108 */
1109 again:
1110 n = mp->ma_used;
1111 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001112 if (v == NULL)
1113 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001114 for (i = 0; i < n; i++) {
1115 item = PyTuple_New(2);
1116 if (item == NULL) {
1117 Py_DECREF(v);
1118 return NULL;
1119 }
1120 PyList_SET_ITEM(v, i, item);
1121 }
1122 if (n != mp->ma_used) {
1123 /* Durnit. The allocations caused the dict to resize.
1124 * Just start over, this shouldn't normally happen.
1125 */
1126 Py_DECREF(v);
1127 goto again;
1128 }
1129 /* Nothing we do below makes any function calls. */
Raymond Hettinger43442782004-03-17 21:55:03 +00001130 ep = mp->ma_table;
1131 mask = mp->ma_mask;
1132 for (i = 0, j = 0; i <= mask; i++) {
Raymond Hettinger06905122004-03-19 10:30:00 +00001133 if ((value=ep[i].me_value) != NULL) {
Raymond Hettinger43442782004-03-17 21:55:03 +00001134 key = ep[i].me_key;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001135 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001136 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001137 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001138 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001139 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001140 j++;
1141 }
1142 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001143 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001144 return v;
1145}
1146
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001147static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001148dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001149{
1150 PyObject *seq;
1151 PyObject *value = Py_None;
1152 PyObject *it; /* iter(seq) */
1153 PyObject *key;
1154 PyObject *d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001155 int status;
1156
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001157 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001158 return NULL;
1159
1160 d = PyObject_CallObject(cls, NULL);
1161 if (d == NULL)
1162 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001163
Guido van Rossum58da9312007-11-10 23:39:45 +00001164 if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) {
1165 PyDictObject *mp = (PyDictObject *)d;
1166 PyObject *oldvalue;
1167 Py_ssize_t pos = 0;
1168 PyObject *key;
1169 long hash;
1170
1171 if (dictresize(mp, PySet_GET_SIZE(seq)))
1172 return NULL;
1173
1174 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
1175 Py_INCREF(key);
1176 Py_INCREF(value);
1177 if (insertdict(mp, key, hash, value))
1178 return NULL;
1179 }
1180 return d;
1181 }
1182
Guido van Rossumd8faa362007-04-27 19:54:29 +00001183 if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001184 PyDictObject *mp = (PyDictObject *)d;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001185 Py_ssize_t pos = 0;
1186 PyObject *key;
1187 long hash;
1188
1189 if (dictresize(mp, PySet_GET_SIZE(seq)))
1190 return NULL;
1191
1192 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
1193 Py_INCREF(key);
1194 Py_INCREF(value);
1195 if (insertdict(mp, key, hash, value))
1196 return NULL;
1197 }
1198 return d;
1199 }
1200
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001201 it = PyObject_GetIter(seq);
1202 if (it == NULL){
1203 Py_DECREF(d);
1204 return NULL;
1205 }
1206
Guido van Rossum58da9312007-11-10 23:39:45 +00001207 if (PyDict_CheckExact(d)) {
1208 while ((key = PyIter_Next(it)) != NULL) {
1209 status = PyDict_SetItem(d, key, value);
1210 Py_DECREF(key);
1211 if (status < 0)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001212 goto Fail;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001213 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001214 } else {
1215 while ((key = PyIter_Next(it)) != NULL) {
1216 status = PyObject_SetItem(d, key, value);
1217 Py_DECREF(key);
1218 if (status < 0)
1219 goto Fail;
1220 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001221 }
1222
Guido van Rossum58da9312007-11-10 23:39:45 +00001223 if (PyErr_Occurred())
1224 goto Fail;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001225 Py_DECREF(it);
1226 return d;
1227
1228Fail:
1229 Py_DECREF(it);
1230 Py_DECREF(d);
1231 return NULL;
1232}
1233
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001234static int
1235dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001236{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001237 PyObject *arg = NULL;
1238 int result = 0;
1239
1240 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1241 result = -1;
1242
1243 else if (arg != NULL) {
1244 if (PyObject_HasAttrString(arg, "keys"))
1245 result = PyDict_Merge(self, arg, 1);
1246 else
1247 result = PyDict_MergeFromSeq2(self, arg, 1);
1248 }
1249 if (result == 0 && kwds != NULL)
1250 result = PyDict_Merge(self, kwds, 1);
1251 return result;
1252}
1253
1254static PyObject *
1255dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1256{
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001257 if (dict_update_common(self, args, kwds, "update") != -1)
1258 Py_RETURN_NONE;
1259 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001260}
1261
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001262/* Update unconditionally replaces existing items.
1263 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001264 otherwise it leaves existing items unchanged.
1265
1266 PyDict_{Update,Merge} update/merge from a mapping object.
1267
Tim Petersf582b822001-12-11 18:51:08 +00001268 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001269 producing iterable objects of length 2.
1270*/
1271
Tim Petersf582b822001-12-11 18:51:08 +00001272int
Tim Peters1fc240e2001-10-26 05:06:50 +00001273PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1274{
1275 PyObject *it; /* iter(seq2) */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001276 Py_ssize_t i; /* index into seq2 of current element */
Tim Peters1fc240e2001-10-26 05:06:50 +00001277 PyObject *item; /* seq2[i] */
1278 PyObject *fast; /* item as a 2-tuple or 2-list */
1279
1280 assert(d != NULL);
1281 assert(PyDict_Check(d));
1282 assert(seq2 != NULL);
1283
1284 it = PyObject_GetIter(seq2);
1285 if (it == NULL)
1286 return -1;
1287
1288 for (i = 0; ; ++i) {
1289 PyObject *key, *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001290 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001291
1292 fast = NULL;
1293 item = PyIter_Next(it);
1294 if (item == NULL) {
1295 if (PyErr_Occurred())
1296 goto Fail;
1297 break;
1298 }
1299
1300 /* Convert item to sequence, and verify length 2. */
1301 fast = PySequence_Fast(item, "");
1302 if (fast == NULL) {
1303 if (PyErr_ExceptionMatches(PyExc_TypeError))
1304 PyErr_Format(PyExc_TypeError,
1305 "cannot convert dictionary update "
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001306 "sequence element #%zd to a sequence",
Tim Peters1fc240e2001-10-26 05:06:50 +00001307 i);
1308 goto Fail;
1309 }
1310 n = PySequence_Fast_GET_SIZE(fast);
1311 if (n != 2) {
1312 PyErr_Format(PyExc_ValueError,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001313 "dictionary update sequence element #%zd "
Martin v. Löwis2c95cc62006-02-16 06:54:25 +00001314 "has length %zd; 2 is required",
Martin v. Löwise0e89f72006-02-16 06:59:22 +00001315 i, n);
Tim Peters1fc240e2001-10-26 05:06:50 +00001316 goto Fail;
1317 }
1318
1319 /* Update/merge with this (key, value) pair. */
1320 key = PySequence_Fast_GET_ITEM(fast, 0);
1321 value = PySequence_Fast_GET_ITEM(fast, 1);
1322 if (override || PyDict_GetItem(d, key) == NULL) {
1323 int status = PyDict_SetItem(d, key, value);
1324 if (status < 0)
1325 goto Fail;
1326 }
1327 Py_DECREF(fast);
1328 Py_DECREF(item);
1329 }
1330
1331 i = 0;
1332 goto Return;
1333Fail:
1334 Py_XDECREF(item);
1335 Py_XDECREF(fast);
1336 i = -1;
1337Return:
1338 Py_DECREF(it);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001339 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001340}
1341
Tim Peters6d6c1a32001-08-02 04:15:00 +00001342int
1343PyDict_Update(PyObject *a, PyObject *b)
1344{
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001345 return PyDict_Merge(a, b, 1);
1346}
1347
1348int
1349PyDict_Merge(PyObject *a, PyObject *b, int override)
1350{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001351 register PyDictObject *mp, *other;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001352 register Py_ssize_t i;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001353 PyDictEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001354
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001355 /* We accept for the argument either a concrete dictionary object,
1356 * or an abstract "mapping" object. For the former, we can do
1357 * things quite efficiently. For the latter, we only require that
1358 * PyMapping_Keys() and PyObject_GetItem() be supported.
1359 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001360 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1361 PyErr_BadInternalCall();
1362 return -1;
1363 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001364 mp = (PyDictObject*)a;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001365 if (PyDict_CheckExact(b)) {
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001366 other = (PyDictObject*)b;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001367 if (other == mp || other->ma_used == 0)
1368 /* a.update(a) or a.update({}); nothing to do */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001369 return 0;
Raymond Hettinger186e7392005-05-14 18:08:25 +00001370 if (mp->ma_used == 0)
1371 /* Since the target dict is empty, PyDict_GetItem()
1372 * always returns NULL. Setting override to 1
1373 * skips the unnecessary test.
1374 */
1375 override = 1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001376 /* Do one big resize at the start, rather than
1377 * incrementally resizing as we insert new items. Expect
1378 * that there will be no (or few) overlapping keys.
1379 */
1380 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
Raymond Hettingerc8d22902003-05-07 00:49:40 +00001381 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001382 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001383 }
1384 for (i = 0; i <= other->ma_mask; i++) {
1385 entry = &other->ma_table[i];
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001386 if (entry->me_value != NULL &&
1387 (override ||
1388 PyDict_GetItem(a, entry->me_key) == NULL)) {
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001389 Py_INCREF(entry->me_key);
1390 Py_INCREF(entry->me_value);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001391 if (insertdict(mp, entry->me_key,
1392 (long)entry->me_hash,
1393 entry->me_value) != 0)
1394 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001395 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001396 }
1397 }
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001398 else {
1399 /* Do it the generic, slower way */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001400 PyObject *keys = PyMapping_Keys(b);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001401 PyObject *iter;
1402 PyObject *key, *value;
1403 int status;
1404
1405 if (keys == NULL)
1406 /* Docstring says this is equivalent to E.keys() so
1407 * if E doesn't have a .keys() method we want
1408 * AttributeError to percolate up. Might as well
1409 * do the same for any other error.
1410 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001411 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001412
1413 iter = PyObject_GetIter(keys);
1414 Py_DECREF(keys);
1415 if (iter == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001416 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001417
1418 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001419 if (!override && PyDict_GetItem(a, key) != NULL) {
1420 Py_DECREF(key);
1421 continue;
1422 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001423 value = PyObject_GetItem(b, key);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001424 if (value == NULL) {
1425 Py_DECREF(iter);
1426 Py_DECREF(key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001427 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001428 }
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001429 status = PyDict_SetItem(a, key, value);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001430 Py_DECREF(key);
1431 Py_DECREF(value);
1432 if (status < 0) {
1433 Py_DECREF(iter);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001434 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001435 }
1436 }
1437 Py_DECREF(iter);
1438 if (PyErr_Occurred())
1439 /* Iterator completed, via error */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001440 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001441 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001442 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001443}
1444
1445static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001446dict_copy(register PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001447{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001448 return PyDict_Copy((PyObject*)mp);
1449}
1450
1451PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001452PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001453{
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001454 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001455
1456 if (o == NULL || !PyDict_Check(o)) {
1457 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001458 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001459 }
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001460 copy = PyDict_New();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001461 if (copy == NULL)
1462 return NULL;
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001463 if (PyDict_Merge(copy, o, 1) == 0)
1464 return copy;
1465 Py_DECREF(copy);
Raymond Hettinger1356f782005-04-15 15:58:42 +00001466 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001467}
1468
Martin v. Löwis18e16552006-02-15 17:27:45 +00001469Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001470PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001471{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001472 if (mp == NULL || !PyDict_Check(mp)) {
1473 PyErr_BadInternalCall();
Jeremy Hylton7083bb72004-02-17 20:10:11 +00001474 return -1;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001475 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001476 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001477}
1478
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001479PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001480PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001481{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001482 if (mp == NULL || !PyDict_Check(mp)) {
1483 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001484 return NULL;
1485 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001486 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001487}
1488
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001489PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001490PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001491{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001492 if (mp == NULL || !PyDict_Check(mp)) {
1493 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001494 return NULL;
1495 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001496 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001497}
1498
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001499PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001500PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001501{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001502 if (mp == NULL || !PyDict_Check(mp)) {
1503 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001504 return NULL;
1505 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001506 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001507}
1508
Tim Peterse63415e2001-05-08 04:38:29 +00001509/* Return 1 if dicts equal, 0 if not, -1 if error.
1510 * Gets out as soon as any difference is detected.
1511 * Uses only Py_EQ comparison.
1512 */
1513static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001514dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00001515{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001516 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00001517
1518 if (a->ma_used != b->ma_used)
1519 /* can't be equal if # of entries differ */
1520 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001521
Tim Peterse63415e2001-05-08 04:38:29 +00001522 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Tim Petersafb6ae82001-06-04 21:00:21 +00001523 for (i = 0; i <= a->ma_mask; i++) {
Tim Peterse63415e2001-05-08 04:38:29 +00001524 PyObject *aval = a->ma_table[i].me_value;
1525 if (aval != NULL) {
1526 int cmp;
1527 PyObject *bval;
1528 PyObject *key = a->ma_table[i].me_key;
1529 /* temporarily bump aval's refcount to ensure it stays
1530 alive until we're done with it */
1531 Py_INCREF(aval);
Guido van Rossumdc5f6b22006-08-24 21:29:26 +00001532 /* ditto for key */
1533 Py_INCREF(key);
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001534 bval = PyDict_GetItemWithError((PyObject *)b, key);
Guido van Rossumdc5f6b22006-08-24 21:29:26 +00001535 Py_DECREF(key);
Tim Peterse63415e2001-05-08 04:38:29 +00001536 if (bval == NULL) {
1537 Py_DECREF(aval);
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001538 if (PyErr_Occurred())
1539 return -1;
Tim Peterse63415e2001-05-08 04:38:29 +00001540 return 0;
1541 }
1542 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1543 Py_DECREF(aval);
1544 if (cmp <= 0) /* error or not equal */
1545 return cmp;
1546 }
1547 }
1548 return 1;
1549 }
1550
1551static PyObject *
1552dict_richcompare(PyObject *v, PyObject *w, int op)
1553{
1554 int cmp;
1555 PyObject *res;
1556
1557 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1558 res = Py_NotImplemented;
1559 }
1560 else if (op == Py_EQ || op == Py_NE) {
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001561 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
Tim Peterse63415e2001-05-08 04:38:29 +00001562 if (cmp < 0)
1563 return NULL;
1564 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1565 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001566 else
1567 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001568 Py_INCREF(res);
1569 return res;
1570 }
1571
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001572static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001573dict_contains(register PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001574{
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001575 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001576 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001577
Guido van Rossum89d8c602007-09-18 17:26:56 +00001578 if (!PyUnicode_CheckExact(key) ||
1579 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001580 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001581 if (hash == -1)
1582 return NULL;
1583 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001584 ep = (mp->ma_lookup)(mp, key, hash);
1585 if (ep == NULL)
1586 return NULL;
1587 return PyBool_FromLong(ep->me_value != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001588}
1589
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001590static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001591dict_get(register PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001592{
1593 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001594 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001595 PyObject *val = NULL;
1596 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001597 PyDictEntry *ep;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001598
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001599 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001600 return NULL;
1601
Guido van Rossum89d8c602007-09-18 17:26:56 +00001602 if (!PyUnicode_CheckExact(key) ||
1603 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Barry Warsawc38c5da1997-10-06 17:49:20 +00001604 hash = PyObject_Hash(key);
1605 if (hash == -1)
1606 return NULL;
1607 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001608 ep = (mp->ma_lookup)(mp, key, hash);
1609 if (ep == NULL)
1610 return NULL;
1611 val = ep->me_value;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001612 if (val == NULL)
1613 val = failobj;
1614 Py_INCREF(val);
1615 return val;
1616}
1617
1618
1619static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001620dict_setdefault(register PyDictObject *mp, PyObject *args)
Guido van Rossum164452c2000-08-08 16:12:54 +00001621{
1622 PyObject *key;
1623 PyObject *failobj = Py_None;
1624 PyObject *val = NULL;
1625 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001626 PyDictEntry *ep;
Guido van Rossum164452c2000-08-08 16:12:54 +00001627
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001628 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
Guido van Rossum164452c2000-08-08 16:12:54 +00001629 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001630
Guido van Rossum89d8c602007-09-18 17:26:56 +00001631 if (!PyUnicode_CheckExact(key) ||
1632 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossum164452c2000-08-08 16:12:54 +00001633 hash = PyObject_Hash(key);
1634 if (hash == -1)
1635 return NULL;
1636 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001637 ep = (mp->ma_lookup)(mp, key, hash);
1638 if (ep == NULL)
1639 return NULL;
1640 val = ep->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001641 if (val == NULL) {
1642 val = failobj;
1643 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1644 val = NULL;
1645 }
1646 Py_XINCREF(val);
1647 return val;
1648}
1649
1650
1651static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001652dict_clear(register PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001653{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001654 PyDict_Clear((PyObject *)mp);
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001655 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001656}
1657
Guido van Rossumba6ab842000-12-12 22:02:18 +00001658static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001659dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00001660{
1661 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001662 PyDictEntry *ep;
Guido van Rossume027d982002-04-12 15:11:59 +00001663 PyObject *old_value, *old_key;
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001664 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001665
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001666 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1667 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001668 if (mp->ma_used == 0) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001669 if (deflt) {
1670 Py_INCREF(deflt);
1671 return deflt;
1672 }
Guido van Rossume027d982002-04-12 15:11:59 +00001673 PyErr_SetString(PyExc_KeyError,
1674 "pop(): dictionary is empty");
1675 return NULL;
1676 }
Guido van Rossum89d8c602007-09-18 17:26:56 +00001677 if (!PyUnicode_CheckExact(key) ||
1678 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossume027d982002-04-12 15:11:59 +00001679 hash = PyObject_Hash(key);
1680 if (hash == -1)
1681 return NULL;
1682 }
1683 ep = (mp->ma_lookup)(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001684 if (ep == NULL)
1685 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001686 if (ep->me_value == NULL) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001687 if (deflt) {
1688 Py_INCREF(deflt);
1689 return deflt;
1690 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001691 set_key_error(key);
Guido van Rossume027d982002-04-12 15:11:59 +00001692 return NULL;
1693 }
1694 old_key = ep->me_key;
1695 Py_INCREF(dummy);
1696 ep->me_key = dummy;
1697 old_value = ep->me_value;
1698 ep->me_value = NULL;
1699 mp->ma_used--;
1700 Py_DECREF(old_key);
1701 return old_value;
1702}
1703
1704static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001705dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001706{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001707 Py_ssize_t i = 0;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001708 PyDictEntry *ep;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001709 PyObject *res;
1710
Tim Petersf4b33f62001-06-02 05:42:29 +00001711 /* Allocate the result tuple before checking the size. Believe it
1712 * or not, this allocation could trigger a garbage collection which
1713 * could empty the dict, so if we checked the size first and that
1714 * happened, the result would be an infinite loop (searching for an
1715 * entry that no longer exists). Note that the usual popitem()
1716 * idiom is "while d: k, v = d.popitem()". so needing to throw the
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001717 * tuple away if the dict *is* empty isn't a significant
Tim Petersf4b33f62001-06-02 05:42:29 +00001718 * inefficiency -- possible, but unlikely in practice.
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001719 */
1720 res = PyTuple_New(2);
1721 if (res == NULL)
1722 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001723 if (mp->ma_used == 0) {
1724 Py_DECREF(res);
1725 PyErr_SetString(PyExc_KeyError,
1726 "popitem(): dictionary is empty");
1727 return NULL;
1728 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001729 /* Set ep to "the first" dict entry with a value. We abuse the hash
1730 * field of slot 0 to hold a search finger:
1731 * If slot 0 has a value, use slot 0.
1732 * Else slot 0 is being used to hold a search finger,
1733 * and we use its hash value as the first index to look.
1734 */
1735 ep = &mp->ma_table[0];
1736 if (ep->me_value == NULL) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001737 i = ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001738 /* The hash field may be a real hash value, or it may be a
1739 * legit search finger, or it may be a once-legit search
1740 * finger that's out of bounds now because it wrapped around
1741 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001742 */
Tim Petersafb6ae82001-06-04 21:00:21 +00001743 if (i > mp->ma_mask || i < 1)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001744 i = 1; /* skip slot 0 */
1745 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1746 i++;
Tim Petersafb6ae82001-06-04 21:00:21 +00001747 if (i > mp->ma_mask)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001748 i = 1;
1749 }
1750 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001751 PyTuple_SET_ITEM(res, 0, ep->me_key);
1752 PyTuple_SET_ITEM(res, 1, ep->me_value);
1753 Py_INCREF(dummy);
1754 ep->me_key = dummy;
1755 ep->me_value = NULL;
1756 mp->ma_used--;
1757 assert(mp->ma_table[0].me_value == NULL);
1758 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001759 return res;
1760}
1761
Jeremy Hylton8caad492000-06-23 14:18:11 +00001762static int
1763dict_traverse(PyObject *op, visitproc visit, void *arg)
1764{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001765 Py_ssize_t i = 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001766 PyObject *pk;
1767 PyObject *pv;
1768
1769 while (PyDict_Next(op, &i, &pk, &pv)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001770 Py_VISIT(pk);
1771 Py_VISIT(pv);
Jeremy Hylton8caad492000-06-23 14:18:11 +00001772 }
1773 return 0;
1774}
1775
1776static int
1777dict_tp_clear(PyObject *op)
1778{
1779 PyDict_Clear(op);
1780 return 0;
1781}
1782
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001783static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001784
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001785PyDoc_STRVAR(contains__doc__,
1786"D.__contains__(k) -> True if D has a key k, else False");
1787
1788PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
1789
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001790PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001791"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001792
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001793PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001794"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 +00001795
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001796PyDoc_STRVAR(pop__doc__,
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001797"D.pop(k[,d]) -> v, remove specified key and return the corresponding value\n\
1798If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00001799
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001800PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00001801"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000018022-tuple; but raise KeyError if D is empty");
Tim Petersf7f88b12000-12-13 23:18:45 +00001803
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001804PyDoc_STRVAR(update__doc__,
Guido van Rossumb90c8482007-02-10 01:11:45 +00001805"D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k]\
1806\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 +00001807
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001808PyDoc_STRVAR(fromkeys__doc__,
1809"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
1810v defaults to None.");
1811
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001812PyDoc_STRVAR(clear__doc__,
1813"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001814
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001815PyDoc_STRVAR(copy__doc__,
1816"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00001817
Guido van Rossumb90c8482007-02-10 01:11:45 +00001818/* Forward */
1819static PyObject *dictkeys_new(PyObject *);
1820static PyObject *dictitems_new(PyObject *);
1821static PyObject *dictvalues_new(PyObject *);
1822
Guido van Rossum45c85d12007-07-27 16:31:40 +00001823PyDoc_STRVAR(keys__doc__,
1824 "D.keys() -> a set-like object providing a view on D's keys");
1825PyDoc_STRVAR(items__doc__,
1826 "D.items() -> a set-like object providing a view on D's items");
1827PyDoc_STRVAR(values__doc__,
1828 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00001829
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001830static PyMethodDef mapp_methods[] = {
Guido van Rossume2b70bc2006-08-18 22:13:04 +00001831 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001832 contains__doc__},
Raymond Hettinger0c669672003-12-13 13:31:55 +00001833 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001834 getitem__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00001835 {"get", (PyCFunction)dict_get, METH_VARARGS,
1836 get__doc__},
1837 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1838 setdefault_doc__},
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001839 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
Just van Rossuma797d812002-11-23 09:45:04 +00001840 pop__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001841 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001842 popitem__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001843 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00001844 keys__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001845 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00001846 items__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001847 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00001848 values__doc__},
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001849 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001850 update__doc__},
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001851 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
1852 fromkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001853 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001854 clear__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001855 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001856 copy__doc__},
1857 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001858};
1859
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001860/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001861int
1862PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001863{
1864 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001865 PyDictObject *mp = (PyDictObject *)op;
1866 PyDictEntry *ep;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001867
Guido van Rossum89d8c602007-09-18 17:26:56 +00001868 if (!PyUnicode_CheckExact(key) ||
1869 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001870 hash = PyObject_Hash(key);
1871 if (hash == -1)
1872 return -1;
1873 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001874 ep = (mp->ma_lookup)(mp, key, hash);
1875 return ep == NULL ? -1 : (ep->me_value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001876}
1877
Thomas Wouterscf297e42007-02-23 15:07:44 +00001878/* Internal version of PyDict_Contains used when the hash value is already known */
1879int
1880_PyDict_Contains(PyObject *op, PyObject *key, long hash)
1881{
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001882 PyDictObject *mp = (PyDictObject *)op;
1883 PyDictEntry *ep;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001884
1885 ep = (mp->ma_lookup)(mp, key, hash);
1886 return ep == NULL ? -1 : (ep->me_value != NULL);
1887}
1888
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001889/* Hack to implement "key in dict" */
1890static PySequenceMethods dict_as_sequence = {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001891 0, /* sq_length */
1892 0, /* sq_concat */
1893 0, /* sq_repeat */
1894 0, /* sq_item */
1895 0, /* sq_slice */
1896 0, /* sq_ass_item */
1897 0, /* sq_ass_slice */
1898 PyDict_Contains, /* sq_contains */
1899 0, /* sq_inplace_concat */
1900 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001901};
1902
Guido van Rossum09e563a2001-05-01 12:10:21 +00001903static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001904dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1905{
1906 PyObject *self;
1907
1908 assert(type != NULL && type->tp_alloc != NULL);
1909 self = type->tp_alloc(type, 0);
1910 if (self != NULL) {
1911 PyDictObject *d = (PyDictObject *)self;
1912 /* It's guaranteed that tp->alloc zeroed out the struct. */
1913 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
1914 INIT_NONZERO_DICT_SLOTS(d);
Guido van Rossum89d8c602007-09-18 17:26:56 +00001915 d->ma_lookup = lookdict_unicode;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001916#ifdef SHOW_CONVERSION_COUNTS
1917 ++created;
1918#endif
1919 }
1920 return self;
1921}
1922
Tim Peters25786c02001-09-02 08:22:48 +00001923static int
1924dict_init(PyObject *self, PyObject *args, PyObject *kwds)
1925{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001926 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00001927}
1928
Tim Peters6d6c1a32001-08-02 04:15:00 +00001929static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001930dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00001931{
Raymond Hettinger019a1482004-03-18 02:41:19 +00001932 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001933}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001934
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001935PyDoc_STRVAR(dictionary_doc,
Tim Petersa427a2b2001-10-29 22:25:45 +00001936"dict() -> new empty dictionary.\n"
1937"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Tim Peters1fc240e2001-10-26 05:06:50 +00001938" (key, value) pairs.\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00001939"dict(seq) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00001940" d = {}\n"
1941" for k, v in seq:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00001942" d[k] = v\n"
1943"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
1944" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00001945
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001946PyTypeObject PyDict_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001947 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Petersa427a2b2001-10-29 22:25:45 +00001948 "dict",
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001949 sizeof(PyDictObject),
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001950 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00001951 (destructor)dict_dealloc, /* tp_dealloc */
Guido van Rossum04dbf3b2007-08-07 19:51:00 +00001952 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001953 0, /* tp_getattr */
Guido van Rossumb9324202001-01-18 00:39:02 +00001954 0, /* tp_setattr */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001955 0, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00001956 (reprfunc)dict_repr, /* tp_repr */
1957 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001958 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00001959 &dict_as_mapping, /* tp_as_mapping */
Guido van Rossum50e9fb92006-08-17 05:42:55 +00001960 0, /* tp_hash */
Guido van Rossumb9324202001-01-18 00:39:02 +00001961 0, /* tp_call */
1962 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001963 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumb9324202001-01-18 00:39:02 +00001964 0, /* tp_setattro */
1965 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00001966 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Thomas Wouters27d517b2007-02-25 20:39:11 +00001967 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
Tim Peters25786c02001-09-02 08:22:48 +00001968 dictionary_doc, /* tp_doc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001969 dict_traverse, /* tp_traverse */
1970 dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00001971 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001972 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00001973 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001974 0, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001975 mapp_methods, /* tp_methods */
1976 0, /* tp_members */
1977 0, /* tp_getset */
1978 0, /* tp_base */
1979 0, /* tp_dict */
1980 0, /* tp_descr_get */
1981 0, /* tp_descr_set */
1982 0, /* tp_dictoffset */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001983 dict_init, /* tp_init */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001984 PyType_GenericAlloc, /* tp_alloc */
1985 dict_new, /* tp_new */
Neil Schemenauer6189b892002-04-12 02:43:00 +00001986 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001987};
1988
Guido van Rossum3cca2451997-05-16 14:23:33 +00001989/* For backward compatibility with old dictionary interface */
1990
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001991PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00001992PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001993{
Guido van Rossum3cca2451997-05-16 14:23:33 +00001994 PyObject *kv, *rv;
Martin v. Löwis5b222132007-06-10 09:51:05 +00001995 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00001996 if (kv == NULL)
1997 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00001998 rv = PyDict_GetItem(v, kv);
1999 Py_DECREF(kv);
2000 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002001}
2002
2003int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002004PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002005{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002006 PyObject *kv;
2007 int err;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002008 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002009 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002010 return -1;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002011 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00002012 err = PyDict_SetItem(v, kv, item);
2013 Py_DECREF(kv);
2014 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002015}
2016
2017int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002018PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002019{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002020 PyObject *kv;
2021 int err;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002022 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002023 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002024 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002025 err = PyDict_DelItem(v, kv);
2026 Py_DECREF(kv);
2027 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002028}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002029
Raymond Hettinger019a1482004-03-18 02:41:19 +00002030/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002031
2032typedef struct {
2033 PyObject_HEAD
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002034 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002035 Py_ssize_t di_used;
2036 Py_ssize_t di_pos;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002037 PyObject* di_result; /* reusable result tuple for iteritems */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002038 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002039} dictiterobject;
2040
2041static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002042dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002043{
2044 dictiterobject *di;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002045 di = PyObject_New(dictiterobject, itertype);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002046 if (di == NULL)
2047 return NULL;
2048 Py_INCREF(dict);
2049 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00002050 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002051 di->di_pos = 0;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002052 di->len = dict->ma_used;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002053 if (itertype == &PyDictIterItem_Type) {
2054 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2055 if (di->di_result == NULL) {
2056 Py_DECREF(di);
2057 return NULL;
2058 }
2059 }
2060 else
2061 di->di_result = NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002062 return (PyObject *)di;
2063}
2064
2065static void
2066dictiter_dealloc(dictiterobject *di)
2067{
Guido van Rossum2147df72002-07-16 20:30:22 +00002068 Py_XDECREF(di->di_dict);
Raymond Hettinger019a1482004-03-18 02:41:19 +00002069 Py_XDECREF(di->di_result);
Neil Schemenauer6189b892002-04-12 02:43:00 +00002070 PyObject_Del(di);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002071}
2072
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002073static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002074dictiter_len(dictiterobject *di)
2075{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002076 Py_ssize_t len = 0;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00002077 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002078 len = di->len;
Christian Heimes217cfd12007-12-02 14:31:20 +00002079 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002080}
2081
Guido van Rossumb90c8482007-02-10 01:11:45 +00002082PyDoc_STRVAR(length_hint_doc,
2083 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002084
2085static PyMethodDef dictiter_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002086 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
2087 length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002088 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002089};
2090
Raymond Hettinger019a1482004-03-18 02:41:19 +00002091static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002092{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002093 PyObject *key;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002094 register Py_ssize_t i, mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002095 register PyDictEntry *ep;
2096 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002097
Raymond Hettinger019a1482004-03-18 02:41:19 +00002098 if (d == NULL)
Guido van Rossum2147df72002-07-16 20:30:22 +00002099 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002100 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002101
Raymond Hettinger019a1482004-03-18 02:41:19 +00002102 if (di->di_used != d->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00002103 PyErr_SetString(PyExc_RuntimeError,
2104 "dictionary changed size during iteration");
Guido van Rossum2147df72002-07-16 20:30:22 +00002105 di->di_used = -1; /* Make this state sticky */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002106 return NULL;
2107 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002108
Raymond Hettinger019a1482004-03-18 02:41:19 +00002109 i = di->di_pos;
2110 if (i < 0)
2111 goto fail;
2112 ep = d->ma_table;
2113 mask = d->ma_mask;
2114 while (i <= mask && ep[i].me_value == NULL)
2115 i++;
2116 di->di_pos = i+1;
2117 if (i > mask)
2118 goto fail;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002119 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002120 key = ep[i].me_key;
2121 Py_INCREF(key);
2122 return key;
2123
2124fail:
2125 Py_DECREF(d);
Guido van Rossum2147df72002-07-16 20:30:22 +00002126 di->di_dict = NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002127 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002128}
2129
Raymond Hettinger019a1482004-03-18 02:41:19 +00002130PyTypeObject PyDictIterKey_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002131 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Christian Heimesf83be4e2007-11-28 09:44:38 +00002132 "dict_keyiterator", /* tp_name */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002133 sizeof(dictiterobject), /* tp_basicsize */
2134 0, /* tp_itemsize */
2135 /* methods */
2136 (destructor)dictiter_dealloc, /* tp_dealloc */
2137 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002138 0, /* tp_getattr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002139 0, /* tp_setattr */
2140 0, /* tp_compare */
2141 0, /* tp_repr */
2142 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002143 0, /* tp_as_sequence */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002144 0, /* tp_as_mapping */
2145 0, /* tp_hash */
2146 0, /* tp_call */
2147 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002148 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002149 0, /* tp_setattro */
2150 0, /* tp_as_buffer */
2151 Py_TPFLAGS_DEFAULT, /* tp_flags */
2152 0, /* tp_doc */
2153 0, /* tp_traverse */
2154 0, /* tp_clear */
2155 0, /* tp_richcompare */
2156 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002157 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002158 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002159 dictiter_methods, /* tp_methods */
2160 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002161};
2162
2163static PyObject *dictiter_iternextvalue(dictiterobject *di)
2164{
2165 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002166 register Py_ssize_t i, mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002167 register PyDictEntry *ep;
2168 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002169
2170 if (d == NULL)
2171 return NULL;
2172 assert (PyDict_Check(d));
2173
2174 if (di->di_used != d->ma_used) {
2175 PyErr_SetString(PyExc_RuntimeError,
2176 "dictionary changed size during iteration");
2177 di->di_used = -1; /* Make this state sticky */
2178 return NULL;
2179 }
2180
2181 i = di->di_pos;
Guido van Rossum09240f62004-03-20 19:11:58 +00002182 mask = d->ma_mask;
2183 if (i < 0 || i > mask)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002184 goto fail;
2185 ep = d->ma_table;
Guido van Rossum09240f62004-03-20 19:11:58 +00002186 while ((value=ep[i].me_value) == NULL) {
Raymond Hettinger019a1482004-03-18 02:41:19 +00002187 i++;
Guido van Rossum09240f62004-03-20 19:11:58 +00002188 if (i > mask)
2189 goto fail;
2190 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002191 di->di_pos = i+1;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002192 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002193 Py_INCREF(value);
2194 return value;
2195
2196fail:
2197 Py_DECREF(d);
2198 di->di_dict = NULL;
2199 return NULL;
2200}
2201
2202PyTypeObject PyDictIterValue_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002203 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Christian Heimesf83be4e2007-11-28 09:44:38 +00002204 "dict_valueiterator", /* tp_name */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002205 sizeof(dictiterobject), /* tp_basicsize */
2206 0, /* tp_itemsize */
2207 /* methods */
2208 (destructor)dictiter_dealloc, /* tp_dealloc */
2209 0, /* tp_print */
2210 0, /* tp_getattr */
2211 0, /* tp_setattr */
2212 0, /* tp_compare */
2213 0, /* tp_repr */
2214 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002215 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002216 0, /* tp_as_mapping */
2217 0, /* tp_hash */
2218 0, /* tp_call */
2219 0, /* tp_str */
2220 PyObject_GenericGetAttr, /* tp_getattro */
2221 0, /* tp_setattro */
2222 0, /* tp_as_buffer */
2223 Py_TPFLAGS_DEFAULT, /* tp_flags */
2224 0, /* tp_doc */
2225 0, /* tp_traverse */
2226 0, /* tp_clear */
2227 0, /* tp_richcompare */
2228 0, /* tp_weaklistoffset */
2229 PyObject_SelfIter, /* tp_iter */
2230 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002231 dictiter_methods, /* tp_methods */
2232 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002233};
2234
2235static PyObject *dictiter_iternextitem(dictiterobject *di)
2236{
2237 PyObject *key, *value, *result = di->di_result;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002238 register Py_ssize_t i, mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002239 register PyDictEntry *ep;
2240 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002241
2242 if (d == NULL)
2243 return NULL;
2244 assert (PyDict_Check(d));
2245
2246 if (di->di_used != d->ma_used) {
2247 PyErr_SetString(PyExc_RuntimeError,
2248 "dictionary changed size during iteration");
2249 di->di_used = -1; /* Make this state sticky */
2250 return NULL;
2251 }
2252
2253 i = di->di_pos;
2254 if (i < 0)
2255 goto fail;
2256 ep = d->ma_table;
2257 mask = d->ma_mask;
2258 while (i <= mask && ep[i].me_value == NULL)
2259 i++;
2260 di->di_pos = i+1;
2261 if (i > mask)
2262 goto fail;
2263
2264 if (result->ob_refcnt == 1) {
2265 Py_INCREF(result);
2266 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2267 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2268 } else {
2269 result = PyTuple_New(2);
Tim Peters60b29962006-01-01 01:19:23 +00002270 if (result == NULL)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002271 return NULL;
2272 }
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002273 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002274 key = ep[i].me_key;
2275 value = ep[i].me_value;
2276 Py_INCREF(key);
2277 Py_INCREF(value);
2278 PyTuple_SET_ITEM(result, 0, key);
2279 PyTuple_SET_ITEM(result, 1, value);
2280 return result;
2281
2282fail:
2283 Py_DECREF(d);
2284 di->di_dict = NULL;
2285 return NULL;
2286}
2287
2288PyTypeObject PyDictIterItem_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002289 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Christian Heimesf83be4e2007-11-28 09:44:38 +00002290 "dict_itemiterator", /* tp_name */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002291 sizeof(dictiterobject), /* tp_basicsize */
2292 0, /* tp_itemsize */
2293 /* methods */
2294 (destructor)dictiter_dealloc, /* tp_dealloc */
2295 0, /* tp_print */
2296 0, /* tp_getattr */
2297 0, /* tp_setattr */
2298 0, /* tp_compare */
2299 0, /* tp_repr */
2300 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002301 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002302 0, /* tp_as_mapping */
2303 0, /* tp_hash */
2304 0, /* tp_call */
2305 0, /* tp_str */
2306 PyObject_GenericGetAttr, /* tp_getattro */
2307 0, /* tp_setattro */
2308 0, /* tp_as_buffer */
2309 Py_TPFLAGS_DEFAULT, /* tp_flags */
2310 0, /* tp_doc */
2311 0, /* tp_traverse */
2312 0, /* tp_clear */
2313 0, /* tp_richcompare */
2314 0, /* tp_weaklistoffset */
2315 PyObject_SelfIter, /* tp_iter */
2316 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002317 dictiter_methods, /* tp_methods */
2318 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002319};
Guido van Rossumb90c8482007-02-10 01:11:45 +00002320
2321
Guido van Rossum3ac67412007-02-10 18:55:06 +00002322/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002323/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002324/***********************************************/
2325
Guido van Rossumb90c8482007-02-10 01:11:45 +00002326/* The instance lay-out is the same for all three; but the type differs. */
2327
2328typedef struct {
2329 PyObject_HEAD
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002330 PyDictObject *dv_dict;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002331} dictviewobject;
2332
2333
2334static void
Guido van Rossum3ac67412007-02-10 18:55:06 +00002335dictview_dealloc(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002336{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002337 Py_XDECREF(dv->dv_dict);
2338 PyObject_Del(dv);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002339}
2340
Guido van Rossum83825ac2007-02-10 04:54:19 +00002341static Py_ssize_t
Guido van Rossum3ac67412007-02-10 18:55:06 +00002342dictview_len(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002343{
2344 Py_ssize_t len = 0;
Guido van Rossum3ac67412007-02-10 18:55:06 +00002345 if (dv->dv_dict != NULL)
2346 len = dv->dv_dict->ma_used;
Guido van Rossum83825ac2007-02-10 04:54:19 +00002347 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002348}
2349
2350static PyObject *
2351dictview_new(PyObject *dict, PyTypeObject *type)
2352{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002353 dictviewobject *dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002354 if (dict == NULL) {
2355 PyErr_BadInternalCall();
2356 return NULL;
2357 }
2358 if (!PyDict_Check(dict)) {
2359 /* XXX Get rid of this restriction later */
2360 PyErr_Format(PyExc_TypeError,
2361 "%s() requires a dict argument, not '%s'",
2362 type->tp_name, dict->ob_type->tp_name);
2363 return NULL;
2364 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002365 dv = PyObject_New(dictviewobject, type);
2366 if (dv == NULL)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002367 return NULL;
2368 Py_INCREF(dict);
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002369 dv->dv_dict = (PyDictObject *)dict;
Guido van Rossum3ac67412007-02-10 18:55:06 +00002370 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002371}
2372
Neal Norwitze36f2ba2007-02-26 23:12:28 +00002373/* TODO(guido): The views objects are not complete:
2374
2375 * support more set operations
2376 * support arbitrary mappings?
2377 - either these should be static or exported in dictobject.h
2378 - if public then they should probably be in builtins
2379*/
2380
Guido van Rossumaac530c2007-08-24 22:33:45 +00002381/* Return 1 if self is a subset of other, iterating over self;
2382 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002383static int
2384all_contained_in(PyObject *self, PyObject *other)
2385{
2386 PyObject *iter = PyObject_GetIter(self);
2387 int ok = 1;
2388
2389 if (iter == NULL)
2390 return -1;
2391 for (;;) {
2392 PyObject *next = PyIter_Next(iter);
2393 if (next == NULL) {
2394 if (PyErr_Occurred())
2395 ok = -1;
2396 break;
2397 }
2398 ok = PySequence_Contains(other, next);
2399 Py_DECREF(next);
2400 if (ok <= 0)
2401 break;
2402 }
2403 Py_DECREF(iter);
2404 return ok;
2405}
2406
2407static PyObject *
2408dictview_richcompare(PyObject *self, PyObject *other, int op)
2409{
Guido van Rossumaac530c2007-08-24 22:33:45 +00002410 Py_ssize_t len_self, len_other;
2411 int ok;
2412 PyObject *result;
2413
Guido van Rossumd9214d12007-02-12 02:23:40 +00002414 assert(self != NULL);
2415 assert(PyDictViewSet_Check(self));
2416 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00002417
Guido van Rossumaac530c2007-08-24 22:33:45 +00002418 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other)) {
Guido van Rossumd9214d12007-02-12 02:23:40 +00002419 Py_INCREF(Py_NotImplemented);
2420 return Py_NotImplemented;
2421 }
Guido van Rossumaac530c2007-08-24 22:33:45 +00002422
2423 len_self = PyObject_Size(self);
2424 if (len_self < 0)
2425 return NULL;
2426 len_other = PyObject_Size(other);
2427 if (len_other < 0)
2428 return NULL;
2429
2430 ok = 0;
2431 switch(op) {
2432
2433 case Py_NE:
2434 case Py_EQ:
2435 if (len_self == len_other)
2436 ok = all_contained_in(self, other);
2437 if (op == Py_NE && ok >= 0)
2438 ok = !ok;
2439 break;
2440
2441 case Py_LT:
2442 if (len_self < len_other)
2443 ok = all_contained_in(self, other);
2444 break;
2445
2446 case Py_LE:
2447 if (len_self <= len_other)
2448 ok = all_contained_in(self, other);
2449 break;
2450
2451 case Py_GT:
2452 if (len_self > len_other)
2453 ok = all_contained_in(other, self);
2454 break;
2455
2456 case Py_GE:
2457 if (len_self >= len_other)
2458 ok = all_contained_in(other, self);
2459 break;
2460
2461 }
2462 if (ok < 0)
2463 return NULL;
2464 result = ok ? Py_True : Py_False;
2465 Py_INCREF(result);
2466 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00002467}
2468
Guido van Rossum3ac67412007-02-10 18:55:06 +00002469/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002470
2471static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002472dictkeys_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002473{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002474 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002475 Py_RETURN_NONE;
2476 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002477 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
2478}
2479
2480static int
2481dictkeys_contains(dictviewobject *dv, PyObject *obj)
2482{
2483 if (dv->dv_dict == NULL)
2484 return 0;
2485 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002486}
2487
Guido van Rossum83825ac2007-02-10 04:54:19 +00002488static PySequenceMethods dictkeys_as_sequence = {
2489 (lenfunc)dictview_len, /* sq_length */
2490 0, /* sq_concat */
2491 0, /* sq_repeat */
2492 0, /* sq_item */
2493 0, /* sq_slice */
2494 0, /* sq_ass_item */
2495 0, /* sq_ass_slice */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002496 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002497};
2498
Guido van Rossum523259b2007-08-24 23:41:22 +00002499static PyObject*
2500dictviews_sub(PyObject* self, PyObject *other)
2501{
2502 PyObject *result = PySet_New(self);
2503 PyObject *tmp;
2504 if (result == NULL)
2505 return NULL;
2506
2507 tmp = PyObject_CallMethod(result, "difference_update", "O", other);
2508 if (tmp == NULL) {
2509 Py_DECREF(result);
2510 return NULL;
2511 }
2512
2513 Py_DECREF(tmp);
2514 return result;
2515}
2516
2517static PyObject*
2518dictviews_and(PyObject* self, PyObject *other)
2519{
2520 PyObject *result = PySet_New(self);
2521 PyObject *tmp;
2522 if (result == NULL)
2523 return NULL;
2524
2525 tmp = PyObject_CallMethod(result, "intersection_update", "O", other);
2526 if (tmp == NULL) {
2527 Py_DECREF(result);
2528 return NULL;
2529 }
2530
2531 Py_DECREF(tmp);
2532 return result;
2533}
2534
2535static PyObject*
2536dictviews_or(PyObject* self, PyObject *other)
2537{
2538 PyObject *result = PySet_New(self);
2539 PyObject *tmp;
2540 if (result == NULL)
2541 return NULL;
2542
2543 tmp = PyObject_CallMethod(result, "update", "O", other);
2544 if (tmp == NULL) {
2545 Py_DECREF(result);
2546 return NULL;
2547 }
2548
2549 Py_DECREF(tmp);
2550 return result;
2551}
2552
2553static PyObject*
2554dictviews_xor(PyObject* self, PyObject *other)
2555{
2556 PyObject *result = PySet_New(self);
2557 PyObject *tmp;
2558 if (result == NULL)
2559 return NULL;
2560
2561 tmp = PyObject_CallMethod(result, "symmetric_difference_update", "O",
2562 other);
2563 if (tmp == NULL) {
2564 Py_DECREF(result);
2565 return NULL;
2566 }
2567
2568 Py_DECREF(tmp);
2569 return result;
2570}
2571
2572static PyNumberMethods dictviews_as_number = {
2573 0, /*nb_add*/
2574 (binaryfunc)dictviews_sub, /*nb_subtract*/
2575 0, /*nb_multiply*/
2576 0, /*nb_remainder*/
2577 0, /*nb_divmod*/
2578 0, /*nb_power*/
2579 0, /*nb_negative*/
2580 0, /*nb_positive*/
2581 0, /*nb_absolute*/
2582 0, /*nb_bool*/
2583 0, /*nb_invert*/
2584 0, /*nb_lshift*/
2585 0, /*nb_rshift*/
2586 (binaryfunc)dictviews_and, /*nb_and*/
2587 (binaryfunc)dictviews_xor, /*nb_xor*/
2588 (binaryfunc)dictviews_or, /*nb_or*/
2589};
2590
Guido van Rossumb90c8482007-02-10 01:11:45 +00002591static PyMethodDef dictkeys_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002592 {NULL, NULL} /* sentinel */
2593};
2594
2595PyTypeObject PyDictKeys_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002596 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002597 "dict_keys", /* tp_name */
2598 sizeof(dictviewobject), /* tp_basicsize */
2599 0, /* tp_itemsize */
2600 /* methods */
2601 (destructor)dictview_dealloc, /* tp_dealloc */
2602 0, /* tp_print */
2603 0, /* tp_getattr */
2604 0, /* tp_setattr */
2605 0, /* tp_compare */
2606 0, /* tp_repr */
Guido van Rossum523259b2007-08-24 23:41:22 +00002607 &dictviews_as_number, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002608 &dictkeys_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002609 0, /* tp_as_mapping */
2610 0, /* tp_hash */
2611 0, /* tp_call */
2612 0, /* tp_str */
2613 PyObject_GenericGetAttr, /* tp_getattro */
2614 0, /* tp_setattro */
2615 0, /* tp_as_buffer */
2616 Py_TPFLAGS_DEFAULT, /* tp_flags */
2617 0, /* tp_doc */
2618 0, /* tp_traverse */
2619 0, /* tp_clear */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002620 dictview_richcompare, /* tp_richcompare */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002621 0, /* tp_weaklistoffset */
2622 (getiterfunc)dictkeys_iter, /* tp_iter */
2623 0, /* tp_iternext */
2624 dictkeys_methods, /* tp_methods */
2625 0,
2626};
2627
2628static PyObject *
2629dictkeys_new(PyObject *dict)
2630{
2631 return dictview_new(dict, &PyDictKeys_Type);
2632}
2633
Guido van Rossum3ac67412007-02-10 18:55:06 +00002634/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002635
2636static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002637dictitems_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002638{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002639 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002640 Py_RETURN_NONE;
2641 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002642 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
2643}
2644
2645static int
2646dictitems_contains(dictviewobject *dv, PyObject *obj)
2647{
2648 PyObject *key, *value, *found;
2649 if (dv->dv_dict == NULL)
2650 return 0;
2651 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
2652 return 0;
2653 key = PyTuple_GET_ITEM(obj, 0);
2654 value = PyTuple_GET_ITEM(obj, 1);
2655 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
2656 if (found == NULL) {
2657 if (PyErr_Occurred())
2658 return -1;
2659 return 0;
2660 }
2661 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002662}
2663
Guido van Rossum83825ac2007-02-10 04:54:19 +00002664static PySequenceMethods dictitems_as_sequence = {
2665 (lenfunc)dictview_len, /* sq_length */
2666 0, /* sq_concat */
2667 0, /* sq_repeat */
2668 0, /* sq_item */
2669 0, /* sq_slice */
2670 0, /* sq_ass_item */
2671 0, /* sq_ass_slice */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002672 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002673};
2674
Guido van Rossumb90c8482007-02-10 01:11:45 +00002675static PyMethodDef dictitems_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002676 {NULL, NULL} /* sentinel */
2677};
2678
2679PyTypeObject PyDictItems_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002680 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002681 "dict_items", /* tp_name */
2682 sizeof(dictviewobject), /* tp_basicsize */
2683 0, /* tp_itemsize */
2684 /* methods */
2685 (destructor)dictview_dealloc, /* tp_dealloc */
2686 0, /* tp_print */
2687 0, /* tp_getattr */
2688 0, /* tp_setattr */
2689 0, /* tp_compare */
2690 0, /* tp_repr */
Guido van Rossum523259b2007-08-24 23:41:22 +00002691 &dictviews_as_number, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002692 &dictitems_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002693 0, /* tp_as_mapping */
2694 0, /* tp_hash */
2695 0, /* tp_call */
2696 0, /* tp_str */
2697 PyObject_GenericGetAttr, /* tp_getattro */
2698 0, /* tp_setattro */
2699 0, /* tp_as_buffer */
2700 Py_TPFLAGS_DEFAULT, /* tp_flags */
2701 0, /* tp_doc */
2702 0, /* tp_traverse */
2703 0, /* tp_clear */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002704 dictview_richcompare, /* tp_richcompare */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002705 0, /* tp_weaklistoffset */
2706 (getiterfunc)dictitems_iter, /* tp_iter */
2707 0, /* tp_iternext */
2708 dictitems_methods, /* tp_methods */
2709 0,
2710};
2711
2712static PyObject *
2713dictitems_new(PyObject *dict)
2714{
2715 return dictview_new(dict, &PyDictItems_Type);
2716}
2717
Guido van Rossum3ac67412007-02-10 18:55:06 +00002718/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002719
2720static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002721dictvalues_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002722{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002723 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002724 Py_RETURN_NONE;
2725 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002726 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002727}
2728
Guido van Rossum83825ac2007-02-10 04:54:19 +00002729static PySequenceMethods dictvalues_as_sequence = {
2730 (lenfunc)dictview_len, /* sq_length */
2731 0, /* sq_concat */
2732 0, /* sq_repeat */
2733 0, /* sq_item */
2734 0, /* sq_slice */
2735 0, /* sq_ass_item */
2736 0, /* sq_ass_slice */
2737 (objobjproc)0, /* sq_contains */
2738};
2739
Guido van Rossumb90c8482007-02-10 01:11:45 +00002740static PyMethodDef dictvalues_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002741 {NULL, NULL} /* sentinel */
2742};
2743
2744PyTypeObject PyDictValues_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002745 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002746 "dict_values", /* tp_name */
2747 sizeof(dictviewobject), /* tp_basicsize */
2748 0, /* tp_itemsize */
2749 /* methods */
2750 (destructor)dictview_dealloc, /* tp_dealloc */
2751 0, /* tp_print */
2752 0, /* tp_getattr */
2753 0, /* tp_setattr */
2754 0, /* tp_compare */
2755 0, /* tp_repr */
2756 0, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002757 &dictvalues_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002758 0, /* tp_as_mapping */
2759 0, /* tp_hash */
2760 0, /* tp_call */
2761 0, /* tp_str */
2762 PyObject_GenericGetAttr, /* tp_getattro */
2763 0, /* tp_setattro */
2764 0, /* tp_as_buffer */
2765 Py_TPFLAGS_DEFAULT, /* tp_flags */
2766 0, /* tp_doc */
2767 0, /* tp_traverse */
2768 0, /* tp_clear */
2769 0, /* tp_richcompare */
2770 0, /* tp_weaklistoffset */
2771 (getiterfunc)dictvalues_iter, /* tp_iter */
2772 0, /* tp_iternext */
2773 dictvalues_methods, /* tp_methods */
2774 0,
2775};
2776
2777static PyObject *
2778dictvalues_new(PyObject *dict)
2779{
2780 return dictview_new(dict, &PyDictValues_Type);
2781}