annotate Objects/dictobject.c @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents b3791309e690
children ba6376dff6c4
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
52288
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
183 /* Debug statistic to count GC tracking of dicts */
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
184 #ifdef SHOW_TRACK_COUNT
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
185 static Py_ssize_t count_untracked = 0;
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
186 static Py_ssize_t count_tracked = 0;
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
187
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
188 static void
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
189 show_track(void)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
190 {
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
191 fprintf(stderr, "Dicts created: %" PY_FORMAT_SIZE_T "d\n",
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
192 count_tracked + count_untracked);
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
193 fprintf(stderr, "Dicts tracked by the GC: %" PY_FORMAT_SIZE_T
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
194 "d\n", count_tracked);
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
195 fprintf(stderr, "%.2f%% dict tracking rate\n\n",
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
196 (100.0*count_tracked/(count_untracked+count_tracked)));
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
197 }
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
198 #endif
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
199
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
200
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
201 /* Initialization macros.
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
202 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
203 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
204 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
205 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
206 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
207 an excellent reason not to).
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
208 */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
209
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
210 #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
211 (mp)->ma_table = (mp)->ma_smalltable; \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
212 (mp)->ma_mask = PyDict_MINSIZE - 1; \
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
213 } while(0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
214
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
215 #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
216 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
217 (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
218 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
219 } while(0)
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
220
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
221 /* 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
222 #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
223 #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
224 #endif
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44949
diff changeset
225 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
226 static int numfree = 0;
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
227
45229
262263737bce Deallocate content of the dict free list on interpreter shutdown
Christian Heimes <christian@cheimes.de>
parents: 45217
diff changeset
228 void
262263737bce Deallocate content of the dict free list on interpreter shutdown
Christian Heimes <christian@cheimes.de>
parents: 45217
diff changeset
229 PyDict_Fini(void)
262263737bce Deallocate content of the dict free list on interpreter shutdown
Christian Heimes <christian@cheimes.de>
parents: 45217
diff changeset
230 {
45230
1c326dea494c Use prefix decrement
Christian Heimes <christian@cheimes.de>
parents: 45229
diff changeset
231 PyDictObject *op;
45229
262263737bce Deallocate content of the dict free list on interpreter shutdown
Christian Heimes <christian@cheimes.de>
parents: 45217
diff changeset
232
262263737bce Deallocate content of the dict free list on interpreter shutdown
Christian Heimes <christian@cheimes.de>
parents: 45217
diff changeset
233 while (numfree) {
45230
1c326dea494c Use prefix decrement
Christian Heimes <christian@cheimes.de>
parents: 45229
diff changeset
234 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
235 assert(PyDict_CheckExact(op));
262263737bce Deallocate content of the dict free list on interpreter shutdown
Christian Heimes <christian@cheimes.de>
parents: 45217
diff changeset
236 PyObject_GC_Del(op);
262263737bce Deallocate content of the dict free list on interpreter shutdown
Christian Heimes <christian@cheimes.de>
parents: 45217
diff changeset
237 }
262263737bce Deallocate content of the dict free list on interpreter shutdown
Christian Heimes <christian@cheimes.de>
parents: 45217
diff changeset
238 }
262263737bce Deallocate content of the dict free list on interpreter shutdown
Christian Heimes <christian@cheimes.de>
parents: 45217
diff changeset
239
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
240 PyObject *
13660
a1ce07218781 Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
241 PyDict_New(void)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 {
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
243 register PyDictObject *mp;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 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
245 dummy = PyString_FromString("<dummy key>");
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 if (dummy == NULL)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 return NULL;
14310
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
248 #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
249 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
250 #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
251 #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
252 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
253 #endif
52288
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
254 #ifdef SHOW_TRACK_COUNT
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
255 Py_AtExit(show_track);
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
256 #endif
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 }
45191
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44949
diff changeset
258 if (numfree) {
920a7633ae08 Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents: 44949
diff changeset
259 mp = free_list[--numfree];
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
260 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
261 assert (Py_TYPE(mp) == &PyDict_Type);
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
262 _Py_NewReference((PyObject *)mp);
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
263 if (mp->ma_fill) {
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
264 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
265 } else {
694814d7b104 - Issue #3537: Fix an assertion failure when an empty but presized dict
Georg Brandl <georg@python.org>
parents: 47961
diff changeset
266 /* 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
267 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
268 INIT_NONZERO_DICT_SLOTS(mp);
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
269 }
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
270 assert (mp->ma_used == 0);
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
271 assert (mp->ma_table == mp->ma_smalltable);
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
272 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
273 #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
274 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
275 #endif
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
276 } else {
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
277 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
278 if (mp == NULL)
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
279 return NULL;
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
280 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
281 #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
282 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
283 #endif
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
284 }
14310
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
285 mp->ma_lookup = lookdict_string;
52288
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
286 #ifdef SHOW_TRACK_COUNT
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
287 count_untracked++;
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
288 #endif
14310
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
289 #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
290 ++created;
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
291 #endif
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
292 return (PyObject *)mp;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
294
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 /*
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
296 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
297 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
298 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
299 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
300
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
301 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
302 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
303
10461
4abc5abaec9a Vladimir Marangozov contributed updated comments.
Guido van Rossum <guido@python.org>
parents: 9487
diff changeset
304 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
305
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
306 (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
307 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
308 Christian Tismer).
14310
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
309
38540
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
310 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
311 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
312 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
313 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
314 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
315 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
316 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
317 PyDictEntry*.
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 */
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
319 static PyDictEntry *
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
320 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
321 {
38540
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
322 register size_t i;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
323 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
324 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
325 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
326 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
327 register PyDictEntry *ep;
14307
b24db770dffd Clear errors raised by PyObject_Compare() without losing any existing
Fred Drake <fdrake@acm.org>
parents: 13892
diff changeset
328 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
329 PyObject *startkey;
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
330
38540
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
331 i = (size_t)hash & mask;
5059
192d571cc461 Small lookmapping nits:
Guido van Rossum <guido@python.org>
parents: 5056
diff changeset
332 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
333 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
334 return ep;
18022
bd1c4fe977cb lookdict: Reduce obfuscating code duplication with a judicious goto.
Tim Peters <tim.peters@gmail.com>
parents: 18019
diff changeset
335
5053
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
336 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
337 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
338 else {
14307
b24db770dffd Clear errors raised by PyObject_Compare() without losing any existing
Fred Drake <fdrake@acm.org>
parents: 13892
diff changeset
339 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
340 startkey = ep->me_key;
44056
b6b5b6cdd202 Spaces vs. Tabs.
Georg Brandl <georg@python.org>
parents: 44055
diff changeset
341 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
342 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
44056
b6b5b6cdd202 Spaces vs. Tabs.
Georg Brandl <georg@python.org>
parents: 44055
diff changeset
343 Py_DECREF(startkey);
18022
bd1c4fe977cb lookdict: Reduce obfuscating code duplication with a judicious goto.
Tim Peters <tim.peters@gmail.com>
parents: 18019
diff changeset
344 if (cmp < 0)
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
345 return NULL;
18023
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
346 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
347 if (cmp > 0)
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
348 return ep;
18023
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
349 }
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
350 else {
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
351 /* 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
352 * dict: start over.
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
353 * 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
354 * XXX from terminating.
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
355 */
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
356 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
357 }
6105
77fb880eca49 Made lookdict nearly twice as fast, resulting in a 5% overall
Guido van Rossum <guido@python.org>
parents: 5784
diff changeset
358 }
77fb880eca49 Made lookdict nearly twice as fast, resulting in a 5% overall
Guido van Rossum <guido@python.org>
parents: 5784
diff changeset
359 freeslot = NULL;
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4976
diff changeset
360 }
17973
2f2ee5daa60d Implement an old idea of Christian Tismer's: use polynomial division
Tim Peters <tim.peters@gmail.com>
parents: 17964
diff changeset
361
17816
8f5ce58119e0 Aggressive reordering of dict comparisons. In case of collision, it stands
Tim Peters <tim.peters@gmail.com>
parents: 17815
diff changeset
362 /* 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
363 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
364 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
365 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
366 ep = &ep0[i & mask];
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
367 if (ep->me_key == NULL)
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
368 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
369 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
370 return ep;
18022
bd1c4fe977cb lookdict: Reduce obfuscating code duplication with a judicious goto.
Tim Peters <tim.peters@gmail.com>
parents: 18019
diff changeset
371 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
372 startkey = ep->me_key;
44056
b6b5b6cdd202 Spaces vs. Tabs.
Georg Brandl <georg@python.org>
parents: 44055
diff changeset
373 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
374 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
44056
b6b5b6cdd202 Spaces vs. Tabs.
Georg Brandl <georg@python.org>
parents: 44055
diff changeset
375 Py_DECREF(startkey);
18022
bd1c4fe977cb lookdict: Reduce obfuscating code duplication with a judicious goto.
Tim Peters <tim.peters@gmail.com>
parents: 18019
diff changeset
376 if (cmp < 0)
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
377 return NULL;
18023
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
378 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
379 if (cmp > 0)
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
380 return ep;
18023
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
381 }
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
382 else {
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
383 /* 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
384 * dict: start over.
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
385 * 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
386 * XXX from terminating.
8d2bbbbf2cb9 lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents: 18022
diff changeset
387 */
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
388 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
389 }
5053
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
390 }
17816
8f5ce58119e0 Aggressive reordering of dict comparisons. In case of collision, it stands
Tim Peters <tim.peters@gmail.com>
parents: 17815
diff changeset
391 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
392 freeslot = ep;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
393 }
40225
796304efa268 Fix warnings with HP's C compiler. It doesn't recognize that infinite
Neal Norwitz <nnorwitz@gmail.com>
parents: 39745
diff changeset
394 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
395 return 0;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
396 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
397
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
398 /*
14310
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
399 * 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
400 * 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
401 * 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
402 * 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
403 * 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
404 *
38540
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
405 * 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
406 */
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
407 static PyDictEntry *
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
408 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
409 {
38540
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
410 register size_t i;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
411 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
412 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
413 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
414 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
415 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
416
19904
58d50094bf80 Changed the dict implementation to take "string shortcuts" only when
Tim Peters <tim.peters@gmail.com>
parents: 19662
diff changeset
417 /* 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
418 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
419 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
420 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
421 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
422 #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
423 ++converted;
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
424 #endif
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
425 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
426 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
427 }
17815
d6df8d371b67 Get rid of the superstitious "~" in dict hashing's "i = (~hash) & mask".
Tim Peters <tim.peters@gmail.com>
parents: 17775
diff changeset
428 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
429 ep = &ep0[i];
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
430 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
431 return ep;
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
432 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
433 freeslot = ep;
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
434 else {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
435 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
436 return ep;
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
437 freeslot = NULL;
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
438 }
17973
2f2ee5daa60d Implement an old idea of Christian Tismer's: use polynomial division
Tim Peters <tim.peters@gmail.com>
parents: 17964
diff changeset
439
17816
8f5ce58119e0 Aggressive reordering of dict comparisons. In case of collision, it stands
Tim Peters <tim.peters@gmail.com>
parents: 17815
diff changeset
440 /* 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
441 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
442 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
443 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
444 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
445 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
446 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
447 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
448 || (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
449 && 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
450 && _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
451 return ep;
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
452 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
453 freeslot = ep;
14310
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
454 }
40225
796304efa268 Fix warnings with HP's C compiler. It doesn't recognize that infinite
Neal Norwitz <nnorwitz@gmail.com>
parents: 39745
diff changeset
455 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
456 return 0;
14310
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
457 }
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
458
52288
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
459 #ifdef SHOW_TRACK_COUNT
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
460 #define INCREASE_TRACK_COUNT \
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
461 (count_tracked++, count_untracked--);
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
462 #define DECREASE_TRACK_COUNT \
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
463 (count_tracked--, count_untracked++);
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
464 #else
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
465 #define INCREASE_TRACK_COUNT
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
466 #define DECREASE_TRACK_COUNT
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
467 #endif
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
468
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
469 #define MAINTAIN_TRACKING(mp, key, value) \
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
470 do { \
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
471 if (!_PyObject_GC_IS_TRACKED(mp)) { \
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
472 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
473 _PyObject_GC_MAY_BE_TRACKED(value)) { \
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
474 _PyObject_GC_TRACK(mp); \
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
475 INCREASE_TRACK_COUNT \
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
476 } \
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
477 } \
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
478 } while(0)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
479
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
480 void
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
481 _PyDict_MaybeUntrack(PyObject *op)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
482 {
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
483 PyDictObject *mp;
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
484 PyObject *value;
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
485 Py_ssize_t mask, i;
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
486 PyDictEntry *ep;
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
487
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
488 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
489 return;
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
490
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
491 mp = (PyDictObject *) op;
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
492 ep = mp->ma_table;
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
493 mask = mp->ma_mask;
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
494 for (i = 0; i <= mask; i++) {
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
495 if ((value = ep[i].me_value) == NULL)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
496 continue;
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
497 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
498 _PyObject_GC_MAY_BE_TRACKED(ep[i].me_key))
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
499 return;
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
500 }
52292
b3791309e690 The tracking statistics were actually too pessimistic
Antoine Pitrou <solipsis@pitrou.net>
parents: 52288
diff changeset
501 DECREASE_TRACK_COUNT
52288
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
502 _PyObject_GC_UNTRACK(op);
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
503 }
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
504
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
505
14310
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
506 /*
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
507 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
508 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
509 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
510 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
511 */
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
512 static int
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
513 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
514 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
515 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
516 register PyDictEntry *ep;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
517 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
518
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
519 assert(mp->ma_lookup != NULL);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
520 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
521 if (ep == NULL) {
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
522 Py_DECREF(key);
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
523 Py_DECREF(value);
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
524 return -1;
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
525 }
52288
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
526 MAINTAIN_TRACKING(mp, key, value);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
527 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
528 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
529 ep->me_value = value;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
530 Py_DECREF(old_value); /* which **CAN** re-enter */
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
531 Py_DECREF(key);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
532 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
533 else {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
534 if (ep->me_key == NULL)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
535 mp->ma_fill++;
34544
f24117a3628a Code simplification -- eliminate lookup when value is known in advance.
Raymond Hettinger <python@rcn.com>
parents: 32695
diff changeset
536 else {
f24117a3628a Code simplification -- eliminate lookup when value is known in advance.
Raymond Hettinger <python@rcn.com>
parents: 32695
diff changeset
537 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
538 Py_DECREF(dummy);
f24117a3628a Code simplification -- eliminate lookup when value is known in advance.
Raymond Hettinger <python@rcn.com>
parents: 32695
diff changeset
539 }
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
540 ep->me_key = key;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
541 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
542 ep->me_value = value;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
543 mp->ma_used++;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
544 }
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
545 return 0;
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
546 }
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
547
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
548 /*
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
549 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
550 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
551 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
552 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
553 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
554 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
555 */
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
556 static void
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
557 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
558 PyObject *value)
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
559 {
38540
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
560 register size_t i;
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
561 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
562 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
563 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
564 register PyDictEntry *ep;
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
565
52288
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
566 MAINTAIN_TRACKING(mp, key, value);
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
567 i = hash & mask;
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
568 ep = &ep0[i];
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
569 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
570 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
571 ep = &ep0[i & mask];
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
572 }
38540
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
573 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
574 mp->ma_fill++;
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
575 ep->me_key = key;
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
576 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
577 ep->me_value = value;
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
578 mp->ma_used++;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
579 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
580
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
581 /*
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
582 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
583 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
584 actually be smaller than the old one.
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
585 */
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
586 static int
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
587 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
588 {
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
589 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
590 PyDictEntry *oldtable, *newtable, *ep;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
591 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
592 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
593 PyDictEntry small_copy[PyDict_MINSIZE];
17861
335fbdee7cf8 Bugfix candidate.
Tim Peters <tim.peters@gmail.com>
parents: 17852
diff changeset
594
335fbdee7cf8 Bugfix candidate.
Tim Peters <tim.peters@gmail.com>
parents: 17852
diff changeset
595 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
596
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
597 /* 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
598 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
599 newsize <= minused && newsize > 0;
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
600 newsize <<= 1)
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
601 ;
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
602 if (newsize <= 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
603 PyErr_NoMemory();
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
604 return -1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
605 }
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
606
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
607 /* 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
608 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
609 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
610 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
611
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
612 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
613 /* 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
614 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
615 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
616 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
617 /* 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
618 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
619 }
95baa333ee43 dictresize(): Rebuild small tables if there are any dummies, not just if
Tim Peters <tim.peters@gmail.com>
parents: 17962
diff changeset
620 /* 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
621 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
622 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
623 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
624 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
625 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
626 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
627 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
628 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
629 }
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
630 }
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
631 else {
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
632 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
633 if (newtable == NULL) {
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
634 PyErr_NoMemory();
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
635 return -1;
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
636 }
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
637 }
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
638
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
639 /* 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
640 assert(newtable != oldtable);
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
641 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
642 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
643 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
644 mp->ma_used = 0;
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
645 i = mp->ma_fill;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
646 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
647
17852
ba97111a6ad9 Speed dictresize by collapsing its two passes into one; the reason given
Tim Peters <tim.peters@gmail.com>
parents: 17816
diff changeset
648 /* 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
649 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
650 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
651 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
652 --i;
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
653 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
654 ep->me_value);
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
655 }
17852
ba97111a6ad9 Speed dictresize by collapsing its two passes into one; the reason given
Tim Peters <tim.peters@gmail.com>
parents: 17816
diff changeset
656 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
657 --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
658 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
659 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
660 }
17852
ba97111a6ad9 Speed dictresize by collapsing its two passes into one; the reason given
Tim Peters <tim.peters@gmail.com>
parents: 17816
diff changeset
661 /* 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
662 }
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
663
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
664 if (is_oldtable_malloced)
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12126
diff changeset
665 PyMem_DEL(oldtable);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
666 return 0;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
667 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
668
44339
7ac117b3be37 Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary.
Raymond Hettinger <python@rcn.com>
parents: 44056
diff changeset
669 /* 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
670 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
671 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
672 */
7ac117b3be37 Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary.
Raymond Hettinger <python@rcn.com>
parents: 44056
diff changeset
673
7ac117b3be37 Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary.
Raymond Hettinger <python@rcn.com>
parents: 44056
diff changeset
674 PyObject *
7ac117b3be37 Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary.
Raymond Hettinger <python@rcn.com>
parents: 44056
diff changeset
675 _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
676 {
7ac117b3be37 Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary.
Raymond Hettinger <python@rcn.com>
parents: 44056
diff changeset
677 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
678
7ac117b3be37 Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary.
Raymond Hettinger <python@rcn.com>
parents: 44056
diff changeset
679 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
680 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
681 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
682 }
7ac117b3be37 Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary.
Raymond Hettinger <python@rcn.com>
parents: 44056
diff changeset
683 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
684 }
7ac117b3be37 Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary.
Raymond Hettinger <python@rcn.com>
parents: 44056
diff changeset
685
38540
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
686 /* 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
687 * 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
688 * 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
689 * 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
690 * (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
691 * (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
692 * 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
693 * 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
694 * 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
695 */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
696 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
697 PyDict_GetItem(PyObject *op, PyObject *key)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
698 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
699 long hash;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
700 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
701 PyDictEntry *ep;
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
702 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
703 if (!PyDict_Check(op))
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
704 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
705 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
706 (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
707 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
708 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
709 if (hash == -1) {
a23b75138bb0 Make sure that PyDict_GetItem[String]() *never* raises an exception.
Guido van Rossum <guido@python.org>
parents: 7985
diff changeset
710 PyErr_Clear();
5038
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
711 return NULL;
8326
a23b75138bb0 Make sure that PyDict_GetItem[String]() *never* raises an exception.
Guido van Rossum <guido@python.org>
parents: 7985
diff changeset
712 }
5038
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
713 }
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
714
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
715 /* 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
716 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
717 interning. Let's just hope that no exception occurs then... */
38715
e2d2dc506528 SF bug #1503294.
Armin Rigo <arigo@tunes.org>
parents: 38540
diff changeset
718 tstate = _PyThreadState_Current;
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
719 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
720 /* preserve the existing exception */
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
721 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
722 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
723 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
724 /* ignore errors */
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
725 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
726 if (ep == NULL)
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
727 return NULL;
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
728 }
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
729 else {
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
730 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
731 if (ep == NULL) {
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
732 PyErr_Clear();
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
733 return NULL;
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
734 }
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
735 }
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
736 return ep->me_value;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
737 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
738
17535
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
739 /* 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
740 * 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
741 * 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
742 * 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
743 * 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
744 */
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
745 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
746 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
747 {
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
748 register PyDictObject *mp;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
749 register long hash;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
750 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
751
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
752 if (!PyDict_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
753 PyErr_BadInternalCall();
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
754 return -1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
755 }
39127
7f38173251bb Add some asserts that we got good params passed
Neal Norwitz <nnorwitz@gmail.com>
parents: 38715
diff changeset
756 assert(key);
7f38173251bb Add some asserts that we got good params passed
Neal Norwitz <nnorwitz@gmail.com>
parents: 38715
diff changeset
757 assert(value);
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
758 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
759 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
760 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
761 if (hash == -1)
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24280
diff changeset
762 hash = PyObject_Hash(key);
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4976
diff changeset
763 }
22651
f3a416b178c4 Remove the CACHE_HASH and INTERN_STRINGS preprocessor symbols.
Tim Peters <tim.peters@gmail.com>
parents: 22625
diff changeset
764 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
765 hash = PyObject_Hash(key);
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4976
diff changeset
766 if (hash == -1)
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4976
diff changeset
767 return -1;
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4976
diff changeset
768 }
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
769 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
770 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
771 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
772 Py_INCREF(key);
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
773 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
774 return -1;
28984
17bfa786075a SF patch #729395: Dictionary tuning
Raymond Hettinger <python@rcn.com>
parents: 28946
diff changeset
775 /* 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
776 * 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
777 * 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
778 * (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
779 * keys have been * deleted).
36164
50886d42b444 Fixed English in a comment; trimmed trailing whitespace;
Tim Peters <tim.peters@gmail.com>
parents: 35636
diff changeset
780 *
28984
17bfa786075a SF patch #729395: Dictionary tuning
Raymond Hettinger <python@rcn.com>
parents: 28946
diff changeset
781 * Quadrupling the size improves average dictionary sparseness
17bfa786075a SF patch #729395: Dictionary tuning
Raymond Hettinger <python@rcn.com>
parents: 28946
diff changeset
782 * (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
783 * 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
784 * 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
785 *
50886d42b444 Fixed English in a comment; trimmed trailing whitespace;
Tim Peters <tim.peters@gmail.com>
parents: 35636
diff changeset
786 * 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
787 * 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
788 */
28984
17bfa786075a SF patch #729395: Dictionary tuning
Raymond Hettinger <python@rcn.com>
parents: 28946
diff changeset
789 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
790 return 0;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
791 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
792 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
793
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
794 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
795 PyDict_DelItem(PyObject *op, PyObject *key)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
796 {
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
797 register PyDictObject *mp;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
798 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
799 register PyDictEntry *ep;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
800 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
801
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
802 if (!PyDict_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
803 PyErr_BadInternalCall();
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
804 return -1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
805 }
39127
7f38173251bb Add some asserts that we got good params passed
Neal Norwitz <nnorwitz@gmail.com>
parents: 38715
diff changeset
806 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
807 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
808 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
809 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
810 if (hash == -1)
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
811 return -1;
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
812 }
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
813 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
814 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
815 if (ep == NULL)
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
816 return -1;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
817 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
818 set_key_error(key);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
819 return -1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
820 }
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
821 old_key = ep->me_key;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
822 Py_INCREF(dummy);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
823 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
824 old_value = ep->me_value;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
825 ep->me_value = NULL;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
826 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
827 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
828 Py_DECREF(old_key);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
829 return 0;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
830 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
831
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
832 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
833 PyDict_Clear(PyObject *op)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
834 {
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
835 PyDictObject *mp;
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
836 PyDictEntry *ep, *table;
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
837 int table_is_malloced;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
838 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
839 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
840 #ifdef Py_DEBUG
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
841 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
842 #endif
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
843
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
844 if (!PyDict_Check(op))
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
845 return;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
846 mp = (PyDictObject *)op;
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
847 #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
848 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
849 i = 0;
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
850 #endif
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
851
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
852 table = mp->ma_table;
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
853 assert(table != NULL);
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
854 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
855
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
856 /* 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
857 * 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
858 * (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
859 * 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
860 * clearing.
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
861 */
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
862 fill = mp->ma_fill;
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
863 if (table_is_malloced)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
864 EMPTY_TO_MINSIZE(mp);
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
865
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
866 else if (fill > 0) {
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
867 /* 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
868 * 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
869 * another small table first.
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
870 */
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
871 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
872 table = small_copy;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
873 EMPTY_TO_MINSIZE(mp);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
874 }
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
875 /* 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
876
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
877 /* 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
878 * 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
879 * 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
880 */
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
881 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
882 #ifdef Py_DEBUG
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
883 assert(i < n);
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
884 ++i;
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
885 #endif
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
886 if (ep->me_key) {
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
887 --fill;
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
888 Py_DECREF(ep->me_key);
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
889 Py_XDECREF(ep->me_value);
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
890 }
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
891 #ifdef Py_DEBUG
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
892 else
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
893 assert(ep->me_value == NULL);
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
894 #endif
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
895 }
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
896
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
897 if (table_is_malloced)
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
898 PyMem_DEL(table);
1256
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
27901
55adf08a213a cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents: 26818
diff changeset
901 /*
55adf08a213a cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents: 26818
diff changeset
902 * 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
903 *
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
904 * 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
905 * PyObject *key, *value;
55adf08a213a cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents: 26818
diff changeset
906 * 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
907 * 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
908 * 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
909 * }
55adf08a213a cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents: 26818
diff changeset
910 *
55adf08a213a cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents: 26818
diff changeset
911 * 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
912 * 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
913 * 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
914 * 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
915 */
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
916 int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
917 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
918 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
919 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
920 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
921 register PyDictEntry *ep;
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
922
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
923 if (!PyDict_Check(op))
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
924 return 0;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
925 i = *ppos;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
926 if (i < 0)
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
927 return 0;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
928 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
929 mask = ((PyDictObject *)op)->ma_mask;
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
930 while (i <= mask && ep[i].me_value == NULL)
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
931 i++;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
932 *ppos = i+1;
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
933 if (i > mask)
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
934 return 0;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
935 if (pkey)
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
936 *pkey = ep[i].me_key;
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
937 if (pvalue)
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
938 *pvalue = ep[i].me_value;
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
939 return 1;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
940 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
941
40864
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
942 /* 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
943 int
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
944 _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
945 {
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
946 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
947 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
948 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
949
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
950 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
951 return 0;
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
952 i = *ppos;
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
953 if (i < 0)
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
954 return 0;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
955 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
956 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
957 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
958 i++;
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
959 *ppos = i+1;
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
960 if (i > mask)
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
961 return 0;
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
962 *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
963 if (pkey)
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
964 *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
965 if (pvalue)
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
966 *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
967 return 1;
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
968 }
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
969
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
970 /* Methods */
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
971
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
972 static void
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
973 dict_dealloc(register PyDictObject *mp)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
974 {
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
975 register PyDictEntry *ep;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
976 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
977 PyObject_GC_UnTrack(mp);
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11838
diff changeset
978 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
979 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
980 if (ep->me_key) {
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
981 --fill;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
982 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
983 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
984 }
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
985 }
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
986 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
987 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
988 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
989 free_list[numfree++] = mp;
36164
50886d42b444 Fixed English in a comment; trimmed trailing whitespace;
Tim Peters <tim.peters@gmail.com>
parents: 35636
diff changeset
990 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
991 Py_TYPE(mp)->tp_free((PyObject *)mp);
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11838
diff changeset
992 Py_TRASHCAN_SAFE_END(mp)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
993 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
994
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
995 static int
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
996 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
997 {
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
998 register Py_ssize_t i;
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
999 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
1000 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
1001
38504
441711cbfe34 dict_print(): So that Neal & I don't spend the rest of
Tim Peters <tim.peters@gmail.com>
parents: 38503
diff changeset
1002 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
1003 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
1004 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
1005 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
1006 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
1007 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
1008 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
1009 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
1010 }
ab4f64561fba Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents: 6490
diff changeset
1011
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
1012 Py_BEGIN_ALLOW_THREADS
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1013 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
1014 Py_END_ALLOW_THREADS
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1015 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
1016 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
1017 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
1018 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
1019 if (pvalue != NULL) {
facff5f50fcc Coredumpers from Michael Hudson, mutating dicts while printing or
Tim Peters <tim.peters@gmail.com>
parents: 18015
diff changeset
1020 /* 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
1021 key format */
facff5f50fcc Coredumpers from Michael Hudson, mutating dicts while printing or
Tim Peters <tim.peters@gmail.com>
parents: 18015
diff changeset
1022 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
1023 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
1024 Py_BEGIN_ALLOW_THREADS
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1025 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
1026 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
1027 }
7985
ab4f64561fba Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents: 6490
diff changeset
1028 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
1029 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
1030 Py_ReprLeave((PyObject*)mp);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1031 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
1032 }
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
1033 Py_BEGIN_ALLOW_THREADS
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1034 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
1035 Py_END_ALLOW_THREADS
18019
ca7e2e1ae8df Finish the dict->string coredump fix. Need sleep.
Tim Peters <tim.peters@gmail.com>
parents: 18017
diff changeset
1036 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
1037 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
1038 Py_ReprLeave((PyObject*)mp);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1039 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
1040 }
18017
facff5f50fcc Coredumpers from Michael Hudson, mutating dicts while printing or
Tim Peters <tim.peters@gmail.com>
parents: 18015
diff changeset
1041 Py_DECREF(pvalue);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1042 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1043 }
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
1044 Py_BEGIN_ALLOW_THREADS
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1045 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
1046 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
1047 Py_ReprLeave((PyObject*)mp);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1048 return 0;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1049 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1050
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1051 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1052 dict_repr(PyDictObject *mp)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1053 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1054 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
1055 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
1056 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
1057 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
1058
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1059 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
1060 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
1061 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
1062 }
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1063
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1064 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
1065 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
1066 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
1067 }
ab4f64561fba Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents: 6490
diff changeset
1068
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1069 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
1070 if (pieces == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1071 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1072
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1073 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
1074 if (colon == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1075 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1076
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1077 /* 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
1078 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
1079 i = 0;
85e3fa1eb836 dict_repr: Reuse one of the int vars (minor code simplification).
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
1080 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
1081 int status;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1082 /* 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
1083 Py_INCREF(value);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1084 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
1085 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
1086 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
1087 Py_DECREF(value);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1088 if (s == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1089 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1090 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
1091 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
1092 if (status < 0)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1093 goto Done;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1094 }
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1095
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1096 /* 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
1097 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
1098 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
1099 if (s == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1100 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1101 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
1102 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
1103 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
1104 if (s == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1105 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1106
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1107 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
1108 if (s == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1109 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1110 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
1111 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
1112 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
1113 if (temp == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1114 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1115
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1116 /* 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
1117 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
1118 if (s == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1119 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
1120 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
1121 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
1122
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1123 Done:
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1124 Py_XDECREF(pieces);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1125 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
1126 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
1127 return result;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1128 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1129
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1130 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
1131 dict_length(PyDictObject *mp)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1132 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1133 return mp->ma_used;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1134 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1135
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1136 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1137 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
1138 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1139 PyObject *v;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1330
diff changeset
1140 long hash;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1141 PyDictEntry *ep;
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
1142 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
1143 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
1144 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1145 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
1146 if (hash == -1)
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
1147 return NULL;
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
1148 }
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1149 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
1150 if (ep == NULL)
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1151 return NULL;
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1152 v = ep->me_value;
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36511
diff changeset
1153 if (v == NULL) {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36511
diff changeset
1154 if (!PyDict_CheckExact(mp)) {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36511
diff changeset
1155 /* 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
1156 PyObject *missing;
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36511
diff changeset
1157 static PyObject *missing_str = NULL;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36511
diff changeset
1158 if (missing_str == NULL)
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1159 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
1160 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
1161 missing = _PyType_Lookup(Py_TYPE(mp), missing_str);
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36511
diff changeset
1162 if (missing != NULL)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36511
diff changeset
1163 return PyObject_CallFunctionObjArgs(missing,
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36511
diff changeset
1164 (PyObject *)mp, key, NULL);
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36511
diff changeset
1165 }
40257
9ab7a7736ca0 Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents: 40225
diff changeset
1166 set_key_error(key);
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36511
diff changeset
1167 return NULL;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36511
diff changeset
1168 }
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1169 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1170 Py_INCREF(v);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1171 return v;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1172 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1173
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1174 static int
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1175 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
1176 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1177 if (w == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1178 return PyDict_DelItem((PyObject *)mp, v);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1179 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1180 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
1181 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1182
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1183 static PyMappingMethods dict_as_mapping = {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1184 (lenfunc)dict_length, /*mp_length*/
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1185 (binaryfunc)dict_subscript, /*mp_subscript*/
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1186 (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
1187 };
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1188
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1189 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1190 dict_keys(register PyDictObject *mp)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1191 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1192 register PyObject *v;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1193 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
1194 PyDictEntry *ep;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1195 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
1196
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1197 again:
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1198 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
1199 v = PyList_New(n);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1200 if (v == NULL)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1201 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
1202 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
1203 /* 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
1204 * 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
1205 */
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 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
1208 }
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1209 ep = mp->ma_table;
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1210 mask = mp->ma_mask;
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1211 for (i = 0, j = 0; i <= mask; i++) {
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1212 if (ep[i].me_value != NULL) {
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1213 PyObject *key = ep[i].me_key;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1214 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
1215 PyList_SET_ITEM(v, j, key);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1216 j++;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1217 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1218 }
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1219 assert(j == n);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1220 return v;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1221 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1222
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1223 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1224 dict_values(register PyDictObject *mp)
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1225 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1226 register PyObject *v;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1227 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
1228 PyDictEntry *ep;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1229 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
1230
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1231 again:
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1232 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
1233 v = PyList_New(n);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1234 if (v == NULL)
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1235 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
1236 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
1237 /* 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
1238 * 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
1239 */
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1240 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
1241 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
1242 }
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1243 ep = mp->ma_table;
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1244 mask = mp->ma_mask;
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1245 for (i = 0, j = 0; i <= mask; i++) {
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1246 if (ep[i].me_value != NULL) {
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1247 PyObject *value = ep[i].me_value;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1248 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
1249 PyList_SET_ITEM(v, j, value);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1250 j++;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1251 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1252 }
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1253 assert(j == n);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1254 return v;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1255 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1256
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1257 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1258 dict_items(register PyDictObject *mp)
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1259 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1260 register PyObject *v;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1261 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
1262 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
1263 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
1264 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
1265
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1266 /* 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
1267 * 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
1268 * 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
1269 */
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1270 again:
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1271 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
1272 v = PyList_New(n);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1273 if (v == NULL)
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1274 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
1275 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
1276 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
1277 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
1278 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
1279 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
1280 }
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1281 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
1282 }
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1283 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
1284 /* 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
1285 * 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
1286 */
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1287 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
1288 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
1289 }
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1290 /* Nothing we do below makes any function calls. */
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1291 ep = mp->ma_table;
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1292 mask = mp->ma_mask;
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1293 for (i = 0, j = 0; i <= mask; i++) {
31689
0fb5066fc2a7 Factor out a double lookup.
Raymond Hettinger <python@rcn.com>
parents: 31683
diff changeset
1294 if ((value=ep[i].me_value) != NULL) {
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1295 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
1296 item = PyList_GET_ITEM(v, j);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1297 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
1298 PyTuple_SET_ITEM(item, 0, key);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1299 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
1300 PyTuple_SET_ITEM(item, 1, value);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1301 j++;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1302 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1303 }
17535
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1304 assert(j == n);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1305 return v;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1306 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1307
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
1308 static PyObject *
26583
8991f78817c7 SF 548651: Fix the METH_CLASS implementation.
Tim Peters <tim.peters@gmail.com>
parents: 26562
diff changeset
1309 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
1310 {
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1311 PyObject *seq;
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1312 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
1313 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
1314 PyObject *key;
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1315 PyObject *d;
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1316 int status;
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1317
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26599
diff changeset
1318 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
1319 return NULL;
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1320
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1321 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
1322 if (d == NULL)
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1323 return NULL;
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1324
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
1325 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
1326 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
1327 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
1328 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
1329 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
1330 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
1331
47847
1dd10b04db47 Issue 3230: Do not the set specific size macro.
Raymond Hettinger <python@rcn.com>
parents: 47829
diff changeset
1332 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
1333 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
1334
9f17a9a5b5ae Optimize dict.fromkeys() with dict inputs. Useful for resetting bag/muliset counts for example.
Raymond Hettinger <python@rcn.com>
parents: 43515
diff changeset
1335 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
1336 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
1337 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
1338 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
1339 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
1340 }
9f17a9a5b5ae Optimize dict.fromkeys() with dict inputs. Useful for resetting bag/muliset counts for example.
Raymond Hettinger <python@rcn.com>
parents: 43515
diff changeset
1341 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
1342 }
9f17a9a5b5ae Optimize dict.fromkeys() with dict inputs. Useful for resetting bag/muliset counts for example.
Raymond Hettinger <python@rcn.com>
parents: 43515
diff changeset
1343
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
1344 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
1345 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
1346 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
1347 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
1348 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
1349
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
1350 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
1351 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
1352
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
1353 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
1354 Py_INCREF(key);
41270
15a7e0b7e425 Add test and fix for fromkeys() optional argument.
Raymond Hettinger <python@rcn.com>
parents: 41248
diff changeset
1355 Py_INCREF(value);
15a7e0b7e425 Add test and fix for fromkeys() optional argument.
Raymond Hettinger <python@rcn.com>
parents: 41248
diff changeset
1356 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
1357 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
1358 }
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
1359 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
1360 }
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
1361
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1362 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
1363 if (it == NULL){
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1364 Py_DECREF(d);
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1365 return NULL;
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1366 }
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1367
43810
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1368 if (PyDict_CheckExact(d)) {
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1369 while ((key = PyIter_Next(it)) != NULL) {
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1370 status = PyDict_SetItem(d, key, value);
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1371 Py_DECREF(key);
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1372 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
1373 goto Fail;
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1374 }
43810
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1375 } else {
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1376 while ((key = PyIter_Next(it)) != NULL) {
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1377 status = PyObject_SetItem(d, key, value);
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1378 Py_DECREF(key);
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1379 if (status < 0)
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1380 goto Fail;
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1381 }
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1382 }
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1383
43810
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1384 if (PyErr_Occurred())
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1385 goto Fail;
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1386 Py_DECREF(it);
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1387 return d;
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1388
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1389 Fail:
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1390 Py_DECREF(it);
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1391 Py_DECREF(d);
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1392 return NULL;
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1393 }
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1394
31616
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1395 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
1396 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
1397 {
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1398 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
1399 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
1400
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1401 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
1402 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
1403
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1404 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
1405 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
1406 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
1407 else
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1408 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
1409 }
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1410 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
1411 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
1412 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
1413 }
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1414
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1415 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
1416 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
1417 {
31908
058821d8aefd * Add unittests for iterators that report their length
Raymond Hettinger <python@rcn.com>
parents: 31702
diff changeset
1418 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
1419 Py_RETURN_NONE;
058821d8aefd * Add unittests for iterators that report their length
Raymond Hettinger <python@rcn.com>
parents: 31702
diff changeset
1420 return NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1421 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1422
19150
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1423 /* Update unconditionally replaces existing items.
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1424 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
1425 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
1426
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1427 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
1428
21348
12310d643d8d SF bug #491415 PyDict_UpdateFromSeq2() unused
Tim Peters <tim.peters@gmail.com>
parents: 21155
diff changeset
1429 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
1430 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
1431 */
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1432
21348
12310d643d8d SF bug #491415 PyDict_UpdateFromSeq2() unused
Tim Peters <tim.peters@gmail.com>
parents: 21155
diff changeset
1433 int
20683
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1434 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
1435 {
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1436 PyObject *it; /* iter(seq2) */
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1437 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
1438 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
1439 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
1440
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1441 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
1442 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
1443 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
1444
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1445 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
1446 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
1447 return -1;
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1448
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1449 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
1450 PyObject *key, *value;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1451 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
1452
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1453 fast = NULL;
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1454 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
1455 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
1456 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
1457 goto Fail;
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1458 break;
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1459 }
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1460
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1461 /* 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
1462 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
1463 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
1464 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
1465 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
1466 "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
1467 "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
1468 i);
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1469 goto Fail;
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1470 }
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1471 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
1472 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
1473 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
1474 "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
1475 "has length %zd; 2 is required",
36511
1596ff5c79b5 Revert 42400.
Martin v. Löwis <martin@v.loewis.de>
parents: 36510
diff changeset
1476 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
1477 goto Fail;
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1478 }
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1479
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1480 /* 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
1481 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
1482 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
1483 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
1484 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
1485 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
1486 goto Fail;
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1487 }
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1488 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
1489 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
1490 }
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1491
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1492 i = 0;
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1493 goto Return;
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1494 Fail:
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1495 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
1496 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
1497 i = -1;
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1498 Return:
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1499 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
1500 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
1501 }
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1502
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1503 int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1504 PyDict_Update(PyObject *a, PyObject *b)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1505 {
19150
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1506 return PyDict_Merge(a, b, 1);
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1507 }
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1508
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1509 int
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1510 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
1511 {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1512 register PyDictObject *mp, *other;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1513 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
1514 PyDictEntry *entry;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1515
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1516 /* 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
1517 * 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
1518 * 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
1519 * 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
1520 */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1521 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
1522 PyErr_BadInternalCall();
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1523 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1524 }
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1525 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
1526 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
1527 other = (PyDictObject*)b;
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1528 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
1529 /* 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
1530 return 0;
34955
9339c99a2982 SF patch #1200051: Small optimization for PyDict_Merge()
Raymond Hettinger <python@rcn.com>
parents: 34909
diff changeset
1531 if (mp->ma_used == 0)
9339c99a2982 SF patch #1200051: Small optimization for PyDict_Merge()
Raymond Hettinger <python@rcn.com>
parents: 34909
diff changeset
1532 /* 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
1533 * 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
1534 * skips the unnecessary test.
9339c99a2982 SF patch #1200051: Small optimization for PyDict_Merge()
Raymond Hettinger <python@rcn.com>
parents: 34909
diff changeset
1535 */
9339c99a2982 SF patch #1200051: Small optimization for PyDict_Merge()
Raymond Hettinger <python@rcn.com>
parents: 34909
diff changeset
1536 override = 1;
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1537 /* 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
1538 * 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
1539 * 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
1540 */
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1541 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
1542 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
1543 return -1;
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1544 }
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1545 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
1546 entry = &other->ma_table[i];
19150
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1547 if (entry->me_value != NULL &&
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1548 (override ||
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1549 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
1550 Py_INCREF(entry->me_key);
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1551 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
1552 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
1553 (long)entry->me_hash,
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1554 entry->me_value) != 0)
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1555 return -1;
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1556 }
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1557 }
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1558 }
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1559 else {
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1560 /* 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
1561 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
1562 PyObject *iter;
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1563 PyObject *key, *value;
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1564 int status;
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1565
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1566 if (keys == NULL)
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1567 /* 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
1568 * 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
1569 * 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
1570 * 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
1571 */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1572 return -1;
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1573
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1574 iter = PyObject_GetIter(keys);
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1575 Py_DECREF(keys);
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1576 if (iter == NULL)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1577 return -1;
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1578
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1579 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
1580 if (!override && PyDict_GetItem(a, key) != NULL) {
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1581 Py_DECREF(key);
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1582 continue;
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1583 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1584 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
1585 if (value == NULL) {
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1586 Py_DECREF(iter);
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1587 Py_DECREF(key);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1588 return -1;
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1589 }
19150
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1590 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
1591 Py_DECREF(key);
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1592 Py_DECREF(value);
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1593 if (status < 0) {
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1594 Py_DECREF(iter);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1595 return -1;
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1596 }
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1597 }
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1598 Py_DECREF(iter);
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1599 if (PyErr_Occurred())
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1600 /* Iterator completed, via error */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1601 return -1;
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
1602 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1603 return 0;
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
1604 }
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
1605
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
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 dict_copy(register PyDictObject *mp)
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
1608 {
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
1609 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
1610 }
e6214ad2a60c Add PyDict_Copy() function to C API for dicts. It returns a new
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
1611
e6214ad2a60c Add PyDict_Copy() function to C API for dicts. It returns a new
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
1612 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
1613 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
1614 {
31620
47890c395eba Factor out code common to PyDict_Copy() and PyDict_Merge().
Raymond Hettinger <python@rcn.com>
parents: 31616
diff changeset
1615 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
1616
e6214ad2a60c Add PyDict_Copy() function to C API for dicts. It returns a new
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
1617 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
1618 PyErr_BadInternalCall();
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
1619 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
1620 }
31620
47890c395eba Factor out code common to PyDict_Copy() and PyDict_Merge().
Raymond Hettinger <python@rcn.com>
parents: 31616
diff changeset
1621 copy = PyDict_New();
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
1622 if (copy == NULL)
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
1623 return NULL;
31620
47890c395eba Factor out code common to PyDict_Copy() and PyDict_Merge().
Raymond Hettinger <python@rcn.com>
parents: 31616
diff changeset
1624 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
1625 return copy;
47890c395eba Factor out code common to PyDict_Copy() and PyDict_Merge().
Raymond Hettinger <python@rcn.com>
parents: 31616
diff changeset
1626 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
1627 return NULL;
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
1628 }
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
1629
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1630 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
1631 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
1632 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1633 if (mp == NULL || !PyDict_Check(mp)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1634 PyErr_BadInternalCall();
31545
391011840a03 Oops. Return -1 to distinguish error from empty dict.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 31174
diff changeset
1635 return -1;
1398
1d841587aede Added getmappingsize(). (Needed by previous checkin of posixmodule.c)
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
1636 }
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1637 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
1638 }
1d841587aede Added getmappingsize(). (Needed by previous checkin of posixmodule.c)
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
1639
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1640 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
1641 PyDict_Keys(PyObject *mp)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1642 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1643 if (mp == NULL || !PyDict_Check(mp)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1644 PyErr_BadInternalCall();
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1645 return NULL;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1646 }
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1647 return dict_keys((PyDictObject *)mp);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1648 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1649
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1650 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
1651 PyDict_Values(PyObject *mp)
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1652 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1653 if (mp == NULL || !PyDict_Check(mp)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1654 PyErr_BadInternalCall();
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1655 return NULL;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1656 }
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1657 return dict_values((PyDictObject *)mp);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1658 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1659
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1660 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
1661 PyDict_Items(PyObject *mp)
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1662 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1663 if (mp == NULL || !PyDict_Check(mp)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1664 PyErr_BadInternalCall();
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1665 return NULL;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1666 }
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1667 return dict_items((PyDictObject *)mp);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1668 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1669
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1670 /* 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
1671 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
1672 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
1673 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
1674 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
1675 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
1676 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
1677
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1678 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1679 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
1680 {
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1681 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
1682 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
1683 Py_ssize_t i;
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1684 int cmp;
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1685
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
1686 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
1687 PyObject *thiskey, *thisaval, *thisbval;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1688 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
1689 continue;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1690 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
1691 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
1692 if (akey != NULL) {
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1693 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
1694 if (cmp < 0) {
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1695 Py_DECREF(thiskey);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1696 goto Fail;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1697 }
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1698 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
1699 i > a->ma_mask ||
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1700 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
1701 {
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1702 /* 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
1703 * 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
1704 * find its associated value anymore; or
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1705 * 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
1706 * 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
1707 */
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1708 Py_DECREF(thiskey);
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1709 continue;
15974
5b9103e62fac Rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15708
diff changeset
1710 }
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1711 }
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1712
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1713 /* 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
1714 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
1715 assert(thisaval);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1716 Py_INCREF(thisaval); /* keep alive */
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1717 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1718 if (thisbval == NULL)
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1719 cmp = 0;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1720 else {
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1721 /* both dicts have thiskey: same values? */
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1722 cmp = PyObject_RichCompareBool(
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1723 thisaval, thisbval, Py_EQ);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1724 if (cmp < 0) {
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1725 Py_DECREF(thiskey);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1726 Py_DECREF(thisaval);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1727 goto Fail;
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1728 }
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1729 }
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1730 if (cmp == 0) {
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1731 /* New winner. */
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1732 Py_XDECREF(akey);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1733 Py_XDECREF(aval);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1734 akey = thiskey;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1735 aval = thisaval;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1736 }
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1737 else {
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1738 Py_DECREF(thiskey);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1739 Py_DECREF(thisaval);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1740 }
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1741 }
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1742 *pval = aval;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1743 return akey;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1744
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1745 Fail:
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1746 Py_XDECREF(akey);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1747 Py_XDECREF(aval);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1748 *pval = NULL;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1749 return NULL;
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1750 }
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1751
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1752 static int
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1753 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
1754 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1755 PyObject *adiff, *bdiff, *aval, *bval;
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1756 int res;
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1757
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1758 /* Compare lengths first */
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1759 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
1760 return -1; /* a is shorter */
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1761 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
1762 return 1; /* b is shorter */
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1763
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1764 /* Same length -- check all keys */
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1765 bdiff = bval = NULL;
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1766 adiff = characterize(a, b, &aval);
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1767 if (adiff == NULL) {
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1768 assert(!aval);
17771
fb103e3da2ae Repair typo in comment.
Tim Peters <tim.peters@gmail.com>
parents: 17756
diff changeset
1769 /* 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
1770 * must be equal.
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1771 */
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1772 res = PyErr_Occurred() ? -1 : 0;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1773 goto Finished;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1774 }
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1775 bdiff = characterize(b, a, &bval);
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1776 if (bdiff == NULL && PyErr_Occurred()) {
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1777 assert(!bval);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1778 res = -1;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1779 goto Finished;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1780 }
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1781 res = 0;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1782 if (bdiff) {
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1783 /* 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
1784 * 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
1785 * 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
1786 */
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1787 res = PyObject_Compare(adiff, bdiff);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1788 }
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1789 if (res == 0 && bval != NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1790 res = PyObject_Compare(aval, bval);
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1791
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1792 Finished:
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1793 Py_XDECREF(adiff);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1794 Py_XDECREF(bdiff);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1795 Py_XDECREF(aval);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1796 Py_XDECREF(bval);
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1797 return res;
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1798 }
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1799
17704
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1800 /* 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
1801 * 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
1802 * Uses only Py_EQ comparison.
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1803 */
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1804 static int
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1805 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
1806 {
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1807 Py_ssize_t i;
17704
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1808
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1809 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
1810 /* 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
1811 return 0;
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
1812
17704
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1813 /* 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
1814 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
1815 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
1816 if (aval != NULL) {
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1817 int cmp;
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1818 PyObject *bval;
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1819 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
1820 /* 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
1821 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
1822 Py_INCREF(aval);
39745
08ca476f8a34 Fix SF bug #1546288, crash in dict_equal.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39376
diff changeset
1823 /* ditto for key */
08ca476f8a34 Fix SF bug #1546288, crash in dict_equal.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39376
diff changeset
1824 Py_INCREF(key);
17704
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1825 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
1826 Py_DECREF(key);
17704
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1827 if (bval == NULL) {
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1828 Py_DECREF(aval);
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1829 return 0;
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1830 }
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1831 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
1832 Py_DECREF(aval);
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1833 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
1834 return cmp;
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1835 }
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1836 }
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1837 return 1;
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1838 }
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1839
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1840 static PyObject *
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1841 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
1842 {
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1843 int cmp;
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1844 PyObject *res;
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1845
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1846 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
1847 res = Py_NotImplemented;
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1848 }
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1849 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
1850 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
1851 if (cmp < 0)
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1852 return NULL;
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1853 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
1854 }
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
1855 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
1856 /* Py3K warning if comparison isn't == or != */
46573
aef3501d8e77 Use PyErr_WarnPy3k throughout
Benjamin Peterson <benjamin@python.org>
parents: 46076
diff changeset
1857 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
1858 "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
1859 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
1860 }
17775
dce1f53451a5 Restore dicts' tp_compare slot, and change dict_richcompare to say it
Tim Peters <tim.peters@gmail.com>
parents: 17771
diff changeset
1861 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
1862 }
17704
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1863 Py_INCREF(res);
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1864 return res;
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1865 }
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1866
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1867 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1868 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
1869 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1870 long hash;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1871 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
1872
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1873 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
1874 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1875 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
1876 if (hash == -1)
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
1877 return NULL;
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
1878 }
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1879 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
1880 if (ep == NULL)
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1881 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
1882 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
1883 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1884
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1885 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1886 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
1887 {
46573
aef3501d8e77 Use PyErr_WarnPy3k throughout
Benjamin Peterson <benjamin@python.org>
parents: 46076
diff changeset
1888 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
1889 "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
1890 return NULL;
a0ee3b2ba361 Add -3 option to the interpreter to warn about features that are
Neal Norwitz <nnorwitz@gmail.com>
parents: 41270
diff changeset
1891 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
1892 }
a0ee3b2ba361 Add -3 option to the interpreter to warn about features that are
Neal Norwitz <nnorwitz@gmail.com>
parents: 41270
diff changeset
1893
a0ee3b2ba361 Add -3 option to the interpreter to warn about features that are
Neal Norwitz <nnorwitz@gmail.com>
parents: 41270
diff changeset
1894 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1895 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
1896 {
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1897 PyObject *key;
6488
37c8d7f967c7 dict_get(): Fixed a couple of stupid mistakes which caused crashes.
Barry Warsaw <barry@python.org>
parents: 6395
diff changeset
1898 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
1899 PyObject *val = NULL;
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1900 long hash;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1901 PyDictEntry *ep;
6395
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1902
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26599
diff changeset
1903 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
1904 return NULL;
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1905
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1906 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
1907 (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
1908 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
1909 if (hash == -1)
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1910 return NULL;
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1911 }
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1912 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
1913 if (ep == NULL)
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1914 return NULL;
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1915 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
1916 if (val == NULL)
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1917 val = failobj;
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1918 Py_INCREF(val);
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1919 return val;
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1920 }
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1921
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1922
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1923 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1924 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
1925 {
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1926 PyObject *key;
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1927 PyObject *failobj = Py_None;
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1928 PyObject *val = NULL;
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1929 long hash;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1930 PyDictEntry *ep;
13892
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1931
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26599
diff changeset
1932 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
1933 return NULL;
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1934
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1935 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
1936 (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
1937 hash = PyObject_Hash(key);
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1938 if (hash == -1)
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1939 return NULL;
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1940 }
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1941 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
1942 if (ep == NULL)
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1943 return NULL;
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1944 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
1945 if (val == NULL) {
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1946 val = failobj;
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1947 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
1948 val = NULL;
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1949 }
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1950 Py_XINCREF(val);
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1951 return val;
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1952 }
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1953
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1954
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1955 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1956 dict_clear(register PyDictObject *mp)
5209
2ec9c933afb8 Add clear() method to dictionary objects.
Guido van Rossum <guido@python.org>
parents: 5059
diff changeset
1957 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1958 PyDict_Clear((PyObject *)mp);
31908
058821d8aefd * Add unittests for iterators that report their length
Raymond Hettinger <python@rcn.com>
parents: 31702
diff changeset
1959 Py_RETURN_NONE;
5209
2ec9c933afb8 Add clear() method to dictionary objects.
Guido van Rossum <guido@python.org>
parents: 5059
diff changeset
1960 }
2ec9c933afb8 Add clear() method to dictionary objects.
Guido van Rossum <guido@python.org>
parents: 5059
diff changeset
1961
15520
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
1962 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1963 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
1964 {
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1965 long hash;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1966 PyDictEntry *ep;
22994
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1967 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
1968 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
1969
28211
92e3b611dc9f SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents: 27902
diff changeset
1970 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
1971 return NULL;
22994
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1972 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
1973 if (deflt) {
92e3b611dc9f SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents: 27902
diff changeset
1974 Py_INCREF(deflt);
92e3b611dc9f SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents: 27902
diff changeset
1975 return deflt;
92e3b611dc9f SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents: 27902
diff changeset
1976 }
22994
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1977 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
1978 "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
1979 return NULL;
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1980 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1981 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
1982 (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
1983 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
1984 if (hash == -1)
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1985 return NULL;
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1986 }
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1987 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
1988 if (ep == NULL)
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1989 return NULL;
22994
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1990 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
1991 if (deflt) {
92e3b611dc9f SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents: 27902
diff changeset
1992 Py_INCREF(deflt);
92e3b611dc9f SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents: 27902
diff changeset
1993 return deflt;
92e3b611dc9f SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents: 27902
diff changeset
1994 }
40257
9ab7a7736ca0 Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents: 40225
diff changeset
1995 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
1996 return NULL;
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1997 }
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1998 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
1999 Py_INCREF(dummy);
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
2000 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
2001 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
2002 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
2003 mp->ma_used--;
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
2004 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
2005 return old_value;
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
2006 }
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
2007
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
2008 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2009 dict_popitem(PyDictObject *mp)
15520
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2010 {
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
2011 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
2012 PyDictEntry *ep;
15520
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2013 PyObject *res;
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2014
18015
0c1a04e98134 dict_popitem(): Repaired last-second 2.1 comment, which misidentified the
Tim Peters <tim.peters@gmail.com>
parents: 18014
diff changeset
2015 /* 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
2016 * 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
2017 * 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
2018 * 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
2019 * 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
2020 * 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
2021 * 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
2022 * 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
2023 */
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
2024 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
2025 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
2026 return NULL;
17536
d2da681ccb54 Tim pointed out a remaining vulnerability in popitem(): the
Guido van Rossum <guido@python.org>
parents: 17535
diff changeset
2027 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
2028 Py_DECREF(res);
d2da681ccb54 Tim pointed out a remaining vulnerability in popitem(): the
Guido van Rossum <guido@python.org>
parents: 17535
diff changeset
2029 PyErr_SetString(PyExc_KeyError,
d2da681ccb54 Tim pointed out a remaining vulnerability in popitem(): the
Guido van Rossum <guido@python.org>
parents: 17535
diff changeset
2030 "popitem(): dictionary is empty");
d2da681ccb54 Tim pointed out a remaining vulnerability in popitem(): the
Guido van Rossum <guido@python.org>
parents: 17535
diff changeset
2031 return NULL;
d2da681ccb54 Tim pointed out a remaining vulnerability in popitem(): the
Guido van Rossum <guido@python.org>
parents: 17535
diff changeset
2032 }
15520
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2033 /* 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
2034 * 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
2035 * 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
2036 * 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
2037 * 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
2038 */
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2039 ep = &mp->ma_table[0];
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2040 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
2041 i = ep->me_hash;
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
2042 /* 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
2043 * 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
2044 * 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
2045 * 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
2046 */
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
2047 if (i > mp->ma_mask || i < 1)
15520
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2048 i = 1; /* skip slot 0 */
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2049 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
2050 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
2051 if (i > mp->ma_mask)
15520
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2052 i = 1;
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2053 }
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2054 }
17535
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
2055 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
2056 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
2057 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
2058 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
2059 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
2060 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
2061 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
2062 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
2063 return res;
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2064 }
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2065
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2066 static int
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2067 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
2068 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2069 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
2070 PyObject *pk;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2071 PyObject *pv;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2072
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2073 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
2074 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
2075 Py_VISIT(pv);
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2076 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2077 return 0;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2078 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2079
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2080 static int
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2081 dict_tp_clear(PyObject *op)
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2082 {
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2083 PyDict_Clear(op);
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2084 return 0;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2085 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2086
15560
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2087
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2088 extern PyTypeObject PyDictIterKey_Type; /* Forward */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2089 extern PyTypeObject PyDictIterValue_Type; /* Forward */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2090 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
2091 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
2092
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2093 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2094 dict_iterkeys(PyDictObject *dict)
17644
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2095 {
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2096 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
2097 }
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2098
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2099 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2100 dict_itervalues(PyDictObject *dict)
17644
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2101 {
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2102 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
2103 }
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2104
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2105 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2106 dict_iteritems(PyDictObject *dict)
17644
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2107 {
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2108 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
2109 }
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2110
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47294
diff changeset
2111 static PyObject *
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47294
diff changeset
2112 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
2113 {
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47294
diff changeset
2114 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
2115
47829
7eb3737657d1 Corrected inconsistencies in sizeof tests and addressed issue pointed
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47527
diff changeset
2116 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
2117 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
2118 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
2119 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
2120 }
17644
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2121
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2122 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
2123 "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
2124
31172
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
2125 PyDoc_STRVAR(contains__doc__,
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
2126 "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
2127
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
2128 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
2129
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47294
diff changeset
2130 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
2131 "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
2132
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2133 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
2134 "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
2135
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2136 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
2137 "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
2138
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2139 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
2140 "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
2141 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
2142
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2143 PyDoc_STRVAR(popitem__doc__,
15560
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2144 "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
2145 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
2146
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2147 PyDoc_STRVAR(keys__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2148 "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
2149
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2150 PyDoc_STRVAR(items__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2151 "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
2152
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2153 PyDoc_STRVAR(values__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2154 "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
2155
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2156 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
2157 "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
2158 "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
2159 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
2160 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
2161
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
2162 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
2163 "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
2164 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
2165
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2166 PyDoc_STRVAR(clear__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2167 "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
2168
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2169 PyDoc_STRVAR(copy__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2170 "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
2171
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2172 PyDoc_STRVAR(iterkeys__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2173 "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
2174
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2175 PyDoc_STRVAR(itervalues__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2176 "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
2177
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2178 PyDoc_STRVAR(iteritems__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2179 "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
2180
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
2181 static PyMethodDef mapp_methods[] = {
41833
8c3578934df6 Whitespace cleanup
Neal Norwitz <nnorwitz@gmail.com>
parents: 41832
diff changeset
2182 {"__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
2183 contains__doc__},
31174
88e80a04328a Simplify previous checkin -- a new function was not needed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
2184 {"__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
2185 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
2186 {"__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
2187 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
2188 {"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
2189 has_key__doc__},
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2190 {"get", (PyCFunction)dict_get, METH_VARARGS,
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2191 get__doc__},
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2192 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2193 setdefault_doc__},
28211
92e3b611dc9f SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents: 27902
diff changeset
2194 {"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
2195 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
2196 {"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
2197 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
2198 {"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
2199 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
2200 {"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
2201 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
2202 {"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
2203 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
2204 {"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
2205 update__doc__},
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
2206 {"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
2207 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
2208 {"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
2209 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
2210 {"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
2211 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
2212 {"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
2213 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
2214 {"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
2215 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
2216 {"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
2217 iteritems__doc__},
15560
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2218 {NULL, NULL} /* sentinel */
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2219 };
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2220
38540
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
2221 /* 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
2222 int
b2fd7af17186 Expose dict_contains() and PyDict_Contains() with is about 10% faster
Raymond Hettinger <python@rcn.com>
parents: 30265
diff changeset
2223 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
2224 {
bf94af396b1e Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents: 17536
diff changeset
2225 long hash;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2226 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
2227 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
2228
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
2229 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
2230 (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
2231 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
2232 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
2233 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
2234 }
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
2235 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
2236 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
2237 }
bf94af396b1e Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents: 17536
diff changeset
2238
40864
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
2239 /* 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
2240 int
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
2241 _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
2242 {
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2243 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
2244 PyDictEntry *ep;
40864
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
2245
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
2246 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
2247 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
2248 }
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
2249
17594
bf94af396b1e Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents: 17536
diff changeset
2250 /* 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
2251 static PySequenceMethods dict_as_sequence = {
37304
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2252 0, /* sq_length */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2253 0, /* sq_concat */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2254 0, /* sq_repeat */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2255 0, /* sq_item */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2256 0, /* sq_slice */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2257 0, /* sq_ass_item */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2258 0, /* sq_ass_slice */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2259 PyDict_Contains, /* sq_contains */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2260 0, /* sq_inplace_concat */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2261 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
2262 };
bf94af396b1e Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents: 17536
diff changeset
2263
17644
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2264 static PyObject *
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2265 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
2266 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2267 PyObject *self;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2268
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2269 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
2270 self = type->tp_alloc(type, 0);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2271 if (self != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2272 PyDictObject *d = (PyDictObject *)self;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2273 /* 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
2274 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
2275 INIT_NONZERO_DICT_SLOTS(d);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2276 d->ma_lookup = lookdict_string;
52288
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
2277 /* The object has been implicitely tracked by tp_alloc */
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
2278 if (type == &PyDict_Type)
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
2279 _PyObject_GC_UNTRACK(d);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2280 #ifdef SHOW_CONVERSION_COUNTS
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2281 ++created;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2282 #endif
52288
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
2283 #ifdef SHOW_TRACK_COUNT
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
2284 if (_PyObject_GC_IS_TRACKED(d))
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
2285 count_tracked++;
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
2286 else
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
2287 count_untracked++;
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
2288 #endif
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2289 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2290 return self;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2291 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2292
19658
2fa615dfc464 Make dictionary() a real constructor. Accepts at most one argument, "a
Tim Peters <tim.peters@gmail.com>
parents: 19581
diff changeset
2293 static int
2fa615dfc464 Make dictionary() a real constructor. Accepts at most one argument, "a
Tim Peters <tim.peters@gmail.com>
parents: 19581
diff changeset
2294 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
2295 {
31616
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
2296 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
2297 }
2fa615dfc464 Make dictionary() a real constructor. Accepts at most one argument, "a
Tim Peters <tim.peters@gmail.com>
parents: 19581
diff changeset
2298
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2299 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2300 dict_iter(PyDictObject *dict)
17644
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2301 {
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2302 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
2303 }
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2304
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2305 PyDoc_STRVAR(dictionary_doc,
20754
272336648d1f Rename "dictionary" (type and constructor) to "dict".
Tim Peters <tim.peters@gmail.com>
parents: 20724
diff changeset
2306 "dict() -> new empty dictionary.\n"
272336648d1f Rename "dictionary" (type and constructor) to "dict".
Tim Peters <tim.peters@gmail.com>
parents: 20724
diff changeset
2307 "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
2308 " (key, value) pairs.\n"
20754
272336648d1f Rename "dictionary" (type and constructor) to "dict".
Tim Peters <tim.peters@gmail.com>
parents: 20724
diff changeset
2309 "dict(seq) -> new dictionary initialized as if via:\n"
20724
f224632d0260 dictionary() constructor:
Tim Peters <tim.peters@gmail.com>
parents: 20683
diff changeset
2310 " d = {}\n"
f224632d0260 dictionary() constructor:
Tim Peters <tim.peters@gmail.com>
parents: 20683
diff changeset
2311 " 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
2312 " d[k] = v\n"
08b720cd387e Patch #642500 with slight modifications: allow keyword arguments in
Just van Rossum <just@letterror.com>
parents: 25115
diff changeset
2313 "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
2314 " 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
2315
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
2316 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
2317 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
2318 "dict",
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2319 sizeof(PyDictObject),
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2320 0,
15974
5b9103e62fac Rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15708
diff changeset
2321 (destructor)dict_dealloc, /* tp_dealloc */
5b9103e62fac Rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15708
diff changeset
2322 (printfunc)dict_print, /* tp_print */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2323 0, /* tp_getattr */
15974
5b9103e62fac Rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15708
diff changeset
2324 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
2325 (cmpfunc)dict_compare, /* tp_compare */
15974
5b9103e62fac Rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15708
diff changeset
2326 (reprfunc)dict_repr, /* tp_repr */
5b9103e62fac Rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15708
diff changeset
2327 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
2328 &dict_as_sequence, /* tp_as_sequence */
15974
5b9103e62fac Rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15708
diff changeset
2329 &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
2330 (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
15974
5b9103e62fac Rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15708
diff changeset
2331 0, /* tp_call */
5b9103e62fac Rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15708
diff changeset
2332 0, /* tp_str */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2333 PyObject_GenericGetAttr, /* tp_getattro */
15974
5b9103e62fac Rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15708
diff changeset
2334 0, /* tp_setattro */
5b9103e62fac Rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15708
diff changeset
2335 0, /* tp_as_buffer */
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
2336 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
2337 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
2338 dictionary_doc, /* tp_doc */
37304
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2339 dict_traverse, /* tp_traverse */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2340 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
2341 dict_richcompare, /* tp_richcompare */
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2342 0, /* tp_weaklistoffset */
17644
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2343 (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
2344 0, /* tp_iternext */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2345 mapp_methods, /* tp_methods */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2346 0, /* tp_members */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2347 0, /* tp_getset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2348 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2349 0, /* tp_dict */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2350 0, /* tp_descr_get */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2351 0, /* tp_descr_set */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2352 0, /* tp_dictoffset */
37304
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2353 dict_init, /* tp_init */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2354 PyType_GenericAlloc, /* tp_alloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2355 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
2356 PyObject_GC_Del, /* tp_free */
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2357 };
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2358
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
2359 /* 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
2360
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
2361 PyObject *
26599
ecea8410178e Constify char* API. Fixes #651363. 2.2 candidate.
Martin v. Löwis <martin@v.loewis.de>
parents: 26583
diff changeset
2362 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
2363 {
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
2364 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
2365 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
2366 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
2367 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
2368 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
2369 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
2370 return rv;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2371 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2372
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2373 int
26599
ecea8410178e Constify char* API. Fixes #651363. 2.2 candidate.
Martin v. Löwis <martin@v.loewis.de>
parents: 26583
diff changeset
2374 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
2375 {
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
2376 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
2377 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
2378 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
2379 if (kv == NULL)
5561
4dcf26fe4da5 Moved PyObject_{Get,Set}Attr to object.c.
Guido van Rossum <guido@python.org>
parents: 5540
diff changeset
2380 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
2381 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
2382 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
2383 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
2384 return err;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2385 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2386
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2387 int
26599
ecea8410178e Constify char* API. Fixes #651363. 2.2 candidate.
Martin v. Löwis <martin@v.loewis.de>
parents: 26583
diff changeset
2388 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
2389 {
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
2390 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
2391 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
2392 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
2393 if (kv == NULL)
5561
4dcf26fe4da5 Moved PyObject_{Get,Set}Attr to object.c.
Guido van Rossum <guido@python.org>
parents: 5540
diff changeset
2394 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
2395 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
2396 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
2397 return err;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2398 }
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2399
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2400 /* Dictionary iterator types */
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2401
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2402 typedef struct {
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2403 PyObject_HEAD
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2404 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
2405 Py_ssize_t di_used;
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
2406 Py_ssize_t di_pos;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2407 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
2408 Py_ssize_t len;
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2409 } dictiterobject;
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2410
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2411 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2412 dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2413 {
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2414 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
2415 di = PyObject_GC_New(dictiterobject, itertype);
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2416 if (di == NULL)
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2417 return NULL;
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2418 Py_INCREF(dict);
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2419 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
2420 di->di_used = dict->ma_used;
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2421 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
2422 di->len = dict->ma_used;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2423 if (itertype == &PyDictIterItem_Type) {
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2424 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2425 if (di->di_result == NULL) {
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2426 Py_DECREF(di);
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2427 return NULL;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2428 }
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2429 }
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2430 else
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2431 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
2432 _PyObject_GC_TRACK(di);
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2433 return (PyObject *)di;
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2434 }
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2435
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2436 static void
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2437 dictiter_dealloc(dictiterobject *di)
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2438 {
24259
2f60f90ce309 Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
2439 Py_XDECREF(di->di_dict);
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2440 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
2441 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
2442 }
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
2443
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
2444 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
2445 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
2446 {
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_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
2448 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
2449 return 0;
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2450 }
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2451
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2452 static PyObject *
31683
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2453 dictiter_len(dictiterobject *di)
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2454 {
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
2455 Py_ssize_t len = 0;
31908
058821d8aefd * Add unittests for iterators that report their length
Raymond Hettinger <python@rcn.com>
parents: 31702
diff changeset
2456 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
2457 len = di->len;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
2458 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
2459 }
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2460
36484
20f75d5e79e9 Renamed _length_cue() to __length_hint__(). See:
Armin Rigo <arigo@tunes.org>
parents: 36164
diff changeset
2461 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
2462
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2463 static PyMethodDef dictiter_methods[] = {
36484
20f75d5e79e9 Renamed _length_cue() to __length_hint__(). See:
Armin Rigo <arigo@tunes.org>
parents: 36164
diff changeset
2464 {"__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
2465 {NULL, NULL} /* sentinel */
31683
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2466 };
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2467
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2468 static PyObject *dictiter_iternextkey(dictiterobject *di)
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2469 {
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2470 PyObject *key;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
2471 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
2472 register PyDictEntry *ep;
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2473 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
2474
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2475 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
2476 return NULL;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2477 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
2478
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2479 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
2480 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
2481 "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
2482 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
2483 return NULL;
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
2484 }
24259
2f60f90ce309 Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
2485
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2486 i = di->di_pos;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2487 if (i < 0)
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2488 goto fail;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2489 ep = d->ma_table;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2490 mask = d->ma_mask;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2491 while (i <= mask && ep[i].me_value == NULL)
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2492 i++;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2493 di->di_pos = i+1;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2494 if (i > mask)
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2495 goto fail;
31683
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2496 di->len--;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2497 key = ep[i].me_key;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2498 Py_INCREF(key);
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2499 return key;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2500
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2501 fail:
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2502 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
2503 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
2504 return NULL;
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2505 }
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2506
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2507 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
2508 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
2509 "dictionary-keyiterator", /* tp_name */
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2510 sizeof(dictiterobject), /* tp_basicsize */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2511 0, /* tp_itemsize */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2512 /* methods */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2513 (destructor)dictiter_dealloc, /* tp_dealloc */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2514 0, /* tp_print */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2515 0, /* tp_getattr */
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2516 0, /* tp_setattr */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2517 0, /* tp_compare */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2518 0, /* tp_repr */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2519 0, /* tp_as_number */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2520 0, /* tp_as_sequence */
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2521 0, /* tp_as_mapping */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2522 0, /* tp_hash */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2523 0, /* tp_call */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2524 0, /* tp_str */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2525 PyObject_GenericGetAttr, /* tp_getattro */
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2526 0, /* tp_setattro */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2527 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
2528 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
2529 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
2530 (traverseproc)dictiter_traverse, /* tp_traverse */
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2531 0, /* tp_clear */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2532 0, /* tp_richcompare */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2533 0, /* tp_weaklistoffset */
28320
fd5ef7003469 Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents: 28308
diff changeset
2534 PyObject_SelfIter, /* tp_iter */
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2535 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2536 dictiter_methods, /* tp_methods */
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2537 0,
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2538 };
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2539
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2540 static PyObject *dictiter_iternextvalue(dictiterobject *di)
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 PyObject *value;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
2543 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
2544 register PyDictEntry *ep;
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2545 PyDictObject *d = di->di_dict;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2546
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2547 if (d == NULL)
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2548 return NULL;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2549 assert (PyDict_Check(d));
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2550
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2551 if (di->di_used != d->ma_used) {
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2552 PyErr_SetString(PyExc_RuntimeError,
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2553 "dictionary changed size during iteration");
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2554 di->di_used = -1; /* Make this state sticky */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2555 return NULL;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2556 }
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2557
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2558 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
2559 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
2560 if (i < 0 || i > mask)
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2561 goto fail;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2562 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
2563 while ((value=ep[i].me_value) == NULL) {
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2564 i++;
31702
36a2d0e8a993 GCC was complaining that 'value' in dictiter_iternextvalue() wasn't
Guido van Rossum <guido@python.org>
parents: 31689
diff changeset
2565 if (i > mask)
36a2d0e8a993 GCC was complaining that 'value' in dictiter_iternextvalue() wasn't
Guido van Rossum <guido@python.org>
parents: 31689
diff changeset
2566 goto fail;
36a2d0e8a993 GCC was complaining that 'value' in dictiter_iternextvalue() wasn't
Guido van Rossum <guido@python.org>
parents: 31689
diff changeset
2567 }
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2568 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
2569 di->len--;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2570 Py_INCREF(value);
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2571 return value;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2572
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2573 fail:
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2574 Py_DECREF(d);
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2575 di->di_dict = NULL;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2576 return NULL;
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
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2579 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
2580 PyVarObject_HEAD_INIT(&PyType_Type, 0)
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2581 "dictionary-valueiterator", /* tp_name */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2582 sizeof(dictiterobject), /* tp_basicsize */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2583 0, /* tp_itemsize */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2584 /* methods */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2585 (destructor)dictiter_dealloc, /* tp_dealloc */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2586 0, /* tp_print */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2587 0, /* tp_getattr */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2588 0, /* tp_setattr */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2589 0, /* tp_compare */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2590 0, /* tp_repr */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2591 0, /* tp_as_number */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2592 0, /* tp_as_sequence */
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2593 0, /* tp_as_mapping */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2594 0, /* tp_hash */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2595 0, /* tp_call */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2596 0, /* tp_str */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2597 PyObject_GenericGetAttr, /* tp_getattro */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2598 0, /* tp_setattro */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2599 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
2600 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2601 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
2602 (traverseproc)dictiter_traverse, /* tp_traverse */
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2603 0, /* tp_clear */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2604 0, /* tp_richcompare */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2605 0, /* tp_weaklistoffset */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2606 PyObject_SelfIter, /* tp_iter */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2607 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2608 dictiter_methods, /* tp_methods */
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2609 0,
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2610 };
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2611
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2612 static PyObject *dictiter_iternextitem(dictiterobject *di)
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2613 {
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2614 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
2615 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
2616 register PyDictEntry *ep;
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2617 PyDictObject *d = di->di_dict;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2618
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2619 if (d == NULL)
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2620 return NULL;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2621 assert (PyDict_Check(d));
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2622
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2623 if (di->di_used != d->ma_used) {
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2624 PyErr_SetString(PyExc_RuntimeError,
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2625 "dictionary changed size during iteration");
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2626 di->di_used = -1; /* Make this state sticky */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2627 return NULL;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2628 }
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2629
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2630 i = di->di_pos;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2631 if (i < 0)
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2632 goto fail;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2633 ep = d->ma_table;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2634 mask = d->ma_mask;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2635 while (i <= mask && ep[i].me_value == NULL)
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2636 i++;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2637 di->di_pos = i+1;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2638 if (i > mask)
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2639 goto fail;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2640
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2641 if (result->ob_refcnt == 1) {
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2642 Py_INCREF(result);
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2643 Py_DECREF(PyTuple_GET_ITEM(result, 0));
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2644 Py_DECREF(PyTuple_GET_ITEM(result, 1));
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2645 } else {
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2646 result = PyTuple_New(2);
36164
50886d42b444 Fixed English in a comment; trimmed trailing whitespace;
Tim Peters <tim.peters@gmail.com>
parents: 35636
diff changeset
2647 if (result == NULL)
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2648 return NULL;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2649 }
31683
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2650 di->len--;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2651 key = ep[i].me_key;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2652 value = ep[i].me_value;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2653 Py_INCREF(key);
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2654 Py_INCREF(value);
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2655 PyTuple_SET_ITEM(result, 0, key);
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2656 PyTuple_SET_ITEM(result, 1, value);
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2657 return result;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2658
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2659 fail:
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2660 Py_DECREF(d);
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2661 di->di_dict = NULL;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2662 return NULL;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2663 }
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2664
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2665 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
2666 PyVarObject_HEAD_INIT(&PyType_Type, 0)
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2667 "dictionary-itemiterator", /* tp_name */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2668 sizeof(dictiterobject), /* tp_basicsize */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2669 0, /* tp_itemsize */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2670 /* methods */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2671 (destructor)dictiter_dealloc, /* tp_dealloc */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2672 0, /* tp_print */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2673 0, /* tp_getattr */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2674 0, /* tp_setattr */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2675 0, /* tp_compare */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2676 0, /* tp_repr */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2677 0, /* tp_as_number */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2678 0, /* tp_as_sequence */
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2679 0, /* tp_as_mapping */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2680 0, /* tp_hash */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2681 0, /* tp_call */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2682 0, /* tp_str */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2683 PyObject_GenericGetAttr, /* tp_getattro */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2684 0, /* tp_setattro */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2685 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
2686 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2687 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
2688 (traverseproc)dictiter_traverse, /* tp_traverse */
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2689 0, /* tp_clear */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2690 0, /* tp_richcompare */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2691 0, /* tp_weaklistoffset */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2692 PyObject_SelfIter, /* tp_iter */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2693 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2694 dictiter_methods, /* tp_methods */
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2695 0,
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2696 };