blob: 6f3ba1bea2a6a76c0934c7a15f3a277c2b88318b [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);
Georg Brandl6aa2d1f2008-08-12 08:35:52 +0000245 } else {
246 /* At least set ma_table and ma_mask; these are wrong
247 if an empty but presized dict is added to freelist */
248 INIT_NONZERO_DICT_SLOTS(mp);
Raymond Hettinger43442782004-03-17 21:55:03 +0000249 }
250 assert (mp->ma_used == 0);
251 assert (mp->ma_table == mp->ma_smalltable);
252 assert (mp->ma_mask == PyDict_MINSIZE - 1);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000253#ifdef SHOW_ALLOC_COUNT
254 count_reuse++;
255#endif
Raymond Hettinger43442782004-03-17 21:55:03 +0000256 } else {
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000257 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
Raymond Hettinger43442782004-03-17 21:55:03 +0000258 if (mp == NULL)
259 return NULL;
260 EMPTY_TO_MINSIZE(mp);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000261#ifdef SHOW_ALLOC_COUNT
262 count_alloc++;
263#endif
Raymond Hettinger43442782004-03-17 21:55:03 +0000264 }
Guido van Rossum89d8c602007-09-18 17:26:56 +0000265 mp->ma_lookup = lookdict_unicode;
Fred Drake1bff34a2000-08-31 19:31:38 +0000266#ifdef SHOW_CONVERSION_COUNTS
267 ++created;
268#endif
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000269 _PyObject_GC_TRACK(mp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000270 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000271}
272
273/*
274The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000275This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000276Open addressing is preferred over chaining since the link overhead for
277chaining would be substantial (100% with typical malloc overhead).
278
Tim Peterseb28ef22001-06-02 05:27:19 +0000279The initial probe index is computed as hash mod the table size. Subsequent
280probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000281
282All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000283
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000284The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000285contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000286Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000287
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000288lookdict() is general-purpose, and may return NULL if (and only if) a
289comparison raises an exception (this was new in Python 2.5).
Guido van Rossum89d8c602007-09-18 17:26:56 +0000290lookdict_unicode() below is specialized to string keys, comparison of which can
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000291never raise an exception; that function can never return NULL. For both, when
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000292the key isn't found a PyDictEntry* is returned for which the me_value field is
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000293NULL; this is the slot in the dict at which the key would have been found, and
294the caller can (if it wishes) add the <key, value> pair to the returned
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000295PyDictEntry*.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000296*/
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000297static PyDictEntry *
298lookdict(PyDictObject *mp, PyObject *key, register long hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000299{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000300 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000301 register size_t perturb;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000302 register PyDictEntry *freeslot;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000303 register size_t mask = (size_t)mp->ma_mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000304 PyDictEntry *ep0 = mp->ma_table;
305 register PyDictEntry *ep;
Fred Drakec88b99c2000-08-31 19:04:07 +0000306 register int cmp;
Tim Peters453163d2001-06-03 04:54:32 +0000307 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000308
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000309 i = (size_t)hash & mask;
Guido van Rossumefb46091997-01-29 15:53:56 +0000310 ep = &ep0[i];
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000311 if (ep->me_key == NULL || ep->me_key == key)
Guido van Rossum7d181591997-01-16 21:06:45 +0000312 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000313
Guido van Rossum16e93a81997-01-28 00:00:11 +0000314 if (ep->me_key == dummy)
Guido van Rossum7d181591997-01-16 21:06:45 +0000315 freeslot = ep;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000316 else {
Fred Drakec88b99c2000-08-31 19:04:07 +0000317 if (ep->me_hash == hash) {
Tim Peters453163d2001-06-03 04:54:32 +0000318 startkey = ep->me_key;
Guido van Rossum2fd4f372007-11-29 18:43:05 +0000319 Py_INCREF(startkey);
Tim Peters453163d2001-06-03 04:54:32 +0000320 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Guido van Rossum2fd4f372007-11-29 18:43:05 +0000321 Py_DECREF(startkey);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000322 if (cmp < 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000323 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000324 if (ep0 == mp->ma_table && ep->me_key == startkey) {
325 if (cmp > 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000326 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000327 }
328 else {
329 /* The compare did major nasty stuff to the
330 * dict: start over.
331 * XXX A clever adversary could prevent this
332 * XXX from terminating.
333 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000334 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000335 }
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000336 }
337 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000338 }
Tim Peters15d49292001-05-27 07:39:22 +0000339
Tim Peters342c65e2001-05-13 06:43:53 +0000340 /* In the loop, me_key == dummy is by far (factor of 100s) the
341 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000342 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
343 i = (i << 2) + i + perturb + 1;
344 ep = &ep0[i & mask];
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000345 if (ep->me_key == NULL)
346 return freeslot == NULL ? ep : freeslot;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000347 if (ep->me_key == key)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000348 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000349 if (ep->me_hash == hash && ep->me_key != dummy) {
Tim Peters453163d2001-06-03 04:54:32 +0000350 startkey = ep->me_key;
Guido van Rossum2fd4f372007-11-29 18:43:05 +0000351 Py_INCREF(startkey);
Tim Peters453163d2001-06-03 04:54:32 +0000352 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Guido van Rossum2fd4f372007-11-29 18:43:05 +0000353 Py_DECREF(startkey);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000354 if (cmp < 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000355 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000356 if (ep0 == mp->ma_table && ep->me_key == startkey) {
357 if (cmp > 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000358 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000359 }
360 else {
361 /* The compare did major nasty stuff to the
362 * dict: start over.
363 * XXX A clever adversary could prevent this
364 * XXX from terminating.
365 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000366 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000367 }
Guido van Rossum16e93a81997-01-28 00:00:11 +0000368 }
Tim Peters342c65e2001-05-13 06:43:53 +0000369 else if (ep->me_key == dummy && freeslot == NULL)
370 freeslot = ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000371 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000372 assert(0); /* NOT REACHED */
373 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000374}
375
376/*
Guido van Rossum89d8c602007-09-18 17:26:56 +0000377 * Hacked up version of lookdict which can assume keys are always
378 * unicodes; this assumption allows testing for errors during
379 * PyObject_RichCompareBool() to be dropped; unicode-unicode
380 * comparisons never raise exceptions. This also means we don't need
381 * to go through PyObject_RichCompareBool(); we can always use
382 * unicode_eq() directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000383 *
Guido van Rossum89d8c602007-09-18 17:26:56 +0000384 * This is valuable because dicts with only unicode keys are very common.
Fred Drake1bff34a2000-08-31 19:31:38 +0000385 */
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000386static PyDictEntry *
387lookdict_unicode(PyDictObject *mp, PyObject *key, register long hash)
Fred Drake1bff34a2000-08-31 19:31:38 +0000388{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000389 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000390 register size_t perturb;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000391 register PyDictEntry *freeslot;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000392 register size_t mask = (size_t)mp->ma_mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000393 PyDictEntry *ep0 = mp->ma_table;
394 register PyDictEntry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000395
Guido van Rossum89d8c602007-09-18 17:26:56 +0000396 /* Make sure this function doesn't have to handle non-unicode keys,
Tim Peters0ab085c2001-09-14 00:25:33 +0000397 including subclasses of str; e.g., one reason to subclass
Guido van Rossum89d8c602007-09-18 17:26:56 +0000398 unicodes is to override __eq__, and for speed we don't cater to
Tim Peters0ab085c2001-09-14 00:25:33 +0000399 that here. */
Guido van Rossum89d8c602007-09-18 17:26:56 +0000400 if (!PyUnicode_CheckExact(key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000401#ifdef SHOW_CONVERSION_COUNTS
402 ++converted;
403#endif
404 mp->ma_lookup = lookdict;
405 return lookdict(mp, key, hash);
406 }
Tim Peters2f228e72001-05-13 00:19:31 +0000407 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000408 ep = &ep0[i];
409 if (ep->me_key == NULL || ep->me_key == key)
410 return ep;
411 if (ep->me_key == dummy)
412 freeslot = ep;
413 else {
Guido van Rossum89d8c602007-09-18 17:26:56 +0000414 if (ep->me_hash == hash && unicode_eq(ep->me_key, key))
Fred Drake1bff34a2000-08-31 19:31:38 +0000415 return ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000416 freeslot = NULL;
417 }
Tim Peters15d49292001-05-27 07:39:22 +0000418
Tim Peters342c65e2001-05-13 06:43:53 +0000419 /* In the loop, me_key == dummy is by far (factor of 100s) the
420 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000421 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
422 i = (i << 2) + i + perturb + 1;
423 ep = &ep0[i & mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000424 if (ep->me_key == NULL)
425 return freeslot == NULL ? ep : freeslot;
426 if (ep->me_key == key
427 || (ep->me_hash == hash
428 && ep->me_key != dummy
Guido van Rossum89d8c602007-09-18 17:26:56 +0000429 && unicode_eq(ep->me_key, key)))
Fred Drake1bff34a2000-08-31 19:31:38 +0000430 return ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000431 if (ep->me_key == dummy && freeslot == NULL)
Tim Peters342c65e2001-05-13 06:43:53 +0000432 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000433 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000434 assert(0); /* NOT REACHED */
435 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000436}
437
438/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000439Internal routine to insert a new item into the table.
440Used both by the internal resize routine and by the public insert routine.
441Eats a reference to key and one to value.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000442Returns -1 if an error occurred, or 0 on success.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000443*/
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000444static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000445insertdict(register PyDictObject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000446{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000447 PyObject *old_value;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000448 register PyDictEntry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000449 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
450
451 assert(mp->ma_lookup != NULL);
452 ep = mp->ma_lookup(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000453 if (ep == NULL) {
454 Py_DECREF(key);
455 Py_DECREF(value);
456 return -1;
457 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000458 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000459 old_value = ep->me_value;
460 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000461 Py_DECREF(old_value); /* which **CAN** re-enter */
462 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000463 }
464 else {
465 if (ep->me_key == NULL)
466 mp->ma_fill++;
Raymond Hettinger07ead172005-02-05 23:42:57 +0000467 else {
468 assert(ep->me_key == dummy);
469 Py_DECREF(dummy);
470 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000471 ep->me_key = key;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000472 ep->me_hash = (Py_ssize_t)hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000473 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000474 mp->ma_used++;
475 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000476 return 0;
477}
478
479/*
480Internal routine used by dictresize() to insert an item which is
481known to be absent from the dict. This routine also assumes that
482the dict contains no deleted entries. Besides the performance benefit,
483using insertdict() in dictresize() is dangerous (SF bug #1456209).
484Note that no refcounts are changed by this routine; if needed, the caller
485is responsible for incref'ing `key` and `value`.
486*/
487static void
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000488insertdict_clean(register PyDictObject *mp, PyObject *key, long hash,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000489 PyObject *value)
490{
491 register size_t i;
492 register size_t perturb;
493 register size_t mask = (size_t)mp->ma_mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000494 PyDictEntry *ep0 = mp->ma_table;
495 register PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000496
497 i = hash & mask;
498 ep = &ep0[i];
499 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
500 i = (i << 2) + i + perturb + 1;
501 ep = &ep0[i & mask];
502 }
503 assert(ep->me_value == NULL);
504 mp->ma_fill++;
505 ep->me_key = key;
506 ep->me_hash = (Py_ssize_t)hash;
507 ep->me_value = value;
508 mp->ma_used++;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000509}
510
511/*
512Restructure the table by allocating a new table and reinserting all
513items again. When entries have been deleted, the new table may
514actually be smaller than the old one.
515*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000516static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000517dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000518{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000519 Py_ssize_t newsize;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000520 PyDictEntry *oldtable, *newtable, *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000521 Py_ssize_t i;
Tim Peters0c6010b2001-05-23 23:33:57 +0000522 int is_oldtable_malloced;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000523 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000524
525 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000526
Tim Peterseb28ef22001-06-02 05:27:19 +0000527 /* Find the smallest table size > minused. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000528 for (newsize = PyDict_MINSIZE;
Tim Peterseb28ef22001-06-02 05:27:19 +0000529 newsize <= minused && newsize > 0;
530 newsize <<= 1)
531 ;
532 if (newsize <= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000533 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000534 return -1;
535 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000536
537 /* Get space for a new table. */
538 oldtable = mp->ma_table;
539 assert(oldtable != NULL);
540 is_oldtable_malloced = oldtable != mp->ma_smalltable;
541
Tim Peters6d6c1a32001-08-02 04:15:00 +0000542 if (newsize == PyDict_MINSIZE) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000543 /* A large table is shrinking, or we can't get any smaller. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000544 newtable = mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000545 if (newtable == oldtable) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000546 if (mp->ma_fill == mp->ma_used) {
547 /* No dummies, so no point doing anything. */
Tim Peters0c6010b2001-05-23 23:33:57 +0000548 return 0;
Tim Petersf8a548c2001-05-24 16:26:40 +0000549 }
550 /* We're not going to resize it, but rebuild the
551 table anyway to purge old dummy entries.
552 Subtle: This is *necessary* if fill==size,
553 as lookdict needs at least one virgin slot to
554 terminate failing searches. If fill < size, it's
555 merely desirable, as dummies slow searches. */
556 assert(mp->ma_fill > mp->ma_used);
Tim Peters0c6010b2001-05-23 23:33:57 +0000557 memcpy(small_copy, oldtable, sizeof(small_copy));
558 oldtable = small_copy;
559 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000560 }
561 else {
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000562 newtable = PyMem_NEW(PyDictEntry, newsize);
Tim Petersdea48ec2001-05-22 20:40:22 +0000563 if (newtable == NULL) {
564 PyErr_NoMemory();
565 return -1;
566 }
567 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000568
569 /* Make the dict empty, using the new table. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000570 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000571 mp->ma_table = newtable;
Tim Petersafb6ae82001-06-04 21:00:21 +0000572 mp->ma_mask = newsize - 1;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000573 memset(newtable, 0, sizeof(PyDictEntry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000574 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000575 i = mp->ma_fill;
576 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000577
Tim Peters19283142001-05-17 22:25:34 +0000578 /* Copy the data over; this is refcount-neutral for active entries;
579 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000580 for (ep = oldtable; i > 0; ep++) {
581 if (ep->me_value != NULL) { /* active entry */
582 --i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000583 insertdict_clean(mp, ep->me_key, (long)ep->me_hash,
584 ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000585 }
Tim Peters19283142001-05-17 22:25:34 +0000586 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000587 --i;
Tim Peters19283142001-05-17 22:25:34 +0000588 assert(ep->me_key == dummy);
589 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000590 }
Tim Peters19283142001-05-17 22:25:34 +0000591 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000592 }
593
Tim Peters0c6010b2001-05-23 23:33:57 +0000594 if (is_oldtable_malloced)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000595 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000596 return 0;
597}
598
Christian Heimes99170a52007-12-19 02:07:34 +0000599/* Create a new dictionary pre-sized to hold an estimated number of elements.
600 Underestimates are okay because the dictionary will resize as necessary.
601 Overestimates just mean the dictionary will be more sparse than usual.
602*/
603
604PyObject *
605_PyDict_NewPresized(Py_ssize_t minused)
606{
607 PyObject *op = PyDict_New();
608
609 if (minused>5 && op != NULL && dictresize((PyDictObject *)op, minused) == -1) {
610 Py_DECREF(op);
611 return NULL;
612 }
613 return op;
614}
615
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000616/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
617 * that may occur (originally dicts supported only string keys, and exceptions
618 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000619 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000620 * (suppressed) occurred while computing the key's hash, or that some error
621 * (suppressed) occurred when comparing keys in the dict's internal probe
622 * sequence. A nasty example of the latter is when a Python-coded comparison
623 * function hits a stack-depth error, which can cause this to return NULL
624 * even if the key is present.
625 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000626PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000627PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000628{
629 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000630 PyDictObject *mp = (PyDictObject *)op;
631 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000632 PyThreadState *tstate;
633 if (!PyDict_Check(op))
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000634 return NULL;
Guido van Rossum4d027722007-09-18 04:30:42 +0000635 if (!PyUnicode_CheckExact(key) ||
636 (hash = ((PyUnicodeObject *) key)->hash) == -1)
Guido van Rossumca756f21997-01-23 19:39:29 +0000637 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000638 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000639 if (hash == -1) {
640 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000641 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000642 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000643 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000644
645 /* We can arrive here with a NULL tstate during initialization:
646 try running "python -Wi" for an example related to string
647 interning. Let's just hope that no exception occurs then... */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000648 tstate = _PyThreadState_Current;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000649 if (tstate != NULL && tstate->curexc_type != NULL) {
650 /* preserve the existing exception */
651 PyObject *err_type, *err_value, *err_tb;
652 PyErr_Fetch(&err_type, &err_value, &err_tb);
653 ep = (mp->ma_lookup)(mp, key, hash);
654 /* ignore errors */
655 PyErr_Restore(err_type, err_value, err_tb);
656 if (ep == NULL)
657 return NULL;
658 }
659 else {
660 ep = (mp->ma_lookup)(mp, key, hash);
661 if (ep == NULL) {
662 PyErr_Clear();
663 return NULL;
664 }
665 }
666 return ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000667}
668
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000669/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
670 This returns NULL *with* an exception set if an exception occurred.
671 It returns NULL *without* an exception set if the key wasn't present.
672*/
673PyObject *
674PyDict_GetItemWithError(PyObject *op, PyObject *key)
675{
676 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000677 PyDictObject*mp = (PyDictObject *)op;
678 PyDictEntry *ep;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000679
680 if (!PyDict_Check(op)) {
681 PyErr_BadInternalCall();
682 return NULL;
683 }
Guido van Rossum89d8c602007-09-18 17:26:56 +0000684 if (!PyUnicode_CheckExact(key) ||
685 (hash = ((PyUnicodeObject *) key)->hash) == -1)
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000686 {
687 hash = PyObject_Hash(key);
688 if (hash == -1) {
689 return NULL;
690 }
691 }
692
693 ep = (mp->ma_lookup)(mp, key, hash);
694 if (ep == NULL)
695 return NULL;
696 return ep->me_value;
697}
698
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000699/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
Tim Peters60b29962006-01-01 01:19:23 +0000700 * dictionary if it's merely replacing the value for an existing key.
701 * This means that it's safe to loop over a dictionary with PyDict_Next()
702 * and occasionally replace a value -- but you can't insert new keys or
703 * remove them.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000704 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000705int
Tim Peters1f5871e2000-07-04 17:44:48 +0000706PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000707{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000708 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000709 register long hash;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000710 register Py_ssize_t n_used;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000711
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000712 if (!PyDict_Check(op)) {
713 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000714 return -1;
715 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000716 assert(key);
717 assert(value);
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000718 mp = (PyDictObject *)op;
Guido van Rossum4d027722007-09-18 04:30:42 +0000719 if (!PyUnicode_CheckExact(key) ||
720 (hash = ((PyUnicodeObject *) key)->hash) == -1)
721 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000722 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000723 if (hash == -1)
724 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000725 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000726 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000727 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000728 Py_INCREF(value);
729 Py_INCREF(key);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000730 if (insertdict(mp, key, hash, value) != 0)
731 return -1;
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000732 /* If we added a key, we can safely resize. Otherwise just return!
733 * If fill >= 2/3 size, adjust size. Normally, this doubles or
734 * quaduples the size, but it's also possible for the dict to shrink
Tim Peters60b29962006-01-01 01:19:23 +0000735 * (if ma_fill is much larger than ma_used, meaning a lot of dict
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000736 * keys have been * deleted).
Tim Peters60b29962006-01-01 01:19:23 +0000737 *
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000738 * Quadrupling the size improves average dictionary sparseness
739 * (reducing collisions) at the cost of some memory and iteration
740 * speed (which loops over every possible entry). It also halves
741 * the number of expensive resize operations in a growing dictionary.
Tim Peters60b29962006-01-01 01:19:23 +0000742 *
743 * Very large dictionaries (over 50K items) use doubling instead.
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000744 * This may help applications with severe memory constraints.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000745 */
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000746 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
747 return 0;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000748 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000749}
750
751int
Tim Peters1f5871e2000-07-04 17:44:48 +0000752PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000753{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000754 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000755 register long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000756 register PyDictEntry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000757 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000758
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000759 if (!PyDict_Check(op)) {
760 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000761 return -1;
762 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000763 assert(key);
Guido van Rossum89d8c602007-09-18 17:26:56 +0000764 if (!PyUnicode_CheckExact(key) ||
765 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000766 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000767 if (hash == -1)
768 return -1;
769 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000770 mp = (PyDictObject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000771 ep = (mp->ma_lookup)(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000772 if (ep == NULL)
773 return -1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000774 if (ep->me_value == NULL) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000775 set_key_error(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000776 return -1;
777 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000778 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000779 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000780 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000781 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000782 ep->me_value = NULL;
783 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000784 Py_DECREF(old_value);
785 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000786 return 0;
787}
788
Guido van Rossum25831651993-05-19 14:50:45 +0000789void
Tim Peters1f5871e2000-07-04 17:44:48 +0000790PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000791{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000792 PyDictObject *mp;
793 PyDictEntry *ep, *table;
Tim Petersdea48ec2001-05-22 20:40:22 +0000794 int table_is_malloced;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000795 Py_ssize_t fill;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000796 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000797#ifdef Py_DEBUG
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000798 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +0000799#endif
800
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000801 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000802 return;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000803 mp = (PyDictObject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000804#ifdef Py_DEBUG
Tim Petersafb6ae82001-06-04 21:00:21 +0000805 n = mp->ma_mask + 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000806 i = 0;
807#endif
808
809 table = mp->ma_table;
810 assert(table != NULL);
811 table_is_malloced = table != mp->ma_smalltable;
812
813 /* This is delicate. During the process of clearing the dict,
814 * decrefs can cause the dict to mutate. To avoid fatal confusion
815 * (voice of experience), we have to make the dict empty before
816 * clearing the slots, and never refer to anything via mp->xxx while
817 * clearing.
818 */
819 fill = mp->ma_fill;
820 if (table_is_malloced)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000821 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000822
823 else if (fill > 0) {
824 /* It's a small table with something that needs to be cleared.
825 * Afraid the only safe way is to copy the dict entries into
826 * another small table first.
827 */
828 memcpy(small_copy, table, sizeof(small_copy));
829 table = small_copy;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000830 EMPTY_TO_MINSIZE(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000831 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000832 /* else it's a small table that's already empty */
833
834 /* Now we can finally clear things. If C had refcounts, we could
835 * assert that the refcount on table is 1 now, i.e. that this function
836 * has unique access to it, so decref side-effects can't alter it.
837 */
838 for (ep = table; fill > 0; ++ep) {
839#ifdef Py_DEBUG
840 assert(i < n);
841 ++i;
842#endif
843 if (ep->me_key) {
844 --fill;
845 Py_DECREF(ep->me_key);
846 Py_XDECREF(ep->me_value);
847 }
848#ifdef Py_DEBUG
849 else
850 assert(ep->me_value == NULL);
851#endif
852 }
853
854 if (table_is_malloced)
855 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000856}
857
Tim Peters080c88b2003-02-15 03:01:11 +0000858/*
859 * Iterate over a dict. Use like so:
860 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000861 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +0000862 * PyObject *key, *value;
863 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +0000864 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +0000865 * Refer to borrowed references in key and value.
866 * }
867 *
868 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +0000869 * mutates the dict. One exception: it is safe if the loop merely changes
870 * the values associated with the keys (but doesn't insert new keys or
871 * delete keys), via PyDict_SetItem().
872 */
Guido van Rossum25831651993-05-19 14:50:45 +0000873int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000874PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000875{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000876 register Py_ssize_t i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000877 register Py_ssize_t mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000878 register PyDictEntry *ep;
Raymond Hettinger43442782004-03-17 21:55:03 +0000879
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000880 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000881 return 0;
Guido van Rossum25831651993-05-19 14:50:45 +0000882 i = *ppos;
883 if (i < 0)
884 return 0;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000885 ep = ((PyDictObject *)op)->ma_table;
886 mask = ((PyDictObject *)op)->ma_mask;
Raymond Hettinger43442782004-03-17 21:55:03 +0000887 while (i <= mask && ep[i].me_value == NULL)
Guido van Rossum25831651993-05-19 14:50:45 +0000888 i++;
889 *ppos = i+1;
Raymond Hettinger43442782004-03-17 21:55:03 +0000890 if (i > mask)
Guido van Rossum25831651993-05-19 14:50:45 +0000891 return 0;
892 if (pkey)
Raymond Hettinger43442782004-03-17 21:55:03 +0000893 *pkey = ep[i].me_key;
Guido van Rossum25831651993-05-19 14:50:45 +0000894 if (pvalue)
Raymond Hettinger43442782004-03-17 21:55:03 +0000895 *pvalue = ep[i].me_value;
Guido van Rossum25831651993-05-19 14:50:45 +0000896 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000897}
898
Thomas Wouterscf297e42007-02-23 15:07:44 +0000899/* Internal version of PyDict_Next that returns a hash value in addition to the key and value.*/
900int
901_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, long *phash)
902{
903 register Py_ssize_t i;
904 register Py_ssize_t mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000905 register PyDictEntry *ep;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000906
907 if (!PyDict_Check(op))
908 return 0;
909 i = *ppos;
910 if (i < 0)
911 return 0;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000912 ep = ((PyDictObject *)op)->ma_table;
913 mask = ((PyDictObject *)op)->ma_mask;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000914 while (i <= mask && ep[i].me_value == NULL)
915 i++;
916 *ppos = i+1;
917 if (i > mask)
918 return 0;
919 *phash = (long)(ep[i].me_hash);
920 if (pkey)
921 *pkey = ep[i].me_key;
922 if (pvalue)
923 *pvalue = ep[i].me_value;
924 return 1;
925}
926
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000927/* Methods */
928
929static void
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000930dict_dealloc(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000931{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000932 register PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000933 Py_ssize_t fill = mp->ma_fill;
Guido van Rossumff413af2002-03-28 20:34:59 +0000934 PyObject_GC_UnTrack(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000935 Py_TRASHCAN_SAFE_BEGIN(mp)
Tim Petersdea48ec2001-05-22 20:40:22 +0000936 for (ep = mp->ma_table; fill > 0; ep++) {
937 if (ep->me_key) {
938 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000939 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +0000940 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +0000941 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000942 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000943 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000944 PyMem_DEL(mp->ma_table);
Christian Heimes2202f872008-02-06 14:31:34 +0000945 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
946 free_list[numfree++] = mp;
Tim Peters60b29962006-01-01 01:19:23 +0000947 else
Christian Heimes90aa7642007-12-19 02:45:37 +0000948 Py_TYPE(mp)->tp_free((PyObject *)mp);
Guido van Rossumd724b232000-03-13 16:01:29 +0000949 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000950}
951
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000952static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000953dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000954{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000955 Py_ssize_t i;
Tim Petersa7259592001-06-16 05:11:17 +0000956 PyObject *s, *temp, *colon = NULL;
957 PyObject *pieces = NULL, *result = NULL;
958 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +0000959
Tim Petersa7259592001-06-16 05:11:17 +0000960 i = Py_ReprEnter((PyObject *)mp);
Guido van Rossum255443b1998-04-10 22:47:14 +0000961 if (i != 0) {
Walter Dörwald1ab83302007-05-18 17:15:44 +0000962 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
Guido van Rossum255443b1998-04-10 22:47:14 +0000963 }
964
Tim Petersa7259592001-06-16 05:11:17 +0000965 if (mp->ma_used == 0) {
Walter Dörwald1ab83302007-05-18 17:15:44 +0000966 result = PyUnicode_FromString("{}");
Tim Petersa7259592001-06-16 05:11:17 +0000967 goto Done;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000968 }
Tim Petersa7259592001-06-16 05:11:17 +0000969
970 pieces = PyList_New(0);
971 if (pieces == NULL)
972 goto Done;
973
Walter Dörwald1ab83302007-05-18 17:15:44 +0000974 colon = PyUnicode_FromString(": ");
Tim Petersa7259592001-06-16 05:11:17 +0000975 if (colon == NULL)
976 goto Done;
977
978 /* Do repr() on each key+value pair, and insert ": " between them.
979 Note that repr may mutate the dict. */
Tim Petersc6057842001-06-16 07:52:53 +0000980 i = 0;
981 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Tim Petersa7259592001-06-16 05:11:17 +0000982 int status;
983 /* Prevent repr from deleting value during key format. */
984 Py_INCREF(value);
985 s = PyObject_Repr(key);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000986 PyUnicode_Append(&s, colon);
987 PyUnicode_AppendAndDel(&s, PyObject_Repr(value));
Tim Petersa7259592001-06-16 05:11:17 +0000988 Py_DECREF(value);
989 if (s == NULL)
990 goto Done;
991 status = PyList_Append(pieces, s);
992 Py_DECREF(s); /* append created a new ref */
993 if (status < 0)
994 goto Done;
995 }
996
997 /* Add "{}" decorations to the first and last items. */
998 assert(PyList_GET_SIZE(pieces) > 0);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000999 s = PyUnicode_FromString("{");
Tim Petersa7259592001-06-16 05:11:17 +00001000 if (s == NULL)
1001 goto Done;
1002 temp = PyList_GET_ITEM(pieces, 0);
Walter Dörwald1ab83302007-05-18 17:15:44 +00001003 PyUnicode_AppendAndDel(&s, temp);
Tim Petersa7259592001-06-16 05:11:17 +00001004 PyList_SET_ITEM(pieces, 0, s);
1005 if (s == NULL)
1006 goto Done;
1007
Walter Dörwald1ab83302007-05-18 17:15:44 +00001008 s = PyUnicode_FromString("}");
Tim Petersa7259592001-06-16 05:11:17 +00001009 if (s == NULL)
1010 goto Done;
1011 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
Walter Dörwald1ab83302007-05-18 17:15:44 +00001012 PyUnicode_AppendAndDel(&temp, s);
Tim Petersa7259592001-06-16 05:11:17 +00001013 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
1014 if (temp == NULL)
1015 goto Done;
1016
1017 /* Paste them all together with ", " between. */
Walter Dörwald1ab83302007-05-18 17:15:44 +00001018 s = PyUnicode_FromString(", ");
Tim Petersa7259592001-06-16 05:11:17 +00001019 if (s == NULL)
1020 goto Done;
Walter Dörwald1ab83302007-05-18 17:15:44 +00001021 result = PyUnicode_Join(s, pieces);
Tim Peters0ab085c2001-09-14 00:25:33 +00001022 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +00001023
1024Done:
1025 Py_XDECREF(pieces);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001026 Py_XDECREF(colon);
Tim Petersa7259592001-06-16 05:11:17 +00001027 Py_ReprLeave((PyObject *)mp);
1028 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001029}
1030
Martin v. Löwis18e16552006-02-15 17:27:45 +00001031static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001032dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001033{
1034 return mp->ma_used;
1035}
1036
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001037static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001038dict_subscript(PyDictObject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001039{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001040 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001041 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001042 PyDictEntry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +00001043 assert(mp->ma_table != NULL);
Guido van Rossum89d8c602007-09-18 17:26:56 +00001044 if (!PyUnicode_CheckExact(key) ||
1045 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001046 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001047 if (hash == -1)
1048 return NULL;
1049 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001050 ep = (mp->ma_lookup)(mp, key, hash);
1051 if (ep == NULL)
1052 return NULL;
1053 v = ep->me_value;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001054 if (v == NULL) {
1055 if (!PyDict_CheckExact(mp)) {
1056 /* Look up __missing__ method if we're a subclass. */
Guido van Rossum4b92a822006-02-25 23:32:30 +00001057 PyObject *missing;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001058 static PyObject *missing_str = NULL;
1059 if (missing_str == NULL)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001060 missing_str =
Martin v. Löwis5b222132007-06-10 09:51:05 +00001061 PyUnicode_InternFromString("__missing__");
Christian Heimes90aa7642007-12-19 02:45:37 +00001062 missing = _PyType_Lookup(Py_TYPE(mp), missing_str);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001063 if (missing != NULL)
1064 return PyObject_CallFunctionObjArgs(missing,
1065 (PyObject *)mp, key, NULL);
1066 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001067 set_key_error(key);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001068 return NULL;
1069 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001070 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001071 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001072 return v;
1073}
1074
1075static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001076dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001077{
1078 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001079 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001080 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001081 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001082}
1083
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001084static PyMappingMethods dict_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001085 (lenfunc)dict_length, /*mp_length*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001086 (binaryfunc)dict_subscript, /*mp_subscript*/
1087 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001088};
1089
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001090static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001091dict_keys(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001092{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001093 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001094 register Py_ssize_t i, j;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001095 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001096 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001097
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001098 again:
1099 n = mp->ma_used;
1100 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001101 if (v == NULL)
1102 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001103 if (n != mp->ma_used) {
1104 /* Durnit. The allocations caused the dict to resize.
1105 * Just start over, this shouldn't normally happen.
1106 */
1107 Py_DECREF(v);
1108 goto again;
1109 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001110 ep = mp->ma_table;
1111 mask = mp->ma_mask;
1112 for (i = 0, j = 0; i <= mask; i++) {
1113 if (ep[i].me_value != NULL) {
1114 PyObject *key = ep[i].me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001115 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001116 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001117 j++;
1118 }
1119 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001120 assert(j == n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001121 return v;
1122}
1123
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001124static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001125dict_values(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001126{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001127 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001128 register Py_ssize_t i, j;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001129 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001130 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001131
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001132 again:
1133 n = mp->ma_used;
1134 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001135 if (v == NULL)
1136 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001137 if (n != mp->ma_used) {
1138 /* Durnit. The allocations caused the dict to resize.
1139 * Just start over, this shouldn't normally happen.
1140 */
1141 Py_DECREF(v);
1142 goto again;
1143 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001144 ep = mp->ma_table;
1145 mask = mp->ma_mask;
1146 for (i = 0, j = 0; i <= mask; i++) {
1147 if (ep[i].me_value != NULL) {
1148 PyObject *value = ep[i].me_value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001149 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001150 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001151 j++;
1152 }
1153 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001154 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001155 return v;
1156}
1157
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001158static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001159dict_items(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001160{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001161 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001162 register Py_ssize_t i, j, n;
1163 Py_ssize_t mask;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001164 PyObject *item, *key, *value;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001165 PyDictEntry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001166
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001167 /* Preallocate the list of tuples, to avoid allocations during
1168 * the loop over the items, which could trigger GC, which
1169 * could resize the dict. :-(
1170 */
1171 again:
1172 n = mp->ma_used;
1173 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001174 if (v == NULL)
1175 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001176 for (i = 0; i < n; i++) {
1177 item = PyTuple_New(2);
1178 if (item == NULL) {
1179 Py_DECREF(v);
1180 return NULL;
1181 }
1182 PyList_SET_ITEM(v, i, item);
1183 }
1184 if (n != mp->ma_used) {
1185 /* Durnit. The allocations caused the dict to resize.
1186 * Just start over, this shouldn't normally happen.
1187 */
1188 Py_DECREF(v);
1189 goto again;
1190 }
1191 /* Nothing we do below makes any function calls. */
Raymond Hettinger43442782004-03-17 21:55:03 +00001192 ep = mp->ma_table;
1193 mask = mp->ma_mask;
1194 for (i = 0, j = 0; i <= mask; i++) {
Raymond Hettinger06905122004-03-19 10:30:00 +00001195 if ((value=ep[i].me_value) != NULL) {
Raymond Hettinger43442782004-03-17 21:55:03 +00001196 key = ep[i].me_key;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001197 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001198 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001199 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001200 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001201 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001202 j++;
1203 }
1204 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001205 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001206 return v;
1207}
1208
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001209static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001210dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001211{
1212 PyObject *seq;
1213 PyObject *value = Py_None;
1214 PyObject *it; /* iter(seq) */
1215 PyObject *key;
1216 PyObject *d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001217 int status;
1218
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001219 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001220 return NULL;
1221
1222 d = PyObject_CallObject(cls, NULL);
1223 if (d == NULL)
1224 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001225
Guido van Rossum58da9312007-11-10 23:39:45 +00001226 if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) {
1227 PyDictObject *mp = (PyDictObject *)d;
1228 PyObject *oldvalue;
1229 Py_ssize_t pos = 0;
1230 PyObject *key;
1231 long hash;
1232
Benjamin Peterson41181742008-07-02 20:22:54 +00001233 if (dictresize(mp, Py_SIZE(seq)))
Guido van Rossum58da9312007-11-10 23:39:45 +00001234 return NULL;
1235
1236 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
1237 Py_INCREF(key);
1238 Py_INCREF(value);
1239 if (insertdict(mp, key, hash, value))
1240 return NULL;
1241 }
1242 return d;
1243 }
1244
Guido van Rossumd8faa362007-04-27 19:54:29 +00001245 if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001246 PyDictObject *mp = (PyDictObject *)d;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001247 Py_ssize_t pos = 0;
1248 PyObject *key;
1249 long hash;
1250
1251 if (dictresize(mp, PySet_GET_SIZE(seq)))
1252 return NULL;
1253
1254 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
1255 Py_INCREF(key);
1256 Py_INCREF(value);
1257 if (insertdict(mp, key, hash, value))
1258 return NULL;
1259 }
1260 return d;
1261 }
1262
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001263 it = PyObject_GetIter(seq);
1264 if (it == NULL){
1265 Py_DECREF(d);
1266 return NULL;
1267 }
1268
Guido van Rossum58da9312007-11-10 23:39:45 +00001269 if (PyDict_CheckExact(d)) {
1270 while ((key = PyIter_Next(it)) != NULL) {
1271 status = PyDict_SetItem(d, key, value);
1272 Py_DECREF(key);
1273 if (status < 0)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001274 goto Fail;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001275 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001276 } else {
1277 while ((key = PyIter_Next(it)) != NULL) {
1278 status = PyObject_SetItem(d, key, value);
1279 Py_DECREF(key);
1280 if (status < 0)
1281 goto Fail;
1282 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001283 }
1284
Guido van Rossum58da9312007-11-10 23:39:45 +00001285 if (PyErr_Occurred())
1286 goto Fail;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001287 Py_DECREF(it);
1288 return d;
1289
1290Fail:
1291 Py_DECREF(it);
1292 Py_DECREF(d);
1293 return NULL;
1294}
1295
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001296static int
1297dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001298{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001299 PyObject *arg = NULL;
1300 int result = 0;
1301
1302 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1303 result = -1;
1304
1305 else if (arg != NULL) {
1306 if (PyObject_HasAttrString(arg, "keys"))
1307 result = PyDict_Merge(self, arg, 1);
1308 else
1309 result = PyDict_MergeFromSeq2(self, arg, 1);
1310 }
1311 if (result == 0 && kwds != NULL)
1312 result = PyDict_Merge(self, kwds, 1);
1313 return result;
1314}
1315
1316static PyObject *
1317dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1318{
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001319 if (dict_update_common(self, args, kwds, "update") != -1)
1320 Py_RETURN_NONE;
1321 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001322}
1323
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001324/* Update unconditionally replaces existing items.
1325 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001326 otherwise it leaves existing items unchanged.
1327
1328 PyDict_{Update,Merge} update/merge from a mapping object.
1329
Tim Petersf582b822001-12-11 18:51:08 +00001330 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001331 producing iterable objects of length 2.
1332*/
1333
Tim Petersf582b822001-12-11 18:51:08 +00001334int
Tim Peters1fc240e2001-10-26 05:06:50 +00001335PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1336{
1337 PyObject *it; /* iter(seq2) */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001338 Py_ssize_t i; /* index into seq2 of current element */
Tim Peters1fc240e2001-10-26 05:06:50 +00001339 PyObject *item; /* seq2[i] */
1340 PyObject *fast; /* item as a 2-tuple or 2-list */
1341
1342 assert(d != NULL);
1343 assert(PyDict_Check(d));
1344 assert(seq2 != NULL);
1345
1346 it = PyObject_GetIter(seq2);
1347 if (it == NULL)
1348 return -1;
1349
1350 for (i = 0; ; ++i) {
1351 PyObject *key, *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001352 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001353
1354 fast = NULL;
1355 item = PyIter_Next(it);
1356 if (item == NULL) {
1357 if (PyErr_Occurred())
1358 goto Fail;
1359 break;
1360 }
1361
1362 /* Convert item to sequence, and verify length 2. */
1363 fast = PySequence_Fast(item, "");
1364 if (fast == NULL) {
1365 if (PyErr_ExceptionMatches(PyExc_TypeError))
1366 PyErr_Format(PyExc_TypeError,
1367 "cannot convert dictionary update "
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001368 "sequence element #%zd to a sequence",
Tim Peters1fc240e2001-10-26 05:06:50 +00001369 i);
1370 goto Fail;
1371 }
1372 n = PySequence_Fast_GET_SIZE(fast);
1373 if (n != 2) {
1374 PyErr_Format(PyExc_ValueError,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001375 "dictionary update sequence element #%zd "
Martin v. Löwis2c95cc62006-02-16 06:54:25 +00001376 "has length %zd; 2 is required",
Martin v. Löwise0e89f72006-02-16 06:59:22 +00001377 i, n);
Tim Peters1fc240e2001-10-26 05:06:50 +00001378 goto Fail;
1379 }
1380
1381 /* Update/merge with this (key, value) pair. */
1382 key = PySequence_Fast_GET_ITEM(fast, 0);
1383 value = PySequence_Fast_GET_ITEM(fast, 1);
1384 if (override || PyDict_GetItem(d, key) == NULL) {
1385 int status = PyDict_SetItem(d, key, value);
1386 if (status < 0)
1387 goto Fail;
1388 }
1389 Py_DECREF(fast);
1390 Py_DECREF(item);
1391 }
1392
1393 i = 0;
1394 goto Return;
1395Fail:
1396 Py_XDECREF(item);
1397 Py_XDECREF(fast);
1398 i = -1;
1399Return:
1400 Py_DECREF(it);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001401 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001402}
1403
Tim Peters6d6c1a32001-08-02 04:15:00 +00001404int
1405PyDict_Update(PyObject *a, PyObject *b)
1406{
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001407 return PyDict_Merge(a, b, 1);
1408}
1409
1410int
1411PyDict_Merge(PyObject *a, PyObject *b, int override)
1412{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001413 register PyDictObject *mp, *other;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001414 register Py_ssize_t i;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001415 PyDictEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001416
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001417 /* We accept for the argument either a concrete dictionary object,
1418 * or an abstract "mapping" object. For the former, we can do
1419 * things quite efficiently. For the latter, we only require that
1420 * PyMapping_Keys() and PyObject_GetItem() be supported.
1421 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001422 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1423 PyErr_BadInternalCall();
1424 return -1;
1425 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001426 mp = (PyDictObject*)a;
Christian Heimesaf98da12008-01-27 15:18:18 +00001427 if (PyDict_Check(b)) {
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001428 other = (PyDictObject*)b;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001429 if (other == mp || other->ma_used == 0)
1430 /* a.update(a) or a.update({}); nothing to do */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001431 return 0;
Raymond Hettinger186e7392005-05-14 18:08:25 +00001432 if (mp->ma_used == 0)
1433 /* Since the target dict is empty, PyDict_GetItem()
1434 * always returns NULL. Setting override to 1
1435 * skips the unnecessary test.
1436 */
1437 override = 1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001438 /* Do one big resize at the start, rather than
1439 * incrementally resizing as we insert new items. Expect
1440 * that there will be no (or few) overlapping keys.
1441 */
1442 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
Raymond Hettingerc8d22902003-05-07 00:49:40 +00001443 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001444 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001445 }
1446 for (i = 0; i <= other->ma_mask; i++) {
1447 entry = &other->ma_table[i];
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001448 if (entry->me_value != NULL &&
1449 (override ||
1450 PyDict_GetItem(a, entry->me_key) == NULL)) {
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001451 Py_INCREF(entry->me_key);
1452 Py_INCREF(entry->me_value);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001453 if (insertdict(mp, entry->me_key,
1454 (long)entry->me_hash,
1455 entry->me_value) != 0)
1456 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001457 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001458 }
1459 }
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001460 else {
1461 /* Do it the generic, slower way */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001462 PyObject *keys = PyMapping_Keys(b);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001463 PyObject *iter;
1464 PyObject *key, *value;
1465 int status;
1466
1467 if (keys == NULL)
1468 /* Docstring says this is equivalent to E.keys() so
1469 * if E doesn't have a .keys() method we want
1470 * AttributeError to percolate up. Might as well
1471 * do the same for any other error.
1472 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001473 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001474
1475 iter = PyObject_GetIter(keys);
1476 Py_DECREF(keys);
1477 if (iter == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001478 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001479
1480 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001481 if (!override && PyDict_GetItem(a, key) != NULL) {
1482 Py_DECREF(key);
1483 continue;
1484 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001485 value = PyObject_GetItem(b, key);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001486 if (value == NULL) {
1487 Py_DECREF(iter);
1488 Py_DECREF(key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001489 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001490 }
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001491 status = PyDict_SetItem(a, key, value);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001492 Py_DECREF(key);
1493 Py_DECREF(value);
1494 if (status < 0) {
1495 Py_DECREF(iter);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001496 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001497 }
1498 }
1499 Py_DECREF(iter);
1500 if (PyErr_Occurred())
1501 /* Iterator completed, via error */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001502 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001503 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001504 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001505}
1506
1507static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001508dict_copy(register PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001509{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001510 return PyDict_Copy((PyObject*)mp);
1511}
1512
1513PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001514PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001515{
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001516 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001517
1518 if (o == NULL || !PyDict_Check(o)) {
1519 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001520 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001521 }
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001522 copy = PyDict_New();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001523 if (copy == NULL)
1524 return NULL;
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001525 if (PyDict_Merge(copy, o, 1) == 0)
1526 return copy;
1527 Py_DECREF(copy);
Raymond Hettinger1356f782005-04-15 15:58:42 +00001528 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001529}
1530
Martin v. Löwis18e16552006-02-15 17:27:45 +00001531Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001532PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001533{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001534 if (mp == NULL || !PyDict_Check(mp)) {
1535 PyErr_BadInternalCall();
Jeremy Hylton7083bb72004-02-17 20:10:11 +00001536 return -1;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001537 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001538 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001539}
1540
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001541PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001542PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001543{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001544 if (mp == NULL || !PyDict_Check(mp)) {
1545 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001546 return NULL;
1547 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001548 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001549}
1550
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001551PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001552PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001553{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001554 if (mp == NULL || !PyDict_Check(mp)) {
1555 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001556 return NULL;
1557 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001558 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001559}
1560
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001561PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001562PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001563{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001564 if (mp == NULL || !PyDict_Check(mp)) {
1565 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001566 return NULL;
1567 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001568 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001569}
1570
Tim Peterse63415e2001-05-08 04:38:29 +00001571/* Return 1 if dicts equal, 0 if not, -1 if error.
1572 * Gets out as soon as any difference is detected.
1573 * Uses only Py_EQ comparison.
1574 */
1575static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001576dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00001577{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001578 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00001579
1580 if (a->ma_used != b->ma_used)
1581 /* can't be equal if # of entries differ */
1582 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001583
Tim Peterse63415e2001-05-08 04:38:29 +00001584 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Tim Petersafb6ae82001-06-04 21:00:21 +00001585 for (i = 0; i <= a->ma_mask; i++) {
Tim Peterse63415e2001-05-08 04:38:29 +00001586 PyObject *aval = a->ma_table[i].me_value;
1587 if (aval != NULL) {
1588 int cmp;
1589 PyObject *bval;
1590 PyObject *key = a->ma_table[i].me_key;
1591 /* temporarily bump aval's refcount to ensure it stays
1592 alive until we're done with it */
1593 Py_INCREF(aval);
Guido van Rossumdc5f6b22006-08-24 21:29:26 +00001594 /* ditto for key */
1595 Py_INCREF(key);
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001596 bval = PyDict_GetItemWithError((PyObject *)b, key);
Guido van Rossumdc5f6b22006-08-24 21:29:26 +00001597 Py_DECREF(key);
Tim Peterse63415e2001-05-08 04:38:29 +00001598 if (bval == NULL) {
1599 Py_DECREF(aval);
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001600 if (PyErr_Occurred())
1601 return -1;
Tim Peterse63415e2001-05-08 04:38:29 +00001602 return 0;
1603 }
1604 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1605 Py_DECREF(aval);
1606 if (cmp <= 0) /* error or not equal */
1607 return cmp;
1608 }
1609 }
1610 return 1;
1611 }
1612
1613static PyObject *
1614dict_richcompare(PyObject *v, PyObject *w, int op)
1615{
1616 int cmp;
1617 PyObject *res;
1618
1619 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1620 res = Py_NotImplemented;
1621 }
1622 else if (op == Py_EQ || op == Py_NE) {
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001623 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
Tim Peterse63415e2001-05-08 04:38:29 +00001624 if (cmp < 0)
1625 return NULL;
1626 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1627 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001628 else
1629 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001630 Py_INCREF(res);
1631 return res;
1632 }
1633
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001634static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001635dict_contains(register PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001636{
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001637 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001638 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001639
Guido van Rossum89d8c602007-09-18 17:26:56 +00001640 if (!PyUnicode_CheckExact(key) ||
1641 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001642 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001643 if (hash == -1)
1644 return NULL;
1645 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001646 ep = (mp->ma_lookup)(mp, key, hash);
1647 if (ep == NULL)
1648 return NULL;
1649 return PyBool_FromLong(ep->me_value != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001650}
1651
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001652static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001653dict_get(register PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001654{
1655 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001656 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001657 PyObject *val = NULL;
1658 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001659 PyDictEntry *ep;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001660
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001661 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001662 return NULL;
1663
Guido van Rossum89d8c602007-09-18 17:26:56 +00001664 if (!PyUnicode_CheckExact(key) ||
1665 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Barry Warsawc38c5da1997-10-06 17:49:20 +00001666 hash = PyObject_Hash(key);
1667 if (hash == -1)
1668 return NULL;
1669 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001670 ep = (mp->ma_lookup)(mp, key, hash);
1671 if (ep == NULL)
1672 return NULL;
1673 val = ep->me_value;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001674 if (val == NULL)
1675 val = failobj;
1676 Py_INCREF(val);
1677 return val;
1678}
1679
1680
1681static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001682dict_setdefault(register PyDictObject *mp, PyObject *args)
Guido van Rossum164452c2000-08-08 16:12:54 +00001683{
1684 PyObject *key;
1685 PyObject *failobj = Py_None;
1686 PyObject *val = NULL;
1687 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001688 PyDictEntry *ep;
Guido van Rossum164452c2000-08-08 16:12:54 +00001689
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001690 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
Guido van Rossum164452c2000-08-08 16:12:54 +00001691 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001692
Guido van Rossum89d8c602007-09-18 17:26:56 +00001693 if (!PyUnicode_CheckExact(key) ||
1694 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossum164452c2000-08-08 16:12:54 +00001695 hash = PyObject_Hash(key);
1696 if (hash == -1)
1697 return NULL;
1698 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001699 ep = (mp->ma_lookup)(mp, key, hash);
1700 if (ep == NULL)
1701 return NULL;
1702 val = ep->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001703 if (val == NULL) {
1704 val = failobj;
1705 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1706 val = NULL;
1707 }
1708 Py_XINCREF(val);
1709 return val;
1710}
1711
1712
1713static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001714dict_clear(register PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001715{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001716 PyDict_Clear((PyObject *)mp);
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001717 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001718}
1719
Guido van Rossumba6ab842000-12-12 22:02:18 +00001720static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001721dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00001722{
1723 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001724 PyDictEntry *ep;
Guido van Rossume027d982002-04-12 15:11:59 +00001725 PyObject *old_value, *old_key;
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001726 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001727
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001728 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1729 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001730 if (mp->ma_used == 0) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001731 if (deflt) {
1732 Py_INCREF(deflt);
1733 return deflt;
1734 }
Guido van Rossume027d982002-04-12 15:11:59 +00001735 PyErr_SetString(PyExc_KeyError,
1736 "pop(): dictionary is empty");
1737 return NULL;
1738 }
Guido van Rossum89d8c602007-09-18 17:26:56 +00001739 if (!PyUnicode_CheckExact(key) ||
1740 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossume027d982002-04-12 15:11:59 +00001741 hash = PyObject_Hash(key);
1742 if (hash == -1)
1743 return NULL;
1744 }
1745 ep = (mp->ma_lookup)(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001746 if (ep == NULL)
1747 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001748 if (ep->me_value == NULL) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001749 if (deflt) {
1750 Py_INCREF(deflt);
1751 return deflt;
1752 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001753 set_key_error(key);
Guido van Rossume027d982002-04-12 15:11:59 +00001754 return NULL;
1755 }
1756 old_key = ep->me_key;
1757 Py_INCREF(dummy);
1758 ep->me_key = dummy;
1759 old_value = ep->me_value;
1760 ep->me_value = NULL;
1761 mp->ma_used--;
1762 Py_DECREF(old_key);
1763 return old_value;
1764}
1765
1766static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001767dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001768{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001769 Py_ssize_t i = 0;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001770 PyDictEntry *ep;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001771 PyObject *res;
1772
Tim Petersf4b33f62001-06-02 05:42:29 +00001773 /* Allocate the result tuple before checking the size. Believe it
1774 * or not, this allocation could trigger a garbage collection which
1775 * could empty the dict, so if we checked the size first and that
1776 * happened, the result would be an infinite loop (searching for an
1777 * entry that no longer exists). Note that the usual popitem()
1778 * idiom is "while d: k, v = d.popitem()". so needing to throw the
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001779 * tuple away if the dict *is* empty isn't a significant
Tim Petersf4b33f62001-06-02 05:42:29 +00001780 * inefficiency -- possible, but unlikely in practice.
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001781 */
1782 res = PyTuple_New(2);
1783 if (res == NULL)
1784 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001785 if (mp->ma_used == 0) {
1786 Py_DECREF(res);
1787 PyErr_SetString(PyExc_KeyError,
1788 "popitem(): dictionary is empty");
1789 return NULL;
1790 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001791 /* Set ep to "the first" dict entry with a value. We abuse the hash
1792 * field of slot 0 to hold a search finger:
1793 * If slot 0 has a value, use slot 0.
1794 * Else slot 0 is being used to hold a search finger,
1795 * and we use its hash value as the first index to look.
1796 */
1797 ep = &mp->ma_table[0];
1798 if (ep->me_value == NULL) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001799 i = ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001800 /* The hash field may be a real hash value, or it may be a
1801 * legit search finger, or it may be a once-legit search
1802 * finger that's out of bounds now because it wrapped around
1803 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001804 */
Tim Petersafb6ae82001-06-04 21:00:21 +00001805 if (i > mp->ma_mask || i < 1)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001806 i = 1; /* skip slot 0 */
1807 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1808 i++;
Tim Petersafb6ae82001-06-04 21:00:21 +00001809 if (i > mp->ma_mask)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001810 i = 1;
1811 }
1812 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001813 PyTuple_SET_ITEM(res, 0, ep->me_key);
1814 PyTuple_SET_ITEM(res, 1, ep->me_value);
1815 Py_INCREF(dummy);
1816 ep->me_key = dummy;
1817 ep->me_value = NULL;
1818 mp->ma_used--;
1819 assert(mp->ma_table[0].me_value == NULL);
1820 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001821 return res;
1822}
1823
Jeremy Hylton8caad492000-06-23 14:18:11 +00001824static int
1825dict_traverse(PyObject *op, visitproc visit, void *arg)
1826{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001827 Py_ssize_t i = 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001828 PyObject *pk;
1829 PyObject *pv;
1830
1831 while (PyDict_Next(op, &i, &pk, &pv)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001832 Py_VISIT(pk);
1833 Py_VISIT(pv);
Jeremy Hylton8caad492000-06-23 14:18:11 +00001834 }
1835 return 0;
1836}
1837
1838static int
1839dict_tp_clear(PyObject *op)
1840{
1841 PyDict_Clear(op);
1842 return 0;
1843}
1844
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001845static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001846
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001847static PyObject *
1848dict_sizeof(PyDictObject *mp)
1849{
1850 Py_ssize_t res;
1851
Robert Schuppenies4d45bfe2008-06-28 23:58:47 +00001852 res = sizeof(PyDictObject);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001853 if (mp->ma_table != mp->ma_smalltable)
1854 res = res + (mp->ma_mask + 1) * sizeof(PyDictEntry);
1855 return PyLong_FromSsize_t(res);
1856}
1857
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001858PyDoc_STRVAR(contains__doc__,
1859"D.__contains__(k) -> True if D has a key k, else False");
1860
1861PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
1862
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001863PyDoc_STRVAR(sizeof__doc__,
1864"D.__sizeof__() -> size of D in memory, in bytes");
1865
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001866PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001867"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001868
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001869PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001870"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 +00001871
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001872PyDoc_STRVAR(pop__doc__,
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001873"D.pop(k[,d]) -> v, remove specified key and return the corresponding value\n\
1874If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00001875
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001876PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00001877"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000018782-tuple; but raise KeyError if D is empty");
Tim Petersf7f88b12000-12-13 23:18:45 +00001879
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001880PyDoc_STRVAR(update__doc__,
Guido van Rossumb90c8482007-02-10 01:11:45 +00001881"D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k]\
1882\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 +00001883
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001884PyDoc_STRVAR(fromkeys__doc__,
1885"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
1886v defaults to None.");
1887
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001888PyDoc_STRVAR(clear__doc__,
1889"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001890
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001891PyDoc_STRVAR(copy__doc__,
1892"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00001893
Guido van Rossumb90c8482007-02-10 01:11:45 +00001894/* Forward */
1895static PyObject *dictkeys_new(PyObject *);
1896static PyObject *dictitems_new(PyObject *);
1897static PyObject *dictvalues_new(PyObject *);
1898
Guido van Rossum45c85d12007-07-27 16:31:40 +00001899PyDoc_STRVAR(keys__doc__,
1900 "D.keys() -> a set-like object providing a view on D's keys");
1901PyDoc_STRVAR(items__doc__,
1902 "D.items() -> a set-like object providing a view on D's items");
1903PyDoc_STRVAR(values__doc__,
1904 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00001905
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001906static PyMethodDef mapp_methods[] = {
Guido van Rossume2b70bc2006-08-18 22:13:04 +00001907 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001908 contains__doc__},
Raymond Hettinger0c669672003-12-13 13:31:55 +00001909 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001910 getitem__doc__},
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001911 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
1912 sizeof__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00001913 {"get", (PyCFunction)dict_get, METH_VARARGS,
1914 get__doc__},
1915 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1916 setdefault_doc__},
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001917 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
Just van Rossuma797d812002-11-23 09:45:04 +00001918 pop__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001919 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001920 popitem__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001921 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00001922 keys__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001923 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00001924 items__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001925 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00001926 values__doc__},
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001927 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001928 update__doc__},
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001929 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
1930 fromkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001931 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001932 clear__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001933 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001934 copy__doc__},
1935 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001936};
1937
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001938/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001939int
1940PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001941{
1942 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001943 PyDictObject *mp = (PyDictObject *)op;
1944 PyDictEntry *ep;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001945
Guido van Rossum89d8c602007-09-18 17:26:56 +00001946 if (!PyUnicode_CheckExact(key) ||
1947 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001948 hash = PyObject_Hash(key);
1949 if (hash == -1)
1950 return -1;
1951 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001952 ep = (mp->ma_lookup)(mp, key, hash);
1953 return ep == NULL ? -1 : (ep->me_value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001954}
1955
Thomas Wouterscf297e42007-02-23 15:07:44 +00001956/* Internal version of PyDict_Contains used when the hash value is already known */
1957int
1958_PyDict_Contains(PyObject *op, PyObject *key, long hash)
1959{
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001960 PyDictObject *mp = (PyDictObject *)op;
1961 PyDictEntry *ep;
Thomas Wouterscf297e42007-02-23 15:07:44 +00001962
1963 ep = (mp->ma_lookup)(mp, key, hash);
1964 return ep == NULL ? -1 : (ep->me_value != NULL);
1965}
1966
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001967/* Hack to implement "key in dict" */
1968static PySequenceMethods dict_as_sequence = {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001969 0, /* sq_length */
1970 0, /* sq_concat */
1971 0, /* sq_repeat */
1972 0, /* sq_item */
1973 0, /* sq_slice */
1974 0, /* sq_ass_item */
1975 0, /* sq_ass_slice */
1976 PyDict_Contains, /* sq_contains */
1977 0, /* sq_inplace_concat */
1978 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00001979};
1980
Guido van Rossum09e563a2001-05-01 12:10:21 +00001981static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001982dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1983{
1984 PyObject *self;
1985
1986 assert(type != NULL && type->tp_alloc != NULL);
1987 self = type->tp_alloc(type, 0);
1988 if (self != NULL) {
1989 PyDictObject *d = (PyDictObject *)self;
1990 /* It's guaranteed that tp->alloc zeroed out the struct. */
1991 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
1992 INIT_NONZERO_DICT_SLOTS(d);
Guido van Rossum89d8c602007-09-18 17:26:56 +00001993 d->ma_lookup = lookdict_unicode;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001994#ifdef SHOW_CONVERSION_COUNTS
1995 ++created;
1996#endif
1997 }
1998 return self;
1999}
2000
Tim Peters25786c02001-09-02 08:22:48 +00002001static int
2002dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2003{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002004 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002005}
2006
Tim Peters6d6c1a32001-08-02 04:15:00 +00002007static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002008dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002009{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002010 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002011}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002012
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002013PyDoc_STRVAR(dictionary_doc,
Tim Petersa427a2b2001-10-29 22:25:45 +00002014"dict() -> new empty dictionary.\n"
2015"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Tim Peters1fc240e2001-10-26 05:06:50 +00002016" (key, value) pairs.\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002017"dict(seq) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002018" d = {}\n"
2019" for k, v in seq:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002020" d[k] = v\n"
2021"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2022" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002023
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002024PyTypeObject PyDict_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002025 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Petersa427a2b2001-10-29 22:25:45 +00002026 "dict",
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002027 sizeof(PyDictObject),
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002028 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00002029 (destructor)dict_dealloc, /* tp_dealloc */
Guido van Rossum04dbf3b2007-08-07 19:51:00 +00002030 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002031 0, /* tp_getattr */
Guido van Rossumb9324202001-01-18 00:39:02 +00002032 0, /* tp_setattr */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00002033 0, /* tp_compare */
Guido van Rossumb9324202001-01-18 00:39:02 +00002034 (reprfunc)dict_repr, /* tp_repr */
2035 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002036 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00002037 &dict_as_mapping, /* tp_as_mapping */
Nick Coghland1abd252008-07-15 15:46:38 +00002038 (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
Guido van Rossumb9324202001-01-18 00:39:02 +00002039 0, /* tp_call */
2040 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002041 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumb9324202001-01-18 00:39:02 +00002042 0, /* tp_setattro */
2043 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002044 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Thomas Wouters27d517b2007-02-25 20:39:11 +00002045 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
Tim Peters25786c02001-09-02 08:22:48 +00002046 dictionary_doc, /* tp_doc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002047 dict_traverse, /* tp_traverse */
2048 dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00002049 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002050 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00002051 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002052 0, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002053 mapp_methods, /* tp_methods */
2054 0, /* tp_members */
2055 0, /* tp_getset */
2056 0, /* tp_base */
2057 0, /* tp_dict */
2058 0, /* tp_descr_get */
2059 0, /* tp_descr_set */
2060 0, /* tp_dictoffset */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002061 dict_init, /* tp_init */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002062 PyType_GenericAlloc, /* tp_alloc */
2063 dict_new, /* tp_new */
Neil Schemenauer6189b892002-04-12 02:43:00 +00002064 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002065};
2066
Guido van Rossum3cca2451997-05-16 14:23:33 +00002067/* For backward compatibility with old dictionary interface */
2068
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002069PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002070PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002071{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002072 PyObject *kv, *rv;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002073 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002074 if (kv == NULL)
2075 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002076 rv = PyDict_GetItem(v, kv);
2077 Py_DECREF(kv);
2078 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002079}
2080
2081int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002082PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002083{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002084 PyObject *kv;
2085 int err;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002086 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002087 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002088 return -1;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002089 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00002090 err = PyDict_SetItem(v, kv, item);
2091 Py_DECREF(kv);
2092 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002093}
2094
2095int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002096PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002097{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002098 PyObject *kv;
2099 int err;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002100 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002101 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002102 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002103 err = PyDict_DelItem(v, kv);
2104 Py_DECREF(kv);
2105 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002106}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002107
Raymond Hettinger019a1482004-03-18 02:41:19 +00002108/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002109
2110typedef struct {
2111 PyObject_HEAD
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002112 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002113 Py_ssize_t di_used;
2114 Py_ssize_t di_pos;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002115 PyObject* di_result; /* reusable result tuple for iteritems */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002116 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002117} dictiterobject;
2118
2119static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002120dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002121{
2122 dictiterobject *di;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002123 di = PyObject_New(dictiterobject, itertype);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002124 if (di == NULL)
2125 return NULL;
2126 Py_INCREF(dict);
2127 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00002128 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002129 di->di_pos = 0;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002130 di->len = dict->ma_used;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002131 if (itertype == &PyDictIterItem_Type) {
2132 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2133 if (di->di_result == NULL) {
2134 Py_DECREF(di);
2135 return NULL;
2136 }
2137 }
2138 else
2139 di->di_result = NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002140 return (PyObject *)di;
2141}
2142
2143static void
2144dictiter_dealloc(dictiterobject *di)
2145{
Guido van Rossum2147df72002-07-16 20:30:22 +00002146 Py_XDECREF(di->di_dict);
Raymond Hettinger019a1482004-03-18 02:41:19 +00002147 Py_XDECREF(di->di_result);
Neil Schemenauer6189b892002-04-12 02:43:00 +00002148 PyObject_Del(di);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002149}
2150
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002151static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002152dictiter_len(dictiterobject *di)
2153{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002154 Py_ssize_t len = 0;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00002155 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002156 len = di->len;
Christian Heimes217cfd12007-12-02 14:31:20 +00002157 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002158}
2159
Guido van Rossumb90c8482007-02-10 01:11:45 +00002160PyDoc_STRVAR(length_hint_doc,
2161 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002162
2163static PyMethodDef dictiter_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002164 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
2165 length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002166 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002167};
2168
Raymond Hettinger019a1482004-03-18 02:41:19 +00002169static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002170{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002171 PyObject *key;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002172 register Py_ssize_t i, mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002173 register PyDictEntry *ep;
2174 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002175
Raymond Hettinger019a1482004-03-18 02:41:19 +00002176 if (d == NULL)
Guido van Rossum2147df72002-07-16 20:30:22 +00002177 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002178 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002179
Raymond Hettinger019a1482004-03-18 02:41:19 +00002180 if (di->di_used != d->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00002181 PyErr_SetString(PyExc_RuntimeError,
2182 "dictionary changed size during iteration");
Guido van Rossum2147df72002-07-16 20:30:22 +00002183 di->di_used = -1; /* Make this state sticky */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002184 return NULL;
2185 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002186
Raymond Hettinger019a1482004-03-18 02:41:19 +00002187 i = di->di_pos;
2188 if (i < 0)
2189 goto fail;
2190 ep = d->ma_table;
2191 mask = d->ma_mask;
2192 while (i <= mask && ep[i].me_value == NULL)
2193 i++;
2194 di->di_pos = i+1;
2195 if (i > mask)
2196 goto fail;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002197 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002198 key = ep[i].me_key;
2199 Py_INCREF(key);
2200 return key;
2201
2202fail:
2203 Py_DECREF(d);
Guido van Rossum2147df72002-07-16 20:30:22 +00002204 di->di_dict = NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002205 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002206}
2207
Raymond Hettinger019a1482004-03-18 02:41:19 +00002208PyTypeObject PyDictIterKey_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002209 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Christian Heimesf83be4e2007-11-28 09:44:38 +00002210 "dict_keyiterator", /* tp_name */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002211 sizeof(dictiterobject), /* tp_basicsize */
2212 0, /* tp_itemsize */
2213 /* methods */
2214 (destructor)dictiter_dealloc, /* tp_dealloc */
2215 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002216 0, /* tp_getattr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002217 0, /* tp_setattr */
2218 0, /* tp_compare */
2219 0, /* tp_repr */
2220 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002221 0, /* tp_as_sequence */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002222 0, /* tp_as_mapping */
2223 0, /* tp_hash */
2224 0, /* tp_call */
2225 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002226 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002227 0, /* tp_setattro */
2228 0, /* tp_as_buffer */
2229 Py_TPFLAGS_DEFAULT, /* tp_flags */
2230 0, /* tp_doc */
2231 0, /* tp_traverse */
2232 0, /* tp_clear */
2233 0, /* tp_richcompare */
2234 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002235 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002236 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002237 dictiter_methods, /* tp_methods */
2238 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002239};
2240
2241static PyObject *dictiter_iternextvalue(dictiterobject *di)
2242{
2243 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002244 register Py_ssize_t i, mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002245 register PyDictEntry *ep;
2246 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002247
2248 if (d == NULL)
2249 return NULL;
2250 assert (PyDict_Check(d));
2251
2252 if (di->di_used != d->ma_used) {
2253 PyErr_SetString(PyExc_RuntimeError,
2254 "dictionary changed size during iteration");
2255 di->di_used = -1; /* Make this state sticky */
2256 return NULL;
2257 }
2258
2259 i = di->di_pos;
Guido van Rossum09240f62004-03-20 19:11:58 +00002260 mask = d->ma_mask;
2261 if (i < 0 || i > mask)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002262 goto fail;
2263 ep = d->ma_table;
Guido van Rossum09240f62004-03-20 19:11:58 +00002264 while ((value=ep[i].me_value) == NULL) {
Raymond Hettinger019a1482004-03-18 02:41:19 +00002265 i++;
Guido van Rossum09240f62004-03-20 19:11:58 +00002266 if (i > mask)
2267 goto fail;
2268 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002269 di->di_pos = i+1;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002270 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002271 Py_INCREF(value);
2272 return value;
2273
2274fail:
2275 Py_DECREF(d);
2276 di->di_dict = NULL;
2277 return NULL;
2278}
2279
2280PyTypeObject PyDictIterValue_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002281 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Christian Heimesf83be4e2007-11-28 09:44:38 +00002282 "dict_valueiterator", /* tp_name */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002283 sizeof(dictiterobject), /* tp_basicsize */
2284 0, /* tp_itemsize */
2285 /* methods */
2286 (destructor)dictiter_dealloc, /* tp_dealloc */
2287 0, /* tp_print */
2288 0, /* tp_getattr */
2289 0, /* tp_setattr */
2290 0, /* tp_compare */
2291 0, /* tp_repr */
2292 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002293 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002294 0, /* tp_as_mapping */
2295 0, /* tp_hash */
2296 0, /* tp_call */
2297 0, /* tp_str */
2298 PyObject_GenericGetAttr, /* tp_getattro */
2299 0, /* tp_setattro */
2300 0, /* tp_as_buffer */
2301 Py_TPFLAGS_DEFAULT, /* tp_flags */
2302 0, /* tp_doc */
2303 0, /* tp_traverse */
2304 0, /* tp_clear */
2305 0, /* tp_richcompare */
2306 0, /* tp_weaklistoffset */
2307 PyObject_SelfIter, /* tp_iter */
2308 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002309 dictiter_methods, /* tp_methods */
2310 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002311};
2312
2313static PyObject *dictiter_iternextitem(dictiterobject *di)
2314{
2315 PyObject *key, *value, *result = di->di_result;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002316 register Py_ssize_t i, mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002317 register PyDictEntry *ep;
2318 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002319
2320 if (d == NULL)
2321 return NULL;
2322 assert (PyDict_Check(d));
2323
2324 if (di->di_used != d->ma_used) {
2325 PyErr_SetString(PyExc_RuntimeError,
2326 "dictionary changed size during iteration");
2327 di->di_used = -1; /* Make this state sticky */
2328 return NULL;
2329 }
2330
2331 i = di->di_pos;
2332 if (i < 0)
2333 goto fail;
2334 ep = d->ma_table;
2335 mask = d->ma_mask;
2336 while (i <= mask && ep[i].me_value == NULL)
2337 i++;
2338 di->di_pos = i+1;
2339 if (i > mask)
2340 goto fail;
2341
2342 if (result->ob_refcnt == 1) {
2343 Py_INCREF(result);
2344 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2345 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2346 } else {
2347 result = PyTuple_New(2);
Tim Peters60b29962006-01-01 01:19:23 +00002348 if (result == NULL)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002349 return NULL;
2350 }
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002351 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002352 key = ep[i].me_key;
2353 value = ep[i].me_value;
2354 Py_INCREF(key);
2355 Py_INCREF(value);
2356 PyTuple_SET_ITEM(result, 0, key);
2357 PyTuple_SET_ITEM(result, 1, value);
2358 return result;
2359
2360fail:
2361 Py_DECREF(d);
2362 di->di_dict = NULL;
2363 return NULL;
2364}
2365
2366PyTypeObject PyDictIterItem_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002367 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Christian Heimesf83be4e2007-11-28 09:44:38 +00002368 "dict_itemiterator", /* tp_name */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002369 sizeof(dictiterobject), /* tp_basicsize */
2370 0, /* tp_itemsize */
2371 /* methods */
2372 (destructor)dictiter_dealloc, /* tp_dealloc */
2373 0, /* tp_print */
2374 0, /* tp_getattr */
2375 0, /* tp_setattr */
2376 0, /* tp_compare */
2377 0, /* tp_repr */
2378 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002379 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002380 0, /* tp_as_mapping */
2381 0, /* tp_hash */
2382 0, /* tp_call */
2383 0, /* tp_str */
2384 PyObject_GenericGetAttr, /* tp_getattro */
2385 0, /* tp_setattro */
2386 0, /* tp_as_buffer */
2387 Py_TPFLAGS_DEFAULT, /* tp_flags */
2388 0, /* tp_doc */
2389 0, /* tp_traverse */
2390 0, /* tp_clear */
2391 0, /* tp_richcompare */
2392 0, /* tp_weaklistoffset */
2393 PyObject_SelfIter, /* tp_iter */
2394 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002395 dictiter_methods, /* tp_methods */
2396 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002397};
Guido van Rossumb90c8482007-02-10 01:11:45 +00002398
2399
Guido van Rossum3ac67412007-02-10 18:55:06 +00002400/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002401/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002402/***********************************************/
2403
Guido van Rossumb90c8482007-02-10 01:11:45 +00002404/* The instance lay-out is the same for all three; but the type differs. */
2405
2406typedef struct {
2407 PyObject_HEAD
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002408 PyDictObject *dv_dict;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002409} dictviewobject;
2410
2411
2412static void
Guido van Rossum3ac67412007-02-10 18:55:06 +00002413dictview_dealloc(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002414{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002415 Py_XDECREF(dv->dv_dict);
2416 PyObject_Del(dv);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002417}
2418
Guido van Rossum83825ac2007-02-10 04:54:19 +00002419static Py_ssize_t
Guido van Rossum3ac67412007-02-10 18:55:06 +00002420dictview_len(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002421{
2422 Py_ssize_t len = 0;
Guido van Rossum3ac67412007-02-10 18:55:06 +00002423 if (dv->dv_dict != NULL)
2424 len = dv->dv_dict->ma_used;
Guido van Rossum83825ac2007-02-10 04:54:19 +00002425 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002426}
2427
2428static PyObject *
2429dictview_new(PyObject *dict, PyTypeObject *type)
2430{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002431 dictviewobject *dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002432 if (dict == NULL) {
2433 PyErr_BadInternalCall();
2434 return NULL;
2435 }
2436 if (!PyDict_Check(dict)) {
2437 /* XXX Get rid of this restriction later */
2438 PyErr_Format(PyExc_TypeError,
2439 "%s() requires a dict argument, not '%s'",
2440 type->tp_name, dict->ob_type->tp_name);
2441 return NULL;
2442 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002443 dv = PyObject_New(dictviewobject, type);
2444 if (dv == NULL)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002445 return NULL;
2446 Py_INCREF(dict);
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002447 dv->dv_dict = (PyDictObject *)dict;
Guido van Rossum3ac67412007-02-10 18:55:06 +00002448 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002449}
2450
Neal Norwitze36f2ba2007-02-26 23:12:28 +00002451/* TODO(guido): The views objects are not complete:
2452
2453 * support more set operations
2454 * support arbitrary mappings?
2455 - either these should be static or exported in dictobject.h
2456 - if public then they should probably be in builtins
2457*/
2458
Guido van Rossumaac530c2007-08-24 22:33:45 +00002459/* Return 1 if self is a subset of other, iterating over self;
2460 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002461static int
2462all_contained_in(PyObject *self, PyObject *other)
2463{
2464 PyObject *iter = PyObject_GetIter(self);
2465 int ok = 1;
2466
2467 if (iter == NULL)
2468 return -1;
2469 for (;;) {
2470 PyObject *next = PyIter_Next(iter);
2471 if (next == NULL) {
2472 if (PyErr_Occurred())
2473 ok = -1;
2474 break;
2475 }
2476 ok = PySequence_Contains(other, next);
2477 Py_DECREF(next);
2478 if (ok <= 0)
2479 break;
2480 }
2481 Py_DECREF(iter);
2482 return ok;
2483}
2484
2485static PyObject *
2486dictview_richcompare(PyObject *self, PyObject *other, int op)
2487{
Guido van Rossumaac530c2007-08-24 22:33:45 +00002488 Py_ssize_t len_self, len_other;
2489 int ok;
2490 PyObject *result;
2491
Guido van Rossumd9214d12007-02-12 02:23:40 +00002492 assert(self != NULL);
2493 assert(PyDictViewSet_Check(self));
2494 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00002495
Guido van Rossumaac530c2007-08-24 22:33:45 +00002496 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other)) {
Guido van Rossumd9214d12007-02-12 02:23:40 +00002497 Py_INCREF(Py_NotImplemented);
2498 return Py_NotImplemented;
2499 }
Guido van Rossumaac530c2007-08-24 22:33:45 +00002500
2501 len_self = PyObject_Size(self);
2502 if (len_self < 0)
2503 return NULL;
2504 len_other = PyObject_Size(other);
2505 if (len_other < 0)
2506 return NULL;
2507
2508 ok = 0;
2509 switch(op) {
2510
2511 case Py_NE:
2512 case Py_EQ:
2513 if (len_self == len_other)
2514 ok = all_contained_in(self, other);
2515 if (op == Py_NE && ok >= 0)
2516 ok = !ok;
2517 break;
2518
2519 case Py_LT:
2520 if (len_self < len_other)
2521 ok = all_contained_in(self, other);
2522 break;
2523
2524 case Py_LE:
2525 if (len_self <= len_other)
2526 ok = all_contained_in(self, other);
2527 break;
2528
2529 case Py_GT:
2530 if (len_self > len_other)
2531 ok = all_contained_in(other, self);
2532 break;
2533
2534 case Py_GE:
2535 if (len_self >= len_other)
2536 ok = all_contained_in(other, self);
2537 break;
2538
2539 }
2540 if (ok < 0)
2541 return NULL;
2542 result = ok ? Py_True : Py_False;
2543 Py_INCREF(result);
2544 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00002545}
2546
Guido van Rossum3ac67412007-02-10 18:55:06 +00002547/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002548
2549static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002550dictkeys_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002551{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002552 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002553 Py_RETURN_NONE;
2554 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002555 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
2556}
2557
2558static int
2559dictkeys_contains(dictviewobject *dv, PyObject *obj)
2560{
2561 if (dv->dv_dict == NULL)
2562 return 0;
2563 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002564}
2565
Guido van Rossum83825ac2007-02-10 04:54:19 +00002566static PySequenceMethods dictkeys_as_sequence = {
2567 (lenfunc)dictview_len, /* sq_length */
2568 0, /* sq_concat */
2569 0, /* sq_repeat */
2570 0, /* sq_item */
2571 0, /* sq_slice */
2572 0, /* sq_ass_item */
2573 0, /* sq_ass_slice */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002574 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002575};
2576
Guido van Rossum523259b2007-08-24 23:41:22 +00002577static PyObject*
2578dictviews_sub(PyObject* self, PyObject *other)
2579{
2580 PyObject *result = PySet_New(self);
2581 PyObject *tmp;
2582 if (result == NULL)
2583 return NULL;
2584
2585 tmp = PyObject_CallMethod(result, "difference_update", "O", other);
2586 if (tmp == NULL) {
2587 Py_DECREF(result);
2588 return NULL;
2589 }
2590
2591 Py_DECREF(tmp);
2592 return result;
2593}
2594
2595static PyObject*
2596dictviews_and(PyObject* self, PyObject *other)
2597{
2598 PyObject *result = PySet_New(self);
2599 PyObject *tmp;
2600 if (result == NULL)
2601 return NULL;
2602
2603 tmp = PyObject_CallMethod(result, "intersection_update", "O", other);
2604 if (tmp == NULL) {
2605 Py_DECREF(result);
2606 return NULL;
2607 }
2608
2609 Py_DECREF(tmp);
2610 return result;
2611}
2612
2613static PyObject*
2614dictviews_or(PyObject* self, PyObject *other)
2615{
2616 PyObject *result = PySet_New(self);
2617 PyObject *tmp;
2618 if (result == NULL)
2619 return NULL;
2620
2621 tmp = PyObject_CallMethod(result, "update", "O", other);
2622 if (tmp == NULL) {
2623 Py_DECREF(result);
2624 return NULL;
2625 }
2626
2627 Py_DECREF(tmp);
2628 return result;
2629}
2630
2631static PyObject*
2632dictviews_xor(PyObject* self, PyObject *other)
2633{
2634 PyObject *result = PySet_New(self);
2635 PyObject *tmp;
2636 if (result == NULL)
2637 return NULL;
2638
2639 tmp = PyObject_CallMethod(result, "symmetric_difference_update", "O",
2640 other);
2641 if (tmp == NULL) {
2642 Py_DECREF(result);
2643 return NULL;
2644 }
2645
2646 Py_DECREF(tmp);
2647 return result;
2648}
2649
2650static PyNumberMethods dictviews_as_number = {
2651 0, /*nb_add*/
2652 (binaryfunc)dictviews_sub, /*nb_subtract*/
2653 0, /*nb_multiply*/
2654 0, /*nb_remainder*/
2655 0, /*nb_divmod*/
2656 0, /*nb_power*/
2657 0, /*nb_negative*/
2658 0, /*nb_positive*/
2659 0, /*nb_absolute*/
2660 0, /*nb_bool*/
2661 0, /*nb_invert*/
2662 0, /*nb_lshift*/
2663 0, /*nb_rshift*/
2664 (binaryfunc)dictviews_and, /*nb_and*/
2665 (binaryfunc)dictviews_xor, /*nb_xor*/
2666 (binaryfunc)dictviews_or, /*nb_or*/
2667};
2668
Guido van Rossumb90c8482007-02-10 01:11:45 +00002669static PyMethodDef dictkeys_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002670 {NULL, NULL} /* sentinel */
2671};
2672
2673PyTypeObject PyDictKeys_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002674 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002675 "dict_keys", /* tp_name */
2676 sizeof(dictviewobject), /* tp_basicsize */
2677 0, /* tp_itemsize */
2678 /* methods */
2679 (destructor)dictview_dealloc, /* tp_dealloc */
2680 0, /* tp_print */
2681 0, /* tp_getattr */
2682 0, /* tp_setattr */
2683 0, /* tp_compare */
2684 0, /* tp_repr */
Guido van Rossum523259b2007-08-24 23:41:22 +00002685 &dictviews_as_number, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002686 &dictkeys_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002687 0, /* tp_as_mapping */
2688 0, /* tp_hash */
2689 0, /* tp_call */
2690 0, /* tp_str */
2691 PyObject_GenericGetAttr, /* tp_getattro */
2692 0, /* tp_setattro */
2693 0, /* tp_as_buffer */
2694 Py_TPFLAGS_DEFAULT, /* tp_flags */
2695 0, /* tp_doc */
2696 0, /* tp_traverse */
2697 0, /* tp_clear */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002698 dictview_richcompare, /* tp_richcompare */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002699 0, /* tp_weaklistoffset */
2700 (getiterfunc)dictkeys_iter, /* tp_iter */
2701 0, /* tp_iternext */
2702 dictkeys_methods, /* tp_methods */
2703 0,
2704};
2705
2706static PyObject *
2707dictkeys_new(PyObject *dict)
2708{
2709 return dictview_new(dict, &PyDictKeys_Type);
2710}
2711
Guido van Rossum3ac67412007-02-10 18:55:06 +00002712/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002713
2714static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002715dictitems_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002716{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002717 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002718 Py_RETURN_NONE;
2719 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002720 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
2721}
2722
2723static int
2724dictitems_contains(dictviewobject *dv, PyObject *obj)
2725{
2726 PyObject *key, *value, *found;
2727 if (dv->dv_dict == NULL)
2728 return 0;
2729 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
2730 return 0;
2731 key = PyTuple_GET_ITEM(obj, 0);
2732 value = PyTuple_GET_ITEM(obj, 1);
2733 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
2734 if (found == NULL) {
2735 if (PyErr_Occurred())
2736 return -1;
2737 return 0;
2738 }
2739 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002740}
2741
Guido van Rossum83825ac2007-02-10 04:54:19 +00002742static PySequenceMethods dictitems_as_sequence = {
2743 (lenfunc)dictview_len, /* sq_length */
2744 0, /* sq_concat */
2745 0, /* sq_repeat */
2746 0, /* sq_item */
2747 0, /* sq_slice */
2748 0, /* sq_ass_item */
2749 0, /* sq_ass_slice */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002750 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002751};
2752
Guido van Rossumb90c8482007-02-10 01:11:45 +00002753static PyMethodDef dictitems_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002754 {NULL, NULL} /* sentinel */
2755};
2756
2757PyTypeObject PyDictItems_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002758 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002759 "dict_items", /* tp_name */
2760 sizeof(dictviewobject), /* tp_basicsize */
2761 0, /* tp_itemsize */
2762 /* methods */
2763 (destructor)dictview_dealloc, /* tp_dealloc */
2764 0, /* tp_print */
2765 0, /* tp_getattr */
2766 0, /* tp_setattr */
2767 0, /* tp_compare */
2768 0, /* tp_repr */
Guido van Rossum523259b2007-08-24 23:41:22 +00002769 &dictviews_as_number, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002770 &dictitems_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002771 0, /* tp_as_mapping */
2772 0, /* tp_hash */
2773 0, /* tp_call */
2774 0, /* tp_str */
2775 PyObject_GenericGetAttr, /* tp_getattro */
2776 0, /* tp_setattro */
2777 0, /* tp_as_buffer */
2778 Py_TPFLAGS_DEFAULT, /* tp_flags */
2779 0, /* tp_doc */
2780 0, /* tp_traverse */
2781 0, /* tp_clear */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002782 dictview_richcompare, /* tp_richcompare */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002783 0, /* tp_weaklistoffset */
2784 (getiterfunc)dictitems_iter, /* tp_iter */
2785 0, /* tp_iternext */
2786 dictitems_methods, /* tp_methods */
2787 0,
2788};
2789
2790static PyObject *
2791dictitems_new(PyObject *dict)
2792{
2793 return dictview_new(dict, &PyDictItems_Type);
2794}
2795
Guido van Rossum3ac67412007-02-10 18:55:06 +00002796/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002797
2798static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002799dictvalues_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002800{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002801 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002802 Py_RETURN_NONE;
2803 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002804 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002805}
2806
Guido van Rossum83825ac2007-02-10 04:54:19 +00002807static PySequenceMethods dictvalues_as_sequence = {
2808 (lenfunc)dictview_len, /* sq_length */
2809 0, /* sq_concat */
2810 0, /* sq_repeat */
2811 0, /* sq_item */
2812 0, /* sq_slice */
2813 0, /* sq_ass_item */
2814 0, /* sq_ass_slice */
2815 (objobjproc)0, /* sq_contains */
2816};
2817
Guido van Rossumb90c8482007-02-10 01:11:45 +00002818static PyMethodDef dictvalues_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002819 {NULL, NULL} /* sentinel */
2820};
2821
2822PyTypeObject PyDictValues_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002823 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002824 "dict_values", /* tp_name */
2825 sizeof(dictviewobject), /* tp_basicsize */
2826 0, /* tp_itemsize */
2827 /* methods */
2828 (destructor)dictview_dealloc, /* tp_dealloc */
2829 0, /* tp_print */
2830 0, /* tp_getattr */
2831 0, /* tp_setattr */
2832 0, /* tp_compare */
2833 0, /* tp_repr */
2834 0, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002835 &dictvalues_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002836 0, /* tp_as_mapping */
2837 0, /* tp_hash */
2838 0, /* tp_call */
2839 0, /* tp_str */
2840 PyObject_GenericGetAttr, /* tp_getattro */
2841 0, /* tp_setattro */
2842 0, /* tp_as_buffer */
2843 Py_TPFLAGS_DEFAULT, /* tp_flags */
2844 0, /* tp_doc */
2845 0, /* tp_traverse */
2846 0, /* tp_clear */
2847 0, /* tp_richcompare */
2848 0, /* tp_weaklistoffset */
2849 (getiterfunc)dictvalues_iter, /* tp_iter */
2850 0, /* tp_iternext */
2851 dictvalues_methods, /* tp_methods */
2852 0,
2853};
2854
2855static PyObject *
2856dictvalues_new(PyObject *dict)
2857{
2858 return dictview_new(dict, &PyDictValues_Type);
2859}