blob: f21cea24c3d6a8b1beb9ad640870e937727147e5 [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
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000184/* Debug statistic to count GC tracking of dicts */
185#ifdef SHOW_TRACK_COUNT
186static Py_ssize_t count_untracked = 0;
187static Py_ssize_t count_tracked = 0;
188
189static void
190show_track(void)
191{
192 fprintf(stderr, "Dicts created: %" PY_FORMAT_SIZE_T "d\n",
193 count_tracked + count_untracked);
194 fprintf(stderr, "Dicts tracked by the GC: %" PY_FORMAT_SIZE_T
195 "d\n", count_tracked);
196 fprintf(stderr, "%.2f%% dict tracking rate\n\n",
197 (100.0*count_tracked/(count_untracked+count_tracked)));
198}
199#endif
200
201
Tim Peters6d6c1a32001-08-02 04:15:00 +0000202/* Initialization macros.
203 There are two ways to create a dict: PyDict_New() is the main C API
204 function, and the tp_new slot maps to dict_new(). In the latter case we
205 can save a little time over what PyDict_New does because it's guaranteed
206 that the PyDictObject struct is already zeroed out.
207 Everyone except dict_new() should use EMPTY_TO_MINSIZE (unless they have
208 an excellent reason not to).
209*/
210
211#define INIT_NONZERO_DICT_SLOTS(mp) do { \
Tim Petersdea48ec2001-05-22 20:40:22 +0000212 (mp)->ma_table = (mp)->ma_smalltable; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000213 (mp)->ma_mask = PyDict_MINSIZE - 1; \
214 } while(0)
215
216#define EMPTY_TO_MINSIZE(mp) do { \
217 memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000218 (mp)->ma_used = (mp)->ma_fill = 0; \
Tim Peters6d6c1a32001-08-02 04:15:00 +0000219 INIT_NONZERO_DICT_SLOTS(mp); \
Tim Petersdea48ec2001-05-22 20:40:22 +0000220 } while(0)
221
Raymond Hettinger43442782004-03-17 21:55:03 +0000222/* Dictionary reuse scheme to save calls to malloc, free, and memset */
Christian Heimes2202f872008-02-06 14:31:34 +0000223#ifndef PyDict_MAXFREELIST
224#define PyDict_MAXFREELIST 80
225#endif
226static PyDictObject *free_list[PyDict_MAXFREELIST];
227static int numfree = 0;
Raymond Hettinger43442782004-03-17 21:55:03 +0000228
Christian Heimes77c02eb2008-02-09 02:18:51 +0000229void
230PyDict_Fini(void)
231{
232 PyDictObject *op;
233
234 while (numfree) {
235 op = free_list[--numfree];
236 assert(PyDict_CheckExact(op));
237 PyObject_GC_Del(op);
238 }
239}
240
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000241PyObject *
Thomas Wouters78890102000-07-22 19:25:51 +0000242PyDict_New(void)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000243{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000244 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000245 if (dummy == NULL) { /* Auto-initialize dummy */
Neal Norwitz53cbdaa2007-08-23 21:42:55 +0000246 dummy = PyUnicode_FromString("<dummy key>");
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000247 if (dummy == NULL)
248 return NULL;
Fred Drake1bff34a2000-08-31 19:31:38 +0000249#ifdef SHOW_CONVERSION_COUNTS
250 Py_AtExit(show_counts);
251#endif
Christian Heimes77c02eb2008-02-09 02:18:51 +0000252#ifdef SHOW_ALLOC_COUNT
253 Py_AtExit(show_alloc);
254#endif
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000255#ifdef SHOW_TRACK_COUNT
256 Py_AtExit(show_track);
257#endif
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000258 }
Christian Heimes2202f872008-02-06 14:31:34 +0000259 if (numfree) {
260 mp = free_list[--numfree];
Raymond Hettinger43442782004-03-17 21:55:03 +0000261 assert (mp != NULL);
Christian Heimes90aa7642007-12-19 02:45:37 +0000262 assert (Py_TYPE(mp) == &PyDict_Type);
Raymond Hettinger43442782004-03-17 21:55:03 +0000263 _Py_NewReference((PyObject *)mp);
264 if (mp->ma_fill) {
265 EMPTY_TO_MINSIZE(mp);
Georg Brandl6aa2d1f2008-08-12 08:35:52 +0000266 } else {
267 /* At least set ma_table and ma_mask; these are wrong
268 if an empty but presized dict is added to freelist */
269 INIT_NONZERO_DICT_SLOTS(mp);
Raymond Hettinger43442782004-03-17 21:55:03 +0000270 }
271 assert (mp->ma_used == 0);
272 assert (mp->ma_table == mp->ma_smalltable);
273 assert (mp->ma_mask == PyDict_MINSIZE - 1);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000274#ifdef SHOW_ALLOC_COUNT
275 count_reuse++;
276#endif
Raymond Hettinger43442782004-03-17 21:55:03 +0000277 } else {
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000278 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
Raymond Hettinger43442782004-03-17 21:55:03 +0000279 if (mp == NULL)
280 return NULL;
281 EMPTY_TO_MINSIZE(mp);
Christian Heimes77c02eb2008-02-09 02:18:51 +0000282#ifdef SHOW_ALLOC_COUNT
283 count_alloc++;
284#endif
Raymond Hettinger43442782004-03-17 21:55:03 +0000285 }
Guido van Rossum89d8c602007-09-18 17:26:56 +0000286 mp->ma_lookup = lookdict_unicode;
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000287#ifdef SHOW_TRACK_COUNT
288 count_untracked++;
289#endif
Fred Drake1bff34a2000-08-31 19:31:38 +0000290#ifdef SHOW_CONVERSION_COUNTS
291 ++created;
292#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000293 return (PyObject *)mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000294}
295
296/*
297The basic lookup function used by all operations.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000298This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000299Open addressing is preferred over chaining since the link overhead for
300chaining would be substantial (100% with typical malloc overhead).
301
Tim Peterseb28ef22001-06-02 05:27:19 +0000302The initial probe index is computed as hash mod the table size. Subsequent
303probe indices are computed as explained earlier.
Guido van Rossum2bc13791999-03-24 19:06:42 +0000304
305All arithmetic on hash should ignore overflow.
Guido van Rossum16e93a81997-01-28 00:00:11 +0000306
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000307The details in this version are due to Tim Peters, building on many past
Tim Peterseb28ef22001-06-02 05:27:19 +0000308contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
Guido van Rossumdc5f6b22006-08-24 21:29:26 +0000309Christian Tismer.
Fred Drake1bff34a2000-08-31 19:31:38 +0000310
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000311lookdict() is general-purpose, and may return NULL if (and only if) a
312comparison raises an exception (this was new in Python 2.5).
Guido van Rossum89d8c602007-09-18 17:26:56 +0000313lookdict_unicode() below is specialized to string keys, comparison of which can
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000314never raise an exception; that function can never return NULL. For both, when
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000315the key isn't found a PyDictEntry* is returned for which the me_value field is
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000316NULL; this is the slot in the dict at which the key would have been found, and
317the caller can (if it wishes) add the <key, value> pair to the returned
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000318PyDictEntry*.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000319*/
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000320static PyDictEntry *
321lookdict(PyDictObject *mp, PyObject *key, register long hash)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000322{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000323 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000324 register size_t perturb;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000325 register PyDictEntry *freeslot;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000326 register size_t mask = (size_t)mp->ma_mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000327 PyDictEntry *ep0 = mp->ma_table;
328 register PyDictEntry *ep;
Fred Drakec88b99c2000-08-31 19:04:07 +0000329 register int cmp;
Tim Peters453163d2001-06-03 04:54:32 +0000330 PyObject *startkey;
Tim Peterseb28ef22001-06-02 05:27:19 +0000331
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000332 i = (size_t)hash & mask;
Guido van Rossumefb46091997-01-29 15:53:56 +0000333 ep = &ep0[i];
Guido van Rossumc1c7b1a1998-10-06 16:01:14 +0000334 if (ep->me_key == NULL || ep->me_key == key)
Guido van Rossum7d181591997-01-16 21:06:45 +0000335 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000336
Guido van Rossum16e93a81997-01-28 00:00:11 +0000337 if (ep->me_key == dummy)
Guido van Rossum7d181591997-01-16 21:06:45 +0000338 freeslot = ep;
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000339 else {
Fred Drakec88b99c2000-08-31 19:04:07 +0000340 if (ep->me_hash == hash) {
Tim Peters453163d2001-06-03 04:54:32 +0000341 startkey = ep->me_key;
Guido van Rossum2fd4f372007-11-29 18:43:05 +0000342 Py_INCREF(startkey);
Tim Peters453163d2001-06-03 04:54:32 +0000343 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Guido van Rossum2fd4f372007-11-29 18:43:05 +0000344 Py_DECREF(startkey);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000345 if (cmp < 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000346 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000347 if (ep0 == mp->ma_table && ep->me_key == startkey) {
348 if (cmp > 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000349 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000350 }
351 else {
352 /* The compare did major nasty stuff to the
353 * dict: start over.
354 * XXX A clever adversary could prevent this
355 * XXX from terminating.
356 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000357 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000358 }
Guido van Rossumfd7a0b81997-08-18 21:52:47 +0000359 }
360 freeslot = NULL;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000361 }
Tim Peters15d49292001-05-27 07:39:22 +0000362
Tim Peters342c65e2001-05-13 06:43:53 +0000363 /* In the loop, me_key == dummy is by far (factor of 100s) the
364 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000365 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
366 i = (i << 2) + i + perturb + 1;
367 ep = &ep0[i & mask];
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000368 if (ep->me_key == NULL)
369 return freeslot == NULL ? ep : freeslot;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000370 if (ep->me_key == key)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000371 return ep;
Tim Peters7b5d0af2001-06-03 04:14:43 +0000372 if (ep->me_hash == hash && ep->me_key != dummy) {
Tim Peters453163d2001-06-03 04:54:32 +0000373 startkey = ep->me_key;
Guido van Rossum2fd4f372007-11-29 18:43:05 +0000374 Py_INCREF(startkey);
Tim Peters453163d2001-06-03 04:54:32 +0000375 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
Guido van Rossum2fd4f372007-11-29 18:43:05 +0000376 Py_DECREF(startkey);
Tim Peters7b5d0af2001-06-03 04:14:43 +0000377 if (cmp < 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000378 return NULL;
Tim Peters453163d2001-06-03 04:54:32 +0000379 if (ep0 == mp->ma_table && ep->me_key == startkey) {
380 if (cmp > 0)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000381 return ep;
Tim Peters453163d2001-06-03 04:54:32 +0000382 }
383 else {
384 /* The compare did major nasty stuff to the
385 * dict: start over.
386 * XXX A clever adversary could prevent this
387 * XXX from terminating.
388 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000389 return lookdict(mp, key, hash);
Tim Peters453163d2001-06-03 04:54:32 +0000390 }
Guido van Rossum16e93a81997-01-28 00:00:11 +0000391 }
Tim Peters342c65e2001-05-13 06:43:53 +0000392 else if (ep->me_key == dummy && freeslot == NULL)
393 freeslot = ep;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000394 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000395 assert(0); /* NOT REACHED */
396 return 0;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000397}
398
399/*
Guido van Rossum89d8c602007-09-18 17:26:56 +0000400 * Hacked up version of lookdict which can assume keys are always
401 * unicodes; this assumption allows testing for errors during
402 * PyObject_RichCompareBool() to be dropped; unicode-unicode
403 * comparisons never raise exceptions. This also means we don't need
404 * to go through PyObject_RichCompareBool(); we can always use
405 * unicode_eq() directly.
Fred Drake1bff34a2000-08-31 19:31:38 +0000406 *
Guido van Rossum89d8c602007-09-18 17:26:56 +0000407 * This is valuable because dicts with only unicode keys are very common.
Fred Drake1bff34a2000-08-31 19:31:38 +0000408 */
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000409static PyDictEntry *
410lookdict_unicode(PyDictObject *mp, PyObject *key, register long hash)
Fred Drake1bff34a2000-08-31 19:31:38 +0000411{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000412 register size_t i;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000413 register size_t perturb;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000414 register PyDictEntry *freeslot;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000415 register size_t mask = (size_t)mp->ma_mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000416 PyDictEntry *ep0 = mp->ma_table;
417 register PyDictEntry *ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000418
Guido van Rossum89d8c602007-09-18 17:26:56 +0000419 /* Make sure this function doesn't have to handle non-unicode keys,
Tim Peters0ab085c2001-09-14 00:25:33 +0000420 including subclasses of str; e.g., one reason to subclass
Guido van Rossum89d8c602007-09-18 17:26:56 +0000421 unicodes is to override __eq__, and for speed we don't cater to
Tim Peters0ab085c2001-09-14 00:25:33 +0000422 that here. */
Guido van Rossum89d8c602007-09-18 17:26:56 +0000423 if (!PyUnicode_CheckExact(key)) {
Fred Drake1bff34a2000-08-31 19:31:38 +0000424#ifdef SHOW_CONVERSION_COUNTS
425 ++converted;
426#endif
427 mp->ma_lookup = lookdict;
428 return lookdict(mp, key, hash);
429 }
Tim Peters2f228e72001-05-13 00:19:31 +0000430 i = hash & mask;
Fred Drake1bff34a2000-08-31 19:31:38 +0000431 ep = &ep0[i];
432 if (ep->me_key == NULL || ep->me_key == key)
433 return ep;
434 if (ep->me_key == dummy)
435 freeslot = ep;
436 else {
Guido van Rossum89d8c602007-09-18 17:26:56 +0000437 if (ep->me_hash == hash && unicode_eq(ep->me_key, key))
Fred Drake1bff34a2000-08-31 19:31:38 +0000438 return ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000439 freeslot = NULL;
440 }
Tim Peters15d49292001-05-27 07:39:22 +0000441
Tim Peters342c65e2001-05-13 06:43:53 +0000442 /* In the loop, me_key == dummy is by far (factor of 100s) the
443 least likely outcome, so test for that last. */
Tim Peterseb28ef22001-06-02 05:27:19 +0000444 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
445 i = (i << 2) + i + perturb + 1;
446 ep = &ep0[i & mask];
Tim Peters342c65e2001-05-13 06:43:53 +0000447 if (ep->me_key == NULL)
448 return freeslot == NULL ? ep : freeslot;
449 if (ep->me_key == key
450 || (ep->me_hash == hash
451 && ep->me_key != dummy
Guido van Rossum89d8c602007-09-18 17:26:56 +0000452 && unicode_eq(ep->me_key, key)))
Fred Drake1bff34a2000-08-31 19:31:38 +0000453 return ep;
Tim Petersdea48ec2001-05-22 20:40:22 +0000454 if (ep->me_key == dummy && freeslot == NULL)
Tim Peters342c65e2001-05-13 06:43:53 +0000455 freeslot = ep;
Fred Drake1bff34a2000-08-31 19:31:38 +0000456 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000457 assert(0); /* NOT REACHED */
458 return 0;
Fred Drake1bff34a2000-08-31 19:31:38 +0000459}
460
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000461#ifdef SHOW_TRACK_COUNT
462#define INCREASE_TRACK_COUNT \
463 (count_tracked++, count_untracked--);
464#define DECREASE_TRACK_COUNT \
465 (count_tracked--, count_untracked++);
466#else
467#define INCREASE_TRACK_COUNT
468#define DECREASE_TRACK_COUNT
469#endif
470
471#define MAINTAIN_TRACKING(mp, key, value) \
472 do { \
473 if (!_PyObject_GC_IS_TRACKED(mp)) { \
474 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
475 _PyObject_GC_MAY_BE_TRACKED(value)) { \
476 _PyObject_GC_TRACK(mp); \
477 INCREASE_TRACK_COUNT \
478 } \
479 } \
480 } while(0)
481
482void
483_PyDict_MaybeUntrack(PyObject *op)
484{
485 PyDictObject *mp;
486 PyObject *value;
487 Py_ssize_t mask, i;
488 PyDictEntry *ep;
489
490 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
491 return;
492
493 mp = (PyDictObject *) op;
494 ep = mp->ma_table;
495 mask = mp->ma_mask;
496 for (i = 0; i <= mask; i++) {
497 if ((value = ep[i].me_value) == NULL)
498 continue;
499 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
500 _PyObject_GC_MAY_BE_TRACKED(ep[i].me_key))
501 return;
502 }
Antoine Pitrouacc5d6b2009-03-23 19:19:54 +0000503 DECREASE_TRACK_COUNT
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000504 _PyObject_GC_UNTRACK(op);
505}
506
507
Fred Drake1bff34a2000-08-31 19:31:38 +0000508/*
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000509Internal routine to insert a new item into the table.
510Used both by the internal resize routine and by the public insert routine.
511Eats a reference to key and one to value.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000512Returns -1 if an error occurred, or 0 on success.
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000513*/
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000514static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000515insertdict(register PyDictObject *mp, PyObject *key, long hash, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000516{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000517 PyObject *old_value;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000518 register PyDictEntry *ep;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000519 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
520
521 assert(mp->ma_lookup != NULL);
522 ep = mp->ma_lookup(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000523 if (ep == NULL) {
524 Py_DECREF(key);
525 Py_DECREF(value);
526 return -1;
527 }
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000528 MAINTAIN_TRACKING(mp, key, value);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000529 if (ep->me_value != NULL) {
Guido van Rossumd7047b31995-01-02 19:07:15 +0000530 old_value = ep->me_value;
531 ep->me_value = value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000532 Py_DECREF(old_value); /* which **CAN** re-enter */
533 Py_DECREF(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000534 }
535 else {
536 if (ep->me_key == NULL)
537 mp->ma_fill++;
Raymond Hettinger07ead172005-02-05 23:42:57 +0000538 else {
539 assert(ep->me_key == dummy);
540 Py_DECREF(dummy);
541 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000542 ep->me_key = key;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000543 ep->me_hash = (Py_ssize_t)hash;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000544 ep->me_value = value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000545 mp->ma_used++;
546 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000547 return 0;
548}
549
550/*
551Internal routine used by dictresize() to insert an item which is
552known to be absent from the dict. This routine also assumes that
553the dict contains no deleted entries. Besides the performance benefit,
554using insertdict() in dictresize() is dangerous (SF bug #1456209).
555Note that no refcounts are changed by this routine; if needed, the caller
556is responsible for incref'ing `key` and `value`.
557*/
558static void
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000559insertdict_clean(register PyDictObject *mp, PyObject *key, long hash,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000560 PyObject *value)
561{
562 register size_t i;
563 register size_t perturb;
564 register size_t mask = (size_t)mp->ma_mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000565 PyDictEntry *ep0 = mp->ma_table;
566 register PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000567
Antoine Pitrou3a652b12009-03-23 18:52:06 +0000568 MAINTAIN_TRACKING(mp, key, value);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000569 i = hash & mask;
570 ep = &ep0[i];
571 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
572 i = (i << 2) + i + perturb + 1;
573 ep = &ep0[i & mask];
574 }
575 assert(ep->me_value == NULL);
576 mp->ma_fill++;
577 ep->me_key = key;
578 ep->me_hash = (Py_ssize_t)hash;
579 ep->me_value = value;
580 mp->ma_used++;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000581}
582
583/*
584Restructure the table by allocating a new table and reinserting all
585items again. When entries have been deleted, the new table may
586actually be smaller than the old one.
587*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000588static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000589dictresize(PyDictObject *mp, Py_ssize_t minused)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000590{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000591 Py_ssize_t newsize;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000592 PyDictEntry *oldtable, *newtable, *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000593 Py_ssize_t i;
Tim Peters0c6010b2001-05-23 23:33:57 +0000594 int is_oldtable_malloced;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000595 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Peters91a364d2001-05-19 07:04:38 +0000596
597 assert(minused >= 0);
Tim Peters0c6010b2001-05-23 23:33:57 +0000598
Tim Peterseb28ef22001-06-02 05:27:19 +0000599 /* Find the smallest table size > minused. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000600 for (newsize = PyDict_MINSIZE;
Tim Peterseb28ef22001-06-02 05:27:19 +0000601 newsize <= minused && newsize > 0;
602 newsize <<= 1)
603 ;
604 if (newsize <= 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000605 PyErr_NoMemory();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000606 return -1;
607 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000608
609 /* Get space for a new table. */
610 oldtable = mp->ma_table;
611 assert(oldtable != NULL);
612 is_oldtable_malloced = oldtable != mp->ma_smalltable;
613
Tim Peters6d6c1a32001-08-02 04:15:00 +0000614 if (newsize == PyDict_MINSIZE) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000615 /* A large table is shrinking, or we can't get any smaller. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000616 newtable = mp->ma_smalltable;
Tim Peters0c6010b2001-05-23 23:33:57 +0000617 if (newtable == oldtable) {
Tim Petersf8a548c2001-05-24 16:26:40 +0000618 if (mp->ma_fill == mp->ma_used) {
619 /* No dummies, so no point doing anything. */
Tim Peters0c6010b2001-05-23 23:33:57 +0000620 return 0;
Tim Petersf8a548c2001-05-24 16:26:40 +0000621 }
622 /* We're not going to resize it, but rebuild the
623 table anyway to purge old dummy entries.
624 Subtle: This is *necessary* if fill==size,
625 as lookdict needs at least one virgin slot to
626 terminate failing searches. If fill < size, it's
627 merely desirable, as dummies slow searches. */
628 assert(mp->ma_fill > mp->ma_used);
Tim Peters0c6010b2001-05-23 23:33:57 +0000629 memcpy(small_copy, oldtable, sizeof(small_copy));
630 oldtable = small_copy;
631 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000632 }
633 else {
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000634 newtable = PyMem_NEW(PyDictEntry, newsize);
Tim Petersdea48ec2001-05-22 20:40:22 +0000635 if (newtable == NULL) {
636 PyErr_NoMemory();
637 return -1;
638 }
639 }
Tim Peters0c6010b2001-05-23 23:33:57 +0000640
641 /* Make the dict empty, using the new table. */
Tim Petersdea48ec2001-05-22 20:40:22 +0000642 assert(newtable != oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000643 mp->ma_table = newtable;
Tim Petersafb6ae82001-06-04 21:00:21 +0000644 mp->ma_mask = newsize - 1;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000645 memset(newtable, 0, sizeof(PyDictEntry) * newsize);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000646 mp->ma_used = 0;
Tim Petersdea48ec2001-05-22 20:40:22 +0000647 i = mp->ma_fill;
648 mp->ma_fill = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000649
Tim Peters19283142001-05-17 22:25:34 +0000650 /* Copy the data over; this is refcount-neutral for active entries;
651 dummy entries aren't copied over, of course */
Tim Petersdea48ec2001-05-22 20:40:22 +0000652 for (ep = oldtable; i > 0; ep++) {
653 if (ep->me_value != NULL) { /* active entry */
654 --i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000655 insertdict_clean(mp, ep->me_key, (long)ep->me_hash,
656 ep->me_value);
Tim Petersdea48ec2001-05-22 20:40:22 +0000657 }
Tim Peters19283142001-05-17 22:25:34 +0000658 else if (ep->me_key != NULL) { /* dummy entry */
Tim Petersdea48ec2001-05-22 20:40:22 +0000659 --i;
Tim Peters19283142001-05-17 22:25:34 +0000660 assert(ep->me_key == dummy);
661 Py_DECREF(ep->me_key);
Guido van Rossum255443b1998-04-10 22:47:14 +0000662 }
Tim Peters19283142001-05-17 22:25:34 +0000663 /* else key == value == NULL: nothing to do */
Guido van Rossumd7047b31995-01-02 19:07:15 +0000664 }
665
Tim Peters0c6010b2001-05-23 23:33:57 +0000666 if (is_oldtable_malloced)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000667 PyMem_DEL(oldtable);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000668 return 0;
669}
670
Christian Heimes99170a52007-12-19 02:07:34 +0000671/* Create a new dictionary pre-sized to hold an estimated number of elements.
672 Underestimates are okay because the dictionary will resize as necessary.
673 Overestimates just mean the dictionary will be more sparse than usual.
674*/
675
676PyObject *
677_PyDict_NewPresized(Py_ssize_t minused)
678{
679 PyObject *op = PyDict_New();
680
681 if (minused>5 && op != NULL && dictresize((PyDictObject *)op, minused) == -1) {
682 Py_DECREF(op);
683 return NULL;
684 }
685 return op;
686}
687
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000688/* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
689 * that may occur (originally dicts supported only string keys, and exceptions
690 * weren't possible). So, while the original intent was that a NULL return
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000691 * meant the key wasn't present, in reality it can mean that, or that an error
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000692 * (suppressed) occurred while computing the key's hash, or that some error
693 * (suppressed) occurred when comparing keys in the dict's internal probe
694 * sequence. A nasty example of the latter is when a Python-coded comparison
695 * function hits a stack-depth error, which can cause this to return NULL
696 * even if the key is present.
697 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000698PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +0000699PyDict_GetItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000700{
701 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000702 PyDictObject *mp = (PyDictObject *)op;
703 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000704 PyThreadState *tstate;
705 if (!PyDict_Check(op))
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000706 return NULL;
Guido van Rossum4d027722007-09-18 04:30:42 +0000707 if (!PyUnicode_CheckExact(key) ||
708 (hash = ((PyUnicodeObject *) key)->hash) == -1)
Guido van Rossumca756f21997-01-23 19:39:29 +0000709 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000710 hash = PyObject_Hash(key);
Guido van Rossum474b19e1998-05-14 01:00:51 +0000711 if (hash == -1) {
712 PyErr_Clear();
Guido van Rossumca756f21997-01-23 19:39:29 +0000713 return NULL;
Guido van Rossum474b19e1998-05-14 01:00:51 +0000714 }
Guido van Rossumca756f21997-01-23 19:39:29 +0000715 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000716
717 /* We can arrive here with a NULL tstate during initialization:
718 try running "python -Wi" for an example related to string
719 interning. Let's just hope that no exception occurs then... */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000720 tstate = _PyThreadState_Current;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000721 if (tstate != NULL && tstate->curexc_type != NULL) {
722 /* preserve the existing exception */
723 PyObject *err_type, *err_value, *err_tb;
724 PyErr_Fetch(&err_type, &err_value, &err_tb);
725 ep = (mp->ma_lookup)(mp, key, hash);
726 /* ignore errors */
727 PyErr_Restore(err_type, err_value, err_tb);
728 if (ep == NULL)
729 return NULL;
730 }
731 else {
732 ep = (mp->ma_lookup)(mp, key, hash);
733 if (ep == NULL) {
734 PyErr_Clear();
735 return NULL;
736 }
737 }
738 return ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000739}
740
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000741/* Variant of PyDict_GetItem() that doesn't suppress exceptions.
742 This returns NULL *with* an exception set if an exception occurred.
743 It returns NULL *without* an exception set if the key wasn't present.
744*/
745PyObject *
746PyDict_GetItemWithError(PyObject *op, PyObject *key)
747{
748 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000749 PyDictObject*mp = (PyDictObject *)op;
750 PyDictEntry *ep;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000751
752 if (!PyDict_Check(op)) {
753 PyErr_BadInternalCall();
754 return NULL;
755 }
Guido van Rossum89d8c602007-09-18 17:26:56 +0000756 if (!PyUnicode_CheckExact(key) ||
757 (hash = ((PyUnicodeObject *) key)->hash) == -1)
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000758 {
759 hash = PyObject_Hash(key);
760 if (hash == -1) {
761 return NULL;
762 }
763 }
764
765 ep = (mp->ma_lookup)(mp, key, hash);
766 if (ep == NULL)
767 return NULL;
768 return ep->me_value;
769}
770
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000771/* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
Tim Peters60b29962006-01-01 01:19:23 +0000772 * dictionary if it's merely replacing the value for an existing key.
773 * This means that it's safe to loop over a dictionary with PyDict_Next()
774 * and occasionally replace a value -- but you can't insert new keys or
775 * remove them.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000776 */
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000777int
Tim Peters1f5871e2000-07-04 17:44:48 +0000778PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000779{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000780 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000781 register long hash;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000782 register Py_ssize_t n_used;
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000783
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000784 if (!PyDict_Check(op)) {
785 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000786 return -1;
787 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000788 assert(key);
789 assert(value);
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000790 mp = (PyDictObject *)op;
Guido van Rossum4d027722007-09-18 04:30:42 +0000791 if (!PyUnicode_CheckExact(key) ||
792 (hash = ((PyUnicodeObject *) key)->hash) == -1)
793 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000794 hash = PyObject_Hash(key);
Guido van Rossum2a61e741997-01-18 07:55:05 +0000795 if (hash == -1)
796 return -1;
Guido van Rossum2a61e741997-01-18 07:55:05 +0000797 }
Tim Petersafb6ae82001-06-04 21:00:21 +0000798 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000799 n_used = mp->ma_used;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000800 Py_INCREF(value);
801 Py_INCREF(key);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000802 if (insertdict(mp, key, hash, value) != 0)
803 return -1;
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000804 /* If we added a key, we can safely resize. Otherwise just return!
805 * If fill >= 2/3 size, adjust size. Normally, this doubles or
806 * quaduples the size, but it's also possible for the dict to shrink
Tim Peters60b29962006-01-01 01:19:23 +0000807 * (if ma_fill is much larger than ma_used, meaning a lot of dict
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000808 * keys have been * deleted).
Tim Peters60b29962006-01-01 01:19:23 +0000809 *
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000810 * Quadrupling the size improves average dictionary sparseness
811 * (reducing collisions) at the cost of some memory and iteration
812 * speed (which loops over every possible entry). It also halves
813 * the number of expensive resize operations in a growing dictionary.
Tim Peters60b29962006-01-01 01:19:23 +0000814 *
815 * Very large dictionaries (over 50K items) use doubling instead.
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000816 * This may help applications with severe memory constraints.
Guido van Rossuma4dd0112001-04-15 22:16:26 +0000817 */
Raymond Hettinger3539f6b2003-05-05 22:22:10 +0000818 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
819 return 0;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000820 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000821}
822
823int
Tim Peters1f5871e2000-07-04 17:44:48 +0000824PyDict_DelItem(PyObject *op, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000825{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000826 register PyDictObject *mp;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000827 register long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000828 register PyDictEntry *ep;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000829 PyObject *old_value, *old_key;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000830
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000831 if (!PyDict_Check(op)) {
832 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000833 return -1;
834 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000835 assert(key);
Guido van Rossum89d8c602007-09-18 17:26:56 +0000836 if (!PyUnicode_CheckExact(key) ||
837 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000838 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +0000839 if (hash == -1)
840 return -1;
841 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000842 mp = (PyDictObject *)op;
Fred Drake1bff34a2000-08-31 19:31:38 +0000843 ep = (mp->ma_lookup)(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000844 if (ep == NULL)
845 return -1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000846 if (ep->me_value == NULL) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000847 set_key_error(key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000848 return -1;
849 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000850 old_key = ep->me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000851 Py_INCREF(dummy);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000852 ep->me_key = dummy;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000853 old_value = ep->me_value;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000854 ep->me_value = NULL;
855 mp->ma_used--;
Tim Petersea8f2bf2000-12-13 01:02:46 +0000856 Py_DECREF(old_value);
857 Py_DECREF(old_key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000858 return 0;
859}
860
Guido van Rossum25831651993-05-19 14:50:45 +0000861void
Tim Peters1f5871e2000-07-04 17:44:48 +0000862PyDict_Clear(PyObject *op)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000863{
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000864 PyDictObject *mp;
865 PyDictEntry *ep, *table;
Tim Petersdea48ec2001-05-22 20:40:22 +0000866 int table_is_malloced;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000867 Py_ssize_t fill;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000868 PyDictEntry small_copy[PyDict_MINSIZE];
Tim Petersdea48ec2001-05-22 20:40:22 +0000869#ifdef Py_DEBUG
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000870 Py_ssize_t i, n;
Tim Petersdea48ec2001-05-22 20:40:22 +0000871#endif
872
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000873 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000874 return;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000875 mp = (PyDictObject *)op;
Tim Petersdea48ec2001-05-22 20:40:22 +0000876#ifdef Py_DEBUG
Tim Petersafb6ae82001-06-04 21:00:21 +0000877 n = mp->ma_mask + 1;
Tim Petersdea48ec2001-05-22 20:40:22 +0000878 i = 0;
879#endif
880
881 table = mp->ma_table;
882 assert(table != NULL);
883 table_is_malloced = table != mp->ma_smalltable;
884
885 /* This is delicate. During the process of clearing the dict,
886 * decrefs can cause the dict to mutate. To avoid fatal confusion
887 * (voice of experience), we have to make the dict empty before
888 * clearing the slots, and never refer to anything via mp->xxx while
889 * clearing.
890 */
891 fill = mp->ma_fill;
892 if (table_is_malloced)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000893 EMPTY_TO_MINSIZE(mp);
Tim Petersdea48ec2001-05-22 20:40:22 +0000894
895 else if (fill > 0) {
896 /* It's a small table with something that needs to be cleared.
897 * Afraid the only safe way is to copy the dict entries into
898 * another small table first.
899 */
900 memcpy(small_copy, table, sizeof(small_copy));
901 table = small_copy;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000902 EMPTY_TO_MINSIZE(mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000903 }
Tim Petersdea48ec2001-05-22 20:40:22 +0000904 /* else it's a small table that's already empty */
905
906 /* Now we can finally clear things. If C had refcounts, we could
907 * assert that the refcount on table is 1 now, i.e. that this function
908 * has unique access to it, so decref side-effects can't alter it.
909 */
910 for (ep = table; fill > 0; ++ep) {
911#ifdef Py_DEBUG
912 assert(i < n);
913 ++i;
914#endif
915 if (ep->me_key) {
916 --fill;
917 Py_DECREF(ep->me_key);
918 Py_XDECREF(ep->me_value);
919 }
920#ifdef Py_DEBUG
921 else
922 assert(ep->me_value == NULL);
923#endif
924 }
925
926 if (table_is_malloced)
927 PyMem_DEL(table);
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000928}
929
Tim Peters080c88b2003-02-15 03:01:11 +0000930/*
931 * Iterate over a dict. Use like so:
932 *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000933 * Py_ssize_t i;
Tim Peters080c88b2003-02-15 03:01:11 +0000934 * PyObject *key, *value;
935 * i = 0; # important! i should not otherwise be changed by you
Neal Norwitz07323012003-02-15 14:45:12 +0000936 * while (PyDict_Next(yourdict, &i, &key, &value)) {
Tim Peters080c88b2003-02-15 03:01:11 +0000937 * Refer to borrowed references in key and value.
938 * }
939 *
940 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
Tim Peters67830702001-03-21 19:23:56 +0000941 * mutates the dict. One exception: it is safe if the loop merely changes
942 * the values associated with the keys (but doesn't insert new keys or
943 * delete keys), via PyDict_SetItem().
944 */
Guido van Rossum25831651993-05-19 14:50:45 +0000945int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000946PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000947{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000948 register Py_ssize_t i;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000949 register Py_ssize_t mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000950 register PyDictEntry *ep;
Raymond Hettinger43442782004-03-17 21:55:03 +0000951
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000952 if (!PyDict_Check(op))
Guido van Rossum25831651993-05-19 14:50:45 +0000953 return 0;
Guido van Rossum25831651993-05-19 14:50:45 +0000954 i = *ppos;
955 if (i < 0)
956 return 0;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000957 ep = ((PyDictObject *)op)->ma_table;
958 mask = ((PyDictObject *)op)->ma_mask;
Raymond Hettinger43442782004-03-17 21:55:03 +0000959 while (i <= mask && ep[i].me_value == NULL)
Guido van Rossum25831651993-05-19 14:50:45 +0000960 i++;
961 *ppos = i+1;
Raymond Hettinger43442782004-03-17 21:55:03 +0000962 if (i > mask)
Guido van Rossum25831651993-05-19 14:50:45 +0000963 return 0;
964 if (pkey)
Raymond Hettinger43442782004-03-17 21:55:03 +0000965 *pkey = ep[i].me_key;
Guido van Rossum25831651993-05-19 14:50:45 +0000966 if (pvalue)
Raymond Hettinger43442782004-03-17 21:55:03 +0000967 *pvalue = ep[i].me_value;
Guido van Rossum25831651993-05-19 14:50:45 +0000968 return 1;
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000969}
970
Thomas Wouterscf297e42007-02-23 15:07:44 +0000971/* Internal version of PyDict_Next that returns a hash value in addition to the key and value.*/
972int
973_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, long *phash)
974{
975 register Py_ssize_t i;
976 register Py_ssize_t mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000977 register PyDictEntry *ep;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000978
979 if (!PyDict_Check(op))
980 return 0;
981 i = *ppos;
982 if (i < 0)
983 return 0;
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000984 ep = ((PyDictObject *)op)->ma_table;
985 mask = ((PyDictObject *)op)->ma_mask;
Thomas Wouterscf297e42007-02-23 15:07:44 +0000986 while (i <= mask && ep[i].me_value == NULL)
987 i++;
988 *ppos = i+1;
989 if (i > mask)
990 return 0;
991 *phash = (long)(ep[i].me_hash);
992 if (pkey)
993 *pkey = ep[i].me_key;
994 if (pvalue)
995 *pvalue = ep[i].me_value;
996 return 1;
997}
998
Guido van Rossum4b1302b1993-03-27 18:11:32 +0000999/* Methods */
1000
1001static void
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001002dict_dealloc(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001003{
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001004 register PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001005 Py_ssize_t fill = mp->ma_fill;
Guido van Rossumff413af2002-03-28 20:34:59 +00001006 PyObject_GC_UnTrack(mp);
Guido van Rossumd724b232000-03-13 16:01:29 +00001007 Py_TRASHCAN_SAFE_BEGIN(mp)
Tim Petersdea48ec2001-05-22 20:40:22 +00001008 for (ep = mp->ma_table; fill > 0; ep++) {
1009 if (ep->me_key) {
1010 --fill;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001011 Py_DECREF(ep->me_key);
Tim Petersdea48ec2001-05-22 20:40:22 +00001012 Py_XDECREF(ep->me_value);
Guido van Rossum255443b1998-04-10 22:47:14 +00001013 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001014 }
Tim Petersdea48ec2001-05-22 20:40:22 +00001015 if (mp->ma_table != mp->ma_smalltable)
Guido van Rossumb18618d2000-05-03 23:44:39 +00001016 PyMem_DEL(mp->ma_table);
Christian Heimes2202f872008-02-06 14:31:34 +00001017 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
1018 free_list[numfree++] = mp;
Tim Peters60b29962006-01-01 01:19:23 +00001019 else
Christian Heimes90aa7642007-12-19 02:45:37 +00001020 Py_TYPE(mp)->tp_free((PyObject *)mp);
Guido van Rossumd724b232000-03-13 16:01:29 +00001021 Py_TRASHCAN_SAFE_END(mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001022}
1023
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001024static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001025dict_repr(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001026{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001027 Py_ssize_t i;
Tim Petersa7259592001-06-16 05:11:17 +00001028 PyObject *s, *temp, *colon = NULL;
1029 PyObject *pieces = NULL, *result = NULL;
1030 PyObject *key, *value;
Guido van Rossum255443b1998-04-10 22:47:14 +00001031
Tim Petersa7259592001-06-16 05:11:17 +00001032 i = Py_ReprEnter((PyObject *)mp);
Guido van Rossum255443b1998-04-10 22:47:14 +00001033 if (i != 0) {
Walter Dörwald1ab83302007-05-18 17:15:44 +00001034 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
Guido van Rossum255443b1998-04-10 22:47:14 +00001035 }
1036
Tim Petersa7259592001-06-16 05:11:17 +00001037 if (mp->ma_used == 0) {
Walter Dörwald1ab83302007-05-18 17:15:44 +00001038 result = PyUnicode_FromString("{}");
Tim Petersa7259592001-06-16 05:11:17 +00001039 goto Done;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001040 }
Tim Petersa7259592001-06-16 05:11:17 +00001041
1042 pieces = PyList_New(0);
1043 if (pieces == NULL)
1044 goto Done;
1045
Walter Dörwald1ab83302007-05-18 17:15:44 +00001046 colon = PyUnicode_FromString(": ");
Tim Petersa7259592001-06-16 05:11:17 +00001047 if (colon == NULL)
1048 goto Done;
1049
1050 /* Do repr() on each key+value pair, and insert ": " between them.
1051 Note that repr may mutate the dict. */
Tim Petersc6057842001-06-16 07:52:53 +00001052 i = 0;
1053 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
Tim Petersa7259592001-06-16 05:11:17 +00001054 int status;
1055 /* Prevent repr from deleting value during key format. */
1056 Py_INCREF(value);
1057 s = PyObject_Repr(key);
Walter Dörwald1ab83302007-05-18 17:15:44 +00001058 PyUnicode_Append(&s, colon);
1059 PyUnicode_AppendAndDel(&s, PyObject_Repr(value));
Tim Petersa7259592001-06-16 05:11:17 +00001060 Py_DECREF(value);
1061 if (s == NULL)
1062 goto Done;
1063 status = PyList_Append(pieces, s);
1064 Py_DECREF(s); /* append created a new ref */
1065 if (status < 0)
1066 goto Done;
1067 }
1068
1069 /* Add "{}" decorations to the first and last items. */
1070 assert(PyList_GET_SIZE(pieces) > 0);
Walter Dörwald1ab83302007-05-18 17:15:44 +00001071 s = PyUnicode_FromString("{");
Tim Petersa7259592001-06-16 05:11:17 +00001072 if (s == NULL)
1073 goto Done;
1074 temp = PyList_GET_ITEM(pieces, 0);
Walter Dörwald1ab83302007-05-18 17:15:44 +00001075 PyUnicode_AppendAndDel(&s, temp);
Tim Petersa7259592001-06-16 05:11:17 +00001076 PyList_SET_ITEM(pieces, 0, s);
1077 if (s == NULL)
1078 goto Done;
1079
Walter Dörwald1ab83302007-05-18 17:15:44 +00001080 s = PyUnicode_FromString("}");
Tim Petersa7259592001-06-16 05:11:17 +00001081 if (s == NULL)
1082 goto Done;
1083 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
Walter Dörwald1ab83302007-05-18 17:15:44 +00001084 PyUnicode_AppendAndDel(&temp, s);
Tim Petersa7259592001-06-16 05:11:17 +00001085 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
1086 if (temp == NULL)
1087 goto Done;
1088
1089 /* Paste them all together with ", " between. */
Walter Dörwald1ab83302007-05-18 17:15:44 +00001090 s = PyUnicode_FromString(", ");
Tim Petersa7259592001-06-16 05:11:17 +00001091 if (s == NULL)
1092 goto Done;
Walter Dörwald1ab83302007-05-18 17:15:44 +00001093 result = PyUnicode_Join(s, pieces);
Tim Peters0ab085c2001-09-14 00:25:33 +00001094 Py_DECREF(s);
Tim Petersa7259592001-06-16 05:11:17 +00001095
1096Done:
1097 Py_XDECREF(pieces);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001098 Py_XDECREF(colon);
Tim Petersa7259592001-06-16 05:11:17 +00001099 Py_ReprLeave((PyObject *)mp);
1100 return result;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001101}
1102
Martin v. Löwis18e16552006-02-15 17:27:45 +00001103static Py_ssize_t
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001104dict_length(PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001105{
1106 return mp->ma_used;
1107}
1108
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001109static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001110dict_subscript(PyDictObject *mp, register PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001111{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001112 PyObject *v;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +00001113 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001114 PyDictEntry *ep;
Tim Petersdea48ec2001-05-22 20:40:22 +00001115 assert(mp->ma_table != NULL);
Guido van Rossum89d8c602007-09-18 17:26:56 +00001116 if (!PyUnicode_CheckExact(key) ||
1117 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001118 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001119 if (hash == -1)
1120 return NULL;
1121 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001122 ep = (mp->ma_lookup)(mp, key, hash);
1123 if (ep == NULL)
1124 return NULL;
1125 v = ep->me_value;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001126 if (v == NULL) {
1127 if (!PyDict_CheckExact(mp)) {
1128 /* Look up __missing__ method if we're a subclass. */
Benjamin Peterson0ffaaa62009-05-27 03:18:19 +00001129 PyObject *missing, *res;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001130 static PyObject *missing_str = NULL;
Benjamin Petersona7205592009-05-27 03:08:59 +00001131 missing = _PyObject_LookupSpecial((PyObject *)mp,
1132 "__missing__",
1133 &missing_str);
Benjamin Peterson0ffaaa62009-05-27 03:18:19 +00001134 if (missing != NULL) {
1135 res = PyObject_CallFunctionObjArgs(missing,
1136 key, NULL);
1137 Py_DECREF(missing);
1138 return res;
1139 }
Benjamin Petersona7205592009-05-27 03:08:59 +00001140 else if (PyErr_Occurred())
1141 return NULL;
Guido van Rossum1968ad32006-02-25 22:38:04 +00001142 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001143 set_key_error(key);
Guido van Rossum1968ad32006-02-25 22:38:04 +00001144 return NULL;
1145 }
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001146 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001147 Py_INCREF(v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001148 return v;
1149}
1150
1151static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001152dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001153{
1154 if (w == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001155 return PyDict_DelItem((PyObject *)mp, v);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001156 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001157 return PyDict_SetItem((PyObject *)mp, v, w);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001158}
1159
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001160static PyMappingMethods dict_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001161 (lenfunc)dict_length, /*mp_length*/
Guido van Rossuma9e7a811997-05-13 21:02:11 +00001162 (binaryfunc)dict_subscript, /*mp_subscript*/
1163 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001164};
1165
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001166static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001167dict_keys(register PyDictObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001168{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001169 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001170 register Py_ssize_t i, j;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001171 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001172 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001173
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001174 again:
1175 n = mp->ma_used;
1176 v = PyList_New(n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001177 if (v == NULL)
1178 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001179 if (n != mp->ma_used) {
1180 /* Durnit. The allocations caused the dict to resize.
1181 * Just start over, this shouldn't normally happen.
1182 */
1183 Py_DECREF(v);
1184 goto again;
1185 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001186 ep = mp->ma_table;
1187 mask = mp->ma_mask;
1188 for (i = 0, j = 0; i <= mask; i++) {
1189 if (ep[i].me_value != NULL) {
1190 PyObject *key = ep[i].me_key;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001191 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001192 PyList_SET_ITEM(v, j, key);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001193 j++;
1194 }
1195 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001196 assert(j == n);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001197 return v;
1198}
1199
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001200static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001201dict_values(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001202{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001203 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001204 register Py_ssize_t i, j;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001205 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001206 Py_ssize_t mask, n;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001207
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001208 again:
1209 n = mp->ma_used;
1210 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001211 if (v == NULL)
1212 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001213 if (n != mp->ma_used) {
1214 /* Durnit. The allocations caused the dict to resize.
1215 * Just start over, this shouldn't normally happen.
1216 */
1217 Py_DECREF(v);
1218 goto again;
1219 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001220 ep = mp->ma_table;
1221 mask = mp->ma_mask;
1222 for (i = 0, j = 0; i <= mask; i++) {
1223 if (ep[i].me_value != NULL) {
1224 PyObject *value = ep[i].me_value;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001225 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001226 PyList_SET_ITEM(v, j, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001227 j++;
1228 }
1229 }
Raymond Hettinger43442782004-03-17 21:55:03 +00001230 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001231 return v;
1232}
1233
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001234static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001235dict_items(register PyDictObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001236{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001237 register PyObject *v;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001238 register Py_ssize_t i, j, n;
1239 Py_ssize_t mask;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001240 PyObject *item, *key, *value;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001241 PyDictEntry *ep;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001242
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001243 /* Preallocate the list of tuples, to avoid allocations during
1244 * the loop over the items, which could trigger GC, which
1245 * could resize the dict. :-(
1246 */
1247 again:
1248 n = mp->ma_used;
1249 v = PyList_New(n);
Guido van Rossum25831651993-05-19 14:50:45 +00001250 if (v == NULL)
1251 return NULL;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001252 for (i = 0; i < n; i++) {
1253 item = PyTuple_New(2);
1254 if (item == NULL) {
1255 Py_DECREF(v);
1256 return NULL;
1257 }
1258 PyList_SET_ITEM(v, i, item);
1259 }
1260 if (n != mp->ma_used) {
1261 /* Durnit. The allocations caused the dict to resize.
1262 * Just start over, this shouldn't normally happen.
1263 */
1264 Py_DECREF(v);
1265 goto again;
1266 }
1267 /* Nothing we do below makes any function calls. */
Raymond Hettinger43442782004-03-17 21:55:03 +00001268 ep = mp->ma_table;
1269 mask = mp->ma_mask;
1270 for (i = 0, j = 0; i <= mask; i++) {
Raymond Hettinger06905122004-03-19 10:30:00 +00001271 if ((value=ep[i].me_value) != NULL) {
Raymond Hettinger43442782004-03-17 21:55:03 +00001272 key = ep[i].me_key;
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001273 item = PyList_GET_ITEM(v, j);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001274 Py_INCREF(key);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001275 PyTuple_SET_ITEM(item, 0, key);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001276 Py_INCREF(value);
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001277 PyTuple_SET_ITEM(item, 1, value);
Guido van Rossum25831651993-05-19 14:50:45 +00001278 j++;
1279 }
1280 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001281 assert(j == n);
Guido van Rossum25831651993-05-19 14:50:45 +00001282 return v;
1283}
1284
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001285static PyObject *
Tim Petersbca1cbc2002-12-09 22:56:13 +00001286dict_fromkeys(PyObject *cls, PyObject *args)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001287{
1288 PyObject *seq;
1289 PyObject *value = Py_None;
1290 PyObject *it; /* iter(seq) */
1291 PyObject *key;
1292 PyObject *d;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001293 int status;
1294
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001295 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001296 return NULL;
1297
1298 d = PyObject_CallObject(cls, NULL);
1299 if (d == NULL)
1300 return NULL;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001301
Guido van Rossum58da9312007-11-10 23:39:45 +00001302 if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) {
1303 PyDictObject *mp = (PyDictObject *)d;
1304 PyObject *oldvalue;
1305 Py_ssize_t pos = 0;
1306 PyObject *key;
1307 long hash;
1308
Benjamin Peterson41181742008-07-02 20:22:54 +00001309 if (dictresize(mp, Py_SIZE(seq)))
Guido van Rossum58da9312007-11-10 23:39:45 +00001310 return NULL;
1311
1312 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
1313 Py_INCREF(key);
1314 Py_INCREF(value);
1315 if (insertdict(mp, key, hash, value))
1316 return NULL;
1317 }
1318 return d;
1319 }
1320
Guido van Rossumd8faa362007-04-27 19:54:29 +00001321 if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001322 PyDictObject *mp = (PyDictObject *)d;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001323 Py_ssize_t pos = 0;
1324 PyObject *key;
1325 long hash;
1326
1327 if (dictresize(mp, PySet_GET_SIZE(seq)))
1328 return NULL;
1329
1330 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
1331 Py_INCREF(key);
1332 Py_INCREF(value);
1333 if (insertdict(mp, key, hash, value))
1334 return NULL;
1335 }
1336 return d;
1337 }
1338
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001339 it = PyObject_GetIter(seq);
1340 if (it == NULL){
1341 Py_DECREF(d);
1342 return NULL;
1343 }
1344
Guido van Rossum58da9312007-11-10 23:39:45 +00001345 if (PyDict_CheckExact(d)) {
1346 while ((key = PyIter_Next(it)) != NULL) {
1347 status = PyDict_SetItem(d, key, value);
1348 Py_DECREF(key);
1349 if (status < 0)
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001350 goto Fail;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001351 }
Guido van Rossum58da9312007-11-10 23:39:45 +00001352 } else {
1353 while ((key = PyIter_Next(it)) != NULL) {
1354 status = PyObject_SetItem(d, key, value);
1355 Py_DECREF(key);
1356 if (status < 0)
1357 goto Fail;
1358 }
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001359 }
1360
Guido van Rossum58da9312007-11-10 23:39:45 +00001361 if (PyErr_Occurred())
1362 goto Fail;
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001363 Py_DECREF(it);
1364 return d;
1365
1366Fail:
1367 Py_DECREF(it);
1368 Py_DECREF(d);
1369 return NULL;
1370}
1371
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001372static int
1373dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001374{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00001375 PyObject *arg = NULL;
1376 int result = 0;
1377
1378 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
1379 result = -1;
1380
1381 else if (arg != NULL) {
1382 if (PyObject_HasAttrString(arg, "keys"))
1383 result = PyDict_Merge(self, arg, 1);
1384 else
1385 result = PyDict_MergeFromSeq2(self, arg, 1);
1386 }
1387 if (result == 0 && kwds != NULL)
1388 result = PyDict_Merge(self, kwds, 1);
1389 return result;
1390}
1391
1392static PyObject *
1393dict_update(PyObject *self, PyObject *args, PyObject *kwds)
1394{
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001395 if (dict_update_common(self, args, kwds, "update") != -1)
1396 Py_RETURN_NONE;
1397 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001398}
1399
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001400/* Update unconditionally replaces existing items.
1401 Merge has a 3rd argument 'override'; if set, it acts like Update,
Tim Peters1fc240e2001-10-26 05:06:50 +00001402 otherwise it leaves existing items unchanged.
1403
1404 PyDict_{Update,Merge} update/merge from a mapping object.
1405
Tim Petersf582b822001-12-11 18:51:08 +00001406 PyDict_MergeFromSeq2 updates/merges from any iterable object
Tim Peters1fc240e2001-10-26 05:06:50 +00001407 producing iterable objects of length 2.
1408*/
1409
Tim Petersf582b822001-12-11 18:51:08 +00001410int
Tim Peters1fc240e2001-10-26 05:06:50 +00001411PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
1412{
1413 PyObject *it; /* iter(seq2) */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001414 Py_ssize_t i; /* index into seq2 of current element */
Tim Peters1fc240e2001-10-26 05:06:50 +00001415 PyObject *item; /* seq2[i] */
1416 PyObject *fast; /* item as a 2-tuple or 2-list */
1417
1418 assert(d != NULL);
1419 assert(PyDict_Check(d));
1420 assert(seq2 != NULL);
1421
1422 it = PyObject_GetIter(seq2);
1423 if (it == NULL)
1424 return -1;
1425
1426 for (i = 0; ; ++i) {
1427 PyObject *key, *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001428 Py_ssize_t n;
Tim Peters1fc240e2001-10-26 05:06:50 +00001429
1430 fast = NULL;
1431 item = PyIter_Next(it);
1432 if (item == NULL) {
1433 if (PyErr_Occurred())
1434 goto Fail;
1435 break;
1436 }
1437
1438 /* Convert item to sequence, and verify length 2. */
1439 fast = PySequence_Fast(item, "");
1440 if (fast == NULL) {
1441 if (PyErr_ExceptionMatches(PyExc_TypeError))
1442 PyErr_Format(PyExc_TypeError,
1443 "cannot convert dictionary update "
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001444 "sequence element #%zd to a sequence",
Tim Peters1fc240e2001-10-26 05:06:50 +00001445 i);
1446 goto Fail;
1447 }
1448 n = PySequence_Fast_GET_SIZE(fast);
1449 if (n != 2) {
1450 PyErr_Format(PyExc_ValueError,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001451 "dictionary update sequence element #%zd "
Martin v. Löwis2c95cc62006-02-16 06:54:25 +00001452 "has length %zd; 2 is required",
Martin v. Löwise0e89f72006-02-16 06:59:22 +00001453 i, n);
Tim Peters1fc240e2001-10-26 05:06:50 +00001454 goto Fail;
1455 }
1456
1457 /* Update/merge with this (key, value) pair. */
1458 key = PySequence_Fast_GET_ITEM(fast, 0);
1459 value = PySequence_Fast_GET_ITEM(fast, 1);
1460 if (override || PyDict_GetItem(d, key) == NULL) {
1461 int status = PyDict_SetItem(d, key, value);
1462 if (status < 0)
1463 goto Fail;
1464 }
1465 Py_DECREF(fast);
1466 Py_DECREF(item);
1467 }
1468
1469 i = 0;
1470 goto Return;
1471Fail:
1472 Py_XDECREF(item);
1473 Py_XDECREF(fast);
1474 i = -1;
1475Return:
1476 Py_DECREF(it);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001477 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
Tim Peters1fc240e2001-10-26 05:06:50 +00001478}
1479
Tim Peters6d6c1a32001-08-02 04:15:00 +00001480int
1481PyDict_Update(PyObject *a, PyObject *b)
1482{
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001483 return PyDict_Merge(a, b, 1);
1484}
1485
1486int
1487PyDict_Merge(PyObject *a, PyObject *b, int override)
1488{
Tim Peters6d6c1a32001-08-02 04:15:00 +00001489 register PyDictObject *mp, *other;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001490 register Py_ssize_t i;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001491 PyDictEntry *entry;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001492
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001493 /* We accept for the argument either a concrete dictionary object,
1494 * or an abstract "mapping" object. For the former, we can do
1495 * things quite efficiently. For the latter, we only require that
1496 * PyMapping_Keys() and PyObject_GetItem() be supported.
1497 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001498 if (a == NULL || !PyDict_Check(a) || b == NULL) {
1499 PyErr_BadInternalCall();
1500 return -1;
1501 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001502 mp = (PyDictObject*)a;
Christian Heimesaf98da12008-01-27 15:18:18 +00001503 if (PyDict_Check(b)) {
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001504 other = (PyDictObject*)b;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001505 if (other == mp || other->ma_used == 0)
1506 /* a.update(a) or a.update({}); nothing to do */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001507 return 0;
Raymond Hettinger186e7392005-05-14 18:08:25 +00001508 if (mp->ma_used == 0)
1509 /* Since the target dict is empty, PyDict_GetItem()
1510 * always returns NULL. Setting override to 1
1511 * skips the unnecessary test.
1512 */
1513 override = 1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001514 /* Do one big resize at the start, rather than
1515 * incrementally resizing as we insert new items. Expect
1516 * that there will be no (or few) overlapping keys.
1517 */
1518 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
Raymond Hettingerc8d22902003-05-07 00:49:40 +00001519 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001520 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001521 }
1522 for (i = 0; i <= other->ma_mask; i++) {
1523 entry = &other->ma_table[i];
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001524 if (entry->me_value != NULL &&
1525 (override ||
1526 PyDict_GetItem(a, entry->me_key) == NULL)) {
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001527 Py_INCREF(entry->me_key);
1528 Py_INCREF(entry->me_value);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001529 if (insertdict(mp, entry->me_key,
1530 (long)entry->me_hash,
1531 entry->me_value) != 0)
1532 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001533 }
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001534 }
1535 }
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001536 else {
1537 /* Do it the generic, slower way */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001538 PyObject *keys = PyMapping_Keys(b);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001539 PyObject *iter;
1540 PyObject *key, *value;
1541 int status;
1542
1543 if (keys == NULL)
1544 /* Docstring says this is equivalent to E.keys() so
1545 * if E doesn't have a .keys() method we want
1546 * AttributeError to percolate up. Might as well
1547 * do the same for any other error.
1548 */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001549 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001550
1551 iter = PyObject_GetIter(keys);
1552 Py_DECREF(keys);
1553 if (iter == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001554 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001555
1556 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001557 if (!override && PyDict_GetItem(a, key) != NULL) {
1558 Py_DECREF(key);
1559 continue;
1560 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001561 value = PyObject_GetItem(b, key);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001562 if (value == NULL) {
1563 Py_DECREF(iter);
1564 Py_DECREF(key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001565 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001566 }
Guido van Rossum05ac6de2001-08-10 20:28:28 +00001567 status = PyDict_SetItem(a, key, value);
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001568 Py_DECREF(key);
1569 Py_DECREF(value);
1570 if (status < 0) {
1571 Py_DECREF(iter);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001572 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001573 }
1574 }
1575 Py_DECREF(iter);
1576 if (PyErr_Occurred())
1577 /* Iterator completed, via error */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001578 return -1;
Barry Warsaw66a0d1d2001-06-26 20:08:32 +00001579 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001580 return 0;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001581}
1582
1583static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001584dict_copy(register PyDictObject *mp)
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001585{
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001586 return PyDict_Copy((PyObject*)mp);
1587}
1588
1589PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001590PyDict_Copy(PyObject *o)
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001591{
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001592 PyObject *copy;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001593
1594 if (o == NULL || !PyDict_Check(o)) {
1595 PyErr_BadInternalCall();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001596 return NULL;
Jeremy Hyltona12c7a72000-03-30 22:27:31 +00001597 }
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001598 copy = PyDict_New();
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001599 if (copy == NULL)
1600 return NULL;
Raymond Hettingerebedb2f2004-03-08 04:19:01 +00001601 if (PyDict_Merge(copy, o, 1) == 0)
1602 return copy;
1603 Py_DECREF(copy);
Raymond Hettinger1356f782005-04-15 15:58:42 +00001604 return NULL;
Guido van Rossume3f5b9c1997-05-28 19:15:28 +00001605}
1606
Martin v. Löwis18e16552006-02-15 17:27:45 +00001607Py_ssize_t
Tim Peters1f5871e2000-07-04 17:44:48 +00001608PyDict_Size(PyObject *mp)
Guido van Rossum4199fac1993-11-05 10:18:44 +00001609{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001610 if (mp == NULL || !PyDict_Check(mp)) {
1611 PyErr_BadInternalCall();
Jeremy Hylton7083bb72004-02-17 20:10:11 +00001612 return -1;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001613 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001614 return ((PyDictObject *)mp)->ma_used;
Guido van Rossum4199fac1993-11-05 10:18:44 +00001615}
1616
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001617PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001618PyDict_Keys(PyObject *mp)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001619{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001620 if (mp == NULL || !PyDict_Check(mp)) {
1621 PyErr_BadInternalCall();
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001622 return NULL;
1623 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001624 return dict_keys((PyDictObject *)mp);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001625}
1626
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001627PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001628PyDict_Values(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001629{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001630 if (mp == NULL || !PyDict_Check(mp)) {
1631 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001632 return NULL;
1633 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001634 return dict_values((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001635}
1636
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001637PyObject *
Tim Peters1f5871e2000-07-04 17:44:48 +00001638PyDict_Items(PyObject *mp)
Guido van Rossum25831651993-05-19 14:50:45 +00001639{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001640 if (mp == NULL || !PyDict_Check(mp)) {
1641 PyErr_BadInternalCall();
Guido van Rossum25831651993-05-19 14:50:45 +00001642 return NULL;
1643 }
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001644 return dict_items((PyDictObject *)mp);
Guido van Rossum25831651993-05-19 14:50:45 +00001645}
1646
Tim Peterse63415e2001-05-08 04:38:29 +00001647/* Return 1 if dicts equal, 0 if not, -1 if error.
1648 * Gets out as soon as any difference is detected.
1649 * Uses only Py_EQ comparison.
1650 */
1651static int
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001652dict_equal(PyDictObject *a, PyDictObject *b)
Tim Peterse63415e2001-05-08 04:38:29 +00001653{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001654 Py_ssize_t i;
Tim Peterse63415e2001-05-08 04:38:29 +00001655
1656 if (a->ma_used != b->ma_used)
1657 /* can't be equal if # of entries differ */
1658 return 0;
Tim Peterseb28ef22001-06-02 05:27:19 +00001659
Tim Peterse63415e2001-05-08 04:38:29 +00001660 /* Same # of entries -- check all of 'em. Exit early on any diff. */
Tim Petersafb6ae82001-06-04 21:00:21 +00001661 for (i = 0; i <= a->ma_mask; i++) {
Tim Peterse63415e2001-05-08 04:38:29 +00001662 PyObject *aval = a->ma_table[i].me_value;
1663 if (aval != NULL) {
1664 int cmp;
1665 PyObject *bval;
1666 PyObject *key = a->ma_table[i].me_key;
1667 /* temporarily bump aval's refcount to ensure it stays
1668 alive until we're done with it */
1669 Py_INCREF(aval);
Guido van Rossumdc5f6b22006-08-24 21:29:26 +00001670 /* ditto for key */
1671 Py_INCREF(key);
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001672 bval = PyDict_GetItemWithError((PyObject *)b, key);
Guido van Rossumdc5f6b22006-08-24 21:29:26 +00001673 Py_DECREF(key);
Tim Peterse63415e2001-05-08 04:38:29 +00001674 if (bval == NULL) {
1675 Py_DECREF(aval);
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001676 if (PyErr_Occurred())
1677 return -1;
Tim Peterse63415e2001-05-08 04:38:29 +00001678 return 0;
1679 }
1680 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
1681 Py_DECREF(aval);
1682 if (cmp <= 0) /* error or not equal */
1683 return cmp;
1684 }
1685 }
1686 return 1;
1687 }
1688
1689static PyObject *
1690dict_richcompare(PyObject *v, PyObject *w, int op)
1691{
1692 int cmp;
1693 PyObject *res;
1694
1695 if (!PyDict_Check(v) || !PyDict_Check(w)) {
1696 res = Py_NotImplemented;
1697 }
1698 else if (op == Py_EQ || op == Py_NE) {
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001699 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
Tim Peterse63415e2001-05-08 04:38:29 +00001700 if (cmp < 0)
1701 return NULL;
1702 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
1703 }
Tim Peters4fa58bf2001-05-10 21:45:19 +00001704 else
1705 res = Py_NotImplemented;
Tim Peterse63415e2001-05-08 04:38:29 +00001706 Py_INCREF(res);
1707 return res;
1708 }
1709
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001710static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001711dict_contains(register PyDictObject *mp, PyObject *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001712{
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001713 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001714 PyDictEntry *ep;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001715
Guido van Rossum89d8c602007-09-18 17:26:56 +00001716 if (!PyUnicode_CheckExact(key) ||
1717 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001718 hash = PyObject_Hash(key);
Guido van Rossumca756f21997-01-23 19:39:29 +00001719 if (hash == -1)
1720 return NULL;
1721 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001722 ep = (mp->ma_lookup)(mp, key, hash);
1723 if (ep == NULL)
1724 return NULL;
1725 return PyBool_FromLong(ep->me_value != NULL);
Guido van Rossum4b1302b1993-03-27 18:11:32 +00001726}
1727
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001728static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001729dict_get(register PyDictObject *mp, PyObject *args)
Barry Warsawc38c5da1997-10-06 17:49:20 +00001730{
1731 PyObject *key;
Barry Warsaw320ac331997-10-20 17:26:25 +00001732 PyObject *failobj = Py_None;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001733 PyObject *val = NULL;
1734 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001735 PyDictEntry *ep;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001736
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001737 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
Barry Warsawc38c5da1997-10-06 17:49:20 +00001738 return NULL;
1739
Guido van Rossum89d8c602007-09-18 17:26:56 +00001740 if (!PyUnicode_CheckExact(key) ||
1741 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Barry Warsawc38c5da1997-10-06 17:49:20 +00001742 hash = PyObject_Hash(key);
1743 if (hash == -1)
1744 return NULL;
1745 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001746 ep = (mp->ma_lookup)(mp, key, hash);
1747 if (ep == NULL)
1748 return NULL;
1749 val = ep->me_value;
Barry Warsawc38c5da1997-10-06 17:49:20 +00001750 if (val == NULL)
1751 val = failobj;
1752 Py_INCREF(val);
1753 return val;
1754}
1755
1756
1757static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001758dict_setdefault(register PyDictObject *mp, PyObject *args)
Guido van Rossum164452c2000-08-08 16:12:54 +00001759{
1760 PyObject *key;
1761 PyObject *failobj = Py_None;
1762 PyObject *val = NULL;
1763 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001764 PyDictEntry *ep;
Guido van Rossum164452c2000-08-08 16:12:54 +00001765
Raymond Hettingerea3fdf42002-12-29 16:33:45 +00001766 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
Guido van Rossum164452c2000-08-08 16:12:54 +00001767 return NULL;
Guido van Rossum164452c2000-08-08 16:12:54 +00001768
Guido van Rossum89d8c602007-09-18 17:26:56 +00001769 if (!PyUnicode_CheckExact(key) ||
1770 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossum164452c2000-08-08 16:12:54 +00001771 hash = PyObject_Hash(key);
1772 if (hash == -1)
1773 return NULL;
1774 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001775 ep = (mp->ma_lookup)(mp, key, hash);
1776 if (ep == NULL)
1777 return NULL;
1778 val = ep->me_value;
Guido van Rossum164452c2000-08-08 16:12:54 +00001779 if (val == NULL) {
1780 val = failobj;
1781 if (PyDict_SetItem((PyObject*)mp, key, failobj))
1782 val = NULL;
1783 }
1784 Py_XINCREF(val);
1785 return val;
1786}
1787
1788
1789static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001790dict_clear(register PyDictObject *mp)
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001791{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001792 PyDict_Clear((PyObject *)mp);
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00001793 Py_RETURN_NONE;
Guido van Rossumfb8f1ca1997-03-21 21:55:12 +00001794}
1795
Guido van Rossumba6ab842000-12-12 22:02:18 +00001796static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001797dict_pop(PyDictObject *mp, PyObject *args)
Guido van Rossume027d982002-04-12 15:11:59 +00001798{
1799 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001800 PyDictEntry *ep;
Guido van Rossume027d982002-04-12 15:11:59 +00001801 PyObject *old_value, *old_key;
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001802 PyObject *key, *deflt = NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001803
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001804 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
1805 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001806 if (mp->ma_used == 0) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001807 if (deflt) {
1808 Py_INCREF(deflt);
1809 return deflt;
1810 }
Guido van Rossume027d982002-04-12 15:11:59 +00001811 PyErr_SetString(PyExc_KeyError,
1812 "pop(): dictionary is empty");
1813 return NULL;
1814 }
Guido van Rossum89d8c602007-09-18 17:26:56 +00001815 if (!PyUnicode_CheckExact(key) ||
1816 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossume027d982002-04-12 15:11:59 +00001817 hash = PyObject_Hash(key);
1818 if (hash == -1)
1819 return NULL;
1820 }
1821 ep = (mp->ma_lookup)(mp, key, hash);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001822 if (ep == NULL)
1823 return NULL;
Guido van Rossume027d982002-04-12 15:11:59 +00001824 if (ep->me_value == NULL) {
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001825 if (deflt) {
1826 Py_INCREF(deflt);
1827 return deflt;
1828 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00001829 set_key_error(key);
Guido van Rossume027d982002-04-12 15:11:59 +00001830 return NULL;
1831 }
1832 old_key = ep->me_key;
1833 Py_INCREF(dummy);
1834 ep->me_key = dummy;
1835 old_value = ep->me_value;
1836 ep->me_value = NULL;
1837 mp->ma_used--;
1838 Py_DECREF(old_key);
1839 return old_value;
1840}
1841
1842static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001843dict_popitem(PyDictObject *mp)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001844{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001845 Py_ssize_t i = 0;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001846 PyDictEntry *ep;
Guido van Rossumba6ab842000-12-12 22:02:18 +00001847 PyObject *res;
1848
Tim Petersf4b33f62001-06-02 05:42:29 +00001849 /* Allocate the result tuple before checking the size. Believe it
1850 * or not, this allocation could trigger a garbage collection which
1851 * could empty the dict, so if we checked the size first and that
1852 * happened, the result would be an infinite loop (searching for an
1853 * entry that no longer exists). Note that the usual popitem()
1854 * idiom is "while d: k, v = d.popitem()". so needing to throw the
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001855 * tuple away if the dict *is* empty isn't a significant
Tim Petersf4b33f62001-06-02 05:42:29 +00001856 * inefficiency -- possible, but unlikely in practice.
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001857 */
1858 res = PyTuple_New(2);
1859 if (res == NULL)
1860 return NULL;
Guido van Rossume04eaec2001-04-16 00:02:32 +00001861 if (mp->ma_used == 0) {
1862 Py_DECREF(res);
1863 PyErr_SetString(PyExc_KeyError,
1864 "popitem(): dictionary is empty");
1865 return NULL;
1866 }
Guido van Rossumba6ab842000-12-12 22:02:18 +00001867 /* Set ep to "the first" dict entry with a value. We abuse the hash
1868 * field of slot 0 to hold a search finger:
1869 * If slot 0 has a value, use slot 0.
1870 * Else slot 0 is being used to hold a search finger,
1871 * and we use its hash value as the first index to look.
1872 */
1873 ep = &mp->ma_table[0];
1874 if (ep->me_value == NULL) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001875 i = ep->me_hash;
Tim Petersdea48ec2001-05-22 20:40:22 +00001876 /* The hash field may be a real hash value, or it may be a
1877 * legit search finger, or it may be a once-legit search
1878 * finger that's out of bounds now because it wrapped around
1879 * or the table shrunk -- simply make sure it's in bounds now.
Guido van Rossumba6ab842000-12-12 22:02:18 +00001880 */
Tim Petersafb6ae82001-06-04 21:00:21 +00001881 if (i > mp->ma_mask || i < 1)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001882 i = 1; /* skip slot 0 */
1883 while ((ep = &mp->ma_table[i])->me_value == NULL) {
1884 i++;
Tim Petersafb6ae82001-06-04 21:00:21 +00001885 if (i > mp->ma_mask)
Guido van Rossumba6ab842000-12-12 22:02:18 +00001886 i = 1;
1887 }
1888 }
Guido van Rossuma4dd0112001-04-15 22:16:26 +00001889 PyTuple_SET_ITEM(res, 0, ep->me_key);
1890 PyTuple_SET_ITEM(res, 1, ep->me_value);
1891 Py_INCREF(dummy);
1892 ep->me_key = dummy;
1893 ep->me_value = NULL;
1894 mp->ma_used--;
1895 assert(mp->ma_table[0].me_value == NULL);
1896 mp->ma_table[0].me_hash = i + 1; /* next place to start */
Guido van Rossumba6ab842000-12-12 22:02:18 +00001897 return res;
1898}
1899
Jeremy Hylton8caad492000-06-23 14:18:11 +00001900static int
1901dict_traverse(PyObject *op, visitproc visit, void *arg)
1902{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001903 Py_ssize_t i = 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001904 PyObject *pk;
1905 PyObject *pv;
1906
1907 while (PyDict_Next(op, &i, &pk, &pv)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001908 Py_VISIT(pk);
1909 Py_VISIT(pv);
Jeremy Hylton8caad492000-06-23 14:18:11 +00001910 }
1911 return 0;
1912}
1913
1914static int
1915dict_tp_clear(PyObject *op)
1916{
1917 PyDict_Clear(op);
1918 return 0;
1919}
1920
Guido van Rossum8ce8a782007-11-01 19:42:39 +00001921static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
Guido van Rossum09e563a2001-05-01 12:10:21 +00001922
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001923static PyObject *
1924dict_sizeof(PyDictObject *mp)
1925{
1926 Py_ssize_t res;
1927
Robert Schuppenies4d45bfe2008-06-28 23:58:47 +00001928 res = sizeof(PyDictObject);
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001929 if (mp->ma_table != mp->ma_smalltable)
1930 res = res + (mp->ma_mask + 1) * sizeof(PyDictEntry);
1931 return PyLong_FromSsize_t(res);
1932}
1933
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001934PyDoc_STRVAR(contains__doc__,
1935"D.__contains__(k) -> True if D has a key k, else False");
1936
1937PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
1938
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001939PyDoc_STRVAR(sizeof__doc__,
1940"D.__sizeof__() -> size of D in memory, in bytes");
1941
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001942PyDoc_STRVAR(get__doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001943"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001944
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001945PyDoc_STRVAR(setdefault_doc__,
Guido van Rossumefae8862002-09-04 11:29:45 +00001946"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 +00001947
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001948PyDoc_STRVAR(pop__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001949"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001950If key is not found, d is returned if given, otherwise KeyError is raised");
Guido van Rossume027d982002-04-12 15:11:59 +00001951
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001952PyDoc_STRVAR(popitem__doc__,
Tim Petersf7f88b12000-12-13 23:18:45 +00001953"D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000019542-tuple; but raise KeyError if D is empty.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001955
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001956PyDoc_STRVAR(update__doc__,
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001957"D.update(E, **F) -> None. Update D from dict/iterable E and F.\n"
1958"If E has a .keys() method, does: for k in E: D[k] = E[k]\n\
1959If E lacks .keys() method, does: for (k, v) in E: D[k] = v\n\
1960In either case, this is followed by: for k in F: D[k] = F[k]");
Tim Petersf7f88b12000-12-13 23:18:45 +00001961
Raymond Hettingere33d3df2002-11-27 07:29:33 +00001962PyDoc_STRVAR(fromkeys__doc__,
1963"dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
1964v defaults to None.");
1965
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001966PyDoc_STRVAR(clear__doc__,
1967"D.clear() -> None. Remove all items from D.");
Tim Petersf7f88b12000-12-13 23:18:45 +00001968
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001969PyDoc_STRVAR(copy__doc__,
1970"D.copy() -> a shallow copy of D");
Tim Petersf7f88b12000-12-13 23:18:45 +00001971
Guido van Rossumb90c8482007-02-10 01:11:45 +00001972/* Forward */
1973static PyObject *dictkeys_new(PyObject *);
1974static PyObject *dictitems_new(PyObject *);
1975static PyObject *dictvalues_new(PyObject *);
1976
Guido van Rossum45c85d12007-07-27 16:31:40 +00001977PyDoc_STRVAR(keys__doc__,
1978 "D.keys() -> a set-like object providing a view on D's keys");
1979PyDoc_STRVAR(items__doc__,
1980 "D.items() -> a set-like object providing a view on D's items");
1981PyDoc_STRVAR(values__doc__,
1982 "D.values() -> an object providing a view on D's values");
Guido van Rossumb90c8482007-02-10 01:11:45 +00001983
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001984static PyMethodDef mapp_methods[] = {
Guido van Rossume2b70bc2006-08-18 22:13:04 +00001985 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001986 contains__doc__},
Raymond Hettinger0c669672003-12-13 13:31:55 +00001987 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
Raymond Hettinger8f5cdaa2003-12-13 11:26:12 +00001988 getitem__doc__},
Martin v. Löwis00709aa2008-06-04 14:18:43 +00001989 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
1990 sizeof__doc__},
Tim Petersf7f88b12000-12-13 23:18:45 +00001991 {"get", (PyCFunction)dict_get, METH_VARARGS,
1992 get__doc__},
1993 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
1994 setdefault_doc__},
Raymond Hettingera3e1e4c2003-03-06 23:54:28 +00001995 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
Just van Rossuma797d812002-11-23 09:45:04 +00001996 pop__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001997 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00001998 popitem__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001999 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00002000 keys__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002001 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00002002 items__doc__},
Guido van Rossumcc2b0162007-02-11 06:12:03 +00002003 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
Guido van Rossumeff072c2007-02-27 05:47:18 +00002004 values__doc__},
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002005 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002006 update__doc__},
Raymond Hettingere33d3df2002-11-27 07:29:33 +00002007 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
2008 fromkeys__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002009 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002010 clear__doc__},
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00002011 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
Tim Petersf7f88b12000-12-13 23:18:45 +00002012 copy__doc__},
2013 {NULL, NULL} /* sentinel */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002014};
2015
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002016/* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002017int
2018PyDict_Contains(PyObject *op, PyObject *key)
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002019{
2020 long hash;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002021 PyDictObject *mp = (PyDictObject *)op;
2022 PyDictEntry *ep;
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002023
Guido van Rossum89d8c602007-09-18 17:26:56 +00002024 if (!PyUnicode_CheckExact(key) ||
2025 (hash = ((PyUnicodeObject *) key)->hash) == -1) {
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002026 hash = PyObject_Hash(key);
2027 if (hash == -1)
2028 return -1;
2029 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002030 ep = (mp->ma_lookup)(mp, key, hash);
2031 return ep == NULL ? -1 : (ep->me_value != NULL);
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002032}
2033
Thomas Wouterscf297e42007-02-23 15:07:44 +00002034/* Internal version of PyDict_Contains used when the hash value is already known */
2035int
2036_PyDict_Contains(PyObject *op, PyObject *key, long hash)
2037{
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002038 PyDictObject *mp = (PyDictObject *)op;
2039 PyDictEntry *ep;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002040
2041 ep = (mp->ma_lookup)(mp, key, hash);
2042 return ep == NULL ? -1 : (ep->me_value != NULL);
2043}
2044
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002045/* Hack to implement "key in dict" */
2046static PySequenceMethods dict_as_sequence = {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002047 0, /* sq_length */
2048 0, /* sq_concat */
2049 0, /* sq_repeat */
2050 0, /* sq_item */
2051 0, /* sq_slice */
2052 0, /* sq_ass_item */
2053 0, /* sq_ass_slice */
2054 PyDict_Contains, /* sq_contains */
2055 0, /* sq_inplace_concat */
2056 0, /* sq_inplace_repeat */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002057};
2058
Guido van Rossum09e563a2001-05-01 12:10:21 +00002059static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002060dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2061{
2062 PyObject *self;
2063
2064 assert(type != NULL && type->tp_alloc != NULL);
2065 self = type->tp_alloc(type, 0);
2066 if (self != NULL) {
2067 PyDictObject *d = (PyDictObject *)self;
2068 /* It's guaranteed that tp->alloc zeroed out the struct. */
2069 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
2070 INIT_NONZERO_DICT_SLOTS(d);
Guido van Rossum89d8c602007-09-18 17:26:56 +00002071 d->ma_lookup = lookdict_unicode;
Antoine Pitrou3a652b12009-03-23 18:52:06 +00002072 /* The object has been implicitely tracked by tp_alloc */
2073 if (type == &PyDict_Type)
2074 _PyObject_GC_UNTRACK(d);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002075#ifdef SHOW_CONVERSION_COUNTS
2076 ++created;
2077#endif
Antoine Pitrou3a652b12009-03-23 18:52:06 +00002078#ifdef SHOW_TRACK_COUNT
2079 if (_PyObject_GC_IS_TRACKED(d))
2080 count_tracked++;
2081 else
2082 count_untracked++;
2083#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00002084 }
2085 return self;
2086}
2087
Tim Peters25786c02001-09-02 08:22:48 +00002088static int
2089dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2090{
Raymond Hettinger31017ae2004-03-04 08:25:44 +00002091 return dict_update_common(self, args, kwds, "dict");
Tim Peters25786c02001-09-02 08:22:48 +00002092}
2093
Tim Peters6d6c1a32001-08-02 04:15:00 +00002094static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002095dict_iter(PyDictObject *dict)
Guido van Rossum09e563a2001-05-01 12:10:21 +00002096{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002097 return dictiter_new(dict, &PyDictIterKey_Type);
Guido van Rossum09e563a2001-05-01 12:10:21 +00002098}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002099
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002100PyDoc_STRVAR(dictionary_doc,
Tim Petersa427a2b2001-10-29 22:25:45 +00002101"dict() -> new empty dictionary.\n"
2102"dict(mapping) -> new dictionary initialized from a mapping object's\n"
Tim Peters1fc240e2001-10-26 05:06:50 +00002103" (key, value) pairs.\n"
Tim Petersa427a2b2001-10-29 22:25:45 +00002104"dict(seq) -> new dictionary initialized as if via:\n"
Tim Peters4d859532001-10-27 18:27:48 +00002105" d = {}\n"
2106" for k, v in seq:\n"
Just van Rossuma797d812002-11-23 09:45:04 +00002107" d[k] = v\n"
2108"dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
2109" in the keyword argument list. For example: dict(one=1, two=2)");
Tim Peters25786c02001-09-02 08:22:48 +00002110
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002111PyTypeObject PyDict_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002112 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Tim Petersa427a2b2001-10-29 22:25:45 +00002113 "dict",
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002114 sizeof(PyDictObject),
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002115 0,
Guido van Rossumb9324202001-01-18 00:39:02 +00002116 (destructor)dict_dealloc, /* tp_dealloc */
Guido van Rossum04dbf3b2007-08-07 19:51:00 +00002117 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002118 0, /* tp_getattr */
Guido van Rossumb9324202001-01-18 00:39:02 +00002119 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00002120 0, /* tp_reserved */
Guido van Rossumb9324202001-01-18 00:39:02 +00002121 (reprfunc)dict_repr, /* tp_repr */
2122 0, /* tp_as_number */
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +00002123 &dict_as_sequence, /* tp_as_sequence */
Guido van Rossumb9324202001-01-18 00:39:02 +00002124 &dict_as_mapping, /* tp_as_mapping */
Nick Coghland1abd252008-07-15 15:46:38 +00002125 (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
Guido van Rossumb9324202001-01-18 00:39:02 +00002126 0, /* tp_call */
2127 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002128 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumb9324202001-01-18 00:39:02 +00002129 0, /* tp_setattro */
2130 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002131 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Thomas Wouters27d517b2007-02-25 20:39:11 +00002132 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
Tim Peters25786c02001-09-02 08:22:48 +00002133 dictionary_doc, /* tp_doc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002134 dict_traverse, /* tp_traverse */
2135 dict_tp_clear, /* tp_clear */
Tim Peterse63415e2001-05-08 04:38:29 +00002136 dict_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002137 0, /* tp_weaklistoffset */
Guido van Rossum09e563a2001-05-01 12:10:21 +00002138 (getiterfunc)dict_iter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002139 0, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002140 mapp_methods, /* tp_methods */
2141 0, /* tp_members */
2142 0, /* tp_getset */
2143 0, /* tp_base */
2144 0, /* tp_dict */
2145 0, /* tp_descr_get */
2146 0, /* tp_descr_set */
2147 0, /* tp_dictoffset */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002148 dict_init, /* tp_init */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002149 PyType_GenericAlloc, /* tp_alloc */
2150 dict_new, /* tp_new */
Neil Schemenauer6189b892002-04-12 02:43:00 +00002151 PyObject_GC_Del, /* tp_free */
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002152};
2153
Guido van Rossum3cca2451997-05-16 14:23:33 +00002154/* For backward compatibility with old dictionary interface */
2155
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002156PyObject *
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002157PyDict_GetItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002158{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002159 PyObject *kv, *rv;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002160 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002161 if (kv == NULL)
2162 return NULL;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002163 rv = PyDict_GetItem(v, kv);
2164 Py_DECREF(kv);
2165 return rv;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002166}
2167
2168int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002169PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002170{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002171 PyObject *kv;
2172 int err;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002173 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002174 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002175 return -1;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002176 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
Guido van Rossum3cca2451997-05-16 14:23:33 +00002177 err = PyDict_SetItem(v, kv, item);
2178 Py_DECREF(kv);
2179 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002180}
2181
2182int
Martin v. Löwis32b4a1b2002-12-11 13:21:12 +00002183PyDict_DelItemString(PyObject *v, const char *key)
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002184{
Guido van Rossum3cca2451997-05-16 14:23:33 +00002185 PyObject *kv;
2186 int err;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002187 kv = PyUnicode_FromString(key);
Guido van Rossum3cca2451997-05-16 14:23:33 +00002188 if (kv == NULL)
Guido van Rossum037b2201997-05-20 18:35:19 +00002189 return -1;
Guido van Rossum3cca2451997-05-16 14:23:33 +00002190 err = PyDict_DelItem(v, kv);
2191 Py_DECREF(kv);
2192 return err;
Guido van Rossum4b1302b1993-03-27 18:11:32 +00002193}
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002194
Raymond Hettinger019a1482004-03-18 02:41:19 +00002195/* Dictionary iterator types */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002196
2197typedef struct {
2198 PyObject_HEAD
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002199 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002200 Py_ssize_t di_used;
2201 Py_ssize_t di_pos;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002202 PyObject* di_result; /* reusable result tuple for iteritems */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002203 Py_ssize_t len;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002204} dictiterobject;
2205
2206static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002207dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002208{
2209 dictiterobject *di;
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002210 di = PyObject_GC_New(dictiterobject, itertype);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002211 if (di == NULL)
2212 return NULL;
2213 Py_INCREF(dict);
2214 di->di_dict = dict;
Guido van Rossumb1f35bf2001-05-02 15:13:44 +00002215 di->di_used = dict->ma_used;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002216 di->di_pos = 0;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002217 di->len = dict->ma_used;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002218 if (itertype == &PyDictIterItem_Type) {
2219 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
2220 if (di->di_result == NULL) {
2221 Py_DECREF(di);
2222 return NULL;
2223 }
2224 }
2225 else
2226 di->di_result = NULL;
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002227 _PyObject_GC_TRACK(di);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002228 return (PyObject *)di;
2229}
2230
2231static void
2232dictiter_dealloc(dictiterobject *di)
2233{
Guido van Rossum2147df72002-07-16 20:30:22 +00002234 Py_XDECREF(di->di_dict);
Raymond Hettinger019a1482004-03-18 02:41:19 +00002235 Py_XDECREF(di->di_result);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002236 PyObject_GC_Del(di);
2237}
2238
2239static int
2240dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
2241{
2242 Py_VISIT(di->di_dict);
2243 Py_VISIT(di->di_result);
2244 return 0;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002245}
2246
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002247static PyObject *
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002248dictiter_len(dictiterobject *di)
2249{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002250 Py_ssize_t len = 0;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +00002251 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002252 len = di->len;
Christian Heimes217cfd12007-12-02 14:31:20 +00002253 return PyLong_FromSize_t(len);
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002254}
2255
Guido van Rossumb90c8482007-02-10 01:11:45 +00002256PyDoc_STRVAR(length_hint_doc,
2257 "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002258
2259static PyMethodDef dictiter_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002260 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
2261 length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002262 {NULL, NULL} /* sentinel */
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002263};
2264
Raymond Hettinger019a1482004-03-18 02:41:19 +00002265static PyObject *dictiter_iternextkey(dictiterobject *di)
Guido van Rossum213c7a62001-04-23 14:08:49 +00002266{
Raymond Hettinger019a1482004-03-18 02:41:19 +00002267 PyObject *key;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002268 register Py_ssize_t i, mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002269 register PyDictEntry *ep;
2270 PyDictObject *d = di->di_dict;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002271
Raymond Hettinger019a1482004-03-18 02:41:19 +00002272 if (d == NULL)
Guido van Rossum2147df72002-07-16 20:30:22 +00002273 return NULL;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002274 assert (PyDict_Check(d));
Guido van Rossum2147df72002-07-16 20:30:22 +00002275
Raymond Hettinger019a1482004-03-18 02:41:19 +00002276 if (di->di_used != d->ma_used) {
Guido van Rossum213c7a62001-04-23 14:08:49 +00002277 PyErr_SetString(PyExc_RuntimeError,
2278 "dictionary changed size during iteration");
Guido van Rossum2147df72002-07-16 20:30:22 +00002279 di->di_used = -1; /* Make this state sticky */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002280 return NULL;
2281 }
Guido van Rossum2147df72002-07-16 20:30:22 +00002282
Raymond Hettinger019a1482004-03-18 02:41:19 +00002283 i = di->di_pos;
2284 if (i < 0)
2285 goto fail;
2286 ep = d->ma_table;
2287 mask = d->ma_mask;
2288 while (i <= mask && ep[i].me_value == NULL)
2289 i++;
2290 di->di_pos = i+1;
2291 if (i > mask)
2292 goto fail;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002293 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002294 key = ep[i].me_key;
2295 Py_INCREF(key);
2296 return key;
2297
2298fail:
2299 Py_DECREF(d);
Guido van Rossum2147df72002-07-16 20:30:22 +00002300 di->di_dict = NULL;
Guido van Rossum213c7a62001-04-23 14:08:49 +00002301 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002302}
2303
Raymond Hettinger019a1482004-03-18 02:41:19 +00002304PyTypeObject PyDictIterKey_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002305 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Christian Heimesf83be4e2007-11-28 09:44:38 +00002306 "dict_keyiterator", /* tp_name */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002307 sizeof(dictiterobject), /* tp_basicsize */
2308 0, /* tp_itemsize */
2309 /* methods */
2310 (destructor)dictiter_dealloc, /* tp_dealloc */
2311 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002312 0, /* tp_getattr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002313 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00002314 0, /* tp_reserved */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002315 0, /* tp_repr */
2316 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002317 0, /* tp_as_sequence */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002318 0, /* tp_as_mapping */
2319 0, /* tp_hash */
2320 0, /* tp_call */
2321 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002322 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002323 0, /* tp_setattro */
2324 0, /* tp_as_buffer */
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002325 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002326 0, /* tp_doc */
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002327 (traverseproc)dictiter_traverse, /* tp_traverse */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002328 0, /* tp_clear */
2329 0, /* tp_richcompare */
2330 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00002331 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002332 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002333 dictiter_methods, /* tp_methods */
2334 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002335};
2336
2337static PyObject *dictiter_iternextvalue(dictiterobject *di)
2338{
2339 PyObject *value;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002340 register Py_ssize_t i, mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002341 register PyDictEntry *ep;
2342 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002343
2344 if (d == NULL)
2345 return NULL;
2346 assert (PyDict_Check(d));
2347
2348 if (di->di_used != d->ma_used) {
2349 PyErr_SetString(PyExc_RuntimeError,
2350 "dictionary changed size during iteration");
2351 di->di_used = -1; /* Make this state sticky */
2352 return NULL;
2353 }
2354
2355 i = di->di_pos;
Guido van Rossum09240f62004-03-20 19:11:58 +00002356 mask = d->ma_mask;
2357 if (i < 0 || i > mask)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002358 goto fail;
2359 ep = d->ma_table;
Guido van Rossum09240f62004-03-20 19:11:58 +00002360 while ((value=ep[i].me_value) == NULL) {
Raymond Hettinger019a1482004-03-18 02:41:19 +00002361 i++;
Guido van Rossum09240f62004-03-20 19:11:58 +00002362 if (i > mask)
2363 goto fail;
2364 }
Raymond Hettinger019a1482004-03-18 02:41:19 +00002365 di->di_pos = i+1;
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002366 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002367 Py_INCREF(value);
2368 return value;
2369
2370fail:
2371 Py_DECREF(d);
2372 di->di_dict = NULL;
2373 return NULL;
2374}
2375
2376PyTypeObject PyDictIterValue_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002377 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Christian Heimesf83be4e2007-11-28 09:44:38 +00002378 "dict_valueiterator", /* tp_name */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002379 sizeof(dictiterobject), /* tp_basicsize */
2380 0, /* tp_itemsize */
2381 /* methods */
2382 (destructor)dictiter_dealloc, /* tp_dealloc */
2383 0, /* tp_print */
2384 0, /* tp_getattr */
2385 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00002386 0, /* tp_reserved */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002387 0, /* tp_repr */
2388 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002389 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002390 0, /* tp_as_mapping */
2391 0, /* tp_hash */
2392 0, /* tp_call */
2393 0, /* tp_str */
2394 PyObject_GenericGetAttr, /* tp_getattro */
2395 0, /* tp_setattro */
2396 0, /* tp_as_buffer */
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002397 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002398 0, /* tp_doc */
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002399 (traverseproc)dictiter_traverse, /* tp_traverse */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002400 0, /* tp_clear */
2401 0, /* tp_richcompare */
2402 0, /* tp_weaklistoffset */
2403 PyObject_SelfIter, /* tp_iter */
2404 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002405 dictiter_methods, /* tp_methods */
2406 0,
Raymond Hettinger019a1482004-03-18 02:41:19 +00002407};
2408
2409static PyObject *dictiter_iternextitem(dictiterobject *di)
2410{
2411 PyObject *key, *value, *result = di->di_result;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002412 register Py_ssize_t i, mask;
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002413 register PyDictEntry *ep;
2414 PyDictObject *d = di->di_dict;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002415
2416 if (d == NULL)
2417 return NULL;
2418 assert (PyDict_Check(d));
2419
2420 if (di->di_used != d->ma_used) {
2421 PyErr_SetString(PyExc_RuntimeError,
2422 "dictionary changed size during iteration");
2423 di->di_used = -1; /* Make this state sticky */
2424 return NULL;
2425 }
2426
2427 i = di->di_pos;
2428 if (i < 0)
2429 goto fail;
2430 ep = d->ma_table;
2431 mask = d->ma_mask;
2432 while (i <= mask && ep[i].me_value == NULL)
2433 i++;
2434 di->di_pos = i+1;
2435 if (i > mask)
2436 goto fail;
2437
2438 if (result->ob_refcnt == 1) {
2439 Py_INCREF(result);
2440 Py_DECREF(PyTuple_GET_ITEM(result, 0));
2441 Py_DECREF(PyTuple_GET_ITEM(result, 1));
2442 } else {
2443 result = PyTuple_New(2);
Tim Peters60b29962006-01-01 01:19:23 +00002444 if (result == NULL)
Raymond Hettinger019a1482004-03-18 02:41:19 +00002445 return NULL;
2446 }
Raymond Hettinger0ce6dc82004-03-18 08:38:00 +00002447 di->len--;
Raymond Hettinger019a1482004-03-18 02:41:19 +00002448 key = ep[i].me_key;
2449 value = ep[i].me_value;
2450 Py_INCREF(key);
2451 Py_INCREF(value);
2452 PyTuple_SET_ITEM(result, 0, key);
2453 PyTuple_SET_ITEM(result, 1, value);
2454 return result;
2455
2456fail:
2457 Py_DECREF(d);
2458 di->di_dict = NULL;
2459 return NULL;
2460}
2461
2462PyTypeObject PyDictIterItem_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002463 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Christian Heimesf83be4e2007-11-28 09:44:38 +00002464 "dict_itemiterator", /* tp_name */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002465 sizeof(dictiterobject), /* tp_basicsize */
2466 0, /* tp_itemsize */
2467 /* methods */
2468 (destructor)dictiter_dealloc, /* tp_dealloc */
2469 0, /* tp_print */
2470 0, /* tp_getattr */
2471 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00002472 0, /* tp_reserved */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002473 0, /* tp_repr */
2474 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002475 0, /* tp_as_sequence */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002476 0, /* tp_as_mapping */
2477 0, /* tp_hash */
2478 0, /* tp_call */
2479 0, /* tp_str */
2480 PyObject_GenericGetAttr, /* tp_getattro */
2481 0, /* tp_setattro */
2482 0, /* tp_as_buffer */
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002483 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002484 0, /* tp_doc */
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002485 (traverseproc)dictiter_traverse, /* tp_traverse */
Raymond Hettinger019a1482004-03-18 02:41:19 +00002486 0, /* tp_clear */
2487 0, /* tp_richcompare */
2488 0, /* tp_weaklistoffset */
2489 PyObject_SelfIter, /* tp_iter */
2490 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +00002491 dictiter_methods, /* tp_methods */
2492 0,
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002493};
Guido van Rossumb90c8482007-02-10 01:11:45 +00002494
2495
Guido van Rossum3ac67412007-02-10 18:55:06 +00002496/***********************************************/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002497/* View objects for keys(), items(), values(). */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002498/***********************************************/
2499
Guido van Rossumb90c8482007-02-10 01:11:45 +00002500/* The instance lay-out is the same for all three; but the type differs. */
2501
2502typedef struct {
2503 PyObject_HEAD
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002504 PyDictObject *dv_dict;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002505} dictviewobject;
2506
2507
2508static void
Guido van Rossum3ac67412007-02-10 18:55:06 +00002509dictview_dealloc(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002510{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002511 Py_XDECREF(dv->dv_dict);
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002512 PyObject_GC_Del(dv);
2513}
2514
2515static int
2516dictview_traverse(dictviewobject *dv, visitproc visit, void *arg)
2517{
2518 Py_VISIT(dv->dv_dict);
2519 return 0;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002520}
2521
Guido van Rossum83825ac2007-02-10 04:54:19 +00002522static Py_ssize_t
Guido van Rossum3ac67412007-02-10 18:55:06 +00002523dictview_len(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002524{
2525 Py_ssize_t len = 0;
Guido van Rossum3ac67412007-02-10 18:55:06 +00002526 if (dv->dv_dict != NULL)
2527 len = dv->dv_dict->ma_used;
Guido van Rossum83825ac2007-02-10 04:54:19 +00002528 return len;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002529}
2530
2531static PyObject *
2532dictview_new(PyObject *dict, PyTypeObject *type)
2533{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002534 dictviewobject *dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002535 if (dict == NULL) {
2536 PyErr_BadInternalCall();
2537 return NULL;
2538 }
2539 if (!PyDict_Check(dict)) {
2540 /* XXX Get rid of this restriction later */
2541 PyErr_Format(PyExc_TypeError,
2542 "%s() requires a dict argument, not '%s'",
2543 type->tp_name, dict->ob_type->tp_name);
2544 return NULL;
2545 }
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002546 dv = PyObject_GC_New(dictviewobject, type);
Guido van Rossum3ac67412007-02-10 18:55:06 +00002547 if (dv == NULL)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002548 return NULL;
2549 Py_INCREF(dict);
Guido van Rossum8ce8a782007-11-01 19:42:39 +00002550 dv->dv_dict = (PyDictObject *)dict;
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002551 _PyObject_GC_TRACK(dv);
Guido van Rossum3ac67412007-02-10 18:55:06 +00002552 return (PyObject *)dv;
Guido van Rossumb90c8482007-02-10 01:11:45 +00002553}
2554
Neal Norwitze36f2ba2007-02-26 23:12:28 +00002555/* TODO(guido): The views objects are not complete:
2556
2557 * support more set operations
2558 * support arbitrary mappings?
2559 - either these should be static or exported in dictobject.h
2560 - if public then they should probably be in builtins
2561*/
2562
Guido van Rossumaac530c2007-08-24 22:33:45 +00002563/* Return 1 if self is a subset of other, iterating over self;
2564 0 if not; -1 if an error occurred. */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002565static int
2566all_contained_in(PyObject *self, PyObject *other)
2567{
2568 PyObject *iter = PyObject_GetIter(self);
2569 int ok = 1;
2570
2571 if (iter == NULL)
2572 return -1;
2573 for (;;) {
2574 PyObject *next = PyIter_Next(iter);
2575 if (next == NULL) {
2576 if (PyErr_Occurred())
2577 ok = -1;
2578 break;
2579 }
2580 ok = PySequence_Contains(other, next);
2581 Py_DECREF(next);
2582 if (ok <= 0)
2583 break;
2584 }
2585 Py_DECREF(iter);
2586 return ok;
2587}
2588
2589static PyObject *
2590dictview_richcompare(PyObject *self, PyObject *other, int op)
2591{
Guido van Rossumaac530c2007-08-24 22:33:45 +00002592 Py_ssize_t len_self, len_other;
2593 int ok;
2594 PyObject *result;
2595
Guido van Rossumd9214d12007-02-12 02:23:40 +00002596 assert(self != NULL);
2597 assert(PyDictViewSet_Check(self));
2598 assert(other != NULL);
Guido van Rossumd9214d12007-02-12 02:23:40 +00002599
Guido van Rossumaac530c2007-08-24 22:33:45 +00002600 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other)) {
Guido van Rossumd9214d12007-02-12 02:23:40 +00002601 Py_INCREF(Py_NotImplemented);
2602 return Py_NotImplemented;
2603 }
Guido van Rossumaac530c2007-08-24 22:33:45 +00002604
2605 len_self = PyObject_Size(self);
2606 if (len_self < 0)
2607 return NULL;
2608 len_other = PyObject_Size(other);
2609 if (len_other < 0)
2610 return NULL;
2611
2612 ok = 0;
2613 switch(op) {
2614
2615 case Py_NE:
2616 case Py_EQ:
2617 if (len_self == len_other)
2618 ok = all_contained_in(self, other);
2619 if (op == Py_NE && ok >= 0)
2620 ok = !ok;
2621 break;
2622
2623 case Py_LT:
2624 if (len_self < len_other)
2625 ok = all_contained_in(self, other);
2626 break;
2627
2628 case Py_LE:
2629 if (len_self <= len_other)
2630 ok = all_contained_in(self, other);
2631 break;
2632
2633 case Py_GT:
2634 if (len_self > len_other)
2635 ok = all_contained_in(other, self);
2636 break;
2637
2638 case Py_GE:
2639 if (len_self >= len_other)
2640 ok = all_contained_in(other, self);
2641 break;
2642
2643 }
2644 if (ok < 0)
2645 return NULL;
2646 result = ok ? Py_True : Py_False;
2647 Py_INCREF(result);
2648 return result;
Guido van Rossumd9214d12007-02-12 02:23:40 +00002649}
2650
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00002651static PyObject *
2652dictview_repr(dictviewobject *dv)
2653{
2654 PyObject *seq;
2655 PyObject *result;
2656
2657 seq = PySequence_List((PyObject *)dv);
2658 if (seq == NULL)
2659 return NULL;
2660
2661 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
2662 Py_DECREF(seq);
2663 return result;
2664}
2665
Guido van Rossum3ac67412007-02-10 18:55:06 +00002666/*** dict_keys ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002667
2668static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002669dictkeys_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002670{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002671 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002672 Py_RETURN_NONE;
2673 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002674 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
2675}
2676
2677static int
2678dictkeys_contains(dictviewobject *dv, PyObject *obj)
2679{
2680 if (dv->dv_dict == NULL)
2681 return 0;
2682 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002683}
2684
Guido van Rossum83825ac2007-02-10 04:54:19 +00002685static PySequenceMethods dictkeys_as_sequence = {
2686 (lenfunc)dictview_len, /* sq_length */
2687 0, /* sq_concat */
2688 0, /* sq_repeat */
2689 0, /* sq_item */
2690 0, /* sq_slice */
2691 0, /* sq_ass_item */
2692 0, /* sq_ass_slice */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002693 (objobjproc)dictkeys_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002694};
2695
Guido van Rossum523259b2007-08-24 23:41:22 +00002696static PyObject*
2697dictviews_sub(PyObject* self, PyObject *other)
2698{
2699 PyObject *result = PySet_New(self);
2700 PyObject *tmp;
2701 if (result == NULL)
2702 return NULL;
2703
2704 tmp = PyObject_CallMethod(result, "difference_update", "O", other);
2705 if (tmp == NULL) {
2706 Py_DECREF(result);
2707 return NULL;
2708 }
2709
2710 Py_DECREF(tmp);
2711 return result;
2712}
2713
2714static PyObject*
2715dictviews_and(PyObject* self, PyObject *other)
2716{
2717 PyObject *result = PySet_New(self);
2718 PyObject *tmp;
2719 if (result == NULL)
2720 return NULL;
2721
2722 tmp = PyObject_CallMethod(result, "intersection_update", "O", other);
2723 if (tmp == NULL) {
2724 Py_DECREF(result);
2725 return NULL;
2726 }
2727
2728 Py_DECREF(tmp);
2729 return result;
2730}
2731
2732static PyObject*
2733dictviews_or(PyObject* self, PyObject *other)
2734{
2735 PyObject *result = PySet_New(self);
2736 PyObject *tmp;
2737 if (result == NULL)
2738 return NULL;
2739
2740 tmp = PyObject_CallMethod(result, "update", "O", other);
2741 if (tmp == NULL) {
2742 Py_DECREF(result);
2743 return NULL;
2744 }
2745
2746 Py_DECREF(tmp);
2747 return result;
2748}
2749
2750static PyObject*
2751dictviews_xor(PyObject* self, PyObject *other)
2752{
2753 PyObject *result = PySet_New(self);
2754 PyObject *tmp;
2755 if (result == NULL)
2756 return NULL;
2757
2758 tmp = PyObject_CallMethod(result, "symmetric_difference_update", "O",
2759 other);
2760 if (tmp == NULL) {
2761 Py_DECREF(result);
2762 return NULL;
2763 }
2764
2765 Py_DECREF(tmp);
2766 return result;
2767}
2768
2769static PyNumberMethods dictviews_as_number = {
2770 0, /*nb_add*/
2771 (binaryfunc)dictviews_sub, /*nb_subtract*/
2772 0, /*nb_multiply*/
2773 0, /*nb_remainder*/
2774 0, /*nb_divmod*/
2775 0, /*nb_power*/
2776 0, /*nb_negative*/
2777 0, /*nb_positive*/
2778 0, /*nb_absolute*/
2779 0, /*nb_bool*/
2780 0, /*nb_invert*/
2781 0, /*nb_lshift*/
2782 0, /*nb_rshift*/
2783 (binaryfunc)dictviews_and, /*nb_and*/
2784 (binaryfunc)dictviews_xor, /*nb_xor*/
2785 (binaryfunc)dictviews_or, /*nb_or*/
2786};
2787
Guido van Rossumb90c8482007-02-10 01:11:45 +00002788static PyMethodDef dictkeys_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002789 {NULL, NULL} /* sentinel */
2790};
2791
2792PyTypeObject PyDictKeys_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002793 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002794 "dict_keys", /* tp_name */
2795 sizeof(dictviewobject), /* tp_basicsize */
2796 0, /* tp_itemsize */
2797 /* methods */
2798 (destructor)dictview_dealloc, /* tp_dealloc */
2799 0, /* tp_print */
2800 0, /* tp_getattr */
2801 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00002802 0, /* tp_reserved */
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00002803 (reprfunc)dictview_repr, /* tp_repr */
Guido van Rossum523259b2007-08-24 23:41:22 +00002804 &dictviews_as_number, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002805 &dictkeys_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002806 0, /* tp_as_mapping */
2807 0, /* tp_hash */
2808 0, /* tp_call */
2809 0, /* tp_str */
2810 PyObject_GenericGetAttr, /* tp_getattro */
2811 0, /* tp_setattro */
2812 0, /* tp_as_buffer */
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002813 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002814 0, /* tp_doc */
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002815 (traverseproc)dictview_traverse, /* tp_traverse */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002816 0, /* tp_clear */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002817 dictview_richcompare, /* tp_richcompare */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002818 0, /* tp_weaklistoffset */
2819 (getiterfunc)dictkeys_iter, /* tp_iter */
2820 0, /* tp_iternext */
2821 dictkeys_methods, /* tp_methods */
2822 0,
2823};
2824
2825static PyObject *
2826dictkeys_new(PyObject *dict)
2827{
2828 return dictview_new(dict, &PyDictKeys_Type);
2829}
2830
Guido van Rossum3ac67412007-02-10 18:55:06 +00002831/*** dict_items ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002832
2833static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002834dictitems_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002835{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002836 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002837 Py_RETURN_NONE;
2838 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002839 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
2840}
2841
2842static int
2843dictitems_contains(dictviewobject *dv, PyObject *obj)
2844{
2845 PyObject *key, *value, *found;
2846 if (dv->dv_dict == NULL)
2847 return 0;
2848 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
2849 return 0;
2850 key = PyTuple_GET_ITEM(obj, 0);
2851 value = PyTuple_GET_ITEM(obj, 1);
2852 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
2853 if (found == NULL) {
2854 if (PyErr_Occurred())
2855 return -1;
2856 return 0;
2857 }
2858 return PyObject_RichCompareBool(value, found, Py_EQ);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002859}
2860
Guido van Rossum83825ac2007-02-10 04:54:19 +00002861static PySequenceMethods dictitems_as_sequence = {
2862 (lenfunc)dictview_len, /* sq_length */
2863 0, /* sq_concat */
2864 0, /* sq_repeat */
2865 0, /* sq_item */
2866 0, /* sq_slice */
2867 0, /* sq_ass_item */
2868 0, /* sq_ass_slice */
Guido van Rossum3ac67412007-02-10 18:55:06 +00002869 (objobjproc)dictitems_contains, /* sq_contains */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002870};
2871
Guido van Rossumb90c8482007-02-10 01:11:45 +00002872static PyMethodDef dictitems_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002873 {NULL, NULL} /* sentinel */
2874};
2875
2876PyTypeObject PyDictItems_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002877 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002878 "dict_items", /* tp_name */
2879 sizeof(dictviewobject), /* tp_basicsize */
2880 0, /* tp_itemsize */
2881 /* methods */
2882 (destructor)dictview_dealloc, /* tp_dealloc */
2883 0, /* tp_print */
2884 0, /* tp_getattr */
2885 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00002886 0, /* tp_reserved */
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00002887 (reprfunc)dictview_repr, /* tp_repr */
Guido van Rossum523259b2007-08-24 23:41:22 +00002888 &dictviews_as_number, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002889 &dictitems_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002890 0, /* tp_as_mapping */
2891 0, /* tp_hash */
2892 0, /* tp_call */
2893 0, /* tp_str */
2894 PyObject_GenericGetAttr, /* tp_getattro */
2895 0, /* tp_setattro */
2896 0, /* tp_as_buffer */
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002897 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002898 0, /* tp_doc */
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002899 (traverseproc)dictview_traverse, /* tp_traverse */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002900 0, /* tp_clear */
Guido van Rossumd9214d12007-02-12 02:23:40 +00002901 dictview_richcompare, /* tp_richcompare */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002902 0, /* tp_weaklistoffset */
2903 (getiterfunc)dictitems_iter, /* tp_iter */
2904 0, /* tp_iternext */
2905 dictitems_methods, /* tp_methods */
2906 0,
2907};
2908
2909static PyObject *
2910dictitems_new(PyObject *dict)
2911{
2912 return dictview_new(dict, &PyDictItems_Type);
2913}
2914
Guido van Rossum3ac67412007-02-10 18:55:06 +00002915/*** dict_values ***/
Guido van Rossumb90c8482007-02-10 01:11:45 +00002916
2917static PyObject *
Guido van Rossum3ac67412007-02-10 18:55:06 +00002918dictvalues_iter(dictviewobject *dv)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002919{
Guido van Rossum3ac67412007-02-10 18:55:06 +00002920 if (dv->dv_dict == NULL) {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002921 Py_RETURN_NONE;
2922 }
Guido van Rossum3ac67412007-02-10 18:55:06 +00002923 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
Guido van Rossumb90c8482007-02-10 01:11:45 +00002924}
2925
Guido van Rossum83825ac2007-02-10 04:54:19 +00002926static PySequenceMethods dictvalues_as_sequence = {
2927 (lenfunc)dictview_len, /* sq_length */
2928 0, /* sq_concat */
2929 0, /* sq_repeat */
2930 0, /* sq_item */
2931 0, /* sq_slice */
2932 0, /* sq_ass_item */
2933 0, /* sq_ass_slice */
2934 (objobjproc)0, /* sq_contains */
2935};
2936
Guido van Rossumb90c8482007-02-10 01:11:45 +00002937static PyMethodDef dictvalues_methods[] = {
Guido van Rossumb90c8482007-02-10 01:11:45 +00002938 {NULL, NULL} /* sentinel */
2939};
2940
2941PyTypeObject PyDictValues_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002942 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumb90c8482007-02-10 01:11:45 +00002943 "dict_values", /* tp_name */
2944 sizeof(dictviewobject), /* tp_basicsize */
2945 0, /* tp_itemsize */
2946 /* methods */
2947 (destructor)dictview_dealloc, /* tp_dealloc */
2948 0, /* tp_print */
2949 0, /* tp_getattr */
2950 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00002951 0, /* tp_reserved */
Raymond Hettingerb0d56af2009-03-03 10:52:49 +00002952 (reprfunc)dictview_repr, /* tp_repr */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002953 0, /* tp_as_number */
Guido van Rossum83825ac2007-02-10 04:54:19 +00002954 &dictvalues_as_sequence, /* tp_as_sequence */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002955 0, /* tp_as_mapping */
2956 0, /* tp_hash */
2957 0, /* tp_call */
2958 0, /* tp_str */
2959 PyObject_GenericGetAttr, /* tp_getattro */
2960 0, /* tp_setattro */
2961 0, /* tp_as_buffer */
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002962 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002963 0, /* tp_doc */
Antoine Pitrou7ddda782009-01-01 15:35:33 +00002964 (traverseproc)dictview_traverse, /* tp_traverse */
Guido van Rossumb90c8482007-02-10 01:11:45 +00002965 0, /* tp_clear */
2966 0, /* tp_richcompare */
2967 0, /* tp_weaklistoffset */
2968 (getiterfunc)dictvalues_iter, /* tp_iter */
2969 0, /* tp_iternext */
2970 dictvalues_methods, /* tp_methods */
2971 0,
2972};
2973
2974static PyObject *
2975dictvalues_new(PyObject *dict)
2976{
2977 return dictview_new(dict, &PyDictValues_Type);
2978}