annotate Objects/dictobject.c @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents df3923c36ec5
children 0265cdc8d5bb 0e498a9fde3c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1
10461
4abc5abaec9a Vladimir Marangozov contributed updated comments.
Guido van Rossum <guido@python.org>
parents: 9487
diff changeset
2 /* Dictionary object implementation using a hash table */
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1256
diff changeset
3
28946
dbdeb734ff97 Add a reference to dictnotes.txt. It does no good if you don't know it's
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
4 /* The distribution includes a separate file, Objects/dictnotes.txt,
36164
50886d42b444 Fixed English in a comment; trimmed trailing whitespace;
Tim Peters <tim.peters@gmail.com>
parents: 35636
diff changeset
5 describing explorations into dictionary design and optimization.
28946
dbdeb734ff97 Add a reference to dictnotes.txt. It does no good if you don't know it's
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
6 It covers typical dictionary use patterns, the parameters for
dbdeb734ff97 Add a reference to dictnotes.txt. It does no good if you don't know it's
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
7 tuning dictionaries, and several ideas for possible optimizations.
dbdeb734ff97 Add a reference to dictnotes.txt. It does no good if you don't know it's
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
8 */
dbdeb734ff97 Add a reference to dictnotes.txt. It does no good if you don't know it's
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
9
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
10 #include "Python.h"
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
5053
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
12
40257
9ab7a7736ca0 Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents: 40225
diff changeset
13 /* Set a key error with the specified argument, wrapping it in a
9ab7a7736ca0 Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents: 40225
diff changeset
14 * tuple automatically so that tuple keys are not unpacked as the
9ab7a7736ca0 Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents: 40225
diff changeset
15 * exception arguments. */
9ab7a7736ca0 Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents: 40225
diff changeset
16 static void
9ab7a7736ca0 Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents: 40225
diff changeset
17 set_key_error(PyObject *arg)
9ab7a7736ca0 Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents: 40225
diff changeset
18 {
9ab7a7736ca0 Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents: 40225
diff changeset
19 PyObject *tup;
9ab7a7736ca0 Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents: 40225
diff changeset
20 tup = PyTuple_Pack(1, arg);
9ab7a7736ca0 Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents: 40225
diff changeset
21 if (!tup)
9ab7a7736ca0 Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents: 40225
diff changeset
22 return; /* caller will expect error to be set anyway */
9ab7a7736ca0 Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents: 40225
diff changeset
23 PyErr_SetObject(PyExc_KeyError, tup);
40272
a8a6e2b84bba Fix refleak
Neal Norwitz <nnorwitz@gmail.com>
parents: 40257
diff changeset
24 Py_DECREF(tup);
40257
9ab7a7736ca0 Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents: 40225
diff changeset
25 }
9ab7a7736ca0 Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents: 40225
diff changeset
26
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
27 /* Define this out if you don't want conversion statistics on exit. */
14310
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
28 #undef SHOW_CONVERSION_COUNTS
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
29
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
30 /* See large comment block below. This must be >= 1. */
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
31 #define PERTURB_SHIFT 5
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
32
5053
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
33 /*
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
34 Major subtleties ahead: Most hash schemes depend on having a "good" hash
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
35 function, in the sense of simulating randomness. Python doesn't: its most
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
36 important hash functions (for strings and ints) are very regular in common
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
37 cases:
17973
2f2ee5daa60d Implement an old idea of Christian Tismer's: use polynomial division
Tim Peters <tim.peters@gmail.com>
parents: 17964
diff changeset
38
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
39 >>> map(hash, (0, 1, 2, 3))
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
40 [0, 1, 2, 3]
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
41 >>> map(hash, ("namea", "nameb", "namec", "named"))
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
42 [-1658398457, -1658398460, -1658398459, -1658398462]
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
43 >>>
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
44
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
45 This isn't necessarily bad! To the contrary, in a table of size 2**i, taking
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
46 the low-order i bits as the initial table index is extremely fast, and there
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
47 are no collisions at all for dicts indexed by a contiguous range of ints.
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
48 The same is approximately true when keys are "consecutive" strings. So this
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
49 gives better-than-random behavior in common cases, and that's very desirable.
17973
2f2ee5daa60d Implement an old idea of Christian Tismer's: use polynomial division
Tim Peters <tim.peters@gmail.com>
parents: 17964
diff changeset
50
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
51 OTOH, when collisions occur, the tendency to fill contiguous slices of the
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
52 hash table makes a good collision resolution strategy crucial. Taking only
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
53 the last i bits of the hash code is also vulnerable: for example, consider
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
54 [i << 16 for i in range(20000)] as a set of keys. Since ints are their own
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
55 hash codes, and this fits in a dict of size 2**15, the last 15 bits of every
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
56 hash code are all 0: they *all* map to the same table index.
17973
2f2ee5daa60d Implement an old idea of Christian Tismer's: use polynomial division
Tim Peters <tim.peters@gmail.com>
parents: 17964
diff changeset
57
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
58 But catering to unusual cases should not slow the usual ones, so we just take
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
59 the last i bits anyway. It's up to collision resolution to do the rest. If
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
60 we *usually* find the key we're looking for on the first try (and, it turns
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
61 out, we usually do -- the table load factor is kept under 2/3, so the odds
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
62 are solidly in our favor), then it makes best sense to keep the initial index
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
63 computation dirt cheap.
17973
2f2ee5daa60d Implement an old idea of Christian Tismer's: use polynomial division
Tim Peters <tim.peters@gmail.com>
parents: 17964
diff changeset
64
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
65 The first half of collision resolution is to visit table indices via this
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
66 recurrence:
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
67
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
68 j = ((5*j) + 1) mod 2**i
17973
2f2ee5daa60d Implement an old idea of Christian Tismer's: use polynomial division
Tim Peters <tim.peters@gmail.com>
parents: 17964
diff changeset
69
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
70 For any initial j in range(2**i), repeating that 2**i times generates each
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
71 int in range(2**i) exactly once (see any text on random-number generation for
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
72 proof). By itself, this doesn't help much: like linear probing (setting
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
73 j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
74 order. This would be bad, except that's not the only thing we do, and it's
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
75 actually *good* in the common cases where hash keys are consecutive. In an
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
76 example that's really too small to make this entirely clear, for a table of
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
77 size 2**3 the order of indices is:
17973
2f2ee5daa60d Implement an old idea of Christian Tismer's: use polynomial division
Tim Peters <tim.peters@gmail.com>
parents: 17964
diff changeset
78
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
79 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
17973
2f2ee5daa60d Implement an old idea of Christian Tismer's: use polynomial division
Tim Peters <tim.peters@gmail.com>
parents: 17964
diff changeset
80
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
81 If two things come in at index 5, the first place we look after is index 2,
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
82 not 6, so if another comes in at index 6 the collision at 5 didn't hurt it.
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
83 Linear probing is deadly in this case because there the fixed probe order
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
84 is the *same* as the order consecutive keys are likely to arrive. But it's
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
85 extremely unlikely hash codes will follow a 5*j+1 recurrence by accident,
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
86 and certain that consecutive hash codes do not.
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
87
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
88 The other half of the strategy is to get the other bits of the hash code
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
89 into play. This is done by initializing a (unsigned) vrbl "perturb" to the
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
90 full hash code, and changing the recurrence to:
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
91
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
92 j = (5*j) + 1 + perturb;
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
93 perturb >>= PERTURB_SHIFT;
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
94 use j % 2**i as the next table index;
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
95
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
96 Now the probe sequence depends (eventually) on every bit in the hash code,
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
97 and the pseudo-scrambling property of recurring on 5*j+1 is more valuable,
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
98 because it quickly magnifies small differences in the bits that didn't affect
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
99 the initial index. Note that because perturb is unsigned, if the recurrence
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
100 is executed often enough perturb eventually becomes and remains 0. At that
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
101 point (very rarely reached) the recurrence is on (just) 5*j+1 again, and
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
102 that's certain to find an empty slot eventually (since it generates every int
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
103 in range(2**i), and we make sure there's always at least one empty slot).
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
104
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
105 Selecting a good value for PERTURB_SHIFT is a balancing act. You want it
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
106 small so that the high bits of the hash code continue to affect the probe
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
107 sequence across iterations; but you want it large so that in really bad cases
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
108 the high-order hash bits have an effect on early iterations. 5 was "the
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
109 best" in minimizing total collisions across experiments Tim Peters ran (on
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
110 both normal and pathological cases), but 4 and 6 weren't significantly worse.
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
111
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
112 Historical: Reimer Behrends contributed the idea of using a polynomial-based
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
113 approach, using repeated multiplication by x in GF(2**n) where an irreducible
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
114 polynomial for each table size was chosen such that x was a primitive root.
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
115 Christian Tismer later extended that to use division by x instead, as an
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
116 efficient way to get the high bits of the hash code into play. This scheme
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
117 also gave excellent collision statistics, but was more expensive: two
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
118 if-tests were required inside the loop; computing "the next" index took about
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
119 the same number of operations but without as much potential parallelism
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
120 (e.g., computing 5*j can go on at the same time as computing 1+perturb in the
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
121 above, and then shifting perturb can be done while the table index is being
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
122 masked); and the PyDictObject struct required a member to hold the table's
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
123 polynomial. In Tim's experiments the current scheme ran faster, produced
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
124 equally good collision statistics, needed less code & used less memory.
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
125
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
126 Theoretical Python 2.5 headache: hash codes are only C "long", but
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
127 sizeof(Py_ssize_t) > sizeof(long) may be possible. In that case, and if a
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
128 dict is genuinely huge, then only the slots directly reachable via indexing
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
129 by a C long can be the first slot in a probe sequence. The probe sequence
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
130 will still eventually reach every slot in the table, but the collision rate
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
131 on initial probes may be much higher than this scheme was designed for.
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
132 Getting a hash code as fat as Py_ssize_t is the only real cure. But in
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
133 practice, this probably won't make a lick of difference for many years (at
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
134 which point everyone will have terabytes of RAM on 64-bit boxes).
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
135 */
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
136
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 /* Object used as dummy key to fill deleted entries */
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
138 static PyObject *dummy = NULL; /* Initialized by first call to newPyDictObject() */
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
139
37638
65ecb83ea7dd Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents: 37304
diff changeset
140 #ifdef Py_REF_DEBUG
65ecb83ea7dd Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents: 37304
diff changeset
141 PyObject *
65ecb83ea7dd Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents: 37304
diff changeset
142 _PyDict_Dummy(void)
65ecb83ea7dd Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents: 37304
diff changeset
143 {
65ecb83ea7dd Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents: 37304
diff changeset
144 return dummy;
65ecb83ea7dd Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents: 37304
diff changeset
145 }
65ecb83ea7dd Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents: 37304
diff changeset
146 #endif
65ecb83ea7dd Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents: 37304
diff changeset
147
14310
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
148 /* forward declarations */
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
149 static PyDictEntry *
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
150 lookdict_string(PyDictObject *mp, PyObject *key, long hash);
14310
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
151
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
152 #ifdef SHOW_CONVERSION_COUNTS
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
153 static long created = 0L;
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
154 static long converted = 0L;
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
155
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
156 static void
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
157 show_counts(void)
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
158 {
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
159 fprintf(stderr, "created %ld string dicts\n", created);
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
160 fprintf(stderr, "converted %ld to normal dicts\n", converted);
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
161 fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created);
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
162 }
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
163 #endif
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
164
45217
5ac605c0f27c Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
165 /* Debug statistic to compare allocations with reuse through the free list */
5ac605c0f27c Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
166 #undef SHOW_ALLOC_COUNT
5ac605c0f27c Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
167 #ifdef SHOW_ALLOC_COUNT
5ac605c0f27c Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
168 static size_t count_alloc = 0;
5ac605c0f27c Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
169 static size_t count_reuse = 0;
5ac605c0f27c Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
170
5ac605c0f27c Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
171 static void
5ac605c0f27c Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
172 show_alloc(void)
5ac605c0f27c Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
173 {
45503
af928ff0c5c6 Use PY_FORMAT_SIZE_T instead of z for string formatting. Thanks Neal.
Christian Heimes <christian@cheimes.de>
parents: 45300
diff changeset
174 fprintf(stderr, "Dict allocations: %" PY_FORMAT_SIZE_T "d\n",
af928ff0c5c6 Use PY_FORMAT_SIZE_T instead of z for string formatting. Thanks Neal.
Christian Heimes <christian@cheimes.de>
parents: 45300
diff changeset
175 count_alloc);
af928ff0c5c6 Use PY_FORMAT_SIZE_T instead of z for string formatting. Thanks Neal.
Christian Heimes <christian@cheimes.de>
parents: 45300
diff changeset
176 fprintf(stderr, "Dict reuse through freelist: %" PY_FORMAT_SIZE_T
af928ff0c5c6 Use PY_FORMAT_SIZE_T instead of z for string formatting. Thanks Neal.
Christian Heimes <christian@cheimes.de>
parents: 45300
diff changeset
177 "d\n", count_reuse);
45217
5ac605c0f27c Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
178 fprintf(stderr, "%.2f%% reuse rate\n\n",
5ac605c0f27c Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
179 (100.0*count_reuse/(count_alloc+count_reuse)));
5ac605c0f27c Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
180 }
5ac605c0f27c Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
181 #endif
5ac605c0f27c Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
182
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
183 /* Initialization macros.
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
184 There are two ways to create a dict: PyDict_New() is the main C API
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
185 function, and the tp_new slot maps to dict_new(). In the latter case we
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
186 can save a little time over what PyDict_New does because it's guaranteed
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
187 that the PyDictObject struct is already zeroed out.
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
188 Everyone except dict_new() should use EMPTY_TO_MINSIZE (unless they have
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
189 an excellent reason not to).
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
190 */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
191
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
192 #define INIT_NONZERO_DICT_SLOTS(mp) do { \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
193 (mp)->ma_table = (mp)->ma_smalltable; \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
194 (mp)->ma_mask = PyDict_MINSIZE - 1; \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
195 } while(0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
196
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
197 #define EMPTY_TO_MINSIZE(mp) do { \
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
198 memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
199 (mp)->ma_used = (mp)->ma_fill = 0; \
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
200 INIT_NONZERO_DICT_SLOTS(mp); \
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
201 } while(0)
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
202
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
203 /* Dictionary reuse scheme to save calls to malloc, free, and memset */
45191
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44949
diff changeset
204 #ifndef PyDict_MAXFREELIST
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44949
diff changeset
205 #define PyDict_MAXFREELIST 80
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44949
diff changeset
206 #endif
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44949
diff changeset
207 static PyDictObject *free_list[PyDict_MAXFREELIST];
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44949
diff changeset
208 static int numfree = 0;
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
209
45229
262263737bce Deallocate content of the dict free list on interpreter shutdown
Christian Heimes <christian@cheimes.de>
parents: 45217
diff changeset
210 void
262263737bce Deallocate content of the dict free list on interpreter shutdown
Christian Heimes <christian@cheimes.de>
parents: 45217
diff changeset
211 PyDict_Fini(void)
262263737bce Deallocate content of the dict free list on interpreter shutdown
Christian Heimes <christian@cheimes.de>
parents: 45217
diff changeset
212 {
45230
1c326dea494c Use prefix decrement
Christian Heimes <christian@cheimes.de>
parents: 45229
diff changeset
213 PyDictObject *op;
45229
262263737bce Deallocate content of the dict free list on interpreter shutdown
Christian Heimes <christian@cheimes.de>
parents: 45217
diff changeset
214
262263737bce Deallocate content of the dict free list on interpreter shutdown
Christian Heimes <christian@cheimes.de>
parents: 45217
diff changeset
215 while (numfree) {
45230
1c326dea494c Use prefix decrement
Christian Heimes <christian@cheimes.de>
parents: 45229
diff changeset
216 op = free_list[--numfree];
45229
262263737bce Deallocate content of the dict free list on interpreter shutdown
Christian Heimes <christian@cheimes.de>
parents: 45217
diff changeset
217 assert(PyDict_CheckExact(op));
262263737bce Deallocate content of the dict free list on interpreter shutdown
Christian Heimes <christian@cheimes.de>
parents: 45217
diff changeset
218 PyObject_GC_Del(op);
262263737bce Deallocate content of the dict free list on interpreter shutdown
Christian Heimes <christian@cheimes.de>
parents: 45217
diff changeset
219 }
262263737bce Deallocate content of the dict free list on interpreter shutdown
Christian Heimes <christian@cheimes.de>
parents: 45217
diff changeset
220 }
262263737bce Deallocate content of the dict free list on interpreter shutdown
Christian Heimes <christian@cheimes.de>
parents: 45217
diff changeset
221
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
222 PyObject *
13660
a1ce07218781 Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
223 PyDict_New(void)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 {
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
225 register PyDictObject *mp;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 if (dummy == NULL) { /* Auto-initialize dummy */
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
227 dummy = PyString_FromString("<dummy key>");
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 if (dummy == NULL)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 return NULL;
14310
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
230 #ifdef SHOW_CONVERSION_COUNTS
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
231 Py_AtExit(show_counts);
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
232 #endif
45217
5ac605c0f27c Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
233 #ifdef SHOW_ALLOC_COUNT
5ac605c0f27c Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
234 Py_AtExit(show_alloc);
5ac605c0f27c Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
235 #endif
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 }
45191
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44949
diff changeset
237 if (numfree) {
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44949
diff changeset
238 mp = free_list[--numfree];
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
239 assert (mp != NULL);
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 44339
diff changeset
240 assert (Py_TYPE(mp) == &PyDict_Type);
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
241 _Py_NewReference((PyObject *)mp);
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
242 if (mp->ma_fill) {
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
243 EMPTY_TO_MINSIZE(mp);
48209
694814d7b104 - Issue #3537: Fix an assertion failure when an empty but presized dict
Georg Brandl <georg@python.org>
parents: 47961
diff changeset
244 } else {
694814d7b104 - Issue #3537: Fix an assertion failure when an empty but presized dict
Georg Brandl <georg@python.org>
parents: 47961
diff changeset
245 /* At least set ma_table and ma_mask; these are wrong
694814d7b104 - Issue #3537: Fix an assertion failure when an empty but presized dict
Georg Brandl <georg@python.org>
parents: 47961
diff changeset
246 if an empty but presized dict is added to freelist */
694814d7b104 - Issue #3537: Fix an assertion failure when an empty but presized dict
Georg Brandl <georg@python.org>
parents: 47961
diff changeset
247 INIT_NONZERO_DICT_SLOTS(mp);
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
248 }
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
249 assert (mp->ma_used == 0);
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
250 assert (mp->ma_table == mp->ma_smalltable);
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
251 assert (mp->ma_mask == PyDict_MINSIZE - 1);
45217
5ac605c0f27c Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
252 #ifdef SHOW_ALLOC_COUNT
5ac605c0f27c Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
253 count_reuse++;
5ac605c0f27c Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
254 #endif
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
255 } else {
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
256 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
257 if (mp == NULL)
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
258 return NULL;
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
259 EMPTY_TO_MINSIZE(mp);
45217
5ac605c0f27c Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
260 #ifdef SHOW_ALLOC_COUNT
5ac605c0f27c Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
261 count_alloc++;
5ac605c0f27c Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents: 45191
diff changeset
262 #endif
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
263 }
14310
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
264 mp->ma_lookup = lookdict_string;
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
265 #ifdef SHOW_CONVERSION_COUNTS
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
266 ++created;
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
267 #endif
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
268 _PyObject_GC_TRACK(mp);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
269 return (PyObject *)mp;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
271
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 /*
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 The basic lookup function used by all operations.
5053
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
274 This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4.
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
275 Open addressing is preferred over chaining since the link overhead for
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 chaining would be substantial (100% with typical malloc overhead).
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
277
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
278 The initial probe index is computed as hash mod the table size. Subsequent
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
279 probe indices are computed as explained earlier.
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
280
10461
4abc5abaec9a Vladimir Marangozov contributed updated comments.
Guido van Rossum <guido@python.org>
parents: 9487
diff changeset
281 All arithmetic on hash should ignore overflow.
5053
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
282
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
283 (The details in this version are due to Tim Peters, building on many past
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
284 contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
285 Christian Tismer).
14310
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
286
38540
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
287 lookdict() is general-purpose, and may return NULL if (and only if) a
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
288 comparison raises an exception (this was new in Python 2.5).
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
289 lookdict_string() below is specialized to string keys, comparison of which can
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
290 never raise an exception; that function can never return NULL. For both, when
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
291 the key isn't found a PyDictEntry* is returned for which the me_value field is
38540
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
292 NULL; this is the slot in the dict at which the key would have been found, and
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
293 the caller can (if it wishes) add the <key, value> pair to the returned
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
294 PyDictEntry*.
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 */
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
296 static PyDictEntry *
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
297 lookdict(PyDictObject *mp, PyObject *key, register long hash)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 {
38540
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
299 register size_t i;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
300 register size_t perturb;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
301 register PyDictEntry *freeslot;
38540
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
302 register size_t mask = (size_t)mp->ma_mask;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
303 PyDictEntry *ep0 = mp->ma_table;
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
304 register PyDictEntry *ep;
14307
b24db770dffd Clear errors raised by PyObject_Compare() without losing any existing
Fred Drake <fdrake@acm.org>
parents: 13892
diff changeset
305 register int cmp;
18023
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
306 PyObject *startkey;
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
307
38540
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
308 i = (size_t)hash & mask;
5059
192d571cc461 Small lookmapping nits:
Guido van Rossum <guido@python.org>
parents: 5056
diff changeset
309 ep = &ep0[i];
9246
1c4ba0e2f8e7 Slight rearrangement of code in lookdict() by Vladimir Marangozov, to
Guido van Rossum <guido@python.org>
parents: 8674
diff changeset
310 if (ep->me_key == NULL || ep->me_key == key)
4976
2e2945d8163c Rewrote lookmapping() according to suggestions by Jyrki Alakuijala.
Guido van Rossum <guido@python.org>
parents: 4586
diff changeset
311 return ep;
18022
bd1c4fe977cb lookdict: Reduce obfuscating code duplication with a judicious goto.
Tim Peters <tim.peters@gmail.com>
parents: 18019
diff changeset
312
5053
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
313 if (ep->me_key == dummy)
4976
2e2945d8163c Rewrote lookmapping() according to suggestions by Jyrki Alakuijala.
Guido van Rossum <guido@python.org>
parents: 4586
diff changeset
314 freeslot = ep;
6105
77fb880eca49 Made lookdict nearly twice as fast, resulting in a 5% overall
Guido van Rossum <guido@python.org>
parents: 5784
diff changeset
315 else {
14307
b24db770dffd Clear errors raised by PyObject_Compare() without losing any existing
Fred Drake <fdrake@acm.org>
parents: 13892
diff changeset
316 if (ep->me_hash == hash) {
18023
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
317 startkey = ep->me_key;
44056
b6b5b6cdd202 Spaces vs. Tabs.
Georg Brandl <georg@python.org>
parents: 44055
diff changeset
318 Py_INCREF(startkey);
18023
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
319 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
44056
b6b5b6cdd202 Spaces vs. Tabs.
Georg Brandl <georg@python.org>
parents: 44055
diff changeset
320 Py_DECREF(startkey);
18022
bd1c4fe977cb lookdict: Reduce obfuscating code duplication with a judicious goto.
Tim Peters <tim.peters@gmail.com>
parents: 18019
diff changeset
321 if (cmp < 0)
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
322 return NULL;
18023
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
323 if (ep0 == mp->ma_table && ep->me_key == startkey) {
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
324 if (cmp > 0)
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
325 return ep;
18023
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
326 }
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
327 else {
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
328 /* The compare did major nasty stuff to the
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
329 * dict: start over.
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
330 * XXX A clever adversary could prevent this
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
331 * XXX from terminating.
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
332 */
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
333 return lookdict(mp, key, hash);
18023
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
334 }
6105
77fb880eca49 Made lookdict nearly twice as fast, resulting in a 5% overall
Guido van Rossum <guido@python.org>
parents: 5784
diff changeset
335 }
77fb880eca49 Made lookdict nearly twice as fast, resulting in a 5% overall
Guido van Rossum <guido@python.org>
parents: 5784
diff changeset
336 freeslot = NULL;
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4976
diff changeset
337 }
17973
2f2ee5daa60d Implement an old idea of Christian Tismer's: use polynomial division
Tim Peters <tim.peters@gmail.com>
parents: 17964
diff changeset
338
17816
8f5ce58119e0 Aggressive reordering of dict comparisons. In case of collision, it stands
Tim Peters <tim.peters@gmail.com>
parents: 17815
diff changeset
339 /* In the loop, me_key == dummy is by far (factor of 100s) the
8f5ce58119e0 Aggressive reordering of dict comparisons. In case of collision, it stands
Tim Peters <tim.peters@gmail.com>
parents: 17815
diff changeset
340 least likely outcome, so test for that last. */
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
341 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
342 i = (i << 2) + i + perturb + 1;
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
343 ep = &ep0[i & mask];
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
344 if (ep->me_key == NULL)
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
345 return freeslot == NULL ? ep : freeslot;
18022
bd1c4fe977cb lookdict: Reduce obfuscating code duplication with a judicious goto.
Tim Peters <tim.peters@gmail.com>
parents: 18019
diff changeset
346 if (ep->me_key == key)
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
347 return ep;
18022
bd1c4fe977cb lookdict: Reduce obfuscating code duplication with a judicious goto.
Tim Peters <tim.peters@gmail.com>
parents: 18019
diff changeset
348 if (ep->me_hash == hash && ep->me_key != dummy) {
18023
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
349 startkey = ep->me_key;
44056
b6b5b6cdd202 Spaces vs. Tabs.
Georg Brandl <georg@python.org>
parents: 44055
diff changeset
350 Py_INCREF(startkey);
18023
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
351 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
44056
b6b5b6cdd202 Spaces vs. Tabs.
Georg Brandl <georg@python.org>
parents: 44055
diff changeset
352 Py_DECREF(startkey);
18022
bd1c4fe977cb lookdict: Reduce obfuscating code duplication with a judicious goto.
Tim Peters <tim.peters@gmail.com>
parents: 18019
diff changeset
353 if (cmp < 0)
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
354 return NULL;
18023
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
355 if (ep0 == mp->ma_table && ep->me_key == startkey) {
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
356 if (cmp > 0)
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
357 return ep;
18023
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
358 }
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
359 else {
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
360 /* The compare did major nasty stuff to the
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
361 * dict: start over.
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
362 * XXX A clever adversary could prevent this
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
363 * XXX from terminating.
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
364 */
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
365 return lookdict(mp, key, hash);
18023
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
366 }
5053
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
367 }
17816
8f5ce58119e0 Aggressive reordering of dict comparisons. In case of collision, it stands
Tim Peters <tim.peters@gmail.com>
parents: 17815
diff changeset
368 else if (ep->me_key == dummy && freeslot == NULL)
8f5ce58119e0 Aggressive reordering of dict comparisons. In case of collision, it stands
Tim Peters <tim.peters@gmail.com>
parents: 17815
diff changeset
369 freeslot = ep;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
370 }
40225
796304efa268 Fix warnings with HP's C compiler. It doesn't recognize that infinite
Neal Norwitz <nnorwitz@gmail.com>
parents: 39745
diff changeset
371 assert(0); /* NOT REACHED */
796304efa268 Fix warnings with HP's C compiler. It doesn't recognize that infinite
Neal Norwitz <nnorwitz@gmail.com>
parents: 39745
diff changeset
372 return 0;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
373 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
374
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 /*
14310
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
376 * Hacked up version of lookdict which can assume keys are always strings;
38540
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
377 * this assumption allows testing for errors during PyObject_RichCompareBool()
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
378 * to be dropped; string-string comparisons never raise exceptions. This also
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
379 * means we don't need to go through PyObject_RichCompareBool(); we can always
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
380 * use _PyString_Eq() directly.
14310
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
381 *
38540
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
382 * This is valuable because dicts with only string keys are very common.
14310
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
383 */
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
384 static PyDictEntry *
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
385 lookdict_string(PyDictObject *mp, PyObject *key, register long hash)
14310
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
386 {
38540
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
387 register size_t i;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
388 register size_t perturb;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
389 register PyDictEntry *freeslot;
38540
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
390 register size_t mask = (size_t)mp->ma_mask;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
391 PyDictEntry *ep0 = mp->ma_table;
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
392 register PyDictEntry *ep;
14310
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
393
19904
58d50094bf80 Changed the dict implementation to take "string shortcuts" only when
Tim Peters <tim.peters@gmail.com>
parents: 19662
diff changeset
394 /* Make sure this function doesn't have to handle non-string keys,
58d50094bf80 Changed the dict implementation to take "string shortcuts" only when
Tim Peters <tim.peters@gmail.com>
parents: 19662
diff changeset
395 including subclasses of str; e.g., one reason to subclass
58d50094bf80 Changed the dict implementation to take "string shortcuts" only when
Tim Peters <tim.peters@gmail.com>
parents: 19662
diff changeset
396 strings is to override __eq__, and for speed we don't cater to
58d50094bf80 Changed the dict implementation to take "string shortcuts" only when
Tim Peters <tim.peters@gmail.com>
parents: 19662
diff changeset
397 that here. */
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
398 if (!PyString_CheckExact(key)) {
14310
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
399 #ifdef SHOW_CONVERSION_COUNTS
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
400 ++converted;
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
401 #endif
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
402 mp->ma_lookup = lookdict;
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
403 return lookdict(mp, key, hash);
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
404 }
17815
d6df8d371b67 Get rid of the superstitious "~" in dict hashing's "i = (~hash) & mask".
Tim Peters <tim.peters@gmail.com>
parents: 17775
diff changeset
405 i = hash & mask;
14310
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
406 ep = &ep0[i];
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
407 if (ep->me_key == NULL || ep->me_key == key)
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
408 return ep;
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
409 if (ep->me_key == dummy)
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
410 freeslot = ep;
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
411 else {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
412 if (ep->me_hash == hash && _PyString_Eq(ep->me_key, key))
14310
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
413 return ep;
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
414 freeslot = NULL;
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
415 }
17973
2f2ee5daa60d Implement an old idea of Christian Tismer's: use polynomial division
Tim Peters <tim.peters@gmail.com>
parents: 17964
diff changeset
416
17816
8f5ce58119e0 Aggressive reordering of dict comparisons. In case of collision, it stands
Tim Peters <tim.peters@gmail.com>
parents: 17815
diff changeset
417 /* In the loop, me_key == dummy is by far (factor of 100s) the
8f5ce58119e0 Aggressive reordering of dict comparisons. In case of collision, it stands
Tim Peters <tim.peters@gmail.com>
parents: 17815
diff changeset
418 least likely outcome, so test for that last. */
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
419 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
420 i = (i << 2) + i + perturb + 1;
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
421 ep = &ep0[i & mask];
17816
8f5ce58119e0 Aggressive reordering of dict comparisons. In case of collision, it stands
Tim Peters <tim.peters@gmail.com>
parents: 17815
diff changeset
422 if (ep->me_key == NULL)
8f5ce58119e0 Aggressive reordering of dict comparisons. In case of collision, it stands
Tim Peters <tim.peters@gmail.com>
parents: 17815
diff changeset
423 return freeslot == NULL ? ep : freeslot;
8f5ce58119e0 Aggressive reordering of dict comparisons. In case of collision, it stands
Tim Peters <tim.peters@gmail.com>
parents: 17815
diff changeset
424 if (ep->me_key == key
8f5ce58119e0 Aggressive reordering of dict comparisons. In case of collision, it stands
Tim Peters <tim.peters@gmail.com>
parents: 17815
diff changeset
425 || (ep->me_hash == hash
8f5ce58119e0 Aggressive reordering of dict comparisons. In case of collision, it stands
Tim Peters <tim.peters@gmail.com>
parents: 17815
diff changeset
426 && ep->me_key != dummy
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
427 && _PyString_Eq(ep->me_key, key)))
14310
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
428 return ep;
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
429 if (ep->me_key == dummy && freeslot == NULL)
17816
8f5ce58119e0 Aggressive reordering of dict comparisons. In case of collision, it stands
Tim Peters <tim.peters@gmail.com>
parents: 17815
diff changeset
430 freeslot = ep;
14310
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
431 }
40225
796304efa268 Fix warnings with HP's C compiler. It doesn't recognize that infinite
Neal Norwitz <nnorwitz@gmail.com>
parents: 39745
diff changeset
432 assert(0); /* NOT REACHED */
796304efa268 Fix warnings with HP's C compiler. It doesn't recognize that infinite
Neal Norwitz <nnorwitz@gmail.com>
parents: 39745
diff changeset
433 return 0;
14310
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
434 }
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
435
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
436 /*
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
437 Internal routine to insert a new item into the table.
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
438 Used both by the internal resize routine and by the public insert routine.
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
439 Eats a reference to key and one to value.
38540
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
440 Returns -1 if an error occurred, or 0 on success.
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
441 */
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
442 static int
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
443 insertdict(register PyDictObject *mp, PyObject *key, long hash, PyObject *value)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
444 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
445 PyObject *old_value;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
446 register PyDictEntry *ep;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
447 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
448
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
449 assert(mp->ma_lookup != NULL);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
450 ep = mp->ma_lookup(mp, key, hash);
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
451 if (ep == NULL) {
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
452 Py_DECREF(key);
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
453 Py_DECREF(value);
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
454 return -1;
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
455 }
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
456 if (ep->me_value != NULL) {
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
457 old_value = ep->me_value;
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
458 ep->me_value = value;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
459 Py_DECREF(old_value); /* which **CAN** re-enter */
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
460 Py_DECREF(key);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
461 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
462 else {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
463 if (ep->me_key == NULL)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
464 mp->ma_fill++;
34544
f24117a3628a Code simplification -- eliminate lookup when value is known in advance.
Raymond Hettinger <python@rcn.com>
parents: 32695
diff changeset
465 else {
f24117a3628a Code simplification -- eliminate lookup when value is known in advance.
Raymond Hettinger <python@rcn.com>
parents: 32695
diff changeset
466 assert(ep->me_key == dummy);
f24117a3628a Code simplification -- eliminate lookup when value is known in advance.
Raymond Hettinger <python@rcn.com>
parents: 32695
diff changeset
467 Py_DECREF(dummy);
f24117a3628a Code simplification -- eliminate lookup when value is known in advance.
Raymond Hettinger <python@rcn.com>
parents: 32695
diff changeset
468 }
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
469 ep->me_key = key;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
470 ep->me_hash = (Py_ssize_t)hash;
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
471 ep->me_value = value;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
472 mp->ma_used++;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
473 }
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
474 return 0;
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
475 }
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
476
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
477 /*
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
478 Internal routine used by dictresize() to insert an item which is
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
479 known to be absent from the dict. This routine also assumes that
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
480 the dict contains no deleted entries. Besides the performance benefit,
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
481 using insertdict() in dictresize() is dangerous (SF bug #1456209).
38540
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
482 Note that no refcounts are changed by this routine; if needed, the caller
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
483 is responsible for incref'ing `key` and `value`.
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
484 */
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
485 static void
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
486 insertdict_clean(register PyDictObject *mp, PyObject *key, long hash,
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
487 PyObject *value)
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
488 {
38540
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
489 register size_t i;
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
490 register size_t perturb;
38540
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
491 register size_t mask = (size_t)mp->ma_mask;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
492 PyDictEntry *ep0 = mp->ma_table;
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
493 register PyDictEntry *ep;
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
494
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
495 i = hash & mask;
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
496 ep = &ep0[i];
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
497 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
498 i = (i << 2) + i + perturb + 1;
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
499 ep = &ep0[i & mask];
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
500 }
38540
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
501 assert(ep->me_value == NULL);
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
502 mp->ma_fill++;
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
503 ep->me_key = key;
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
504 ep->me_hash = (Py_ssize_t)hash;
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
505 ep->me_value = value;
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
506 mp->ma_used++;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
507 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
508
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
509 /*
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
510 Restructure the table by allocating a new table and reinserting all
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
511 items again. When entries have been deleted, the new table may
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
512 actually be smaller than the old one.
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
513 */
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
514 static int
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
515 dictresize(PyDictObject *mp, Py_ssize_t minused)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
516 {
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
517 Py_ssize_t newsize;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
518 PyDictEntry *oldtable, *newtable, *ep;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
519 Py_ssize_t i;
17962
d86b245e0858 Jack Jansen hit a bug in the new dict code, reported on python-dev.
Tim Peters <tim.peters@gmail.com>
parents: 17930
diff changeset
520 int is_oldtable_malloced;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
521 PyDictEntry small_copy[PyDict_MINSIZE];
17861
335fbdee7cf8 Bugfix candidate.
Tim Peters <tim.peters@gmail.com>
parents: 17852
diff changeset
522
335fbdee7cf8 Bugfix candidate.
Tim Peters <tim.peters@gmail.com>
parents: 17852
diff changeset
523 assert(minused >= 0);
17962
d86b245e0858 Jack Jansen hit a bug in the new dict code, reported on python-dev.
Tim Peters <tim.peters@gmail.com>
parents: 17930
diff changeset
524
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
525 /* Find the smallest table size > minused. */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
526 for (newsize = PyDict_MINSIZE;
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
527 newsize <= minused && newsize > 0;
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
528 newsize <<= 1)
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
529 ;
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
530 if (newsize <= 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
531 PyErr_NoMemory();
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
532 return -1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
533 }
17962
d86b245e0858 Jack Jansen hit a bug in the new dict code, reported on python-dev.
Tim Peters <tim.peters@gmail.com>
parents: 17930
diff changeset
534
d86b245e0858 Jack Jansen hit a bug in the new dict code, reported on python-dev.
Tim Peters <tim.peters@gmail.com>
parents: 17930
diff changeset
535 /* Get space for a new table. */
d86b245e0858 Jack Jansen hit a bug in the new dict code, reported on python-dev.
Tim Peters <tim.peters@gmail.com>
parents: 17930
diff changeset
536 oldtable = mp->ma_table;
d86b245e0858 Jack Jansen hit a bug in the new dict code, reported on python-dev.
Tim Peters <tim.peters@gmail.com>
parents: 17930
diff changeset
537 assert(oldtable != NULL);
d86b245e0858 Jack Jansen hit a bug in the new dict code, reported on python-dev.
Tim Peters <tim.peters@gmail.com>
parents: 17930
diff changeset
538 is_oldtable_malloced = oldtable != mp->ma_smalltable;
d86b245e0858 Jack Jansen hit a bug in the new dict code, reported on python-dev.
Tim Peters <tim.peters@gmail.com>
parents: 17930
diff changeset
539
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
540 if (newsize == PyDict_MINSIZE) {
17963
95baa333ee43 dictresize(): Rebuild small tables if there are any dummies, not just if
Tim Peters <tim.peters@gmail.com>
parents: 17962
diff changeset
541 /* A large table is shrinking, or we can't get any smaller. */
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
542 newtable = mp->ma_smalltable;
17962
d86b245e0858 Jack Jansen hit a bug in the new dict code, reported on python-dev.
Tim Peters <tim.peters@gmail.com>
parents: 17930
diff changeset
543 if (newtable == oldtable) {
17963
95baa333ee43 dictresize(): Rebuild small tables if there are any dummies, not just if
Tim Peters <tim.peters@gmail.com>
parents: 17962
diff changeset
544 if (mp->ma_fill == mp->ma_used) {
95baa333ee43 dictresize(): Rebuild small tables if there are any dummies, not just if
Tim Peters <tim.peters@gmail.com>
parents: 17962
diff changeset
545 /* No dummies, so no point doing anything. */
17962
d86b245e0858 Jack Jansen hit a bug in the new dict code, reported on python-dev.
Tim Peters <tim.peters@gmail.com>
parents: 17930
diff changeset
546 return 0;
17963
95baa333ee43 dictresize(): Rebuild small tables if there are any dummies, not just if
Tim Peters <tim.peters@gmail.com>
parents: 17962
diff changeset
547 }
95baa333ee43 dictresize(): Rebuild small tables if there are any dummies, not just if
Tim Peters <tim.peters@gmail.com>
parents: 17962
diff changeset
548 /* We're not going to resize it, but rebuild the
95baa333ee43 dictresize(): Rebuild small tables if there are any dummies, not just if
Tim Peters <tim.peters@gmail.com>
parents: 17962
diff changeset
549 table anyway to purge old dummy entries.
95baa333ee43 dictresize(): Rebuild small tables if there are any dummies, not just if
Tim Peters <tim.peters@gmail.com>
parents: 17962
diff changeset
550 Subtle: This is *necessary* if fill==size,
95baa333ee43 dictresize(): Rebuild small tables if there are any dummies, not just if
Tim Peters <tim.peters@gmail.com>
parents: 17962
diff changeset
551 as lookdict needs at least one virgin slot to
95baa333ee43 dictresize(): Rebuild small tables if there are any dummies, not just if
Tim Peters <tim.peters@gmail.com>
parents: 17962
diff changeset
552 terminate failing searches. If fill < size, it's
95baa333ee43 dictresize(): Rebuild small tables if there are any dummies, not just if
Tim Peters <tim.peters@gmail.com>
parents: 17962
diff changeset
553 merely desirable, as dummies slow searches. */
95baa333ee43 dictresize(): Rebuild small tables if there are any dummies, not just if
Tim Peters <tim.peters@gmail.com>
parents: 17962
diff changeset
554 assert(mp->ma_fill > mp->ma_used);
17962
d86b245e0858 Jack Jansen hit a bug in the new dict code, reported on python-dev.
Tim Peters <tim.peters@gmail.com>
parents: 17930
diff changeset
555 memcpy(small_copy, oldtable, sizeof(small_copy));
d86b245e0858 Jack Jansen hit a bug in the new dict code, reported on python-dev.
Tim Peters <tim.peters@gmail.com>
parents: 17930
diff changeset
556 oldtable = small_copy;
d86b245e0858 Jack Jansen hit a bug in the new dict code, reported on python-dev.
Tim Peters <tim.peters@gmail.com>
parents: 17930
diff changeset
557 }
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
558 }
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
559 else {
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
560 newtable = PyMem_NEW(PyDictEntry, newsize);
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
561 if (newtable == NULL) {
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
562 PyErr_NoMemory();
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
563 return -1;
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
564 }
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
565 }
17962
d86b245e0858 Jack Jansen hit a bug in the new dict code, reported on python-dev.
Tim Peters <tim.peters@gmail.com>
parents: 17930
diff changeset
566
d86b245e0858 Jack Jansen hit a bug in the new dict code, reported on python-dev.
Tim Peters <tim.peters@gmail.com>
parents: 17930
diff changeset
567 /* Make the dict empty, using the new table. */
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
568 assert(newtable != oldtable);
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
569 mp->ma_table = newtable;
18032
b99979d0f9e7 Store the mask instead of the size in dictobjects. The mask is more
Tim Peters <tim.peters@gmail.com>
parents: 18023
diff changeset
570 mp->ma_mask = newsize - 1;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
571 memset(newtable, 0, sizeof(PyDictEntry) * newsize);
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
572 mp->ma_used = 0;
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
573 i = mp->ma_fill;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
574 mp->ma_fill = 0;
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
575
17852
ba97111a6ad9 Speed dictresize by collapsing its two passes into one; the reason given
Tim Peters <tim.peters@gmail.com>
parents: 17816
diff changeset
576 /* Copy the data over; this is refcount-neutral for active entries;
ba97111a6ad9 Speed dictresize by collapsing its two passes into one; the reason given
Tim Peters <tim.peters@gmail.com>
parents: 17816
diff changeset
577 dummy entries aren't copied over, of course */
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
578 for (ep = oldtable; i > 0; ep++) {
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
579 if (ep->me_value != NULL) { /* active entry */
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
580 --i;
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
581 insertdict_clean(mp, ep->me_key, (long)ep->me_hash,
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
582 ep->me_value);
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
583 }
17852
ba97111a6ad9 Speed dictresize by collapsing its two passes into one; the reason given
Tim Peters <tim.peters@gmail.com>
parents: 17816
diff changeset
584 else if (ep->me_key != NULL) { /* dummy entry */
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
585 --i;
17852
ba97111a6ad9 Speed dictresize by collapsing its two passes into one; the reason given
Tim Peters <tim.peters@gmail.com>
parents: 17816
diff changeset
586 assert(ep->me_key == dummy);
ba97111a6ad9 Speed dictresize by collapsing its two passes into one; the reason given
Tim Peters <tim.peters@gmail.com>
parents: 17816
diff changeset
587 Py_DECREF(ep->me_key);
7985
ab4f64561fba Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents: 6490
diff changeset
588 }
17852
ba97111a6ad9 Speed dictresize by collapsing its two passes into one; the reason given
Tim Peters <tim.peters@gmail.com>
parents: 17816
diff changeset
589 /* else key == value == NULL: nothing to do */
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
590 }
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
591
17962
d86b245e0858 Jack Jansen hit a bug in the new dict code, reported on python-dev.
Tim Peters <tim.peters@gmail.com>
parents: 17930
diff changeset
592 if (is_oldtable_malloced)
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12126
diff changeset
593 PyMem_DEL(oldtable);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
594 return 0;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
595 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
596
44339
7ac117b3be37 Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary.
Raymond Hettinger <python@rcn.com>
parents: 44056
diff changeset
597 /* Create a new dictionary pre-sized to hold an estimated number of elements.
7ac117b3be37 Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary.
Raymond Hettinger <python@rcn.com>
parents: 44056
diff changeset
598 Underestimates are okay because the dictionary will resize as necessary.
7ac117b3be37 Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary.
Raymond Hettinger <python@rcn.com>
parents: 44056
diff changeset
599 Overestimates just mean the dictionary will be more sparse than usual.
7ac117b3be37 Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary.
Raymond Hettinger <python@rcn.com>
parents: 44056
diff changeset
600 */
7ac117b3be37 Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary.
Raymond Hettinger <python@rcn.com>
parents: 44056
diff changeset
601
7ac117b3be37 Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary.
Raymond Hettinger <python@rcn.com>
parents: 44056
diff changeset
602 PyObject *
7ac117b3be37 Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary.
Raymond Hettinger <python@rcn.com>
parents: 44056
diff changeset
603 _PyDict_NewPresized(Py_ssize_t minused)
7ac117b3be37 Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary.
Raymond Hettinger <python@rcn.com>
parents: 44056
diff changeset
604 {
7ac117b3be37 Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary.
Raymond Hettinger <python@rcn.com>
parents: 44056
diff changeset
605 PyObject *op = PyDict_New();
7ac117b3be37 Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary.
Raymond Hettinger <python@rcn.com>
parents: 44056
diff changeset
606
7ac117b3be37 Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary.
Raymond Hettinger <python@rcn.com>
parents: 44056
diff changeset
607 if (minused>5 && op != NULL && dictresize((PyDictObject *)op, minused) == -1) {
7ac117b3be37 Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary.
Raymond Hettinger <python@rcn.com>
parents: 44056
diff changeset
608 Py_DECREF(op);
7ac117b3be37 Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary.
Raymond Hettinger <python@rcn.com>
parents: 44056
diff changeset
609 return NULL;
7ac117b3be37 Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary.
Raymond Hettinger <python@rcn.com>
parents: 44056
diff changeset
610 }
7ac117b3be37 Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary.
Raymond Hettinger <python@rcn.com>
parents: 44056
diff changeset
611 return op;
7ac117b3be37 Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary.
Raymond Hettinger <python@rcn.com>
parents: 44056
diff changeset
612 }
7ac117b3be37 Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary.
Raymond Hettinger <python@rcn.com>
parents: 44056
diff changeset
613
38540
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
614 /* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
615 * that may occur (originally dicts supported only string keys, and exceptions
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
616 * weren't possible). So, while the original intent was that a NULL return
39376
efa80633309d Typo fixes
Andrew M. Kuchling <amk@amk.ca>
parents: 39127
diff changeset
617 * meant the key wasn't present, in reality it can mean that, or that an error
38540
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
618 * (suppressed) occurred while computing the key's hash, or that some error
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
619 * (suppressed) occurred when comparing keys in the dict's internal probe
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
620 * sequence. A nasty example of the latter is when a Python-coded comparison
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
621 * function hits a stack-depth error, which can cause this to return NULL
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
622 * even if the key is present.
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
623 */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
624 PyObject *
13341
3f1bd4782628 Removed Py_PROTO and switched to ANSI C declarations in the dict
Tim Peters <tim.peters@gmail.com>
parents: 13265
diff changeset
625 PyDict_GetItem(PyObject *op, PyObject *key)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
626 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
627 long hash;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
628 PyDictObject *mp = (PyDictObject *)op;
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
629 PyDictEntry *ep;
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
630 PyThreadState *tstate;
38540
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
631 if (!PyDict_Check(op))
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
632 return NULL;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
633 if (!PyString_CheckExact(key) ||
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
634 (hash = ((PyStringObject *) key)->ob_shash) == -1)
5038
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
635 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
636 hash = PyObject_Hash(key);
8326
a23b75138bb0 Make sure that PyDict_GetItem[String]() *never* raises an exception.
Guido van Rossum <guido@python.org>
parents: 7985
diff changeset
637 if (hash == -1) {
a23b75138bb0 Make sure that PyDict_GetItem[String]() *never* raises an exception.
Guido van Rossum <guido@python.org>
parents: 7985
diff changeset
638 PyErr_Clear();
5038
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
639 return NULL;
8326
a23b75138bb0 Make sure that PyDict_GetItem[String]() *never* raises an exception.
Guido van Rossum <guido@python.org>
parents: 7985
diff changeset
640 }
5038
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
641 }
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
642
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
643 /* We can arrive here with a NULL tstate during initialization:
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
644 try running "python -Wi" for an example related to string
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
645 interning. Let's just hope that no exception occurs then... */
38715
e2d2dc506528 SF bug #1503294.
Armin Rigo <arigo@tunes.org>
parents: 38540
diff changeset
646 tstate = _PyThreadState_Current;
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
647 if (tstate != NULL && tstate->curexc_type != NULL) {
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
648 /* preserve the existing exception */
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
649 PyObject *err_type, *err_value, *err_tb;
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
650 PyErr_Fetch(&err_type, &err_value, &err_tb);
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
651 ep = (mp->ma_lookup)(mp, key, hash);
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
652 /* ignore errors */
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
653 PyErr_Restore(err_type, err_value, err_tb);
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
654 if (ep == NULL)
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
655 return NULL;
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
656 }
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
657 else {
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
658 ep = (mp->ma_lookup)(mp, key, hash);
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
659 if (ep == NULL) {
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
660 PyErr_Clear();
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
661 return NULL;
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
662 }
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
663 }
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
664 return ep->me_value;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
665 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
666
17535
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
667 /* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
36164
50886d42b444 Fixed English in a comment; trimmed trailing whitespace;
Tim Peters <tim.peters@gmail.com>
parents: 35636
diff changeset
668 * dictionary if it's merely replacing the value for an existing key.
50886d42b444 Fixed English in a comment; trimmed trailing whitespace;
Tim Peters <tim.peters@gmail.com>
parents: 35636
diff changeset
669 * This means that it's safe to loop over a dictionary with PyDict_Next()
50886d42b444 Fixed English in a comment; trimmed trailing whitespace;
Tim Peters <tim.peters@gmail.com>
parents: 35636
diff changeset
670 * and occasionally replace a value -- but you can't insert new keys or
50886d42b444 Fixed English in a comment; trimmed trailing whitespace;
Tim Peters <tim.peters@gmail.com>
parents: 35636
diff changeset
671 * remove them.
17535
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
672 */
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
673 int
13341
3f1bd4782628 Removed Py_PROTO and switched to ANSI C declarations in the dict
Tim Peters <tim.peters@gmail.com>
parents: 13265
diff changeset
674 PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
675 {
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
676 register PyDictObject *mp;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
677 register long hash;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
678 register Py_ssize_t n_used;
17535
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
679
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
680 if (!PyDict_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
681 PyErr_BadInternalCall();
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
682 return -1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
683 }
39127
7f38173251bb Add some asserts that we got good params passed
Neal Norwitz <nnorwitz@gmail.com>
parents: 38715
diff changeset
684 assert(key);
7f38173251bb Add some asserts that we got good params passed
Neal Norwitz <nnorwitz@gmail.com>
parents: 38715
diff changeset
685 assert(value);
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
686 mp = (PyDictObject *)op;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
687 if (PyString_CheckExact(key)) {
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
688 hash = ((PyStringObject *)key)->ob_shash;
24901
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24280
diff changeset
689 if (hash == -1)
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24280
diff changeset
690 hash = PyObject_Hash(key);
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4976
diff changeset
691 }
22651
f3a416b178c4 Remove the CACHE_HASH and INTERN_STRINGS preprocessor symbols.
Tim Peters <tim.peters@gmail.com>
parents: 22625
diff changeset
692 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
693 hash = PyObject_Hash(key);
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4976
diff changeset
694 if (hash == -1)
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4976
diff changeset
695 return -1;
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4976
diff changeset
696 }
18032
b99979d0f9e7 Store the mask instead of the size in dictobjects. The mask is more
Tim Peters <tim.peters@gmail.com>
parents: 18023
diff changeset
697 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */
17535
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
698 n_used = mp->ma_used;
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
699 Py_INCREF(value);
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
700 Py_INCREF(key);
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
701 if (insertdict(mp, key, hash, value) != 0)
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
702 return -1;
28984
17bfa786075a SF patch #729395: Dictionary tuning
Raymond Hettinger <python@rcn.com>
parents: 28946
diff changeset
703 /* If we added a key, we can safely resize. Otherwise just return!
17bfa786075a SF patch #729395: Dictionary tuning
Raymond Hettinger <python@rcn.com>
parents: 28946
diff changeset
704 * If fill >= 2/3 size, adjust size. Normally, this doubles or
17bfa786075a SF patch #729395: Dictionary tuning
Raymond Hettinger <python@rcn.com>
parents: 28946
diff changeset
705 * quaduples the size, but it's also possible for the dict to shrink
36164
50886d42b444 Fixed English in a comment; trimmed trailing whitespace;
Tim Peters <tim.peters@gmail.com>
parents: 35636
diff changeset
706 * (if ma_fill is much larger than ma_used, meaning a lot of dict
28984
17bfa786075a SF patch #729395: Dictionary tuning
Raymond Hettinger <python@rcn.com>
parents: 28946
diff changeset
707 * keys have been * deleted).
36164
50886d42b444 Fixed English in a comment; trimmed trailing whitespace;
Tim Peters <tim.peters@gmail.com>
parents: 35636
diff changeset
708 *
28984
17bfa786075a SF patch #729395: Dictionary tuning
Raymond Hettinger <python@rcn.com>
parents: 28946
diff changeset
709 * Quadrupling the size improves average dictionary sparseness
17bfa786075a SF patch #729395: Dictionary tuning
Raymond Hettinger <python@rcn.com>
parents: 28946
diff changeset
710 * (reducing collisions) at the cost of some memory and iteration
17bfa786075a SF patch #729395: Dictionary tuning
Raymond Hettinger <python@rcn.com>
parents: 28946
diff changeset
711 * speed (which loops over every possible entry). It also halves
38500
9f862d9f7785 Remove stray | in comment
Neal Norwitz <nnorwitz@gmail.com>
parents: 38499
diff changeset
712 * the number of expensive resize operations in a growing dictionary.
36164
50886d42b444 Fixed English in a comment; trimmed trailing whitespace;
Tim Peters <tim.peters@gmail.com>
parents: 35636
diff changeset
713 *
50886d42b444 Fixed English in a comment; trimmed trailing whitespace;
Tim Peters <tim.peters@gmail.com>
parents: 35636
diff changeset
714 * Very large dictionaries (over 50K items) use doubling instead.
28984
17bfa786075a SF patch #729395: Dictionary tuning
Raymond Hettinger <python@rcn.com>
parents: 28946
diff changeset
715 * This may help applications with severe memory constraints.
17115
e1214f9e12a3 Make PyDict_Next safe to use for loops that merely modify the values
Tim Peters <tim.peters@gmail.com>
parents: 15974
diff changeset
716 */
28984
17bfa786075a SF patch #729395: Dictionary tuning
Raymond Hettinger <python@rcn.com>
parents: 28946
diff changeset
717 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
17bfa786075a SF patch #729395: Dictionary tuning
Raymond Hettinger <python@rcn.com>
parents: 28946
diff changeset
718 return 0;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
719 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
720 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
721
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
722 int
13341
3f1bd4782628 Removed Py_PROTO and switched to ANSI C declarations in the dict
Tim Peters <tim.peters@gmail.com>
parents: 13265
diff changeset
723 PyDict_DelItem(PyObject *op, PyObject *key)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
724 {
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
725 register PyDictObject *mp;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
726 register long hash;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
727 register PyDictEntry *ep;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
728 PyObject *old_value, *old_key;
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
729
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
730 if (!PyDict_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
731 PyErr_BadInternalCall();
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
732 return -1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
733 }
39127
7f38173251bb Add some asserts that we got good params passed
Neal Norwitz <nnorwitz@gmail.com>
parents: 38715
diff changeset
734 assert(key);
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
735 if (!PyString_CheckExact(key) ||
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
736 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
737 hash = PyObject_Hash(key);
5038
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
738 if (hash == -1)
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
739 return -1;
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
740 }
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
741 mp = (PyDictObject *)op;
14310
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
742 ep = (mp->ma_lookup)(mp, key, hash);
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
743 if (ep == NULL)
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
744 return -1;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
745 if (ep->me_value == NULL) {
40257
9ab7a7736ca0 Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents: 40225
diff changeset
746 set_key_error(key);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
747 return -1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
748 }
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
749 old_key = ep->me_key;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
750 Py_INCREF(dummy);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
751 ep->me_key = dummy;
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
752 old_value = ep->me_value;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
753 ep->me_value = NULL;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
754 mp->ma_used--;
15544
cbd4f355bb96 Bring comments up to date (e.g., they still said the table had to be
Tim Peters <tim.peters@gmail.com>
parents: 15520
diff changeset
755 Py_DECREF(old_value);
cbd4f355bb96 Bring comments up to date (e.g., they still said the table had to be
Tim Peters <tim.peters@gmail.com>
parents: 15520
diff changeset
756 Py_DECREF(old_key);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
757 return 0;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
758 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
759
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
760 void
13341
3f1bd4782628 Removed Py_PROTO and switched to ANSI C declarations in the dict
Tim Peters <tim.peters@gmail.com>
parents: 13265
diff changeset
761 PyDict_Clear(PyObject *op)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
762 {
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
763 PyDictObject *mp;
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
764 PyDictEntry *ep, *table;
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
765 int table_is_malloced;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
766 Py_ssize_t fill;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
767 PyDictEntry small_copy[PyDict_MINSIZE];
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
768 #ifdef Py_DEBUG
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
769 Py_ssize_t i, n;
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
770 #endif
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
771
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
772 if (!PyDict_Check(op))
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
773 return;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
774 mp = (PyDictObject *)op;
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
775 #ifdef Py_DEBUG
18032
b99979d0f9e7 Store the mask instead of the size in dictobjects. The mask is more
Tim Peters <tim.peters@gmail.com>
parents: 18023
diff changeset
776 n = mp->ma_mask + 1;
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
777 i = 0;
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
778 #endif
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
779
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
780 table = mp->ma_table;
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
781 assert(table != NULL);
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
782 table_is_malloced = table != mp->ma_smalltable;
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
783
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
784 /* This is delicate. During the process of clearing the dict,
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
785 * decrefs can cause the dict to mutate. To avoid fatal confusion
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
786 * (voice of experience), we have to make the dict empty before
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
787 * clearing the slots, and never refer to anything via mp->xxx while
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
788 * clearing.
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
789 */
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
790 fill = mp->ma_fill;
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
791 if (table_is_malloced)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
792 EMPTY_TO_MINSIZE(mp);
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
793
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
794 else if (fill > 0) {
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
795 /* It's a small table with something that needs to be cleared.
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
796 * Afraid the only safe way is to copy the dict entries into
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
797 * another small table first.
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
798 */
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
799 memcpy(small_copy, table, sizeof(small_copy));
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
800 table = small_copy;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
801 EMPTY_TO_MINSIZE(mp);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
802 }
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
803 /* else it's a small table that's already empty */
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
804
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
805 /* Now we can finally clear things. If C had refcounts, we could
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
806 * assert that the refcount on table is 1 now, i.e. that this function
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
807 * has unique access to it, so decref side-effects can't alter it.
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
808 */
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
809 for (ep = table; fill > 0; ++ep) {
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
810 #ifdef Py_DEBUG
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
811 assert(i < n);
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
812 ++i;
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
813 #endif
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
814 if (ep->me_key) {
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
815 --fill;
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
816 Py_DECREF(ep->me_key);
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
817 Py_XDECREF(ep->me_value);
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
818 }
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
819 #ifdef Py_DEBUG
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
820 else
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
821 assert(ep->me_value == NULL);
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
822 #endif
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
823 }
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
824
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
825 if (table_is_malloced)
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
826 PyMem_DEL(table);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
827 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
828
27901
55adf08a213a cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents: 26818
diff changeset
829 /*
55adf08a213a cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents: 26818
diff changeset
830 * Iterate over a dict. Use like so:
55adf08a213a cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents: 26818
diff changeset
831 *
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
832 * Py_ssize_t i;
27901
55adf08a213a cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents: 26818
diff changeset
833 * PyObject *key, *value;
55adf08a213a cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents: 26818
diff changeset
834 * i = 0; # important! i should not otherwise be changed by you
27902
81b2bcbfe39b Add closing ) in comment
Neal Norwitz <nnorwitz@gmail.com>
parents: 27901
diff changeset
835 * while (PyDict_Next(yourdict, &i, &key, &value)) {
27901
55adf08a213a cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents: 26818
diff changeset
836 * Refer to borrowed references in key and value.
55adf08a213a cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents: 26818
diff changeset
837 * }
55adf08a213a cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents: 26818
diff changeset
838 *
55adf08a213a cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents: 26818
diff changeset
839 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that
17115
e1214f9e12a3 Make PyDict_Next safe to use for loops that merely modify the values
Tim Peters <tim.peters@gmail.com>
parents: 15974
diff changeset
840 * mutates the dict. One exception: it is safe if the loop merely changes
e1214f9e12a3 Make PyDict_Next safe to use for loops that merely modify the values
Tim Peters <tim.peters@gmail.com>
parents: 15974
diff changeset
841 * the values associated with the keys (but doesn't insert new keys or
e1214f9e12a3 Make PyDict_Next safe to use for loops that merely modify the values
Tim Peters <tim.peters@gmail.com>
parents: 15974
diff changeset
842 * delete keys), via PyDict_SetItem().
e1214f9e12a3 Make PyDict_Next safe to use for loops that merely modify the values
Tim Peters <tim.peters@gmail.com>
parents: 15974
diff changeset
843 */
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
844 int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
845 PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
846 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
847 register Py_ssize_t i;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
848 register Py_ssize_t mask;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
849 register PyDictEntry *ep;
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
850
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
851 if (!PyDict_Check(op))
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
852 return 0;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
853 i = *ppos;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
854 if (i < 0)
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
855 return 0;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
856 ep = ((PyDictObject *)op)->ma_table;
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
857 mask = ((PyDictObject *)op)->ma_mask;
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
858 while (i <= mask && ep[i].me_value == NULL)
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
859 i++;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
860 *ppos = i+1;
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
861 if (i > mask)
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
862 return 0;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
863 if (pkey)
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
864 *pkey = ep[i].me_key;
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
865 if (pvalue)
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
866 *pvalue = ep[i].me_value;
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
867 return 1;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
868 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
869
40864
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
870 /* Internal version of PyDict_Next that returns a hash value in addition to the key and value.*/
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
871 int
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
872 _PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, long *phash)
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
873 {
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
874 register Py_ssize_t i;
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
875 register Py_ssize_t mask;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
876 register PyDictEntry *ep;
40864
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
877
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
878 if (!PyDict_Check(op))
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
879 return 0;
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
880 i = *ppos;
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
881 if (i < 0)
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
882 return 0;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
883 ep = ((PyDictObject *)op)->ma_table;
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
884 mask = ((PyDictObject *)op)->ma_mask;
40864
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
885 while (i <= mask && ep[i].me_value == NULL)
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
886 i++;
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
887 *ppos = i+1;
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
888 if (i > mask)
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
889 return 0;
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
890 *phash = (long)(ep[i].me_hash);
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
891 if (pkey)
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
892 *pkey = ep[i].me_key;
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
893 if (pvalue)
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
894 *pvalue = ep[i].me_value;
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
895 return 1;
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
896 }
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
897
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
898 /* Methods */
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
899
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
900 static void
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
901 dict_dealloc(register PyDictObject *mp)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
902 {
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
903 register PyDictEntry *ep;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
904 Py_ssize_t fill = mp->ma_fill;
22625
898c392477d9 This is Neil's fix for SF bug 535905 (Evil Trashcan and GC interaction).
Guido van Rossum <guido@python.org>
parents: 22446
diff changeset
905 PyObject_GC_UnTrack(mp);
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11838
diff changeset
906 Py_TRASHCAN_SAFE_BEGIN(mp)
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
907 for (ep = mp->ma_table; fill > 0; ep++) {
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
908 if (ep->me_key) {
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
909 --fill;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
910 Py_DECREF(ep->me_key);
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
911 Py_XDECREF(ep->me_value);
7985
ab4f64561fba Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents: 6490
diff changeset
912 }
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
913 }
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
914 if (mp->ma_table != mp->ma_smalltable)
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12126
diff changeset
915 PyMem_DEL(mp->ma_table);
45191
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44949
diff changeset
916 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44949
diff changeset
917 free_list[numfree++] = mp;
36164
50886d42b444 Fixed English in a comment; trimmed trailing whitespace;
Tim Peters <tim.peters@gmail.com>
parents: 35636
diff changeset
918 else
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 44339
diff changeset
919 Py_TYPE(mp)->tp_free((PyObject *)mp);
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11838
diff changeset
920 Py_TRASHCAN_SAFE_END(mp)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
921 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
922
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
923 static int
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
924 dict_print(register PyDictObject *mp, register FILE *fp, register int flags)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
925 {
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
926 register Py_ssize_t i;
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
927 register Py_ssize_t any;
38504
441711cbfe34 dict_print(): So that Neal & I don't spend the rest of
Tim Peters <tim.peters@gmail.com>
parents: 38503
diff changeset
928 int status;
7985
ab4f64561fba Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents: 6490
diff changeset
929
38504
441711cbfe34 dict_print(): So that Neal & I don't spend the rest of
Tim Peters <tim.peters@gmail.com>
parents: 38503
diff changeset
930 status = Py_ReprEnter((PyObject*)mp);
441711cbfe34 dict_print(): So that Neal & I don't spend the rest of
Tim Peters <tim.peters@gmail.com>
parents: 38503
diff changeset
931 if (status != 0) {
441711cbfe34 dict_print(): So that Neal & I don't spend the rest of
Tim Peters <tim.peters@gmail.com>
parents: 38503
diff changeset
932 if (status < 0)
441711cbfe34 dict_print(): So that Neal & I don't spend the rest of
Tim Peters <tim.peters@gmail.com>
parents: 38503
diff changeset
933 return status;
43378
7630248b593c Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents: 42333
diff changeset
934 Py_BEGIN_ALLOW_THREADS
7985
ab4f64561fba Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents: 6490
diff changeset
935 fprintf(fp, "{...}");
43378
7630248b593c Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents: 42333
diff changeset
936 Py_END_ALLOW_THREADS
7985
ab4f64561fba Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents: 6490
diff changeset
937 return 0;
ab4f64561fba Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents: 6490
diff changeset
938 }
ab4f64561fba Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents: 6490
diff changeset
939
43378
7630248b593c Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents: 42333
diff changeset
940 Py_BEGIN_ALLOW_THREADS
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
941 fprintf(fp, "{");
43378
7630248b593c Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents: 42333
diff changeset
942 Py_END_ALLOW_THREADS
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
943 any = 0;
18032
b99979d0f9e7 Store the mask instead of the size in dictobjects. The mask is more
Tim Peters <tim.peters@gmail.com>
parents: 18023
diff changeset
944 for (i = 0; i <= mp->ma_mask; i++) {
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
945 PyDictEntry *ep = mp->ma_table + i;
18017
facff5f50fcc Coredumpers from Michael Hudson, mutating dicts while printing or
Tim Peters <tim.peters@gmail.com>
parents: 18015
diff changeset
946 PyObject *pvalue = ep->me_value;
facff5f50fcc Coredumpers from Michael Hudson, mutating dicts while printing or
Tim Peters <tim.peters@gmail.com>
parents: 18015
diff changeset
947 if (pvalue != NULL) {
facff5f50fcc Coredumpers from Michael Hudson, mutating dicts while printing or
Tim Peters <tim.peters@gmail.com>
parents: 18015
diff changeset
948 /* Prevent PyObject_Repr from deleting value during
facff5f50fcc Coredumpers from Michael Hudson, mutating dicts while printing or
Tim Peters <tim.peters@gmail.com>
parents: 18015
diff changeset
949 key format */
facff5f50fcc Coredumpers from Michael Hudson, mutating dicts while printing or
Tim Peters <tim.peters@gmail.com>
parents: 18015
diff changeset
950 Py_INCREF(pvalue);
43378
7630248b593c Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents: 42333
diff changeset
951 if (any++ > 0) {
7630248b593c Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents: 42333
diff changeset
952 Py_BEGIN_ALLOW_THREADS
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
953 fprintf(fp, ", ");
43378
7630248b593c Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents: 42333
diff changeset
954 Py_END_ALLOW_THREADS
7630248b593c Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents: 42333
diff changeset
955 }
7985
ab4f64561fba Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents: 6490
diff changeset
956 if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
18017
facff5f50fcc Coredumpers from Michael Hudson, mutating dicts while printing or
Tim Peters <tim.peters@gmail.com>
parents: 18015
diff changeset
957 Py_DECREF(pvalue);
7985
ab4f64561fba Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents: 6490
diff changeset
958 Py_ReprLeave((PyObject*)mp);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
959 return -1;
7985
ab4f64561fba Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents: 6490
diff changeset
960 }
43378
7630248b593c Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents: 42333
diff changeset
961 Py_BEGIN_ALLOW_THREADS
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
962 fprintf(fp, ": ");
43378
7630248b593c Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents: 42333
diff changeset
963 Py_END_ALLOW_THREADS
18019
ca7e2e1ae8df Finish the dict->string coredump fix. Need sleep.
Tim Peters <tim.peters@gmail.com>
parents: 18017
diff changeset
964 if (PyObject_Print(pvalue, fp, 0) != 0) {
18017
facff5f50fcc Coredumpers from Michael Hudson, mutating dicts while printing or
Tim Peters <tim.peters@gmail.com>
parents: 18015
diff changeset
965 Py_DECREF(pvalue);
7985
ab4f64561fba Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents: 6490
diff changeset
966 Py_ReprLeave((PyObject*)mp);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
967 return -1;
7985
ab4f64561fba Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents: 6490
diff changeset
968 }
18017
facff5f50fcc Coredumpers from Michael Hudson, mutating dicts while printing or
Tim Peters <tim.peters@gmail.com>
parents: 18015
diff changeset
969 Py_DECREF(pvalue);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
970 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
971 }
43378
7630248b593c Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents: 42333
diff changeset
972 Py_BEGIN_ALLOW_THREADS
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
973 fprintf(fp, "}");
43378
7630248b593c Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents: 42333
diff changeset
974 Py_END_ALLOW_THREADS
7985
ab4f64561fba Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents: 6490
diff changeset
975 Py_ReprLeave((PyObject*)mp);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
976 return 0;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
977 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
978
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
979 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
980 dict_repr(PyDictObject *mp)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
981 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
982 Py_ssize_t i;
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
983 PyObject *s, *temp, *colon = NULL;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
984 PyObject *pieces = NULL, *result = NULL;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
985 PyObject *key, *value;
7985
ab4f64561fba Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents: 6490
diff changeset
986
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
987 i = Py_ReprEnter((PyObject *)mp);
7985
ab4f64561fba Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents: 6490
diff changeset
988 if (i != 0) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
989 return i > 0 ? PyString_FromString("{...}") : NULL;
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
990 }
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
991
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
992 if (mp->ma_used == 0) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
993 result = PyString_FromString("{}");
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
994 goto Done;
7985
ab4f64561fba Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents: 6490
diff changeset
995 }
ab4f64561fba Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents: 6490
diff changeset
996
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
997 pieces = PyList_New(0);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
998 if (pieces == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
999 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1000
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1001 colon = PyString_FromString(": ");
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1002 if (colon == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1003 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1004
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1005 /* Do repr() on each key+value pair, and insert ": " between them.
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1006 Note that repr may mutate the dict. */
18131
85e3fa1eb836 dict_repr: Reuse one of the int vars (minor code simplification).
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
1007 i = 0;
85e3fa1eb836 dict_repr: Reuse one of the int vars (minor code simplification).
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
1008 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1009 int status;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1010 /* Prevent repr from deleting value during key format. */
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1011 Py_INCREF(value);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1012 s = PyObject_Repr(key);
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1013 PyString_Concat(&s, colon);
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1014 PyString_ConcatAndDel(&s, PyObject_Repr(value));
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1015 Py_DECREF(value);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1016 if (s == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1017 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1018 status = PyList_Append(pieces, s);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1019 Py_DECREF(s); /* append created a new ref */
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1020 if (status < 0)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1021 goto Done;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1022 }
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1023
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1024 /* Add "{}" decorations to the first and last items. */
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1025 assert(PyList_GET_SIZE(pieces) > 0);
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1026 s = PyString_FromString("{");
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1027 if (s == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1028 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1029 temp = PyList_GET_ITEM(pieces, 0);
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1030 PyString_ConcatAndDel(&s, temp);
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1031 PyList_SET_ITEM(pieces, 0, s);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1032 if (s == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1033 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1034
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1035 s = PyString_FromString("}");
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1036 if (s == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1037 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1038 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1039 PyString_ConcatAndDel(&temp, s);
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1040 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1041 if (temp == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1042 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1043
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1044 /* Paste them all together with ", " between. */
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1045 s = PyString_FromString(", ");
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1046 if (s == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1047 goto Done;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1048 result = _PyString_Join(s, pieces);
19904
58d50094bf80 Changed the dict implementation to take "string shortcuts" only when
Tim Peters <tim.peters@gmail.com>
parents: 19662
diff changeset
1049 Py_DECREF(s);
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1050
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1051 Done:
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1052 Py_XDECREF(pieces);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1053 Py_XDECREF(colon);
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1054 Py_ReprLeave((PyObject *)mp);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1055 return result;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1056 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1057
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1058 static Py_ssize_t
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1059 dict_length(PyDictObject *mp)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1060 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1061 return mp->ma_used;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1062 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1063
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1064 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1065 dict_subscript(PyDictObject *mp, register PyObject *key)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1066 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1067 PyObject *v;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1330
diff changeset
1068 long hash;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1069 PyDictEntry *ep;
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
1070 assert(mp->ma_table != NULL);
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1071 if (!PyString_CheckExact(key) ||
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1072 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1073 hash = PyObject_Hash(key);
5038
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
1074 if (hash == -1)
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
1075 return NULL;
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
1076 }
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1077 ep = (mp->ma_lookup)(mp, key, hash);
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1078 if (ep == NULL)
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1079 return NULL;
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1080 v = ep->me_value;
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36511
diff changeset
1081 if (v == NULL) {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36511
diff changeset
1082 if (!PyDict_CheckExact(mp)) {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36511
diff changeset
1083 /* Look up __missing__ method if we're a subclass. */
36657
c996a12234c2 Oops. Fix syntax for C89 compilers.
Guido van Rossum <guido@python.org>
parents: 36656
diff changeset
1084 PyObject *missing;
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36511
diff changeset
1085 static PyObject *missing_str = NULL;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36511
diff changeset
1086 if (missing_str == NULL)
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1087 missing_str =
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1088 PyString_InternFromString("__missing__");
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 44339
diff changeset
1089 missing = _PyType_Lookup(Py_TYPE(mp), missing_str);
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36511
diff changeset
1090 if (missing != NULL)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36511
diff changeset
1091 return PyObject_CallFunctionObjArgs(missing,
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36511
diff changeset
1092 (PyObject *)mp, key, NULL);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36511
diff changeset
1093 }
40257
9ab7a7736ca0 Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents: 40225
diff changeset
1094 set_key_error(key);
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36511
diff changeset
1095 return NULL;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36511
diff changeset
1096 }
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1097 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1098 Py_INCREF(v);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1099 return v;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1100 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1101
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1102 static int
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1103 dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1104 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1105 if (w == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1106 return PyDict_DelItem((PyObject *)mp, v);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1107 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1108 return PyDict_SetItem((PyObject *)mp, v, w);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1109 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1110
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1111 static PyMappingMethods dict_as_mapping = {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1112 (lenfunc)dict_length, /*mp_length*/
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1113 (binaryfunc)dict_subscript, /*mp_subscript*/
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1114 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1115 };
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1116
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1117 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1118 dict_keys(register PyDictObject *mp)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1119 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1120 register PyObject *v;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1121 register Py_ssize_t i, j;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1122 PyDictEntry *ep;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1123 Py_ssize_t mask, n;
17535
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1124
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1125 again:
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1126 n = mp->ma_used;
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1127 v = PyList_New(n);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1128 if (v == NULL)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1129 return NULL;
17535
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1130 if (n != mp->ma_used) {
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1131 /* Durnit. The allocations caused the dict to resize.
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1132 * Just start over, this shouldn't normally happen.
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1133 */
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1134 Py_DECREF(v);
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1135 goto again;
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1136 }
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1137 ep = mp->ma_table;
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1138 mask = mp->ma_mask;
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1139 for (i = 0, j = 0; i <= mask; i++) {
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1140 if (ep[i].me_value != NULL) {
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1141 PyObject *key = ep[i].me_key;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1142 Py_INCREF(key);
17535
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1143 PyList_SET_ITEM(v, j, key);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1144 j++;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1145 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1146 }
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1147 assert(j == n);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1148 return v;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1149 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1150
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1151 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1152 dict_values(register PyDictObject *mp)
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1153 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1154 register PyObject *v;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1155 register Py_ssize_t i, j;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1156 PyDictEntry *ep;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1157 Py_ssize_t mask, n;
17535
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1158
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1159 again:
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1160 n = mp->ma_used;
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1161 v = PyList_New(n);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1162 if (v == NULL)
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1163 return NULL;
17535
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1164 if (n != mp->ma_used) {
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1165 /* Durnit. The allocations caused the dict to resize.
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1166 * Just start over, this shouldn't normally happen.
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1167 */
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1168 Py_DECREF(v);
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1169 goto again;
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1170 }
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1171 ep = mp->ma_table;
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1172 mask = mp->ma_mask;
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1173 for (i = 0, j = 0; i <= mask; i++) {
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1174 if (ep[i].me_value != NULL) {
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1175 PyObject *value = ep[i].me_value;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1176 Py_INCREF(value);
17535
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1177 PyList_SET_ITEM(v, j, value);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1178 j++;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1179 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1180 }
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1181 assert(j == n);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1182 return v;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1183 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1184
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1185 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1186 dict_items(register PyDictObject *mp)
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1187 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1188 register PyObject *v;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1189 register Py_ssize_t i, j, n;
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1190 Py_ssize_t mask;
17535
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1191 PyObject *item, *key, *value;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1192 PyDictEntry *ep;
17535
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1193
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1194 /* Preallocate the list of tuples, to avoid allocations during
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1195 * the loop over the items, which could trigger GC, which
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1196 * could resize the dict. :-(
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1197 */
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1198 again:
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1199 n = mp->ma_used;
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1200 v = PyList_New(n);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1201 if (v == NULL)
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1202 return NULL;
17535
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1203 for (i = 0; i < n; i++) {
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1204 item = PyTuple_New(2);
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1205 if (item == NULL) {
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1206 Py_DECREF(v);
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1207 return NULL;
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1208 }
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1209 PyList_SET_ITEM(v, i, item);
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1210 }
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1211 if (n != mp->ma_used) {
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1212 /* Durnit. The allocations caused the dict to resize.
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1213 * Just start over, this shouldn't normally happen.
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1214 */
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1215 Py_DECREF(v);
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1216 goto again;
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1217 }
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1218 /* Nothing we do below makes any function calls. */
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1219 ep = mp->ma_table;
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1220 mask = mp->ma_mask;
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1221 for (i = 0, j = 0; i <= mask; i++) {
31689
0fb5066fc2a7 Factor out a double lookup.
Raymond Hettinger <python@rcn.com>
parents: 31683
diff changeset
1222 if ((value=ep[i].me_value) != NULL) {
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1223 key = ep[i].me_key;
17535
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1224 item = PyList_GET_ITEM(v, j);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1225 Py_INCREF(key);
17535
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1226 PyTuple_SET_ITEM(item, 0, key);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1227 Py_INCREF(value);
17535
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1228 PyTuple_SET_ITEM(item, 1, value);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1229 j++;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1230 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1231 }
17535
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1232 assert(j == n);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1233 return v;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1234 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1235
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
1236 static PyObject *
26583
8991f78817c7 SF 548651: Fix the METH_CLASS implementation.
Tim Peters <tim.peters@gmail.com>
parents: 26562
diff changeset
1237 dict_fromkeys(PyObject *cls, PyObject *args)
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1238 {
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1239 PyObject *seq;
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1240 PyObject *value = Py_None;
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1241 PyObject *it; /* iter(seq) */
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1242 PyObject *key;
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1243 PyObject *d;
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1244 int status;
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1245
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26599
diff changeset
1246 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1247 return NULL;
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1248
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1249 d = PyObject_CallObject(cls, NULL);
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1250 if (d == NULL)
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1251 return NULL;
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1252
43783
9f17a9a5b5ae Optimize dict.fromkeys() with dict inputs. Useful for resetting bag/muliset counts for example.
Raymond Hettinger <python@rcn.com>
parents: 43515
diff changeset
1253 if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) {
9f17a9a5b5ae Optimize dict.fromkeys() with dict inputs. Useful for resetting bag/muliset counts for example.
Raymond Hettinger <python@rcn.com>
parents: 43515
diff changeset
1254 PyDictObject *mp = (PyDictObject *)d;
9f17a9a5b5ae Optimize dict.fromkeys() with dict inputs. Useful for resetting bag/muliset counts for example.
Raymond Hettinger <python@rcn.com>
parents: 43515
diff changeset
1255 PyObject *oldvalue;
9f17a9a5b5ae Optimize dict.fromkeys() with dict inputs. Useful for resetting bag/muliset counts for example.
Raymond Hettinger <python@rcn.com>
parents: 43515
diff changeset
1256 Py_ssize_t pos = 0;
9f17a9a5b5ae Optimize dict.fromkeys() with dict inputs. Useful for resetting bag/muliset counts for example.
Raymond Hettinger <python@rcn.com>
parents: 43515
diff changeset
1257 PyObject *key;
9f17a9a5b5ae Optimize dict.fromkeys() with dict inputs. Useful for resetting bag/muliset counts for example.
Raymond Hettinger <python@rcn.com>
parents: 43515
diff changeset
1258 long hash;
9f17a9a5b5ae Optimize dict.fromkeys() with dict inputs. Useful for resetting bag/muliset counts for example.
Raymond Hettinger <python@rcn.com>
parents: 43515
diff changeset
1259
47847
1dd10b04db47 Issue 3230: Do not the set specific size macro.
Raymond Hettinger <python@rcn.com>
parents: 47829
diff changeset
1260 if (dictresize(mp, Py_SIZE(seq)))
43783
9f17a9a5b5ae Optimize dict.fromkeys() with dict inputs. Useful for resetting bag/muliset counts for example.
Raymond Hettinger <python@rcn.com>
parents: 43515
diff changeset
1261 return NULL;
9f17a9a5b5ae Optimize dict.fromkeys() with dict inputs. Useful for resetting bag/muliset counts for example.
Raymond Hettinger <python@rcn.com>
parents: 43515
diff changeset
1262
9f17a9a5b5ae Optimize dict.fromkeys() with dict inputs. Useful for resetting bag/muliset counts for example.
Raymond Hettinger <python@rcn.com>
parents: 43515
diff changeset
1263 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
9f17a9a5b5ae Optimize dict.fromkeys() with dict inputs. Useful for resetting bag/muliset counts for example.
Raymond Hettinger <python@rcn.com>
parents: 43515
diff changeset
1264 Py_INCREF(key);
9f17a9a5b5ae Optimize dict.fromkeys() with dict inputs. Useful for resetting bag/muliset counts for example.
Raymond Hettinger <python@rcn.com>
parents: 43515
diff changeset
1265 Py_INCREF(value);
9f17a9a5b5ae Optimize dict.fromkeys() with dict inputs. Useful for resetting bag/muliset counts for example.
Raymond Hettinger <python@rcn.com>
parents: 43515
diff changeset
1266 if (insertdict(mp, key, hash, value))
9f17a9a5b5ae Optimize dict.fromkeys() with dict inputs. Useful for resetting bag/muliset counts for example.
Raymond Hettinger <python@rcn.com>
parents: 43515
diff changeset
1267 return NULL;
9f17a9a5b5ae Optimize dict.fromkeys() with dict inputs. Useful for resetting bag/muliset counts for example.
Raymond Hettinger <python@rcn.com>
parents: 43515
diff changeset
1268 }
9f17a9a5b5ae Optimize dict.fromkeys() with dict inputs. Useful for resetting bag/muliset counts for example.
Raymond Hettinger <python@rcn.com>
parents: 43515
diff changeset
1269 return d;
9f17a9a5b5ae Optimize dict.fromkeys() with dict inputs. Useful for resetting bag/muliset counts for example.
Raymond Hettinger <python@rcn.com>
parents: 43515
diff changeset
1270 }
9f17a9a5b5ae Optimize dict.fromkeys() with dict inputs. Useful for resetting bag/muliset counts for example.
Raymond Hettinger <python@rcn.com>
parents: 43515
diff changeset
1271
41248
ec6fb091039b Extend work on rev 52962 and 53829 eliminating redundant PyObject_Hash() calls and fixing set/dict interoperability.
Raymond Hettinger <python@rcn.com>
parents: 40908
diff changeset
1272 if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1273 PyDictObject *mp = (PyDictObject *)d;
41248
ec6fb091039b Extend work on rev 52962 and 53829 eliminating redundant PyObject_Hash() calls and fixing set/dict interoperability.
Raymond Hettinger <python@rcn.com>
parents: 40908
diff changeset
1274 Py_ssize_t pos = 0;
ec6fb091039b Extend work on rev 52962 and 53829 eliminating redundant PyObject_Hash() calls and fixing set/dict interoperability.
Raymond Hettinger <python@rcn.com>
parents: 40908
diff changeset
1275 PyObject *key;
ec6fb091039b Extend work on rev 52962 and 53829 eliminating redundant PyObject_Hash() calls and fixing set/dict interoperability.
Raymond Hettinger <python@rcn.com>
parents: 40908
diff changeset
1276 long hash;
ec6fb091039b Extend work on rev 52962 and 53829 eliminating redundant PyObject_Hash() calls and fixing set/dict interoperability.
Raymond Hettinger <python@rcn.com>
parents: 40908
diff changeset
1277
ec6fb091039b Extend work on rev 52962 and 53829 eliminating redundant PyObject_Hash() calls and fixing set/dict interoperability.
Raymond Hettinger <python@rcn.com>
parents: 40908
diff changeset
1278 if (dictresize(mp, PySet_GET_SIZE(seq)))
ec6fb091039b Extend work on rev 52962 and 53829 eliminating redundant PyObject_Hash() calls and fixing set/dict interoperability.
Raymond Hettinger <python@rcn.com>
parents: 40908
diff changeset
1279 return NULL;
ec6fb091039b Extend work on rev 52962 and 53829 eliminating redundant PyObject_Hash() calls and fixing set/dict interoperability.
Raymond Hettinger <python@rcn.com>
parents: 40908
diff changeset
1280
ec6fb091039b Extend work on rev 52962 and 53829 eliminating redundant PyObject_Hash() calls and fixing set/dict interoperability.
Raymond Hettinger <python@rcn.com>
parents: 40908
diff changeset
1281 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
ec6fb091039b Extend work on rev 52962 and 53829 eliminating redundant PyObject_Hash() calls and fixing set/dict interoperability.
Raymond Hettinger <python@rcn.com>
parents: 40908
diff changeset
1282 Py_INCREF(key);
41270
15a7e0b7e425 Add test and fix for fromkeys() optional argument.
Raymond Hettinger <python@rcn.com>
parents: 41248
diff changeset
1283 Py_INCREF(value);
15a7e0b7e425 Add test and fix for fromkeys() optional argument.
Raymond Hettinger <python@rcn.com>
parents: 41248
diff changeset
1284 if (insertdict(mp, key, hash, value))
41248
ec6fb091039b Extend work on rev 52962 and 53829 eliminating redundant PyObject_Hash() calls and fixing set/dict interoperability.
Raymond Hettinger <python@rcn.com>
parents: 40908
diff changeset
1285 return NULL;
ec6fb091039b Extend work on rev 52962 and 53829 eliminating redundant PyObject_Hash() calls and fixing set/dict interoperability.
Raymond Hettinger <python@rcn.com>
parents: 40908
diff changeset
1286 }
ec6fb091039b Extend work on rev 52962 and 53829 eliminating redundant PyObject_Hash() calls and fixing set/dict interoperability.
Raymond Hettinger <python@rcn.com>
parents: 40908
diff changeset
1287 return d;
ec6fb091039b Extend work on rev 52962 and 53829 eliminating redundant PyObject_Hash() calls and fixing set/dict interoperability.
Raymond Hettinger <python@rcn.com>
parents: 40908
diff changeset
1288 }
ec6fb091039b Extend work on rev 52962 and 53829 eliminating redundant PyObject_Hash() calls and fixing set/dict interoperability.
Raymond Hettinger <python@rcn.com>
parents: 40908
diff changeset
1289
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1290 it = PyObject_GetIter(seq);
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1291 if (it == NULL){
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1292 Py_DECREF(d);
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1293 return NULL;
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1294 }
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1295
43810
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1296 if (PyDict_CheckExact(d)) {
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1297 while ((key = PyIter_Next(it)) != NULL) {
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1298 status = PyDict_SetItem(d, key, value);
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1299 Py_DECREF(key);
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1300 if (status < 0)
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1301 goto Fail;
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1302 }
43810
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1303 } else {
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1304 while ((key = PyIter_Next(it)) != NULL) {
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1305 status = PyObject_SetItem(d, key, value);
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1306 Py_DECREF(key);
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1307 if (status < 0)
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1308 goto Fail;
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1309 }
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1310 }
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1311
43810
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1312 if (PyErr_Occurred())
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1313 goto Fail;
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1314 Py_DECREF(it);
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1315 return d;
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1316
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1317 Fail:
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1318 Py_DECREF(it);
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1319 Py_DECREF(d);
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1320 return NULL;
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1321 }
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1322
31616
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1323 static int
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1324 dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1325 {
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1326 PyObject *arg = NULL;
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1327 int result = 0;
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1328
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1329 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1330 result = -1;
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1331
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1332 else if (arg != NULL) {
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1333 if (PyObject_HasAttrString(arg, "keys"))
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1334 result = PyDict_Merge(self, arg, 1);
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1335 else
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1336 result = PyDict_MergeFromSeq2(self, arg, 1);
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1337 }
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1338 if (result == 0 && kwds != NULL)
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1339 result = PyDict_Merge(self, kwds, 1);
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1340 return result;
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1341 }
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1342
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1343 static PyObject *
31616
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1344 dict_update(PyObject *self, PyObject *args, PyObject *kwds)
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
1345 {
31908
058821d8aefd * Add unittests for iterators that report their length
Raymond Hettinger <python@rcn.com>
parents: 31702
diff changeset
1346 if (dict_update_common(self, args, kwds, "update") != -1)
058821d8aefd * Add unittests for iterators that report their length
Raymond Hettinger <python@rcn.com>
parents: 31702
diff changeset
1347 Py_RETURN_NONE;
058821d8aefd * Add unittests for iterators that report their length
Raymond Hettinger <python@rcn.com>
parents: 31702
diff changeset
1348 return NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1349 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1350
19150
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1351 /* Update unconditionally replaces existing items.
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1352 Merge has a 3rd argument 'override'; if set, it acts like Update,
20683
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1353 otherwise it leaves existing items unchanged.
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1354
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1355 PyDict_{Update,Merge} update/merge from a mapping object.
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1356
21348
12310d643d8d SF bug #491415 PyDict_UpdateFromSeq2() unused
Tim Peters <tim.peters@gmail.com>
parents: 21155
diff changeset
1357 PyDict_MergeFromSeq2 updates/merges from any iterable object
20683
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1358 producing iterable objects of length 2.
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1359 */
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1360
21348
12310d643d8d SF bug #491415 PyDict_UpdateFromSeq2() unused
Tim Peters <tim.peters@gmail.com>
parents: 21155
diff changeset
1361 int
20683
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1362 PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1363 {
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1364 PyObject *it; /* iter(seq2) */
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1365 Py_ssize_t i; /* index into seq2 of current element */
20683
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1366 PyObject *item; /* seq2[i] */
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1367 PyObject *fast; /* item as a 2-tuple or 2-list */
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1368
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1369 assert(d != NULL);
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1370 assert(PyDict_Check(d));
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1371 assert(seq2 != NULL);
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1372
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1373 it = PyObject_GetIter(seq2);
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1374 if (it == NULL)
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1375 return -1;
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1376
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1377 for (i = 0; ; ++i) {
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1378 PyObject *key, *value;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1379 Py_ssize_t n;
20683
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1380
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1381 fast = NULL;
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1382 item = PyIter_Next(it);
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1383 if (item == NULL) {
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1384 if (PyErr_Occurred())
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1385 goto Fail;
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1386 break;
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1387 }
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1388
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1389 /* Convert item to sequence, and verify length 2. */
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1390 fast = PySequence_Fast(item, "");
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1391 if (fast == NULL) {
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1392 if (PyErr_ExceptionMatches(PyExc_TypeError))
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1393 PyErr_Format(PyExc_TypeError,
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1394 "cannot convert dictionary update "
38501
ec5dc8a49edf Use Py_SAFE_DOWNCAST for safety. Fix format strings. Remove 2 more stray | in comment
Neal Norwitz <nnorwitz@gmail.com>
parents: 38500
diff changeset
1395 "sequence element #%zd to a sequence",
20683
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1396 i);
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1397 goto Fail;
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1398 }
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1399 n = PySequence_Fast_GET_SIZE(fast);
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1400 if (n != 2) {
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1401 PyErr_Format(PyExc_ValueError,
38501
ec5dc8a49edf Use Py_SAFE_DOWNCAST for safety. Fix format strings. Remove 2 more stray | in comment
Neal Norwitz <nnorwitz@gmail.com>
parents: 38500
diff changeset
1402 "dictionary update sequence element #%zd "
36510
0f3894bb2ada Support %zd in PyErr_Format and PyString_FromFormat.
Martin v. Löwis <martin@v.loewis.de>
parents: 36509
diff changeset
1403 "has length %zd; 2 is required",
36511
1596ff5c79b5 Revert 42400.
Martin v. Löwis <martin@v.loewis.de>
parents: 36510
diff changeset
1404 i, n);
20683
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1405 goto Fail;
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1406 }
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1407
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1408 /* Update/merge with this (key, value) pair. */
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1409 key = PySequence_Fast_GET_ITEM(fast, 0);
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1410 value = PySequence_Fast_GET_ITEM(fast, 1);
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1411 if (override || PyDict_GetItem(d, key) == NULL) {
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1412 int status = PyDict_SetItem(d, key, value);
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1413 if (status < 0)
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1414 goto Fail;
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1415 }
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1416 Py_DECREF(fast);
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1417 Py_DECREF(item);
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1418 }
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1419
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1420 i = 0;
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1421 goto Return;
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1422 Fail:
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1423 Py_XDECREF(item);
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1424 Py_XDECREF(fast);
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1425 i = -1;
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1426 Return:
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1427 Py_DECREF(it);
38501
ec5dc8a49edf Use Py_SAFE_DOWNCAST for safety. Fix format strings. Remove 2 more stray | in comment
Neal Norwitz <nnorwitz@gmail.com>
parents: 38500
diff changeset
1428 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
20683
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1429 }
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1430
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1431 int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1432 PyDict_Update(PyObject *a, PyObject *b)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1433 {
19150
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1434 return PyDict_Merge(a, b, 1);
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1435 }
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1436
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1437 int
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1438 PyDict_Merge(PyObject *a, PyObject *b, int override)
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1439 {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1440 register PyDictObject *mp, *other;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1441 register Py_ssize_t i;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1442 PyDictEntry *entry;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1443
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1444 /* We accept for the argument either a concrete dictionary object,
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1445 * or an abstract "mapping" object. For the former, we can do
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1446 * things quite efficiently. For the latter, we only require that
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1447 * PyMapping_Keys() and PyObject_GetItem() be supported.
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1448 */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1449 if (a == NULL || !PyDict_Check(a) || b == NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1450 PyErr_BadInternalCall();
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1451 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1452 }
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1453 mp = (PyDictObject*)a;
44949
ddb1e499a01f Changes 54857 and 54840 broke code and were reverted in Py2.5 just before
Raymond Hettinger <python@rcn.com>
parents: 44350
diff changeset
1454 if (PyDict_Check(b)) {
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1455 other = (PyDictObject*)b;
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1456 if (other == mp || other->ma_used == 0)
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1457 /* a.update(a) or a.update({}); nothing to do */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1458 return 0;
34955
9339c99a2982 SF patch #1200051: Small optimization for PyDict_Merge()
Raymond Hettinger <python@rcn.com>
parents: 34909
diff changeset
1459 if (mp->ma_used == 0)
9339c99a2982 SF patch #1200051: Small optimization for PyDict_Merge()
Raymond Hettinger <python@rcn.com>
parents: 34909
diff changeset
1460 /* Since the target dict is empty, PyDict_GetItem()
9339c99a2982 SF patch #1200051: Small optimization for PyDict_Merge()
Raymond Hettinger <python@rcn.com>
parents: 34909
diff changeset
1461 * always returns NULL. Setting override to 1
9339c99a2982 SF patch #1200051: Small optimization for PyDict_Merge()
Raymond Hettinger <python@rcn.com>
parents: 34909
diff changeset
1462 * skips the unnecessary test.
9339c99a2982 SF patch #1200051: Small optimization for PyDict_Merge()
Raymond Hettinger <python@rcn.com>
parents: 34909
diff changeset
1463 */
9339c99a2982 SF patch #1200051: Small optimization for PyDict_Merge()
Raymond Hettinger <python@rcn.com>
parents: 34909
diff changeset
1464 override = 1;
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1465 /* Do one big resize at the start, rather than
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1466 * incrementally resizing as we insert new items. Expect
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1467 * that there will be no (or few) overlapping keys.
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1468 */
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1469 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
29003
013d95c346d2 SF patch #729395: Dictionary tuning
Raymond Hettinger <python@rcn.com>
parents: 28984
diff changeset
1470 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1471 return -1;
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1472 }
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1473 for (i = 0; i <= other->ma_mask; i++) {
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1474 entry = &other->ma_table[i];
19150
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1475 if (entry->me_value != NULL &&
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1476 (override ||
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1477 PyDict_GetItem(a, entry->me_key) == NULL)) {
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1478 Py_INCREF(entry->me_key);
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1479 Py_INCREF(entry->me_value);
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1480 if (insertdict(mp, entry->me_key,
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1481 (long)entry->me_hash,
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1482 entry->me_value) != 0)
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1483 return -1;
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1484 }
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1485 }
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1486 }
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1487 else {
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1488 /* Do it the generic, slower way */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1489 PyObject *keys = PyMapping_Keys(b);
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1490 PyObject *iter;
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1491 PyObject *key, *value;
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1492 int status;
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1493
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1494 if (keys == NULL)
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1495 /* Docstring says this is equivalent to E.keys() so
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1496 * if E doesn't have a .keys() method we want
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1497 * AttributeError to percolate up. Might as well
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1498 * do the same for any other error.
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1499 */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1500 return -1;
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1501
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1502 iter = PyObject_GetIter(keys);
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1503 Py_DECREF(keys);
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1504 if (iter == NULL)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1505 return -1;
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1506
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1507 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
19150
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1508 if (!override && PyDict_GetItem(a, key) != NULL) {
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1509 Py_DECREF(key);
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1510 continue;
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1511 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1512 value = PyObject_GetItem(b, key);
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1513 if (value == NULL) {
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1514 Py_DECREF(iter);
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1515 Py_DECREF(key);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1516 return -1;
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1517 }
19150
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1518 status = PyDict_SetItem(a, key, value);
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1519 Py_DECREF(key);
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1520 Py_DECREF(value);
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1521 if (status < 0) {
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1522 Py_DECREF(iter);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1523 return -1;
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1524 }
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1525 }
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1526 Py_DECREF(iter);
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1527 if (PyErr_Occurred())
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1528 /* Iterator completed, via error */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1529 return -1;
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
1530 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1531 return 0;
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
1532 }
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
1533
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
1534 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1535 dict_copy(register PyDictObject *mp)
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
1536 {
12126
e6214ad2a60c Add PyDict_Copy() function to C API for dicts. It returns a new
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
1537 return PyDict_Copy((PyObject*)mp);
e6214ad2a60c Add PyDict_Copy() function to C API for dicts. It returns a new
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
1538 }
e6214ad2a60c Add PyDict_Copy() function to C API for dicts. It returns a new
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
1539
e6214ad2a60c Add PyDict_Copy() function to C API for dicts. It returns a new
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
1540 PyObject *
13341
3f1bd4782628 Removed Py_PROTO and switched to ANSI C declarations in the dict
Tim Peters <tim.peters@gmail.com>
parents: 13265
diff changeset
1541 PyDict_Copy(PyObject *o)
12126
e6214ad2a60c Add PyDict_Copy() function to C API for dicts. It returns a new
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
1542 {
31620
47890c395eba Factor out code common to PyDict_Copy() and PyDict_Merge().
Raymond Hettinger <python@rcn.com>
parents: 31616
diff changeset
1543 PyObject *copy;
12126
e6214ad2a60c Add PyDict_Copy() function to C API for dicts. It returns a new
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
1544
e6214ad2a60c Add PyDict_Copy() function to C API for dicts. It returns a new
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
1545 if (o == NULL || !PyDict_Check(o)) {
e6214ad2a60c Add PyDict_Copy() function to C API for dicts. It returns a new
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
1546 PyErr_BadInternalCall();
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
1547 return NULL;
12126
e6214ad2a60c Add PyDict_Copy() function to C API for dicts. It returns a new
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
1548 }
31620
47890c395eba Factor out code common to PyDict_Copy() and PyDict_Merge().
Raymond Hettinger <python@rcn.com>
parents: 31616
diff changeset
1549 copy = PyDict_New();
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
1550 if (copy == NULL)
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
1551 return NULL;
31620
47890c395eba Factor out code common to PyDict_Copy() and PyDict_Merge().
Raymond Hettinger <python@rcn.com>
parents: 31616
diff changeset
1552 if (PyDict_Merge(copy, o, 1) == 0)
47890c395eba Factor out code common to PyDict_Copy() and PyDict_Merge().
Raymond Hettinger <python@rcn.com>
parents: 31616
diff changeset
1553 return copy;
47890c395eba Factor out code common to PyDict_Copy() and PyDict_Merge().
Raymond Hettinger <python@rcn.com>
parents: 31616
diff changeset
1554 Py_DECREF(copy);
34909
7267fc40adac SF bug #1183742: PyDict_Copy() can return non-NULL value on error
Raymond Hettinger <python@rcn.com>
parents: 34544
diff changeset
1555 return NULL;
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
1556 }
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
1557
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1558 Py_ssize_t
13341
3f1bd4782628 Removed Py_PROTO and switched to ANSI C declarations in the dict
Tim Peters <tim.peters@gmail.com>
parents: 13265
diff changeset
1559 PyDict_Size(PyObject *mp)
1398
1d841587aede Added getmappingsize(). (Needed by previous checkin of posixmodule.c)
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
1560 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1561 if (mp == NULL || !PyDict_Check(mp)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1562 PyErr_BadInternalCall();
31545
391011840a03 Oops. Return -1 to distinguish error from empty dict.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 31174
diff changeset
1563 return -1;
1398
1d841587aede Added getmappingsize(). (Needed by previous checkin of posixmodule.c)
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
1564 }
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1565 return ((PyDictObject *)mp)->ma_used;
1398
1d841587aede Added getmappingsize(). (Needed by previous checkin of posixmodule.c)
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
1566 }
1d841587aede Added getmappingsize(). (Needed by previous checkin of posixmodule.c)
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
1567
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1568 PyObject *
13341
3f1bd4782628 Removed Py_PROTO and switched to ANSI C declarations in the dict
Tim Peters <tim.peters@gmail.com>
parents: 13265
diff changeset
1569 PyDict_Keys(PyObject *mp)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1570 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1571 if (mp == NULL || !PyDict_Check(mp)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1572 PyErr_BadInternalCall();
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1573 return NULL;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1574 }
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1575 return dict_keys((PyDictObject *)mp);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1576 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1577
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1578 PyObject *
13341
3f1bd4782628 Removed Py_PROTO and switched to ANSI C declarations in the dict
Tim Peters <tim.peters@gmail.com>
parents: 13265
diff changeset
1579 PyDict_Values(PyObject *mp)
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1580 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1581 if (mp == NULL || !PyDict_Check(mp)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1582 PyErr_BadInternalCall();
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1583 return NULL;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1584 }
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1585 return dict_values((PyDictObject *)mp);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1586 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1587
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1588 PyObject *
13341
3f1bd4782628 Removed Py_PROTO and switched to ANSI C declarations in the dict
Tim Peters <tim.peters@gmail.com>
parents: 13265
diff changeset
1589 PyDict_Items(PyObject *mp)
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1590 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1591 if (mp == NULL || !PyDict_Check(mp)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1592 PyErr_BadInternalCall();
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1593 return NULL;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1594 }
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1595 return dict_items((PyDictObject *)mp);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1596 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1597
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1598 /* Subroutine which returns the smallest key in a for which b's value
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1599 is different or absent. The value is returned too, through the
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1600 pval argument. Both are NULL if no key in a is found for which b's status
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1601 differs. The refcounts on (and only on) non-NULL *pval and function return
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1602 values must be decremented by the caller (characterize() increments them
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1603 to ensure that mutating comparison and PyDict_GetItem calls can't delete
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1604 them before the caller is done looking at them). */
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1605
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1606 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1607 characterize(PyDictObject *a, PyDictObject *b, PyObject **pval)
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1608 {
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1609 PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1610 PyObject *aval = NULL; /* a[akey] */
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1611 Py_ssize_t i;
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1612 int cmp;
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1613
18032
b99979d0f9e7 Store the mask instead of the size in dictobjects. The mask is more
Tim Peters <tim.peters@gmail.com>
parents: 18023
diff changeset
1614 for (i = 0; i <= a->ma_mask; i++) {
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1615 PyObject *thiskey, *thisaval, *thisbval;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1616 if (a->ma_table[i].me_value == NULL)
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1617 continue;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1618 thiskey = a->ma_table[i].me_key;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1619 Py_INCREF(thiskey); /* keep alive across compares */
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1620 if (akey != NULL) {
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1621 cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1622 if (cmp < 0) {
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1623 Py_DECREF(thiskey);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1624 goto Fail;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1625 }
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1626 if (cmp > 0 ||
18032
b99979d0f9e7 Store the mask instead of the size in dictobjects. The mask is more
Tim Peters <tim.peters@gmail.com>
parents: 18023
diff changeset
1627 i > a->ma_mask ||
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1628 a->ma_table[i].me_value == NULL)
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1629 {
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1630 /* Not the *smallest* a key; or maybe it is
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1631 * but the compare shrunk the dict so we can't
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1632 * find its associated value anymore; or
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1633 * maybe it is but the compare deleted the
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1634 * a[thiskey] entry.
38501
ec5dc8a49edf Use Py_SAFE_DOWNCAST for safety. Fix format strings. Remove 2 more stray | in comment
Neal Norwitz <nnorwitz@gmail.com>
parents: 38500
diff changeset
1635 */
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1636 Py_DECREF(thiskey);
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1637 continue;
15974
5b9103e62fac Rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15708
diff changeset
1638 }
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1639 }
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1640
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1641 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1642 thisaval = a->ma_table[i].me_value;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1643 assert(thisaval);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1644 Py_INCREF(thisaval); /* keep alive */
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1645 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1646 if (thisbval == NULL)
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1647 cmp = 0;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1648 else {
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1649 /* both dicts have thiskey: same values? */
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1650 cmp = PyObject_RichCompareBool(
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1651 thisaval, thisbval, Py_EQ);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1652 if (cmp < 0) {
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1653 Py_DECREF(thiskey);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1654 Py_DECREF(thisaval);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1655 goto Fail;
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1656 }
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1657 }
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1658 if (cmp == 0) {
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1659 /* New winner. */
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1660 Py_XDECREF(akey);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1661 Py_XDECREF(aval);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1662 akey = thiskey;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1663 aval = thisaval;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1664 }
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1665 else {
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1666 Py_DECREF(thiskey);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1667 Py_DECREF(thisaval);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1668 }
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1669 }
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1670 *pval = aval;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1671 return akey;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1672
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1673 Fail:
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1674 Py_XDECREF(akey);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1675 Py_XDECREF(aval);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1676 *pval = NULL;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1677 return NULL;
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1678 }
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1679
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1680 static int
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1681 dict_compare(PyDictObject *a, PyDictObject *b)
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1682 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1683 PyObject *adiff, *bdiff, *aval, *bval;
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1684 int res;
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1685
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1686 /* Compare lengths first */
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1687 if (a->ma_used < b->ma_used)
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1688 return -1; /* a is shorter */
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1689 else if (a->ma_used > b->ma_used)
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1690 return 1; /* b is shorter */
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1691
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1692 /* Same length -- check all keys */
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1693 bdiff = bval = NULL;
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1694 adiff = characterize(a, b, &aval);
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1695 if (adiff == NULL) {
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1696 assert(!aval);
17771
fb103e3da2ae Repair typo in comment.
Tim Peters <tim.peters@gmail.com>
parents: 17756
diff changeset
1697 /* Either an error, or a is a subset with the same length so
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1698 * must be equal.
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1699 */
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1700 res = PyErr_Occurred() ? -1 : 0;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1701 goto Finished;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1702 }
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1703 bdiff = characterize(b, a, &bval);
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1704 if (bdiff == NULL && PyErr_Occurred()) {
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1705 assert(!bval);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1706 res = -1;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1707 goto Finished;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1708 }
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1709 res = 0;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1710 if (bdiff) {
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1711 /* bdiff == NULL "should be" impossible now, but perhaps
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1712 * the last comparison done by the characterize() on a had
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1713 * the side effect of making the dicts equal!
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1714 */
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1715 res = PyObject_Compare(adiff, bdiff);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1716 }
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1717 if (res == 0 && bval != NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1718 res = PyObject_Compare(aval, bval);
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1719
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1720 Finished:
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1721 Py_XDECREF(adiff);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1722 Py_XDECREF(bdiff);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1723 Py_XDECREF(aval);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1724 Py_XDECREF(bval);
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1725 return res;
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1726 }
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1727
17704
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1728 /* Return 1 if dicts equal, 0 if not, -1 if error.
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1729 * Gets out as soon as any difference is detected.
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1730 * Uses only Py_EQ comparison.
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1731 */
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1732 static int
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1733 dict_equal(PyDictObject *a, PyDictObject *b)
17704
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1734 {
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1735 Py_ssize_t i;
17704
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1736
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1737 if (a->ma_used != b->ma_used)
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1738 /* can't be equal if # of entries differ */
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1739 return 0;
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
1740
17704
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1741 /* Same # of entries -- check all of 'em. Exit early on any diff. */
18032
b99979d0f9e7 Store the mask instead of the size in dictobjects. The mask is more
Tim Peters <tim.peters@gmail.com>
parents: 18023
diff changeset
1742 for (i = 0; i <= a->ma_mask; i++) {
17704
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1743 PyObject *aval = a->ma_table[i].me_value;
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1744 if (aval != NULL) {
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1745 int cmp;
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1746 PyObject *bval;
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1747 PyObject *key = a->ma_table[i].me_key;
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1748 /* temporarily bump aval's refcount to ensure it stays
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1749 alive until we're done with it */
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1750 Py_INCREF(aval);
39745
08ca476f8a34 Fix SF bug #1546288, crash in dict_equal.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39376
diff changeset
1751 /* ditto for key */
08ca476f8a34 Fix SF bug #1546288, crash in dict_equal.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39376
diff changeset
1752 Py_INCREF(key);
17704
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1753 bval = PyDict_GetItem((PyObject *)b, key);
39745
08ca476f8a34 Fix SF bug #1546288, crash in dict_equal.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39376
diff changeset
1754 Py_DECREF(key);
17704
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1755 if (bval == NULL) {
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1756 Py_DECREF(aval);
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1757 return 0;
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1758 }
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1759 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1760 Py_DECREF(aval);
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1761 if (cmp <= 0) /* error or not equal */
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1762 return cmp;
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1763 }
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1764 }
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1765 return 1;
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1766 }
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1767
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1768 static PyObject *
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1769 dict_richcompare(PyObject *v, PyObject *w, int op)
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1770 {
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1771 int cmp;
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1772 PyObject *res;
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1773
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1774 if (!PyDict_Check(v) || !PyDict_Check(w)) {
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1775 res = Py_NotImplemented;
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1776 }
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1777 else if (op == Py_EQ || op == Py_NE) {
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1778 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
17704
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1779 if (cmp < 0)
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1780 return NULL;
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1781 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1782 }
45860
50771a3fad2c Add py3k warnings for object, type, cell and dict comparisons. This should resolve issue2342 and partly resolve issue2373.
Steven Bethard <steven.bethard@gmail.com>
parents: 45503
diff changeset
1783 else {
50771a3fad2c Add py3k warnings for object, type, cell and dict comparisons. This should resolve issue2342 and partly resolve issue2373.
Steven Bethard <steven.bethard@gmail.com>
parents: 45503
diff changeset
1784 /* Py3K warning if comparison isn't == or != */
46573
aef3501d8e77 Use PyErr_WarnPy3k throughout
Benjamin Peterson <benjamin@python.org>
parents: 46076
diff changeset
1785 if (PyErr_WarnPy3k("dict inequality comparisons not supported "
46578
7670005da8c5 A little reformating of Py3k warnings
Benjamin Peterson <benjamin@python.org>
parents: 46573
diff changeset
1786 "in 3.x", 1) < 0) {
45860
50771a3fad2c Add py3k warnings for object, type, cell and dict comparisons. This should resolve issue2342 and partly resolve issue2373.
Steven Bethard <steven.bethard@gmail.com>
parents: 45503
diff changeset
1787 return NULL;
50771a3fad2c Add py3k warnings for object, type, cell and dict comparisons. This should resolve issue2342 and partly resolve issue2373.
Steven Bethard <steven.bethard@gmail.com>
parents: 45503
diff changeset
1788 }
17775
dce1f53451a5 Restore dicts' tp_compare slot, and change dict_richcompare to say it
Tim Peters <tim.peters@gmail.com>
parents: 17771
diff changeset
1789 res = Py_NotImplemented;
45860
50771a3fad2c Add py3k warnings for object, type, cell and dict comparisons. This should resolve issue2342 and partly resolve issue2373.
Steven Bethard <steven.bethard@gmail.com>
parents: 45503
diff changeset
1790 }
17704
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1791 Py_INCREF(res);
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1792 return res;
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1793 }
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1794
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1795 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1796 dict_contains(register PyDictObject *mp, PyObject *key)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1797 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1798 long hash;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1799 PyDictEntry *ep;
38540
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
1800
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1801 if (!PyString_CheckExact(key) ||
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1802 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1803 hash = PyObject_Hash(key);
5038
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
1804 if (hash == -1)
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
1805 return NULL;
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
1806 }
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1807 ep = (mp->ma_lookup)(mp, key, hash);
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1808 if (ep == NULL)
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1809 return NULL;
38540
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
1810 return PyBool_FromLong(ep->me_value != NULL);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1811 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1812
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1813 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1814 dict_has_key(register PyDictObject *mp, PyObject *key)
41832
a0ee3b2ba361 Add -3 option to the interpreter to warn about features that are
Neal Norwitz <nnorwitz@gmail.com>
parents: 41270
diff changeset
1815 {
46573
aef3501d8e77 Use PyErr_WarnPy3k throughout
Benjamin Peterson <benjamin@python.org>
parents: 46076
diff changeset
1816 if (PyErr_WarnPy3k("dict.has_key() not supported in 3.x; "
46578
7670005da8c5 A little reformating of Py3k warnings
Benjamin Peterson <benjamin@python.org>
parents: 46573
diff changeset
1817 "use the in operator", 1) < 0)
41832
a0ee3b2ba361 Add -3 option to the interpreter to warn about features that are
Neal Norwitz <nnorwitz@gmail.com>
parents: 41270
diff changeset
1818 return NULL;
a0ee3b2ba361 Add -3 option to the interpreter to warn about features that are
Neal Norwitz <nnorwitz@gmail.com>
parents: 41270
diff changeset
1819 return dict_contains(mp, key);
a0ee3b2ba361 Add -3 option to the interpreter to warn about features that are
Neal Norwitz <nnorwitz@gmail.com>
parents: 41270
diff changeset
1820 }
a0ee3b2ba361 Add -3 option to the interpreter to warn about features that are
Neal Norwitz <nnorwitz@gmail.com>
parents: 41270
diff changeset
1821
a0ee3b2ba361 Add -3 option to the interpreter to warn about features that are
Neal Norwitz <nnorwitz@gmail.com>
parents: 41270
diff changeset
1822 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1823 dict_get(register PyDictObject *mp, PyObject *args)
6395
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1824 {
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1825 PyObject *key;
6488
37c8d7f967c7 dict_get(): Fixed a couple of stupid mistakes which caused crashes.
Barry Warsaw <barry@python.org>
parents: 6395
diff changeset
1826 PyObject *failobj = Py_None;
6395
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1827 PyObject *val = NULL;
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1828 long hash;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1829 PyDictEntry *ep;
6395
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1830
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26599
diff changeset
1831 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
6395
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1832 return NULL;
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1833
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1834 if (!PyString_CheckExact(key) ||
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1835 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
6395
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1836 hash = PyObject_Hash(key);
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1837 if (hash == -1)
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1838 return NULL;
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1839 }
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1840 ep = (mp->ma_lookup)(mp, key, hash);
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1841 if (ep == NULL)
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1842 return NULL;
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1843 val = ep->me_value;
6395
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1844 if (val == NULL)
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1845 val = failobj;
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1846 Py_INCREF(val);
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1847 return val;
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1848 }
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1849
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1850
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1851 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1852 dict_setdefault(register PyDictObject *mp, PyObject *args)
13892
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1853 {
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1854 PyObject *key;
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1855 PyObject *failobj = Py_None;
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1856 PyObject *val = NULL;
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1857 long hash;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1858 PyDictEntry *ep;
13892
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1859
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26599
diff changeset
1860 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
13892
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1861 return NULL;
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1862
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1863 if (!PyString_CheckExact(key) ||
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1864 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
13892
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1865 hash = PyObject_Hash(key);
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1866 if (hash == -1)
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1867 return NULL;
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1868 }
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1869 ep = (mp->ma_lookup)(mp, key, hash);
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1870 if (ep == NULL)
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1871 return NULL;
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1872 val = ep->me_value;
13892
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1873 if (val == NULL) {
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1874 val = failobj;
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1875 if (PyDict_SetItem((PyObject*)mp, key, failobj))
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1876 val = NULL;
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1877 }
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1878 Py_XINCREF(val);
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1879 return val;
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1880 }
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1881
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1882
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1883 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1884 dict_clear(register PyDictObject *mp)
5209
2ec9c933afb8 Add clear() method to dictionary objects.
Guido van Rossum <guido@python.org>
parents: 5059
diff changeset
1885 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1886 PyDict_Clear((PyObject *)mp);
31908
058821d8aefd * Add unittests for iterators that report their length
Raymond Hettinger <python@rcn.com>
parents: 31702
diff changeset
1887 Py_RETURN_NONE;
5209
2ec9c933afb8 Add clear() method to dictionary objects.
Guido van Rossum <guido@python.org>
parents: 5059
diff changeset
1888 }
2ec9c933afb8 Add clear() method to dictionary objects.
Guido van Rossum <guido@python.org>
parents: 5059
diff changeset
1889
15520
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
1890 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1891 dict_pop(PyDictObject *mp, PyObject *args)
22994
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1892 {
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1893 long hash;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1894 PyDictEntry *ep;
22994
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1895 PyObject *old_value, *old_key;
28211
92e3b611dc9f SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents: 27902
diff changeset
1896 PyObject *key, *deflt = NULL;
22994
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1897
28211
92e3b611dc9f SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents: 27902
diff changeset
1898 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
92e3b611dc9f SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents: 27902
diff changeset
1899 return NULL;
22994
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1900 if (mp->ma_used == 0) {
28211
92e3b611dc9f SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents: 27902
diff changeset
1901 if (deflt) {
92e3b611dc9f SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents: 27902
diff changeset
1902 Py_INCREF(deflt);
92e3b611dc9f SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents: 27902
diff changeset
1903 return deflt;
92e3b611dc9f SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents: 27902
diff changeset
1904 }
22994
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1905 PyErr_SetString(PyExc_KeyError,
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1906 "pop(): dictionary is empty");
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1907 return NULL;
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1908 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1909 if (!PyString_CheckExact(key) ||
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1910 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
22994
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1911 hash = PyObject_Hash(key);
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1912 if (hash == -1)
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1913 return NULL;
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1914 }
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1915 ep = (mp->ma_lookup)(mp, key, hash);
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1916 if (ep == NULL)
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1917 return NULL;
22994
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1918 if (ep->me_value == NULL) {
28211
92e3b611dc9f SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents: 27902
diff changeset
1919 if (deflt) {
92e3b611dc9f SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents: 27902
diff changeset
1920 Py_INCREF(deflt);
92e3b611dc9f SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents: 27902
diff changeset
1921 return deflt;
92e3b611dc9f SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents: 27902
diff changeset
1922 }
40257
9ab7a7736ca0 Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents: 40225
diff changeset
1923 set_key_error(key);
22994
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1924 return NULL;
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1925 }
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1926 old_key = ep->me_key;
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1927 Py_INCREF(dummy);
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1928 ep->me_key = dummy;
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1929 old_value = ep->me_value;
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1930 ep->me_value = NULL;
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1931 mp->ma_used--;
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1932 Py_DECREF(old_key);
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1933 return old_value;
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1934 }
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1935
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1936 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1937 dict_popitem(PyDictObject *mp)
15520
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
1938 {
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1939 Py_ssize_t i = 0;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1940 PyDictEntry *ep;
15520
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
1941 PyObject *res;
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
1942
18015
0c1a04e98134 dict_popitem(): Repaired last-second 2.1 comment, which misidentified the
Tim Peters <tim.peters@gmail.com>
parents: 18014
diff changeset
1943 /* Allocate the result tuple before checking the size. Believe it
0c1a04e98134 dict_popitem(): Repaired last-second 2.1 comment, which misidentified the
Tim Peters <tim.peters@gmail.com>
parents: 18014
diff changeset
1944 * or not, this allocation could trigger a garbage collection which
0c1a04e98134 dict_popitem(): Repaired last-second 2.1 comment, which misidentified the
Tim Peters <tim.peters@gmail.com>
parents: 18014
diff changeset
1945 * could empty the dict, so if we checked the size first and that
0c1a04e98134 dict_popitem(): Repaired last-second 2.1 comment, which misidentified the
Tim Peters <tim.peters@gmail.com>
parents: 18014
diff changeset
1946 * happened, the result would be an infinite loop (searching for an
0c1a04e98134 dict_popitem(): Repaired last-second 2.1 comment, which misidentified the
Tim Peters <tim.peters@gmail.com>
parents: 18014
diff changeset
1947 * entry that no longer exists). Note that the usual popitem()
0c1a04e98134 dict_popitem(): Repaired last-second 2.1 comment, which misidentified the
Tim Peters <tim.peters@gmail.com>
parents: 18014
diff changeset
1948 * idiom is "while d: k, v = d.popitem()". so needing to throw the
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1949 * tuple away if the dict *is* empty isn't a significant
18015
0c1a04e98134 dict_popitem(): Repaired last-second 2.1 comment, which misidentified the
Tim Peters <tim.peters@gmail.com>
parents: 18014
diff changeset
1950 * inefficiency -- possible, but unlikely in practice.
17535
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1951 */
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1952 res = PyTuple_New(2);
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1953 if (res == NULL)
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1954 return NULL;
17536
d2da681ccb54 Tim pointed out a remaining vulnerability in popitem(): the
Guido van Rossum <guido@python.org>
parents: 17535
diff changeset
1955 if (mp->ma_used == 0) {
d2da681ccb54 Tim pointed out a remaining vulnerability in popitem(): the
Guido van Rossum <guido@python.org>
parents: 17535
diff changeset
1956 Py_DECREF(res);
d2da681ccb54 Tim pointed out a remaining vulnerability in popitem(): the
Guido van Rossum <guido@python.org>
parents: 17535
diff changeset
1957 PyErr_SetString(PyExc_KeyError,
d2da681ccb54 Tim pointed out a remaining vulnerability in popitem(): the
Guido van Rossum <guido@python.org>
parents: 17535
diff changeset
1958 "popitem(): dictionary is empty");
d2da681ccb54 Tim pointed out a remaining vulnerability in popitem(): the
Guido van Rossum <guido@python.org>
parents: 17535
diff changeset
1959 return NULL;
d2da681ccb54 Tim pointed out a remaining vulnerability in popitem(): the
Guido van Rossum <guido@python.org>
parents: 17535
diff changeset
1960 }
15520
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
1961 /* Set ep to "the first" dict entry with a value. We abuse the hash
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
1962 * field of slot 0 to hold a search finger:
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
1963 * If slot 0 has a value, use slot 0.
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
1964 * Else slot 0 is being used to hold a search finger,
38501
ec5dc8a49edf Use Py_SAFE_DOWNCAST for safety. Fix format strings. Remove 2 more stray | in comment
Neal Norwitz <nnorwitz@gmail.com>
parents: 38500
diff changeset
1965 * and we use its hash value as the first index to look.
15520
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
1966 */
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
1967 ep = &mp->ma_table[0];
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
1968 if (ep->me_value == NULL) {
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1969 i = ep->me_hash;
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
1970 /* The hash field may be a real hash value, or it may be a
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
1971 * legit search finger, or it may be a once-legit search
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
1972 * finger that's out of bounds now because it wrapped around
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
1973 * or the table shrunk -- simply make sure it's in bounds now.
15520
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
1974 */
18032
b99979d0f9e7 Store the mask instead of the size in dictobjects. The mask is more
Tim Peters <tim.peters@gmail.com>
parents: 18023
diff changeset
1975 if (i > mp->ma_mask || i < 1)
15520
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
1976 i = 1; /* skip slot 0 */
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
1977 while ((ep = &mp->ma_table[i])->me_value == NULL) {
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
1978 i++;
18032
b99979d0f9e7 Store the mask instead of the size in dictobjects. The mask is more
Tim Peters <tim.peters@gmail.com>
parents: 18023
diff changeset
1979 if (i > mp->ma_mask)
15520
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
1980 i = 1;
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
1981 }
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
1982 }
17535
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1983 PyTuple_SET_ITEM(res, 0, ep->me_key);
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1984 PyTuple_SET_ITEM(res, 1, ep->me_value);
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1985 Py_INCREF(dummy);
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1986 ep->me_key = dummy;
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1987 ep->me_value = NULL;
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1988 mp->ma_used--;
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1989 assert(mp->ma_table[0].me_value == NULL);
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1990 mp->ma_table[0].me_hash = i + 1; /* next place to start */
15520
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
1991 return res;
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
1992 }
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
1993
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
1994 static int
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
1995 dict_traverse(PyObject *op, visitproc visit, void *arg)
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
1996 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1997 Py_ssize_t i = 0;
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
1998 PyObject *pk;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
1999 PyObject *pv;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2000
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2001 while (PyDict_Next(op, &i, &pk, &pv)) {
37756
7b2e436a4a21 Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents: 37638
diff changeset
2002 Py_VISIT(pk);
7b2e436a4a21 Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents: 37638
diff changeset
2003 Py_VISIT(pv);
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2004 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2005 return 0;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2006 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2007
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2008 static int
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2009 dict_tp_clear(PyObject *op)
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2010 {
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2011 PyDict_Clear(op);
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2012 return 0;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2013 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2014
15560
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2015
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2016 extern PyTypeObject PyDictIterKey_Type; /* Forward */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2017 extern PyTypeObject PyDictIterValue_Type; /* Forward */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2018 extern PyTypeObject PyDictIterItem_Type; /* Forward */
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2019 static PyObject *dictiter_new(PyDictObject *, PyTypeObject *);
17644
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2020
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2021 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2022 dict_iterkeys(PyDictObject *dict)
17644
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2023 {
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2024 return dictiter_new(dict, &PyDictIterKey_Type);
17644
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2025 }
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2026
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2027 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2028 dict_itervalues(PyDictObject *dict)
17644
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2029 {
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2030 return dictiter_new(dict, &PyDictIterValue_Type);
17644
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2031 }
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2032
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2033 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2034 dict_iteritems(PyDictObject *dict)
17644
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2035 {
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2036 return dictiter_new(dict, &PyDictIterItem_Type);
17644
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2037 }
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2038
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47294
diff changeset
2039 static PyObject *
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47294
diff changeset
2040 dict_sizeof(PyDictObject *mp)
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47294
diff changeset
2041 {
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47294
diff changeset
2042 Py_ssize_t res;
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47294
diff changeset
2043
47829
7eb3737657d1 Corrected inconsistencies in sizeof tests and addressed issue pointed
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47527
diff changeset
2044 res = sizeof(PyDictObject);
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47294
diff changeset
2045 if (mp->ma_table != mp->ma_smalltable)
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47294
diff changeset
2046 res = res + (mp->ma_mask + 1) * sizeof(PyDictEntry);
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47294
diff changeset
2047 return PyInt_FromSsize_t(res);
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47294
diff changeset
2048 }
17644
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2049
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2050 PyDoc_STRVAR(has_key__doc__,
30265
51744279458f SF patch #798467: Update docstring of has_key for bool changes
Raymond Hettinger <python@rcn.com>
parents: 29003
diff changeset
2051 "D.has_key(k) -> True if D has a key k, else False");
15560
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2052
31172
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
2053 PyDoc_STRVAR(contains__doc__,
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
2054 "D.__contains__(k) -> True if D has a key k, else False");
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
2055
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
2056 PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]");
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
2057
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47294
diff changeset
2058 PyDoc_STRVAR(sizeof__doc__,
47421
a65f095b27f3 Some style nits. Also clarify in the docstrings what __sizeof__ does.
Georg Brandl <georg@python.org>
parents: 47419
diff changeset
2059 "D.__sizeof__() -> size of D in memory, in bytes");
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47294
diff changeset
2060
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2061 PyDoc_STRVAR(get__doc__,
25115
4ea081100305 In doc strings, use 'k in D' rather than D.has_key(k).
Guido van Rossum <guido@python.org>
parents: 24901
diff changeset
2062 "D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.");
15560
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2063
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2064 PyDoc_STRVAR(setdefault_doc__,
25115
4ea081100305 In doc strings, use 'k in D' rather than D.has_key(k).
Guido van Rossum <guido@python.org>
parents: 24901
diff changeset
2065 "D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D");
15560
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2066
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2067 PyDoc_STRVAR(pop__doc__,
49677
82ccc7e77ec5 Merged revisions 66801,66803-66804,66813,66854-66856,66866,66870-66872,66874,66887,66903,66905,66911,66913,66927,66932,66938,66942,66962,66964,66973-66974,66977,66992,66998-66999,67002,67005,67007,67028,67040-67041,67044,67070,67089,67091,67101,67117-67119,67123-67124 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48209
diff changeset
2068 "D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\
28211
92e3b611dc9f SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents: 27902
diff changeset
2069 If key is not found, d is returned if given, otherwise KeyError is raised");
22994
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
2070
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2071 PyDoc_STRVAR(popitem__doc__,
15560
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2072 "D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
49677
82ccc7e77ec5 Merged revisions 66801,66803-66804,66813,66854-66856,66866,66870-66872,66874,66887,66903,66905,66911,66913,66927,66932,66938,66942,66962,66964,66973-66974,66977,66992,66998-66999,67002,67005,67007,67028,67040-67041,67044,67070,67089,67091,67101,67117-67119,67123-67124 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48209
diff changeset
2073 2-tuple; but raise KeyError if D is empty.");
15560
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2074
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2075 PyDoc_STRVAR(keys__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2076 "D.keys() -> list of D's keys");
15560
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2077
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2078 PyDoc_STRVAR(items__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2079 "D.items() -> list of D's (key, value) pairs, as 2-tuples");
15560
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2080
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2081 PyDoc_STRVAR(values__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2082 "D.values() -> list of D's values");
15560
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2083
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2084 PyDoc_STRVAR(update__doc__,
49677
82ccc7e77ec5 Merged revisions 66801,66803-66804,66813,66854-66856,66866,66870-66872,66874,66887,66903,66905,66911,66913,66927,66932,66938,66942,66962,66964,66973-66974,66977,66992,66998-66999,67002,67005,67007,67028,67040-67041,67044,67070,67089,67091,67101,67117-67119,67123-67124 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48209
diff changeset
2085 "D.update(E, **F) -> None. Update D from dict/iterable E and F.\n"
82ccc7e77ec5 Merged revisions 66801,66803-66804,66813,66854-66856,66866,66870-66872,66874,66887,66903,66905,66911,66913,66927,66932,66938,66942,66962,66964,66973-66974,66977,66992,66998-66999,67002,67005,67007,67028,67040-67041,67044,67070,67089,67091,67101,67117-67119,67123-67124 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48209
diff changeset
2086 "If E has a .keys() method, does: for k in E: D[k] = E[k]\n\
82ccc7e77ec5 Merged revisions 66801,66803-66804,66813,66854-66856,66866,66870-66872,66874,66887,66903,66905,66911,66913,66927,66932,66938,66942,66962,66964,66973-66974,66977,66992,66998-66999,67002,67005,67007,67028,67040-67041,67044,67070,67089,67091,67101,67117-67119,67123-67124 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48209
diff changeset
2087 If E lacks .keys() method, does: for (k, v) in E: D[k] = v\n\
82ccc7e77ec5 Merged revisions 66801,66803-66804,66813,66854-66856,66866,66870-66872,66874,66887,66903,66905,66911,66913,66927,66932,66938,66942,66962,66964,66973-66974,66977,66992,66998-66999,67002,67005,67007,67028,67040-67041,67044,67070,67089,67091,67101,67117-67119,67123-67124 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48209
diff changeset
2088 In either case, this is followed by: for k in F: D[k] = F[k]");
15560
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2089
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
2090 PyDoc_STRVAR(fromkeys__doc__,
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
2091 "dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
2092 v defaults to None.");
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
2093
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2094 PyDoc_STRVAR(clear__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2095 "D.clear() -> None. Remove all items from D.");
15560
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2096
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2097 PyDoc_STRVAR(copy__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2098 "D.copy() -> a shallow copy of D");
15560
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2099
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2100 PyDoc_STRVAR(iterkeys__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2101 "D.iterkeys() -> an iterator over the keys of D");
17644
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2102
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2103 PyDoc_STRVAR(itervalues__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2104 "D.itervalues() -> an iterator over the values of D");
17644
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2105
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2106 PyDoc_STRVAR(iteritems__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2107 "D.iteritems() -> an iterator over the (key, value) items of D");
17644
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2108
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
2109 static PyMethodDef mapp_methods[] = {
41833
8c3578934df6 Whitespace cleanup
Neal Norwitz <nnorwitz@gmail.com>
parents: 41832
diff changeset
2110 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST,
31172
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
2111 contains__doc__},
31174
88e80a04328a Simplify previous checkin -- a new function was not needed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
2112 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
31172
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
2113 getitem__doc__},
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47294
diff changeset
2114 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47294
diff changeset
2115 sizeof__doc__},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19150
diff changeset
2116 {"has_key", (PyCFunction)dict_has_key, METH_O,
15560
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2117 has_key__doc__},
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2118 {"get", (PyCFunction)dict_get, METH_VARARGS,
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2119 get__doc__},
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2120 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2121 setdefault_doc__},
28211
92e3b611dc9f SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents: 27902
diff changeset
2122 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
26366
08b720cd387e Patch #642500 with slight modifications: allow keyword arguments in
Just van Rossum <just@letterror.com>
parents: 25115
diff changeset
2123 pop__doc__},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19150
diff changeset
2124 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
15560
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2125 popitem__doc__},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19150
diff changeset
2126 {"keys", (PyCFunction)dict_keys, METH_NOARGS,
15560
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2127 keys__doc__},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19150
diff changeset
2128 {"items", (PyCFunction)dict_items, METH_NOARGS,
15560
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2129 items__doc__},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19150
diff changeset
2130 {"values", (PyCFunction)dict_values, METH_NOARGS,
15560
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2131 values__doc__},
31616
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
2132 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
15560
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2133 update__doc__},
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
2134 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
2135 fromkeys__doc__},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19150
diff changeset
2136 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
15560
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2137 clear__doc__},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19150
diff changeset
2138 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
15560
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2139 copy__doc__},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19150
diff changeset
2140 {"iterkeys", (PyCFunction)dict_iterkeys, METH_NOARGS,
17644
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2141 iterkeys__doc__},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19150
diff changeset
2142 {"itervalues", (PyCFunction)dict_itervalues, METH_NOARGS,
17644
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2143 itervalues__doc__},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19150
diff changeset
2144 {"iteritems", (PyCFunction)dict_iteritems, METH_NOARGS,
17644
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2145 iteritems__doc__},
15560
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2146 {NULL, NULL} /* sentinel */
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2147 };
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2148
38540
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
2149 /* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */
31019
b2fd7af17186 Expose dict_contains() and PyDict_Contains() with is about 10% faster
Raymond Hettinger <python@rcn.com>
parents: 30265
diff changeset
2150 int
b2fd7af17186 Expose dict_contains() and PyDict_Contains() with is about 10% faster
Raymond Hettinger <python@rcn.com>
parents: 30265
diff changeset
2151 PyDict_Contains(PyObject *op, PyObject *key)
17594
bf94af396b1e Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents: 17536
diff changeset
2152 {
bf94af396b1e Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents: 17536
diff changeset
2153 long hash;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2154 PyDictObject *mp = (PyDictObject *)op;
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2155 PyDictEntry *ep;
17594
bf94af396b1e Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents: 17536
diff changeset
2156
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
2157 if (!PyString_CheckExact(key) ||
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
2158 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
17594
bf94af396b1e Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents: 17536
diff changeset
2159 hash = PyObject_Hash(key);
bf94af396b1e Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents: 17536
diff changeset
2160 if (hash == -1)
bf94af396b1e Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents: 17536
diff changeset
2161 return -1;
bf94af396b1e Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents: 17536
diff changeset
2162 }
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
2163 ep = (mp->ma_lookup)(mp, key, hash);
38540
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
2164 return ep == NULL ? -1 : (ep->me_value != NULL);
17594
bf94af396b1e Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents: 17536
diff changeset
2165 }
bf94af396b1e Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents: 17536
diff changeset
2166
40864
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
2167 /* Internal version of PyDict_Contains used when the hash value is already known */
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
2168 int
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
2169 _PyDict_Contains(PyObject *op, PyObject *key, long hash)
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
2170 {
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2171 PyDictObject *mp = (PyDictObject *)op;
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2172 PyDictEntry *ep;
40864
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
2173
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
2174 ep = (mp->ma_lookup)(mp, key, hash);
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
2175 return ep == NULL ? -1 : (ep->me_value != NULL);
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
2176 }
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
2177
17594
bf94af396b1e Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents: 17536
diff changeset
2178 /* Hack to implement "key in dict" */
bf94af396b1e Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents: 17536
diff changeset
2179 static PySequenceMethods dict_as_sequence = {
37304
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2180 0, /* sq_length */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2181 0, /* sq_concat */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2182 0, /* sq_repeat */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2183 0, /* sq_item */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2184 0, /* sq_slice */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2185 0, /* sq_ass_item */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2186 0, /* sq_ass_slice */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2187 PyDict_Contains, /* sq_contains */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2188 0, /* sq_inplace_concat */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2189 0, /* sq_inplace_repeat */
17594
bf94af396b1e Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents: 17536
diff changeset
2190 };
bf94af396b1e Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents: 17536
diff changeset
2191
17644
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2192 static PyObject *
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2193 dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2194 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2195 PyObject *self;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2196
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2197 assert(type != NULL && type->tp_alloc != NULL);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2198 self = type->tp_alloc(type, 0);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2199 if (self != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2200 PyDictObject *d = (PyDictObject *)self;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2201 /* It's guaranteed that tp->alloc zeroed out the struct. */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2202 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2203 INIT_NONZERO_DICT_SLOTS(d);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2204 d->ma_lookup = lookdict_string;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2205 #ifdef SHOW_CONVERSION_COUNTS
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2206 ++created;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2207 #endif
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2208 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2209 return self;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2210 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2211
19658
2fa615dfc464 Make dictionary() a real constructor. Accepts at most one argument, "a
Tim Peters <tim.peters@gmail.com>
parents: 19581
diff changeset
2212 static int
2fa615dfc464 Make dictionary() a real constructor. Accepts at most one argument, "a
Tim Peters <tim.peters@gmail.com>
parents: 19581
diff changeset
2213 dict_init(PyObject *self, PyObject *args, PyObject *kwds)
2fa615dfc464 Make dictionary() a real constructor. Accepts at most one argument, "a
Tim Peters <tim.peters@gmail.com>
parents: 19581
diff changeset
2214 {
31616
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
2215 return dict_update_common(self, args, kwds, "dict");
19658
2fa615dfc464 Make dictionary() a real constructor. Accepts at most one argument, "a
Tim Peters <tim.peters@gmail.com>
parents: 19581
diff changeset
2216 }
2fa615dfc464 Make dictionary() a real constructor. Accepts at most one argument, "a
Tim Peters <tim.peters@gmail.com>
parents: 19581
diff changeset
2217
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2218 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2219 dict_iter(PyDictObject *dict)
17644
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2220 {
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2221 return dictiter_new(dict, &PyDictIterKey_Type);
17644
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2222 }
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2223
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2224 PyDoc_STRVAR(dictionary_doc,
20754
272336648d1f Rename "dictionary" (type and constructor) to "dict".
Tim Peters <tim.peters@gmail.com>
parents: 20724
diff changeset
2225 "dict() -> new empty dictionary.\n"
272336648d1f Rename "dictionary" (type and constructor) to "dict".
Tim Peters <tim.peters@gmail.com>
parents: 20724
diff changeset
2226 "dict(mapping) -> new dictionary initialized from a mapping object's\n"
20683
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
2227 " (key, value) pairs.\n"
20754
272336648d1f Rename "dictionary" (type and constructor) to "dict".
Tim Peters <tim.peters@gmail.com>
parents: 20724
diff changeset
2228 "dict(seq) -> new dictionary initialized as if via:\n"
20724
f224632d0260 dictionary() constructor:
Tim Peters <tim.peters@gmail.com>
parents: 20683
diff changeset
2229 " d = {}\n"
f224632d0260 dictionary() constructor:
Tim Peters <tim.peters@gmail.com>
parents: 20683
diff changeset
2230 " for k, v in seq:\n"
26366
08b720cd387e Patch #642500 with slight modifications: allow keyword arguments in
Just van Rossum <just@letterror.com>
parents: 25115
diff changeset
2231 " d[k] = v\n"
08b720cd387e Patch #642500 with slight modifications: allow keyword arguments in
Just van Rossum <just@letterror.com>
parents: 25115
diff changeset
2232 "dict(**kwargs) -> new dictionary initialized with the name=value pairs\n"
08b720cd387e Patch #642500 with slight modifications: allow keyword arguments in
Just van Rossum <just@letterror.com>
parents: 25115
diff changeset
2233 " in the keyword argument list. For example: dict(one=1, two=2)");
19658
2fa615dfc464 Make dictionary() a real constructor. Accepts at most one argument, "a
Tim Peters <tim.peters@gmail.com>
parents: 19581
diff changeset
2234
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
2235 PyTypeObject PyDict_Type = {
42333
6620a510e9a9 PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents: 41833
diff changeset
2236 PyVarObject_HEAD_INIT(&PyType_Type, 0)
20754
272336648d1f Rename "dictionary" (type and constructor) to "dict".
Tim Peters <tim.peters@gmail.com>
parents: 20724
diff changeset
2237 "dict",
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2238 sizeof(PyDictObject),
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2239 0,
15974
5b9103e62fac Rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15708
diff changeset
2240 (destructor)dict_dealloc, /* tp_dealloc */
5b9103e62fac Rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15708
diff changeset
2241 (printfunc)dict_print, /* tp_print */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2242 0, /* tp_getattr */
15974
5b9103e62fac Rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15708
diff changeset
2243 0, /* tp_setattr */
17775
dce1f53451a5 Restore dicts' tp_compare slot, and change dict_richcompare to say it
Tim Peters <tim.peters@gmail.com>
parents: 17771
diff changeset
2244 (cmpfunc)dict_compare, /* tp_compare */
15974
5b9103e62fac Rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15708
diff changeset
2245 (reprfunc)dict_repr, /* tp_repr */
5b9103e62fac Rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15708
diff changeset
2246 0, /* tp_as_number */
17594
bf94af396b1e Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents: 17536
diff changeset
2247 &dict_as_sequence, /* tp_as_sequence */
15974
5b9103e62fac Rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15708
diff changeset
2248 &dict_as_mapping, /* tp_as_mapping */
47961
f9b80a820a25 Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents: 47847
diff changeset
2249 (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
15974
5b9103e62fac Rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15708
diff changeset
2250 0, /* tp_call */
5b9103e62fac Rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15708
diff changeset
2251 0, /* tp_str */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2252 PyObject_GenericGetAttr, /* tp_getattro */
15974
5b9103e62fac Rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15708
diff changeset
2253 0, /* tp_setattro */
5b9103e62fac Rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15708
diff changeset
2254 0, /* tp_as_buffer */
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
2255 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
40908
d01bd37bf5ce Variation of patch # 1624059 to speed up checking if an object is a subclass
Neal Norwitz <nnorwitz@gmail.com>
parents: 40864
diff changeset
2256 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
19658
2fa615dfc464 Make dictionary() a real constructor. Accepts at most one argument, "a
Tim Peters <tim.peters@gmail.com>
parents: 19581
diff changeset
2257 dictionary_doc, /* tp_doc */
37304
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2258 dict_traverse, /* tp_traverse */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2259 dict_tp_clear, /* tp_clear */
17704
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
2260 dict_richcompare, /* tp_richcompare */
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2261 0, /* tp_weaklistoffset */
17644
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2262 (getiterfunc)dict_iter, /* tp_iter */
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
2263 0, /* tp_iternext */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2264 mapp_methods, /* tp_methods */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2265 0, /* tp_members */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2266 0, /* tp_getset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2267 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2268 0, /* tp_dict */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2269 0, /* tp_descr_get */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2270 0, /* tp_descr_set */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2271 0, /* tp_dictoffset */
37304
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2272 dict_init, /* tp_init */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2273 PyType_GenericAlloc, /* tp_alloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2274 dict_new, /* tp_new */
22973
699cc139d2d2 PyObject_GC_Del and PyObject_Del can now be used as a function
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22785
diff changeset
2275 PyObject_GC_Del, /* tp_free */
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2276 };
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2277
5540
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
2278 /* For backward compatibility with old dictionary interface */
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
2279
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
2280 PyObject *
26599
ecea8410178e Constify char* API. Fixes #651363. 2.2 candidate.
Martin v. Löwis <martin@v.loewis.de>
parents: 26583
diff changeset
2281 PyDict_GetItemString(PyObject *v, const char *key)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2282 {
5540
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
2283 PyObject *kv, *rv;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
2284 kv = PyString_FromString(key);
5540
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
2285 if (kv == NULL)
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
2286 return NULL;
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
2287 rv = PyDict_GetItem(v, kv);
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
2288 Py_DECREF(kv);
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
2289 return rv;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2290 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2291
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2292 int
26599
ecea8410178e Constify char* API. Fixes #651363. 2.2 candidate.
Martin v. Löwis <martin@v.loewis.de>
parents: 26583
diff changeset
2293 PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2294 {
5540
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
2295 PyObject *kv;
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
2296 int err;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
2297 kv = PyString_FromString(key);
5540
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
2298 if (kv == NULL)
5561
4dcf26fe4da5 Moved PyObject_{Get,Set}Attr to object.c.
Guido van Rossum <guido@python.org>
parents: 5540
diff changeset
2299 return -1;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
2300 PyString_InternInPlace(&kv); /* XXX Should we really? */
5540
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
2301 err = PyDict_SetItem(v, kv, item);
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
2302 Py_DECREF(kv);
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
2303 return err;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2304 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2305
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2306 int
26599
ecea8410178e Constify char* API. Fixes #651363. 2.2 candidate.
Martin v. Löwis <martin@v.loewis.de>
parents: 26583
diff changeset
2307 PyDict_DelItemString(PyObject *v, const char *key)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2308 {
5540
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
2309 PyObject *kv;
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
2310 int err;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
2311 kv = PyString_FromString(key);
5540
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
2312 if (kv == NULL)
5561
4dcf26fe4da5 Moved PyObject_{Get,Set}Attr to object.c.
Guido van Rossum <guido@python.org>
parents: 5540
diff changeset
2313 return -1;
5540
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
2314 err = PyDict_DelItem(v, kv);
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
2315 Py_DECREF(kv);
a0cc005eccd7 Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents: 5505
diff changeset
2316 return err;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2317 }
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2318
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2319 /* Dictionary iterator types */
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2320
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2321 typedef struct {
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2322 PyObject_HEAD
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2323 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
2324 Py_ssize_t di_used;
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
2325 Py_ssize_t di_pos;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2326 PyObject* di_result; /* reusable result tuple for iteritems */
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
2327 Py_ssize_t len;
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2328 } dictiterobject;
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2329
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2330 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2331 dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2332 {
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2333 dictiterobject *di;
50424
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49677
diff changeset
2334 di = PyObject_GC_New(dictiterobject, itertype);
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2335 if (di == NULL)
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2336 return NULL;
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2337 Py_INCREF(dict);
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2338 di->di_dict = dict;
17656
a6698a016afe Mchael Hudson pointed out that the code for detecting changes in
Guido van Rossum <guido@python.org>
parents: 17644
diff changeset
2339 di->di_used = dict->ma_used;
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2340 di->di_pos = 0;
31683
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2341 di->len = dict->ma_used;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2342 if (itertype == &PyDictIterItem_Type) {
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2343 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2344 if (di->di_result == NULL) {
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2345 Py_DECREF(di);
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2346 return NULL;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2347 }
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2348 }
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2349 else
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2350 di->di_result = NULL;
50424
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49677
diff changeset
2351 _PyObject_GC_TRACK(di);
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2352 return (PyObject *)di;
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2353 }
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2354
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2355 static void
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2356 dictiter_dealloc(dictiterobject *di)
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2357 {
24259
2f60f90ce309 Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
2358 Py_XDECREF(di->di_dict);
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2359 Py_XDECREF(di->di_result);
50424
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49677
diff changeset
2360 PyObject_GC_Del(di);
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49677
diff changeset
2361 }
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49677
diff changeset
2362
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49677
diff changeset
2363 static int
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49677
diff changeset
2364 dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49677
diff changeset
2365 {
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49677
diff changeset
2366 Py_VISIT(di->di_dict);
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49677
diff changeset
2367 Py_VISIT(di->di_result);
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49677
diff changeset
2368 return 0;
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2369 }
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2370
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2371 static PyObject *
31683
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2372 dictiter_len(dictiterobject *di)
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2373 {
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
2374 Py_ssize_t len = 0;
31908
058821d8aefd * Add unittests for iterators that report their length
Raymond Hettinger <python@rcn.com>
parents: 31702
diff changeset
2375 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2376 len = di->len;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
2377 return PyInt_FromSize_t(len);
31683
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2378 }
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2379
36484
20f75d5e79e9 Renamed _length_cue() to __length_hint__(). See:
Armin Rigo <arigo@tunes.org>
parents: 36164
diff changeset
2380 PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2381
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2382 static PyMethodDef dictiter_methods[] = {
36484
20f75d5e79e9 Renamed _length_cue() to __length_hint__(). See:
Armin Rigo <arigo@tunes.org>
parents: 36164
diff changeset
2383 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS, length_hint_doc},
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2384 {NULL, NULL} /* sentinel */
31683
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2385 };
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2386
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2387 static PyObject *dictiter_iternextkey(dictiterobject *di)
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2388 {
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2389 PyObject *key;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
2390 register Py_ssize_t i, mask;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2391 register PyDictEntry *ep;
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2392 PyDictObject *d = di->di_dict;
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
2393
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2394 if (d == NULL)
24259
2f60f90ce309 Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
2395 return NULL;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2396 assert (PyDict_Check(d));
24259
2f60f90ce309 Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
2397
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2398 if (di->di_used != d->ma_used) {
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
2399 PyErr_SetString(PyExc_RuntimeError,
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
2400 "dictionary changed size during iteration");
24259
2f60f90ce309 Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
2401 di->di_used = -1; /* Make this state sticky */
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
2402 return NULL;
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
2403 }
24259
2f60f90ce309 Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
2404
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2405 i = di->di_pos;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2406 if (i < 0)
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2407 goto fail;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2408 ep = d->ma_table;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2409 mask = d->ma_mask;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2410 while (i <= mask && ep[i].me_value == NULL)
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2411 i++;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2412 di->di_pos = i+1;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2413 if (i > mask)
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2414 goto fail;
31683
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2415 di->len--;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2416 key = ep[i].me_key;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2417 Py_INCREF(key);
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2418 return key;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2419
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2420 fail:
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2421 Py_DECREF(d);
24259
2f60f90ce309 Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
2422 di->di_dict = NULL;
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
2423 return NULL;
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2424 }
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2425
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2426 PyTypeObject PyDictIterKey_Type = {
42333
6620a510e9a9 PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents: 41833
diff changeset
2427 PyVarObject_HEAD_INIT(&PyType_Type, 0)
31683
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2428 "dictionary-keyiterator", /* tp_name */
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2429 sizeof(dictiterobject), /* tp_basicsize */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2430 0, /* tp_itemsize */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2431 /* methods */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2432 (destructor)dictiter_dealloc, /* tp_dealloc */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2433 0, /* tp_print */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2434 0, /* tp_getattr */
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2435 0, /* tp_setattr */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2436 0, /* tp_compare */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2437 0, /* tp_repr */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2438 0, /* tp_as_number */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2439 0, /* tp_as_sequence */
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2440 0, /* tp_as_mapping */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2441 0, /* tp_hash */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2442 0, /* tp_call */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2443 0, /* tp_str */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2444 PyObject_GenericGetAttr, /* tp_getattro */
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2445 0, /* tp_setattro */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2446 0, /* tp_as_buffer */
50424
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49677
diff changeset
2447 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2448 0, /* tp_doc */
50424
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49677
diff changeset
2449 (traverseproc)dictiter_traverse, /* tp_traverse */
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2450 0, /* tp_clear */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2451 0, /* tp_richcompare */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2452 0, /* tp_weaklistoffset */
28320
fd5ef7003469 Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents: 28308
diff changeset
2453 PyObject_SelfIter, /* tp_iter */
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2454 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2455 dictiter_methods, /* tp_methods */
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2456 0,
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2457 };
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2458
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2459 static PyObject *dictiter_iternextvalue(dictiterobject *di)
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2460 {
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2461 PyObject *value;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
2462 register Py_ssize_t i, mask;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2463 register PyDictEntry *ep;
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2464 PyDictObject *d = di->di_dict;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2465
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2466 if (d == NULL)
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2467 return NULL;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2468 assert (PyDict_Check(d));
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2469
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2470 if (di->di_used != d->ma_used) {
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2471 PyErr_SetString(PyExc_RuntimeError,
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2472 "dictionary changed size during iteration");
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2473 di->di_used = -1; /* Make this state sticky */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2474 return NULL;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2475 }
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2476
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2477 i = di->di_pos;
31702
36a2d0e8a993 GCC was complaining that 'value' in dictiter_iternextvalue() wasn't
Guido van Rossum <guido@python.org>
parents: 31689
diff changeset
2478 mask = d->ma_mask;
36a2d0e8a993 GCC was complaining that 'value' in dictiter_iternextvalue() wasn't
Guido van Rossum <guido@python.org>
parents: 31689
diff changeset
2479 if (i < 0 || i > mask)
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2480 goto fail;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2481 ep = d->ma_table;
31702
36a2d0e8a993 GCC was complaining that 'value' in dictiter_iternextvalue() wasn't
Guido van Rossum <guido@python.org>
parents: 31689
diff changeset
2482 while ((value=ep[i].me_value) == NULL) {
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2483 i++;
31702
36a2d0e8a993 GCC was complaining that 'value' in dictiter_iternextvalue() wasn't
Guido van Rossum <guido@python.org>
parents: 31689
diff changeset
2484 if (i > mask)
36a2d0e8a993 GCC was complaining that 'value' in dictiter_iternextvalue() wasn't
Guido van Rossum <guido@python.org>
parents: 31689
diff changeset
2485 goto fail;
36a2d0e8a993 GCC was complaining that 'value' in dictiter_iternextvalue() wasn't
Guido van Rossum <guido@python.org>
parents: 31689
diff changeset
2486 }
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2487 di->di_pos = i+1;
31683
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2488 di->len--;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2489 Py_INCREF(value);
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2490 return value;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2491
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2492 fail:
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2493 Py_DECREF(d);
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2494 di->di_dict = NULL;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2495 return NULL;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2496 }
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2497
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2498 PyTypeObject PyDictIterValue_Type = {
42333
6620a510e9a9 PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents: 41833
diff changeset
2499 PyVarObject_HEAD_INIT(&PyType_Type, 0)
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2500 "dictionary-valueiterator", /* tp_name */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2501 sizeof(dictiterobject), /* tp_basicsize */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2502 0, /* tp_itemsize */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2503 /* methods */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2504 (destructor)dictiter_dealloc, /* tp_dealloc */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2505 0, /* tp_print */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2506 0, /* tp_getattr */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2507 0, /* tp_setattr */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2508 0, /* tp_compare */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2509 0, /* tp_repr */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2510 0, /* tp_as_number */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2511 0, /* tp_as_sequence */
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2512 0, /* tp_as_mapping */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2513 0, /* tp_hash */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2514 0, /* tp_call */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2515 0, /* tp_str */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2516 PyObject_GenericGetAttr, /* tp_getattro */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2517 0, /* tp_setattro */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2518 0, /* tp_as_buffer */
50424
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49677
diff changeset
2519 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2520 0, /* tp_doc */
50424
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49677
diff changeset
2521 (traverseproc)dictiter_traverse, /* tp_traverse */
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2522 0, /* tp_clear */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2523 0, /* tp_richcompare */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2524 0, /* tp_weaklistoffset */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2525 PyObject_SelfIter, /* tp_iter */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2526 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2527 dictiter_methods, /* tp_methods */
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2528 0,
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2529 };
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2530
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2531 static PyObject *dictiter_iternextitem(dictiterobject *di)
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2532 {
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2533 PyObject *key, *value, *result = di->di_result;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
2534 register Py_ssize_t i, mask;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2535 register PyDictEntry *ep;
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2536 PyDictObject *d = di->di_dict;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2537
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2538 if (d == NULL)
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2539 return NULL;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2540 assert (PyDict_Check(d));
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2541
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2542 if (di->di_used != d->ma_used) {
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2543 PyErr_SetString(PyExc_RuntimeError,
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2544 "dictionary changed size during iteration");
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2545 di->di_used = -1; /* Make this state sticky */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2546 return NULL;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2547 }
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2548
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2549 i = di->di_pos;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2550 if (i < 0)
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2551 goto fail;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2552 ep = d->ma_table;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2553 mask = d->ma_mask;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2554 while (i <= mask && ep[i].me_value == NULL)
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2555 i++;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2556 di->di_pos = i+1;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2557 if (i > mask)
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2558 goto fail;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2559
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2560 if (result->ob_refcnt == 1) {
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2561 Py_INCREF(result);
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2562 Py_DECREF(PyTuple_GET_ITEM(result, 0));
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2563 Py_DECREF(PyTuple_GET_ITEM(result, 1));
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2564 } else {
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2565 result = PyTuple_New(2);
36164
50886d42b444 Fixed English in a comment; trimmed trailing whitespace;
Tim Peters <tim.peters@gmail.com>
parents: 35636
diff changeset
2566 if (result == NULL)
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2567 return NULL;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2568 }
31683
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2569 di->len--;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2570 key = ep[i].me_key;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2571 value = ep[i].me_value;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2572 Py_INCREF(key);
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2573 Py_INCREF(value);
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2574 PyTuple_SET_ITEM(result, 0, key);
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2575 PyTuple_SET_ITEM(result, 1, value);
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2576 return result;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2577
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2578 fail:
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2579 Py_DECREF(d);
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2580 di->di_dict = NULL;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2581 return NULL;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2582 }
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2583
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2584 PyTypeObject PyDictIterItem_Type = {
42333
6620a510e9a9 PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents: 41833
diff changeset
2585 PyVarObject_HEAD_INIT(&PyType_Type, 0)
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2586 "dictionary-itemiterator", /* tp_name */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2587 sizeof(dictiterobject), /* tp_basicsize */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2588 0, /* tp_itemsize */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2589 /* methods */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2590 (destructor)dictiter_dealloc, /* tp_dealloc */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2591 0, /* tp_print */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2592 0, /* tp_getattr */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2593 0, /* tp_setattr */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2594 0, /* tp_compare */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2595 0, /* tp_repr */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2596 0, /* tp_as_number */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2597 0, /* tp_as_sequence */
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2598 0, /* tp_as_mapping */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2599 0, /* tp_hash */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2600 0, /* tp_call */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2601 0, /* tp_str */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2602 PyObject_GenericGetAttr, /* tp_getattro */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2603 0, /* tp_setattro */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2604 0, /* tp_as_buffer */
50424
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49677
diff changeset
2605 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2606 0, /* tp_doc */
50424
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49677
diff changeset
2607 (traverseproc)dictiter_traverse, /* tp_traverse */
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2608 0, /* tp_clear */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2609 0, /* tp_richcompare */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2610 0, /* tp_weaklistoffset */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2611 PyObject_SelfIter, /* tp_iter */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2612 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2613 dictiter_methods, /* tp_methods */
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2614 0,
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2615 };