blob: 9c17be7a2525816e8109fdccbd50ef82415f4441 [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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000020 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);
Thomas Wouters89f507f2006-12-13 04:49:30 +000026}
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
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000055the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
Guido van Rossumdc5f6b22006-08-24 21:29:26 +000056their 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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000145 return dummy;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000146}
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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000160 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);
Fred Drake1bff34a2000-08-31 19:31:38 +0000163}
164#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000165
Christian Heimes77c02eb2008-02-09 02:18:51 +0000166/* Debug statistic to compare allocations with reuse through the free list */
167#undef SHOW_ALLOC_COUNT
168#ifdef SHOW_ALLOC_COUNT
169static size_t count_alloc = 0;
170static size_t count_reuse = 0;
171
172static void
173show_alloc(void)
174{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000175 fprintf(stderr, "Dict allocations: %" PY_FORMAT_SIZE_T "d\n",
176 count_alloc);
177 fprintf(stderr, "Dict reuse through freelist: %" PY_FORMAT_SIZE_T
178 "d\n", count_reuse);
179 fprintf(stderr, "%.2f%% reuse rate\n\n",
180 (100.0*count_reuse/(count_alloc+count_reuse)));
Christian Heimes77c02eb2008-02-09 02:18:51 +0000181}
182#endif
183
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000184/* Debug statistic to count GC tracking of dicts */
185#ifdef SHOW_TRACK_COUNT
186static Py_ssize_t count_untracked = 0;
187static Py_ssize_t count_tracked = 0;
188
189static void
190show_track(void)
191{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000192 fprintf(stderr, "Dicts created: %" PY_FORMAT_SIZE_T "d\n",
193 count_tracked + count_untracked);
194 fprintf(stderr, "Dicts tracked by the GC: %" PY_FORMAT_SIZE_T
195 "d\n", count_tracked);
196 fprintf(stderr, "%.2f%% dict tracking rate\n\n",
197 (100.0*count_tracked/(count_untracked+count_tracked)));
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000198}
199#endif
200
201
Tim Peters6d6c1a32001-08-02 04:15:00 +0000202/* Initialization macros.
203 There are two ways to create a dict: PyDict_New() is the main C API
204 function, and the tp_new slot maps to dict_new(). In the latter case we
205 can save a little time over what PyDict_New does because it's guaranteed
206 that the PyDictObject struct is already zeroed out.
207 Everyone except dict_new() should use EMPTY_TO_MINSIZE (unless they have
208 an excellent reason not to).
209*/
210
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000211#define INIT_NONZERO_DICT_SLOTS(mp) do { \
212 (mp)->ma_table = (mp)->ma_smalltable; \
213 (mp)->ma_mask = PyDict_MINSIZE - 1; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000214 } while(0)
215
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000216#define EMPTY_TO_MINSIZE(mp) do { \
217 memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \
218 (mp)->ma_used = (mp)->ma_fill = 0; \
219 INIT_NONZERO_DICT_SLOTS(mp); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000220 } while(0)
221
Raymond Hettinger43442782004-03-17 21:55:03 +0000222/* Dictionary reuse scheme to save calls to malloc, free, and memset */
Christian Heimes2202f872008-02-06 14:31:34 +0000223#ifndef PyDict_MAXFREELIST
224#define PyDict_MAXFREELIST 80
225#endif
226static PyDictObject *free_list[PyDict_MAXFREELIST];
227static int numfree = 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000228
Christian Heimes77c02eb2008-02-09 02:18:51 +0000229void
230PyDict_Fini(void)
231{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000232 PyDictObject *op;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000233
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000234 while (numfree) {
235 op = free_list[--numfree];
236 assert(PyDict_CheckExact(op));
237 PyObject_GC_Del(op);
238 }
Christian Heimes77c02eb2008-02-09 02:18:51 +0000239}
240
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000241PyObject *
Thomas Wouters78890102000-07-22 19:25:51 +0000242PyDict_New(void)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000243{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000244 register PyDictObject *mp;
245 if (dummy == NULL) { /* Auto-initialize dummy */
246 dummy = PyUnicode_FromString("<dummy key>");
247 if (dummy == NULL)
248 return NULL;
Fred Drake1bff34a2000-08-31 19:31:38 +0000249#ifdef SHOW_CONVERSION_COUNTS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000250 Py_AtExit(show_counts);
Fred Drake1bff34a2000-08-31 19:31:38 +0000251#endif
Christian Heimes77c02eb2008-02-09 02:18:51 +0000252#ifdef SHOW_ALLOC_COUNT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000253 Py_AtExit(show_alloc);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000254#endif
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000255#ifdef SHOW_TRACK_COUNT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000256 Py_AtExit(show_track);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000257#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000258 }
259 if (numfree) {
260 mp = free_list[--numfree];
261 assert (mp != NULL);
262 assert (Py_TYPE(mp) == &PyDict_Type);
263 _Py_NewReference((PyObject *)mp);
264 if (mp->ma_fill) {
265 EMPTY_TO_MINSIZE(mp);
266 } else {
267 /* At least set ma_table and ma_mask; these are wrong
268 if an empty but presized dict is added to freelist */
269 INIT_NONZERO_DICT_SLOTS(mp);
270 }
271 assert (mp->ma_used == 0);
272 assert (mp->ma_table == mp->ma_smalltable);
273 assert (mp->ma_mask == PyDict_MINSIZE - 1);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000274#ifdef SHOW_ALLOC_COUNT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000275 count_reuse++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000276#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000277 } else {
278 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
279 if (mp == NULL)
280 return NULL;
281 EMPTY_TO_MINSIZE(mp);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000282#ifdef SHOW_ALLOC_COUNT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000283 count_alloc++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000284#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000285 }
286 mp->ma_lookup = lookdict_unicode;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000287#ifdef SHOW_TRACK_COUNT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000288 count_untracked++;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000289#endif
Fred Drake1bff34a2000-08-31 19:31:38 +0000290#ifdef SHOW_CONVERSION_COUNTS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000291 ++created;
Fred Drake1bff34a2000-08-31 19:31:38 +0000292#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000293 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000294}
295
296/*
297The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000298This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000299Open addressing is preferred over chaining since the link overhead for
300chaining would be substantial (100% with typical malloc overhead).
301
Tim Peterseb28ef22001-06-02 05:27:19 +0000302The initial probe index is computed as hash mod the table size. Subsequent
303probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000304
305All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000306
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000307The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000308contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000309Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000310
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000311lookdict() is general-purpose, and may return NULL if (and only if) a
312comparison raises an exception (this was new in Python 2.5).
Guido van Rossum89d8c602007-09-18 17:26:56 +0000313lookdict_unicode() below is specialized to string keys, comparison of which can
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000314never raise an exception; that function can never return NULL. For both, when
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000315the key isn't found a PyDictEntry* is returned for which the me_value field is
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000316NULL; this is the slot in the dict at which the key would have been found, and
317the caller can (if it wishes) add the <key, value> pair to the returned
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000318PyDictEntry*.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000319*/
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000320static PyDictEntry *
321lookdict(PyDictObject *mp, PyObject *key, register long hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000322{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000323 register size_t i;
324 register size_t perturb;
325 register PyDictEntry *freeslot;
326 register size_t mask = (size_t)mp->ma_mask;
327 PyDictEntry *ep0 = mp->ma_table;
328 register PyDictEntry *ep;
329 register int cmp;
330 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000331
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000332 i = (size_t)hash & mask;
333 ep = &ep0[i];
334 if (ep->me_key == NULL || ep->me_key == key)
335 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000336
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000337 if (ep->me_key == dummy)
338 freeslot = ep;
339 else {
340 if (ep->me_hash == hash) {
341 startkey = ep->me_key;
342 Py_INCREF(startkey);
343 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
344 Py_DECREF(startkey);
345 if (cmp < 0)
346 return NULL;
347 if (ep0 == mp->ma_table && ep->me_key == startkey) {
348 if (cmp > 0)
349 return ep;
350 }
351 else {
352 /* The compare did major nasty stuff to the
353 * dict: start over.
354 * XXX A clever adversary could prevent this
355 * XXX from terminating.
356 */
357 return lookdict(mp, key, hash);
358 }
359 }
360 freeslot = NULL;
361 }
Tim Peters15d49292001-05-27 07:39:22 +0000362
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000363 /* In the loop, me_key == dummy is by far (factor of 100s) the
364 least likely outcome, so test for that last. */
365 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
366 i = (i << 2) + i + perturb + 1;
367 ep = &ep0[i & mask];
368 if (ep->me_key == NULL)
369 return freeslot == NULL ? ep : freeslot;
370 if (ep->me_key == key)
371 return ep;
372 if (ep->me_hash == hash && ep->me_key != dummy) {
373 startkey = ep->me_key;
374 Py_INCREF(startkey);
375 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
376 Py_DECREF(startkey);
377 if (cmp < 0)
378 return NULL;
379 if (ep0 == mp->ma_table && ep->me_key == startkey) {
380 if (cmp > 0)
381 return ep;
382 }
383 else {
384 /* The compare did major nasty stuff to the
385 * dict: start over.
386 * XXX A clever adversary could prevent this
387 * XXX from terminating.
388 */
389 return lookdict(mp, key, hash);
390 }
391 }
392 else if (ep->me_key == dummy && freeslot == NULL)
393 freeslot = ep;
394 }
395 assert(0); /* NOT REACHED */
396 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000397}
398
399/*
Guido van Rossum89d8c602007-09-18 17:26:56 +0000400 * Hacked up version of lookdict which can assume keys are always
401 * unicodes; this assumption allows testing for errors during
402 * PyObject_RichCompareBool() to be dropped; unicode-unicode
403 * comparisons never raise exceptions. This also means we don't need
404 * to go through PyObject_RichCompareBool(); we can always use
405 * unicode_eq() directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000406 *
Guido van Rossum89d8c602007-09-18 17:26:56 +0000407 * This is valuable because dicts with only unicode keys are very common.
Fred Drake1bff34a2000-08-31 19:31:38 +0000408 */
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000409static PyDictEntry *
410lookdict_unicode(PyDictObject *mp, PyObject *key, register long hash)
Fred Drake1bff34a2000-08-31 19:31:38 +0000411{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000412 register size_t i;
413 register size_t perturb;
414 register PyDictEntry *freeslot;
415 register size_t mask = (size_t)mp->ma_mask;
416 PyDictEntry *ep0 = mp->ma_table;
417 register PyDictEntry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000418
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000419 /* Make sure this function doesn't have to handle non-unicode keys,
420 including subclasses of str; e.g., one reason to subclass
421 unicodes is to override __eq__, and for speed we don't cater to
422 that here. */
423 if (!PyUnicode_CheckExact(key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000424#ifdef SHOW_CONVERSION_COUNTS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000425 ++converted;
Fred Drake1bff34a2000-08-31 19:31:38 +0000426#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000427 mp->ma_lookup = lookdict;
428 return lookdict(mp, key, hash);
429 }
430 i = hash & mask;
431 ep = &ep0[i];
432 if (ep->me_key == NULL || ep->me_key == key)
433 return ep;
434 if (ep->me_key == dummy)
435 freeslot = ep;
436 else {
437 if (ep->me_hash == hash && unicode_eq(ep->me_key, key))
438 return ep;
439 freeslot = NULL;
440 }
Tim Peters15d49292001-05-27 07:39:22 +0000441
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000442 /* In the loop, me_key == dummy is by far (factor of 100s) the
443 least likely outcome, so test for that last. */
444 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
445 i = (i << 2) + i + perturb + 1;
446 ep = &ep0[i & mask];
447 if (ep->me_key == NULL)
448 return freeslot == NULL ? ep : freeslot;
449 if (ep->me_key == key
450 || (ep->me_hash == hash
451 && ep->me_key != dummy
452 && unicode_eq(ep->me_key, key)))
453 return ep;
454 if (ep->me_key == dummy && freeslot == NULL)
455 freeslot = ep;
456 }
457 assert(0); /* NOT REACHED */
458 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000459}
460
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000461#ifdef SHOW_TRACK_COUNT
462#define INCREASE_TRACK_COUNT \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000463 (count_tracked++, count_untracked--);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000464#define DECREASE_TRACK_COUNT \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000465 (count_tracked--, count_untracked++);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000466#else
467#define INCREASE_TRACK_COUNT
468#define DECREASE_TRACK_COUNT
469#endif
470
471#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000472 do { \
473 if (!_PyObject_GC_IS_TRACKED(mp)) { \
474 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
475 _PyObject_GC_MAY_BE_TRACKED(value)) { \
476 _PyObject_GC_TRACK(mp); \
477 INCREASE_TRACK_COUNT \
478 } \
479 } \
480 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000481
482void
483_PyDict_MaybeUntrack(PyObject *op)
484{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000485 PyDictObject *mp;
486 PyObject *value;
487 Py_ssize_t mask, i;
488 PyDictEntry *ep;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000489
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000490 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
491 return;
492
493 mp = (PyDictObject *) op;
494 ep = mp->ma_table;
495 mask = mp->ma_mask;
496 for (i = 0; i <= mask; i++) {
497 if ((value = ep[i].me_value) == NULL)
498 continue;
499 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
500 _PyObject_GC_MAY_BE_TRACKED(ep[i].me_key))
501 return;
502 }
503 DECREASE_TRACK_COUNT
504 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000505}
506
507
Fred Drake1bff34a2000-08-31 19:31:38 +0000508/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000509Internal routine to insert a new item into the table.
510Used both by the internal resize routine and by the public insert routine.
511Eats a reference to key and one to value.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000512Returns -1 if an error occurred, or 0 on success.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000513*/
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000514static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000515insertdict(register PyDictObject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000516{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000517 PyObject *old_value;
518 register PyDictEntry *ep;
519 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000520
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000521 assert(mp->ma_lookup != NULL);
522 ep = mp->ma_lookup(mp, key, hash);
523 if (ep == NULL) {
524 Py_DECREF(key);
525 Py_DECREF(value);
526 return -1;
527 }
528 MAINTAIN_TRACKING(mp, key, value);
529 if (ep->me_value != NULL) {
530 old_value = ep->me_value;
531 ep->me_value = value;
532 Py_DECREF(old_value); /* which **CAN** re-enter */
533 Py_DECREF(key);
534 }
535 else {
536 if (ep->me_key == NULL)
537 mp->ma_fill++;
538 else {
539 assert(ep->me_key == dummy);
540 Py_DECREF(dummy);
541 }
542 ep->me_key = key;
543 ep->me_hash = (Py_ssize_t)hash;
544 ep->me_value = value;
545 mp->ma_used++;
546 }
547 return 0;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000548}
549
550/*
551Internal routine used by dictresize() to insert an item which is
552known to be absent from the dict. This routine also assumes that
553the dict contains no deleted entries. Besides the performance benefit,
554using insertdict() in dictresize() is dangerous (SF bug #1456209).
555Note that no refcounts are changed by this routine; if needed, the caller
556is responsible for incref'ing `key` and `value`.
557*/
558static void
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000559insertdict_clean(register PyDictObject *mp, PyObject *key, long hash,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000560 PyObject *value)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000561{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000562 register size_t i;
563 register size_t perturb;
564 register size_t mask = (size_t)mp->ma_mask;
565 PyDictEntry *ep0 = mp->ma_table;
566 register PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000567
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000568 MAINTAIN_TRACKING(mp, key, value);
569 i = hash & mask;
570 ep = &ep0[i];
571 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
572 i = (i << 2) + i + perturb + 1;
573 ep = &ep0[i & mask];
574 }
575 assert(ep->me_value == NULL);
576 mp->ma_fill++;
577 ep->me_key = key;
578 ep->me_hash = (Py_ssize_t)hash;
579 ep->me_value = value;
580 mp->ma_used++;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000581}
582
583/*
584Restructure the table by allocating a new table and reinserting all
585items again. When entries have been deleted, the new table may
586actually be smaller than the old one.
587*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000588static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000589dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000590{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000591 Py_ssize_t newsize;
592 PyDictEntry *oldtable, *newtable, *ep;
593 Py_ssize_t i;
594 int is_oldtable_malloced;
595 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000596
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000597 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000598
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000599 /* Find the smallest table size > minused. */
600 for (newsize = PyDict_MINSIZE;
601 newsize <= minused && newsize > 0;
602 newsize <<= 1)
603 ;
604 if (newsize <= 0) {
605 PyErr_NoMemory();
606 return -1;
607 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000608
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000609 /* Get space for a new table. */
610 oldtable = mp->ma_table;
611 assert(oldtable != NULL);
612 is_oldtable_malloced = oldtable != mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000613
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000614 if (newsize == PyDict_MINSIZE) {
615 /* A large table is shrinking, or we can't get any smaller. */
616 newtable = mp->ma_smalltable;
617 if (newtable == oldtable) {
618 if (mp->ma_fill == mp->ma_used) {
619 /* No dummies, so no point doing anything. */
620 return 0;
621 }
622 /* We're not going to resize it, but rebuild the
623 table anyway to purge old dummy entries.
624 Subtle: This is *necessary* if fill==size,
625 as lookdict needs at least one virgin slot to
626 terminate failing searches. If fill < size, it's
627 merely desirable, as dummies slow searches. */
628 assert(mp->ma_fill > mp->ma_used);
629 memcpy(small_copy, oldtable, sizeof(small_copy));
630 oldtable = small_copy;
631 }
632 }
633 else {
634 newtable = PyMem_NEW(PyDictEntry, newsize);
635 if (newtable == NULL) {
636 PyErr_NoMemory();
637 return -1;
638 }
639 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000640
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000641 /* Make the dict empty, using the new table. */
642 assert(newtable != oldtable);
643 mp->ma_table = newtable;
644 mp->ma_mask = newsize - 1;
645 memset(newtable, 0, sizeof(PyDictEntry) * newsize);
646 mp->ma_used = 0;
647 i = mp->ma_fill;
648 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000649
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000650 /* Copy the data over; this is refcount-neutral for active entries;
651 dummy entries aren't copied over, of course */
652 for (ep = oldtable; i > 0; ep++) {
653 if (ep->me_value != NULL) { /* active entry */
654 --i;
655 insertdict_clean(mp, ep->me_key, (long)ep->me_hash,
656 ep->me_value);
657 }
658 else if (ep->me_key != NULL) { /* dummy entry */
659 --i;
660 assert(ep->me_key == dummy);
661 Py_DECREF(ep->me_key);
662 }
663 /* else key == value == NULL: nothing to do */
664 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000665
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000666 if (is_oldtable_malloced)
667 PyMem_DEL(oldtable);
668 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000669}
670
Christian Heimes99170a52007-12-19 02:07:34 +0000671/* Create a new dictionary pre-sized to hold an estimated number of elements.
672 Underestimates are okay because the dictionary will resize as necessary.
673 Overestimates just mean the dictionary will be more sparse than usual.
674*/
675
676PyObject *
677_PyDict_NewPresized(Py_ssize_t minused)
678{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000679 PyObject *op = PyDict_New();
Christian Heimes99170a52007-12-19 02:07:34 +0000680
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000681 if (minused>5 && op != NULL && dictresize((PyDictObject *)op, minused) == -1) {
682 Py_DECREF(op);
683 return NULL;
684 }
685 return op;
Christian Heimes99170a52007-12-19 02:07:34 +0000686}
687
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000688/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
689 * that may occur (originally dicts supported only string keys, and exceptions
690 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000691 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000692 * (suppressed) occurred while computing the key's hash, or that some error
693 * (suppressed) occurred when comparing keys in the dict's internal probe
694 * sequence. A nasty example of the latter is when a Python-coded comparison
695 * function hits a stack-depth error, which can cause this to return NULL
696 * even if the key is present.
697 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000698PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000699PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000700{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000701 long hash;
702 PyDictObject *mp = (PyDictObject *)op;
703 PyDictEntry *ep;
704 PyThreadState *tstate;
705 if (!PyDict_Check(op))
706 return NULL;
707 if (!PyUnicode_CheckExact(key) ||
708 (hash = ((PyUnicodeObject *) key)->hash) == -1)
709 {
710 hash = PyObject_Hash(key);
711 if (hash == -1) {
712 PyErr_Clear();
713 return NULL;
714 }
715 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000716
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000717 /* We can arrive here with a NULL tstate during initialization: try
718 running "python -Wi" for an example related to string interning.
719 Let's just hope that no exception occurs then... This must be
720 _PyThreadState_Current and not PyThreadState_GET() because in debug
721 mode, the latter complains if tstate is NULL. */
722 tstate = _PyThreadState_Current;
723 if (tstate != NULL && tstate->curexc_type != NULL) {
724 /* preserve the existing exception */
725 PyObject *err_type, *err_value, *err_tb;
726 PyErr_Fetch(&err_type, &err_value, &err_tb);
727 ep = (mp->ma_lookup)(mp, key, hash);
728 /* ignore errors */
729 PyErr_Restore(err_type, err_value, err_tb);
730 if (ep == NULL)
731 return NULL;
732 }
733 else {
734 ep = (mp->ma_lookup)(mp, key, hash);
735 if (ep == NULL) {
736 PyErr_Clear();
737 return NULL;
738 }
739 }
740 return ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000741}
742
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000743/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
744 This returns NULL *with* an exception set if an exception occurred.
745 It returns NULL *without* an exception set if the key wasn't present.
746*/
747PyObject *
748PyDict_GetItemWithError(PyObject *op, PyObject *key)
749{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000750 long hash;
751 PyDictObject*mp = (PyDictObject *)op;
752 PyDictEntry *ep;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000753
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000754 if (!PyDict_Check(op)) {
755 PyErr_BadInternalCall();
756 return NULL;
757 }
758 if (!PyUnicode_CheckExact(key) ||
759 (hash = ((PyUnicodeObject *) key)->hash) == -1)
760 {
761 hash = PyObject_Hash(key);
762 if (hash == -1) {
763 return NULL;
764 }
765 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000766
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000767 ep = (mp->ma_lookup)(mp, key, hash);
768 if (ep == NULL)
769 return NULL;
770 return ep->me_value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000771}
772
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000773/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
Tim Peters60b29962006-01-01 01:19:23 +0000774 * dictionary if it's merely replacing the value for an existing key.
775 * This means that it's safe to loop over a dictionary with PyDict_Next()
776 * and occasionally replace a value -- but you can't insert new keys or
777 * remove them.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000778 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000779int
Tim Peters1f5871e2000-07-04 17:44:48 +0000780PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000781{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000782 register PyDictObject *mp;
783 register long hash;
784 register Py_ssize_t n_used;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000785
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000786 if (!PyDict_Check(op)) {
787 PyErr_BadInternalCall();
788 return -1;
789 }
790 assert(key);
791 assert(value);
792 mp = (PyDictObject *)op;
793 if (!PyUnicode_CheckExact(key) ||
794 (hash = ((PyUnicodeObject *) key)->hash) == -1)
795 {
796 hash = PyObject_Hash(key);
797 if (hash == -1)
798 return -1;
799 }
800 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
801 n_used = mp->ma_used;
802 Py_INCREF(value);
803 Py_INCREF(key);
804 if (insertdict(mp, key, hash, value) != 0)
805 return -1;
806 /* If we added a key, we can safely resize. Otherwise just return!
807 * If fill >= 2/3 size, adjust size. Normally, this doubles or
808 * quaduples the size, but it's also possible for the dict to shrink
809 * (if ma_fill is much larger than ma_used, meaning a lot of dict
810 * keys have been * deleted).
811 *
812 * Quadrupling the size improves average dictionary sparseness
813 * (reducing collisions) at the cost of some memory and iteration
814 * speed (which loops over every possible entry). It also halves
815 * the number of expensive resize operations in a growing dictionary.
816 *
817 * Very large dictionaries (over 50K items) use doubling instead.
818 * This may help applications with severe memory constraints.
819 */
820 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
821 return 0;
822 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000823}
824
825int
Tim Peters1f5871e2000-07-04 17:44:48 +0000826PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000827{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000828 register PyDictObject *mp;
829 register long hash;
830 register PyDictEntry *ep;
831 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000832
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000833 if (!PyDict_Check(op)) {
834 PyErr_BadInternalCall();
835 return -1;
836 }
837 assert(key);
838 if (!PyUnicode_CheckExact(key) ||
839 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
840 hash = PyObject_Hash(key);
841 if (hash == -1)
842 return -1;
843 }
844 mp = (PyDictObject *)op;
845 ep = (mp->ma_lookup)(mp, key, hash);
846 if (ep == NULL)
847 return -1;
848 if (ep->me_value == NULL) {
849 set_key_error(key);
850 return -1;
851 }
852 old_key = ep->me_key;
853 Py_INCREF(dummy);
854 ep->me_key = dummy;
855 old_value = ep->me_value;
856 ep->me_value = NULL;
857 mp->ma_used--;
858 Py_DECREF(old_value);
859 Py_DECREF(old_key);
860 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000861}
862
Guido van Rossum25831651993-05-19 14:50:45 +0000863void
Tim Peters1f5871e2000-07-04 17:44:48 +0000864PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000865{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000866 PyDictObject *mp;
867 PyDictEntry *ep, *table;
868 int table_is_malloced;
869 Py_ssize_t fill;
870 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000871#ifdef Py_DEBUG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000872 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +0000873#endif
874
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000875 if (!PyDict_Check(op))
876 return;
877 mp = (PyDictObject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000878#ifdef Py_DEBUG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000879 n = mp->ma_mask + 1;
880 i = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000881#endif
882
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000883 table = mp->ma_table;
884 assert(table != NULL);
885 table_is_malloced = table != mp->ma_smalltable;
Tim Petersdea48ec2001-05-22 20:40:22 +0000886
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000887 /* This is delicate. During the process of clearing the dict,
888 * decrefs can cause the dict to mutate. To avoid fatal confusion
889 * (voice of experience), we have to make the dict empty before
890 * clearing the slots, and never refer to anything via mp->xxx while
891 * clearing.
892 */
893 fill = mp->ma_fill;
894 if (table_is_malloced)
895 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000896
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000897 else if (fill > 0) {
898 /* It's a small table with something that needs to be cleared.
899 * Afraid the only safe way is to copy the dict entries into
900 * another small table first.
901 */
902 memcpy(small_copy, table, sizeof(small_copy));
903 table = small_copy;
904 EMPTY_TO_MINSIZE(mp);
905 }
906 /* else it's a small table that's already empty */
Tim Petersdea48ec2001-05-22 20:40:22 +0000907
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000908 /* Now we can finally clear things. If C had refcounts, we could
909 * assert that the refcount on table is 1 now, i.e. that this function
910 * has unique access to it, so decref side-effects can't alter it.
911 */
912 for (ep = table; fill > 0; ++ep) {
Tim Petersdea48ec2001-05-22 20:40:22 +0000913#ifdef Py_DEBUG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000914 assert(i < n);
915 ++i;
Tim Petersdea48ec2001-05-22 20:40:22 +0000916#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000917 if (ep->me_key) {
918 --fill;
919 Py_DECREF(ep->me_key);
920 Py_XDECREF(ep->me_value);
921 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000922#ifdef Py_DEBUG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000923 else
924 assert(ep->me_value == NULL);
Tim Petersdea48ec2001-05-22 20:40:22 +0000925#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000926 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000927
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000928 if (table_is_malloced)
929 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000930}
931
Tim Peters080c88b2003-02-15 03:01:11 +0000932/*
933 * Iterate over a dict. Use like so:
934 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000935 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +0000936 * PyObject *key, *value;
937 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +0000938 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +0000939 * Refer to borrowed references in key and value.
940 * }
941 *
942 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +0000943 * mutates the dict. One exception: it is safe if the loop merely changes
944 * the values associated with the keys (but doesn't insert new keys or
945 * delete keys), via PyDict_SetItem().
946 */
Guido van Rossum25831651993-05-19 14:50:45 +0000947int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000948PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000949{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000950 register Py_ssize_t i;
951 register Py_ssize_t mask;
952 register PyDictEntry *ep;
Raymond Hettinger43442782004-03-17 21:55:03 +0000953
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000954 if (!PyDict_Check(op))
955 return 0;
956 i = *ppos;
957 if (i < 0)
958 return 0;
959 ep = ((PyDictObject *)op)->ma_table;
960 mask = ((PyDictObject *)op)->ma_mask;
961 while (i <= mask && ep[i].me_value == NULL)
962 i++;
963 *ppos = i+1;
964 if (i > mask)
965 return 0;
966 if (pkey)
967 *pkey = ep[i].me_key;
968 if (pvalue)
969 *pvalue = ep[i].me_value;
970 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000971}
972
Thomas Wouterscf297e42007-02-23 15:07:44 +0000973/* Internal version of PyDict_Next that returns a hash value in addition to the key and value.*/
974int
975_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, long *phash)
976{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000977 register Py_ssize_t i;
978 register Py_ssize_t mask;
979 register PyDictEntry *ep;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000980
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000981 if (!PyDict_Check(op))
982 return 0;
983 i = *ppos;
984 if (i < 0)
985 return 0;
986 ep = ((PyDictObject *)op)->ma_table;
987 mask = ((PyDictObject *)op)->ma_mask;
988 while (i <= mask && ep[i].me_value == NULL)
989 i++;
990 *ppos = i+1;
991 if (i > mask)
992 return 0;
993 *phash = (long)(ep[i].me_hash);
994 if (pkey)
995 *pkey = ep[i].me_key;
996 if (pvalue)
997 *pvalue = ep[i].me_value;
998 return 1;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000999}
1000
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001001/* Methods */
1002
1003static void
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001004dict_dealloc(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001005{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001006 register PyDictEntry *ep;
1007 Py_ssize_t fill = mp->ma_fill;
1008 PyObject_GC_UnTrack(mp);
1009 Py_TRASHCAN_SAFE_BEGIN(mp)
1010 for (ep = mp->ma_table; fill > 0; ep++) {
1011 if (ep->me_key) {
1012 --fill;
1013 Py_DECREF(ep->me_key);
1014 Py_XDECREF(ep->me_value);
1015 }
1016 }
1017 if (mp->ma_table != mp->ma_smalltable)
1018 PyMem_DEL(mp->ma_table);
1019 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1020 free_list[numfree++] = mp;
1021 else
1022 Py_TYPE(mp)->tp_free((PyObject *)mp);
1023 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001024}
1025
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001026static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001027dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001028{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001029 Py_ssize_t i;
1030 PyObject *s, *temp, *colon = NULL;
1031 PyObject *pieces = NULL, *result = NULL;
1032 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +00001033
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001034 i = Py_ReprEnter((PyObject *)mp);
1035 if (i != 0) {
1036 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1037 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001038
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001039 if (mp->ma_used == 0) {
1040 result = PyUnicode_FromString("{}");
1041 goto Done;
1042 }
Tim Petersa7259592001-06-16 05:11:17 +00001043
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001044 pieces = PyList_New(0);
1045 if (pieces == NULL)
1046 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001047
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001048 colon = PyUnicode_FromString(": ");
1049 if (colon == NULL)
1050 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001051
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001052 /* Do repr() on each key+value pair, and insert ": " between them.
1053 Note that repr may mutate the dict. */
1054 i = 0;
1055 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
1056 int status;
1057 /* Prevent repr from deleting value during key format. */
1058 Py_INCREF(value);
1059 s = PyObject_Repr(key);
1060 PyUnicode_Append(&s, colon);
1061 PyUnicode_AppendAndDel(&s, PyObject_Repr(value));
1062 Py_DECREF(value);
1063 if (s == NULL)
1064 goto Done;
1065 status = PyList_Append(pieces, s);
1066 Py_DECREF(s); /* append created a new ref */
1067 if (status < 0)
1068 goto Done;
1069 }
Tim Petersa7259592001-06-16 05:11:17 +00001070
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001071 /* Add "{}" decorations to the first and last items. */
1072 assert(PyList_GET_SIZE(pieces) > 0);
1073 s = PyUnicode_FromString("{");
1074 if (s == NULL)
1075 goto Done;
1076 temp = PyList_GET_ITEM(pieces, 0);
1077 PyUnicode_AppendAndDel(&s, temp);
1078 PyList_SET_ITEM(pieces, 0, s);
1079 if (s == NULL)
1080 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001081
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001082 s = PyUnicode_FromString("}");
1083 if (s == NULL)
1084 goto Done;
1085 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
1086 PyUnicode_AppendAndDel(&temp, s);
1087 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
1088 if (temp == NULL)
1089 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001090
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001091 /* Paste them all together with ", " between. */
1092 s = PyUnicode_FromString(", ");
1093 if (s == NULL)
1094 goto Done;
1095 result = PyUnicode_Join(s, pieces);
1096 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +00001097
1098Done:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001099 Py_XDECREF(pieces);
1100 Py_XDECREF(colon);
1101 Py_ReprLeave((PyObject *)mp);
1102 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001103}
1104
Martin v. Löwis18e16552006-02-15 17:27:45 +00001105static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001106dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001107{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001108 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001109}
1110
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001111static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001112dict_subscript(PyDictObject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001113{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001114 PyObject *v;
1115 long hash;
1116 PyDictEntry *ep;
1117 assert(mp->ma_table != NULL);
1118 if (!PyUnicode_CheckExact(key) ||
1119 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
1120 hash = PyObject_Hash(key);
1121 if (hash == -1)
1122 return NULL;
1123 }
1124 ep = (mp->ma_lookup)(mp, key, hash);
1125 if (ep == NULL)
1126 return NULL;
1127 v = ep->me_value;
1128 if (v == NULL) {
1129 if (!PyDict_CheckExact(mp)) {
1130 /* Look up __missing__ method if we're a subclass. */
1131 PyObject *missing, *res;
1132 static PyObject *missing_str = NULL;
1133 missing = _PyObject_LookupSpecial((PyObject *)mp,
1134 "__missing__",
1135 &missing_str);
1136 if (missing != NULL) {
1137 res = PyObject_CallFunctionObjArgs(missing,
1138 key, NULL);
1139 Py_DECREF(missing);
1140 return res;
1141 }
1142 else if (PyErr_Occurred())
1143 return NULL;
1144 }
1145 set_key_error(key);
1146 return NULL;
1147 }
1148 else
1149 Py_INCREF(v);
1150 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001151}
1152
1153static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001154dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001155{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001156 if (w == NULL)
1157 return PyDict_DelItem((PyObject *)mp, v);
1158 else
1159 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001160}
1161
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001162static PyMappingMethods dict_as_mapping = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001163 (lenfunc)dict_length, /*mp_length*/
1164 (binaryfunc)dict_subscript, /*mp_subscript*/
1165 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001166};
1167
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001168static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001169dict_keys(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001170{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001171 register PyObject *v;
1172 register Py_ssize_t i, j;
1173 PyDictEntry *ep;
1174 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001175
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001176 again:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001177 n = mp->ma_used;
1178 v = PyList_New(n);
1179 if (v == NULL)
1180 return NULL;
1181 if (n != mp->ma_used) {
1182 /* Durnit. The allocations caused the dict to resize.
1183 * Just start over, this shouldn't normally happen.
1184 */
1185 Py_DECREF(v);
1186 goto again;
1187 }
1188 ep = mp->ma_table;
1189 mask = mp->ma_mask;
1190 for (i = 0, j = 0; i <= mask; i++) {
1191 if (ep[i].me_value != NULL) {
1192 PyObject *key = ep[i].me_key;
1193 Py_INCREF(key);
1194 PyList_SET_ITEM(v, j, key);
1195 j++;
1196 }
1197 }
1198 assert(j == n);
1199 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001200}
1201
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001202static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001203dict_values(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001204{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001205 register PyObject *v;
1206 register Py_ssize_t i, j;
1207 PyDictEntry *ep;
1208 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001209
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001210 again:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001211 n = mp->ma_used;
1212 v = PyList_New(n);
1213 if (v == NULL)
1214 return NULL;
1215 if (n != mp->ma_used) {
1216 /* Durnit. The allocations caused the dict to resize.
1217 * Just start over, this shouldn't normally happen.
1218 */
1219 Py_DECREF(v);
1220 goto again;
1221 }
1222 ep = mp->ma_table;
1223 mask = mp->ma_mask;
1224 for (i = 0, j = 0; i <= mask; i++) {
1225 if (ep[i].me_value != NULL) {
1226 PyObject *value = ep[i].me_value;
1227 Py_INCREF(value);
1228 PyList_SET_ITEM(v, j, value);
1229 j++;
1230 }
1231 }
1232 assert(j == n);
1233 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001234}
1235
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001236static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001237dict_items(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001238{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001239 register PyObject *v;
1240 register Py_ssize_t i, j, n;
1241 Py_ssize_t mask;
1242 PyObject *item, *key, *value;
1243 PyDictEntry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001244
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001245 /* Preallocate the list of tuples, to avoid allocations during
1246 * the loop over the items, which could trigger GC, which
1247 * could resize the dict. :-(
1248 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001249 again:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001250 n = mp->ma_used;
1251 v = PyList_New(n);
1252 if (v == NULL)
1253 return NULL;
1254 for (i = 0; i < n; i++) {
1255 item = PyTuple_New(2);
1256 if (item == NULL) {
1257 Py_DECREF(v);
1258 return NULL;
1259 }
1260 PyList_SET_ITEM(v, i, item);
1261 }
1262 if (n != mp->ma_used) {
1263 /* Durnit. The allocations caused the dict to resize.
1264 * Just start over, this shouldn't normally happen.
1265 */
1266 Py_DECREF(v);
1267 goto again;
1268 }
1269 /* Nothing we do below makes any function calls. */
1270 ep = mp->ma_table;
1271 mask = mp->ma_mask;
1272 for (i = 0, j = 0; i <= mask; i++) {
1273 if ((value=ep[i].me_value) != NULL) {
1274 key = ep[i].me_key;
1275 item = PyList_GET_ITEM(v, j);
1276 Py_INCREF(key);
1277 PyTuple_SET_ITEM(item, 0, key);
1278 Py_INCREF(value);
1279 PyTuple_SET_ITEM(item, 1, value);
1280 j++;
1281 }
1282 }
1283 assert(j == n);
1284 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001285}
1286
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001287static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001288dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001289{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001290 PyObject *seq;
1291 PyObject *value = Py_None;
1292 PyObject *it; /* iter(seq) */
1293 PyObject *key;
1294 PyObject *d;
1295 int status;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001296
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001297 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
1298 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001299
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001300 d = PyObject_CallObject(cls, NULL);
1301 if (d == NULL)
1302 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001303
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001304 if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) {
1305 PyDictObject *mp = (PyDictObject *)d;
1306 PyObject *oldvalue;
1307 Py_ssize_t pos = 0;
1308 PyObject *key;
1309 long hash;
Guido van Rossum58da9312007-11-10 23:39:45 +00001310
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001311 if (dictresize(mp, Py_SIZE(seq)))
1312 return NULL;
Guido van Rossum58da9312007-11-10 23:39:45 +00001313
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001314 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
1315 Py_INCREF(key);
1316 Py_INCREF(value);
1317 if (insertdict(mp, key, hash, value))
1318 return NULL;
1319 }
1320 return d;
1321 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001322
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001323 if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
1324 PyDictObject *mp = (PyDictObject *)d;
1325 Py_ssize_t pos = 0;
1326 PyObject *key;
1327 long hash;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001328
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001329 if (dictresize(mp, PySet_GET_SIZE(seq)))
1330 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001331
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001332 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
1333 Py_INCREF(key);
1334 Py_INCREF(value);
1335 if (insertdict(mp, key, hash, value))
1336 return NULL;
1337 }
1338 return d;
1339 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001340
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001341 it = PyObject_GetIter(seq);
1342 if (it == NULL){
1343 Py_DECREF(d);
1344 return NULL;
1345 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001346
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001347 if (PyDict_CheckExact(d)) {
1348 while ((key = PyIter_Next(it)) != NULL) {
1349 status = PyDict_SetItem(d, key, value);
1350 Py_DECREF(key);
1351 if (status < 0)
1352 goto Fail;
1353 }
1354 } else {
1355 while ((key = PyIter_Next(it)) != NULL) {
1356 status = PyObject_SetItem(d, key, value);
1357 Py_DECREF(key);
1358 if (status < 0)
1359 goto Fail;
1360 }
1361 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001362
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001363 if (PyErr_Occurred())
1364 goto Fail;
1365 Py_DECREF(it);
1366 return d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001367
1368Fail:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001369 Py_DECREF(it);
1370 Py_DECREF(d);
1371 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001372}
1373
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001374static int
1375dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001376{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001377 PyObject *arg = NULL;
1378 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001379
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001380 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1381 result = -1;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001382
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001383 else if (arg != NULL) {
1384 if (PyObject_HasAttrString(arg, "keys"))
1385 result = PyDict_Merge(self, arg, 1);
1386 else
1387 result = PyDict_MergeFromSeq2(self, arg, 1);
1388 }
1389 if (result == 0 && kwds != NULL)
1390 result = PyDict_Merge(self, kwds, 1);
1391 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001392}
1393
1394static PyObject *
1395dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1396{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001397 if (dict_update_common(self, args, kwds, "update") != -1)
1398 Py_RETURN_NONE;
1399 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001400}
1401
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001402/* Update unconditionally replaces existing items.
1403 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001404 otherwise it leaves existing items unchanged.
1405
1406 PyDict_{Update,Merge} update/merge from a mapping object.
1407
Tim Petersf582b822001-12-11 18:51:08 +00001408 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001409 producing iterable objects of length 2.
1410*/
1411
Tim Petersf582b822001-12-11 18:51:08 +00001412int
Tim Peters1fc240e2001-10-26 05:06:50 +00001413PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1414{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001415 PyObject *it; /* iter(seq2) */
1416 Py_ssize_t i; /* index into seq2 of current element */
1417 PyObject *item; /* seq2[i] */
1418 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00001419
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001420 assert(d != NULL);
1421 assert(PyDict_Check(d));
1422 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00001423
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001424 it = PyObject_GetIter(seq2);
1425 if (it == NULL)
1426 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001427
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001428 for (i = 0; ; ++i) {
1429 PyObject *key, *value;
1430 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001431
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001432 fast = NULL;
1433 item = PyIter_Next(it);
1434 if (item == NULL) {
1435 if (PyErr_Occurred())
1436 goto Fail;
1437 break;
1438 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001439
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001440 /* Convert item to sequence, and verify length 2. */
1441 fast = PySequence_Fast(item, "");
1442 if (fast == NULL) {
1443 if (PyErr_ExceptionMatches(PyExc_TypeError))
1444 PyErr_Format(PyExc_TypeError,
1445 "cannot convert dictionary update "
1446 "sequence element #%zd to a sequence",
1447 i);
1448 goto Fail;
1449 }
1450 n = PySequence_Fast_GET_SIZE(fast);
1451 if (n != 2) {
1452 PyErr_Format(PyExc_ValueError,
1453 "dictionary update sequence element #%zd "
1454 "has length %zd; 2 is required",
1455 i, n);
1456 goto Fail;
1457 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001458
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001459 /* Update/merge with this (key, value) pair. */
1460 key = PySequence_Fast_GET_ITEM(fast, 0);
1461 value = PySequence_Fast_GET_ITEM(fast, 1);
1462 if (override || PyDict_GetItem(d, key) == NULL) {
1463 int status = PyDict_SetItem(d, key, value);
1464 if (status < 0)
1465 goto Fail;
1466 }
1467 Py_DECREF(fast);
1468 Py_DECREF(item);
1469 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001470
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001471 i = 0;
1472 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00001473Fail:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001474 Py_XDECREF(item);
1475 Py_XDECREF(fast);
1476 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001477Return:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001478 Py_DECREF(it);
1479 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001480}
1481
Tim Peters6d6c1a32001-08-02 04:15:00 +00001482int
1483PyDict_Update(PyObject *a, PyObject *b)
1484{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001485 return PyDict_Merge(a, b, 1);
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001486}
1487
1488int
1489PyDict_Merge(PyObject *a, PyObject *b, int override)
1490{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001491 register PyDictObject *mp, *other;
1492 register Py_ssize_t i;
1493 PyDictEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001494
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001495 /* We accept for the argument either a concrete dictionary object,
1496 * or an abstract "mapping" object. For the former, we can do
1497 * things quite efficiently. For the latter, we only require that
1498 * PyMapping_Keys() and PyObject_GetItem() be supported.
1499 */
1500 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1501 PyErr_BadInternalCall();
1502 return -1;
1503 }
1504 mp = (PyDictObject*)a;
1505 if (PyDict_Check(b)) {
1506 other = (PyDictObject*)b;
1507 if (other == mp || other->ma_used == 0)
1508 /* a.update(a) or a.update({}); nothing to do */
1509 return 0;
1510 if (mp->ma_used == 0)
1511 /* Since the target dict is empty, PyDict_GetItem()
1512 * always returns NULL. Setting override to 1
1513 * skips the unnecessary test.
1514 */
1515 override = 1;
1516 /* Do one big resize at the start, rather than
1517 * incrementally resizing as we insert new items. Expect
1518 * that there will be no (or few) overlapping keys.
1519 */
1520 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
1521 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
1522 return -1;
1523 }
1524 for (i = 0; i <= other->ma_mask; i++) {
1525 entry = &other->ma_table[i];
1526 if (entry->me_value != NULL &&
1527 (override ||
1528 PyDict_GetItem(a, entry->me_key) == NULL)) {
1529 Py_INCREF(entry->me_key);
1530 Py_INCREF(entry->me_value);
1531 if (insertdict(mp, entry->me_key,
1532 (long)entry->me_hash,
1533 entry->me_value) != 0)
1534 return -1;
1535 }
1536 }
1537 }
1538 else {
1539 /* Do it the generic, slower way */
1540 PyObject *keys = PyMapping_Keys(b);
1541 PyObject *iter;
1542 PyObject *key, *value;
1543 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001544
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001545 if (keys == NULL)
1546 /* Docstring says this is equivalent to E.keys() so
1547 * if E doesn't have a .keys() method we want
1548 * AttributeError to percolate up. Might as well
1549 * do the same for any other error.
1550 */
1551 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001552
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001553 iter = PyObject_GetIter(keys);
1554 Py_DECREF(keys);
1555 if (iter == NULL)
1556 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001557
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001558 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
1559 if (!override && PyDict_GetItem(a, key) != NULL) {
1560 Py_DECREF(key);
1561 continue;
1562 }
1563 value = PyObject_GetItem(b, key);
1564 if (value == NULL) {
1565 Py_DECREF(iter);
1566 Py_DECREF(key);
1567 return -1;
1568 }
1569 status = PyDict_SetItem(a, key, value);
1570 Py_DECREF(key);
1571 Py_DECREF(value);
1572 if (status < 0) {
1573 Py_DECREF(iter);
1574 return -1;
1575 }
1576 }
1577 Py_DECREF(iter);
1578 if (PyErr_Occurred())
1579 /* Iterator completed, via error */
1580 return -1;
1581 }
1582 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001583}
1584
1585static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001586dict_copy(register PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001587{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001588 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001589}
1590
1591PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001592PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001593{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001594 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001595
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001596 if (o == NULL || !PyDict_Check(o)) {
1597 PyErr_BadInternalCall();
1598 return NULL;
1599 }
1600 copy = PyDict_New();
1601 if (copy == NULL)
1602 return NULL;
1603 if (PyDict_Merge(copy, o, 1) == 0)
1604 return copy;
1605 Py_DECREF(copy);
1606 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001607}
1608
Martin v. Löwis18e16552006-02-15 17:27:45 +00001609Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001610PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001611{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001612 if (mp == NULL || !PyDict_Check(mp)) {
1613 PyErr_BadInternalCall();
1614 return -1;
1615 }
1616 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001617}
1618
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001619PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001620PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001621{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001622 if (mp == NULL || !PyDict_Check(mp)) {
1623 PyErr_BadInternalCall();
1624 return NULL;
1625 }
1626 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001627}
1628
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001629PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001630PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001631{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001632 if (mp == NULL || !PyDict_Check(mp)) {
1633 PyErr_BadInternalCall();
1634 return NULL;
1635 }
1636 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001637}
1638
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001639PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001640PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001641{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001642 if (mp == NULL || !PyDict_Check(mp)) {
1643 PyErr_BadInternalCall();
1644 return NULL;
1645 }
1646 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001647}
1648
Tim Peterse63415e2001-05-08 04:38:29 +00001649/* Return 1 if dicts equal, 0 if not, -1 if error.
1650 * Gets out as soon as any difference is detected.
1651 * Uses only Py_EQ comparison.
1652 */
1653static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001654dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00001655{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001656 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00001657
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001658 if (a->ma_used != b->ma_used)
1659 /* can't be equal if # of entries differ */
1660 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001661
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001662 /* Same # of entries -- check all of 'em. Exit early on any diff. */
1663 for (i = 0; i <= a->ma_mask; i++) {
1664 PyObject *aval = a->ma_table[i].me_value;
1665 if (aval != NULL) {
1666 int cmp;
1667 PyObject *bval;
1668 PyObject *key = a->ma_table[i].me_key;
1669 /* temporarily bump aval's refcount to ensure it stays
1670 alive until we're done with it */
1671 Py_INCREF(aval);
1672 /* ditto for key */
1673 Py_INCREF(key);
1674 bval = PyDict_GetItemWithError((PyObject *)b, key);
1675 Py_DECREF(key);
1676 if (bval == NULL) {
1677 Py_DECREF(aval);
1678 if (PyErr_Occurred())
1679 return -1;
1680 return 0;
1681 }
1682 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1683 Py_DECREF(aval);
1684 if (cmp <= 0) /* error or not equal */
1685 return cmp;
1686 }
1687 }
1688 return 1;
Tim Peterse63415e2001-05-08 04:38:29 +00001689 }
1690
1691static PyObject *
1692dict_richcompare(PyObject *v, PyObject *w, int op)
1693{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001694 int cmp;
1695 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00001696
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001697 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1698 res = Py_NotImplemented;
1699 }
1700 else if (op == Py_EQ || op == Py_NE) {
1701 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
1702 if (cmp < 0)
1703 return NULL;
1704 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1705 }
1706 else
1707 res = Py_NotImplemented;
1708 Py_INCREF(res);
1709 return res;
Tim Peterse63415e2001-05-08 04:38:29 +00001710 }
1711
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001712static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001713dict_contains(register PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001714{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001715 long hash;
1716 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001717
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001718 if (!PyUnicode_CheckExact(key) ||
1719 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
1720 hash = PyObject_Hash(key);
1721 if (hash == -1)
1722 return NULL;
1723 }
1724 ep = (mp->ma_lookup)(mp, key, hash);
1725 if (ep == NULL)
1726 return NULL;
1727 return PyBool_FromLong(ep->me_value != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001728}
1729
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001730static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001731dict_get(register PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001732{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001733 PyObject *key;
1734 PyObject *failobj = Py_None;
1735 PyObject *val = NULL;
1736 long hash;
1737 PyDictEntry *ep;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001738
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001739 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
1740 return NULL;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001741
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001742 if (!PyUnicode_CheckExact(key) ||
1743 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
1744 hash = PyObject_Hash(key);
1745 if (hash == -1)
1746 return NULL;
1747 }
1748 ep = (mp->ma_lookup)(mp, key, hash);
1749 if (ep == NULL)
1750 return NULL;
1751 val = ep->me_value;
1752 if (val == NULL)
1753 val = failobj;
1754 Py_INCREF(val);
1755 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001756}
1757
1758
1759static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001760dict_setdefault(register PyDictObject *mp, PyObject *args)
Guido van Rossum164452c2000-08-08 16:12:54 +00001761{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001762 PyObject *key;
1763 PyObject *failobj = Py_None;
1764 PyObject *val = NULL;
1765 long hash;
1766 PyDictEntry *ep;
Guido van Rossum164452c2000-08-08 16:12:54 +00001767
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001768 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
1769 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001770
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001771 if (!PyUnicode_CheckExact(key) ||
1772 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
1773 hash = PyObject_Hash(key);
1774 if (hash == -1)
1775 return NULL;
1776 }
1777 ep = (mp->ma_lookup)(mp, key, hash);
1778 if (ep == NULL)
1779 return NULL;
1780 val = ep->me_value;
1781 if (val == NULL) {
1782 val = failobj;
1783 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1784 val = NULL;
1785 }
1786 Py_XINCREF(val);
1787 return val;
Guido van Rossum164452c2000-08-08 16:12:54 +00001788}
1789
1790
1791static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001792dict_clear(register PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001793{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001794 PyDict_Clear((PyObject *)mp);
1795 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001796}
1797
Guido van Rossumba6ab842000-12-12 22:02:18 +00001798static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001799dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00001800{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001801 long hash;
1802 PyDictEntry *ep;
1803 PyObject *old_value, *old_key;
1804 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001805
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001806 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1807 return NULL;
1808 if (mp->ma_used == 0) {
1809 if (deflt) {
1810 Py_INCREF(deflt);
1811 return deflt;
1812 }
Raymond Hettinger7529afc2010-10-30 08:14:53 +00001813 set_key_error(key);
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001814 return NULL;
1815 }
1816 if (!PyUnicode_CheckExact(key) ||
1817 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
1818 hash = PyObject_Hash(key);
1819 if (hash == -1)
1820 return NULL;
1821 }
1822 ep = (mp->ma_lookup)(mp, key, hash);
1823 if (ep == NULL)
1824 return NULL;
1825 if (ep->me_value == NULL) {
1826 if (deflt) {
1827 Py_INCREF(deflt);
1828 return deflt;
1829 }
1830 set_key_error(key);
1831 return NULL;
1832 }
1833 old_key = ep->me_key;
1834 Py_INCREF(dummy);
1835 ep->me_key = dummy;
1836 old_value = ep->me_value;
1837 ep->me_value = NULL;
1838 mp->ma_used--;
1839 Py_DECREF(old_key);
1840 return old_value;
Guido van Rossume027d982002-04-12 15:11:59 +00001841}
1842
1843static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001844dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001845{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001846 Py_ssize_t i = 0;
1847 PyDictEntry *ep;
1848 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001849
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001850 /* Allocate the result tuple before checking the size. Believe it
1851 * or not, this allocation could trigger a garbage collection which
1852 * could empty the dict, so if we checked the size first and that
1853 * happened, the result would be an infinite loop (searching for an
1854 * entry that no longer exists). Note that the usual popitem()
1855 * idiom is "while d: k, v = d.popitem()". so needing to throw the
1856 * tuple away if the dict *is* empty isn't a significant
1857 * inefficiency -- possible, but unlikely in practice.
1858 */
1859 res = PyTuple_New(2);
1860 if (res == NULL)
1861 return NULL;
1862 if (mp->ma_used == 0) {
1863 Py_DECREF(res);
1864 PyErr_SetString(PyExc_KeyError,
1865 "popitem(): dictionary is empty");
1866 return NULL;
1867 }
1868 /* Set ep to "the first" dict entry with a value. We abuse the hash
1869 * field of slot 0 to hold a search finger:
1870 * If slot 0 has a value, use slot 0.
1871 * Else slot 0 is being used to hold a search finger,
1872 * and we use its hash value as the first index to look.
1873 */
1874 ep = &mp->ma_table[0];
1875 if (ep->me_value == NULL) {
1876 i = ep->me_hash;
1877 /* The hash field may be a real hash value, or it may be a
1878 * legit search finger, or it may be a once-legit search
1879 * finger that's out of bounds now because it wrapped around
1880 * or the table shrunk -- simply make sure it's in bounds now.
1881 */
1882 if (i > mp->ma_mask || i < 1)
1883 i = 1; /* skip slot 0 */
1884 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1885 i++;
1886 if (i > mp->ma_mask)
1887 i = 1;
1888 }
1889 }
1890 PyTuple_SET_ITEM(res, 0, ep->me_key);
1891 PyTuple_SET_ITEM(res, 1, ep->me_value);
1892 Py_INCREF(dummy);
1893 ep->me_key = dummy;
1894 ep->me_value = NULL;
1895 mp->ma_used--;
1896 assert(mp->ma_table[0].me_value == NULL);
1897 mp->ma_table[0].me_hash = i + 1; /* next place to start */
1898 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001899}
1900
Jeremy Hylton8caad492000-06-23 14:18:11 +00001901static int
1902dict_traverse(PyObject *op, visitproc visit, void *arg)
1903{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001904 Py_ssize_t i = 0;
1905 PyObject *pk;
1906 PyObject *pv;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001907
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001908 while (PyDict_Next(op, &i, &pk, &pv)) {
1909 Py_VISIT(pk);
1910 Py_VISIT(pv);
1911 }
1912 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001913}
1914
1915static int
1916dict_tp_clear(PyObject *op)
1917{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001918 PyDict_Clear(op);
1919 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001920}
1921
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001922static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001923
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001924static PyObject *
1925dict_sizeof(PyDictObject *mp)
1926{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001927 Py_ssize_t res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001928
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001929 res = sizeof(PyDictObject);
1930 if (mp->ma_table != mp->ma_smalltable)
1931 res = res + (mp->ma_mask + 1) * sizeof(PyDictEntry);
1932 return PyLong_FromSsize_t(res);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001933}
1934
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001935PyDoc_STRVAR(contains__doc__,
1936"D.__contains__(k) -> True if D has a key k, else False");
1937
1938PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
1939
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001940PyDoc_STRVAR(sizeof__doc__,
1941"D.__sizeof__() -> size of D in memory, in bytes");
1942
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001943PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001944"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001945
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001946PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001947"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 +00001948
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001949PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001950"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001951If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00001952
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001953PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00001954"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000019552-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001956
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001957PyDoc_STRVAR(update__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001958"D.update(E, **F) -> None. Update D from dict/iterable E and F.\n"
1959"If E has a .keys() method, does: for k in E: D[k] = E[k]\n\
1960If E lacks .keys() method, does: for (k, v) in E: D[k] = v\n\
1961In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00001962
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001963PyDoc_STRVAR(fromkeys__doc__,
1964"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
1965v defaults to None.");
1966
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001967PyDoc_STRVAR(clear__doc__,
1968"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001969
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001970PyDoc_STRVAR(copy__doc__,
1971"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00001972
Guido van Rossumb90c8482007-02-10 01:11:45 +00001973/* Forward */
1974static PyObject *dictkeys_new(PyObject *);
1975static PyObject *dictitems_new(PyObject *);
1976static PyObject *dictvalues_new(PyObject *);
1977
Guido van Rossum45c85d12007-07-27 16:31:40 +00001978PyDoc_STRVAR(keys__doc__,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001979 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00001980PyDoc_STRVAR(items__doc__,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001981 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00001982PyDoc_STRVAR(values__doc__,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001983 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00001984
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001985static PyMethodDef mapp_methods[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001986 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
1987 contains__doc__},
1988 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
1989 getitem__doc__},
1990 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
1991 sizeof__doc__},
1992 {"get", (PyCFunction)dict_get, METH_VARARGS,
1993 get__doc__},
1994 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1995 setdefault_doc__},
1996 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
1997 pop__doc__},
1998 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
1999 popitem__doc__},
2000 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
2001 keys__doc__},
2002 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
2003 items__doc__},
2004 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
2005 values__doc__},
2006 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
2007 update__doc__},
2008 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
2009 fromkeys__doc__},
2010 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
2011 clear__doc__},
2012 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
2013 copy__doc__},
2014 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002015};
2016
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002017/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002018int
2019PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002020{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002021 long hash;
2022 PyDictObject *mp = (PyDictObject *)op;
2023 PyDictEntry *ep;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002024
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002025 if (!PyUnicode_CheckExact(key) ||
2026 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
2027 hash = PyObject_Hash(key);
2028 if (hash == -1)
2029 return -1;
2030 }
2031 ep = (mp->ma_lookup)(mp, key, hash);
2032 return ep == NULL ? -1 : (ep->me_value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002033}
2034
Thomas Wouterscf297e42007-02-23 15:07:44 +00002035/* Internal version of PyDict_Contains used when the hash value is already known */
2036int
2037_PyDict_Contains(PyObject *op, PyObject *key, long hash)
2038{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002039 PyDictObject *mp = (PyDictObject *)op;
2040 PyDictEntry *ep;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002041
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002042 ep = (mp->ma_lookup)(mp, key, hash);
2043 return ep == NULL ? -1 : (ep->me_value != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00002044}
2045
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002046/* Hack to implement "key in dict" */
2047static PySequenceMethods dict_as_sequence = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002048 0, /* sq_length */
2049 0, /* sq_concat */
2050 0, /* sq_repeat */
2051 0, /* sq_item */
2052 0, /* sq_slice */
2053 0, /* sq_ass_item */
2054 0, /* sq_ass_slice */
2055 PyDict_Contains, /* sq_contains */
2056 0, /* sq_inplace_concat */
2057 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002058};
2059
Guido van Rossum09e563a2001-05-01 12:10:21 +00002060static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002061dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2062{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002063 PyObject *self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002064
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002065 assert(type != NULL && type->tp_alloc != NULL);
2066 self = type->tp_alloc(type, 0);
2067 if (self != NULL) {
2068 PyDictObject *d = (PyDictObject *)self;
2069 /* It's guaranteed that tp->alloc zeroed out the struct. */
2070 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
2071 INIT_NONZERO_DICT_SLOTS(d);
2072 d->ma_lookup = lookdict_unicode;
2073 /* The object has been implicitely tracked by tp_alloc */
2074 if (type == &PyDict_Type)
2075 _PyObject_GC_UNTRACK(d);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002076#ifdef SHOW_CONVERSION_COUNTS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002077 ++created;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002078#endif
Antoine Pitrou3a652b12009-03-23 18:52:06 +00002079#ifdef SHOW_TRACK_COUNT
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002080 if (_PyObject_GC_IS_TRACKED(d))
2081 count_tracked++;
2082 else
2083 count_untracked++;
Antoine Pitrou3a652b12009-03-23 18:52:06 +00002084#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002085 }
2086 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002087}
2088
Tim Peters25786c02001-09-02 08:22:48 +00002089static int
2090dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2091{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002092 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002093}
2094
Tim Peters6d6c1a32001-08-02 04:15:00 +00002095static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002096dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002097{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002098 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002099}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002100
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002101PyDoc_STRVAR(dictionary_doc,
Ezio Melotti807e98e2010-03-01 04:10:55 +00002102"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002103"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti807e98e2010-03-01 04:10:55 +00002104" (key, value) pairs\n"
2105"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002106" d = {}\n"
Ezio Melotti807e98e2010-03-01 04:10:55 +00002107" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002108" d[k] = v\n"
2109"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2110" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002111
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002112PyTypeObject PyDict_Type = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002113 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2114 "dict",
2115 sizeof(PyDictObject),
2116 0,
2117 (destructor)dict_dealloc, /* tp_dealloc */
2118 0, /* tp_print */
2119 0, /* tp_getattr */
2120 0, /* tp_setattr */
2121 0, /* tp_reserved */
2122 (reprfunc)dict_repr, /* tp_repr */
2123 0, /* tp_as_number */
2124 &dict_as_sequence, /* tp_as_sequence */
2125 &dict_as_mapping, /* tp_as_mapping */
2126 (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
2127 0, /* tp_call */
2128 0, /* tp_str */
2129 PyObject_GenericGetAttr, /* tp_getattro */
2130 0, /* tp_setattro */
2131 0, /* tp_as_buffer */
2132 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2133 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
2134 dictionary_doc, /* tp_doc */
2135 dict_traverse, /* tp_traverse */
2136 dict_tp_clear, /* tp_clear */
2137 dict_richcompare, /* tp_richcompare */
2138 0, /* tp_weaklistoffset */
2139 (getiterfunc)dict_iter, /* tp_iter */
2140 0, /* tp_iternext */
2141 mapp_methods, /* tp_methods */
2142 0, /* tp_members */
2143 0, /* tp_getset */
2144 0, /* tp_base */
2145 0, /* tp_dict */
2146 0, /* tp_descr_get */
2147 0, /* tp_descr_set */
2148 0, /* tp_dictoffset */
2149 dict_init, /* tp_init */
2150 PyType_GenericAlloc, /* tp_alloc */
2151 dict_new, /* tp_new */
2152 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002153};
2154
Guido van Rossum3cca2451997-05-16 14:23:33 +00002155/* For backward compatibility with old dictionary interface */
2156
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002157PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002158PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002159{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002160 PyObject *kv, *rv;
2161 kv = PyUnicode_FromString(key);
2162 if (kv == NULL)
2163 return NULL;
2164 rv = PyDict_GetItem(v, kv);
2165 Py_DECREF(kv);
2166 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002167}
2168
2169int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002170PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002171{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002172 PyObject *kv;
2173 int err;
2174 kv = PyUnicode_FromString(key);
2175 if (kv == NULL)
2176 return -1;
2177 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
2178 err = PyDict_SetItem(v, kv, item);
2179 Py_DECREF(kv);
2180 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002181}
2182
2183int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002184PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002185{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002186 PyObject *kv;
2187 int err;
2188 kv = PyUnicode_FromString(key);
2189 if (kv == NULL)
2190 return -1;
2191 err = PyDict_DelItem(v, kv);
2192 Py_DECREF(kv);
2193 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002194}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002195
Raymond Hettinger019a1482004-03-18 02:41:19 +00002196/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002197
2198typedef struct {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002199 PyObject_HEAD
2200 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
2201 Py_ssize_t di_used;
2202 Py_ssize_t di_pos;
2203 PyObject* di_result; /* reusable result tuple for iteritems */
2204 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002205} dictiterobject;
2206
2207static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002208dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002209{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002210 dictiterobject *di;
2211 di = PyObject_GC_New(dictiterobject, itertype);
2212 if (di == NULL)
2213 return NULL;
2214 Py_INCREF(dict);
2215 di->di_dict = dict;
2216 di->di_used = dict->ma_used;
2217 di->di_pos = 0;
2218 di->len = dict->ma_used;
2219 if (itertype == &PyDictIterItem_Type) {
2220 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2221 if (di->di_result == NULL) {
2222 Py_DECREF(di);
2223 return NULL;
2224 }
2225 }
2226 else
2227 di->di_result = NULL;
2228 _PyObject_GC_TRACK(di);
2229 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002230}
2231
2232static void
2233dictiter_dealloc(dictiterobject *di)
2234{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002235 Py_XDECREF(di->di_dict);
2236 Py_XDECREF(di->di_result);
2237 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002238}
2239
2240static int
2241dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
2242{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002243 Py_VISIT(di->di_dict);
2244 Py_VISIT(di->di_result);
2245 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002246}
2247
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002248static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002249dictiter_len(dictiterobject *di)
2250{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002251 Py_ssize_t len = 0;
2252 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
2253 len = di->len;
2254 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002255}
2256
Guido van Rossumb90c8482007-02-10 01:11:45 +00002257PyDoc_STRVAR(length_hint_doc,
2258 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002259
2260static PyMethodDef dictiter_methods[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002261 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
2262 length_hint_doc},
2263 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002264};
2265
Raymond Hettinger019a1482004-03-18 02:41:19 +00002266static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002267{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002268 PyObject *key;
2269 register Py_ssize_t i, mask;
2270 register PyDictEntry *ep;
2271 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002272
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002273 if (d == NULL)
2274 return NULL;
2275 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002276
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002277 if (di->di_used != d->ma_used) {
2278 PyErr_SetString(PyExc_RuntimeError,
2279 "dictionary changed size during iteration");
2280 di->di_used = -1; /* Make this state sticky */
2281 return NULL;
2282 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002283
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002284 i = di->di_pos;
2285 if (i < 0)
2286 goto fail;
2287 ep = d->ma_table;
2288 mask = d->ma_mask;
2289 while (i <= mask && ep[i].me_value == NULL)
2290 i++;
2291 di->di_pos = i+1;
2292 if (i > mask)
2293 goto fail;
2294 di->len--;
2295 key = ep[i].me_key;
2296 Py_INCREF(key);
2297 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002298
2299fail:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002300 Py_DECREF(d);
2301 di->di_dict = NULL;
2302 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002303}
2304
Raymond Hettinger019a1482004-03-18 02:41:19 +00002305PyTypeObject PyDictIterKey_Type = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002306 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2307 "dict_keyiterator", /* tp_name */
2308 sizeof(dictiterobject), /* tp_basicsize */
2309 0, /* tp_itemsize */
2310 /* methods */
2311 (destructor)dictiter_dealloc, /* tp_dealloc */
2312 0, /* tp_print */
2313 0, /* tp_getattr */
2314 0, /* tp_setattr */
2315 0, /* tp_reserved */
2316 0, /* tp_repr */
2317 0, /* tp_as_number */
2318 0, /* tp_as_sequence */
2319 0, /* tp_as_mapping */
2320 0, /* tp_hash */
2321 0, /* tp_call */
2322 0, /* tp_str */
2323 PyObject_GenericGetAttr, /* tp_getattro */
2324 0, /* tp_setattro */
2325 0, /* tp_as_buffer */
2326 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2327 0, /* tp_doc */
2328 (traverseproc)dictiter_traverse, /* tp_traverse */
2329 0, /* tp_clear */
2330 0, /* tp_richcompare */
2331 0, /* tp_weaklistoffset */
2332 PyObject_SelfIter, /* tp_iter */
2333 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
2334 dictiter_methods, /* tp_methods */
2335 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002336};
2337
2338static PyObject *dictiter_iternextvalue(dictiterobject *di)
2339{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002340 PyObject *value;
2341 register Py_ssize_t i, mask;
2342 register PyDictEntry *ep;
2343 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002344
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002345 if (d == NULL)
2346 return NULL;
2347 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002348
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002349 if (di->di_used != d->ma_used) {
2350 PyErr_SetString(PyExc_RuntimeError,
2351 "dictionary changed size during iteration");
2352 di->di_used = -1; /* Make this state sticky */
2353 return NULL;
2354 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002355
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002356 i = di->di_pos;
2357 mask = d->ma_mask;
2358 if (i < 0 || i > mask)
2359 goto fail;
2360 ep = d->ma_table;
2361 while ((value=ep[i].me_value) == NULL) {
2362 i++;
2363 if (i > mask)
2364 goto fail;
2365 }
2366 di->di_pos = i+1;
2367 di->len--;
2368 Py_INCREF(value);
2369 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002370
2371fail:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002372 Py_DECREF(d);
2373 di->di_dict = NULL;
2374 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002375}
2376
2377PyTypeObject PyDictIterValue_Type = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002378 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2379 "dict_valueiterator", /* tp_name */
2380 sizeof(dictiterobject), /* tp_basicsize */
2381 0, /* tp_itemsize */
2382 /* methods */
2383 (destructor)dictiter_dealloc, /* tp_dealloc */
2384 0, /* tp_print */
2385 0, /* tp_getattr */
2386 0, /* tp_setattr */
2387 0, /* tp_reserved */
2388 0, /* tp_repr */
2389 0, /* tp_as_number */
2390 0, /* tp_as_sequence */
2391 0, /* tp_as_mapping */
2392 0, /* tp_hash */
2393 0, /* tp_call */
2394 0, /* tp_str */
2395 PyObject_GenericGetAttr, /* tp_getattro */
2396 0, /* tp_setattro */
2397 0, /* tp_as_buffer */
2398 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2399 0, /* tp_doc */
2400 (traverseproc)dictiter_traverse, /* tp_traverse */
2401 0, /* tp_clear */
2402 0, /* tp_richcompare */
2403 0, /* tp_weaklistoffset */
2404 PyObject_SelfIter, /* tp_iter */
2405 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
2406 dictiter_methods, /* tp_methods */
2407 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002408};
2409
2410static PyObject *dictiter_iternextitem(dictiterobject *di)
2411{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002412 PyObject *key, *value, *result = di->di_result;
2413 register Py_ssize_t i, mask;
2414 register PyDictEntry *ep;
2415 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002416
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002417 if (d == NULL)
2418 return NULL;
2419 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002420
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002421 if (di->di_used != d->ma_used) {
2422 PyErr_SetString(PyExc_RuntimeError,
2423 "dictionary changed size during iteration");
2424 di->di_used = -1; /* Make this state sticky */
2425 return NULL;
2426 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002427
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002428 i = di->di_pos;
2429 if (i < 0)
2430 goto fail;
2431 ep = d->ma_table;
2432 mask = d->ma_mask;
2433 while (i <= mask && ep[i].me_value == NULL)
2434 i++;
2435 di->di_pos = i+1;
2436 if (i > mask)
2437 goto fail;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002438
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002439 if (result->ob_refcnt == 1) {
2440 Py_INCREF(result);
2441 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2442 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2443 } else {
2444 result = PyTuple_New(2);
2445 if (result == NULL)
2446 return NULL;
2447 }
2448 di->len--;
2449 key = ep[i].me_key;
2450 value = ep[i].me_value;
2451 Py_INCREF(key);
2452 Py_INCREF(value);
2453 PyTuple_SET_ITEM(result, 0, key);
2454 PyTuple_SET_ITEM(result, 1, value);
2455 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002456
2457fail:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002458 Py_DECREF(d);
2459 di->di_dict = NULL;
2460 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002461}
2462
2463PyTypeObject PyDictIterItem_Type = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002464 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2465 "dict_itemiterator", /* tp_name */
2466 sizeof(dictiterobject), /* tp_basicsize */
2467 0, /* tp_itemsize */
2468 /* methods */
2469 (destructor)dictiter_dealloc, /* tp_dealloc */
2470 0, /* tp_print */
2471 0, /* tp_getattr */
2472 0, /* tp_setattr */
2473 0, /* tp_reserved */
2474 0, /* tp_repr */
2475 0, /* tp_as_number */
2476 0, /* tp_as_sequence */
2477 0, /* tp_as_mapping */
2478 0, /* tp_hash */
2479 0, /* tp_call */
2480 0, /* tp_str */
2481 PyObject_GenericGetAttr, /* tp_getattro */
2482 0, /* tp_setattro */
2483 0, /* tp_as_buffer */
2484 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2485 0, /* tp_doc */
2486 (traverseproc)dictiter_traverse, /* tp_traverse */
2487 0, /* tp_clear */
2488 0, /* tp_richcompare */
2489 0, /* tp_weaklistoffset */
2490 PyObject_SelfIter, /* tp_iter */
2491 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
2492 dictiter_methods, /* tp_methods */
2493 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002494};
Guido van Rossumb90c8482007-02-10 01:11:45 +00002495
2496
Guido van Rossum3ac67412007-02-10 18:55:06 +00002497/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002498/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002499/***********************************************/
2500
Guido van Rossumb90c8482007-02-10 01:11:45 +00002501/* The instance lay-out is the same for all three; but the type differs. */
2502
2503typedef struct {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002504 PyObject_HEAD
2505 PyDictObject *dv_dict;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002506} dictviewobject;
2507
2508
2509static void
Guido van Rossum3ac67412007-02-10 18:55:06 +00002510dictview_dealloc(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002511{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002512 Py_XDECREF(dv->dv_dict);
2513 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002514}
2515
2516static int
2517dictview_traverse(dictviewobject *dv, visitproc visit, void *arg)
2518{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002519 Py_VISIT(dv->dv_dict);
2520 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002521}
2522
Guido van Rossum83825ac2007-02-10 04:54:19 +00002523static Py_ssize_t
Guido van Rossum3ac67412007-02-10 18:55:06 +00002524dictview_len(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002525{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002526 Py_ssize_t len = 0;
2527 if (dv->dv_dict != NULL)
2528 len = dv->dv_dict->ma_used;
2529 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002530}
2531
2532static PyObject *
2533dictview_new(PyObject *dict, PyTypeObject *type)
2534{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002535 dictviewobject *dv;
2536 if (dict == NULL) {
2537 PyErr_BadInternalCall();
2538 return NULL;
2539 }
2540 if (!PyDict_Check(dict)) {
2541 /* XXX Get rid of this restriction later */
2542 PyErr_Format(PyExc_TypeError,
2543 "%s() requires a dict argument, not '%s'",
2544 type->tp_name, dict->ob_type->tp_name);
2545 return NULL;
2546 }
2547 dv = PyObject_GC_New(dictviewobject, type);
2548 if (dv == NULL)
2549 return NULL;
2550 Py_INCREF(dict);
2551 dv->dv_dict = (PyDictObject *)dict;
2552 _PyObject_GC_TRACK(dv);
2553 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002554}
2555
Neal Norwitze36f2ba2007-02-26 23:12:28 +00002556/* TODO(guido): The views objects are not complete:
2557
2558 * support more set operations
2559 * support arbitrary mappings?
2560 - either these should be static or exported in dictobject.h
2561 - if public then they should probably be in builtins
2562*/
2563
Guido van Rossumaac530c2007-08-24 22:33:45 +00002564/* Return 1 if self is a subset of other, iterating over self;
2565 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002566static int
2567all_contained_in(PyObject *self, PyObject *other)
2568{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002569 PyObject *iter = PyObject_GetIter(self);
2570 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00002571
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002572 if (iter == NULL)
2573 return -1;
2574 for (;;) {
2575 PyObject *next = PyIter_Next(iter);
2576 if (next == NULL) {
2577 if (PyErr_Occurred())
2578 ok = -1;
2579 break;
2580 }
2581 ok = PySequence_Contains(other, next);
2582 Py_DECREF(next);
2583 if (ok <= 0)
2584 break;
2585 }
2586 Py_DECREF(iter);
2587 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00002588}
2589
2590static PyObject *
2591dictview_richcompare(PyObject *self, PyObject *other, int op)
2592{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002593 Py_ssize_t len_self, len_other;
2594 int ok;
2595 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002596
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002597 assert(self != NULL);
2598 assert(PyDictViewSet_Check(self));
2599 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00002600
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002601 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other)) {
2602 Py_INCREF(Py_NotImplemented);
2603 return Py_NotImplemented;
2604 }
Guido van Rossumaac530c2007-08-24 22:33:45 +00002605
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002606 len_self = PyObject_Size(self);
2607 if (len_self < 0)
2608 return NULL;
2609 len_other = PyObject_Size(other);
2610 if (len_other < 0)
2611 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002612
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002613 ok = 0;
2614 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00002615
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002616 case Py_NE:
2617 case Py_EQ:
2618 if (len_self == len_other)
2619 ok = all_contained_in(self, other);
2620 if (op == Py_NE && ok >= 0)
2621 ok = !ok;
2622 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002623
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002624 case Py_LT:
2625 if (len_self < len_other)
2626 ok = all_contained_in(self, other);
2627 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002628
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002629 case Py_LE:
2630 if (len_self <= len_other)
2631 ok = all_contained_in(self, other);
2632 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002633
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002634 case Py_GT:
2635 if (len_self > len_other)
2636 ok = all_contained_in(other, self);
2637 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002638
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002639 case Py_GE:
2640 if (len_self >= len_other)
2641 ok = all_contained_in(other, self);
2642 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002643
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002644 }
2645 if (ok < 0)
2646 return NULL;
2647 result = ok ? Py_True : Py_False;
2648 Py_INCREF(result);
2649 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00002650}
2651
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00002652static PyObject *
2653dictview_repr(dictviewobject *dv)
2654{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002655 PyObject *seq;
2656 PyObject *result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00002657
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002658 seq = PySequence_List((PyObject *)dv);
2659 if (seq == NULL)
2660 return NULL;
2661
2662 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
2663 Py_DECREF(seq);
2664 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00002665}
2666
Guido van Rossum3ac67412007-02-10 18:55:06 +00002667/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002668
2669static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002670dictkeys_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002671{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002672 if (dv->dv_dict == NULL) {
2673 Py_RETURN_NONE;
2674 }
2675 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00002676}
2677
2678static int
2679dictkeys_contains(dictviewobject *dv, PyObject *obj)
2680{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002681 if (dv->dv_dict == NULL)
2682 return 0;
2683 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002684}
2685
Guido van Rossum83825ac2007-02-10 04:54:19 +00002686static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002687 (lenfunc)dictview_len, /* sq_length */
2688 0, /* sq_concat */
2689 0, /* sq_repeat */
2690 0, /* sq_item */
2691 0, /* sq_slice */
2692 0, /* sq_ass_item */
2693 0, /* sq_ass_slice */
2694 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002695};
2696
Guido van Rossum523259b2007-08-24 23:41:22 +00002697static PyObject*
2698dictviews_sub(PyObject* self, PyObject *other)
2699{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002700 PyObject *result = PySet_New(self);
2701 PyObject *tmp;
2702 if (result == NULL)
2703 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00002704
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002705 tmp = PyObject_CallMethod(result, "difference_update", "O", other);
2706 if (tmp == NULL) {
2707 Py_DECREF(result);
2708 return NULL;
2709 }
Guido van Rossum523259b2007-08-24 23:41:22 +00002710
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002711 Py_DECREF(tmp);
2712 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00002713}
2714
2715static PyObject*
2716dictviews_and(PyObject* self, PyObject *other)
2717{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002718 PyObject *result = PySet_New(self);
2719 PyObject *tmp;
2720 if (result == NULL)
2721 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00002722
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002723 tmp = PyObject_CallMethod(result, "intersection_update", "O", other);
2724 if (tmp == NULL) {
2725 Py_DECREF(result);
2726 return NULL;
2727 }
Guido van Rossum523259b2007-08-24 23:41:22 +00002728
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002729 Py_DECREF(tmp);
2730 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00002731}
2732
2733static PyObject*
2734dictviews_or(PyObject* self, PyObject *other)
2735{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002736 PyObject *result = PySet_New(self);
2737 PyObject *tmp;
2738 if (result == NULL)
2739 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00002740
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002741 tmp = PyObject_CallMethod(result, "update", "O", other);
2742 if (tmp == NULL) {
2743 Py_DECREF(result);
2744 return NULL;
2745 }
Guido van Rossum523259b2007-08-24 23:41:22 +00002746
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002747 Py_DECREF(tmp);
2748 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00002749}
2750
2751static PyObject*
2752dictviews_xor(PyObject* self, PyObject *other)
2753{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002754 PyObject *result = PySet_New(self);
2755 PyObject *tmp;
2756 if (result == NULL)
2757 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00002758
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002759 tmp = PyObject_CallMethod(result, "symmetric_difference_update", "O",
2760 other);
2761 if (tmp == NULL) {
2762 Py_DECREF(result);
2763 return NULL;
2764 }
Guido van Rossum523259b2007-08-24 23:41:22 +00002765
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002766 Py_DECREF(tmp);
2767 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00002768}
2769
2770static PyNumberMethods dictviews_as_number = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002771 0, /*nb_add*/
2772 (binaryfunc)dictviews_sub, /*nb_subtract*/
2773 0, /*nb_multiply*/
2774 0, /*nb_remainder*/
2775 0, /*nb_divmod*/
2776 0, /*nb_power*/
2777 0, /*nb_negative*/
2778 0, /*nb_positive*/
2779 0, /*nb_absolute*/
2780 0, /*nb_bool*/
2781 0, /*nb_invert*/
2782 0, /*nb_lshift*/
2783 0, /*nb_rshift*/
2784 (binaryfunc)dictviews_and, /*nb_and*/
2785 (binaryfunc)dictviews_xor, /*nb_xor*/
2786 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00002787};
2788
Guido van Rossumb90c8482007-02-10 01:11:45 +00002789static PyMethodDef dictkeys_methods[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002790 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002791};
2792
2793PyTypeObject PyDictKeys_Type = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002794 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2795 "dict_keys", /* tp_name */
2796 sizeof(dictviewobject), /* tp_basicsize */
2797 0, /* tp_itemsize */
2798 /* methods */
2799 (destructor)dictview_dealloc, /* tp_dealloc */
2800 0, /* tp_print */
2801 0, /* tp_getattr */
2802 0, /* tp_setattr */
2803 0, /* tp_reserved */
2804 (reprfunc)dictview_repr, /* tp_repr */
2805 &dictviews_as_number, /* tp_as_number */
2806 &dictkeys_as_sequence, /* tp_as_sequence */
2807 0, /* tp_as_mapping */
2808 0, /* tp_hash */
2809 0, /* tp_call */
2810 0, /* tp_str */
2811 PyObject_GenericGetAttr, /* tp_getattro */
2812 0, /* tp_setattro */
2813 0, /* tp_as_buffer */
2814 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2815 0, /* tp_doc */
2816 (traverseproc)dictview_traverse, /* tp_traverse */
2817 0, /* tp_clear */
2818 dictview_richcompare, /* tp_richcompare */
2819 0, /* tp_weaklistoffset */
2820 (getiterfunc)dictkeys_iter, /* tp_iter */
2821 0, /* tp_iternext */
2822 dictkeys_methods, /* tp_methods */
2823 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00002824};
2825
2826static PyObject *
2827dictkeys_new(PyObject *dict)
2828{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002829 return dictview_new(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002830}
2831
Guido van Rossum3ac67412007-02-10 18:55:06 +00002832/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002833
2834static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002835dictitems_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002836{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002837 if (dv->dv_dict == NULL) {
2838 Py_RETURN_NONE;
2839 }
2840 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00002841}
2842
2843static int
2844dictitems_contains(dictviewobject *dv, PyObject *obj)
2845{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002846 PyObject *key, *value, *found;
2847 if (dv->dv_dict == NULL)
2848 return 0;
2849 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
2850 return 0;
2851 key = PyTuple_GET_ITEM(obj, 0);
2852 value = PyTuple_GET_ITEM(obj, 1);
2853 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
2854 if (found == NULL) {
2855 if (PyErr_Occurred())
2856 return -1;
2857 return 0;
2858 }
2859 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002860}
2861
Guido van Rossum83825ac2007-02-10 04:54:19 +00002862static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002863 (lenfunc)dictview_len, /* sq_length */
2864 0, /* sq_concat */
2865 0, /* sq_repeat */
2866 0, /* sq_item */
2867 0, /* sq_slice */
2868 0, /* sq_ass_item */
2869 0, /* sq_ass_slice */
2870 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002871};
2872
Guido van Rossumb90c8482007-02-10 01:11:45 +00002873static PyMethodDef dictitems_methods[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002874 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002875};
2876
2877PyTypeObject PyDictItems_Type = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002878 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2879 "dict_items", /* tp_name */
2880 sizeof(dictviewobject), /* tp_basicsize */
2881 0, /* tp_itemsize */
2882 /* methods */
2883 (destructor)dictview_dealloc, /* tp_dealloc */
2884 0, /* tp_print */
2885 0, /* tp_getattr */
2886 0, /* tp_setattr */
2887 0, /* tp_reserved */
2888 (reprfunc)dictview_repr, /* tp_repr */
2889 &dictviews_as_number, /* tp_as_number */
2890 &dictitems_as_sequence, /* tp_as_sequence */
2891 0, /* tp_as_mapping */
2892 0, /* tp_hash */
2893 0, /* tp_call */
2894 0, /* tp_str */
2895 PyObject_GenericGetAttr, /* tp_getattro */
2896 0, /* tp_setattro */
2897 0, /* tp_as_buffer */
2898 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2899 0, /* tp_doc */
2900 (traverseproc)dictview_traverse, /* tp_traverse */
2901 0, /* tp_clear */
2902 dictview_richcompare, /* tp_richcompare */
2903 0, /* tp_weaklistoffset */
2904 (getiterfunc)dictitems_iter, /* tp_iter */
2905 0, /* tp_iternext */
2906 dictitems_methods, /* tp_methods */
2907 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00002908};
2909
2910static PyObject *
2911dictitems_new(PyObject *dict)
2912{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002913 return dictview_new(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002914}
2915
Guido van Rossum3ac67412007-02-10 18:55:06 +00002916/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002917
2918static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002919dictvalues_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002920{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002921 if (dv->dv_dict == NULL) {
2922 Py_RETURN_NONE;
2923 }
2924 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002925}
2926
Guido van Rossum83825ac2007-02-10 04:54:19 +00002927static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002928 (lenfunc)dictview_len, /* sq_length */
2929 0, /* sq_concat */
2930 0, /* sq_repeat */
2931 0, /* sq_item */
2932 0, /* sq_slice */
2933 0, /* sq_ass_item */
2934 0, /* sq_ass_slice */
2935 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002936};
2937
Guido van Rossumb90c8482007-02-10 01:11:45 +00002938static PyMethodDef dictvalues_methods[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002939 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002940};
2941
2942PyTypeObject PyDictValues_Type = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002943 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2944 "dict_values", /* tp_name */
2945 sizeof(dictviewobject), /* tp_basicsize */
2946 0, /* tp_itemsize */
2947 /* methods */
2948 (destructor)dictview_dealloc, /* tp_dealloc */
2949 0, /* tp_print */
2950 0, /* tp_getattr */
2951 0, /* tp_setattr */
2952 0, /* tp_reserved */
2953 (reprfunc)dictview_repr, /* tp_repr */
2954 0, /* tp_as_number */
2955 &dictvalues_as_sequence, /* tp_as_sequence */
2956 0, /* tp_as_mapping */
2957 0, /* tp_hash */
2958 0, /* tp_call */
2959 0, /* tp_str */
2960 PyObject_GenericGetAttr, /* tp_getattro */
2961 0, /* tp_setattro */
2962 0, /* tp_as_buffer */
2963 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2964 0, /* tp_doc */
2965 (traverseproc)dictview_traverse, /* tp_traverse */
2966 0, /* tp_clear */
2967 0, /* tp_richcompare */
2968 0, /* tp_weaklistoffset */
2969 (getiterfunc)dictvalues_iter, /* tp_iter */
2970 0, /* tp_iternext */
2971 dictvalues_methods, /* tp_methods */
2972 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00002973};
2974
2975static PyObject *
2976dictvalues_new(PyObject *dict)
2977{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002978 return dictview_new(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002979}