blob: 5d8910f48c39eb08a1b1bf35bee2ac333f06a82e [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 {
Victor Stinner198b2912012-03-06 01:03:13 +0100350 PyErr_SetString(PyExc_RuntimeError,
351 "dictionary changed size during lookup");
352 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 }
354 }
355 freeslot = NULL;
356 }
Tim Peters15d49292001-05-27 07:39:22 +0000357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 /* In the loop, me_key == dummy is by far (factor of 100s) the
359 least likely outcome, so test for that last. */
360 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
361 i = (i << 2) + i + perturb + 1;
362 ep = &ep0[i & mask];
363 if (ep->me_key == NULL)
364 return freeslot == NULL ? ep : freeslot;
365 if (ep->me_key == key)
366 return ep;
367 if (ep->me_hash == hash && ep->me_key != dummy) {
368 startkey = ep->me_key;
369 Py_INCREF(startkey);
370 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
371 Py_DECREF(startkey);
372 if (cmp < 0)
373 return NULL;
374 if (ep0 == mp->ma_table && ep->me_key == startkey) {
375 if (cmp > 0)
376 return ep;
377 }
378 else {
Victor Stinner198b2912012-03-06 01:03:13 +0100379 PyErr_SetString(PyExc_RuntimeError,
380 "dictionary changed size during lookup");
381 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 }
383 }
384 else if (ep->me_key == dummy && freeslot == NULL)
385 freeslot = ep;
386 }
387 assert(0); /* NOT REACHED */
388 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000389}
390
391/*
Guido van Rossum89d8c602007-09-18 17:26:56 +0000392 * Hacked up version of lookdict which can assume keys are always
393 * unicodes; this assumption allows testing for errors during
394 * PyObject_RichCompareBool() to be dropped; unicode-unicode
395 * comparisons never raise exceptions. This also means we don't need
396 * to go through PyObject_RichCompareBool(); we can always use
397 * unicode_eq() directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000398 *
Guido van Rossum89d8c602007-09-18 17:26:56 +0000399 * This is valuable because dicts with only unicode keys are very common.
Fred Drake1bff34a2000-08-31 19:31:38 +0000400 */
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000401static PyDictEntry *
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000402lookdict_unicode(PyDictObject *mp, PyObject *key, register Py_hash_t hash)
Fred Drake1bff34a2000-08-31 19:31:38 +0000403{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 register size_t i;
405 register size_t perturb;
406 register PyDictEntry *freeslot;
407 register size_t mask = (size_t)mp->ma_mask;
408 PyDictEntry *ep0 = mp->ma_table;
409 register PyDictEntry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 /* Make sure this function doesn't have to handle non-unicode keys,
412 including subclasses of str; e.g., one reason to subclass
413 unicodes is to override __eq__, and for speed we don't cater to
414 that here. */
415 if (!PyUnicode_CheckExact(key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000416#ifdef SHOW_CONVERSION_COUNTS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 ++converted;
Fred Drake1bff34a2000-08-31 19:31:38 +0000418#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 mp->ma_lookup = lookdict;
420 return lookdict(mp, key, hash);
421 }
Mark Dickinson57e683e2011-09-24 18:18:40 +0100422 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 ep = &ep0[i];
424 if (ep->me_key == NULL || ep->me_key == key)
425 return ep;
426 if (ep->me_key == dummy)
427 freeslot = ep;
428 else {
429 if (ep->me_hash == hash && unicode_eq(ep->me_key, key))
430 return ep;
431 freeslot = NULL;
432 }
Tim Peters15d49292001-05-27 07:39:22 +0000433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 /* In the loop, me_key == dummy is by far (factor of 100s) the
435 least likely outcome, so test for that last. */
436 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
437 i = (i << 2) + i + perturb + 1;
438 ep = &ep0[i & mask];
439 if (ep->me_key == NULL)
440 return freeslot == NULL ? ep : freeslot;
441 if (ep->me_key == key
442 || (ep->me_hash == hash
443 && ep->me_key != dummy
444 && unicode_eq(ep->me_key, key)))
445 return ep;
446 if (ep->me_key == dummy && freeslot == NULL)
447 freeslot = ep;
448 }
449 assert(0); /* NOT REACHED */
450 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000451}
452
Benjamin Petersonfb886362010-04-24 18:21:17 +0000453int
454_PyDict_HasOnlyStringKeys(PyObject *dict)
455{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 Py_ssize_t pos = 0;
457 PyObject *key, *value;
Benjamin Petersonf6096542010-11-17 22:33:12 +0000458 assert(PyDict_Check(dict));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 /* Shortcut */
460 if (((PyDictObject *)dict)->ma_lookup == lookdict_unicode)
461 return 1;
462 while (PyDict_Next(dict, &pos, &key, &value))
463 if (!PyUnicode_Check(key))
464 return 0;
465 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +0000466}
467
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000468#ifdef SHOW_TRACK_COUNT
469#define INCREASE_TRACK_COUNT \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 (count_tracked++, count_untracked--);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000471#define DECREASE_TRACK_COUNT \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 (count_tracked--, count_untracked++);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000473#else
474#define INCREASE_TRACK_COUNT
475#define DECREASE_TRACK_COUNT
476#endif
477
478#define MAINTAIN_TRACKING(mp, key, value) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 do { \
480 if (!_PyObject_GC_IS_TRACKED(mp)) { \
481 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
482 _PyObject_GC_MAY_BE_TRACKED(value)) { \
483 _PyObject_GC_TRACK(mp); \
484 INCREASE_TRACK_COUNT \
485 } \
486 } \
487 } while(0)
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000488
489void
490_PyDict_MaybeUntrack(PyObject *op)
491{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 PyDictObject *mp;
493 PyObject *value;
494 Py_ssize_t mask, i;
495 PyDictEntry *ep;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
498 return;
499
500 mp = (PyDictObject *) op;
501 ep = mp->ma_table;
502 mask = mp->ma_mask;
503 for (i = 0; i <= mask; i++) {
504 if ((value = ep[i].me_value) == NULL)
505 continue;
506 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
507 _PyObject_GC_MAY_BE_TRACKED(ep[i].me_key))
508 return;
509 }
510 DECREASE_TRACK_COUNT
511 _PyObject_GC_UNTRACK(op);
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000512}
513
Fred Drake1bff34a2000-08-31 19:31:38 +0000514/*
Antoine Pitroue965d972012-02-27 00:45:12 +0100515Internal routine to insert a new item into the table when you have entry object.
516Used by insertdict.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000517*/
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000518static int
Antoine Pitroue965d972012-02-27 00:45:12 +0100519insertdict_by_entry(register PyDictObject *mp, PyObject *key, Py_hash_t hash,
520 PyDictEntry *ep, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000521{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 PyObject *old_value;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 MAINTAIN_TRACKING(mp, key, value);
525 if (ep->me_value != NULL) {
526 old_value = ep->me_value;
527 ep->me_value = value;
528 Py_DECREF(old_value); /* which **CAN** re-enter */
529 Py_DECREF(key);
530 }
531 else {
532 if (ep->me_key == NULL)
533 mp->ma_fill++;
534 else {
535 assert(ep->me_key == dummy);
536 Py_DECREF(dummy);
537 }
538 ep->me_key = key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000539 ep->me_hash = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 ep->me_value = value;
541 mp->ma_used++;
542 }
543 return 0;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000544}
545
Antoine Pitroue965d972012-02-27 00:45:12 +0100546
547/*
548Internal routine to insert a new item into the table.
549Used both by the internal resize routine and by the public insert routine.
550Eats a reference to key and one to value.
551Returns -1 if an error occurred, or 0 on success.
552*/
553static int
554insertdict(register PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
555{
556 register PyDictEntry *ep;
557
558 assert(mp->ma_lookup != NULL);
559 ep = mp->ma_lookup(mp, key, hash);
560 if (ep == NULL) {
561 Py_DECREF(key);
562 Py_DECREF(value);
563 return -1;
564 }
565 return insertdict_by_entry(mp, key, hash, ep, value);
566}
567
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000568/*
569Internal routine used by dictresize() to insert an item which is
570known to be absent from the dict. This routine also assumes that
571the dict contains no deleted entries. Besides the performance benefit,
572using insertdict() in dictresize() is dangerous (SF bug #1456209).
573Note that no refcounts are changed by this routine; if needed, the caller
574is responsible for incref'ing `key` and `value`.
575*/
576static void
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000577insertdict_clean(register PyDictObject *mp, PyObject *key, Py_hash_t hash,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 PyObject *value)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000579{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 register size_t i;
581 register size_t perturb;
582 register size_t mask = (size_t)mp->ma_mask;
583 PyDictEntry *ep0 = mp->ma_table;
584 register PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 MAINTAIN_TRACKING(mp, key, value);
Mark Dickinson57e683e2011-09-24 18:18:40 +0100587 i = (size_t)hash & mask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 ep = &ep0[i];
589 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
590 i = (i << 2) + i + perturb + 1;
591 ep = &ep0[i & mask];
592 }
593 assert(ep->me_value == NULL);
594 mp->ma_fill++;
595 ep->me_key = key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000596 ep->me_hash = hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 ep->me_value = value;
598 mp->ma_used++;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000599}
600
601/*
602Restructure the table by allocating a new table and reinserting all
603items again. When entries have been deleted, the new table may
604actually be smaller than the old one.
605*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000606static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000607dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000608{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 Py_ssize_t newsize;
610 PyDictEntry *oldtable, *newtable, *ep;
611 Py_ssize_t i;
612 int is_oldtable_malloced;
613 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000616
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 /* Find the smallest table size > minused. */
618 for (newsize = PyDict_MINSIZE;
619 newsize <= minused && newsize > 0;
620 newsize <<= 1)
621 ;
622 if (newsize <= 0) {
623 PyErr_NoMemory();
624 return -1;
625 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 /* Get space for a new table. */
628 oldtable = mp->ma_table;
629 assert(oldtable != NULL);
630 is_oldtable_malloced = oldtable != mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 if (newsize == PyDict_MINSIZE) {
633 /* A large table is shrinking, or we can't get any smaller. */
634 newtable = mp->ma_smalltable;
635 if (newtable == oldtable) {
636 if (mp->ma_fill == mp->ma_used) {
637 /* No dummies, so no point doing anything. */
638 return 0;
639 }
640 /* We're not going to resize it, but rebuild the
641 table anyway to purge old dummy entries.
642 Subtle: This is *necessary* if fill==size,
643 as lookdict needs at least one virgin slot to
644 terminate failing searches. If fill < size, it's
645 merely desirable, as dummies slow searches. */
646 assert(mp->ma_fill > mp->ma_used);
647 memcpy(small_copy, oldtable, sizeof(small_copy));
648 oldtable = small_copy;
649 }
650 }
651 else {
652 newtable = PyMem_NEW(PyDictEntry, newsize);
653 if (newtable == NULL) {
654 PyErr_NoMemory();
655 return -1;
656 }
657 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000658
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 /* Make the dict empty, using the new table. */
660 assert(newtable != oldtable);
661 mp->ma_table = newtable;
662 mp->ma_mask = newsize - 1;
663 memset(newtable, 0, sizeof(PyDictEntry) * newsize);
664 mp->ma_used = 0;
665 i = mp->ma_fill;
666 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 /* Copy the data over; this is refcount-neutral for active entries;
669 dummy entries aren't copied over, of course */
670 for (ep = oldtable; i > 0; ep++) {
671 if (ep->me_value != NULL) { /* active entry */
672 --i;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000673 insertdict_clean(mp, ep->me_key, ep->me_hash, ep->me_value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 }
675 else if (ep->me_key != NULL) { /* dummy entry */
676 --i;
677 assert(ep->me_key == dummy);
678 Py_DECREF(ep->me_key);
679 }
680 /* else key == value == NULL: nothing to do */
681 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 if (is_oldtable_malloced)
684 PyMem_DEL(oldtable);
685 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000686}
687
Christian Heimes99170a52007-12-19 02:07:34 +0000688/* Create a new dictionary pre-sized to hold an estimated number of elements.
689 Underestimates are okay because the dictionary will resize as necessary.
690 Overestimates just mean the dictionary will be more sparse than usual.
691*/
692
693PyObject *
694_PyDict_NewPresized(Py_ssize_t minused)
695{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 PyObject *op = PyDict_New();
Christian Heimes99170a52007-12-19 02:07:34 +0000697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 if (minused>5 && op != NULL && dictresize((PyDictObject *)op, minused) == -1) {
699 Py_DECREF(op);
700 return NULL;
701 }
702 return op;
Christian Heimes99170a52007-12-19 02:07:34 +0000703}
704
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000705/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
706 * that may occur (originally dicts supported only string keys, and exceptions
707 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000708 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000709 * (suppressed) occurred while computing the key's hash, or that some error
710 * (suppressed) occurred when comparing keys in the dict's internal probe
711 * sequence. A nasty example of the latter is when a Python-coded comparison
712 * function hits a stack-depth error, which can cause this to return NULL
713 * even if the key is present.
714 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000715PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000716PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000717{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000718 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 PyDictObject *mp = (PyDictObject *)op;
720 PyDictEntry *ep;
721 PyThreadState *tstate;
722 if (!PyDict_Check(op))
723 return NULL;
724 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200725 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 {
727 hash = PyObject_Hash(key);
728 if (hash == -1) {
729 PyErr_Clear();
730 return NULL;
731 }
732 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 /* We can arrive here with a NULL tstate during initialization: try
735 running "python -Wi" for an example related to string interning.
736 Let's just hope that no exception occurs then... This must be
737 _PyThreadState_Current and not PyThreadState_GET() because in debug
738 mode, the latter complains if tstate is NULL. */
739 tstate = (PyThreadState*)_Py_atomic_load_relaxed(
740 &_PyThreadState_Current);
741 if (tstate != NULL && tstate->curexc_type != NULL) {
742 /* preserve the existing exception */
743 PyObject *err_type, *err_value, *err_tb;
744 PyErr_Fetch(&err_type, &err_value, &err_tb);
745 ep = (mp->ma_lookup)(mp, key, hash);
746 /* ignore errors */
747 PyErr_Restore(err_type, err_value, err_tb);
748 if (ep == NULL)
749 return NULL;
750 }
751 else {
752 ep = (mp->ma_lookup)(mp, key, hash);
753 if (ep == NULL) {
754 PyErr_Clear();
755 return NULL;
756 }
757 }
758 return ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000759}
760
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000761/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
762 This returns NULL *with* an exception set if an exception occurred.
763 It returns NULL *without* an exception set if the key wasn't present.
764*/
765PyObject *
766PyDict_GetItemWithError(PyObject *op, PyObject *key)
767{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000768 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 PyDictObject*mp = (PyDictObject *)op;
770 PyDictEntry *ep;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 if (!PyDict_Check(op)) {
773 PyErr_BadInternalCall();
774 return NULL;
775 }
776 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200777 (hash = ((PyASCIIObject *) key)->hash) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 {
779 hash = PyObject_Hash(key);
780 if (hash == -1) {
781 return NULL;
782 }
783 }
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 ep = (mp->ma_lookup)(mp, key, hash);
786 if (ep == NULL)
787 return NULL;
788 return ep->me_value;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000789}
790
Brett Cannonfd074152012-04-14 14:10:13 -0400791PyObject *
792_PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
793{
794 PyObject *kv;
795 kv = _PyUnicode_FromId(key); /* borrowed */
796 if (kv == NULL)
797 return NULL;
798 return PyDict_GetItemWithError(dp, kv);
799}
800
Antoine Pitroue965d972012-02-27 00:45:12 +0100801static int
802dict_set_item_by_hash_or_entry(register PyObject *op, PyObject *key,
803 Py_hash_t hash, PyDictEntry *ep, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000804{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 register PyDictObject *mp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 register Py_ssize_t n_used;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 mp = (PyDictObject *)op;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
810 n_used = mp->ma_used;
811 Py_INCREF(value);
812 Py_INCREF(key);
Antoine Pitroue965d972012-02-27 00:45:12 +0100813 if (ep == NULL) {
814 if (insertdict(mp, key, hash, value) != 0)
815 return -1;
816 }
817 else {
818 if (insertdict_by_entry(mp, key, hash, ep, value) != 0)
819 return -1;
820 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 /* If we added a key, we can safely resize. Otherwise just return!
822 * If fill >= 2/3 size, adjust size. Normally, this doubles or
823 * quaduples the size, but it's also possible for the dict to shrink
824 * (if ma_fill is much larger than ma_used, meaning a lot of dict
825 * keys have been * deleted).
826 *
827 * Quadrupling the size improves average dictionary sparseness
828 * (reducing collisions) at the cost of some memory and iteration
829 * speed (which loops over every possible entry). It also halves
830 * the number of expensive resize operations in a growing dictionary.
831 *
832 * Very large dictionaries (over 50K items) use doubling instead.
833 * This may help applications with severe memory constraints.
834 */
835 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
836 return 0;
837 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000838}
839
Antoine Pitroue965d972012-02-27 00:45:12 +0100840/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
841 * dictionary if it's merely replacing the value for an existing key.
842 * This means that it's safe to loop over a dictionary with PyDict_Next()
843 * and occasionally replace a value -- but you can't insert new keys or
844 * remove them.
845 */
846int
847PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
848{
849 register Py_hash_t hash;
850
851 if (!PyDict_Check(op)) {
852 PyErr_BadInternalCall();
853 return -1;
854 }
855 assert(key);
856 assert(value);
857 if (PyUnicode_CheckExact(key)) {
Antoine Pitrou70d27172012-02-27 00:59:34 +0100858 hash = ((PyASCIIObject *) key)->hash;
Antoine Pitroue965d972012-02-27 00:45:12 +0100859 if (hash == -1)
860 hash = PyObject_Hash(key);
861 }
862 else {
863 hash = PyObject_Hash(key);
864 if (hash == -1)
865 return -1;
866 }
867 return dict_set_item_by_hash_or_entry(op, key, hash, NULL, value);
868}
869
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000870int
Tim Peters1f5871e2000-07-04 17:44:48 +0000871PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000872{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 register PyDictObject *mp;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000874 register Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 register PyDictEntry *ep;
876 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 if (!PyDict_Check(op)) {
879 PyErr_BadInternalCall();
880 return -1;
881 }
882 assert(key);
883 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200884 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 hash = PyObject_Hash(key);
886 if (hash == -1)
887 return -1;
888 }
889 mp = (PyDictObject *)op;
890 ep = (mp->ma_lookup)(mp, key, hash);
891 if (ep == NULL)
892 return -1;
893 if (ep->me_value == NULL) {
894 set_key_error(key);
895 return -1;
896 }
897 old_key = ep->me_key;
898 Py_INCREF(dummy);
899 ep->me_key = dummy;
900 old_value = ep->me_value;
901 ep->me_value = NULL;
902 mp->ma_used--;
903 Py_DECREF(old_value);
904 Py_DECREF(old_key);
905 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000906}
907
Guido van Rossum25831651993-05-19 14:50:45 +0000908void
Tim Peters1f5871e2000-07-04 17:44:48 +0000909PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000910{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 PyDictObject *mp;
912 PyDictEntry *ep, *table;
913 int table_is_malloced;
914 Py_ssize_t fill;
915 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000916#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +0000918#endif
919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 if (!PyDict_Check(op))
921 return;
922 mp = (PyDictObject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000923#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 n = mp->ma_mask + 1;
925 i = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000926#endif
927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 table = mp->ma_table;
929 assert(table != NULL);
930 table_is_malloced = table != mp->ma_smalltable;
Tim Petersdea48ec2001-05-22 20:40:22 +0000931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 /* This is delicate. During the process of clearing the dict,
933 * decrefs can cause the dict to mutate. To avoid fatal confusion
934 * (voice of experience), we have to make the dict empty before
935 * clearing the slots, and never refer to anything via mp->xxx while
936 * clearing.
937 */
938 fill = mp->ma_fill;
939 if (table_is_malloced)
940 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 else if (fill > 0) {
943 /* It's a small table with something that needs to be cleared.
944 * Afraid the only safe way is to copy the dict entries into
945 * another small table first.
946 */
947 memcpy(small_copy, table, sizeof(small_copy));
948 table = small_copy;
949 EMPTY_TO_MINSIZE(mp);
950 }
951 /* else it's a small table that's already empty */
Tim Petersdea48ec2001-05-22 20:40:22 +0000952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 /* Now we can finally clear things. If C had refcounts, we could
954 * assert that the refcount on table is 1 now, i.e. that this function
955 * has unique access to it, so decref side-effects can't alter it.
956 */
957 for (ep = table; fill > 0; ++ep) {
Tim Petersdea48ec2001-05-22 20:40:22 +0000958#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 assert(i < n);
960 ++i;
Tim Petersdea48ec2001-05-22 20:40:22 +0000961#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 if (ep->me_key) {
963 --fill;
964 Py_DECREF(ep->me_key);
965 Py_XDECREF(ep->me_value);
966 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000967#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 else
969 assert(ep->me_value == NULL);
Tim Petersdea48ec2001-05-22 20:40:22 +0000970#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 if (table_is_malloced)
974 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000975}
976
Tim Peters080c88b2003-02-15 03:01:11 +0000977/*
978 * Iterate over a dict. Use like so:
979 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000980 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +0000981 * PyObject *key, *value;
982 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +0000983 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +0000984 * Refer to borrowed references in key and value.
985 * }
986 *
987 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +0000988 * mutates the dict. One exception: it is safe if the loop merely changes
989 * the values associated with the keys (but doesn't insert new keys or
990 * delete keys), via PyDict_SetItem().
991 */
Guido van Rossum25831651993-05-19 14:50:45 +0000992int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000993PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000994{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 register Py_ssize_t i;
996 register Py_ssize_t mask;
997 register PyDictEntry *ep;
Raymond Hettinger43442782004-03-17 21:55:03 +0000998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 if (!PyDict_Check(op))
1000 return 0;
1001 i = *ppos;
1002 if (i < 0)
1003 return 0;
1004 ep = ((PyDictObject *)op)->ma_table;
1005 mask = ((PyDictObject *)op)->ma_mask;
1006 while (i <= mask && ep[i].me_value == NULL)
1007 i++;
1008 *ppos = i+1;
1009 if (i > mask)
1010 return 0;
1011 if (pkey)
1012 *pkey = ep[i].me_key;
1013 if (pvalue)
1014 *pvalue = ep[i].me_value;
1015 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001016}
1017
Thomas Wouterscf297e42007-02-23 15:07:44 +00001018/* Internal version of PyDict_Next that returns a hash value in addition to the key and value.*/
1019int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001020_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, Py_hash_t *phash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00001021{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 register Py_ssize_t i;
1023 register Py_ssize_t mask;
1024 register PyDictEntry *ep;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 if (!PyDict_Check(op))
1027 return 0;
1028 i = *ppos;
1029 if (i < 0)
1030 return 0;
1031 ep = ((PyDictObject *)op)->ma_table;
1032 mask = ((PyDictObject *)op)->ma_mask;
1033 while (i <= mask && ep[i].me_value == NULL)
1034 i++;
1035 *ppos = i+1;
1036 if (i > mask)
1037 return 0;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001038 *phash = ep[i].me_hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 if (pkey)
1040 *pkey = ep[i].me_key;
1041 if (pvalue)
1042 *pvalue = ep[i].me_value;
1043 return 1;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001044}
1045
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001046/* Methods */
1047
1048static void
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001049dict_dealloc(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001050{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 register PyDictEntry *ep;
1052 Py_ssize_t fill = mp->ma_fill;
1053 PyObject_GC_UnTrack(mp);
1054 Py_TRASHCAN_SAFE_BEGIN(mp)
1055 for (ep = mp->ma_table; fill > 0; ep++) {
1056 if (ep->me_key) {
1057 --fill;
1058 Py_DECREF(ep->me_key);
1059 Py_XDECREF(ep->me_value);
1060 }
1061 }
1062 if (mp->ma_table != mp->ma_smalltable)
1063 PyMem_DEL(mp->ma_table);
1064 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1065 free_list[numfree++] = mp;
1066 else
1067 Py_TYPE(mp)->tp_free((PyObject *)mp);
1068 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001069}
1070
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001071static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001072dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001073{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 Py_ssize_t i;
1075 PyObject *s, *temp, *colon = NULL;
1076 PyObject *pieces = NULL, *result = NULL;
1077 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +00001078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 i = Py_ReprEnter((PyObject *)mp);
1080 if (i != 0) {
1081 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
1082 }
Guido van Rossum255443b1998-04-10 22:47:14 +00001083
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 if (mp->ma_used == 0) {
1085 result = PyUnicode_FromString("{}");
1086 goto Done;
1087 }
Tim Petersa7259592001-06-16 05:11:17 +00001088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 pieces = PyList_New(0);
1090 if (pieces == NULL)
1091 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 colon = PyUnicode_FromString(": ");
1094 if (colon == NULL)
1095 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 /* Do repr() on each key+value pair, and insert ": " between them.
1098 Note that repr may mutate the dict. */
1099 i = 0;
1100 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
1101 int status;
1102 /* Prevent repr from deleting value during key format. */
1103 Py_INCREF(value);
1104 s = PyObject_Repr(key);
1105 PyUnicode_Append(&s, colon);
1106 PyUnicode_AppendAndDel(&s, PyObject_Repr(value));
1107 Py_DECREF(value);
1108 if (s == NULL)
1109 goto Done;
1110 status = PyList_Append(pieces, s);
1111 Py_DECREF(s); /* append created a new ref */
1112 if (status < 0)
1113 goto Done;
1114 }
Tim Petersa7259592001-06-16 05:11:17 +00001115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 /* Add "{}" decorations to the first and last items. */
1117 assert(PyList_GET_SIZE(pieces) > 0);
1118 s = PyUnicode_FromString("{");
1119 if (s == NULL)
1120 goto Done;
1121 temp = PyList_GET_ITEM(pieces, 0);
1122 PyUnicode_AppendAndDel(&s, temp);
1123 PyList_SET_ITEM(pieces, 0, s);
1124 if (s == NULL)
1125 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 s = PyUnicode_FromString("}");
1128 if (s == NULL)
1129 goto Done;
1130 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
1131 PyUnicode_AppendAndDel(&temp, s);
1132 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
1133 if (temp == NULL)
1134 goto Done;
Tim Petersa7259592001-06-16 05:11:17 +00001135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 /* Paste them all together with ", " between. */
1137 s = PyUnicode_FromString(", ");
1138 if (s == NULL)
1139 goto Done;
1140 result = PyUnicode_Join(s, pieces);
1141 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +00001142
1143Done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 Py_XDECREF(pieces);
1145 Py_XDECREF(colon);
1146 Py_ReprLeave((PyObject *)mp);
1147 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001148}
1149
Martin v. Löwis18e16552006-02-15 17:27:45 +00001150static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001151dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001152{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 return mp->ma_used;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001154}
1155
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001156static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001157dict_subscript(PyDictObject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001158{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 PyObject *v;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001160 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 PyDictEntry *ep;
1162 assert(mp->ma_table != NULL);
1163 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001164 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 hash = PyObject_Hash(key);
1166 if (hash == -1)
1167 return NULL;
1168 }
1169 ep = (mp->ma_lookup)(mp, key, hash);
1170 if (ep == NULL)
1171 return NULL;
1172 v = ep->me_value;
1173 if (v == NULL) {
1174 if (!PyDict_CheckExact(mp)) {
1175 /* Look up __missing__ method if we're a subclass. */
1176 PyObject *missing, *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05001177 _Py_IDENTIFIER(__missing__);
1178 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 if (missing != NULL) {
1180 res = PyObject_CallFunctionObjArgs(missing,
1181 key, NULL);
1182 Py_DECREF(missing);
1183 return res;
1184 }
1185 else if (PyErr_Occurred())
1186 return NULL;
1187 }
1188 set_key_error(key);
1189 return NULL;
1190 }
1191 else
1192 Py_INCREF(v);
1193 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001194}
1195
1196static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001197dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001198{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 if (w == NULL)
1200 return PyDict_DelItem((PyObject *)mp, v);
1201 else
1202 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001203}
1204
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001205static PyMappingMethods dict_as_mapping = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 (lenfunc)dict_length, /*mp_length*/
1207 (binaryfunc)dict_subscript, /*mp_subscript*/
1208 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001209};
1210
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001211static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001212dict_keys(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 register PyObject *v;
1215 register Py_ssize_t i, j;
1216 PyDictEntry *ep;
1217 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001218
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001219 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 n = mp->ma_used;
1221 v = PyList_New(n);
1222 if (v == NULL)
1223 return NULL;
1224 if (n != mp->ma_used) {
1225 /* Durnit. The allocations caused the dict to resize.
1226 * Just start over, this shouldn't normally happen.
1227 */
1228 Py_DECREF(v);
1229 goto again;
1230 }
1231 ep = mp->ma_table;
1232 mask = mp->ma_mask;
1233 for (i = 0, j = 0; i <= mask; i++) {
1234 if (ep[i].me_value != NULL) {
1235 PyObject *key = ep[i].me_key;
1236 Py_INCREF(key);
1237 PyList_SET_ITEM(v, j, key);
1238 j++;
1239 }
1240 }
1241 assert(j == n);
1242 return v;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001243}
1244
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001245static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001246dict_values(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001247{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 register PyObject *v;
1249 register Py_ssize_t i, j;
1250 PyDictEntry *ep;
1251 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001252
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001253 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 n = mp->ma_used;
1255 v = PyList_New(n);
1256 if (v == NULL)
1257 return NULL;
1258 if (n != mp->ma_used) {
1259 /* Durnit. The allocations caused the dict to resize.
1260 * Just start over, this shouldn't normally happen.
1261 */
1262 Py_DECREF(v);
1263 goto again;
1264 }
1265 ep = mp->ma_table;
1266 mask = mp->ma_mask;
1267 for (i = 0, j = 0; i <= mask; i++) {
1268 if (ep[i].me_value != NULL) {
1269 PyObject *value = ep[i].me_value;
1270 Py_INCREF(value);
1271 PyList_SET_ITEM(v, j, value);
1272 j++;
1273 }
1274 }
1275 assert(j == n);
1276 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001277}
1278
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001279static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001280dict_items(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001281{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 register PyObject *v;
1283 register Py_ssize_t i, j, n;
1284 Py_ssize_t mask;
1285 PyObject *item, *key, *value;
1286 PyDictEntry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 /* Preallocate the list of tuples, to avoid allocations during
1289 * the loop over the items, which could trigger GC, which
1290 * could resize the dict. :-(
1291 */
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001292 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 n = mp->ma_used;
1294 v = PyList_New(n);
1295 if (v == NULL)
1296 return NULL;
1297 for (i = 0; i < n; i++) {
1298 item = PyTuple_New(2);
1299 if (item == NULL) {
1300 Py_DECREF(v);
1301 return NULL;
1302 }
1303 PyList_SET_ITEM(v, i, item);
1304 }
1305 if (n != mp->ma_used) {
1306 /* Durnit. The allocations caused the dict to resize.
1307 * Just start over, this shouldn't normally happen.
1308 */
1309 Py_DECREF(v);
1310 goto again;
1311 }
1312 /* Nothing we do below makes any function calls. */
1313 ep = mp->ma_table;
1314 mask = mp->ma_mask;
1315 for (i = 0, j = 0; i <= mask; i++) {
1316 if ((value=ep[i].me_value) != NULL) {
1317 key = ep[i].me_key;
1318 item = PyList_GET_ITEM(v, j);
1319 Py_INCREF(key);
1320 PyTuple_SET_ITEM(item, 0, key);
1321 Py_INCREF(value);
1322 PyTuple_SET_ITEM(item, 1, value);
1323 j++;
1324 }
1325 }
1326 assert(j == n);
1327 return v;
Guido van Rossum25831651993-05-19 14:50:45 +00001328}
1329
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001330static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001331dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001332{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 PyObject *seq;
1334 PyObject *value = Py_None;
1335 PyObject *it; /* iter(seq) */
1336 PyObject *key;
1337 PyObject *d;
1338 int status;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
1341 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 d = PyObject_CallObject(cls, NULL);
1344 if (d == NULL)
1345 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) {
1348 PyDictObject *mp = (PyDictObject *)d;
1349 PyObject *oldvalue;
1350 Py_ssize_t pos = 0;
1351 PyObject *key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001352 Py_hash_t hash;
Guido van Rossum58da9312007-11-10 23:39:45 +00001353
Petri Lehtinena94200e2011-10-24 21:12:58 +03001354 if (dictresize(mp, Py_SIZE(seq))) {
1355 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001357 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001359 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
1360 Py_INCREF(key);
1361 Py_INCREF(value);
Petri Lehtinena94200e2011-10-24 21:12:58 +03001362 if (insertdict(mp, key, hash, value)) {
1363 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001365 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 }
1367 return d;
1368 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
1371 PyDictObject *mp = (PyDictObject *)d;
1372 Py_ssize_t pos = 0;
1373 PyObject *key;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001374 Py_hash_t hash;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001375
Petri Lehtinena94200e2011-10-24 21:12:58 +03001376 if (dictresize(mp, PySet_GET_SIZE(seq))) {
1377 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001379 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
1382 Py_INCREF(key);
1383 Py_INCREF(value);
Petri Lehtinena94200e2011-10-24 21:12:58 +03001384 if (insertdict(mp, key, hash, value)) {
1385 Py_DECREF(d);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 return NULL;
Petri Lehtinena94200e2011-10-24 21:12:58 +03001387 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 }
1389 return d;
1390 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 it = PyObject_GetIter(seq);
1393 if (it == NULL){
1394 Py_DECREF(d);
1395 return NULL;
1396 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 if (PyDict_CheckExact(d)) {
1399 while ((key = PyIter_Next(it)) != NULL) {
1400 status = PyDict_SetItem(d, key, value);
1401 Py_DECREF(key);
1402 if (status < 0)
1403 goto Fail;
1404 }
1405 } else {
1406 while ((key = PyIter_Next(it)) != NULL) {
1407 status = PyObject_SetItem(d, key, value);
1408 Py_DECREF(key);
1409 if (status < 0)
1410 goto Fail;
1411 }
1412 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001414 if (PyErr_Occurred())
1415 goto Fail;
1416 Py_DECREF(it);
1417 return d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001418
1419Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 Py_DECREF(it);
1421 Py_DECREF(d);
1422 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001423}
1424
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001425static int
1426dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001427{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 PyObject *arg = NULL;
1429 int result = 0;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1432 result = -1;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 else if (arg != NULL) {
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001435 _Py_IDENTIFIER(keys);
1436 if (_PyObject_HasAttrId(arg, &PyId_keys))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 result = PyDict_Merge(self, arg, 1);
1438 else
1439 result = PyDict_MergeFromSeq2(self, arg, 1);
1440 }
1441 if (result == 0 && kwds != NULL) {
1442 if (PyArg_ValidateKeywordArguments(kwds))
1443 result = PyDict_Merge(self, kwds, 1);
1444 else
1445 result = -1;
1446 }
1447 return result;
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001448}
1449
1450static PyObject *
1451dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1452{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 if (dict_update_common(self, args, kwds, "update") != -1)
1454 Py_RETURN_NONE;
1455 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001456}
1457
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001458/* Update unconditionally replaces existing items.
1459 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001460 otherwise it leaves existing items unchanged.
1461
1462 PyDict_{Update,Merge} update/merge from a mapping object.
1463
Tim Petersf582b822001-12-11 18:51:08 +00001464 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001465 producing iterable objects of length 2.
1466*/
1467
Tim Petersf582b822001-12-11 18:51:08 +00001468int
Tim Peters1fc240e2001-10-26 05:06:50 +00001469PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1470{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 PyObject *it; /* iter(seq2) */
1472 Py_ssize_t i; /* index into seq2 of current element */
1473 PyObject *item; /* seq2[i] */
1474 PyObject *fast; /* item as a 2-tuple or 2-list */
Tim Peters1fc240e2001-10-26 05:06:50 +00001475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 assert(d != NULL);
1477 assert(PyDict_Check(d));
1478 assert(seq2 != NULL);
Tim Peters1fc240e2001-10-26 05:06:50 +00001479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 it = PyObject_GetIter(seq2);
1481 if (it == NULL)
1482 return -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 for (i = 0; ; ++i) {
1485 PyObject *key, *value;
1486 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001488 fast = NULL;
1489 item = PyIter_Next(it);
1490 if (item == NULL) {
1491 if (PyErr_Occurred())
1492 goto Fail;
1493 break;
1494 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 /* Convert item to sequence, and verify length 2. */
1497 fast = PySequence_Fast(item, "");
1498 if (fast == NULL) {
1499 if (PyErr_ExceptionMatches(PyExc_TypeError))
1500 PyErr_Format(PyExc_TypeError,
1501 "cannot convert dictionary update "
1502 "sequence element #%zd to a sequence",
1503 i);
1504 goto Fail;
1505 }
1506 n = PySequence_Fast_GET_SIZE(fast);
1507 if (n != 2) {
1508 PyErr_Format(PyExc_ValueError,
1509 "dictionary update sequence element #%zd "
1510 "has length %zd; 2 is required",
1511 i, n);
1512 goto Fail;
1513 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 /* Update/merge with this (key, value) pair. */
1516 key = PySequence_Fast_GET_ITEM(fast, 0);
1517 value = PySequence_Fast_GET_ITEM(fast, 1);
1518 if (override || PyDict_GetItem(d, key) == NULL) {
1519 int status = PyDict_SetItem(d, key, value);
1520 if (status < 0)
1521 goto Fail;
1522 }
1523 Py_DECREF(fast);
1524 Py_DECREF(item);
1525 }
Tim Peters1fc240e2001-10-26 05:06:50 +00001526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527 i = 0;
1528 goto Return;
Tim Peters1fc240e2001-10-26 05:06:50 +00001529Fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 Py_XDECREF(item);
1531 Py_XDECREF(fast);
1532 i = -1;
Tim Peters1fc240e2001-10-26 05:06:50 +00001533Return:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 Py_DECREF(it);
1535 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001536}
1537
Tim Peters6d6c1a32001-08-02 04:15:00 +00001538int
1539PyDict_Update(PyObject *a, PyObject *b)
1540{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001541 return PyDict_Merge(a, b, 1);
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001542}
1543
1544int
1545PyDict_Merge(PyObject *a, PyObject *b, int override)
1546{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001547 register PyDictObject *mp, *other;
1548 register Py_ssize_t i;
1549 PyDictEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 /* We accept for the argument either a concrete dictionary object,
1552 * or an abstract "mapping" object. For the former, we can do
1553 * things quite efficiently. For the latter, we only require that
1554 * PyMapping_Keys() and PyObject_GetItem() be supported.
1555 */
1556 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1557 PyErr_BadInternalCall();
1558 return -1;
1559 }
1560 mp = (PyDictObject*)a;
1561 if (PyDict_Check(b)) {
1562 other = (PyDictObject*)b;
1563 if (other == mp || other->ma_used == 0)
1564 /* a.update(a) or a.update({}); nothing to do */
1565 return 0;
1566 if (mp->ma_used == 0)
1567 /* Since the target dict is empty, PyDict_GetItem()
1568 * always returns NULL. Setting override to 1
1569 * skips the unnecessary test.
1570 */
1571 override = 1;
1572 /* Do one big resize at the start, rather than
1573 * incrementally resizing as we insert new items. Expect
1574 * that there will be no (or few) overlapping keys.
1575 */
1576 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
1577 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
1578 return -1;
1579 }
1580 for (i = 0; i <= other->ma_mask; i++) {
1581 entry = &other->ma_table[i];
1582 if (entry->me_value != NULL &&
1583 (override ||
1584 PyDict_GetItem(a, entry->me_key) == NULL)) {
1585 Py_INCREF(entry->me_key);
1586 Py_INCREF(entry->me_value);
1587 if (insertdict(mp, entry->me_key,
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001588 entry->me_hash,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001589 entry->me_value) != 0)
1590 return -1;
1591 }
1592 }
1593 }
1594 else {
1595 /* Do it the generic, slower way */
1596 PyObject *keys = PyMapping_Keys(b);
1597 PyObject *iter;
1598 PyObject *key, *value;
1599 int status;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 if (keys == NULL)
1602 /* Docstring says this is equivalent to E.keys() so
1603 * if E doesn't have a .keys() method we want
1604 * AttributeError to percolate up. Might as well
1605 * do the same for any other error.
1606 */
1607 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 iter = PyObject_GetIter(keys);
1610 Py_DECREF(keys);
1611 if (iter == NULL)
1612 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
1615 if (!override && PyDict_GetItem(a, key) != NULL) {
1616 Py_DECREF(key);
1617 continue;
1618 }
1619 value = PyObject_GetItem(b, key);
1620 if (value == NULL) {
1621 Py_DECREF(iter);
1622 Py_DECREF(key);
1623 return -1;
1624 }
1625 status = PyDict_SetItem(a, key, value);
1626 Py_DECREF(key);
1627 Py_DECREF(value);
1628 if (status < 0) {
1629 Py_DECREF(iter);
1630 return -1;
1631 }
1632 }
1633 Py_DECREF(iter);
1634 if (PyErr_Occurred())
1635 /* Iterator completed, via error */
1636 return -1;
1637 }
1638 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001639}
1640
1641static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001642dict_copy(register PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001643{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 return PyDict_Copy((PyObject*)mp);
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001645}
1646
1647PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001648PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001649{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001650 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 if (o == NULL || !PyDict_Check(o)) {
1653 PyErr_BadInternalCall();
1654 return NULL;
1655 }
1656 copy = PyDict_New();
1657 if (copy == NULL)
1658 return NULL;
1659 if (PyDict_Merge(copy, o, 1) == 0)
1660 return copy;
1661 Py_DECREF(copy);
1662 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001663}
1664
Martin v. Löwis18e16552006-02-15 17:27:45 +00001665Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001666PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001667{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 if (mp == NULL || !PyDict_Check(mp)) {
1669 PyErr_BadInternalCall();
1670 return -1;
1671 }
1672 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001673}
1674
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001675PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001676PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001677{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001678 if (mp == NULL || !PyDict_Check(mp)) {
1679 PyErr_BadInternalCall();
1680 return NULL;
1681 }
1682 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001683}
1684
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001685PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001686PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001687{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 if (mp == NULL || !PyDict_Check(mp)) {
1689 PyErr_BadInternalCall();
1690 return NULL;
1691 }
1692 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001693}
1694
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001695PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001696PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001697{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698 if (mp == NULL || !PyDict_Check(mp)) {
1699 PyErr_BadInternalCall();
1700 return NULL;
1701 }
1702 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001703}
1704
Tim Peterse63415e2001-05-08 04:38:29 +00001705/* Return 1 if dicts equal, 0 if not, -1 if error.
1706 * Gets out as soon as any difference is detected.
1707 * Uses only Py_EQ comparison.
1708 */
1709static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001710dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00001711{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00001713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 if (a->ma_used != b->ma_used)
1715 /* can't be equal if # of entries differ */
1716 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 /* Same # of entries -- check all of 'em. Exit early on any diff. */
1719 for (i = 0; i <= a->ma_mask; i++) {
1720 PyObject *aval = a->ma_table[i].me_value;
1721 if (aval != NULL) {
1722 int cmp;
1723 PyObject *bval;
1724 PyObject *key = a->ma_table[i].me_key;
1725 /* temporarily bump aval's refcount to ensure it stays
1726 alive until we're done with it */
1727 Py_INCREF(aval);
1728 /* ditto for key */
1729 Py_INCREF(key);
1730 bval = PyDict_GetItemWithError((PyObject *)b, key);
1731 Py_DECREF(key);
1732 if (bval == NULL) {
1733 Py_DECREF(aval);
1734 if (PyErr_Occurred())
1735 return -1;
1736 return 0;
1737 }
1738 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1739 Py_DECREF(aval);
1740 if (cmp <= 0) /* error or not equal */
1741 return cmp;
1742 }
1743 }
1744 return 1;
Tim Peterse63415e2001-05-08 04:38:29 +00001745 }
1746
1747static PyObject *
1748dict_richcompare(PyObject *v, PyObject *w, int op)
1749{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 int cmp;
1751 PyObject *res;
Tim Peterse63415e2001-05-08 04:38:29 +00001752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1754 res = Py_NotImplemented;
1755 }
1756 else if (op == Py_EQ || op == Py_NE) {
1757 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
1758 if (cmp < 0)
1759 return NULL;
1760 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1761 }
1762 else
1763 res = Py_NotImplemented;
1764 Py_INCREF(res);
1765 return res;
Tim Peterse63415e2001-05-08 04:38:29 +00001766 }
1767
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001768static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001769dict_contains(register PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001770{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001771 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001775 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 hash = PyObject_Hash(key);
1777 if (hash == -1)
1778 return NULL;
1779 }
1780 ep = (mp->ma_lookup)(mp, key, hash);
1781 if (ep == NULL)
1782 return NULL;
1783 return PyBool_FromLong(ep->me_value != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001784}
1785
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001786static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001787dict_get(register PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001788{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789 PyObject *key;
1790 PyObject *failobj = Py_None;
1791 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001792 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001793 PyDictEntry *ep;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001794
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001795 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
1796 return NULL;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001798 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001799 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 hash = PyObject_Hash(key);
1801 if (hash == -1)
1802 return NULL;
1803 }
1804 ep = (mp->ma_lookup)(mp, key, hash);
1805 if (ep == NULL)
1806 return NULL;
1807 val = ep->me_value;
1808 if (val == NULL)
1809 val = failobj;
1810 Py_INCREF(val);
1811 return val;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001812}
1813
1814
1815static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001816dict_setdefault(register PyDictObject *mp, PyObject *args)
Guido van Rossum164452c2000-08-08 16:12:54 +00001817{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 PyObject *key;
1819 PyObject *failobj = Py_None;
1820 PyObject *val = NULL;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001821 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 PyDictEntry *ep;
Guido van Rossum164452c2000-08-08 16:12:54 +00001823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
1825 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001827 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001828 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829 hash = PyObject_Hash(key);
1830 if (hash == -1)
1831 return NULL;
1832 }
1833 ep = (mp->ma_lookup)(mp, key, hash);
1834 if (ep == NULL)
1835 return NULL;
1836 val = ep->me_value;
1837 if (val == NULL) {
Antoine Pitroue965d972012-02-27 00:45:12 +01001838 if (dict_set_item_by_hash_or_entry((PyObject*)mp, key, hash, ep,
1839 failobj) == 0)
1840 val = failobj;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001841 }
1842 Py_XINCREF(val);
1843 return val;
Guido van Rossum164452c2000-08-08 16:12:54 +00001844}
1845
1846
1847static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001848dict_clear(register PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001849{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001850 PyDict_Clear((PyObject *)mp);
1851 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001852}
1853
Guido van Rossumba6ab842000-12-12 22:02:18 +00001854static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001855dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00001856{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001857 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 PyDictEntry *ep;
1859 PyObject *old_value, *old_key;
1860 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1863 return NULL;
1864 if (mp->ma_used == 0) {
1865 if (deflt) {
1866 Py_INCREF(deflt);
1867 return deflt;
1868 }
Raymond Hettingerdd421542010-10-30 08:10:29 +00001869 set_key_error(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001870 return NULL;
1871 }
1872 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001873 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 hash = PyObject_Hash(key);
1875 if (hash == -1)
1876 return NULL;
1877 }
1878 ep = (mp->ma_lookup)(mp, key, hash);
1879 if (ep == NULL)
1880 return NULL;
1881 if (ep->me_value == NULL) {
1882 if (deflt) {
1883 Py_INCREF(deflt);
1884 return deflt;
1885 }
1886 set_key_error(key);
1887 return NULL;
1888 }
1889 old_key = ep->me_key;
1890 Py_INCREF(dummy);
1891 ep->me_key = dummy;
1892 old_value = ep->me_value;
1893 ep->me_value = NULL;
1894 mp->ma_used--;
1895 Py_DECREF(old_key);
1896 return old_value;
Guido van Rossume027d982002-04-12 15:11:59 +00001897}
1898
1899static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001900dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001901{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00001902 Py_hash_t i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 PyDictEntry *ep;
1904 PyObject *res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 /* Allocate the result tuple before checking the size. Believe it
1907 * or not, this allocation could trigger a garbage collection which
1908 * could empty the dict, so if we checked the size first and that
1909 * happened, the result would be an infinite loop (searching for an
1910 * entry that no longer exists). Note that the usual popitem()
1911 * idiom is "while d: k, v = d.popitem()". so needing to throw the
1912 * tuple away if the dict *is* empty isn't a significant
1913 * inefficiency -- possible, but unlikely in practice.
1914 */
1915 res = PyTuple_New(2);
1916 if (res == NULL)
1917 return NULL;
1918 if (mp->ma_used == 0) {
1919 Py_DECREF(res);
1920 PyErr_SetString(PyExc_KeyError,
1921 "popitem(): dictionary is empty");
1922 return NULL;
1923 }
1924 /* Set ep to "the first" dict entry with a value. We abuse the hash
1925 * field of slot 0 to hold a search finger:
1926 * If slot 0 has a value, use slot 0.
1927 * Else slot 0 is being used to hold a search finger,
1928 * and we use its hash value as the first index to look.
1929 */
1930 ep = &mp->ma_table[0];
1931 if (ep->me_value == NULL) {
1932 i = ep->me_hash;
1933 /* The hash field may be a real hash value, or it may be a
1934 * legit search finger, or it may be a once-legit search
1935 * finger that's out of bounds now because it wrapped around
1936 * or the table shrunk -- simply make sure it's in bounds now.
1937 */
1938 if (i > mp->ma_mask || i < 1)
1939 i = 1; /* skip slot 0 */
1940 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1941 i++;
1942 if (i > mp->ma_mask)
1943 i = 1;
1944 }
1945 }
1946 PyTuple_SET_ITEM(res, 0, ep->me_key);
1947 PyTuple_SET_ITEM(res, 1, ep->me_value);
1948 Py_INCREF(dummy);
1949 ep->me_key = dummy;
1950 ep->me_value = NULL;
1951 mp->ma_used--;
1952 assert(mp->ma_table[0].me_value == NULL);
1953 mp->ma_table[0].me_hash = i + 1; /* next place to start */
1954 return res;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001955}
1956
Jeremy Hylton8caad492000-06-23 14:18:11 +00001957static int
1958dict_traverse(PyObject *op, visitproc visit, void *arg)
1959{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 Py_ssize_t i = 0;
1961 PyObject *pk;
1962 PyObject *pv;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 while (PyDict_Next(op, &i, &pk, &pv)) {
1965 Py_VISIT(pk);
1966 Py_VISIT(pv);
1967 }
1968 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001969}
1970
1971static int
1972dict_tp_clear(PyObject *op)
1973{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 PyDict_Clear(op);
1975 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001976}
1977
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001978static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001979
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001980static PyObject *
1981dict_sizeof(PyDictObject *mp)
1982{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001983 Py_ssize_t res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001985 res = sizeof(PyDictObject);
1986 if (mp->ma_table != mp->ma_smalltable)
1987 res = res + (mp->ma_mask + 1) * sizeof(PyDictEntry);
1988 return PyLong_FromSsize_t(res);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001989}
1990
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001991PyDoc_STRVAR(contains__doc__,
1992"D.__contains__(k) -> True if D has a key k, else False");
1993
1994PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
1995
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001996PyDoc_STRVAR(sizeof__doc__,
1997"D.__sizeof__() -> size of D in memory, in bytes");
1998
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001999PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002000"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002001
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002002PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00002003"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 +00002004
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002005PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002006"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00002007If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00002008
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002009PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00002010"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000020112-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002012
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002013PyDoc_STRVAR(update__doc__,
Georg Brandlac0675c2011-12-18 19:30:55 +01002014"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n"
2015"If E present and has a .keys() method, does: for k in E: D[k] = E[k]\n\
2016If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00002017In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00002018
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002019PyDoc_STRVAR(fromkeys__doc__,
2020"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
2021v defaults to None.");
2022
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002023PyDoc_STRVAR(clear__doc__,
2024"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00002025
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002026PyDoc_STRVAR(copy__doc__,
2027"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00002028
Guido van Rossumb90c8482007-02-10 01:11:45 +00002029/* Forward */
2030static PyObject *dictkeys_new(PyObject *);
2031static PyObject *dictitems_new(PyObject *);
2032static PyObject *dictvalues_new(PyObject *);
2033
Guido van Rossum45c85d12007-07-27 16:31:40 +00002034PyDoc_STRVAR(keys__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002035 "D.keys() -> a set-like object providing a view on D's keys");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002036PyDoc_STRVAR(items__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002037 "D.items() -> a set-like object providing a view on D's items");
Guido van Rossum45c85d12007-07-27 16:31:40 +00002038PyDoc_STRVAR(values__doc__,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002039 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00002040
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002041static PyMethodDef mapp_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002042 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
2043 contains__doc__},
2044 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
2045 getitem__doc__},
2046 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
2047 sizeof__doc__},
2048 {"get", (PyCFunction)dict_get, METH_VARARGS,
2049 get__doc__},
2050 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
2051 setdefault_doc__},
2052 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
2053 pop__doc__},
2054 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
2055 popitem__doc__},
2056 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
2057 keys__doc__},
2058 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
2059 items__doc__},
2060 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
2061 values__doc__},
2062 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
2063 update__doc__},
2064 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
2065 fromkeys__doc__},
2066 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
2067 clear__doc__},
2068 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
2069 copy__doc__},
2070 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002071};
2072
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002073/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002074int
2075PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002076{
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002077 Py_hash_t hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002078 PyDictObject *mp = (PyDictObject *)op;
2079 PyDictEntry *ep;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002081 if (!PyUnicode_CheckExact(key) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002082 (hash = ((PyASCIIObject *) key)->hash) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002083 hash = PyObject_Hash(key);
2084 if (hash == -1)
2085 return -1;
2086 }
2087 ep = (mp->ma_lookup)(mp, key, hash);
2088 return ep == NULL ? -1 : (ep->me_value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002089}
2090
Thomas Wouterscf297e42007-02-23 15:07:44 +00002091/* Internal version of PyDict_Contains used when the hash value is already known */
2092int
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002093_PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
Thomas Wouterscf297e42007-02-23 15:07:44 +00002094{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002095 PyDictObject *mp = (PyDictObject *)op;
2096 PyDictEntry *ep;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002098 ep = (mp->ma_lookup)(mp, key, hash);
2099 return ep == NULL ? -1 : (ep->me_value != NULL);
Thomas Wouterscf297e42007-02-23 15:07:44 +00002100}
2101
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002102/* Hack to implement "key in dict" */
2103static PySequenceMethods dict_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002104 0, /* sq_length */
2105 0, /* sq_concat */
2106 0, /* sq_repeat */
2107 0, /* sq_item */
2108 0, /* sq_slice */
2109 0, /* sq_ass_item */
2110 0, /* sq_ass_slice */
2111 PyDict_Contains, /* sq_contains */
2112 0, /* sq_inplace_concat */
2113 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002114};
2115
Guido van Rossum09e563a2001-05-01 12:10:21 +00002116static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002117dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2118{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002119 PyObject *self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 assert(type != NULL && type->tp_alloc != NULL);
2122 self = type->tp_alloc(type, 0);
2123 if (self != NULL) {
2124 PyDictObject *d = (PyDictObject *)self;
2125 /* It's guaranteed that tp->alloc zeroed out the struct. */
2126 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
2127 INIT_NONZERO_DICT_SLOTS(d);
2128 d->ma_lookup = lookdict_unicode;
Ezio Melotti13925002011-03-16 11:05:33 +02002129 /* The object has been implicitly tracked by tp_alloc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002130 if (type == &PyDict_Type)
2131 _PyObject_GC_UNTRACK(d);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002132#ifdef SHOW_CONVERSION_COUNTS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002133 ++created;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002134#endif
Antoine Pitrou3a652b12009-03-23 18:52:06 +00002135#ifdef SHOW_TRACK_COUNT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002136 if (_PyObject_GC_IS_TRACKED(d))
2137 count_tracked++;
2138 else
2139 count_untracked++;
Antoine Pitrou3a652b12009-03-23 18:52:06 +00002140#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002141 }
2142 return self;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002143}
2144
Tim Peters25786c02001-09-02 08:22:48 +00002145static int
2146dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2147{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002148 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002149}
2150
Tim Peters6d6c1a32001-08-02 04:15:00 +00002151static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002152dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002153{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002155}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002156
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002157PyDoc_STRVAR(dictionary_doc,
Ezio Melotti7f807b72010-03-01 04:08:34 +00002158"dict() -> new empty dictionary\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002159"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002160" (key, value) pairs\n"
2161"dict(iterable) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002162" d = {}\n"
Ezio Melotti7f807b72010-03-01 04:08:34 +00002163" for k, v in iterable:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002164" d[k] = v\n"
2165"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2166" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002167
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002168PyTypeObject PyDict_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2170 "dict",
2171 sizeof(PyDictObject),
2172 0,
2173 (destructor)dict_dealloc, /* tp_dealloc */
2174 0, /* tp_print */
2175 0, /* tp_getattr */
2176 0, /* tp_setattr */
2177 0, /* tp_reserved */
2178 (reprfunc)dict_repr, /* tp_repr */
2179 0, /* tp_as_number */
2180 &dict_as_sequence, /* tp_as_sequence */
2181 &dict_as_mapping, /* tp_as_mapping */
Georg Brandl00da4e02010-10-18 07:32:48 +00002182 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002183 0, /* tp_call */
2184 0, /* tp_str */
2185 PyObject_GenericGetAttr, /* tp_getattro */
2186 0, /* tp_setattro */
2187 0, /* tp_as_buffer */
2188 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2189 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
2190 dictionary_doc, /* tp_doc */
2191 dict_traverse, /* tp_traverse */
2192 dict_tp_clear, /* tp_clear */
2193 dict_richcompare, /* tp_richcompare */
2194 0, /* tp_weaklistoffset */
2195 (getiterfunc)dict_iter, /* tp_iter */
2196 0, /* tp_iternext */
2197 mapp_methods, /* tp_methods */
2198 0, /* tp_members */
2199 0, /* tp_getset */
2200 0, /* tp_base */
2201 0, /* tp_dict */
2202 0, /* tp_descr_get */
2203 0, /* tp_descr_set */
2204 0, /* tp_dictoffset */
2205 dict_init, /* tp_init */
2206 PyType_GenericAlloc, /* tp_alloc */
2207 dict_new, /* tp_new */
2208 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002209};
2210
Victor Stinner3c1e4812012-03-26 22:10:51 +02002211PyObject *
2212_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
2213{
2214 PyObject *kv;
2215 kv = _PyUnicode_FromId(key); /* borrowed */
2216 if (kv == NULL)
2217 return NULL;
2218 return PyDict_GetItem(dp, kv);
2219}
2220
Guido van Rossum3cca2451997-05-16 14:23:33 +00002221/* For backward compatibility with old dictionary interface */
2222
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002223PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002224PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002226 PyObject *kv, *rv;
2227 kv = PyUnicode_FromString(key);
2228 if (kv == NULL)
2229 return NULL;
2230 rv = PyDict_GetItem(v, kv);
2231 Py_DECREF(kv);
2232 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002233}
2234
2235int
Victor Stinner3c1e4812012-03-26 22:10:51 +02002236_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
2237{
2238 PyObject *kv;
2239 kv = _PyUnicode_FromId(key); /* borrowed */
2240 if (kv == NULL)
2241 return -1;
2242 return PyDict_SetItem(v, kv, item);
2243}
2244
2245int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002246PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002247{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002248 PyObject *kv;
2249 int err;
2250 kv = PyUnicode_FromString(key);
2251 if (kv == NULL)
2252 return -1;
2253 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
2254 err = PyDict_SetItem(v, kv, item);
2255 Py_DECREF(kv);
2256 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002257}
2258
2259int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002260PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002261{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262 PyObject *kv;
2263 int err;
2264 kv = PyUnicode_FromString(key);
2265 if (kv == NULL)
2266 return -1;
2267 err = PyDict_DelItem(v, kv);
2268 Py_DECREF(kv);
2269 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002270}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002271
Raymond Hettinger019a1482004-03-18 02:41:19 +00002272/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002273
2274typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 PyObject_HEAD
2276 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
2277 Py_ssize_t di_used;
2278 Py_ssize_t di_pos;
2279 PyObject* di_result; /* reusable result tuple for iteritems */
2280 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002281} dictiterobject;
2282
2283static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002284dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002285{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002286 dictiterobject *di;
2287 di = PyObject_GC_New(dictiterobject, itertype);
2288 if (di == NULL)
2289 return NULL;
2290 Py_INCREF(dict);
2291 di->di_dict = dict;
2292 di->di_used = dict->ma_used;
2293 di->di_pos = 0;
2294 di->len = dict->ma_used;
2295 if (itertype == &PyDictIterItem_Type) {
2296 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2297 if (di->di_result == NULL) {
2298 Py_DECREF(di);
2299 return NULL;
2300 }
2301 }
2302 else
2303 di->di_result = NULL;
2304 _PyObject_GC_TRACK(di);
2305 return (PyObject *)di;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002306}
2307
2308static void
2309dictiter_dealloc(dictiterobject *di)
2310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 Py_XDECREF(di->di_dict);
2312 Py_XDECREF(di->di_result);
2313 PyObject_GC_Del(di);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002314}
2315
2316static int
2317dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
2318{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002319 Py_VISIT(di->di_dict);
2320 Py_VISIT(di->di_result);
2321 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002322}
2323
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002324static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002325dictiter_len(dictiterobject *di)
2326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002327 Py_ssize_t len = 0;
2328 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
2329 len = di->len;
2330 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002331}
2332
Guido van Rossumb90c8482007-02-10 01:11:45 +00002333PyDoc_STRVAR(length_hint_doc,
2334 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002335
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002336static PyObject *
2337dictiter_reduce(dictiterobject *di);
2338
2339PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
2340
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002341static PyMethodDef dictiter_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002342 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
2343 length_hint_doc},
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002344 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
2345 reduce_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002347};
2348
Raymond Hettinger019a1482004-03-18 02:41:19 +00002349static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002350{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002351 PyObject *key;
2352 register Py_ssize_t i, mask;
2353 register PyDictEntry *ep;
2354 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356 if (d == NULL)
2357 return NULL;
2358 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 if (di->di_used != d->ma_used) {
2361 PyErr_SetString(PyExc_RuntimeError,
2362 "dictionary changed size during iteration");
2363 di->di_used = -1; /* Make this state sticky */
2364 return NULL;
2365 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 i = di->di_pos;
2368 if (i < 0)
2369 goto fail;
2370 ep = d->ma_table;
2371 mask = d->ma_mask;
2372 while (i <= mask && ep[i].me_value == NULL)
2373 i++;
2374 di->di_pos = i+1;
2375 if (i > mask)
2376 goto fail;
2377 di->len--;
2378 key = ep[i].me_key;
2379 Py_INCREF(key);
2380 return key;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002381
2382fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 Py_DECREF(d);
2384 di->di_dict = NULL;
2385 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002386}
2387
Raymond Hettinger019a1482004-03-18 02:41:19 +00002388PyTypeObject PyDictIterKey_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2390 "dict_keyiterator", /* tp_name */
2391 sizeof(dictiterobject), /* tp_basicsize */
2392 0, /* tp_itemsize */
2393 /* methods */
2394 (destructor)dictiter_dealloc, /* tp_dealloc */
2395 0, /* tp_print */
2396 0, /* tp_getattr */
2397 0, /* tp_setattr */
2398 0, /* tp_reserved */
2399 0, /* tp_repr */
2400 0, /* tp_as_number */
2401 0, /* tp_as_sequence */
2402 0, /* tp_as_mapping */
2403 0, /* tp_hash */
2404 0, /* tp_call */
2405 0, /* tp_str */
2406 PyObject_GenericGetAttr, /* tp_getattro */
2407 0, /* tp_setattro */
2408 0, /* tp_as_buffer */
2409 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2410 0, /* tp_doc */
2411 (traverseproc)dictiter_traverse, /* tp_traverse */
2412 0, /* tp_clear */
2413 0, /* tp_richcompare */
2414 0, /* tp_weaklistoffset */
2415 PyObject_SelfIter, /* tp_iter */
2416 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
2417 dictiter_methods, /* tp_methods */
2418 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002419};
2420
2421static PyObject *dictiter_iternextvalue(dictiterobject *di)
2422{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002423 PyObject *value;
2424 register Py_ssize_t i, mask;
2425 register PyDictEntry *ep;
2426 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002428 if (d == NULL)
2429 return NULL;
2430 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 if (di->di_used != d->ma_used) {
2433 PyErr_SetString(PyExc_RuntimeError,
2434 "dictionary changed size during iteration");
2435 di->di_used = -1; /* Make this state sticky */
2436 return NULL;
2437 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 i = di->di_pos;
2440 mask = d->ma_mask;
2441 if (i < 0 || i > mask)
2442 goto fail;
2443 ep = d->ma_table;
2444 while ((value=ep[i].me_value) == NULL) {
2445 i++;
2446 if (i > mask)
2447 goto fail;
2448 }
2449 di->di_pos = i+1;
2450 di->len--;
2451 Py_INCREF(value);
2452 return value;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002453
2454fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002455 Py_DECREF(d);
2456 di->di_dict = NULL;
2457 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002458}
2459
2460PyTypeObject PyDictIterValue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002461 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2462 "dict_valueiterator", /* tp_name */
2463 sizeof(dictiterobject), /* tp_basicsize */
2464 0, /* tp_itemsize */
2465 /* methods */
2466 (destructor)dictiter_dealloc, /* tp_dealloc */
2467 0, /* tp_print */
2468 0, /* tp_getattr */
2469 0, /* tp_setattr */
2470 0, /* tp_reserved */
2471 0, /* tp_repr */
2472 0, /* tp_as_number */
2473 0, /* tp_as_sequence */
2474 0, /* tp_as_mapping */
2475 0, /* tp_hash */
2476 0, /* tp_call */
2477 0, /* tp_str */
2478 PyObject_GenericGetAttr, /* tp_getattro */
2479 0, /* tp_setattro */
2480 0, /* tp_as_buffer */
2481 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2482 0, /* tp_doc */
2483 (traverseproc)dictiter_traverse, /* tp_traverse */
2484 0, /* tp_clear */
2485 0, /* tp_richcompare */
2486 0, /* tp_weaklistoffset */
2487 PyObject_SelfIter, /* tp_iter */
2488 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
2489 dictiter_methods, /* tp_methods */
2490 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002491};
2492
2493static PyObject *dictiter_iternextitem(dictiterobject *di)
2494{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002495 PyObject *key, *value, *result = di->di_result;
2496 register Py_ssize_t i, mask;
2497 register PyDictEntry *ep;
2498 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002500 if (d == NULL)
2501 return NULL;
2502 assert (PyDict_Check(d));
Raymond Hettinger019a1482004-03-18 02:41:19 +00002503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002504 if (di->di_used != d->ma_used) {
2505 PyErr_SetString(PyExc_RuntimeError,
2506 "dictionary changed size during iteration");
2507 di->di_used = -1; /* Make this state sticky */
2508 return NULL;
2509 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002511 i = di->di_pos;
2512 if (i < 0)
2513 goto fail;
2514 ep = d->ma_table;
2515 mask = d->ma_mask;
2516 while (i <= mask && ep[i].me_value == NULL)
2517 i++;
2518 di->di_pos = i+1;
2519 if (i > mask)
2520 goto fail;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002522 if (result->ob_refcnt == 1) {
2523 Py_INCREF(result);
2524 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2525 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2526 } else {
2527 result = PyTuple_New(2);
2528 if (result == NULL)
2529 return NULL;
2530 }
2531 di->len--;
2532 key = ep[i].me_key;
2533 value = ep[i].me_value;
2534 Py_INCREF(key);
2535 Py_INCREF(value);
2536 PyTuple_SET_ITEM(result, 0, key);
2537 PyTuple_SET_ITEM(result, 1, value);
2538 return result;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002539
2540fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002541 Py_DECREF(d);
2542 di->di_dict = NULL;
2543 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002544}
2545
2546PyTypeObject PyDictIterItem_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002547 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2548 "dict_itemiterator", /* tp_name */
2549 sizeof(dictiterobject), /* tp_basicsize */
2550 0, /* tp_itemsize */
2551 /* methods */
2552 (destructor)dictiter_dealloc, /* tp_dealloc */
2553 0, /* tp_print */
2554 0, /* tp_getattr */
2555 0, /* tp_setattr */
2556 0, /* tp_reserved */
2557 0, /* tp_repr */
2558 0, /* tp_as_number */
2559 0, /* tp_as_sequence */
2560 0, /* tp_as_mapping */
2561 0, /* tp_hash */
2562 0, /* tp_call */
2563 0, /* tp_str */
2564 PyObject_GenericGetAttr, /* tp_getattro */
2565 0, /* tp_setattro */
2566 0, /* tp_as_buffer */
2567 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
2568 0, /* tp_doc */
2569 (traverseproc)dictiter_traverse, /* tp_traverse */
2570 0, /* tp_clear */
2571 0, /* tp_richcompare */
2572 0, /* tp_weaklistoffset */
2573 PyObject_SelfIter, /* tp_iter */
2574 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
2575 dictiter_methods, /* tp_methods */
2576 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002577};
Guido van Rossumb90c8482007-02-10 01:11:45 +00002578
2579
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002580static PyObject *
2581dictiter_reduce(dictiterobject *di)
2582{
2583 PyObject *list;
2584 dictiterobject tmp;
2585
2586 list = PyList_New(0);
2587 if (!list)
2588 return NULL;
2589
2590 /* copy the itertor state */
2591 tmp = *di;
2592 Py_XINCREF(tmp.di_dict);
2593
2594 /* iterate the temporary into a list */
2595 for(;;) {
2596 PyObject *element = 0;
2597 if (Py_TYPE(di) == &PyDictIterItem_Type)
2598 element = dictiter_iternextitem(&tmp);
2599 else if (Py_TYPE(di) == &PyDictIterKey_Type)
2600 element = dictiter_iternextkey(&tmp);
2601 else if (Py_TYPE(di) == &PyDictIterValue_Type)
2602 element = dictiter_iternextvalue(&tmp);
2603 else
2604 assert(0);
2605 if (element) {
2606 if (PyList_Append(list, element)) {
2607 Py_DECREF(element);
2608 Py_DECREF(list);
2609 Py_XDECREF(tmp.di_dict);
2610 return NULL;
2611 }
2612 Py_DECREF(element);
2613 } else
2614 break;
2615 }
2616 Py_XDECREF(tmp.di_dict);
2617 /* check for error */
2618 if (tmp.di_dict != NULL) {
2619 /* we have an error */
2620 Py_DECREF(list);
2621 return NULL;
2622 }
Antoine Pitroua7013882012-04-05 00:04:20 +02002623 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
Kristján Valur Jónsson31668b82012-04-03 10:49:41 +00002624}
2625
Guido van Rossum3ac67412007-02-10 18:55:06 +00002626/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002627/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002628/***********************************************/
2629
Guido van Rossumb90c8482007-02-10 01:11:45 +00002630/* The instance lay-out is the same for all three; but the type differs. */
2631
2632typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002633 PyObject_HEAD
2634 PyDictObject *dv_dict;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002635} dictviewobject;
2636
2637
2638static void
Guido van Rossum3ac67412007-02-10 18:55:06 +00002639dictview_dealloc(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002640{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002641 Py_XDECREF(dv->dv_dict);
2642 PyObject_GC_Del(dv);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002643}
2644
2645static int
2646dictview_traverse(dictviewobject *dv, visitproc visit, void *arg)
2647{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002648 Py_VISIT(dv->dv_dict);
2649 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002650}
2651
Guido van Rossum83825ac2007-02-10 04:54:19 +00002652static Py_ssize_t
Guido van Rossum3ac67412007-02-10 18:55:06 +00002653dictview_len(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002654{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002655 Py_ssize_t len = 0;
2656 if (dv->dv_dict != NULL)
2657 len = dv->dv_dict->ma_used;
2658 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002659}
2660
2661static PyObject *
2662dictview_new(PyObject *dict, PyTypeObject *type)
2663{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002664 dictviewobject *dv;
2665 if (dict == NULL) {
2666 PyErr_BadInternalCall();
2667 return NULL;
2668 }
2669 if (!PyDict_Check(dict)) {
2670 /* XXX Get rid of this restriction later */
2671 PyErr_Format(PyExc_TypeError,
2672 "%s() requires a dict argument, not '%s'",
2673 type->tp_name, dict->ob_type->tp_name);
2674 return NULL;
2675 }
2676 dv = PyObject_GC_New(dictviewobject, type);
2677 if (dv == NULL)
2678 return NULL;
2679 Py_INCREF(dict);
2680 dv->dv_dict = (PyDictObject *)dict;
2681 _PyObject_GC_TRACK(dv);
2682 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002683}
2684
Neal Norwitze36f2ba2007-02-26 23:12:28 +00002685/* TODO(guido): The views objects are not complete:
2686
2687 * support more set operations
2688 * support arbitrary mappings?
2689 - either these should be static or exported in dictobject.h
2690 - if public then they should probably be in builtins
2691*/
2692
Guido van Rossumaac530c2007-08-24 22:33:45 +00002693/* Return 1 if self is a subset of other, iterating over self;
2694 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002695static int
2696all_contained_in(PyObject *self, PyObject *other)
2697{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002698 PyObject *iter = PyObject_GetIter(self);
2699 int ok = 1;
Guido van Rossumd9214d12007-02-12 02:23:40 +00002700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002701 if (iter == NULL)
2702 return -1;
2703 for (;;) {
2704 PyObject *next = PyIter_Next(iter);
2705 if (next == NULL) {
2706 if (PyErr_Occurred())
2707 ok = -1;
2708 break;
2709 }
2710 ok = PySequence_Contains(other, next);
2711 Py_DECREF(next);
2712 if (ok <= 0)
2713 break;
2714 }
2715 Py_DECREF(iter);
2716 return ok;
Guido van Rossumd9214d12007-02-12 02:23:40 +00002717}
2718
2719static PyObject *
2720dictview_richcompare(PyObject *self, PyObject *other, int op)
2721{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002722 Py_ssize_t len_self, len_other;
2723 int ok;
2724 PyObject *result;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002725
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002726 assert(self != NULL);
2727 assert(PyDictViewSet_Check(self));
2728 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00002729
Brian Curtindfc80e32011-08-10 20:28:54 -05002730 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
2731 Py_RETURN_NOTIMPLEMENTED;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002733 len_self = PyObject_Size(self);
2734 if (len_self < 0)
2735 return NULL;
2736 len_other = PyObject_Size(other);
2737 if (len_other < 0)
2738 return NULL;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002740 ok = 0;
2741 switch(op) {
Guido van Rossumaac530c2007-08-24 22:33:45 +00002742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002743 case Py_NE:
2744 case Py_EQ:
2745 if (len_self == len_other)
2746 ok = all_contained_in(self, other);
2747 if (op == Py_NE && ok >= 0)
2748 ok = !ok;
2749 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 case Py_LT:
2752 if (len_self < len_other)
2753 ok = all_contained_in(self, other);
2754 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002756 case Py_LE:
2757 if (len_self <= len_other)
2758 ok = all_contained_in(self, other);
2759 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002761 case Py_GT:
2762 if (len_self > len_other)
2763 ok = all_contained_in(other, self);
2764 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002766 case Py_GE:
2767 if (len_self >= len_other)
2768 ok = all_contained_in(other, self);
2769 break;
Guido van Rossumaac530c2007-08-24 22:33:45 +00002770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002771 }
2772 if (ok < 0)
2773 return NULL;
2774 result = ok ? Py_True : Py_False;
2775 Py_INCREF(result);
2776 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00002777}
2778
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00002779static PyObject *
2780dictview_repr(dictviewobject *dv)
2781{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002782 PyObject *seq;
2783 PyObject *result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00002784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002785 seq = PySequence_List((PyObject *)dv);
2786 if (seq == NULL)
2787 return NULL;
2788
2789 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
2790 Py_DECREF(seq);
2791 return result;
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00002792}
2793
Guido van Rossum3ac67412007-02-10 18:55:06 +00002794/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002795
2796static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002797dictkeys_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002798{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002799 if (dv->dv_dict == NULL) {
2800 Py_RETURN_NONE;
2801 }
2802 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00002803}
2804
2805static int
2806dictkeys_contains(dictviewobject *dv, PyObject *obj)
2807{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002808 if (dv->dv_dict == NULL)
2809 return 0;
2810 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002811}
2812
Guido van Rossum83825ac2007-02-10 04:54:19 +00002813static PySequenceMethods dictkeys_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002814 (lenfunc)dictview_len, /* sq_length */
2815 0, /* sq_concat */
2816 0, /* sq_repeat */
2817 0, /* sq_item */
2818 0, /* sq_slice */
2819 0, /* sq_ass_item */
2820 0, /* sq_ass_slice */
2821 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002822};
2823
Guido van Rossum523259b2007-08-24 23:41:22 +00002824static PyObject*
2825dictviews_sub(PyObject* self, PyObject *other)
2826{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002827 PyObject *result = PySet_New(self);
2828 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002829 _Py_IDENTIFIER(difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002830
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002831 if (result == NULL)
2832 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00002833
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002834 tmp = _PyObject_CallMethodId(result, &PyId_difference_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002835 if (tmp == NULL) {
2836 Py_DECREF(result);
2837 return NULL;
2838 }
Guido van Rossum523259b2007-08-24 23:41:22 +00002839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002840 Py_DECREF(tmp);
2841 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00002842}
2843
2844static PyObject*
2845dictviews_and(PyObject* self, PyObject *other)
2846{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002847 PyObject *result = PySet_New(self);
2848 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002849 _Py_IDENTIFIER(intersection_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002851 if (result == NULL)
2852 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00002853
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002854 tmp = _PyObject_CallMethodId(result, &PyId_intersection_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002855 if (tmp == NULL) {
2856 Py_DECREF(result);
2857 return NULL;
2858 }
Guido van Rossum523259b2007-08-24 23:41:22 +00002859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002860 Py_DECREF(tmp);
2861 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00002862}
2863
2864static PyObject*
2865dictviews_or(PyObject* self, PyObject *other)
2866{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002867 PyObject *result = PySet_New(self);
2868 PyObject *tmp;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02002869 _Py_IDENTIFIER(update);
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02002870
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002871 if (result == NULL)
2872 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00002873
Victor Stinnerd1a9cc22011-10-13 22:51:17 +02002874 tmp = _PyObject_CallMethodId(result, &PyId_update, "O", other);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002875 if (tmp == NULL) {
2876 Py_DECREF(result);
2877 return NULL;
2878 }
Guido van Rossum523259b2007-08-24 23:41:22 +00002879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 Py_DECREF(tmp);
2881 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00002882}
2883
2884static PyObject*
2885dictviews_xor(PyObject* self, PyObject *other)
2886{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002887 PyObject *result = PySet_New(self);
2888 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002889 _Py_IDENTIFIER(symmetric_difference_update);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002891 if (result == NULL)
2892 return NULL;
Guido van Rossum523259b2007-08-24 23:41:22 +00002893
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002894 tmp = _PyObject_CallMethodId(result, &PyId_symmetric_difference_update, "O",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002895 other);
2896 if (tmp == NULL) {
2897 Py_DECREF(result);
2898 return NULL;
2899 }
Guido van Rossum523259b2007-08-24 23:41:22 +00002900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 Py_DECREF(tmp);
2902 return result;
Guido van Rossum523259b2007-08-24 23:41:22 +00002903}
2904
2905static PyNumberMethods dictviews_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002906 0, /*nb_add*/
2907 (binaryfunc)dictviews_sub, /*nb_subtract*/
2908 0, /*nb_multiply*/
2909 0, /*nb_remainder*/
2910 0, /*nb_divmod*/
2911 0, /*nb_power*/
2912 0, /*nb_negative*/
2913 0, /*nb_positive*/
2914 0, /*nb_absolute*/
2915 0, /*nb_bool*/
2916 0, /*nb_invert*/
2917 0, /*nb_lshift*/
2918 0, /*nb_rshift*/
2919 (binaryfunc)dictviews_and, /*nb_and*/
2920 (binaryfunc)dictviews_xor, /*nb_xor*/
2921 (binaryfunc)dictviews_or, /*nb_or*/
Guido van Rossum523259b2007-08-24 23:41:22 +00002922};
2923
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00002924static PyObject*
2925dictviews_isdisjoint(PyObject *self, PyObject *other)
2926{
2927 PyObject *it;
2928 PyObject *item = NULL;
2929
2930 if (self == other) {
2931 if (dictview_len((dictviewobject *)self) == 0)
2932 Py_RETURN_TRUE;
2933 else
2934 Py_RETURN_FALSE;
2935 }
2936
2937 /* Iterate over the shorter object (only if other is a set,
2938 * because PySequence_Contains may be expensive otherwise): */
2939 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
2940 Py_ssize_t len_self = dictview_len((dictviewobject *)self);
2941 Py_ssize_t len_other = PyObject_Size(other);
2942 if (len_other == -1)
2943 return NULL;
2944
2945 if ((len_other > len_self)) {
2946 PyObject *tmp = other;
2947 other = self;
2948 self = tmp;
2949 }
2950 }
2951
2952 it = PyObject_GetIter(other);
2953 if (it == NULL)
2954 return NULL;
2955
2956 while ((item = PyIter_Next(it)) != NULL) {
2957 int contains = PySequence_Contains(self, item);
2958 Py_DECREF(item);
2959 if (contains == -1) {
2960 Py_DECREF(it);
2961 return NULL;
2962 }
2963
2964 if (contains) {
2965 Py_DECREF(it);
2966 Py_RETURN_FALSE;
2967 }
2968 }
2969 Py_DECREF(it);
2970 if (PyErr_Occurred())
2971 return NULL; /* PyIter_Next raised an exception. */
2972 Py_RETURN_TRUE;
2973}
2974
2975PyDoc_STRVAR(isdisjoint_doc,
2976"Return True if the view and the given iterable have a null intersection.");
2977
Guido van Rossumb90c8482007-02-10 01:11:45 +00002978static PyMethodDef dictkeys_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00002979 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
2980 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002981 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002982};
2983
2984PyTypeObject PyDictKeys_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002985 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2986 "dict_keys", /* tp_name */
2987 sizeof(dictviewobject), /* tp_basicsize */
2988 0, /* tp_itemsize */
2989 /* methods */
2990 (destructor)dictview_dealloc, /* tp_dealloc */
2991 0, /* tp_print */
2992 0, /* tp_getattr */
2993 0, /* tp_setattr */
2994 0, /* tp_reserved */
2995 (reprfunc)dictview_repr, /* tp_repr */
2996 &dictviews_as_number, /* tp_as_number */
2997 &dictkeys_as_sequence, /* tp_as_sequence */
2998 0, /* tp_as_mapping */
2999 0, /* tp_hash */
3000 0, /* tp_call */
3001 0, /* tp_str */
3002 PyObject_GenericGetAttr, /* tp_getattro */
3003 0, /* tp_setattro */
3004 0, /* tp_as_buffer */
3005 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3006 0, /* tp_doc */
3007 (traverseproc)dictview_traverse, /* tp_traverse */
3008 0, /* tp_clear */
3009 dictview_richcompare, /* tp_richcompare */
3010 0, /* tp_weaklistoffset */
3011 (getiterfunc)dictkeys_iter, /* tp_iter */
3012 0, /* tp_iternext */
3013 dictkeys_methods, /* tp_methods */
3014 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003015};
3016
3017static PyObject *
3018dictkeys_new(PyObject *dict)
3019{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003020 return dictview_new(dict, &PyDictKeys_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003021}
3022
Guido van Rossum3ac67412007-02-10 18:55:06 +00003023/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003024
3025static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003026dictitems_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003027{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003028 if (dv->dv_dict == NULL) {
3029 Py_RETURN_NONE;
3030 }
3031 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00003032}
3033
3034static int
3035dictitems_contains(dictviewobject *dv, PyObject *obj)
3036{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003037 PyObject *key, *value, *found;
3038 if (dv->dv_dict == NULL)
3039 return 0;
3040 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
3041 return 0;
3042 key = PyTuple_GET_ITEM(obj, 0);
3043 value = PyTuple_GET_ITEM(obj, 1);
3044 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
3045 if (found == NULL) {
3046 if (PyErr_Occurred())
3047 return -1;
3048 return 0;
3049 }
3050 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003051}
3052
Guido van Rossum83825ac2007-02-10 04:54:19 +00003053static PySequenceMethods dictitems_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003054 (lenfunc)dictview_len, /* sq_length */
3055 0, /* sq_concat */
3056 0, /* sq_repeat */
3057 0, /* sq_item */
3058 0, /* sq_slice */
3059 0, /* sq_ass_item */
3060 0, /* sq_ass_slice */
3061 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003062};
3063
Guido van Rossumb90c8482007-02-10 01:11:45 +00003064static PyMethodDef dictitems_methods[] = {
Daniel Stutzbach045b3ba2010-09-02 15:06:06 +00003065 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
3066 isdisjoint_doc},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003067 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003068};
3069
3070PyTypeObject PyDictItems_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003071 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3072 "dict_items", /* tp_name */
3073 sizeof(dictviewobject), /* tp_basicsize */
3074 0, /* tp_itemsize */
3075 /* methods */
3076 (destructor)dictview_dealloc, /* tp_dealloc */
3077 0, /* tp_print */
3078 0, /* tp_getattr */
3079 0, /* tp_setattr */
3080 0, /* tp_reserved */
3081 (reprfunc)dictview_repr, /* tp_repr */
3082 &dictviews_as_number, /* tp_as_number */
3083 &dictitems_as_sequence, /* tp_as_sequence */
3084 0, /* tp_as_mapping */
3085 0, /* tp_hash */
3086 0, /* tp_call */
3087 0, /* tp_str */
3088 PyObject_GenericGetAttr, /* tp_getattro */
3089 0, /* tp_setattro */
3090 0, /* tp_as_buffer */
3091 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3092 0, /* tp_doc */
3093 (traverseproc)dictview_traverse, /* tp_traverse */
3094 0, /* tp_clear */
3095 dictview_richcompare, /* tp_richcompare */
3096 0, /* tp_weaklistoffset */
3097 (getiterfunc)dictitems_iter, /* tp_iter */
3098 0, /* tp_iternext */
3099 dictitems_methods, /* tp_methods */
3100 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003101};
3102
3103static PyObject *
3104dictitems_new(PyObject *dict)
3105{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003106 return dictview_new(dict, &PyDictItems_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003107}
3108
Guido van Rossum3ac67412007-02-10 18:55:06 +00003109/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00003110
3111static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00003112dictvalues_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00003113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003114 if (dv->dv_dict == NULL) {
3115 Py_RETURN_NONE;
3116 }
3117 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003118}
3119
Guido van Rossum83825ac2007-02-10 04:54:19 +00003120static PySequenceMethods dictvalues_as_sequence = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003121 (lenfunc)dictview_len, /* sq_length */
3122 0, /* sq_concat */
3123 0, /* sq_repeat */
3124 0, /* sq_item */
3125 0, /* sq_slice */
3126 0, /* sq_ass_item */
3127 0, /* sq_ass_slice */
3128 (objobjproc)0, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00003129};
3130
Guido van Rossumb90c8482007-02-10 01:11:45 +00003131static PyMethodDef dictvalues_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003132 {NULL, NULL} /* sentinel */
Guido van Rossumb90c8482007-02-10 01:11:45 +00003133};
3134
3135PyTypeObject PyDictValues_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003136 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3137 "dict_values", /* tp_name */
3138 sizeof(dictviewobject), /* tp_basicsize */
3139 0, /* tp_itemsize */
3140 /* methods */
3141 (destructor)dictview_dealloc, /* tp_dealloc */
3142 0, /* tp_print */
3143 0, /* tp_getattr */
3144 0, /* tp_setattr */
3145 0, /* tp_reserved */
3146 (reprfunc)dictview_repr, /* tp_repr */
3147 0, /* tp_as_number */
3148 &dictvalues_as_sequence, /* tp_as_sequence */
3149 0, /* tp_as_mapping */
3150 0, /* tp_hash */
3151 0, /* tp_call */
3152 0, /* tp_str */
3153 PyObject_GenericGetAttr, /* tp_getattro */
3154 0, /* tp_setattro */
3155 0, /* tp_as_buffer */
3156 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
3157 0, /* tp_doc */
3158 (traverseproc)dictview_traverse, /* tp_traverse */
3159 0, /* tp_clear */
3160 0, /* tp_richcompare */
3161 0, /* tp_weaklistoffset */
3162 (getiterfunc)dictvalues_iter, /* tp_iter */
3163 0, /* tp_iternext */
3164 dictvalues_methods, /* tp_methods */
3165 0,
Guido van Rossumb90c8482007-02-10 01:11:45 +00003166};
3167
3168static PyObject *
3169dictvalues_new(PyObject *dict)
3170{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003171 return dictview_new(dict, &PyDictValues_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00003172}