blob: f13361472edeaaecd6a3fd7b6245b83e7218d0ab [file] [log] [blame]
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001
Guido van Rossum2bc13791999-03-24 19:06:42 +00002/* Dictionary object implementation using a hash table */
Guido van Rossum9bfef441993-03-29 10:43:31 +00003
Raymond Hettinger930427b2003-05-03 06:51:59 +00004/* The distribution includes a separate file, Objects/dictnotes.txt,
Tim Peters60b29962006-01-01 01:19:23 +00005 describing explorations into dictionary design and optimization.
Raymond Hettinger930427b2003-05-03 06:51:59 +00006 It covers typical dictionary use patterns, the parameters for
7 tuning dictionaries, and several ideas for possible optimizations.
8*/
9
Guido van Rossumc0b618a1997-05-02 03:12:38 +000010#include "Python.h"
Christian Heimes0ded5b52007-12-10 15:50:56 +000011#include "stringlib/eq.h"
Guido van Rossum4b1302b1993-03-27 18:11:32 +000012
Guido van Rossum16e93a81997-01-28 00:00:11 +000013
Thomas Wouters89f507f2006-12-13 04:49:30 +000014/* Set a key error with the specified argument, wrapping it in a
15 * tuple automatically so that tuple keys are not unpacked as the
16 * exception arguments. */
17static void
18set_key_error(PyObject *arg)
19{
20 PyObject *tup;
21 tup = PyTuple_Pack(1, arg);
22 if (!tup)
23 return; /* caller will expect error to be set anyway */
24 PyErr_SetObject(PyExc_KeyError, tup);
25 Py_DECREF(tup);
26}
27
Tim Peterseb28ef22001-06-02 05:27:19 +000028/* Define this out if you don't want conversion statistics on exit. */
Fred Drake1bff34a2000-08-31 19:31:38 +000029#undef SHOW_CONVERSION_COUNTS
30
Tim Peterseb28ef22001-06-02 05:27:19 +000031/* See large comment block below. This must be >= 1. */
32#define PERTURB_SHIFT 5
33
Guido van Rossum16e93a81997-01-28 00:00:11 +000034/*
Tim Peterseb28ef22001-06-02 05:27:19 +000035Major subtleties ahead: Most hash schemes depend on having a "good" hash
36function, in the sense of simulating randomness. Python doesn't: its most
37important hash functions (for strings and ints) are very regular in common
38cases:
Tim Peters15d49292001-05-27 07:39:22 +000039
Guido van Rossumdc5f6b22006-08-24 21:29:26 +000040 >>> map(hash, (0, 1, 2, 3))
41 [0, 1, 2, 3]
42 >>> map(hash, ("namea", "nameb", "namec", "named"))
43 [-1658398457, -1658398460, -1658398459, -1658398462]
44 >>>
Tim Peters15d49292001-05-27 07:39:22 +000045
Tim Peterseb28ef22001-06-02 05:27:19 +000046This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
47the low-order i bits as the initial table index is extremely fast, and there
48are no collisions at all for dicts indexed by a contiguous range of ints.
49The same is approximately true when keys are "consecutive" strings. So this
50gives better-than-random behavior in common cases, and that's very desirable.
Tim Peters15d49292001-05-27 07:39:22 +000051
Tim Peterseb28ef22001-06-02 05:27:19 +000052OTOH, when collisions occur, the tendency to fill contiguous slices of the
53hash table makes a good collision resolution strategy crucial. Taking only
54the last i bits of the hash code is also vulnerable: for example, consider
Guido van Rossumdc5f6b22006-08-24 21:29:26 +000055the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
56their own hash codes, and this fits in a dict of size 2**15, the last 15 bits
57 of every hash code are all 0: they *all* map to the same table index.
Tim Peters15d49292001-05-27 07:39:22 +000058
Tim Peterseb28ef22001-06-02 05:27:19 +000059But catering to unusual cases should not slow the usual ones, so we just take
60the last i bits anyway. It's up to collision resolution to do the rest. If
61we *usually* find the key we're looking for on the first try (and, it turns
62out, we usually do -- the table load factor is kept under 2/3, so the odds
63are solidly in our favor), then it makes best sense to keep the initial index
64computation dirt cheap.
Tim Peters15d49292001-05-27 07:39:22 +000065
Tim Peterseb28ef22001-06-02 05:27:19 +000066The first half of collision resolution is to visit table indices via this
67recurrence:
Tim Peters15d49292001-05-27 07:39:22 +000068
Tim Peterseb28ef22001-06-02 05:27:19 +000069 j = ((5*j) + 1) mod 2**i
Tim Peters15d49292001-05-27 07:39:22 +000070
Tim Peterseb28ef22001-06-02 05:27:19 +000071For any initial j in range(2**i), repeating that 2**i times generates each
72int in range(2**i) exactly once (see any text on random-number generation for
73proof). By itself, this doesn't help much: like linear probing (setting
74j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
75order. This would be bad, except that's not the only thing we do, and it's
76actually *good* in the common cases where hash keys are consecutive. In an
77example that's really too small to make this entirely clear, for a table of
78size 2**3 the order of indices is:
Tim Peters15d49292001-05-27 07:39:22 +000079
Tim Peterseb28ef22001-06-02 05:27:19 +000080 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
81
82If two things come in at index 5, the first place we look after is index 2,
83not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
84Linear probing is deadly in this case because there the fixed probe order
85is the *same* as the order consecutive keys are likely to arrive. But it's
86extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
87and certain that consecutive hash codes do not.
88
89The other half of the strategy is to get the other bits of the hash code
90into play. This is done by initializing a (unsigned) vrbl "perturb" to the
91full hash code, and changing the recurrence to:
92
93 j = (5*j) + 1 + perturb;
94 perturb >>= PERTURB_SHIFT;
95 use j % 2**i as the next table index;
96
97Now the probe sequence depends (eventually) on every bit in the hash code,
98and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
99because it quickly magnifies small differences in the bits that didn't affect
100the initial index. Note that because perturb is unsigned, if the recurrence
101is executed often enough perturb eventually becomes and remains 0. At that
102point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
103that's certain to find an empty slot eventually (since it generates every int
104in range(2**i), and we make sure there's always at least one empty slot).
105
106Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
107small so that the high bits of the hash code continue to affect the probe
108sequence across iterations; but you want it large so that in really bad cases
109the high-order hash bits have an effect on early iterations. 5 was "the
110best" in minimizing total collisions across experiments Tim Peters ran (on
111both normal and pathological cases), but 4 and 6 weren't significantly worse.
112
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000113Historical: Reimer Behrends contributed the idea of using a polynomial-based
Tim Peterseb28ef22001-06-02 05:27:19 +0000114approach, using repeated multiplication by x in GF(2**n) where an irreducible
115polynomial for each table size was chosen such that x was a primitive root.
116Christian Tismer later extended that to use division by x instead, as an
117efficient way to get the high bits of the hash code into play. This scheme
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000118also gave excellent collision statistics, but was more expensive: two
119if-tests were required inside the loop; computing "the next" index took about
120the same number of operations but without as much potential parallelism
121(e.g., computing 5*j can go on at the same time as computing 1+perturb in the
122above, and then shifting perturb can be done while the table index is being
123masked); and the PyDictObject struct required a member to hold the table's
124polynomial. In Tim's experiments the current scheme ran faster, produced
125equally good collision statistics, needed less code & used less memory.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000126
127Theoretical Python 2.5 headache: hash codes are only C "long", but
128sizeof(Py_ssize_t) > sizeof(long) may be possible. In that case, and if a
129dict is genuinely huge, then only the slots directly reachable via indexing
130by a C long can be the first slot in a probe sequence. The probe sequence
131will still eventually reach every slot in the table, but the collision rate
132on initial probes may be much higher than this scheme was designed for.
133Getting a hash code as fat as Py_ssize_t is the only real cure. But in
134practice, this probably won't make a lick of difference for many years (at
135which point everyone will have terabytes of RAM on 64-bit boxes).
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000136*/
Tim Petersdea48ec2001-05-22 20:40:22 +0000137
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000138/* Object used as dummy key to fill deleted entries */
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000139static PyObject *dummy = NULL; /* Initialized by first call to newPyDictObject() */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000140
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000141#ifdef Py_REF_DEBUG
142PyObject *
143_PyDict_Dummy(void)
144{
145 return dummy;
146}
147#endif
148
Fred Drake1bff34a2000-08-31 19:31:38 +0000149/* forward declarations */
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000150static PyDictEntry *
151lookdict_unicode(PyDictObject *mp, PyObject *key, long hash);
Fred Drake1bff34a2000-08-31 19:31:38 +0000152
153#ifdef SHOW_CONVERSION_COUNTS
154static long created = 0L;
155static long converted = 0L;
156
157static void
158show_counts(void)
159{
160 fprintf(stderr, "created %ld string dicts\n", created);
161 fprintf(stderr, "converted %ld to normal dicts\n", converted);
162 fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created);
163}
164#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000165
Christian Heimes77c02eb2008-02-09 02:18:51 +0000166/* Debug statistic to compare allocations with reuse through the free list */
167#undef SHOW_ALLOC_COUNT
168#ifdef SHOW_ALLOC_COUNT
169static size_t count_alloc = 0;
170static size_t count_reuse = 0;
171
172static void
173show_alloc(void)
174{
Christian Heimes23daade02008-02-25 12:39:23 +0000175 fprintf(stderr, "Dict allocations: %" PY_FORMAT_SIZE_T "d\n",
176 count_alloc);
177 fprintf(stderr, "Dict reuse through freelist: %" PY_FORMAT_SIZE_T
178 "d\n", count_reuse);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000179 fprintf(stderr, "%.2f%% reuse rate\n\n",
180 (100.0*count_reuse/(count_alloc+count_reuse)));
181}
182#endif
183
Tim Peters6d6c1a32001-08-02 04:15:00 +0000184/* Initialization macros.
185 There are two ways to create a dict: PyDict_New() is the main C API
186 function, and the tp_new slot maps to dict_new(). In the latter case we
187 can save a little time over what PyDict_New does because it's guaranteed
188 that the PyDictObject struct is already zeroed out.
189 Everyone except dict_new() should use EMPTY_TO_MINSIZE (unless they have
190 an excellent reason not to).
191*/
192
193#define INIT_NONZERO_DICT_SLOTS(mp) do { \
Tim Petersdea48ec2001-05-22 20:40:22 +0000194 (mp)->ma_table = (mp)->ma_smalltable; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000195 (mp)->ma_mask = PyDict_MINSIZE - 1; \
196 } while(0)
197
198#define EMPTY_TO_MINSIZE(mp) do { \
199 memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000200 (mp)->ma_used = (mp)->ma_fill = 0; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000201 INIT_NONZERO_DICT_SLOTS(mp); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000202 } while(0)
203
Raymond Hettinger43442782004-03-17 21:55:03 +0000204/* Dictionary reuse scheme to save calls to malloc, free, and memset */
Christian Heimes2202f872008-02-06 14:31:34 +0000205#ifndef PyDict_MAXFREELIST
206#define PyDict_MAXFREELIST 80
207#endif
208static PyDictObject *free_list[PyDict_MAXFREELIST];
209static int numfree = 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000210
Christian Heimes77c02eb2008-02-09 02:18:51 +0000211void
212PyDict_Fini(void)
213{
214 PyDictObject *op;
215
216 while (numfree) {
217 op = free_list[--numfree];
218 assert(PyDict_CheckExact(op));
219 PyObject_GC_Del(op);
220 }
221}
222
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000223PyObject *
Thomas Wouters78890102000-07-22 19:25:51 +0000224PyDict_New(void)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000225{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000226 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000227 if (dummy == NULL) { /* Auto-initialize dummy */
Neal Norwitz53cbdaa2007-08-23 21:42:55 +0000228 dummy = PyUnicode_FromString("<dummy key>");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000229 if (dummy == NULL)
230 return NULL;
Fred Drake1bff34a2000-08-31 19:31:38 +0000231#ifdef SHOW_CONVERSION_COUNTS
232 Py_AtExit(show_counts);
233#endif
Christian Heimes77c02eb2008-02-09 02:18:51 +0000234#ifdef SHOW_ALLOC_COUNT
235 Py_AtExit(show_alloc);
236#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000237 }
Christian Heimes2202f872008-02-06 14:31:34 +0000238 if (numfree) {
239 mp = free_list[--numfree];
Raymond Hettinger43442782004-03-17 21:55:03 +0000240 assert (mp != NULL);
Christian Heimes90aa7642007-12-19 02:45:37 +0000241 assert (Py_TYPE(mp) == &PyDict_Type);
Raymond Hettinger43442782004-03-17 21:55:03 +0000242 _Py_NewReference((PyObject *)mp);
243 if (mp->ma_fill) {
244 EMPTY_TO_MINSIZE(mp);
245 }
246 assert (mp->ma_used == 0);
247 assert (mp->ma_table == mp->ma_smalltable);
248 assert (mp->ma_mask == PyDict_MINSIZE - 1);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000249#ifdef SHOW_ALLOC_COUNT
250 count_reuse++;
251#endif
Raymond Hettinger43442782004-03-17 21:55:03 +0000252 } else {
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000253 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
Raymond Hettinger43442782004-03-17 21:55:03 +0000254 if (mp == NULL)
255 return NULL;
256 EMPTY_TO_MINSIZE(mp);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000257#ifdef SHOW_ALLOC_COUNT
258 count_alloc++;
259#endif
Raymond Hettinger43442782004-03-17 21:55:03 +0000260 }
Guido van Rossum89d8c602007-09-18 17:26:56 +0000261 mp->ma_lookup = lookdict_unicode;
Fred Drake1bff34a2000-08-31 19:31:38 +0000262#ifdef SHOW_CONVERSION_COUNTS
263 ++created;
264#endif
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000265 _PyObject_GC_TRACK(mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000266 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000267}
268
269/*
270The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000271This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000272Open addressing is preferred over chaining since the link overhead for
273chaining would be substantial (100% with typical malloc overhead).
274
Tim Peterseb28ef22001-06-02 05:27:19 +0000275The initial probe index is computed as hash mod the table size. Subsequent
276probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000277
278All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000279
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000280The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000281contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000282Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000283
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000284lookdict() is general-purpose, and may return NULL if (and only if) a
285comparison raises an exception (this was new in Python 2.5).
Guido van Rossum89d8c602007-09-18 17:26:56 +0000286lookdict_unicode() below is specialized to string keys, comparison of which can
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000287never raise an exception; that function can never return NULL. For both, when
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000288the key isn't found a PyDictEntry* is returned for which the me_value field is
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000289NULL; this is the slot in the dict at which the key would have been found, and
290the caller can (if it wishes) add the <key, value> pair to the returned
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000291PyDictEntry*.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000292*/
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000293static PyDictEntry *
294lookdict(PyDictObject *mp, PyObject *key, register long hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000295{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000296 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000297 register size_t perturb;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000298 register PyDictEntry *freeslot;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000299 register size_t mask = (size_t)mp->ma_mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000300 PyDictEntry *ep0 = mp->ma_table;
301 register PyDictEntry *ep;
Fred Drakec88b99c2000-08-31 19:04:07 +0000302 register int cmp;
Tim Peters453163d2001-06-03 04:54:32 +0000303 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000304
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000305 i = (size_t)hash & mask;
Guido van Rossumefb46091997-01-29 15:53:56 +0000306 ep = &ep0[i];
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000307 if (ep->me_key == NULL || ep->me_key == key)
Guido van Rossum7d181591997-01-16 21:06:45 +0000308 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000309
Guido van Rossum16e93a81997-01-28 00:00:11 +0000310 if (ep->me_key == dummy)
Guido van Rossum7d181591997-01-16 21:06:45 +0000311 freeslot = ep;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000312 else {
Fred Drakec88b99c2000-08-31 19:04:07 +0000313 if (ep->me_hash == hash) {
Tim Peters453163d2001-06-03 04:54:32 +0000314 startkey = ep->me_key;
Guido van Rossum2fd4f372007-11-29 18:43:05 +0000315 Py_INCREF(startkey);
Tim Peters453163d2001-06-03 04:54:32 +0000316 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Guido van Rossum2fd4f372007-11-29 18:43:05 +0000317 Py_DECREF(startkey);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000318 if (cmp < 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000319 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000320 if (ep0 == mp->ma_table && ep->me_key == startkey) {
321 if (cmp > 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000322 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000323 }
324 else {
325 /* The compare did major nasty stuff to the
326 * dict: start over.
327 * XXX A clever adversary could prevent this
328 * XXX from terminating.
329 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000330 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000331 }
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000332 }
333 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000334 }
Tim Peters15d49292001-05-27 07:39:22 +0000335
Tim Peters342c65e2001-05-13 06:43:53 +0000336 /* In the loop, me_key == dummy is by far (factor of 100s) the
337 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000338 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
339 i = (i << 2) + i + perturb + 1;
340 ep = &ep0[i & mask];
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000341 if (ep->me_key == NULL)
342 return freeslot == NULL ? ep : freeslot;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000343 if (ep->me_key == key)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000344 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000345 if (ep->me_hash == hash && ep->me_key != dummy) {
Tim Peters453163d2001-06-03 04:54:32 +0000346 startkey = ep->me_key;
Guido van Rossum2fd4f372007-11-29 18:43:05 +0000347 Py_INCREF(startkey);
Tim Peters453163d2001-06-03 04:54:32 +0000348 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Guido van Rossum2fd4f372007-11-29 18:43:05 +0000349 Py_DECREF(startkey);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000350 if (cmp < 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000351 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000352 if (ep0 == mp->ma_table && ep->me_key == startkey) {
353 if (cmp > 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000354 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000355 }
356 else {
357 /* The compare did major nasty stuff to the
358 * dict: start over.
359 * XXX A clever adversary could prevent this
360 * XXX from terminating.
361 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000362 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000363 }
Guido van Rossum16e93a81997-01-28 00:00:11 +0000364 }
Tim Peters342c65e2001-05-13 06:43:53 +0000365 else if (ep->me_key == dummy && freeslot == NULL)
366 freeslot = ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000367 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000368 assert(0); /* NOT REACHED */
369 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000370}
371
372/*
Guido van Rossum89d8c602007-09-18 17:26:56 +0000373 * Hacked up version of lookdict which can assume keys are always
374 * unicodes; this assumption allows testing for errors during
375 * PyObject_RichCompareBool() to be dropped; unicode-unicode
376 * comparisons never raise exceptions. This also means we don't need
377 * to go through PyObject_RichCompareBool(); we can always use
378 * unicode_eq() directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000379 *
Guido van Rossum89d8c602007-09-18 17:26:56 +0000380 * This is valuable because dicts with only unicode keys are very common.
Fred Drake1bff34a2000-08-31 19:31:38 +0000381 */
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000382static PyDictEntry *
383lookdict_unicode(PyDictObject *mp, PyObject *key, register long hash)
Fred Drake1bff34a2000-08-31 19:31:38 +0000384{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000385 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000386 register size_t perturb;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000387 register PyDictEntry *freeslot;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000388 register size_t mask = (size_t)mp->ma_mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000389 PyDictEntry *ep0 = mp->ma_table;
390 register PyDictEntry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000391
Guido van Rossum89d8c602007-09-18 17:26:56 +0000392 /* Make sure this function doesn't have to handle non-unicode keys,
Tim Peters0ab085c2001-09-14 00:25:33 +0000393 including subclasses of str; e.g., one reason to subclass
Guido van Rossum89d8c602007-09-18 17:26:56 +0000394 unicodes is to override __eq__, and for speed we don't cater to
Tim Peters0ab085c2001-09-14 00:25:33 +0000395 that here. */
Guido van Rossum89d8c602007-09-18 17:26:56 +0000396 if (!PyUnicode_CheckExact(key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000397#ifdef SHOW_CONVERSION_COUNTS
398 ++converted;
399#endif
400 mp->ma_lookup = lookdict;
401 return lookdict(mp, key, hash);
402 }
Tim Peters2f228e72001-05-13 00:19:31 +0000403 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000404 ep = &ep0[i];
405 if (ep->me_key == NULL || ep->me_key == key)
406 return ep;
407 if (ep->me_key == dummy)
408 freeslot = ep;
409 else {
Guido van Rossum89d8c602007-09-18 17:26:56 +0000410 if (ep->me_hash == hash && unicode_eq(ep->me_key, key))
Fred Drake1bff34a2000-08-31 19:31:38 +0000411 return ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000412 freeslot = NULL;
413 }
Tim Peters15d49292001-05-27 07:39:22 +0000414
Tim Peters342c65e2001-05-13 06:43:53 +0000415 /* In the loop, me_key == dummy is by far (factor of 100s) the
416 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000417 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
418 i = (i << 2) + i + perturb + 1;
419 ep = &ep0[i & mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000420 if (ep->me_key == NULL)
421 return freeslot == NULL ? ep : freeslot;
422 if (ep->me_key == key
423 || (ep->me_hash == hash
424 && ep->me_key != dummy
Guido van Rossum89d8c602007-09-18 17:26:56 +0000425 && unicode_eq(ep->me_key, key)))
Fred Drake1bff34a2000-08-31 19:31:38 +0000426 return ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000427 if (ep->me_key == dummy && freeslot == NULL)
Tim Peters342c65e2001-05-13 06:43:53 +0000428 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000429 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000430 assert(0); /* NOT REACHED */
431 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000432}
433
434/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000435Internal routine to insert a new item into the table.
436Used both by the internal resize routine and by the public insert routine.
437Eats a reference to key and one to value.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000438Returns -1 if an error occurred, or 0 on success.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000439*/
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000440static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000441insertdict(register PyDictObject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000442{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000443 PyObject *old_value;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000444 register PyDictEntry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000445 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
446
447 assert(mp->ma_lookup != NULL);
448 ep = mp->ma_lookup(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000449 if (ep == NULL) {
450 Py_DECREF(key);
451 Py_DECREF(value);
452 return -1;
453 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000454 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000455 old_value = ep->me_value;
456 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000457 Py_DECREF(old_value); /* which **CAN** re-enter */
458 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000459 }
460 else {
461 if (ep->me_key == NULL)
462 mp->ma_fill++;
Raymond Hettinger07ead172005-02-05 23:42:57 +0000463 else {
464 assert(ep->me_key == dummy);
465 Py_DECREF(dummy);
466 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000467 ep->me_key = key;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000468 ep->me_hash = (Py_ssize_t)hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000469 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000470 mp->ma_used++;
471 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000472 return 0;
473}
474
475/*
476Internal routine used by dictresize() to insert an item which is
477known to be absent from the dict. This routine also assumes that
478the dict contains no deleted entries. Besides the performance benefit,
479using insertdict() in dictresize() is dangerous (SF bug #1456209).
480Note that no refcounts are changed by this routine; if needed, the caller
481is responsible for incref'ing `key` and `value`.
482*/
483static void
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000484insertdict_clean(register PyDictObject *mp, PyObject *key, long hash,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000485 PyObject *value)
486{
487 register size_t i;
488 register size_t perturb;
489 register size_t mask = (size_t)mp->ma_mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000490 PyDictEntry *ep0 = mp->ma_table;
491 register PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000492
493 i = hash & mask;
494 ep = &ep0[i];
495 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
496 i = (i << 2) + i + perturb + 1;
497 ep = &ep0[i & mask];
498 }
499 assert(ep->me_value == NULL);
500 mp->ma_fill++;
501 ep->me_key = key;
502 ep->me_hash = (Py_ssize_t)hash;
503 ep->me_value = value;
504 mp->ma_used++;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000505}
506
507/*
508Restructure the table by allocating a new table and reinserting all
509items again. When entries have been deleted, the new table may
510actually be smaller than the old one.
511*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000512static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000513dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000514{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000515 Py_ssize_t newsize;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000516 PyDictEntry *oldtable, *newtable, *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000517 Py_ssize_t i;
Tim Peters0c6010b2001-05-23 23:33:57 +0000518 int is_oldtable_malloced;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000519 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000520
521 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000522
Tim Peterseb28ef22001-06-02 05:27:19 +0000523 /* Find the smallest table size > minused. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000524 for (newsize = PyDict_MINSIZE;
Tim Peterseb28ef22001-06-02 05:27:19 +0000525 newsize <= minused && newsize > 0;
526 newsize <<= 1)
527 ;
528 if (newsize <= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000529 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000530 return -1;
531 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000532
533 /* Get space for a new table. */
534 oldtable = mp->ma_table;
535 assert(oldtable != NULL);
536 is_oldtable_malloced = oldtable != mp->ma_smalltable;
537
Tim Peters6d6c1a32001-08-02 04:15:00 +0000538 if (newsize == PyDict_MINSIZE) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000539 /* A large table is shrinking, or we can't get any smaller. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000540 newtable = mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000541 if (newtable == oldtable) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000542 if (mp->ma_fill == mp->ma_used) {
543 /* No dummies, so no point doing anything. */
Tim Peters0c6010b2001-05-23 23:33:57 +0000544 return 0;
Tim Petersf8a548c2001-05-24 16:26:40 +0000545 }
546 /* We're not going to resize it, but rebuild the
547 table anyway to purge old dummy entries.
548 Subtle: This is *necessary* if fill==size,
549 as lookdict needs at least one virgin slot to
550 terminate failing searches. If fill < size, it's
551 merely desirable, as dummies slow searches. */
552 assert(mp->ma_fill > mp->ma_used);
Tim Peters0c6010b2001-05-23 23:33:57 +0000553 memcpy(small_copy, oldtable, sizeof(small_copy));
554 oldtable = small_copy;
555 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000556 }
557 else {
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000558 newtable = PyMem_NEW(PyDictEntry, newsize);
Tim Petersdea48ec2001-05-22 20:40:22 +0000559 if (newtable == NULL) {
560 PyErr_NoMemory();
561 return -1;
562 }
563 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000564
565 /* Make the dict empty, using the new table. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000566 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000567 mp->ma_table = newtable;
Tim Petersafb6ae82001-06-04 21:00:21 +0000568 mp->ma_mask = newsize - 1;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000569 memset(newtable, 0, sizeof(PyDictEntry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000570 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000571 i = mp->ma_fill;
572 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000573
Tim Peters19283142001-05-17 22:25:34 +0000574 /* Copy the data over; this is refcount-neutral for active entries;
575 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000576 for (ep = oldtable; i > 0; ep++) {
577 if (ep->me_value != NULL) { /* active entry */
578 --i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000579 insertdict_clean(mp, ep->me_key, (long)ep->me_hash,
580 ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000581 }
Tim Peters19283142001-05-17 22:25:34 +0000582 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000583 --i;
Tim Peters19283142001-05-17 22:25:34 +0000584 assert(ep->me_key == dummy);
585 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000586 }
Tim Peters19283142001-05-17 22:25:34 +0000587 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000588 }
589
Tim Peters0c6010b2001-05-23 23:33:57 +0000590 if (is_oldtable_malloced)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000591 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000592 return 0;
593}
594
Christian Heimes99170a52007-12-19 02:07:34 +0000595/* Create a new dictionary pre-sized to hold an estimated number of elements.
596 Underestimates are okay because the dictionary will resize as necessary.
597 Overestimates just mean the dictionary will be more sparse than usual.
598*/
599
600PyObject *
601_PyDict_NewPresized(Py_ssize_t minused)
602{
603 PyObject *op = PyDict_New();
604
605 if (minused>5 && op != NULL && dictresize((PyDictObject *)op, minused) == -1) {
606 Py_DECREF(op);
607 return NULL;
608 }
609 return op;
610}
611
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000612/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
613 * that may occur (originally dicts supported only string keys, and exceptions
614 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000615 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000616 * (suppressed) occurred while computing the key's hash, or that some error
617 * (suppressed) occurred when comparing keys in the dict's internal probe
618 * sequence. A nasty example of the latter is when a Python-coded comparison
619 * function hits a stack-depth error, which can cause this to return NULL
620 * even if the key is present.
621 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000622PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000623PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000624{
625 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000626 PyDictObject *mp = (PyDictObject *)op;
627 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000628 PyThreadState *tstate;
629 if (!PyDict_Check(op))
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000630 return NULL;
Guido van Rossum4d027722007-09-18 04:30:42 +0000631 if (!PyUnicode_CheckExact(key) ||
632 (hash = ((PyUnicodeObject *) key)->hash) == -1)
Guido van Rossumca756f21997-01-23 19:39:29 +0000633 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000634 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000635 if (hash == -1) {
636 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000637 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000638 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000639 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000640
641 /* We can arrive here with a NULL tstate during initialization:
642 try running "python -Wi" for an example related to string
643 interning. Let's just hope that no exception occurs then... */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000644 tstate = _PyThreadState_Current;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000645 if (tstate != NULL && tstate->curexc_type != NULL) {
646 /* preserve the existing exception */
647 PyObject *err_type, *err_value, *err_tb;
648 PyErr_Fetch(&err_type, &err_value, &err_tb);
649 ep = (mp->ma_lookup)(mp, key, hash);
650 /* ignore errors */
651 PyErr_Restore(err_type, err_value, err_tb);
652 if (ep == NULL)
653 return NULL;
654 }
655 else {
656 ep = (mp->ma_lookup)(mp, key, hash);
657 if (ep == NULL) {
658 PyErr_Clear();
659 return NULL;
660 }
661 }
662 return ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000663}
664
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000665/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
666 This returns NULL *with* an exception set if an exception occurred.
667 It returns NULL *without* an exception set if the key wasn't present.
668*/
669PyObject *
670PyDict_GetItemWithError(PyObject *op, PyObject *key)
671{
672 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000673 PyDictObject*mp = (PyDictObject *)op;
674 PyDictEntry *ep;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000675
676 if (!PyDict_Check(op)) {
677 PyErr_BadInternalCall();
678 return NULL;
679 }
Guido van Rossum89d8c602007-09-18 17:26:56 +0000680 if (!PyUnicode_CheckExact(key) ||
681 (hash = ((PyUnicodeObject *) key)->hash) == -1)
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000682 {
683 hash = PyObject_Hash(key);
684 if (hash == -1) {
685 return NULL;
686 }
687 }
688
689 ep = (mp->ma_lookup)(mp, key, hash);
690 if (ep == NULL)
691 return NULL;
692 return ep->me_value;
693}
694
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000695/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
Tim Peters60b29962006-01-01 01:19:23 +0000696 * dictionary if it's merely replacing the value for an existing key.
697 * This means that it's safe to loop over a dictionary with PyDict_Next()
698 * and occasionally replace a value -- but you can't insert new keys or
699 * remove them.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000700 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000701int
Tim Peters1f5871e2000-07-04 17:44:48 +0000702PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000703{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000704 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000705 register long hash;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000706 register Py_ssize_t n_used;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000707
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000708 if (!PyDict_Check(op)) {
709 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000710 return -1;
711 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000712 assert(key);
713 assert(value);
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000714 mp = (PyDictObject *)op;
Guido van Rossum4d027722007-09-18 04:30:42 +0000715 if (!PyUnicode_CheckExact(key) ||
716 (hash = ((PyUnicodeObject *) key)->hash) == -1)
717 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000718 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000719 if (hash == -1)
720 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000721 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000722 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000723 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000724 Py_INCREF(value);
725 Py_INCREF(key);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000726 if (insertdict(mp, key, hash, value) != 0)
727 return -1;
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000728 /* If we added a key, we can safely resize. Otherwise just return!
729 * If fill >= 2/3 size, adjust size. Normally, this doubles or
730 * quaduples the size, but it's also possible for the dict to shrink
Tim Peters60b29962006-01-01 01:19:23 +0000731 * (if ma_fill is much larger than ma_used, meaning a lot of dict
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000732 * keys have been * deleted).
Tim Peters60b29962006-01-01 01:19:23 +0000733 *
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000734 * Quadrupling the size improves average dictionary sparseness
735 * (reducing collisions) at the cost of some memory and iteration
736 * speed (which loops over every possible entry). It also halves
737 * the number of expensive resize operations in a growing dictionary.
Tim Peters60b29962006-01-01 01:19:23 +0000738 *
739 * Very large dictionaries (over 50K items) use doubling instead.
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000740 * This may help applications with severe memory constraints.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000741 */
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000742 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
743 return 0;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000744 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000745}
746
747int
Tim Peters1f5871e2000-07-04 17:44:48 +0000748PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000749{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000750 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000751 register long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000752 register PyDictEntry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000753 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000754
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000755 if (!PyDict_Check(op)) {
756 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000757 return -1;
758 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000759 assert(key);
Guido van Rossum89d8c602007-09-18 17:26:56 +0000760 if (!PyUnicode_CheckExact(key) ||
761 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000762 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000763 if (hash == -1)
764 return -1;
765 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000766 mp = (PyDictObject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000767 ep = (mp->ma_lookup)(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000768 if (ep == NULL)
769 return -1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000770 if (ep->me_value == NULL) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000771 set_key_error(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000772 return -1;
773 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000774 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000775 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000776 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000777 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000778 ep->me_value = NULL;
779 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000780 Py_DECREF(old_value);
781 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000782 return 0;
783}
784
Guido van Rossum25831651993-05-19 14:50:45 +0000785void
Tim Peters1f5871e2000-07-04 17:44:48 +0000786PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000787{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000788 PyDictObject *mp;
789 PyDictEntry *ep, *table;
Tim Petersdea48ec2001-05-22 20:40:22 +0000790 int table_is_malloced;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000791 Py_ssize_t fill;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000792 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000793#ifdef Py_DEBUG
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000794 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +0000795#endif
796
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000797 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000798 return;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000799 mp = (PyDictObject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000800#ifdef Py_DEBUG
Tim Petersafb6ae82001-06-04 21:00:21 +0000801 n = mp->ma_mask + 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000802 i = 0;
803#endif
804
805 table = mp->ma_table;
806 assert(table != NULL);
807 table_is_malloced = table != mp->ma_smalltable;
808
809 /* This is delicate. During the process of clearing the dict,
810 * decrefs can cause the dict to mutate. To avoid fatal confusion
811 * (voice of experience), we have to make the dict empty before
812 * clearing the slots, and never refer to anything via mp->xxx while
813 * clearing.
814 */
815 fill = mp->ma_fill;
816 if (table_is_malloced)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000817 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000818
819 else if (fill > 0) {
820 /* It's a small table with something that needs to be cleared.
821 * Afraid the only safe way is to copy the dict entries into
822 * another small table first.
823 */
824 memcpy(small_copy, table, sizeof(small_copy));
825 table = small_copy;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000826 EMPTY_TO_MINSIZE(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000827 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000828 /* else it's a small table that's already empty */
829
830 /* Now we can finally clear things. If C had refcounts, we could
831 * assert that the refcount on table is 1 now, i.e. that this function
832 * has unique access to it, so decref side-effects can't alter it.
833 */
834 for (ep = table; fill > 0; ++ep) {
835#ifdef Py_DEBUG
836 assert(i < n);
837 ++i;
838#endif
839 if (ep->me_key) {
840 --fill;
841 Py_DECREF(ep->me_key);
842 Py_XDECREF(ep->me_value);
843 }
844#ifdef Py_DEBUG
845 else
846 assert(ep->me_value == NULL);
847#endif
848 }
849
850 if (table_is_malloced)
851 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000852}
853
Tim Peters080c88b2003-02-15 03:01:11 +0000854/*
855 * Iterate over a dict. Use like so:
856 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000857 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +0000858 * PyObject *key, *value;
859 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +0000860 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +0000861 * Refer to borrowed references in key and value.
862 * }
863 *
864 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +0000865 * mutates the dict. One exception: it is safe if the loop merely changes
866 * the values associated with the keys (but doesn't insert new keys or
867 * delete keys), via PyDict_SetItem().
868 */
Guido van Rossum25831651993-05-19 14:50:45 +0000869int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000870PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000871{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000872 register Py_ssize_t i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000873 register Py_ssize_t mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000874 register PyDictEntry *ep;
Raymond Hettinger43442782004-03-17 21:55:03 +0000875
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000876 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000877 return 0;
Guido van Rossum25831651993-05-19 14:50:45 +0000878 i = *ppos;
879 if (i < 0)
880 return 0;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000881 ep = ((PyDictObject *)op)->ma_table;
882 mask = ((PyDictObject *)op)->ma_mask;
Raymond Hettinger43442782004-03-17 21:55:03 +0000883 while (i <= mask && ep[i].me_value == NULL)
Guido van Rossum25831651993-05-19 14:50:45 +0000884 i++;
885 *ppos = i+1;
Raymond Hettinger43442782004-03-17 21:55:03 +0000886 if (i > mask)
Guido van Rossum25831651993-05-19 14:50:45 +0000887 return 0;
888 if (pkey)
Raymond Hettinger43442782004-03-17 21:55:03 +0000889 *pkey = ep[i].me_key;
Guido van Rossum25831651993-05-19 14:50:45 +0000890 if (pvalue)
Raymond Hettinger43442782004-03-17 21:55:03 +0000891 *pvalue = ep[i].me_value;
Guido van Rossum25831651993-05-19 14:50:45 +0000892 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000893}
894
Thomas Wouterscf297e42007-02-23 15:07:44 +0000895/* Internal version of PyDict_Next that returns a hash value in addition to the key and value.*/
896int
897_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, long *phash)
898{
899 register Py_ssize_t i;
900 register Py_ssize_t mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000901 register PyDictEntry *ep;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000902
903 if (!PyDict_Check(op))
904 return 0;
905 i = *ppos;
906 if (i < 0)
907 return 0;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000908 ep = ((PyDictObject *)op)->ma_table;
909 mask = ((PyDictObject *)op)->ma_mask;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000910 while (i <= mask && ep[i].me_value == NULL)
911 i++;
912 *ppos = i+1;
913 if (i > mask)
914 return 0;
915 *phash = (long)(ep[i].me_hash);
916 if (pkey)
917 *pkey = ep[i].me_key;
918 if (pvalue)
919 *pvalue = ep[i].me_value;
920 return 1;
921}
922
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000923/* Methods */
924
925static void
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000926dict_dealloc(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000927{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000928 register PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000929 Py_ssize_t fill = mp->ma_fill;
Guido van Rossumff413af2002-03-28 20:34:59 +0000930 PyObject_GC_UnTrack(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000931 Py_TRASHCAN_SAFE_BEGIN(mp)
Tim Petersdea48ec2001-05-22 20:40:22 +0000932 for (ep = mp->ma_table; fill > 0; ep++) {
933 if (ep->me_key) {
934 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000935 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000936 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000937 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000938 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000939 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000940 PyMem_DEL(mp->ma_table);
Christian Heimes2202f872008-02-06 14:31:34 +0000941 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
942 free_list[numfree++] = mp;
Tim Peters60b29962006-01-01 01:19:23 +0000943 else
Christian Heimes90aa7642007-12-19 02:45:37 +0000944 Py_TYPE(mp)->tp_free((PyObject *)mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000945 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000946}
947
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000948static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000949dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000950{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000951 Py_ssize_t i;
Tim Petersa7259592001-06-16 05:11:17 +0000952 PyObject *s, *temp, *colon = NULL;
953 PyObject *pieces = NULL, *result = NULL;
954 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +0000955
Tim Petersa7259592001-06-16 05:11:17 +0000956 i = Py_ReprEnter((PyObject *)mp);
Guido van Rossum255443b1998-04-10 22:47:14 +0000957 if (i != 0) {
Walter Dörwald1ab83302007-05-18 17:15:44 +0000958 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
Guido van Rossum255443b1998-04-10 22:47:14 +0000959 }
960
Tim Petersa7259592001-06-16 05:11:17 +0000961 if (mp->ma_used == 0) {
Walter Dörwald1ab83302007-05-18 17:15:44 +0000962 result = PyUnicode_FromString("{}");
Tim Petersa7259592001-06-16 05:11:17 +0000963 goto Done;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000964 }
Tim Petersa7259592001-06-16 05:11:17 +0000965
966 pieces = PyList_New(0);
967 if (pieces == NULL)
968 goto Done;
969
Walter Dörwald1ab83302007-05-18 17:15:44 +0000970 colon = PyUnicode_FromString(": ");
Tim Petersa7259592001-06-16 05:11:17 +0000971 if (colon == NULL)
972 goto Done;
973
974 /* Do repr() on each key+value pair, and insert ": " between them.
975 Note that repr may mutate the dict. */
Tim Petersc6057842001-06-16 07:52:53 +0000976 i = 0;
977 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Tim Petersa7259592001-06-16 05:11:17 +0000978 int status;
979 /* Prevent repr from deleting value during key format. */
980 Py_INCREF(value);
981 s = PyObject_Repr(key);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000982 PyUnicode_Append(&s, colon);
983 PyUnicode_AppendAndDel(&s, PyObject_Repr(value));
Tim Petersa7259592001-06-16 05:11:17 +0000984 Py_DECREF(value);
985 if (s == NULL)
986 goto Done;
987 status = PyList_Append(pieces, s);
988 Py_DECREF(s); /* append created a new ref */
989 if (status < 0)
990 goto Done;
991 }
992
993 /* Add "{}" decorations to the first and last items. */
994 assert(PyList_GET_SIZE(pieces) > 0);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000995 s = PyUnicode_FromString("{");
Tim Petersa7259592001-06-16 05:11:17 +0000996 if (s == NULL)
997 goto Done;
998 temp = PyList_GET_ITEM(pieces, 0);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000999 PyUnicode_AppendAndDel(&s, temp);
Tim Petersa7259592001-06-16 05:11:17 +00001000 PyList_SET_ITEM(pieces, 0, s);
1001 if (s == NULL)
1002 goto Done;
1003
Walter Dörwald1ab83302007-05-18 17:15:44 +00001004 s = PyUnicode_FromString("}");
Tim Petersa7259592001-06-16 05:11:17 +00001005 if (s == NULL)
1006 goto Done;
1007 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
Walter Dörwald1ab83302007-05-18 17:15:44 +00001008 PyUnicode_AppendAndDel(&temp, s);
Tim Petersa7259592001-06-16 05:11:17 +00001009 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
1010 if (temp == NULL)
1011 goto Done;
1012
1013 /* Paste them all together with ", " between. */
Walter Dörwald1ab83302007-05-18 17:15:44 +00001014 s = PyUnicode_FromString(", ");
Tim Petersa7259592001-06-16 05:11:17 +00001015 if (s == NULL)
1016 goto Done;
Walter Dörwald1ab83302007-05-18 17:15:44 +00001017 result = PyUnicode_Join(s, pieces);
Tim Peters0ab085c2001-09-14 00:25:33 +00001018 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +00001019
1020Done:
1021 Py_XDECREF(pieces);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001022 Py_XDECREF(colon);
Tim Petersa7259592001-06-16 05:11:17 +00001023 Py_ReprLeave((PyObject *)mp);
1024 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001025}
1026
Martin v. Löwis18e16552006-02-15 17:27:45 +00001027static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001028dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001029{
1030 return mp->ma_used;
1031}
1032
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001033static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001034dict_subscript(PyDictObject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001035{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001036 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001037 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001038 PyDictEntry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +00001039 assert(mp->ma_table != NULL);
Guido van Rossum89d8c602007-09-18 17:26:56 +00001040 if (!PyUnicode_CheckExact(key) ||
1041 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001042 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001043 if (hash == -1)
1044 return NULL;
1045 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001046 ep = (mp->ma_lookup)(mp, key, hash);
1047 if (ep == NULL)
1048 return NULL;
1049 v = ep->me_value;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001050 if (v == NULL) {
1051 if (!PyDict_CheckExact(mp)) {
1052 /* Look up __missing__ method if we're a subclass. */
Guido van Rossum4b92a822006-02-25 23:32:30 +00001053 PyObject *missing;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001054 static PyObject *missing_str = NULL;
1055 if (missing_str == NULL)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001056 missing_str =
Martin v. Löwis5b222132007-06-10 09:51:05 +00001057 PyUnicode_InternFromString("__missing__");
Christian Heimes90aa7642007-12-19 02:45:37 +00001058 missing = _PyType_Lookup(Py_TYPE(mp), missing_str);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001059 if (missing != NULL)
1060 return PyObject_CallFunctionObjArgs(missing,
1061 (PyObject *)mp, key, NULL);
1062 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001063 set_key_error(key);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001064 return NULL;
1065 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001066 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001067 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001068 return v;
1069}
1070
1071static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001072dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001073{
1074 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001075 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001076 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001077 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001078}
1079
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001080static PyMappingMethods dict_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001081 (lenfunc)dict_length, /*mp_length*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001082 (binaryfunc)dict_subscript, /*mp_subscript*/
1083 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001084};
1085
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001086static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001087dict_keys(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001088{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001089 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001090 register Py_ssize_t i, j;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001091 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001092 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001093
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001094 again:
1095 n = mp->ma_used;
1096 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001097 if (v == NULL)
1098 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001099 if (n != mp->ma_used) {
1100 /* Durnit. The allocations caused the dict to resize.
1101 * Just start over, this shouldn't normally happen.
1102 */
1103 Py_DECREF(v);
1104 goto again;
1105 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001106 ep = mp->ma_table;
1107 mask = mp->ma_mask;
1108 for (i = 0, j = 0; i <= mask; i++) {
1109 if (ep[i].me_value != NULL) {
1110 PyObject *key = ep[i].me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001111 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001112 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001113 j++;
1114 }
1115 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001116 assert(j == n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001117 return v;
1118}
1119
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001120static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001121dict_values(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001122{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001123 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001124 register Py_ssize_t i, j;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001125 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001126 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001127
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001128 again:
1129 n = mp->ma_used;
1130 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001131 if (v == NULL)
1132 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001133 if (n != mp->ma_used) {
1134 /* Durnit. The allocations caused the dict to resize.
1135 * Just start over, this shouldn't normally happen.
1136 */
1137 Py_DECREF(v);
1138 goto again;
1139 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001140 ep = mp->ma_table;
1141 mask = mp->ma_mask;
1142 for (i = 0, j = 0; i <= mask; i++) {
1143 if (ep[i].me_value != NULL) {
1144 PyObject *value = ep[i].me_value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001145 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001146 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001147 j++;
1148 }
1149 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001150 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001151 return v;
1152}
1153
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001154static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001155dict_items(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001156{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001157 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001158 register Py_ssize_t i, j, n;
1159 Py_ssize_t mask;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001160 PyObject *item, *key, *value;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001161 PyDictEntry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001162
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001163 /* Preallocate the list of tuples, to avoid allocations during
1164 * the loop over the items, which could trigger GC, which
1165 * could resize the dict. :-(
1166 */
1167 again:
1168 n = mp->ma_used;
1169 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001170 if (v == NULL)
1171 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001172 for (i = 0; i < n; i++) {
1173 item = PyTuple_New(2);
1174 if (item == NULL) {
1175 Py_DECREF(v);
1176 return NULL;
1177 }
1178 PyList_SET_ITEM(v, i, item);
1179 }
1180 if (n != mp->ma_used) {
1181 /* Durnit. The allocations caused the dict to resize.
1182 * Just start over, this shouldn't normally happen.
1183 */
1184 Py_DECREF(v);
1185 goto again;
1186 }
1187 /* Nothing we do below makes any function calls. */
Raymond Hettinger43442782004-03-17 21:55:03 +00001188 ep = mp->ma_table;
1189 mask = mp->ma_mask;
1190 for (i = 0, j = 0; i <= mask; i++) {
Raymond Hettinger06905122004-03-19 10:30:00 +00001191 if ((value=ep[i].me_value) != NULL) {
Raymond Hettinger43442782004-03-17 21:55:03 +00001192 key = ep[i].me_key;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001193 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001194 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001195 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001196 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001197 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001198 j++;
1199 }
1200 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001201 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001202 return v;
1203}
1204
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001205static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001206dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001207{
1208 PyObject *seq;
1209 PyObject *value = Py_None;
1210 PyObject *it; /* iter(seq) */
1211 PyObject *key;
1212 PyObject *d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001213 int status;
1214
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001215 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001216 return NULL;
1217
1218 d = PyObject_CallObject(cls, NULL);
1219 if (d == NULL)
1220 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001221
Guido van Rossum58da9312007-11-10 23:39:45 +00001222 if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) {
1223 PyDictObject *mp = (PyDictObject *)d;
1224 PyObject *oldvalue;
1225 Py_ssize_t pos = 0;
1226 PyObject *key;
1227 long hash;
1228
1229 if (dictresize(mp, PySet_GET_SIZE(seq)))
1230 return NULL;
1231
1232 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
1233 Py_INCREF(key);
1234 Py_INCREF(value);
1235 if (insertdict(mp, key, hash, value))
1236 return NULL;
1237 }
1238 return d;
1239 }
1240
Guido van Rossumd8faa362007-04-27 19:54:29 +00001241 if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001242 PyDictObject *mp = (PyDictObject *)d;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001243 Py_ssize_t pos = 0;
1244 PyObject *key;
1245 long hash;
1246
1247 if (dictresize(mp, PySet_GET_SIZE(seq)))
1248 return NULL;
1249
1250 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
1251 Py_INCREF(key);
1252 Py_INCREF(value);
1253 if (insertdict(mp, key, hash, value))
1254 return NULL;
1255 }
1256 return d;
1257 }
1258
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001259 it = PyObject_GetIter(seq);
1260 if (it == NULL){
1261 Py_DECREF(d);
1262 return NULL;
1263 }
1264
Guido van Rossum58da9312007-11-10 23:39:45 +00001265 if (PyDict_CheckExact(d)) {
1266 while ((key = PyIter_Next(it)) != NULL) {
1267 status = PyDict_SetItem(d, key, value);
1268 Py_DECREF(key);
1269 if (status < 0)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001270 goto Fail;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001271 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001272 } else {
1273 while ((key = PyIter_Next(it)) != NULL) {
1274 status = PyObject_SetItem(d, key, value);
1275 Py_DECREF(key);
1276 if (status < 0)
1277 goto Fail;
1278 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001279 }
1280
Guido van Rossum58da9312007-11-10 23:39:45 +00001281 if (PyErr_Occurred())
1282 goto Fail;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001283 Py_DECREF(it);
1284 return d;
1285
1286Fail:
1287 Py_DECREF(it);
1288 Py_DECREF(d);
1289 return NULL;
1290}
1291
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001292static int
1293dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001294{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001295 PyObject *arg = NULL;
1296 int result = 0;
1297
1298 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1299 result = -1;
1300
1301 else if (arg != NULL) {
1302 if (PyObject_HasAttrString(arg, "keys"))
1303 result = PyDict_Merge(self, arg, 1);
1304 else
1305 result = PyDict_MergeFromSeq2(self, arg, 1);
1306 }
1307 if (result == 0 && kwds != NULL)
1308 result = PyDict_Merge(self, kwds, 1);
1309 return result;
1310}
1311
1312static PyObject *
1313dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1314{
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001315 if (dict_update_common(self, args, kwds, "update") != -1)
1316 Py_RETURN_NONE;
1317 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001318}
1319
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001320/* Update unconditionally replaces existing items.
1321 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001322 otherwise it leaves existing items unchanged.
1323
1324 PyDict_{Update,Merge} update/merge from a mapping object.
1325
Tim Petersf582b822001-12-11 18:51:08 +00001326 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001327 producing iterable objects of length 2.
1328*/
1329
Tim Petersf582b822001-12-11 18:51:08 +00001330int
Tim Peters1fc240e2001-10-26 05:06:50 +00001331PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1332{
1333 PyObject *it; /* iter(seq2) */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001334 Py_ssize_t i; /* index into seq2 of current element */
Tim Peters1fc240e2001-10-26 05:06:50 +00001335 PyObject *item; /* seq2[i] */
1336 PyObject *fast; /* item as a 2-tuple or 2-list */
1337
1338 assert(d != NULL);
1339 assert(PyDict_Check(d));
1340 assert(seq2 != NULL);
1341
1342 it = PyObject_GetIter(seq2);
1343 if (it == NULL)
1344 return -1;
1345
1346 for (i = 0; ; ++i) {
1347 PyObject *key, *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001348 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001349
1350 fast = NULL;
1351 item = PyIter_Next(it);
1352 if (item == NULL) {
1353 if (PyErr_Occurred())
1354 goto Fail;
1355 break;
1356 }
1357
1358 /* Convert item to sequence, and verify length 2. */
1359 fast = PySequence_Fast(item, "");
1360 if (fast == NULL) {
1361 if (PyErr_ExceptionMatches(PyExc_TypeError))
1362 PyErr_Format(PyExc_TypeError,
1363 "cannot convert dictionary update "
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001364 "sequence element #%zd to a sequence",
Tim Peters1fc240e2001-10-26 05:06:50 +00001365 i);
1366 goto Fail;
1367 }
1368 n = PySequence_Fast_GET_SIZE(fast);
1369 if (n != 2) {
1370 PyErr_Format(PyExc_ValueError,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001371 "dictionary update sequence element #%zd "
Martin v. Löwis2c95cc62006-02-16 06:54:25 +00001372 "has length %zd; 2 is required",
Martin v. Löwise0e89f72006-02-16 06:59:22 +00001373 i, n);
Tim Peters1fc240e2001-10-26 05:06:50 +00001374 goto Fail;
1375 }
1376
1377 /* Update/merge with this (key, value) pair. */
1378 key = PySequence_Fast_GET_ITEM(fast, 0);
1379 value = PySequence_Fast_GET_ITEM(fast, 1);
1380 if (override || PyDict_GetItem(d, key) == NULL) {
1381 int status = PyDict_SetItem(d, key, value);
1382 if (status < 0)
1383 goto Fail;
1384 }
1385 Py_DECREF(fast);
1386 Py_DECREF(item);
1387 }
1388
1389 i = 0;
1390 goto Return;
1391Fail:
1392 Py_XDECREF(item);
1393 Py_XDECREF(fast);
1394 i = -1;
1395Return:
1396 Py_DECREF(it);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001397 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001398}
1399
Tim Peters6d6c1a32001-08-02 04:15:00 +00001400int
1401PyDict_Update(PyObject *a, PyObject *b)
1402{
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001403 return PyDict_Merge(a, b, 1);
1404}
1405
1406int
1407PyDict_Merge(PyObject *a, PyObject *b, int override)
1408{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001409 register PyDictObject *mp, *other;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001410 register Py_ssize_t i;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001411 PyDictEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001412
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001413 /* We accept for the argument either a concrete dictionary object,
1414 * or an abstract "mapping" object. For the former, we can do
1415 * things quite efficiently. For the latter, we only require that
1416 * PyMapping_Keys() and PyObject_GetItem() be supported.
1417 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001418 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1419 PyErr_BadInternalCall();
1420 return -1;
1421 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001422 mp = (PyDictObject*)a;
Christian Heimesaf98da12008-01-27 15:18:18 +00001423 if (PyDict_Check(b)) {
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001424 other = (PyDictObject*)b;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001425 if (other == mp || other->ma_used == 0)
1426 /* a.update(a) or a.update({}); nothing to do */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001427 return 0;
Raymond Hettinger186e7392005-05-14 18:08:25 +00001428 if (mp->ma_used == 0)
1429 /* Since the target dict is empty, PyDict_GetItem()
1430 * always returns NULL. Setting override to 1
1431 * skips the unnecessary test.
1432 */
1433 override = 1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001434 /* Do one big resize at the start, rather than
1435 * incrementally resizing as we insert new items. Expect
1436 * that there will be no (or few) overlapping keys.
1437 */
1438 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
Raymond Hettingerc8d22902003-05-07 00:49:40 +00001439 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001440 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001441 }
1442 for (i = 0; i <= other->ma_mask; i++) {
1443 entry = &other->ma_table[i];
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001444 if (entry->me_value != NULL &&
1445 (override ||
1446 PyDict_GetItem(a, entry->me_key) == NULL)) {
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001447 Py_INCREF(entry->me_key);
1448 Py_INCREF(entry->me_value);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001449 if (insertdict(mp, entry->me_key,
1450 (long)entry->me_hash,
1451 entry->me_value) != 0)
1452 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001453 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001454 }
1455 }
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001456 else {
1457 /* Do it the generic, slower way */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001458 PyObject *keys = PyMapping_Keys(b);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001459 PyObject *iter;
1460 PyObject *key, *value;
1461 int status;
1462
1463 if (keys == NULL)
1464 /* Docstring says this is equivalent to E.keys() so
1465 * if E doesn't have a .keys() method we want
1466 * AttributeError to percolate up. Might as well
1467 * do the same for any other error.
1468 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001469 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001470
1471 iter = PyObject_GetIter(keys);
1472 Py_DECREF(keys);
1473 if (iter == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001474 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001475
1476 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001477 if (!override && PyDict_GetItem(a, key) != NULL) {
1478 Py_DECREF(key);
1479 continue;
1480 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001481 value = PyObject_GetItem(b, key);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001482 if (value == NULL) {
1483 Py_DECREF(iter);
1484 Py_DECREF(key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001485 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001486 }
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001487 status = PyDict_SetItem(a, key, value);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001488 Py_DECREF(key);
1489 Py_DECREF(value);
1490 if (status < 0) {
1491 Py_DECREF(iter);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001492 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001493 }
1494 }
1495 Py_DECREF(iter);
1496 if (PyErr_Occurred())
1497 /* Iterator completed, via error */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001498 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001499 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001500 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001501}
1502
1503static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001504dict_copy(register PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001505{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001506 return PyDict_Copy((PyObject*)mp);
1507}
1508
1509PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001510PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001511{
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001512 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001513
1514 if (o == NULL || !PyDict_Check(o)) {
1515 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001516 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001517 }
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001518 copy = PyDict_New();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001519 if (copy == NULL)
1520 return NULL;
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001521 if (PyDict_Merge(copy, o, 1) == 0)
1522 return copy;
1523 Py_DECREF(copy);
Raymond Hettinger1356f782005-04-15 15:58:42 +00001524 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001525}
1526
Martin v. Löwis18e16552006-02-15 17:27:45 +00001527Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001528PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001529{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001530 if (mp == NULL || !PyDict_Check(mp)) {
1531 PyErr_BadInternalCall();
Jeremy Hylton7083bb72004-02-17 20:10:11 +00001532 return -1;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001533 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001534 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001535}
1536
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001537PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001538PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001539{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001540 if (mp == NULL || !PyDict_Check(mp)) {
1541 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001542 return NULL;
1543 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001544 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001545}
1546
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001547PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001548PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001549{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001550 if (mp == NULL || !PyDict_Check(mp)) {
1551 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001552 return NULL;
1553 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001554 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001555}
1556
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001557PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001558PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001559{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001560 if (mp == NULL || !PyDict_Check(mp)) {
1561 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001562 return NULL;
1563 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001564 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001565}
1566
Tim Peterse63415e2001-05-08 04:38:29 +00001567/* Return 1 if dicts equal, 0 if not, -1 if error.
1568 * Gets out as soon as any difference is detected.
1569 * Uses only Py_EQ comparison.
1570 */
1571static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001572dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00001573{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001574 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00001575
1576 if (a->ma_used != b->ma_used)
1577 /* can't be equal if # of entries differ */
1578 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001579
Tim Peterse63415e2001-05-08 04:38:29 +00001580 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Tim Petersafb6ae82001-06-04 21:00:21 +00001581 for (i = 0; i <= a->ma_mask; i++) {
Tim Peterse63415e2001-05-08 04:38:29 +00001582 PyObject *aval = a->ma_table[i].me_value;
1583 if (aval != NULL) {
1584 int cmp;
1585 PyObject *bval;
1586 PyObject *key = a->ma_table[i].me_key;
1587 /* temporarily bump aval's refcount to ensure it stays
1588 alive until we're done with it */
1589 Py_INCREF(aval);
Guido van Rossumdc5f6b22006-08-24 21:29:26 +00001590 /* ditto for key */
1591 Py_INCREF(key);
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001592 bval = PyDict_GetItemWithError((PyObject *)b, key);
Guido van Rossumdc5f6b22006-08-24 21:29:26 +00001593 Py_DECREF(key);
Tim Peterse63415e2001-05-08 04:38:29 +00001594 if (bval == NULL) {
1595 Py_DECREF(aval);
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001596 if (PyErr_Occurred())
1597 return -1;
Tim Peterse63415e2001-05-08 04:38:29 +00001598 return 0;
1599 }
1600 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1601 Py_DECREF(aval);
1602 if (cmp <= 0) /* error or not equal */
1603 return cmp;
1604 }
1605 }
1606 return 1;
1607 }
1608
1609static PyObject *
1610dict_richcompare(PyObject *v, PyObject *w, int op)
1611{
1612 int cmp;
1613 PyObject *res;
1614
1615 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1616 res = Py_NotImplemented;
1617 }
1618 else if (op == Py_EQ || op == Py_NE) {
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001619 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
Tim Peterse63415e2001-05-08 04:38:29 +00001620 if (cmp < 0)
1621 return NULL;
1622 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1623 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001624 else
1625 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001626 Py_INCREF(res);
1627 return res;
1628 }
1629
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001630static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001631dict_contains(register PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001632{
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001633 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001634 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001635
Guido van Rossum89d8c602007-09-18 17:26:56 +00001636 if (!PyUnicode_CheckExact(key) ||
1637 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001638 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001639 if (hash == -1)
1640 return NULL;
1641 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001642 ep = (mp->ma_lookup)(mp, key, hash);
1643 if (ep == NULL)
1644 return NULL;
1645 return PyBool_FromLong(ep->me_value != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001646}
1647
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001648static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001649dict_get(register PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001650{
1651 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001652 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001653 PyObject *val = NULL;
1654 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001655 PyDictEntry *ep;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001656
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001657 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001658 return NULL;
1659
Guido van Rossum89d8c602007-09-18 17:26:56 +00001660 if (!PyUnicode_CheckExact(key) ||
1661 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Barry Warsawc38c5da1997-10-06 17:49:20 +00001662 hash = PyObject_Hash(key);
1663 if (hash == -1)
1664 return NULL;
1665 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001666 ep = (mp->ma_lookup)(mp, key, hash);
1667 if (ep == NULL)
1668 return NULL;
1669 val = ep->me_value;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001670 if (val == NULL)
1671 val = failobj;
1672 Py_INCREF(val);
1673 return val;
1674}
1675
1676
1677static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001678dict_setdefault(register PyDictObject *mp, PyObject *args)
Guido van Rossum164452c2000-08-08 16:12:54 +00001679{
1680 PyObject *key;
1681 PyObject *failobj = Py_None;
1682 PyObject *val = NULL;
1683 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001684 PyDictEntry *ep;
Guido van Rossum164452c2000-08-08 16:12:54 +00001685
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001686 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
Guido van Rossum164452c2000-08-08 16:12:54 +00001687 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001688
Guido van Rossum89d8c602007-09-18 17:26:56 +00001689 if (!PyUnicode_CheckExact(key) ||
1690 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossum164452c2000-08-08 16:12:54 +00001691 hash = PyObject_Hash(key);
1692 if (hash == -1)
1693 return NULL;
1694 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001695 ep = (mp->ma_lookup)(mp, key, hash);
1696 if (ep == NULL)
1697 return NULL;
1698 val = ep->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001699 if (val == NULL) {
1700 val = failobj;
1701 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1702 val = NULL;
1703 }
1704 Py_XINCREF(val);
1705 return val;
1706}
1707
1708
1709static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001710dict_clear(register PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001711{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001712 PyDict_Clear((PyObject *)mp);
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001713 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001714}
1715
Guido van Rossumba6ab842000-12-12 22:02:18 +00001716static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001717dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00001718{
1719 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001720 PyDictEntry *ep;
Guido van Rossume027d982002-04-12 15:11:59 +00001721 PyObject *old_value, *old_key;
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001722 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001723
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001724 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1725 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001726 if (mp->ma_used == 0) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001727 if (deflt) {
1728 Py_INCREF(deflt);
1729 return deflt;
1730 }
Guido van Rossume027d982002-04-12 15:11:59 +00001731 PyErr_SetString(PyExc_KeyError,
1732 "pop(): dictionary is empty");
1733 return NULL;
1734 }
Guido van Rossum89d8c602007-09-18 17:26:56 +00001735 if (!PyUnicode_CheckExact(key) ||
1736 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossume027d982002-04-12 15:11:59 +00001737 hash = PyObject_Hash(key);
1738 if (hash == -1)
1739 return NULL;
1740 }
1741 ep = (mp->ma_lookup)(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001742 if (ep == NULL)
1743 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001744 if (ep->me_value == NULL) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001745 if (deflt) {
1746 Py_INCREF(deflt);
1747 return deflt;
1748 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001749 set_key_error(key);
Guido van Rossume027d982002-04-12 15:11:59 +00001750 return NULL;
1751 }
1752 old_key = ep->me_key;
1753 Py_INCREF(dummy);
1754 ep->me_key = dummy;
1755 old_value = ep->me_value;
1756 ep->me_value = NULL;
1757 mp->ma_used--;
1758 Py_DECREF(old_key);
1759 return old_value;
1760}
1761
1762static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001763dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001764{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001765 Py_ssize_t i = 0;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001766 PyDictEntry *ep;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001767 PyObject *res;
1768
Tim Petersf4b33f62001-06-02 05:42:29 +00001769 /* Allocate the result tuple before checking the size. Believe it
1770 * or not, this allocation could trigger a garbage collection which
1771 * could empty the dict, so if we checked the size first and that
1772 * happened, the result would be an infinite loop (searching for an
1773 * entry that no longer exists). Note that the usual popitem()
1774 * idiom is "while d: k, v = d.popitem()". so needing to throw the
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001775 * tuple away if the dict *is* empty isn't a significant
Tim Petersf4b33f62001-06-02 05:42:29 +00001776 * inefficiency -- possible, but unlikely in practice.
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001777 */
1778 res = PyTuple_New(2);
1779 if (res == NULL)
1780 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001781 if (mp->ma_used == 0) {
1782 Py_DECREF(res);
1783 PyErr_SetString(PyExc_KeyError,
1784 "popitem(): dictionary is empty");
1785 return NULL;
1786 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001787 /* Set ep to "the first" dict entry with a value. We abuse the hash
1788 * field of slot 0 to hold a search finger:
1789 * If slot 0 has a value, use slot 0.
1790 * Else slot 0 is being used to hold a search finger,
1791 * and we use its hash value as the first index to look.
1792 */
1793 ep = &mp->ma_table[0];
1794 if (ep->me_value == NULL) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001795 i = ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001796 /* The hash field may be a real hash value, or it may be a
1797 * legit search finger, or it may be a once-legit search
1798 * finger that's out of bounds now because it wrapped around
1799 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001800 */
Tim Petersafb6ae82001-06-04 21:00:21 +00001801 if (i > mp->ma_mask || i < 1)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001802 i = 1; /* skip slot 0 */
1803 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1804 i++;
Tim Petersafb6ae82001-06-04 21:00:21 +00001805 if (i > mp->ma_mask)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001806 i = 1;
1807 }
1808 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001809 PyTuple_SET_ITEM(res, 0, ep->me_key);
1810 PyTuple_SET_ITEM(res, 1, ep->me_value);
1811 Py_INCREF(dummy);
1812 ep->me_key = dummy;
1813 ep->me_value = NULL;
1814 mp->ma_used--;
1815 assert(mp->ma_table[0].me_value == NULL);
1816 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001817 return res;
1818}
1819
Jeremy Hylton8caad492000-06-23 14:18:11 +00001820static int
1821dict_traverse(PyObject *op, visitproc visit, void *arg)
1822{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001823 Py_ssize_t i = 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001824 PyObject *pk;
1825 PyObject *pv;
1826
1827 while (PyDict_Next(op, &i, &pk, &pv)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001828 Py_VISIT(pk);
1829 Py_VISIT(pv);
Jeremy Hylton8caad492000-06-23 14:18:11 +00001830 }
1831 return 0;
1832}
1833
1834static int
1835dict_tp_clear(PyObject *op)
1836{
1837 PyDict_Clear(op);
1838 return 0;
1839}
1840
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001841static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001842
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001843static PyObject *
1844dict_sizeof(PyDictObject *mp)
1845{
1846 Py_ssize_t res;
1847
1848 res = sizeof(PyDictObject) + sizeof(mp->ma_table);
1849 if (mp->ma_table != mp->ma_smalltable)
1850 res = res + (mp->ma_mask + 1) * sizeof(PyDictEntry);
1851 return PyLong_FromSsize_t(res);
1852}
1853
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001854PyDoc_STRVAR(contains__doc__,
1855"D.__contains__(k) -> True if D has a key k, else False");
1856
1857PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
1858
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001859PyDoc_STRVAR(sizeof__doc__,
1860"D.__sizeof__() -> size of D in memory, in bytes");
1861
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001862PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001863"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001864
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001865PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001866"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 +00001867
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001868PyDoc_STRVAR(pop__doc__,
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001869"D.pop(k[,d]) -> v, remove specified key and return the corresponding value\n\
1870If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00001871
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001872PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00001873"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000018742-tuple; but raise KeyError if D is empty");
Tim Petersf7f88b12000-12-13 23:18:45 +00001875
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001876PyDoc_STRVAR(update__doc__,
Guido van Rossumb90c8482007-02-10 01:11:45 +00001877"D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k]\
1878\n(if E has keys else: for (k, v) in E: D[k] = v) then: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00001879
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001880PyDoc_STRVAR(fromkeys__doc__,
1881"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
1882v defaults to None.");
1883
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001884PyDoc_STRVAR(clear__doc__,
1885"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001886
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001887PyDoc_STRVAR(copy__doc__,
1888"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00001889
Guido van Rossumb90c8482007-02-10 01:11:45 +00001890/* Forward */
1891static PyObject *dictkeys_new(PyObject *);
1892static PyObject *dictitems_new(PyObject *);
1893static PyObject *dictvalues_new(PyObject *);
1894
Guido van Rossum45c85d12007-07-27 16:31:40 +00001895PyDoc_STRVAR(keys__doc__,
1896 "D.keys() -> a set-like object providing a view on D's keys");
1897PyDoc_STRVAR(items__doc__,
1898 "D.items() -> a set-like object providing a view on D's items");
1899PyDoc_STRVAR(values__doc__,
1900 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00001901
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001902static PyMethodDef mapp_methods[] = {
Guido van Rossume2b70bc2006-08-18 22:13:04 +00001903 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001904 contains__doc__},
Raymond Hettinger0c669672003-12-13 13:31:55 +00001905 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001906 getitem__doc__},
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001907 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
1908 sizeof__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00001909 {"get", (PyCFunction)dict_get, METH_VARARGS,
1910 get__doc__},
1911 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1912 setdefault_doc__},
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001913 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
Just van Rossuma797d812002-11-23 09:45:04 +00001914 pop__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001915 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001916 popitem__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001917 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00001918 keys__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001919 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00001920 items__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001921 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00001922 values__doc__},
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001923 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001924 update__doc__},
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001925 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
1926 fromkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001927 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001928 clear__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001929 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001930 copy__doc__},
1931 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001932};
1933
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001934/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001935int
1936PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001937{
1938 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001939 PyDictObject *mp = (PyDictObject *)op;
1940 PyDictEntry *ep;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001941
Guido van Rossum89d8c602007-09-18 17:26:56 +00001942 if (!PyUnicode_CheckExact(key) ||
1943 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001944 hash = PyObject_Hash(key);
1945 if (hash == -1)
1946 return -1;
1947 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001948 ep = (mp->ma_lookup)(mp, key, hash);
1949 return ep == NULL ? -1 : (ep->me_value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001950}
1951
Thomas Wouterscf297e42007-02-23 15:07:44 +00001952/* Internal version of PyDict_Contains used when the hash value is already known */
1953int
1954_PyDict_Contains(PyObject *op, PyObject *key, long hash)
1955{
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001956 PyDictObject *mp = (PyDictObject *)op;
1957 PyDictEntry *ep;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001958
1959 ep = (mp->ma_lookup)(mp, key, hash);
1960 return ep == NULL ? -1 : (ep->me_value != NULL);
1961}
1962
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001963/* Hack to implement "key in dict" */
1964static PySequenceMethods dict_as_sequence = {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001965 0, /* sq_length */
1966 0, /* sq_concat */
1967 0, /* sq_repeat */
1968 0, /* sq_item */
1969 0, /* sq_slice */
1970 0, /* sq_ass_item */
1971 0, /* sq_ass_slice */
1972 PyDict_Contains, /* sq_contains */
1973 0, /* sq_inplace_concat */
1974 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001975};
1976
Guido van Rossum09e563a2001-05-01 12:10:21 +00001977static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001978dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1979{
1980 PyObject *self;
1981
1982 assert(type != NULL && type->tp_alloc != NULL);
1983 self = type->tp_alloc(type, 0);
1984 if (self != NULL) {
1985 PyDictObject *d = (PyDictObject *)self;
1986 /* It's guaranteed that tp->alloc zeroed out the struct. */
1987 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
1988 INIT_NONZERO_DICT_SLOTS(d);
Guido van Rossum89d8c602007-09-18 17:26:56 +00001989 d->ma_lookup = lookdict_unicode;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001990#ifdef SHOW_CONVERSION_COUNTS
1991 ++created;
1992#endif
1993 }
1994 return self;
1995}
1996
Tim Peters25786c02001-09-02 08:22:48 +00001997static int
1998dict_init(PyObject *self, PyObject *args, PyObject *kwds)
1999{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002000 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002001}
2002
Tim Peters6d6c1a32001-08-02 04:15:00 +00002003static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002004dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002005{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002006 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002007}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002008
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002009PyDoc_STRVAR(dictionary_doc,
Tim Petersa427a2b2001-10-29 22:25:45 +00002010"dict() -> new empty dictionary.\n"
2011"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Tim Peters1fc240e2001-10-26 05:06:50 +00002012" (key, value) pairs.\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002013"dict(seq) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002014" d = {}\n"
2015" for k, v in seq:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002016" d[k] = v\n"
2017"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2018" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002019
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002020PyTypeObject PyDict_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002021 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Petersa427a2b2001-10-29 22:25:45 +00002022 "dict",
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002023 sizeof(PyDictObject),
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002024 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00002025 (destructor)dict_dealloc, /* tp_dealloc */
Guido van Rossum04dbf3b2007-08-07 19:51:00 +00002026 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002027 0, /* tp_getattr */
Guido van Rossumb9324202001-01-18 00:39:02 +00002028 0, /* tp_setattr */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00002029 0, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00002030 (reprfunc)dict_repr, /* tp_repr */
2031 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002032 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00002033 &dict_as_mapping, /* tp_as_mapping */
Guido van Rossum50e9fb92006-08-17 05:42:55 +00002034 0, /* tp_hash */
Guido van Rossumb9324202001-01-18 00:39:02 +00002035 0, /* tp_call */
2036 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002037 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumb9324202001-01-18 00:39:02 +00002038 0, /* tp_setattro */
2039 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002040 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Thomas Wouters27d517b2007-02-25 20:39:11 +00002041 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
Tim Peters25786c02001-09-02 08:22:48 +00002042 dictionary_doc, /* tp_doc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002043 dict_traverse, /* tp_traverse */
2044 dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00002045 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002046 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00002047 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002048 0, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002049 mapp_methods, /* tp_methods */
2050 0, /* tp_members */
2051 0, /* tp_getset */
2052 0, /* tp_base */
2053 0, /* tp_dict */
2054 0, /* tp_descr_get */
2055 0, /* tp_descr_set */
2056 0, /* tp_dictoffset */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002057 dict_init, /* tp_init */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002058 PyType_GenericAlloc, /* tp_alloc */
2059 dict_new, /* tp_new */
Neil Schemenauer6189b892002-04-12 02:43:00 +00002060 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002061};
2062
Guido van Rossum3cca2451997-05-16 14:23:33 +00002063/* For backward compatibility with old dictionary interface */
2064
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002065PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002066PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002067{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002068 PyObject *kv, *rv;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002069 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002070 if (kv == NULL)
2071 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002072 rv = PyDict_GetItem(v, kv);
2073 Py_DECREF(kv);
2074 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002075}
2076
2077int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002078PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002079{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002080 PyObject *kv;
2081 int err;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002082 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002083 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002084 return -1;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002085 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00002086 err = PyDict_SetItem(v, kv, item);
2087 Py_DECREF(kv);
2088 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002089}
2090
2091int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002092PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002093{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002094 PyObject *kv;
2095 int err;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002096 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002097 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002098 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002099 err = PyDict_DelItem(v, kv);
2100 Py_DECREF(kv);
2101 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002102}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002103
Raymond Hettinger019a1482004-03-18 02:41:19 +00002104/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002105
2106typedef struct {
2107 PyObject_HEAD
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002108 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002109 Py_ssize_t di_used;
2110 Py_ssize_t di_pos;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002111 PyObject* di_result; /* reusable result tuple for iteritems */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002112 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002113} dictiterobject;
2114
2115static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002116dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002117{
2118 dictiterobject *di;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002119 di = PyObject_New(dictiterobject, itertype);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002120 if (di == NULL)
2121 return NULL;
2122 Py_INCREF(dict);
2123 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00002124 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002125 di->di_pos = 0;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002126 di->len = dict->ma_used;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002127 if (itertype == &PyDictIterItem_Type) {
2128 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2129 if (di->di_result == NULL) {
2130 Py_DECREF(di);
2131 return NULL;
2132 }
2133 }
2134 else
2135 di->di_result = NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002136 return (PyObject *)di;
2137}
2138
2139static void
2140dictiter_dealloc(dictiterobject *di)
2141{
Guido van Rossum2147df72002-07-16 20:30:22 +00002142 Py_XDECREF(di->di_dict);
Raymond Hettinger019a1482004-03-18 02:41:19 +00002143 Py_XDECREF(di->di_result);
Neil Schemenauer6189b892002-04-12 02:43:00 +00002144 PyObject_Del(di);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002145}
2146
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002147static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002148dictiter_len(dictiterobject *di)
2149{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002150 Py_ssize_t len = 0;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00002151 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002152 len = di->len;
Christian Heimes217cfd12007-12-02 14:31:20 +00002153 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002154}
2155
Guido van Rossumb90c8482007-02-10 01:11:45 +00002156PyDoc_STRVAR(length_hint_doc,
2157 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002158
2159static PyMethodDef dictiter_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002160 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
2161 length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002162 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002163};
2164
Raymond Hettinger019a1482004-03-18 02:41:19 +00002165static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002166{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002167 PyObject *key;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002168 register Py_ssize_t i, mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002169 register PyDictEntry *ep;
2170 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002171
Raymond Hettinger019a1482004-03-18 02:41:19 +00002172 if (d == NULL)
Guido van Rossum2147df72002-07-16 20:30:22 +00002173 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002174 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002175
Raymond Hettinger019a1482004-03-18 02:41:19 +00002176 if (di->di_used != d->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00002177 PyErr_SetString(PyExc_RuntimeError,
2178 "dictionary changed size during iteration");
Guido van Rossum2147df72002-07-16 20:30:22 +00002179 di->di_used = -1; /* Make this state sticky */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002180 return NULL;
2181 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002182
Raymond Hettinger019a1482004-03-18 02:41:19 +00002183 i = di->di_pos;
2184 if (i < 0)
2185 goto fail;
2186 ep = d->ma_table;
2187 mask = d->ma_mask;
2188 while (i <= mask && ep[i].me_value == NULL)
2189 i++;
2190 di->di_pos = i+1;
2191 if (i > mask)
2192 goto fail;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002193 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002194 key = ep[i].me_key;
2195 Py_INCREF(key);
2196 return key;
2197
2198fail:
2199 Py_DECREF(d);
Guido van Rossum2147df72002-07-16 20:30:22 +00002200 di->di_dict = NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002201 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002202}
2203
Raymond Hettinger019a1482004-03-18 02:41:19 +00002204PyTypeObject PyDictIterKey_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002205 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Christian Heimesf83be4e2007-11-28 09:44:38 +00002206 "dict_keyiterator", /* tp_name */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002207 sizeof(dictiterobject), /* tp_basicsize */
2208 0, /* tp_itemsize */
2209 /* methods */
2210 (destructor)dictiter_dealloc, /* tp_dealloc */
2211 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002212 0, /* tp_getattr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002213 0, /* tp_setattr */
2214 0, /* tp_compare */
2215 0, /* tp_repr */
2216 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002217 0, /* tp_as_sequence */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002218 0, /* tp_as_mapping */
2219 0, /* tp_hash */
2220 0, /* tp_call */
2221 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002222 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002223 0, /* tp_setattro */
2224 0, /* tp_as_buffer */
2225 Py_TPFLAGS_DEFAULT, /* tp_flags */
2226 0, /* tp_doc */
2227 0, /* tp_traverse */
2228 0, /* tp_clear */
2229 0, /* tp_richcompare */
2230 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002231 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002232 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002233 dictiter_methods, /* tp_methods */
2234 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002235};
2236
2237static PyObject *dictiter_iternextvalue(dictiterobject *di)
2238{
2239 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002240 register Py_ssize_t i, mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002241 register PyDictEntry *ep;
2242 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002243
2244 if (d == NULL)
2245 return NULL;
2246 assert (PyDict_Check(d));
2247
2248 if (di->di_used != d->ma_used) {
2249 PyErr_SetString(PyExc_RuntimeError,
2250 "dictionary changed size during iteration");
2251 di->di_used = -1; /* Make this state sticky */
2252 return NULL;
2253 }
2254
2255 i = di->di_pos;
Guido van Rossum09240f62004-03-20 19:11:58 +00002256 mask = d->ma_mask;
2257 if (i < 0 || i > mask)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002258 goto fail;
2259 ep = d->ma_table;
Guido van Rossum09240f62004-03-20 19:11:58 +00002260 while ((value=ep[i].me_value) == NULL) {
Raymond Hettinger019a1482004-03-18 02:41:19 +00002261 i++;
Guido van Rossum09240f62004-03-20 19:11:58 +00002262 if (i > mask)
2263 goto fail;
2264 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002265 di->di_pos = i+1;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002266 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002267 Py_INCREF(value);
2268 return value;
2269
2270fail:
2271 Py_DECREF(d);
2272 di->di_dict = NULL;
2273 return NULL;
2274}
2275
2276PyTypeObject PyDictIterValue_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002277 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Christian Heimesf83be4e2007-11-28 09:44:38 +00002278 "dict_valueiterator", /* tp_name */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002279 sizeof(dictiterobject), /* tp_basicsize */
2280 0, /* tp_itemsize */
2281 /* methods */
2282 (destructor)dictiter_dealloc, /* tp_dealloc */
2283 0, /* tp_print */
2284 0, /* tp_getattr */
2285 0, /* tp_setattr */
2286 0, /* tp_compare */
2287 0, /* tp_repr */
2288 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002289 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002290 0, /* tp_as_mapping */
2291 0, /* tp_hash */
2292 0, /* tp_call */
2293 0, /* tp_str */
2294 PyObject_GenericGetAttr, /* tp_getattro */
2295 0, /* tp_setattro */
2296 0, /* tp_as_buffer */
2297 Py_TPFLAGS_DEFAULT, /* tp_flags */
2298 0, /* tp_doc */
2299 0, /* tp_traverse */
2300 0, /* tp_clear */
2301 0, /* tp_richcompare */
2302 0, /* tp_weaklistoffset */
2303 PyObject_SelfIter, /* tp_iter */
2304 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002305 dictiter_methods, /* tp_methods */
2306 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002307};
2308
2309static PyObject *dictiter_iternextitem(dictiterobject *di)
2310{
2311 PyObject *key, *value, *result = di->di_result;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002312 register Py_ssize_t i, mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002313 register PyDictEntry *ep;
2314 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002315
2316 if (d == NULL)
2317 return NULL;
2318 assert (PyDict_Check(d));
2319
2320 if (di->di_used != d->ma_used) {
2321 PyErr_SetString(PyExc_RuntimeError,
2322 "dictionary changed size during iteration");
2323 di->di_used = -1; /* Make this state sticky */
2324 return NULL;
2325 }
2326
2327 i = di->di_pos;
2328 if (i < 0)
2329 goto fail;
2330 ep = d->ma_table;
2331 mask = d->ma_mask;
2332 while (i <= mask && ep[i].me_value == NULL)
2333 i++;
2334 di->di_pos = i+1;
2335 if (i > mask)
2336 goto fail;
2337
2338 if (result->ob_refcnt == 1) {
2339 Py_INCREF(result);
2340 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2341 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2342 } else {
2343 result = PyTuple_New(2);
Tim Peters60b29962006-01-01 01:19:23 +00002344 if (result == NULL)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002345 return NULL;
2346 }
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002347 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002348 key = ep[i].me_key;
2349 value = ep[i].me_value;
2350 Py_INCREF(key);
2351 Py_INCREF(value);
2352 PyTuple_SET_ITEM(result, 0, key);
2353 PyTuple_SET_ITEM(result, 1, value);
2354 return result;
2355
2356fail:
2357 Py_DECREF(d);
2358 di->di_dict = NULL;
2359 return NULL;
2360}
2361
2362PyTypeObject PyDictIterItem_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002363 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Christian Heimesf83be4e2007-11-28 09:44:38 +00002364 "dict_itemiterator", /* tp_name */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002365 sizeof(dictiterobject), /* tp_basicsize */
2366 0, /* tp_itemsize */
2367 /* methods */
2368 (destructor)dictiter_dealloc, /* tp_dealloc */
2369 0, /* tp_print */
2370 0, /* tp_getattr */
2371 0, /* tp_setattr */
2372 0, /* tp_compare */
2373 0, /* tp_repr */
2374 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002375 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002376 0, /* tp_as_mapping */
2377 0, /* tp_hash */
2378 0, /* tp_call */
2379 0, /* tp_str */
2380 PyObject_GenericGetAttr, /* tp_getattro */
2381 0, /* tp_setattro */
2382 0, /* tp_as_buffer */
2383 Py_TPFLAGS_DEFAULT, /* tp_flags */
2384 0, /* tp_doc */
2385 0, /* tp_traverse */
2386 0, /* tp_clear */
2387 0, /* tp_richcompare */
2388 0, /* tp_weaklistoffset */
2389 PyObject_SelfIter, /* tp_iter */
2390 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002391 dictiter_methods, /* tp_methods */
2392 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002393};
Guido van Rossumb90c8482007-02-10 01:11:45 +00002394
2395
Guido van Rossum3ac67412007-02-10 18:55:06 +00002396/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002397/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002398/***********************************************/
2399
Guido van Rossumb90c8482007-02-10 01:11:45 +00002400/* The instance lay-out is the same for all three; but the type differs. */
2401
2402typedef struct {
2403 PyObject_HEAD
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002404 PyDictObject *dv_dict;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002405} dictviewobject;
2406
2407
2408static void
Guido van Rossum3ac67412007-02-10 18:55:06 +00002409dictview_dealloc(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002410{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002411 Py_XDECREF(dv->dv_dict);
2412 PyObject_Del(dv);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002413}
2414
Guido van Rossum83825ac2007-02-10 04:54:19 +00002415static Py_ssize_t
Guido van Rossum3ac67412007-02-10 18:55:06 +00002416dictview_len(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002417{
2418 Py_ssize_t len = 0;
Guido van Rossum3ac67412007-02-10 18:55:06 +00002419 if (dv->dv_dict != NULL)
2420 len = dv->dv_dict->ma_used;
Guido van Rossum83825ac2007-02-10 04:54:19 +00002421 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002422}
2423
2424static PyObject *
2425dictview_new(PyObject *dict, PyTypeObject *type)
2426{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002427 dictviewobject *dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002428 if (dict == NULL) {
2429 PyErr_BadInternalCall();
2430 return NULL;
2431 }
2432 if (!PyDict_Check(dict)) {
2433 /* XXX Get rid of this restriction later */
2434 PyErr_Format(PyExc_TypeError,
2435 "%s() requires a dict argument, not '%s'",
2436 type->tp_name, dict->ob_type->tp_name);
2437 return NULL;
2438 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002439 dv = PyObject_New(dictviewobject, type);
2440 if (dv == NULL)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002441 return NULL;
2442 Py_INCREF(dict);
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002443 dv->dv_dict = (PyDictObject *)dict;
Guido van Rossum3ac67412007-02-10 18:55:06 +00002444 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002445}
2446
Neal Norwitze36f2ba2007-02-26 23:12:28 +00002447/* TODO(guido): The views objects are not complete:
2448
2449 * support more set operations
2450 * support arbitrary mappings?
2451 - either these should be static or exported in dictobject.h
2452 - if public then they should probably be in builtins
2453*/
2454
Guido van Rossumaac530c2007-08-24 22:33:45 +00002455/* Return 1 if self is a subset of other, iterating over self;
2456 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002457static int
2458all_contained_in(PyObject *self, PyObject *other)
2459{
2460 PyObject *iter = PyObject_GetIter(self);
2461 int ok = 1;
2462
2463 if (iter == NULL)
2464 return -1;
2465 for (;;) {
2466 PyObject *next = PyIter_Next(iter);
2467 if (next == NULL) {
2468 if (PyErr_Occurred())
2469 ok = -1;
2470 break;
2471 }
2472 ok = PySequence_Contains(other, next);
2473 Py_DECREF(next);
2474 if (ok <= 0)
2475 break;
2476 }
2477 Py_DECREF(iter);
2478 return ok;
2479}
2480
2481static PyObject *
2482dictview_richcompare(PyObject *self, PyObject *other, int op)
2483{
Guido van Rossumaac530c2007-08-24 22:33:45 +00002484 Py_ssize_t len_self, len_other;
2485 int ok;
2486 PyObject *result;
2487
Guido van Rossumd9214d12007-02-12 02:23:40 +00002488 assert(self != NULL);
2489 assert(PyDictViewSet_Check(self));
2490 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00002491
Guido van Rossumaac530c2007-08-24 22:33:45 +00002492 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other)) {
Guido van Rossumd9214d12007-02-12 02:23:40 +00002493 Py_INCREF(Py_NotImplemented);
2494 return Py_NotImplemented;
2495 }
Guido van Rossumaac530c2007-08-24 22:33:45 +00002496
2497 len_self = PyObject_Size(self);
2498 if (len_self < 0)
2499 return NULL;
2500 len_other = PyObject_Size(other);
2501 if (len_other < 0)
2502 return NULL;
2503
2504 ok = 0;
2505 switch(op) {
2506
2507 case Py_NE:
2508 case Py_EQ:
2509 if (len_self == len_other)
2510 ok = all_contained_in(self, other);
2511 if (op == Py_NE && ok >= 0)
2512 ok = !ok;
2513 break;
2514
2515 case Py_LT:
2516 if (len_self < len_other)
2517 ok = all_contained_in(self, other);
2518 break;
2519
2520 case Py_LE:
2521 if (len_self <= len_other)
2522 ok = all_contained_in(self, other);
2523 break;
2524
2525 case Py_GT:
2526 if (len_self > len_other)
2527 ok = all_contained_in(other, self);
2528 break;
2529
2530 case Py_GE:
2531 if (len_self >= len_other)
2532 ok = all_contained_in(other, self);
2533 break;
2534
2535 }
2536 if (ok < 0)
2537 return NULL;
2538 result = ok ? Py_True : Py_False;
2539 Py_INCREF(result);
2540 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00002541}
2542
Guido van Rossum3ac67412007-02-10 18:55:06 +00002543/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002544
2545static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002546dictkeys_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002547{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002548 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002549 Py_RETURN_NONE;
2550 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002551 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
2552}
2553
2554static int
2555dictkeys_contains(dictviewobject *dv, PyObject *obj)
2556{
2557 if (dv->dv_dict == NULL)
2558 return 0;
2559 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002560}
2561
Guido van Rossum83825ac2007-02-10 04:54:19 +00002562static PySequenceMethods dictkeys_as_sequence = {
2563 (lenfunc)dictview_len, /* sq_length */
2564 0, /* sq_concat */
2565 0, /* sq_repeat */
2566 0, /* sq_item */
2567 0, /* sq_slice */
2568 0, /* sq_ass_item */
2569 0, /* sq_ass_slice */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002570 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002571};
2572
Guido van Rossum523259b2007-08-24 23:41:22 +00002573static PyObject*
2574dictviews_sub(PyObject* self, PyObject *other)
2575{
2576 PyObject *result = PySet_New(self);
2577 PyObject *tmp;
2578 if (result == NULL)
2579 return NULL;
2580
2581 tmp = PyObject_CallMethod(result, "difference_update", "O", other);
2582 if (tmp == NULL) {
2583 Py_DECREF(result);
2584 return NULL;
2585 }
2586
2587 Py_DECREF(tmp);
2588 return result;
2589}
2590
2591static PyObject*
2592dictviews_and(PyObject* self, PyObject *other)
2593{
2594 PyObject *result = PySet_New(self);
2595 PyObject *tmp;
2596 if (result == NULL)
2597 return NULL;
2598
2599 tmp = PyObject_CallMethod(result, "intersection_update", "O", other);
2600 if (tmp == NULL) {
2601 Py_DECREF(result);
2602 return NULL;
2603 }
2604
2605 Py_DECREF(tmp);
2606 return result;
2607}
2608
2609static PyObject*
2610dictviews_or(PyObject* self, PyObject *other)
2611{
2612 PyObject *result = PySet_New(self);
2613 PyObject *tmp;
2614 if (result == NULL)
2615 return NULL;
2616
2617 tmp = PyObject_CallMethod(result, "update", "O", other);
2618 if (tmp == NULL) {
2619 Py_DECREF(result);
2620 return NULL;
2621 }
2622
2623 Py_DECREF(tmp);
2624 return result;
2625}
2626
2627static PyObject*
2628dictviews_xor(PyObject* self, PyObject *other)
2629{
2630 PyObject *result = PySet_New(self);
2631 PyObject *tmp;
2632 if (result == NULL)
2633 return NULL;
2634
2635 tmp = PyObject_CallMethod(result, "symmetric_difference_update", "O",
2636 other);
2637 if (tmp == NULL) {
2638 Py_DECREF(result);
2639 return NULL;
2640 }
2641
2642 Py_DECREF(tmp);
2643 return result;
2644}
2645
2646static PyNumberMethods dictviews_as_number = {
2647 0, /*nb_add*/
2648 (binaryfunc)dictviews_sub, /*nb_subtract*/
2649 0, /*nb_multiply*/
2650 0, /*nb_remainder*/
2651 0, /*nb_divmod*/
2652 0, /*nb_power*/
2653 0, /*nb_negative*/
2654 0, /*nb_positive*/
2655 0, /*nb_absolute*/
2656 0, /*nb_bool*/
2657 0, /*nb_invert*/
2658 0, /*nb_lshift*/
2659 0, /*nb_rshift*/
2660 (binaryfunc)dictviews_and, /*nb_and*/
2661 (binaryfunc)dictviews_xor, /*nb_xor*/
2662 (binaryfunc)dictviews_or, /*nb_or*/
2663};
2664
Guido van Rossumb90c8482007-02-10 01:11:45 +00002665static PyMethodDef dictkeys_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002666 {NULL, NULL} /* sentinel */
2667};
2668
2669PyTypeObject PyDictKeys_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002670 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002671 "dict_keys", /* tp_name */
2672 sizeof(dictviewobject), /* tp_basicsize */
2673 0, /* tp_itemsize */
2674 /* methods */
2675 (destructor)dictview_dealloc, /* tp_dealloc */
2676 0, /* tp_print */
2677 0, /* tp_getattr */
2678 0, /* tp_setattr */
2679 0, /* tp_compare */
2680 0, /* tp_repr */
Guido van Rossum523259b2007-08-24 23:41:22 +00002681 &dictviews_as_number, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002682 &dictkeys_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002683 0, /* tp_as_mapping */
2684 0, /* tp_hash */
2685 0, /* tp_call */
2686 0, /* tp_str */
2687 PyObject_GenericGetAttr, /* tp_getattro */
2688 0, /* tp_setattro */
2689 0, /* tp_as_buffer */
2690 Py_TPFLAGS_DEFAULT, /* tp_flags */
2691 0, /* tp_doc */
2692 0, /* tp_traverse */
2693 0, /* tp_clear */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002694 dictview_richcompare, /* tp_richcompare */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002695 0, /* tp_weaklistoffset */
2696 (getiterfunc)dictkeys_iter, /* tp_iter */
2697 0, /* tp_iternext */
2698 dictkeys_methods, /* tp_methods */
2699 0,
2700};
2701
2702static PyObject *
2703dictkeys_new(PyObject *dict)
2704{
2705 return dictview_new(dict, &PyDictKeys_Type);
2706}
2707
Guido van Rossum3ac67412007-02-10 18:55:06 +00002708/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002709
2710static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002711dictitems_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002712{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002713 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002714 Py_RETURN_NONE;
2715 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002716 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
2717}
2718
2719static int
2720dictitems_contains(dictviewobject *dv, PyObject *obj)
2721{
2722 PyObject *key, *value, *found;
2723 if (dv->dv_dict == NULL)
2724 return 0;
2725 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
2726 return 0;
2727 key = PyTuple_GET_ITEM(obj, 0);
2728 value = PyTuple_GET_ITEM(obj, 1);
2729 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
2730 if (found == NULL) {
2731 if (PyErr_Occurred())
2732 return -1;
2733 return 0;
2734 }
2735 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002736}
2737
Guido van Rossum83825ac2007-02-10 04:54:19 +00002738static PySequenceMethods dictitems_as_sequence = {
2739 (lenfunc)dictview_len, /* sq_length */
2740 0, /* sq_concat */
2741 0, /* sq_repeat */
2742 0, /* sq_item */
2743 0, /* sq_slice */
2744 0, /* sq_ass_item */
2745 0, /* sq_ass_slice */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002746 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002747};
2748
Guido van Rossumb90c8482007-02-10 01:11:45 +00002749static PyMethodDef dictitems_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002750 {NULL, NULL} /* sentinel */
2751};
2752
2753PyTypeObject PyDictItems_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002754 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002755 "dict_items", /* tp_name */
2756 sizeof(dictviewobject), /* tp_basicsize */
2757 0, /* tp_itemsize */
2758 /* methods */
2759 (destructor)dictview_dealloc, /* tp_dealloc */
2760 0, /* tp_print */
2761 0, /* tp_getattr */
2762 0, /* tp_setattr */
2763 0, /* tp_compare */
2764 0, /* tp_repr */
Guido van Rossum523259b2007-08-24 23:41:22 +00002765 &dictviews_as_number, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002766 &dictitems_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002767 0, /* tp_as_mapping */
2768 0, /* tp_hash */
2769 0, /* tp_call */
2770 0, /* tp_str */
2771 PyObject_GenericGetAttr, /* tp_getattro */
2772 0, /* tp_setattro */
2773 0, /* tp_as_buffer */
2774 Py_TPFLAGS_DEFAULT, /* tp_flags */
2775 0, /* tp_doc */
2776 0, /* tp_traverse */
2777 0, /* tp_clear */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002778 dictview_richcompare, /* tp_richcompare */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002779 0, /* tp_weaklistoffset */
2780 (getiterfunc)dictitems_iter, /* tp_iter */
2781 0, /* tp_iternext */
2782 dictitems_methods, /* tp_methods */
2783 0,
2784};
2785
2786static PyObject *
2787dictitems_new(PyObject *dict)
2788{
2789 return dictview_new(dict, &PyDictItems_Type);
2790}
2791
Guido van Rossum3ac67412007-02-10 18:55:06 +00002792/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002793
2794static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002795dictvalues_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002796{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002797 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002798 Py_RETURN_NONE;
2799 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002800 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002801}
2802
Guido van Rossum83825ac2007-02-10 04:54:19 +00002803static PySequenceMethods dictvalues_as_sequence = {
2804 (lenfunc)dictview_len, /* sq_length */
2805 0, /* sq_concat */
2806 0, /* sq_repeat */
2807 0, /* sq_item */
2808 0, /* sq_slice */
2809 0, /* sq_ass_item */
2810 0, /* sq_ass_slice */
2811 (objobjproc)0, /* sq_contains */
2812};
2813
Guido van Rossumb90c8482007-02-10 01:11:45 +00002814static PyMethodDef dictvalues_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002815 {NULL, NULL} /* sentinel */
2816};
2817
2818PyTypeObject PyDictValues_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002819 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002820 "dict_values", /* tp_name */
2821 sizeof(dictviewobject), /* tp_basicsize */
2822 0, /* tp_itemsize */
2823 /* methods */
2824 (destructor)dictview_dealloc, /* tp_dealloc */
2825 0, /* tp_print */
2826 0, /* tp_getattr */
2827 0, /* tp_setattr */
2828 0, /* tp_compare */
2829 0, /* tp_repr */
2830 0, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002831 &dictvalues_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002832 0, /* tp_as_mapping */
2833 0, /* tp_hash */
2834 0, /* tp_call */
2835 0, /* tp_str */
2836 PyObject_GenericGetAttr, /* tp_getattro */
2837 0, /* tp_setattro */
2838 0, /* tp_as_buffer */
2839 Py_TPFLAGS_DEFAULT, /* tp_flags */
2840 0, /* tp_doc */
2841 0, /* tp_traverse */
2842 0, /* tp_clear */
2843 0, /* tp_richcompare */
2844 0, /* tp_weaklistoffset */
2845 (getiterfunc)dictvalues_iter, /* tp_iter */
2846 0, /* tp_iternext */
2847 dictvalues_methods, /* tp_methods */
2848 0,
2849};
2850
2851static PyObject *
2852dictvalues_new(PyObject *dict)
2853{
2854 return dictview_new(dict, &PyDictValues_Type);
2855}