blob: 83957ac05bf9dde28cdd4f244bd628b7e5e4aa57 [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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000127*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000128
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000129/* Object used as dummy key to fill deleted entries */
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000130static PyObject *dummy = NULL; /* Initialized by first call to newPyDictObject() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000131
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000132#ifdef Py_REF_DEBUG
133PyObject *
134_PyDict_Dummy(void)
135{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 return dummy;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000137}
138#endif
139
Fred Drake1bff34a2000-08-31 19:31:38 +0000140/* forward declarations */
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000141static PyDictEntry *
Benjamin Petersone6baa462010-10-17 21:20:58 +0000142lookdict_unicode(PyDictObject *mp, PyObject *key, Py_hash_t hash);
Fred Drake1bff34a2000-08-31 19:31:38 +0000143
144#ifdef SHOW_CONVERSION_COUNTS
145static long created = 0L;
146static long converted = 0L;
147
148static void
149show_counts(void)
150{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 fprintf(stderr, "created %ld string dicts\n", created);
152 fprintf(stderr, "converted %ld to normal dicts\n", converted);
153 fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created);
Fred Drake1bff34a2000-08-31 19:31:38 +0000154}
155#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000156
Christian Heimes77c02eb2008-02-09 02:18:51 +0000157/* Debug statistic to compare allocations with reuse through the free list */
158#undef SHOW_ALLOC_COUNT
159#ifdef SHOW_ALLOC_COUNT
160static size_t count_alloc = 0;
161static size_t count_reuse = 0;
162
163static void
164show_alloc(void)
165{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 fprintf(stderr, "Dict allocations: %" PY_FORMAT_SIZE_T "d\n",
167 count_alloc);
168 fprintf(stderr, "Dict reuse through freelist: %" PY_FORMAT_SIZE_T
169 "d\n", count_reuse);
170 fprintf(stderr, "%.2f%% reuse rate\n\n",
171 (100.0*count_reuse/(count_alloc+count_reuse)));
Christian Heimes77c02eb2008-02-09 02:18:51 +0000172}
173#endif
174
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000175/* Debug statistic to count GC tracking of dicts */
176#ifdef SHOW_TRACK_COUNT
177static Py_ssize_t count_untracked = 0;
178static Py_ssize_t count_tracked = 0;
179
180static void
181show_track(void)
182{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 fprintf(stderr, "Dicts created: %" PY_FORMAT_SIZE_T "d\n",
184 count_tracked + count_untracked);
185 fprintf(stderr, "Dicts tracked by the GC: %" PY_FORMAT_SIZE_T
186 "d\n", count_tracked);
187 fprintf(stderr, "%.2f%% dict tracking rate\n\n",
188 (100.0*count_tracked/(count_untracked+count_tracked)));
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000189}
190#endif
191
192
Tim Peters6d6c1a32001-08-02 04:15:00 +0000193/* Initialization macros.
194 There are two ways to create a dict: PyDict_New() is the main C API
195 function, and the tp_new slot maps to dict_new(). In the latter case we
196 can save a little time over what PyDict_New does because it's guaranteed
197 that the PyDictObject struct is already zeroed out.
198 Everyone except dict_new() should use EMPTY_TO_MINSIZE (unless they have
199 an excellent reason not to).
200*/
201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202#define INIT_NONZERO_DICT_SLOTS(mp) do { \
203 (mp)->ma_table = (mp)->ma_smalltable; \
204 (mp)->ma_mask = PyDict_MINSIZE - 1; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000205 } while(0)
206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207#define EMPTY_TO_MINSIZE(mp) do { \
208 memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \
209 (mp)->ma_used = (mp)->ma_fill = 0; \
210 INIT_NONZERO_DICT_SLOTS(mp); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000211 } while(0)
212
Raymond Hettinger43442782004-03-17 21:55:03 +0000213/* Dictionary reuse scheme to save calls to malloc, free, and memset */
Christian Heimes2202f872008-02-06 14:31:34 +0000214#ifndef PyDict_MAXFREELIST
215#define PyDict_MAXFREELIST 80
216#endif
217static PyDictObject *free_list[PyDict_MAXFREELIST];
218static int numfree = 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000219
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100220int
221PyDict_ClearFreeList(void)
Christian Heimes77c02eb2008-02-09 02:18:51 +0000222{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 PyDictObject *op;
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100224 int ret = numfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 while (numfree) {
226 op = free_list[--numfree];
227 assert(PyDict_CheckExact(op));
228 PyObject_GC_Del(op);
229 }
Antoine Pitrou9a812cb2011-11-15 00:00:12 +0100230 return ret;
231}
232
233void
234PyDict_Fini(void)
235{
236 PyDict_ClearFreeList();
Christian Heimes77c02eb2008-02-09 02:18:51 +0000237}
238
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000239PyObject *
Thomas Wouters78890102000-07-22 19:25:51 +0000240PyDict_New(void)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000241{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 register PyDictObject *mp;
243 if (dummy == NULL) { /* Auto-initialize dummy */
244 dummy = PyUnicode_FromString("<dummy key>");
245 if (dummy == NULL)
246 return NULL;
Fred Drake1bff34a2000-08-31 19:31:38 +0000247#ifdef SHOW_CONVERSION_COUNTS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 Py_AtExit(show_counts);
Fred Drake1bff34a2000-08-31 19:31:38 +0000249#endif
Christian Heimes77c02eb2008-02-09 02:18:51 +0000250#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 Py_AtExit(show_alloc);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000252#endif
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000253#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 Py_AtExit(show_track);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000255#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 }
257 if (numfree) {
258 mp = free_list[--numfree];
259 assert (mp != NULL);
260 assert (Py_TYPE(mp) == &PyDict_Type);
261 _Py_NewReference((PyObject *)mp);
262 if (mp->ma_fill) {
263 EMPTY_TO_MINSIZE(mp);
264 } else {
265 /* At least set ma_table and ma_mask; these are wrong
266 if an empty but presized dict is added to freelist */
267 INIT_NONZERO_DICT_SLOTS(mp);
268 }
269 assert (mp->ma_used == 0);
270 assert (mp->ma_table == mp->ma_smalltable);
271 assert (mp->ma_mask == PyDict_MINSIZE - 1);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000272#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 count_reuse++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000274#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 } else {
276 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
277 if (mp == NULL)
278 return NULL;
279 EMPTY_TO_MINSIZE(mp);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000280#ifdef SHOW_ALLOC_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 count_alloc++;
Christian Heimes77c02eb2008-02-09 02:18:51 +0000282#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 }
284 mp->ma_lookup = lookdict_unicode;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000285#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 count_untracked++;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000287#endif
Fred Drake1bff34a2000-08-31 19:31:38 +0000288#ifdef SHOW_CONVERSION_COUNTS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 ++created;
Fred Drake1bff34a2000-08-31 19:31:38 +0000290#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000292}
293
294/*
295The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000296This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000297Open addressing is preferred over chaining since the link overhead for
298chaining would be substantial (100% with typical malloc overhead).
299
Tim Peterseb28ef22001-06-02 05:27:19 +0000300The initial probe index is computed as hash mod the table size. Subsequent
301probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000302
303All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000304
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000305The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000306contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000307Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000308
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000309lookdict() is general-purpose, and may return NULL if (and only if) a
310comparison raises an exception (this was new in Python 2.5).
Guido van Rossum89d8c602007-09-18 17:26:56 +0000311lookdict_unicode() below is specialized to string keys, comparison of which can
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000312never raise an exception; that function can never return NULL. For both, when
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000313the key isn't found a PyDictEntry* is returned for which the me_value field is
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000314NULL; this is the slot in the dict at which the key would have been found, and
315the caller can (if it wishes) add the <key, value> pair to the returned
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000316PyDictEntry*.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000317*/
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000318static PyDictEntry *
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000319lookdict(PyDictObject *mp, PyObject *key, register Py_hash_t hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000320{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 register size_t i;
322 register size_t perturb;
323 register PyDictEntry *freeslot;
324 register size_t mask = (size_t)mp->ma_mask;
325 PyDictEntry *ep0 = mp->ma_table;
326 register PyDictEntry *ep;
327 register int cmp;
328 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 i = (size_t)hash & mask;
331 ep = &ep0[i];
332 if (ep->me_key == NULL || ep->me_key == key)
333 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 if (ep->me_key == dummy)
336 freeslot = ep;
337 else {
338 if (ep->me_hash == hash) {
339 startkey = ep->me_key;
340 Py_INCREF(startkey);
341 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
342 Py_DECREF(startkey);
343 if (cmp < 0)
344 return NULL;
345 if (ep0 == mp->ma_table && ep->me_key == startkey) {
346 if (cmp > 0)
347 return ep;
348 }
349 else {
350 /* The compare did major nasty stuff to the
351 * dict: start over.
352 * XXX A clever adversary could prevent this
353 * XXX from terminating.
354 */
355 return lookdict(mp, key, hash);
356 }
357 }
358 freeslot = NULL;
359 }
Tim Peters15d49292001-05-27 07:39:22 +0000360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 /* In the loop, me_key == dummy is by far (factor of 100s) the
362 least likely outcome, so test for that last. */
363 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
364 i = (i << 2) + i + perturb + 1;
365 ep = &ep0[i & mask];
366 if (ep->me_key == NULL)
367 return freeslot == NULL ? ep : freeslot;
368 if (ep->me_key == key)
369 return ep;
370 if (ep->me_hash == hash && ep->me_key != dummy) {
371 startkey = ep->me_key;
372 Py_INCREF(startkey);
373 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
374 Py_DECREF(startkey);
375 if (cmp < 0)
376 return NULL;
377 if (ep0 == mp->ma_table && ep->me_key == startkey) {
378 if (cmp > 0)
379 return ep;
380 }
381 else {
382 /* The compare did major nasty stuff to the
383 * dict: start over.
384 * XXX A clever adversary could prevent this
385 * XXX from terminating.
386 */
387 return lookdict(mp, key, hash);
388 }
389 }
390 else if (ep->me_key == dummy && freeslot == NULL)
391 freeslot = ep;
392 }
393 assert(0); /* NOT REACHED */
394 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000395}
396
397/*
Guido van Rossum89d8c602007-09-18 17:26:56 +0000398 * Hacked up version of lookdict which can assume keys are always
399 * unicodes; this assumption allows testing for errors during
400 * PyObject_RichCompareBool() to be dropped; unicode-unicode
401 * comparisons never raise exceptions. This also means we don't need
402 * to go through PyObject_RichCompareBool(); we can always use
403 * unicode_eq() directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000404 *
Guido van Rossum89d8c602007-09-18 17:26:56 +0000405 * This is valuable because dicts with only unicode keys are very common.
Fred Drake1bff34a2000-08-31 19:31:38 +0000406 */
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000407static PyDictEntry *
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000408lookdict_unicode(PyDictObject *mp, PyObject *key, register Py_hash_t hash)
Fred Drake1bff34a2000-08-31 19:31:38 +0000409{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 register size_t i;
411 register size_t perturb;
412 register PyDictEntry *freeslot;
413 register size_t mask = (size_t)mp->ma_mask;
414 PyDictEntry *ep0 = mp->ma_table;
415 register PyDictEntry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 /* Make sure this function doesn't have to handle non-unicode keys,
418 including subclasses of str; e.g., one reason to subclass
419 unicodes is to override __eq__, and for speed we don't cater to
420 that here. */
421 if (!PyUnicode_CheckExact(key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000422#ifdef SHOW_CONVERSION_COUNTS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 ++converted;
Fred Drake1bff34a2000-08-31 19:31:38 +0000424#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 mp->ma_lookup = lookdict;
426 return lookdict(mp, key, hash);
427 }
Mark Dickinson57e683e2011-09-24 18:18:40 +0100428 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 ep = &ep0[i];
430 if (ep->me_key == NULL || ep->me_key == key)
431 return ep;
432 if (ep->me_key == dummy)
433 freeslot = ep;
434 else {
435 if (ep->me_hash == hash && unicode_eq(ep->me_key, key))
436 return ep;
437 freeslot = NULL;
438 }
Tim Peters15d49292001-05-27 07:39:22 +0000439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 /* In the loop, me_key == dummy is by far (factor of 100s) the
441 least likely outcome, so test for that last. */
442 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
443 i = (i << 2) + i + perturb + 1;
444 ep = &ep0[i & mask];
445 if (ep->me_key == NULL)
446 return freeslot == NULL ? ep : freeslot;
447 if (ep->me_key == key
448 || (ep->me_hash == hash
449 && ep->me_key != dummy
450 && unicode_eq(ep->me_key, key)))
451 return ep;
452 if (ep->me_key == dummy && freeslot == NULL)
453 freeslot = ep;
454 }
455 assert(0); /* NOT REACHED */
456 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000457}
458
Benjamin Petersonfb886362010-04-24 18:21:17 +0000459int
460_PyDict_HasOnlyStringKeys(PyObject *dict)
461{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 Py_ssize_t pos = 0;
463 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000464 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 /* Shortcut */
466 if (((PyDictObject *)dict)->ma_lookup == lookdict_unicode)
467 return 1;
468 while (PyDict_Next(dict, &pos, &key, &value))
469 if (!PyUnicode_Check(key))
470 return 0;
471 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000472}
473
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000474#ifdef SHOW_TRACK_COUNT
475#define INCREASE_TRACK_COUNT \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 (count_tracked++, count_untracked--);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000477#define DECREASE_TRACK_COUNT \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 (count_tracked--, count_untracked++);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000479#else
480#define INCREASE_TRACK_COUNT
481#define DECREASE_TRACK_COUNT
482#endif
483
484#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 do { \
486 if (!_PyObject_GC_IS_TRACKED(mp)) { \
487 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
488 _PyObject_GC_MAY_BE_TRACKED(value)) { \
489 _PyObject_GC_TRACK(mp); \
490 INCREASE_TRACK_COUNT \
491 } \
492 } \
493 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000494
495void
496_PyDict_MaybeUntrack(PyObject *op)
497{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 PyDictObject *mp;
499 PyObject *value;
500 Py_ssize_t mask, i;
501 PyDictEntry *ep;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
504 return;
505
506 mp = (PyDictObject *) op;
507 ep = mp->ma_table;
508 mask = mp->ma_mask;
509 for (i = 0; i <= mask; i++) {
510 if ((value = ep[i].me_value) == NULL)
511 continue;
512 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
513 _PyObject_GC_MAY_BE_TRACKED(ep[i].me_key))
514 return;
515 }
516 DECREASE_TRACK_COUNT
517 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000518}
519
Fred Drake1bff34a2000-08-31 19:31:38 +0000520/*
Antoine Pitroue965d972012-02-27 00:45:12 +0100521Internal routine to insert a new item into the table when you have entry object.
522Used by insertdict.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000523*/
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000524static int
Antoine Pitroue965d972012-02-27 00:45:12 +0100525insertdict_by_entry(register PyDictObject *mp, PyObject *key, Py_hash_t hash,
526 PyDictEntry *ep, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000527{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 PyObject *old_value;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 MAINTAIN_TRACKING(mp, key, value);
531 if (ep->me_value != NULL) {
532 old_value = ep->me_value;
533 ep->me_value = value;
534 Py_DECREF(old_value); /* which **CAN** re-enter */
535 Py_DECREF(key);
536 }
537 else {
538 if (ep->me_key == NULL)
539 mp->ma_fill++;
540 else {
541 assert(ep->me_key == dummy);
542 Py_DECREF(dummy);
543 }
544 ep->me_key = key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000545 ep->me_hash = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 ep->me_value = value;
547 mp->ma_used++;
548 }
549 return 0;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000550}
551
Antoine Pitroue965d972012-02-27 00:45:12 +0100552
553/*
554Internal routine to insert a new item into the table.
555Used both by the internal resize routine and by the public insert routine.
556Eats a reference to key and one to value.
557Returns -1 if an error occurred, or 0 on success.
558*/
559static int
560insertdict(register PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
561{
562 register PyDictEntry *ep;
563
564 assert(mp->ma_lookup != NULL);
565 ep = mp->ma_lookup(mp, key, hash);
566 if (ep == NULL) {
567 Py_DECREF(key);
568 Py_DECREF(value);
569 return -1;
570 }
571 return insertdict_by_entry(mp, key, hash, ep, value);
572}
573
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000574/*
575Internal routine used by dictresize() to insert an item which is
576known to be absent from the dict. This routine also assumes that
577the dict contains no deleted entries. Besides the performance benefit,
578using insertdict() in dictresize() is dangerous (SF bug #1456209).
579Note that no refcounts are changed by this routine; if needed, the caller
580is responsible for incref'ing `key` and `value`.
581*/
582static void
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000583insertdict_clean(register PyDictObject *mp, PyObject *key, Py_hash_t hash,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 PyObject *value)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000585{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 register size_t i;
587 register size_t perturb;
588 register size_t mask = (size_t)mp->ma_mask;
589 PyDictEntry *ep0 = mp->ma_table;
590 register PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 MAINTAIN_TRACKING(mp, key, value);
Mark Dickinson57e683e2011-09-24 18:18:40 +0100593 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 ep = &ep0[i];
595 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
596 i = (i << 2) + i + perturb + 1;
597 ep = &ep0[i & mask];
598 }
599 assert(ep->me_value == NULL);
600 mp->ma_fill++;
601 ep->me_key = key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000602 ep->me_hash = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 ep->me_value = value;
604 mp->ma_used++;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000605}
606
607/*
608Restructure the table by allocating a new table and reinserting all
609items again. When entries have been deleted, the new table may
610actually be smaller than the old one.
611*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000612static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000613dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000614{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 Py_ssize_t newsize;
616 PyDictEntry *oldtable, *newtable, *ep;
617 Py_ssize_t i;
618 int is_oldtable_malloced;
619 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 /* Find the smallest table size > minused. */
624 for (newsize = PyDict_MINSIZE;
625 newsize <= minused && newsize > 0;
626 newsize <<= 1)
627 ;
628 if (newsize <= 0) {
629 PyErr_NoMemory();
630 return -1;
631 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 /* Get space for a new table. */
634 oldtable = mp->ma_table;
635 assert(oldtable != NULL);
636 is_oldtable_malloced = oldtable != mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 if (newsize == PyDict_MINSIZE) {
639 /* A large table is shrinking, or we can't get any smaller. */
640 newtable = mp->ma_smalltable;
641 if (newtable == oldtable) {
642 if (mp->ma_fill == mp->ma_used) {
643 /* No dummies, so no point doing anything. */
644 return 0;
645 }
646 /* We're not going to resize it, but rebuild the
647 table anyway to purge old dummy entries.
648 Subtle: This is *necessary* if fill==size,
649 as lookdict needs at least one virgin slot to
650 terminate failing searches. If fill < size, it's
651 merely desirable, as dummies slow searches. */
652 assert(mp->ma_fill > mp->ma_used);
653 memcpy(small_copy, oldtable, sizeof(small_copy));
654 oldtable = small_copy;
655 }
656 }
657 else {
658 newtable = PyMem_NEW(PyDictEntry, newsize);
659 if (newtable == NULL) {
660 PyErr_NoMemory();
661 return -1;
662 }
663 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 /* Make the dict empty, using the new table. */
666 assert(newtable != oldtable);
667 mp->ma_table = newtable;
668 mp->ma_mask = newsize - 1;
669 memset(newtable, 0, sizeof(PyDictEntry) * newsize);
670 mp->ma_used = 0;
671 i = mp->ma_fill;
672 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 /* Copy the data over; this is refcount-neutral for active entries;
675 dummy entries aren't copied over, of course */
676 for (ep = oldtable; i > 0; ep++) {
677 if (ep->me_value != NULL) { /* active entry */
678 --i;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000679 insertdict_clean(mp, ep->me_key, ep->me_hash, ep->me_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 }
681 else if (ep->me_key != NULL) { /* dummy entry */
682 --i;
683 assert(ep->me_key == dummy);
684 Py_DECREF(ep->me_key);
685 }
686 /* else key == value == NULL: nothing to do */
687 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 if (is_oldtable_malloced)
690 PyMem_DEL(oldtable);
691 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000692}
693
Christian Heimes99170a52007-12-19 02:07:34 +0000694/* Create a new dictionary pre-sized to hold an estimated number of elements.
695 Underestimates are okay because the dictionary will resize as necessary.
696 Overestimates just mean the dictionary will be more sparse than usual.
697*/
698
699PyObject *
700_PyDict_NewPresized(Py_ssize_t minused)
701{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 PyObject *op = PyDict_New();
Christian Heimes99170a52007-12-19 02:07:34 +0000703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 if (minused>5 && op != NULL && dictresize((PyDictObject *)op, minused) == -1) {
705 Py_DECREF(op);
706 return NULL;
707 }
708 return op;
Christian Heimes99170a52007-12-19 02:07:34 +0000709}
710
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000711/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
712 * that may occur (originally dicts supported only string keys, and exceptions
713 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000714 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000715 * (suppressed) occurred while computing the key's hash, or that some error
716 * (suppressed) occurred when comparing keys in the dict's internal probe
717 * sequence. A nasty example of the latter is when a Python-coded comparison
718 * function hits a stack-depth error, which can cause this to return NULL
719 * even if the key is present.
720 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000721PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000722PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000723{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000724 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 PyDictObject *mp = (PyDictObject *)op;
726 PyDictEntry *ep;
727 PyThreadState *tstate;
728 if (!PyDict_Check(op))
729 return NULL;
730 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200731 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 {
733 hash = PyObject_Hash(key);
734 if (hash == -1) {
735 PyErr_Clear();
736 return NULL;
737 }
738 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 /* We can arrive here with a NULL tstate during initialization: try
741 running "python -Wi" for an example related to string interning.
742 Let's just hope that no exception occurs then... This must be
743 _PyThreadState_Current and not PyThreadState_GET() because in debug
744 mode, the latter complains if tstate is NULL. */
745 tstate = (PyThreadState*)_Py_atomic_load_relaxed(
746 &_PyThreadState_Current);
747 if (tstate != NULL && tstate->curexc_type != NULL) {
748 /* preserve the existing exception */
749 PyObject *err_type, *err_value, *err_tb;
750 PyErr_Fetch(&err_type, &err_value, &err_tb);
751 ep = (mp->ma_lookup)(mp, key, hash);
752 /* ignore errors */
753 PyErr_Restore(err_type, err_value, err_tb);
754 if (ep == NULL)
755 return NULL;
756 }
757 else {
758 ep = (mp->ma_lookup)(mp, key, hash);
759 if (ep == NULL) {
760 PyErr_Clear();
761 return NULL;
762 }
763 }
764 return ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000765}
766
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000767/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
768 This returns NULL *with* an exception set if an exception occurred.
769 It returns NULL *without* an exception set if the key wasn't present.
770*/
771PyObject *
772PyDict_GetItemWithError(PyObject *op, PyObject *key)
773{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000774 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 PyDictObject*mp = (PyDictObject *)op;
776 PyDictEntry *ep;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 if (!PyDict_Check(op)) {
779 PyErr_BadInternalCall();
780 return NULL;
781 }
782 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200783 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 {
785 hash = PyObject_Hash(key);
786 if (hash == -1) {
787 return NULL;
788 }
789 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 ep = (mp->ma_lookup)(mp, key, hash);
792 if (ep == NULL)
793 return NULL;
794 return ep->me_value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000795}
796
Antoine Pitroue965d972012-02-27 00:45:12 +0100797static int
798dict_set_item_by_hash_or_entry(register PyObject *op, PyObject *key,
799 Py_hash_t hash, PyDictEntry *ep, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000800{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 register PyDictObject *mp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 register Py_ssize_t n_used;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000803
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
806 n_used = mp->ma_used;
807 Py_INCREF(value);
808 Py_INCREF(key);
Antoine Pitroue965d972012-02-27 00:45:12 +0100809 if (ep == NULL) {
810 if (insertdict(mp, key, hash, value) != 0)
811 return -1;
812 }
813 else {
814 if (insertdict_by_entry(mp, key, hash, ep, value) != 0)
815 return -1;
816 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 /* If we added a key, we can safely resize. Otherwise just return!
818 * If fill >= 2/3 size, adjust size. Normally, this doubles or
819 * quaduples the size, but it's also possible for the dict to shrink
820 * (if ma_fill is much larger than ma_used, meaning a lot of dict
821 * keys have been * deleted).
822 *
823 * Quadrupling the size improves average dictionary sparseness
824 * (reducing collisions) at the cost of some memory and iteration
825 * speed (which loops over every possible entry). It also halves
826 * the number of expensive resize operations in a growing dictionary.
827 *
828 * Very large dictionaries (over 50K items) use doubling instead.
829 * This may help applications with severe memory constraints.
830 */
831 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
832 return 0;
833 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000834}
835
Antoine Pitroue965d972012-02-27 00:45:12 +0100836/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
837 * dictionary if it's merely replacing the value for an existing key.
838 * This means that it's safe to loop over a dictionary with PyDict_Next()
839 * and occasionally replace a value -- but you can't insert new keys or
840 * remove them.
841 */
842int
843PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
844{
845 register Py_hash_t hash;
846
847 if (!PyDict_Check(op)) {
848 PyErr_BadInternalCall();
849 return -1;
850 }
851 assert(key);
852 assert(value);
853 if (PyUnicode_CheckExact(key)) {
Antoine Pitrou70d27172012-02-27 00:59:34 +0100854 hash = ((PyASCIIObject *) key)->hash;
Antoine Pitroue965d972012-02-27 00:45:12 +0100855 if (hash == -1)
856 hash = PyObject_Hash(key);
857 }
858 else {
859 hash = PyObject_Hash(key);
860 if (hash == -1)
861 return -1;
862 }
863 return dict_set_item_by_hash_or_entry(op, key, hash, NULL, value);
864}
865
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000866int
Tim Peters1f5871e2000-07-04 17:44:48 +0000867PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000868{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 register PyDictObject *mp;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000870 register Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 register PyDictEntry *ep;
872 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 if (!PyDict_Check(op)) {
875 PyErr_BadInternalCall();
876 return -1;
877 }
878 assert(key);
879 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200880 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 hash = PyObject_Hash(key);
882 if (hash == -1)
883 return -1;
884 }
885 mp = (PyDictObject *)op;
886 ep = (mp->ma_lookup)(mp, key, hash);
887 if (ep == NULL)
888 return -1;
889 if (ep->me_value == NULL) {
890 set_key_error(key);
891 return -1;
892 }
893 old_key = ep->me_key;
894 Py_INCREF(dummy);
895 ep->me_key = dummy;
896 old_value = ep->me_value;
897 ep->me_value = NULL;
898 mp->ma_used--;
899 Py_DECREF(old_value);
900 Py_DECREF(old_key);
901 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000902}
903
Guido van Rossum25831651993-05-19 14:50:45 +0000904void
Tim Peters1f5871e2000-07-04 17:44:48 +0000905PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000906{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 PyDictObject *mp;
908 PyDictEntry *ep, *table;
909 int table_is_malloced;
910 Py_ssize_t fill;
911 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000912#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +0000914#endif
915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 if (!PyDict_Check(op))
917 return;
918 mp = (PyDictObject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000919#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 n = mp->ma_mask + 1;
921 i = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000922#endif
923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 table = mp->ma_table;
925 assert(table != NULL);
926 table_is_malloced = table != mp->ma_smalltable;
Tim Petersdea48ec2001-05-22 20:40:22 +0000927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 /* This is delicate. During the process of clearing the dict,
929 * decrefs can cause the dict to mutate. To avoid fatal confusion
930 * (voice of experience), we have to make the dict empty before
931 * clearing the slots, and never refer to anything via mp->xxx while
932 * clearing.
933 */
934 fill = mp->ma_fill;
935 if (table_is_malloced)
936 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 else if (fill > 0) {
939 /* It's a small table with something that needs to be cleared.
940 * Afraid the only safe way is to copy the dict entries into
941 * another small table first.
942 */
943 memcpy(small_copy, table, sizeof(small_copy));
944 table = small_copy;
945 EMPTY_TO_MINSIZE(mp);
946 }
947 /* else it's a small table that's already empty */
Tim Petersdea48ec2001-05-22 20:40:22 +0000948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 /* Now we can finally clear things. If C had refcounts, we could
950 * assert that the refcount on table is 1 now, i.e. that this function
951 * has unique access to it, so decref side-effects can't alter it.
952 */
953 for (ep = table; fill > 0; ++ep) {
Tim Petersdea48ec2001-05-22 20:40:22 +0000954#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 assert(i < n);
956 ++i;
Tim Petersdea48ec2001-05-22 20:40:22 +0000957#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 if (ep->me_key) {
959 --fill;
960 Py_DECREF(ep->me_key);
961 Py_XDECREF(ep->me_value);
962 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000963#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 else
965 assert(ep->me_value == NULL);
Tim Petersdea48ec2001-05-22 20:40:22 +0000966#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000968
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 if (table_is_malloced)
970 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000971}
972
Tim Peters080c88b2003-02-15 03:01:11 +0000973/*
974 * Iterate over a dict. Use like so:
975 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000976 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +0000977 * PyObject *key, *value;
978 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +0000979 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +0000980 * Refer to borrowed references in key and value.
981 * }
982 *
983 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +0000984 * mutates the dict. One exception: it is safe if the loop merely changes
985 * the values associated with the keys (but doesn't insert new keys or
986 * delete keys), via PyDict_SetItem().
987 */
Guido van Rossum25831651993-05-19 14:50:45 +0000988int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000989PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000990{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 register Py_ssize_t i;
992 register Py_ssize_t mask;
993 register PyDictEntry *ep;
Raymond Hettinger43442782004-03-17 21:55:03 +0000994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 if (!PyDict_Check(op))
996 return 0;
997 i = *ppos;
998 if (i < 0)
999 return 0;
1000 ep = ((PyDictObject *)op)->ma_table;
1001 mask = ((PyDictObject *)op)->ma_mask;
1002 while (i <= mask && ep[i].me_value == NULL)
1003 i++;
1004 *ppos = i+1;
1005 if (i > mask)
1006 return 0;
1007 if (pkey)
1008 *pkey = ep[i].me_key;
1009 if (pvalue)
1010 *pvalue = ep[i].me_value;
1011 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001012}
1013
Thomas Wouterscf297e42007-02-23 15:07:44 +00001014/* Internal version of PyDict_Next that returns a hash value in addition to the key and value.*/
1015int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001016_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, Py_hash_t *phash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00001017{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 register Py_ssize_t i;
1019 register Py_ssize_t mask;
1020 register PyDictEntry *ep;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 if (!PyDict_Check(op))
1023 return 0;
1024 i = *ppos;
1025 if (i < 0)
1026 return 0;
1027 ep = ((PyDictObject *)op)->ma_table;
1028 mask = ((PyDictObject *)op)->ma_mask;
1029 while (i <= mask && ep[i].me_value == NULL)
1030 i++;
1031 *ppos = i+1;
1032 if (i > mask)
1033 return 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001034 *phash = ep[i].me_hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 if (pkey)
1036 *pkey = ep[i].me_key;
1037 if (pvalue)
1038 *pvalue = ep[i].me_value;
1039 return 1;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001040}
1041
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001042/* Methods */
1043
1044static void
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001045dict_dealloc(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001046{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 register PyDictEntry *ep;
1048 Py_ssize_t fill = mp->ma_fill;
1049 PyObject_GC_UnTrack(mp);
1050 Py_TRASHCAN_SAFE_BEGIN(mp)
1051 for (ep = mp->ma_table; fill > 0; ep++) {
1052 if (ep->me_key) {
1053 --fill;
1054 Py_DECREF(ep->me_key);
1055 Py_XDECREF(ep->me_value);
1056 }
1057 }
1058 if (mp->ma_table != mp->ma_smalltable)
1059 PyMem_DEL(mp->ma_table);
1060 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1061 free_list[numfree++] = mp;
1062 else
1063 Py_TYPE(mp)->tp_free((PyObject *)mp);
1064 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001065}
1066
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001067static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001068dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001069{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 Py_ssize_t i;
1071 PyObject *s, *temp, *colon = NULL;
1072 PyObject *pieces = NULL, *result = NULL;
1073 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +00001074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 i = Py_ReprEnter((PyObject *)mp);
1076 if (i != 0) {
1077 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1078 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001079
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 if (mp->ma_used == 0) {
1081 result = PyUnicode_FromString("{}");
1082 goto Done;
1083 }
Tim Petersa7259592001-06-16 05:11:17 +00001084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 pieces = PyList_New(0);
1086 if (pieces == NULL)
1087 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 colon = PyUnicode_FromString(": ");
1090 if (colon == NULL)
1091 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 /* Do repr() on each key+value pair, and insert ": " between them.
1094 Note that repr may mutate the dict. */
1095 i = 0;
1096 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
1097 int status;
1098 /* Prevent repr from deleting value during key format. */
1099 Py_INCREF(value);
1100 s = PyObject_Repr(key);
1101 PyUnicode_Append(&s, colon);
1102 PyUnicode_AppendAndDel(&s, PyObject_Repr(value));
1103 Py_DECREF(value);
1104 if (s == NULL)
1105 goto Done;
1106 status = PyList_Append(pieces, s);
1107 Py_DECREF(s); /* append created a new ref */
1108 if (status < 0)
1109 goto Done;
1110 }
Tim Petersa7259592001-06-16 05:11:17 +00001111
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 /* Add "{}" decorations to the first and last items. */
1113 assert(PyList_GET_SIZE(pieces) > 0);
1114 s = PyUnicode_FromString("{");
1115 if (s == NULL)
1116 goto Done;
1117 temp = PyList_GET_ITEM(pieces, 0);
1118 PyUnicode_AppendAndDel(&s, temp);
1119 PyList_SET_ITEM(pieces, 0, s);
1120 if (s == NULL)
1121 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 s = PyUnicode_FromString("}");
1124 if (s == NULL)
1125 goto Done;
1126 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
1127 PyUnicode_AppendAndDel(&temp, s);
1128 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
1129 if (temp == NULL)
1130 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 /* Paste them all together with ", " between. */
1133 s = PyUnicode_FromString(", ");
1134 if (s == NULL)
1135 goto Done;
1136 result = PyUnicode_Join(s, pieces);
1137 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +00001138
1139Done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 Py_XDECREF(pieces);
1141 Py_XDECREF(colon);
1142 Py_ReprLeave((PyObject *)mp);
1143 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001144}
1145
Martin v. Löwis18e16552006-02-15 17:27:45 +00001146static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001147dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001148{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001150}
1151
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001152static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001153dict_subscript(PyDictObject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001154{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 PyObject *v;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001156 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 PyDictEntry *ep;
1158 assert(mp->ma_table != NULL);
1159 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001160 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 hash = PyObject_Hash(key);
1162 if (hash == -1)
1163 return NULL;
1164 }
1165 ep = (mp->ma_lookup)(mp, key, hash);
1166 if (ep == NULL)
1167 return NULL;
1168 v = ep->me_value;
1169 if (v == NULL) {
1170 if (!PyDict_CheckExact(mp)) {
1171 /* Look up __missing__ method if we're a subclass. */
1172 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05001173 _Py_IDENTIFIER(__missing__);
1174 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 if (missing != NULL) {
1176 res = PyObject_CallFunctionObjArgs(missing,
1177 key, NULL);
1178 Py_DECREF(missing);
1179 return res;
1180 }
1181 else if (PyErr_Occurred())
1182 return NULL;
1183 }
1184 set_key_error(key);
1185 return NULL;
1186 }
1187 else
1188 Py_INCREF(v);
1189 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001190}
1191
1192static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001193dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001194{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 if (w == NULL)
1196 return PyDict_DelItem((PyObject *)mp, v);
1197 else
1198 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001199}
1200
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001201static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 (lenfunc)dict_length, /*mp_length*/
1203 (binaryfunc)dict_subscript, /*mp_subscript*/
1204 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001205};
1206
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001207static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001208dict_keys(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 register PyObject *v;
1211 register Py_ssize_t i, j;
1212 PyDictEntry *ep;
1213 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001214
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001215 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 n = mp->ma_used;
1217 v = PyList_New(n);
1218 if (v == NULL)
1219 return NULL;
1220 if (n != mp->ma_used) {
1221 /* Durnit. The allocations caused the dict to resize.
1222 * Just start over, this shouldn't normally happen.
1223 */
1224 Py_DECREF(v);
1225 goto again;
1226 }
1227 ep = mp->ma_table;
1228 mask = mp->ma_mask;
1229 for (i = 0, j = 0; i <= mask; i++) {
1230 if (ep[i].me_value != NULL) {
1231 PyObject *key = ep[i].me_key;
1232 Py_INCREF(key);
1233 PyList_SET_ITEM(v, j, key);
1234 j++;
1235 }
1236 }
1237 assert(j == n);
1238 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001239}
1240
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001241static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001242dict_values(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001243{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 register PyObject *v;
1245 register Py_ssize_t i, j;
1246 PyDictEntry *ep;
1247 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001248
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001249 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 n = mp->ma_used;
1251 v = PyList_New(n);
1252 if (v == NULL)
1253 return NULL;
1254 if (n != mp->ma_used) {
1255 /* Durnit. The allocations caused the dict to resize.
1256 * Just start over, this shouldn't normally happen.
1257 */
1258 Py_DECREF(v);
1259 goto again;
1260 }
1261 ep = mp->ma_table;
1262 mask = mp->ma_mask;
1263 for (i = 0, j = 0; i <= mask; i++) {
1264 if (ep[i].me_value != NULL) {
1265 PyObject *value = ep[i].me_value;
1266 Py_INCREF(value);
1267 PyList_SET_ITEM(v, j, value);
1268 j++;
1269 }
1270 }
1271 assert(j == n);
1272 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001273}
1274
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001275static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001276dict_items(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001277{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 register PyObject *v;
1279 register Py_ssize_t i, j, n;
1280 Py_ssize_t mask;
1281 PyObject *item, *key, *value;
1282 PyDictEntry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 /* Preallocate the list of tuples, to avoid allocations during
1285 * the loop over the items, which could trigger GC, which
1286 * could resize the dict. :-(
1287 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001288 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 n = mp->ma_used;
1290 v = PyList_New(n);
1291 if (v == NULL)
1292 return NULL;
1293 for (i = 0; i < n; i++) {
1294 item = PyTuple_New(2);
1295 if (item == NULL) {
1296 Py_DECREF(v);
1297 return NULL;
1298 }
1299 PyList_SET_ITEM(v, i, item);
1300 }
1301 if (n != mp->ma_used) {
1302 /* Durnit. The allocations caused the dict to resize.
1303 * Just start over, this shouldn't normally happen.
1304 */
1305 Py_DECREF(v);
1306 goto again;
1307 }
1308 /* Nothing we do below makes any function calls. */
1309 ep = mp->ma_table;
1310 mask = mp->ma_mask;
1311 for (i = 0, j = 0; i <= mask; i++) {
1312 if ((value=ep[i].me_value) != NULL) {
1313 key = ep[i].me_key;
1314 item = PyList_GET_ITEM(v, j);
1315 Py_INCREF(key);
1316 PyTuple_SET_ITEM(item, 0, key);
1317 Py_INCREF(value);
1318 PyTuple_SET_ITEM(item, 1, value);
1319 j++;
1320 }
1321 }
1322 assert(j == n);
1323 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001324}
1325
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001326static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001327dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001328{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 PyObject *seq;
1330 PyObject *value = Py_None;
1331 PyObject *it; /* iter(seq) */
1332 PyObject *key;
1333 PyObject *d;
1334 int status;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
1337 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 d = PyObject_CallObject(cls, NULL);
1340 if (d == NULL)
1341 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) {
1344 PyDictObject *mp = (PyDictObject *)d;
1345 PyObject *oldvalue;
1346 Py_ssize_t pos = 0;
1347 PyObject *key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001348 Py_hash_t hash;
Guido van Rossum58da9312007-11-10 23:39:45 +00001349
Petri Lehtinena94200e2011-10-24 21:12:58 +03001350 if (dictresize(mp, Py_SIZE(seq))) {
1351 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001353 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
1356 Py_INCREF(key);
1357 Py_INCREF(value);
Petri Lehtinena94200e2011-10-24 21:12:58 +03001358 if (insertdict(mp, key, hash, value)) {
1359 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001361 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 }
1363 return d;
1364 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
1367 PyDictObject *mp = (PyDictObject *)d;
1368 Py_ssize_t pos = 0;
1369 PyObject *key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001370 Py_hash_t hash;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001371
Petri Lehtinena94200e2011-10-24 21:12:58 +03001372 if (dictresize(mp, PySet_GET_SIZE(seq))) {
1373 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001375 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
1378 Py_INCREF(key);
1379 Py_INCREF(value);
Petri Lehtinena94200e2011-10-24 21:12:58 +03001380 if (insertdict(mp, key, hash, value)) {
1381 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001383 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 }
1385 return d;
1386 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 it = PyObject_GetIter(seq);
1389 if (it == NULL){
1390 Py_DECREF(d);
1391 return NULL;
1392 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 if (PyDict_CheckExact(d)) {
1395 while ((key = PyIter_Next(it)) != NULL) {
1396 status = PyDict_SetItem(d, key, value);
1397 Py_DECREF(key);
1398 if (status < 0)
1399 goto Fail;
1400 }
1401 } else {
1402 while ((key = PyIter_Next(it)) != NULL) {
1403 status = PyObject_SetItem(d, key, value);
1404 Py_DECREF(key);
1405 if (status < 0)
1406 goto Fail;
1407 }
1408 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 if (PyErr_Occurred())
1411 goto Fail;
1412 Py_DECREF(it);
1413 return d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001414
1415Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 Py_DECREF(it);
1417 Py_DECREF(d);
1418 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001419}
1420
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001421static int
1422dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001423{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 PyObject *arg = NULL;
1425 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1428 result = -1;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001430 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001431 _Py_IDENTIFIER(keys);
1432 if (_PyObject_HasAttrId(arg, &PyId_keys))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 result = PyDict_Merge(self, arg, 1);
1434 else
1435 result = PyDict_MergeFromSeq2(self, arg, 1);
1436 }
1437 if (result == 0 && kwds != NULL) {
1438 if (PyArg_ValidateKeywordArguments(kwds))
1439 result = PyDict_Merge(self, kwds, 1);
1440 else
1441 result = -1;
1442 }
1443 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001444}
1445
1446static PyObject *
1447dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1448{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 if (dict_update_common(self, args, kwds, "update") != -1)
1450 Py_RETURN_NONE;
1451 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001452}
1453
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001454/* Update unconditionally replaces existing items.
1455 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001456 otherwise it leaves existing items unchanged.
1457
1458 PyDict_{Update,Merge} update/merge from a mapping object.
1459
Tim Petersf582b822001-12-11 18:51:08 +00001460 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001461 producing iterable objects of length 2.
1462*/
1463
Tim Petersf582b822001-12-11 18:51:08 +00001464int
Tim Peters1fc240e2001-10-26 05:06:50 +00001465PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1466{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001467 PyObject *it; /* iter(seq2) */
1468 Py_ssize_t i; /* index into seq2 of current element */
1469 PyObject *item; /* seq2[i] */
1470 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00001471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 assert(d != NULL);
1473 assert(PyDict_Check(d));
1474 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00001475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 it = PyObject_GetIter(seq2);
1477 if (it == NULL)
1478 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 for (i = 0; ; ++i) {
1481 PyObject *key, *value;
1482 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 fast = NULL;
1485 item = PyIter_Next(it);
1486 if (item == NULL) {
1487 if (PyErr_Occurred())
1488 goto Fail;
1489 break;
1490 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 /* Convert item to sequence, and verify length 2. */
1493 fast = PySequence_Fast(item, "");
1494 if (fast == NULL) {
1495 if (PyErr_ExceptionMatches(PyExc_TypeError))
1496 PyErr_Format(PyExc_TypeError,
1497 "cannot convert dictionary update "
1498 "sequence element #%zd to a sequence",
1499 i);
1500 goto Fail;
1501 }
1502 n = PySequence_Fast_GET_SIZE(fast);
1503 if (n != 2) {
1504 PyErr_Format(PyExc_ValueError,
1505 "dictionary update sequence element #%zd "
1506 "has length %zd; 2 is required",
1507 i, n);
1508 goto Fail;
1509 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511 /* Update/merge with this (key, value) pair. */
1512 key = PySequence_Fast_GET_ITEM(fast, 0);
1513 value = PySequence_Fast_GET_ITEM(fast, 1);
1514 if (override || PyDict_GetItem(d, key) == NULL) {
1515 int status = PyDict_SetItem(d, key, value);
1516 if (status < 0)
1517 goto Fail;
1518 }
1519 Py_DECREF(fast);
1520 Py_DECREF(item);
1521 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523 i = 0;
1524 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00001525Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526 Py_XDECREF(item);
1527 Py_XDECREF(fast);
1528 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001529Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 Py_DECREF(it);
1531 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001532}
1533
Tim Peters6d6c1a32001-08-02 04:15:00 +00001534int
1535PyDict_Update(PyObject *a, PyObject *b)
1536{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 return PyDict_Merge(a, b, 1);
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001538}
1539
1540int
1541PyDict_Merge(PyObject *a, PyObject *b, int override)
1542{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543 register PyDictObject *mp, *other;
1544 register Py_ssize_t i;
1545 PyDictEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001547 /* We accept for the argument either a concrete dictionary object,
1548 * or an abstract "mapping" object. For the former, we can do
1549 * things quite efficiently. For the latter, we only require that
1550 * PyMapping_Keys() and PyObject_GetItem() be supported.
1551 */
1552 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1553 PyErr_BadInternalCall();
1554 return -1;
1555 }
1556 mp = (PyDictObject*)a;
1557 if (PyDict_Check(b)) {
1558 other = (PyDictObject*)b;
1559 if (other == mp || other->ma_used == 0)
1560 /* a.update(a) or a.update({}); nothing to do */
1561 return 0;
1562 if (mp->ma_used == 0)
1563 /* Since the target dict is empty, PyDict_GetItem()
1564 * always returns NULL. Setting override to 1
1565 * skips the unnecessary test.
1566 */
1567 override = 1;
1568 /* Do one big resize at the start, rather than
1569 * incrementally resizing as we insert new items. Expect
1570 * that there will be no (or few) overlapping keys.
1571 */
1572 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
1573 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
1574 return -1;
1575 }
1576 for (i = 0; i <= other->ma_mask; i++) {
1577 entry = &other->ma_table[i];
1578 if (entry->me_value != NULL &&
1579 (override ||
1580 PyDict_GetItem(a, entry->me_key) == NULL)) {
1581 Py_INCREF(entry->me_key);
1582 Py_INCREF(entry->me_value);
1583 if (insertdict(mp, entry->me_key,
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001584 entry->me_hash,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 entry->me_value) != 0)
1586 return -1;
1587 }
1588 }
1589 }
1590 else {
1591 /* Do it the generic, slower way */
1592 PyObject *keys = PyMapping_Keys(b);
1593 PyObject *iter;
1594 PyObject *key, *value;
1595 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 if (keys == NULL)
1598 /* Docstring says this is equivalent to E.keys() so
1599 * if E doesn't have a .keys() method we want
1600 * AttributeError to percolate up. Might as well
1601 * do the same for any other error.
1602 */
1603 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 iter = PyObject_GetIter(keys);
1606 Py_DECREF(keys);
1607 if (iter == NULL)
1608 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
1611 if (!override && PyDict_GetItem(a, key) != NULL) {
1612 Py_DECREF(key);
1613 continue;
1614 }
1615 value = PyObject_GetItem(b, key);
1616 if (value == NULL) {
1617 Py_DECREF(iter);
1618 Py_DECREF(key);
1619 return -1;
1620 }
1621 status = PyDict_SetItem(a, key, value);
1622 Py_DECREF(key);
1623 Py_DECREF(value);
1624 if (status < 0) {
1625 Py_DECREF(iter);
1626 return -1;
1627 }
1628 }
1629 Py_DECREF(iter);
1630 if (PyErr_Occurred())
1631 /* Iterator completed, via error */
1632 return -1;
1633 }
1634 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001635}
1636
1637static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001638dict_copy(register PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001639{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001641}
1642
1643PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001644PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001645{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 if (o == NULL || !PyDict_Check(o)) {
1649 PyErr_BadInternalCall();
1650 return NULL;
1651 }
1652 copy = PyDict_New();
1653 if (copy == NULL)
1654 return NULL;
1655 if (PyDict_Merge(copy, o, 1) == 0)
1656 return copy;
1657 Py_DECREF(copy);
1658 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001659}
1660
Martin v. Löwis18e16552006-02-15 17:27:45 +00001661Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001662PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001663{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 if (mp == NULL || !PyDict_Check(mp)) {
1665 PyErr_BadInternalCall();
1666 return -1;
1667 }
1668 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001669}
1670
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001671PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001672PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001673{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 if (mp == NULL || !PyDict_Check(mp)) {
1675 PyErr_BadInternalCall();
1676 return NULL;
1677 }
1678 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001679}
1680
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001681PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001682PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001683{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 if (mp == NULL || !PyDict_Check(mp)) {
1685 PyErr_BadInternalCall();
1686 return NULL;
1687 }
1688 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001689}
1690
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001691PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001692PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001693{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 if (mp == NULL || !PyDict_Check(mp)) {
1695 PyErr_BadInternalCall();
1696 return NULL;
1697 }
1698 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001699}
1700
Tim Peterse63415e2001-05-08 04:38:29 +00001701/* Return 1 if dicts equal, 0 if not, -1 if error.
1702 * Gets out as soon as any difference is detected.
1703 * Uses only Py_EQ comparison.
1704 */
1705static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001706dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00001707{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00001709
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710 if (a->ma_used != b->ma_used)
1711 /* can't be equal if # of entries differ */
1712 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 /* Same # of entries -- check all of 'em. Exit early on any diff. */
1715 for (i = 0; i <= a->ma_mask; i++) {
1716 PyObject *aval = a->ma_table[i].me_value;
1717 if (aval != NULL) {
1718 int cmp;
1719 PyObject *bval;
1720 PyObject *key = a->ma_table[i].me_key;
1721 /* temporarily bump aval's refcount to ensure it stays
1722 alive until we're done with it */
1723 Py_INCREF(aval);
1724 /* ditto for key */
1725 Py_INCREF(key);
1726 bval = PyDict_GetItemWithError((PyObject *)b, key);
1727 Py_DECREF(key);
1728 if (bval == NULL) {
1729 Py_DECREF(aval);
1730 if (PyErr_Occurred())
1731 return -1;
1732 return 0;
1733 }
1734 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1735 Py_DECREF(aval);
1736 if (cmp <= 0) /* error or not equal */
1737 return cmp;
1738 }
1739 }
1740 return 1;
Tim Peterse63415e2001-05-08 04:38:29 +00001741 }
1742
1743static PyObject *
1744dict_richcompare(PyObject *v, PyObject *w, int op)
1745{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 int cmp;
1747 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00001748
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1750 res = Py_NotImplemented;
1751 }
1752 else if (op == Py_EQ || op == Py_NE) {
1753 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
1754 if (cmp < 0)
1755 return NULL;
1756 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1757 }
1758 else
1759 res = Py_NotImplemented;
1760 Py_INCREF(res);
1761 return res;
Tim Peterse63415e2001-05-08 04:38:29 +00001762 }
1763
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001764static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001765dict_contains(register PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001766{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001767 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001771 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 hash = PyObject_Hash(key);
1773 if (hash == -1)
1774 return NULL;
1775 }
1776 ep = (mp->ma_lookup)(mp, key, hash);
1777 if (ep == NULL)
1778 return NULL;
1779 return PyBool_FromLong(ep->me_value != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001780}
1781
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001782static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001783dict_get(register PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001784{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 PyObject *key;
1786 PyObject *failobj = Py_None;
1787 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001788 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789 PyDictEntry *ep;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
1792 return NULL;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001794 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001795 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001796 hash = PyObject_Hash(key);
1797 if (hash == -1)
1798 return NULL;
1799 }
1800 ep = (mp->ma_lookup)(mp, key, hash);
1801 if (ep == NULL)
1802 return NULL;
1803 val = ep->me_value;
1804 if (val == NULL)
1805 val = failobj;
1806 Py_INCREF(val);
1807 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001808}
1809
1810
1811static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001812dict_setdefault(register PyDictObject *mp, PyObject *args)
Guido van Rossum164452c2000-08-08 16:12:54 +00001813{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 PyObject *key;
1815 PyObject *failobj = Py_None;
1816 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001817 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 PyDictEntry *ep;
Guido van Rossum164452c2000-08-08 16:12:54 +00001819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001820 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
1821 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001823 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001824 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 hash = PyObject_Hash(key);
1826 if (hash == -1)
1827 return NULL;
1828 }
1829 ep = (mp->ma_lookup)(mp, key, hash);
1830 if (ep == NULL)
1831 return NULL;
1832 val = ep->me_value;
1833 if (val == NULL) {
Antoine Pitroue965d972012-02-27 00:45:12 +01001834 if (dict_set_item_by_hash_or_entry((PyObject*)mp, key, hash, ep,
1835 failobj) == 0)
1836 val = failobj;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 }
1838 Py_XINCREF(val);
1839 return val;
Guido van Rossum164452c2000-08-08 16:12:54 +00001840}
1841
1842
1843static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001844dict_clear(register PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001845{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001846 PyDict_Clear((PyObject *)mp);
1847 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001848}
1849
Guido van Rossumba6ab842000-12-12 22:02:18 +00001850static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001851dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00001852{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001853 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854 PyDictEntry *ep;
1855 PyObject *old_value, *old_key;
1856 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1859 return NULL;
1860 if (mp->ma_used == 0) {
1861 if (deflt) {
1862 Py_INCREF(deflt);
1863 return deflt;
1864 }
Raymond Hettingerdd421542010-10-30 08:10:29 +00001865 set_key_error(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 return NULL;
1867 }
1868 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001869 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001870 hash = PyObject_Hash(key);
1871 if (hash == -1)
1872 return NULL;
1873 }
1874 ep = (mp->ma_lookup)(mp, key, hash);
1875 if (ep == NULL)
1876 return NULL;
1877 if (ep->me_value == NULL) {
1878 if (deflt) {
1879 Py_INCREF(deflt);
1880 return deflt;
1881 }
1882 set_key_error(key);
1883 return NULL;
1884 }
1885 old_key = ep->me_key;
1886 Py_INCREF(dummy);
1887 ep->me_key = dummy;
1888 old_value = ep->me_value;
1889 ep->me_value = NULL;
1890 mp->ma_used--;
1891 Py_DECREF(old_key);
1892 return old_value;
Guido van Rossume027d982002-04-12 15:11:59 +00001893}
1894
1895static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001896dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001897{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001898 Py_hash_t i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 PyDictEntry *ep;
1900 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 /* Allocate the result tuple before checking the size. Believe it
1903 * or not, this allocation could trigger a garbage collection which
1904 * could empty the dict, so if we checked the size first and that
1905 * happened, the result would be an infinite loop (searching for an
1906 * entry that no longer exists). Note that the usual popitem()
1907 * idiom is "while d: k, v = d.popitem()". so needing to throw the
1908 * tuple away if the dict *is* empty isn't a significant
1909 * inefficiency -- possible, but unlikely in practice.
1910 */
1911 res = PyTuple_New(2);
1912 if (res == NULL)
1913 return NULL;
1914 if (mp->ma_used == 0) {
1915 Py_DECREF(res);
1916 PyErr_SetString(PyExc_KeyError,
1917 "popitem(): dictionary is empty");
1918 return NULL;
1919 }
1920 /* Set ep to "the first" dict entry with a value. We abuse the hash
1921 * field of slot 0 to hold a search finger:
1922 * If slot 0 has a value, use slot 0.
1923 * Else slot 0 is being used to hold a search finger,
1924 * and we use its hash value as the first index to look.
1925 */
1926 ep = &mp->ma_table[0];
1927 if (ep->me_value == NULL) {
1928 i = ep->me_hash;
1929 /* The hash field may be a real hash value, or it may be a
1930 * legit search finger, or it may be a once-legit search
1931 * finger that's out of bounds now because it wrapped around
1932 * or the table shrunk -- simply make sure it's in bounds now.
1933 */
1934 if (i > mp->ma_mask || i < 1)
1935 i = 1; /* skip slot 0 */
1936 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1937 i++;
1938 if (i > mp->ma_mask)
1939 i = 1;
1940 }
1941 }
1942 PyTuple_SET_ITEM(res, 0, ep->me_key);
1943 PyTuple_SET_ITEM(res, 1, ep->me_value);
1944 Py_INCREF(dummy);
1945 ep->me_key = dummy;
1946 ep->me_value = NULL;
1947 mp->ma_used--;
1948 assert(mp->ma_table[0].me_value == NULL);
1949 mp->ma_table[0].me_hash = i + 1; /* next place to start */
1950 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001951}
1952
Jeremy Hylton8caad492000-06-23 14:18:11 +00001953static int
1954dict_traverse(PyObject *op, visitproc visit, void *arg)
1955{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 Py_ssize_t i = 0;
1957 PyObject *pk;
1958 PyObject *pv;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 while (PyDict_Next(op, &i, &pk, &pv)) {
1961 Py_VISIT(pk);
1962 Py_VISIT(pv);
1963 }
1964 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001965}
1966
1967static int
1968dict_tp_clear(PyObject *op)
1969{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001970 PyDict_Clear(op);
1971 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001972}
1973
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001974static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001975
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001976static PyObject *
1977dict_sizeof(PyDictObject *mp)
1978{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 Py_ssize_t res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 res = sizeof(PyDictObject);
1982 if (mp->ma_table != mp->ma_smalltable)
1983 res = res + (mp->ma_mask + 1) * sizeof(PyDictEntry);
1984 return PyLong_FromSsize_t(res);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001985}
1986
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001987PyDoc_STRVAR(contains__doc__,
1988"D.__contains__(k) -> True if D has a key k, else False");
1989
1990PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
1991
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001992PyDoc_STRVAR(sizeof__doc__,
1993"D.__sizeof__() -> size of D in memory, in bytes");
1994
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001995PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001996"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001997
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001998PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001999"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 +00002000
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002001PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002002"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002003If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00002004
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002005PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00002006"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000020072-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002008
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002009PyDoc_STRVAR(update__doc__,
Georg Brandlac0675c2011-12-18 19:30:55 +01002010"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n"
2011"If E present and has a .keys() method, does: for k in E: D[k] = E[k]\n\
2012If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002013In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00002014
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002015PyDoc_STRVAR(fromkeys__doc__,
2016"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
2017v defaults to None.");
2018
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002019PyDoc_STRVAR(clear__doc__,
2020"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002021
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002022PyDoc_STRVAR(copy__doc__,
2023"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00002024
Guido van Rossumb90c8482007-02-10 01:11:45 +00002025/* Forward */
2026static PyObject *dictkeys_new(PyObject *);
2027static PyObject *dictitems_new(PyObject *);
2028static PyObject *dictvalues_new(PyObject *);
2029
Guido van Rossum45c85d12007-07-27 16:31:40 +00002030PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002031 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002032PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002033 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002034PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002035 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00002036
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002037static PyMethodDef mapp_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002038 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
2039 contains__doc__},
2040 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
2041 getitem__doc__},
2042 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
2043 sizeof__doc__},
2044 {"get", (PyCFunction)dict_get, METH_VARARGS,
2045 get__doc__},
2046 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
2047 setdefault_doc__},
2048 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
2049 pop__doc__},
2050 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
2051 popitem__doc__},
2052 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
2053 keys__doc__},
2054 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
2055 items__doc__},
2056 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
2057 values__doc__},
2058 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
2059 update__doc__},
2060 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
2061 fromkeys__doc__},
2062 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
2063 clear__doc__},
2064 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
2065 copy__doc__},
2066 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002067};
2068
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002069/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002070int
2071PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002072{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002073 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002074 PyDictObject *mp = (PyDictObject *)op;
2075 PyDictEntry *ep;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002078 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002079 hash = PyObject_Hash(key);
2080 if (hash == -1)
2081 return -1;
2082 }
2083 ep = (mp->ma_lookup)(mp, key, hash);
2084 return ep == NULL ? -1 : (ep->me_value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002085}
2086
Thomas Wouterscf297e42007-02-23 15:07:44 +00002087/* Internal version of PyDict_Contains used when the hash value is already known */
2088int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002089_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00002090{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002091 PyDictObject *mp = (PyDictObject *)op;
2092 PyDictEntry *ep;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002094 ep = (mp->ma_lookup)(mp, key, hash);
2095 return ep == NULL ? -1 : (ep->me_value != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00002096}
2097
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002098/* Hack to implement "key in dict" */
2099static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002100 0, /* sq_length */
2101 0, /* sq_concat */
2102 0, /* sq_repeat */
2103 0, /* sq_item */
2104 0, /* sq_slice */
2105 0, /* sq_ass_item */
2106 0, /* sq_ass_slice */
2107 PyDict_Contains, /* sq_contains */
2108 0, /* sq_inplace_concat */
2109 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002110};
2111
Guido van Rossum09e563a2001-05-01 12:10:21 +00002112static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002113dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2114{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002115 PyObject *self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002117 assert(type != NULL && type->tp_alloc != NULL);
2118 self = type->tp_alloc(type, 0);
2119 if (self != NULL) {
2120 PyDictObject *d = (PyDictObject *)self;
2121 /* It's guaranteed that tp->alloc zeroed out the struct. */
2122 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
2123 INIT_NONZERO_DICT_SLOTS(d);
2124 d->ma_lookup = lookdict_unicode;
Ezio Melotti13925002011-03-16 11:05:33 +02002125 /* The object has been implicitly tracked by tp_alloc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002126 if (type == &PyDict_Type)
2127 _PyObject_GC_UNTRACK(d);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002128#ifdef SHOW_CONVERSION_COUNTS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002129 ++created;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002130#endif
Antoine Pitrou3a652b12009-03-23 18:52:06 +00002131#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002132 if (_PyObject_GC_IS_TRACKED(d))
2133 count_tracked++;
2134 else
2135 count_untracked++;
Antoine Pitrou3a652b12009-03-23 18:52:06 +00002136#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002137 }
2138 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002139}
2140
Tim Peters25786c02001-09-02 08:22:48 +00002141static int
2142dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002144 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002145}
2146
Tim Peters6d6c1a32001-08-02 04:15:00 +00002147static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002148dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002149{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002150 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002151}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002152
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002153PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002154"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002155"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002156" (key, value) pairs\n"
2157"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002158" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002159" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002160" d[k] = v\n"
2161"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2162" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002163
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002164PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002165 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2166 "dict",
2167 sizeof(PyDictObject),
2168 0,
2169 (destructor)dict_dealloc, /* tp_dealloc */
2170 0, /* tp_print */
2171 0, /* tp_getattr */
2172 0, /* tp_setattr */
2173 0, /* tp_reserved */
2174 (reprfunc)dict_repr, /* tp_repr */
2175 0, /* tp_as_number */
2176 &dict_as_sequence, /* tp_as_sequence */
2177 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002178 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002179 0, /* tp_call */
2180 0, /* tp_str */
2181 PyObject_GenericGetAttr, /* tp_getattro */
2182 0, /* tp_setattro */
2183 0, /* tp_as_buffer */
2184 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2185 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
2186 dictionary_doc, /* tp_doc */
2187 dict_traverse, /* tp_traverse */
2188 dict_tp_clear, /* tp_clear */
2189 dict_richcompare, /* tp_richcompare */
2190 0, /* tp_weaklistoffset */
2191 (getiterfunc)dict_iter, /* tp_iter */
2192 0, /* tp_iternext */
2193 mapp_methods, /* tp_methods */
2194 0, /* tp_members */
2195 0, /* tp_getset */
2196 0, /* tp_base */
2197 0, /* tp_dict */
2198 0, /* tp_descr_get */
2199 0, /* tp_descr_set */
2200 0, /* tp_dictoffset */
2201 dict_init, /* tp_init */
2202 PyType_GenericAlloc, /* tp_alloc */
2203 dict_new, /* tp_new */
2204 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002205};
2206
Guido van Rossum3cca2451997-05-16 14:23:33 +00002207/* For backward compatibility with old dictionary interface */
2208
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002209PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002210PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002211{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002212 PyObject *kv, *rv;
2213 kv = PyUnicode_FromString(key);
2214 if (kv == NULL)
2215 return NULL;
2216 rv = PyDict_GetItem(v, kv);
2217 Py_DECREF(kv);
2218 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002219}
2220
2221int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002222PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002223{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224 PyObject *kv;
2225 int err;
2226 kv = PyUnicode_FromString(key);
2227 if (kv == NULL)
2228 return -1;
2229 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
2230 err = PyDict_SetItem(v, kv, item);
2231 Py_DECREF(kv);
2232 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002233}
2234
2235int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002236PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002237{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002238 PyObject *kv;
2239 int err;
2240 kv = PyUnicode_FromString(key);
2241 if (kv == NULL)
2242 return -1;
2243 err = PyDict_DelItem(v, kv);
2244 Py_DECREF(kv);
2245 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002246}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002247
Raymond Hettinger019a1482004-03-18 02:41:19 +00002248/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002249
2250typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002251 PyObject_HEAD
2252 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
2253 Py_ssize_t di_used;
2254 Py_ssize_t di_pos;
2255 PyObject* di_result; /* reusable result tuple for iteritems */
2256 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002257} dictiterobject;
2258
2259static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002260dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002261{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262 dictiterobject *di;
2263 di = PyObject_GC_New(dictiterobject, itertype);
2264 if (di == NULL)
2265 return NULL;
2266 Py_INCREF(dict);
2267 di->di_dict = dict;
2268 di->di_used = dict->ma_used;
2269 di->di_pos = 0;
2270 di->len = dict->ma_used;
2271 if (itertype == &PyDictIterItem_Type) {
2272 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2273 if (di->di_result == NULL) {
2274 Py_DECREF(di);
2275 return NULL;
2276 }
2277 }
2278 else
2279 di->di_result = NULL;
2280 _PyObject_GC_TRACK(di);
2281 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002282}
2283
2284static void
2285dictiter_dealloc(dictiterobject *di)
2286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287 Py_XDECREF(di->di_dict);
2288 Py_XDECREF(di->di_result);
2289 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002290}
2291
2292static int
2293dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
2294{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002295 Py_VISIT(di->di_dict);
2296 Py_VISIT(di->di_result);
2297 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002298}
2299
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002300static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002301dictiter_len(dictiterobject *di)
2302{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002303 Py_ssize_t len = 0;
2304 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
2305 len = di->len;
2306 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002307}
2308
Guido van Rossumb90c8482007-02-10 01:11:45 +00002309PyDoc_STRVAR(length_hint_doc,
2310 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002311
2312static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002313 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
2314 length_hint_doc},
2315 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002316};
2317
Raymond Hettinger019a1482004-03-18 02:41:19 +00002318static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002319{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002320 PyObject *key;
2321 register Py_ssize_t i, mask;
2322 register PyDictEntry *ep;
2323 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002325 if (d == NULL)
2326 return NULL;
2327 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002329 if (di->di_used != d->ma_used) {
2330 PyErr_SetString(PyExc_RuntimeError,
2331 "dictionary changed size during iteration");
2332 di->di_used = -1; /* Make this state sticky */
2333 return NULL;
2334 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 i = di->di_pos;
2337 if (i < 0)
2338 goto fail;
2339 ep = d->ma_table;
2340 mask = d->ma_mask;
2341 while (i <= mask && ep[i].me_value == NULL)
2342 i++;
2343 di->di_pos = i+1;
2344 if (i > mask)
2345 goto fail;
2346 di->len--;
2347 key = ep[i].me_key;
2348 Py_INCREF(key);
2349 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002350
2351fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 Py_DECREF(d);
2353 di->di_dict = NULL;
2354 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002355}
2356
Raymond Hettinger019a1482004-03-18 02:41:19 +00002357PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2359 "dict_keyiterator", /* tp_name */
2360 sizeof(dictiterobject), /* tp_basicsize */
2361 0, /* tp_itemsize */
2362 /* methods */
2363 (destructor)dictiter_dealloc, /* tp_dealloc */
2364 0, /* tp_print */
2365 0, /* tp_getattr */
2366 0, /* tp_setattr */
2367 0, /* tp_reserved */
2368 0, /* tp_repr */
2369 0, /* tp_as_number */
2370 0, /* tp_as_sequence */
2371 0, /* tp_as_mapping */
2372 0, /* tp_hash */
2373 0, /* tp_call */
2374 0, /* tp_str */
2375 PyObject_GenericGetAttr, /* tp_getattro */
2376 0, /* tp_setattro */
2377 0, /* tp_as_buffer */
2378 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2379 0, /* tp_doc */
2380 (traverseproc)dictiter_traverse, /* tp_traverse */
2381 0, /* tp_clear */
2382 0, /* tp_richcompare */
2383 0, /* tp_weaklistoffset */
2384 PyObject_SelfIter, /* tp_iter */
2385 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
2386 dictiter_methods, /* tp_methods */
2387 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002388};
2389
2390static PyObject *dictiter_iternextvalue(dictiterobject *di)
2391{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392 PyObject *value;
2393 register Py_ssize_t i, mask;
2394 register PyDictEntry *ep;
2395 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002397 if (d == NULL)
2398 return NULL;
2399 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 if (di->di_used != d->ma_used) {
2402 PyErr_SetString(PyExc_RuntimeError,
2403 "dictionary changed size during iteration");
2404 di->di_used = -1; /* Make this state sticky */
2405 return NULL;
2406 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002408 i = di->di_pos;
2409 mask = d->ma_mask;
2410 if (i < 0 || i > mask)
2411 goto fail;
2412 ep = d->ma_table;
2413 while ((value=ep[i].me_value) == NULL) {
2414 i++;
2415 if (i > mask)
2416 goto fail;
2417 }
2418 di->di_pos = i+1;
2419 di->len--;
2420 Py_INCREF(value);
2421 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002422
2423fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002424 Py_DECREF(d);
2425 di->di_dict = NULL;
2426 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002427}
2428
2429PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002430 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2431 "dict_valueiterator", /* tp_name */
2432 sizeof(dictiterobject), /* tp_basicsize */
2433 0, /* tp_itemsize */
2434 /* methods */
2435 (destructor)dictiter_dealloc, /* tp_dealloc */
2436 0, /* tp_print */
2437 0, /* tp_getattr */
2438 0, /* tp_setattr */
2439 0, /* tp_reserved */
2440 0, /* tp_repr */
2441 0, /* tp_as_number */
2442 0, /* tp_as_sequence */
2443 0, /* tp_as_mapping */
2444 0, /* tp_hash */
2445 0, /* tp_call */
2446 0, /* tp_str */
2447 PyObject_GenericGetAttr, /* tp_getattro */
2448 0, /* tp_setattro */
2449 0, /* tp_as_buffer */
2450 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2451 0, /* tp_doc */
2452 (traverseproc)dictiter_traverse, /* tp_traverse */
2453 0, /* tp_clear */
2454 0, /* tp_richcompare */
2455 0, /* tp_weaklistoffset */
2456 PyObject_SelfIter, /* tp_iter */
2457 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
2458 dictiter_methods, /* tp_methods */
2459 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002460};
2461
2462static PyObject *dictiter_iternextitem(dictiterobject *di)
2463{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002464 PyObject *key, *value, *result = di->di_result;
2465 register Py_ssize_t i, mask;
2466 register PyDictEntry *ep;
2467 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002469 if (d == NULL)
2470 return NULL;
2471 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002473 if (di->di_used != d->ma_used) {
2474 PyErr_SetString(PyExc_RuntimeError,
2475 "dictionary changed size during iteration");
2476 di->di_used = -1; /* Make this state sticky */
2477 return NULL;
2478 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002480 i = di->di_pos;
2481 if (i < 0)
2482 goto fail;
2483 ep = d->ma_table;
2484 mask = d->ma_mask;
2485 while (i <= mask && ep[i].me_value == NULL)
2486 i++;
2487 di->di_pos = i+1;
2488 if (i > mask)
2489 goto fail;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002491 if (result->ob_refcnt == 1) {
2492 Py_INCREF(result);
2493 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2494 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2495 } else {
2496 result = PyTuple_New(2);
2497 if (result == NULL)
2498 return NULL;
2499 }
2500 di->len--;
2501 key = ep[i].me_key;
2502 value = ep[i].me_value;
2503 Py_INCREF(key);
2504 Py_INCREF(value);
2505 PyTuple_SET_ITEM(result, 0, key);
2506 PyTuple_SET_ITEM(result, 1, value);
2507 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002508
2509fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002510 Py_DECREF(d);
2511 di->di_dict = NULL;
2512 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002513}
2514
2515PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002516 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2517 "dict_itemiterator", /* tp_name */
2518 sizeof(dictiterobject), /* tp_basicsize */
2519 0, /* tp_itemsize */
2520 /* methods */
2521 (destructor)dictiter_dealloc, /* tp_dealloc */
2522 0, /* tp_print */
2523 0, /* tp_getattr */
2524 0, /* tp_setattr */
2525 0, /* tp_reserved */
2526 0, /* tp_repr */
2527 0, /* tp_as_number */
2528 0, /* tp_as_sequence */
2529 0, /* tp_as_mapping */
2530 0, /* tp_hash */
2531 0, /* tp_call */
2532 0, /* tp_str */
2533 PyObject_GenericGetAttr, /* tp_getattro */
2534 0, /* tp_setattro */
2535 0, /* tp_as_buffer */
2536 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2537 0, /* tp_doc */
2538 (traverseproc)dictiter_traverse, /* tp_traverse */
2539 0, /* tp_clear */
2540 0, /* tp_richcompare */
2541 0, /* tp_weaklistoffset */
2542 PyObject_SelfIter, /* tp_iter */
2543 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
2544 dictiter_methods, /* tp_methods */
2545 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002546};
Guido van Rossumb90c8482007-02-10 01:11:45 +00002547
2548
Guido van Rossum3ac67412007-02-10 18:55:06 +00002549/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002550/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002551/***********************************************/
2552
Guido van Rossumb90c8482007-02-10 01:11:45 +00002553/* The instance lay-out is the same for all three; but the type differs. */
2554
2555typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002556 PyObject_HEAD
2557 PyDictObject *dv_dict;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002558} dictviewobject;
2559
2560
2561static void
Guido van Rossum3ac67412007-02-10 18:55:06 +00002562dictview_dealloc(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002563{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002564 Py_XDECREF(dv->dv_dict);
2565 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002566}
2567
2568static int
2569dictview_traverse(dictviewobject *dv, visitproc visit, void *arg)
2570{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002571 Py_VISIT(dv->dv_dict);
2572 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002573}
2574
Guido van Rossum83825ac2007-02-10 04:54:19 +00002575static Py_ssize_t
Guido van Rossum3ac67412007-02-10 18:55:06 +00002576dictview_len(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002577{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002578 Py_ssize_t len = 0;
2579 if (dv->dv_dict != NULL)
2580 len = dv->dv_dict->ma_used;
2581 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002582}
2583
2584static PyObject *
2585dictview_new(PyObject *dict, PyTypeObject *type)
2586{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002587 dictviewobject *dv;
2588 if (dict == NULL) {
2589 PyErr_BadInternalCall();
2590 return NULL;
2591 }
2592 if (!PyDict_Check(dict)) {
2593 /* XXX Get rid of this restriction later */
2594 PyErr_Format(PyExc_TypeError,
2595 "%s() requires a dict argument, not '%s'",
2596 type->tp_name, dict->ob_type->tp_name);
2597 return NULL;
2598 }
2599 dv = PyObject_GC_New(dictviewobject, type);
2600 if (dv == NULL)
2601 return NULL;
2602 Py_INCREF(dict);
2603 dv->dv_dict = (PyDictObject *)dict;
2604 _PyObject_GC_TRACK(dv);
2605 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002606}
2607
Neal Norwitze36f2ba2007-02-26 23:12:28 +00002608/* TODO(guido): The views objects are not complete:
2609
2610 * support more set operations
2611 * support arbitrary mappings?
2612 - either these should be static or exported in dictobject.h
2613 - if public then they should probably be in builtins
2614*/
2615
Guido van Rossumaac530c2007-08-24 22:33:45 +00002616/* Return 1 if self is a subset of other, iterating over self;
2617 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002618static int
2619all_contained_in(PyObject *self, PyObject *other)
2620{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002621 PyObject *iter = PyObject_GetIter(self);
2622 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00002623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002624 if (iter == NULL)
2625 return -1;
2626 for (;;) {
2627 PyObject *next = PyIter_Next(iter);
2628 if (next == NULL) {
2629 if (PyErr_Occurred())
2630 ok = -1;
2631 break;
2632 }
2633 ok = PySequence_Contains(other, next);
2634 Py_DECREF(next);
2635 if (ok <= 0)
2636 break;
2637 }
2638 Py_DECREF(iter);
2639 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00002640}
2641
2642static PyObject *
2643dictview_richcompare(PyObject *self, PyObject *other, int op)
2644{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645 Py_ssize_t len_self, len_other;
2646 int ok;
2647 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002649 assert(self != NULL);
2650 assert(PyDictViewSet_Check(self));
2651 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00002652
Brian Curtindfc80e32011-08-10 20:28:54 -05002653 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
2654 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002656 len_self = PyObject_Size(self);
2657 if (len_self < 0)
2658 return NULL;
2659 len_other = PyObject_Size(other);
2660 if (len_other < 0)
2661 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002663 ok = 0;
2664 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00002665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002666 case Py_NE:
2667 case Py_EQ:
2668 if (len_self == len_other)
2669 ok = all_contained_in(self, other);
2670 if (op == Py_NE && ok >= 0)
2671 ok = !ok;
2672 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002674 case Py_LT:
2675 if (len_self < len_other)
2676 ok = all_contained_in(self, other);
2677 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002679 case Py_LE:
2680 if (len_self <= len_other)
2681 ok = all_contained_in(self, other);
2682 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002684 case Py_GT:
2685 if (len_self > len_other)
2686 ok = all_contained_in(other, self);
2687 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002689 case Py_GE:
2690 if (len_self >= len_other)
2691 ok = all_contained_in(other, self);
2692 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002694 }
2695 if (ok < 0)
2696 return NULL;
2697 result = ok ? Py_True : Py_False;
2698 Py_INCREF(result);
2699 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00002700}
2701
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00002702static PyObject *
2703dictview_repr(dictviewobject *dv)
2704{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002705 PyObject *seq;
2706 PyObject *result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00002707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002708 seq = PySequence_List((PyObject *)dv);
2709 if (seq == NULL)
2710 return NULL;
2711
2712 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
2713 Py_DECREF(seq);
2714 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00002715}
2716
Guido van Rossum3ac67412007-02-10 18:55:06 +00002717/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002718
2719static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002720dictkeys_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002721{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002722 if (dv->dv_dict == NULL) {
2723 Py_RETURN_NONE;
2724 }
2725 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00002726}
2727
2728static int
2729dictkeys_contains(dictviewobject *dv, PyObject *obj)
2730{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002731 if (dv->dv_dict == NULL)
2732 return 0;
2733 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002734}
2735
Guido van Rossum83825ac2007-02-10 04:54:19 +00002736static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002737 (lenfunc)dictview_len, /* sq_length */
2738 0, /* sq_concat */
2739 0, /* sq_repeat */
2740 0, /* sq_item */
2741 0, /* sq_slice */
2742 0, /* sq_ass_item */
2743 0, /* sq_ass_slice */
2744 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002745};
2746
Guido van Rossum523259b2007-08-24 23:41:22 +00002747static PyObject*
2748dictviews_sub(PyObject* self, PyObject *other)
2749{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002750 PyObject *result = PySet_New(self);
2751 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002752 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002754 if (result == NULL)
2755 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00002756
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002757 tmp = _PyObject_CallMethodId(result, &PyId_difference_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002758 if (tmp == NULL) {
2759 Py_DECREF(result);
2760 return NULL;
2761 }
Guido van Rossum523259b2007-08-24 23:41:22 +00002762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002763 Py_DECREF(tmp);
2764 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00002765}
2766
2767static PyObject*
2768dictviews_and(PyObject* self, PyObject *other)
2769{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002770 PyObject *result = PySet_New(self);
2771 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002772 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002774 if (result == NULL)
2775 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00002776
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002777 tmp = _PyObject_CallMethodId(result, &PyId_intersection_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002778 if (tmp == NULL) {
2779 Py_DECREF(result);
2780 return NULL;
2781 }
Guido van Rossum523259b2007-08-24 23:41:22 +00002782
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002783 Py_DECREF(tmp);
2784 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00002785}
2786
2787static PyObject*
2788dictviews_or(PyObject* self, PyObject *other)
2789{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002790 PyObject *result = PySet_New(self);
2791 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002792 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02002793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002794 if (result == NULL)
2795 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00002796
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02002797 tmp = _PyObject_CallMethodId(result, &PyId_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002798 if (tmp == NULL) {
2799 Py_DECREF(result);
2800 return NULL;
2801 }
Guido van Rossum523259b2007-08-24 23:41:22 +00002802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002803 Py_DECREF(tmp);
2804 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00002805}
2806
2807static PyObject*
2808dictviews_xor(PyObject* self, PyObject *other)
2809{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002810 PyObject *result = PySet_New(self);
2811 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002812 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002814 if (result == NULL)
2815 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00002816
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002817 tmp = _PyObject_CallMethodId(result, &PyId_symmetric_difference_update, "O",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002818 other);
2819 if (tmp == NULL) {
2820 Py_DECREF(result);
2821 return NULL;
2822 }
Guido van Rossum523259b2007-08-24 23:41:22 +00002823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002824 Py_DECREF(tmp);
2825 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00002826}
2827
2828static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002829 0, /*nb_add*/
2830 (binaryfunc)dictviews_sub, /*nb_subtract*/
2831 0, /*nb_multiply*/
2832 0, /*nb_remainder*/
2833 0, /*nb_divmod*/
2834 0, /*nb_power*/
2835 0, /*nb_negative*/
2836 0, /*nb_positive*/
2837 0, /*nb_absolute*/
2838 0, /*nb_bool*/
2839 0, /*nb_invert*/
2840 0, /*nb_lshift*/
2841 0, /*nb_rshift*/
2842 (binaryfunc)dictviews_and, /*nb_and*/
2843 (binaryfunc)dictviews_xor, /*nb_xor*/
2844 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00002845};
2846
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00002847static PyObject*
2848dictviews_isdisjoint(PyObject *self, PyObject *other)
2849{
2850 PyObject *it;
2851 PyObject *item = NULL;
2852
2853 if (self == other) {
2854 if (dictview_len((dictviewobject *)self) == 0)
2855 Py_RETURN_TRUE;
2856 else
2857 Py_RETURN_FALSE;
2858 }
2859
2860 /* Iterate over the shorter object (only if other is a set,
2861 * because PySequence_Contains may be expensive otherwise): */
2862 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
2863 Py_ssize_t len_self = dictview_len((dictviewobject *)self);
2864 Py_ssize_t len_other = PyObject_Size(other);
2865 if (len_other == -1)
2866 return NULL;
2867
2868 if ((len_other > len_self)) {
2869 PyObject *tmp = other;
2870 other = self;
2871 self = tmp;
2872 }
2873 }
2874
2875 it = PyObject_GetIter(other);
2876 if (it == NULL)
2877 return NULL;
2878
2879 while ((item = PyIter_Next(it)) != NULL) {
2880 int contains = PySequence_Contains(self, item);
2881 Py_DECREF(item);
2882 if (contains == -1) {
2883 Py_DECREF(it);
2884 return NULL;
2885 }
2886
2887 if (contains) {
2888 Py_DECREF(it);
2889 Py_RETURN_FALSE;
2890 }
2891 }
2892 Py_DECREF(it);
2893 if (PyErr_Occurred())
2894 return NULL; /* PyIter_Next raised an exception. */
2895 Py_RETURN_TRUE;
2896}
2897
2898PyDoc_STRVAR(isdisjoint_doc,
2899"Return True if the view and the given iterable have a null intersection.");
2900
Guido van Rossumb90c8482007-02-10 01:11:45 +00002901static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00002902 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
2903 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002904 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002905};
2906
2907PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2909 "dict_keys", /* tp_name */
2910 sizeof(dictviewobject), /* tp_basicsize */
2911 0, /* tp_itemsize */
2912 /* methods */
2913 (destructor)dictview_dealloc, /* tp_dealloc */
2914 0, /* tp_print */
2915 0, /* tp_getattr */
2916 0, /* tp_setattr */
2917 0, /* tp_reserved */
2918 (reprfunc)dictview_repr, /* tp_repr */
2919 &dictviews_as_number, /* tp_as_number */
2920 &dictkeys_as_sequence, /* tp_as_sequence */
2921 0, /* tp_as_mapping */
2922 0, /* tp_hash */
2923 0, /* tp_call */
2924 0, /* tp_str */
2925 PyObject_GenericGetAttr, /* tp_getattro */
2926 0, /* tp_setattro */
2927 0, /* tp_as_buffer */
2928 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2929 0, /* tp_doc */
2930 (traverseproc)dictview_traverse, /* tp_traverse */
2931 0, /* tp_clear */
2932 dictview_richcompare, /* tp_richcompare */
2933 0, /* tp_weaklistoffset */
2934 (getiterfunc)dictkeys_iter, /* tp_iter */
2935 0, /* tp_iternext */
2936 dictkeys_methods, /* tp_methods */
2937 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00002938};
2939
2940static PyObject *
2941dictkeys_new(PyObject *dict)
2942{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002943 return dictview_new(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002944}
2945
Guido van Rossum3ac67412007-02-10 18:55:06 +00002946/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002947
2948static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002949dictitems_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002950{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002951 if (dv->dv_dict == NULL) {
2952 Py_RETURN_NONE;
2953 }
2954 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00002955}
2956
2957static int
2958dictitems_contains(dictviewobject *dv, PyObject *obj)
2959{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002960 PyObject *key, *value, *found;
2961 if (dv->dv_dict == NULL)
2962 return 0;
2963 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
2964 return 0;
2965 key = PyTuple_GET_ITEM(obj, 0);
2966 value = PyTuple_GET_ITEM(obj, 1);
2967 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
2968 if (found == NULL) {
2969 if (PyErr_Occurred())
2970 return -1;
2971 return 0;
2972 }
2973 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002974}
2975
Guido van Rossum83825ac2007-02-10 04:54:19 +00002976static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002977 (lenfunc)dictview_len, /* sq_length */
2978 0, /* sq_concat */
2979 0, /* sq_repeat */
2980 0, /* sq_item */
2981 0, /* sq_slice */
2982 0, /* sq_ass_item */
2983 0, /* sq_ass_slice */
2984 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002985};
2986
Guido van Rossumb90c8482007-02-10 01:11:45 +00002987static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00002988 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
2989 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002990 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002991};
2992
2993PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002994 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2995 "dict_items", /* tp_name */
2996 sizeof(dictviewobject), /* tp_basicsize */
2997 0, /* tp_itemsize */
2998 /* methods */
2999 (destructor)dictview_dealloc, /* tp_dealloc */
3000 0, /* tp_print */
3001 0, /* tp_getattr */
3002 0, /* tp_setattr */
3003 0, /* tp_reserved */
3004 (reprfunc)dictview_repr, /* tp_repr */
3005 &dictviews_as_number, /* tp_as_number */
3006 &dictitems_as_sequence, /* tp_as_sequence */
3007 0, /* tp_as_mapping */
3008 0, /* tp_hash */
3009 0, /* tp_call */
3010 0, /* tp_str */
3011 PyObject_GenericGetAttr, /* tp_getattro */
3012 0, /* tp_setattro */
3013 0, /* tp_as_buffer */
3014 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3015 0, /* tp_doc */
3016 (traverseproc)dictview_traverse, /* tp_traverse */
3017 0, /* tp_clear */
3018 dictview_richcompare, /* tp_richcompare */
3019 0, /* tp_weaklistoffset */
3020 (getiterfunc)dictitems_iter, /* tp_iter */
3021 0, /* tp_iternext */
3022 dictitems_methods, /* tp_methods */
3023 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003024};
3025
3026static PyObject *
3027dictitems_new(PyObject *dict)
3028{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003029 return dictview_new(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003030}
3031
Guido van Rossum3ac67412007-02-10 18:55:06 +00003032/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003033
3034static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003035dictvalues_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003036{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003037 if (dv->dv_dict == NULL) {
3038 Py_RETURN_NONE;
3039 }
3040 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003041}
3042
Guido van Rossum83825ac2007-02-10 04:54:19 +00003043static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003044 (lenfunc)dictview_len, /* sq_length */
3045 0, /* sq_concat */
3046 0, /* sq_repeat */
3047 0, /* sq_item */
3048 0, /* sq_slice */
3049 0, /* sq_ass_item */
3050 0, /* sq_ass_slice */
3051 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003052};
3053
Guido van Rossumb90c8482007-02-10 01:11:45 +00003054static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003055 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003056};
3057
3058PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003059 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3060 "dict_values", /* tp_name */
3061 sizeof(dictviewobject), /* tp_basicsize */
3062 0, /* tp_itemsize */
3063 /* methods */
3064 (destructor)dictview_dealloc, /* tp_dealloc */
3065 0, /* tp_print */
3066 0, /* tp_getattr */
3067 0, /* tp_setattr */
3068 0, /* tp_reserved */
3069 (reprfunc)dictview_repr, /* tp_repr */
3070 0, /* tp_as_number */
3071 &dictvalues_as_sequence, /* tp_as_sequence */
3072 0, /* tp_as_mapping */
3073 0, /* tp_hash */
3074 0, /* tp_call */
3075 0, /* tp_str */
3076 PyObject_GenericGetAttr, /* tp_getattro */
3077 0, /* tp_setattro */
3078 0, /* tp_as_buffer */
3079 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3080 0, /* tp_doc */
3081 (traverseproc)dictview_traverse, /* tp_traverse */
3082 0, /* tp_clear */
3083 0, /* tp_richcompare */
3084 0, /* tp_weaklistoffset */
3085 (getiterfunc)dictvalues_iter, /* tp_iter */
3086 0, /* tp_iternext */
3087 dictvalues_methods, /* tp_methods */
3088 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003089};
3090
3091static PyObject *
3092dictvalues_new(PyObject *dict)
3093{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003094 return dictview_new(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003095}