annotate Objects/dictobject.c @ 58765:2ba7677bfa95 legacy-trunk

testCopysign was defined twice in test_math; combine the definitions
author Mark Dickinson <dickinsm@gmail.com>
date Sat, 06 Feb 2010 23:11:25 +0000
parents e30fcb956b5f
children 799bc4964a1d
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
55379
3815abed9279 must use _PyThreadState_Current so it isn't checked for NULL #6530
Benjamin Peterson <benjamin@python.org>
parents: 54556
diff changeset
715 /* We can arrive here with a NULL tstate during initialization: try
3815abed9279 must use _PyThreadState_Current so it isn't checked for NULL #6530
Benjamin Peterson <benjamin@python.org>
parents: 54556
diff changeset
716 running "python -Wi" for an example related to string interning.
3815abed9279 must use _PyThreadState_Current so it isn't checked for NULL #6530
Benjamin Peterson <benjamin@python.org>
parents: 54556
diff changeset
717 Let's just hope that no exception occurs then... This must be
3815abed9279 must use _PyThreadState_Current so it isn't checked for NULL #6530
Benjamin Peterson <benjamin@python.org>
parents: 54556
diff changeset
718 _PyThreadState_Current and not PyThreadState_GET() because in debug
55437
cffb1e963dd8 clarify
Benjamin Peterson <benjamin@python.org>
parents: 55379
diff changeset
719 mode, the latter complains if tstate is NULL. */
55379
3815abed9279 must use _PyThreadState_Current so it isn't checked for NULL #6530
Benjamin Peterson <benjamin@python.org>
parents: 54556
diff changeset
720 tstate = _PyThreadState_Current;
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
721 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
722 /* preserve the existing exception */
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
723 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
724 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
725 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
726 /* ignore errors */
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
727 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
728 if (ep == NULL)
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
729 return NULL;
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
730 }
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
731 else {
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
732 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
733 if (ep == NULL) {
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
734 PyErr_Clear();
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
735 return NULL;
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
736 }
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
737 }
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
738 return ep->me_value;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
739 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
740
17535
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
741 /* 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
742 * 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
743 * 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
744 * 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
745 * 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
746 */
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
747 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
748 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
749 {
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
750 register PyDictObject *mp;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
751 register long hash;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
752 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
753
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
754 if (!PyDict_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
755 PyErr_BadInternalCall();
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
756 return -1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
757 }
39127
7f38173251bb Add some asserts that we got good params passed
Neal Norwitz <nnorwitz@gmail.com>
parents: 38715
diff changeset
758 assert(key);
7f38173251bb Add some asserts that we got good params passed
Neal Norwitz <nnorwitz@gmail.com>
parents: 38715
diff changeset
759 assert(value);
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
760 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
761 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
762 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
763 if (hash == -1)
bc660f73795c SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents: 24280
diff changeset
764 hash = PyObject_Hash(key);
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4976
diff changeset
765 }
22651
f3a416b178c4 Remove the CACHE_HASH and INTERN_STRINGS preprocessor symbols.
Tim Peters <tim.peters@gmail.com>
parents: 22625
diff changeset
766 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
767 hash = PyObject_Hash(key);
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4976
diff changeset
768 if (hash == -1)
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4976
diff changeset
769 return -1;
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4976
diff changeset
770 }
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
771 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
772 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
773 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
774 Py_INCREF(key);
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
775 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
776 return -1;
28984
17bfa786075a SF patch #729395: Dictionary tuning
Raymond Hettinger <python@rcn.com>
parents: 28946
diff changeset
777 /* 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
778 * 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
779 * 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
780 * (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
781 * keys have been * deleted).
36164
50886d42b444 Fixed English in a comment; trimmed trailing whitespace;
Tim Peters <tim.peters@gmail.com>
parents: 35636
diff changeset
782 *
28984
17bfa786075a SF patch #729395: Dictionary tuning
Raymond Hettinger <python@rcn.com>
parents: 28946
diff changeset
783 * Quadrupling the size improves average dictionary sparseness
17bfa786075a SF patch #729395: Dictionary tuning
Raymond Hettinger <python@rcn.com>
parents: 28946
diff changeset
784 * (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
785 * 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
786 * 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
787 *
50886d42b444 Fixed English in a comment; trimmed trailing whitespace;
Tim Peters <tim.peters@gmail.com>
parents: 35636
diff changeset
788 * 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
789 * 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
790 */
28984
17bfa786075a SF patch #729395: Dictionary tuning
Raymond Hettinger <python@rcn.com>
parents: 28946
diff changeset
791 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
792 return 0;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
793 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
794 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
795
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
796 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
797 PyDict_DelItem(PyObject *op, PyObject *key)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
798 {
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
799 register PyDictObject *mp;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
800 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
801 register PyDictEntry *ep;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
802 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
803
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
804 if (!PyDict_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
805 PyErr_BadInternalCall();
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
806 return -1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
807 }
39127
7f38173251bb Add some asserts that we got good params passed
Neal Norwitz <nnorwitz@gmail.com>
parents: 38715
diff changeset
808 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
809 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
810 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
811 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
812 if (hash == -1)
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
813 return -1;
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
814 }
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
815 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
816 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
817 if (ep == NULL)
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
818 return -1;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
819 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
820 set_key_error(key);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
821 return -1;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
822 }
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
823 old_key = ep->me_key;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
824 Py_INCREF(dummy);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
825 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
826 old_value = ep->me_value;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
827 ep->me_value = NULL;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
828 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
829 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
830 Py_DECREF(old_key);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
831 return 0;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
832 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
833
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
834 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
835 PyDict_Clear(PyObject *op)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
836 {
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
837 PyDictObject *mp;
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
838 PyDictEntry *ep, *table;
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
839 int table_is_malloced;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
840 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
841 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
842 #ifdef Py_DEBUG
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
843 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
844 #endif
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
845
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
846 if (!PyDict_Check(op))
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
847 return;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
848 mp = (PyDictObject *)op;
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
849 #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
850 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
851 i = 0;
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
852 #endif
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
853
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
854 table = mp->ma_table;
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
855 assert(table != NULL);
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
856 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
857
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
858 /* 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
859 * 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
860 * (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
861 * 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
862 * clearing.
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
863 */
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
864 fill = mp->ma_fill;
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
865 if (table_is_malloced)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
866 EMPTY_TO_MINSIZE(mp);
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
867
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
868 else if (fill > 0) {
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
869 /* 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
870 * 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
871 * another small table first.
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
872 */
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
873 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
874 table = small_copy;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
875 EMPTY_TO_MINSIZE(mp);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
876 }
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
877 /* 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
878
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
879 /* 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
880 * 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
881 * 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
882 */
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
883 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
884 #ifdef Py_DEBUG
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
885 assert(i < n);
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
886 ++i;
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
887 #endif
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
888 if (ep->me_key) {
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
889 --fill;
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
890 Py_DECREF(ep->me_key);
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
891 Py_XDECREF(ep->me_value);
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
892 }
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
893 #ifdef Py_DEBUG
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
894 else
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
895 assert(ep->me_value == NULL);
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
896 #endif
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
897 }
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
898
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
899 if (table_is_malloced)
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
900 PyMem_DEL(table);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
901 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
902
27901
55adf08a213a cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents: 26818
diff changeset
903 /*
55adf08a213a cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents: 26818
diff changeset
904 * 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
905 *
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
906 * 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
907 * PyObject *key, *value;
55adf08a213a cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents: 26818
diff changeset
908 * 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
909 * 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
910 * 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
911 * }
55adf08a213a cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents: 26818
diff changeset
912 *
55adf08a213a cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents: 26818
diff changeset
913 * 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
914 * 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
915 * 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
916 * 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
917 */
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
918 int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
919 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
920 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
921 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
922 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
923 register PyDictEntry *ep;
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
924
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
925 if (!PyDict_Check(op))
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
926 return 0;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
927 i = *ppos;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
928 if (i < 0)
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
929 return 0;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
930 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
931 mask = ((PyDictObject *)op)->ma_mask;
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
932 while (i <= mask && ep[i].me_value == NULL)
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
933 i++;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
934 *ppos = i+1;
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
935 if (i > mask)
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
936 return 0;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
937 if (pkey)
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
938 *pkey = ep[i].me_key;
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
939 if (pvalue)
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
940 *pvalue = ep[i].me_value;
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
941 return 1;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
942 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
943
40864
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
944 /* 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
945 int
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
946 _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
947 {
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
948 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
949 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
950 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
951
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
952 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
953 return 0;
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
954 i = *ppos;
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
955 if (i < 0)
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
956 return 0;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
957 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
958 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
959 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
960 i++;
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
961 *ppos = i+1;
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
962 if (i > mask)
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
963 return 0;
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
964 *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
965 if (pkey)
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
966 *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
967 if (pvalue)
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
968 *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
969 return 1;
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
970 }
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
971
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
972 /* Methods */
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
973
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
974 static void
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
975 dict_dealloc(register PyDictObject *mp)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
976 {
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
977 register PyDictEntry *ep;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
978 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
979 PyObject_GC_UnTrack(mp);
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11838
diff changeset
980 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
981 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
982 if (ep->me_key) {
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
983 --fill;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
984 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
985 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
986 }
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
987 }
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
988 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
989 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
990 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
991 free_list[numfree++] = mp;
36164
50886d42b444 Fixed English in a comment; trimmed trailing whitespace;
Tim Peters <tim.peters@gmail.com>
parents: 35636
diff changeset
992 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
993 Py_TYPE(mp)->tp_free((PyObject *)mp);
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11838
diff changeset
994 Py_TRASHCAN_SAFE_END(mp)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
995 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
996
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
997 static int
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
998 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
999 {
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1000 register Py_ssize_t i;
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1001 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
1002 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
1003
38504
441711cbfe34 dict_print(): So that Neal & I don't spend the rest of
Tim Peters <tim.peters@gmail.com>
parents: 38503
diff changeset
1004 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
1005 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
1006 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
1007 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
1008 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
1009 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
1010 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
1011 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
1012 }
ab4f64561fba Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents: 6490
diff changeset
1013
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_BEGIN_ALLOW_THREADS
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1015 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
1016 Py_END_ALLOW_THREADS
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1017 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
1018 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
1019 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
1020 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
1021 if (pvalue != NULL) {
facff5f50fcc Coredumpers from Michael Hudson, mutating dicts while printing or
Tim Peters <tim.peters@gmail.com>
parents: 18015
diff changeset
1022 /* 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
1023 key format */
facff5f50fcc Coredumpers from Michael Hudson, mutating dicts while printing or
Tim Peters <tim.peters@gmail.com>
parents: 18015
diff changeset
1024 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
1025 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
1026 Py_BEGIN_ALLOW_THREADS
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1027 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
1028 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
1029 }
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 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
1031 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
1032 Py_ReprLeave((PyObject*)mp);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1033 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
1034 }
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_BEGIN_ALLOW_THREADS
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1036 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
1037 Py_END_ALLOW_THREADS
18019
ca7e2e1ae8df Finish the dict->string coredump fix. Need sleep.
Tim Peters <tim.peters@gmail.com>
parents: 18017
diff changeset
1038 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
1039 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
1040 Py_ReprLeave((PyObject*)mp);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1041 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
1042 }
18017
facff5f50fcc Coredumpers from Michael Hudson, mutating dicts while printing or
Tim Peters <tim.peters@gmail.com>
parents: 18015
diff changeset
1043 Py_DECREF(pvalue);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1044 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1045 }
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_BEGIN_ALLOW_THREADS
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1047 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
1048 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
1049 Py_ReprLeave((PyObject*)mp);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1050 return 0;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1051 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1052
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1053 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1054 dict_repr(PyDictObject *mp)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1055 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1056 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
1057 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
1058 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
1059 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
1060
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1061 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
1062 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
1063 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
1064 }
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1065
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1066 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
1067 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
1068 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
1069 }
ab4f64561fba Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents: 6490
diff changeset
1070
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1071 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
1072 if (pieces == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1073 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1074
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1075 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
1076 if (colon == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1077 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1078
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1079 /* 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
1080 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
1081 i = 0;
85e3fa1eb836 dict_repr: Reuse one of the int vars (minor code simplification).
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
1082 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
1083 int status;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1084 /* 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
1085 Py_INCREF(value);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1086 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
1087 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
1088 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
1089 Py_DECREF(value);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1090 if (s == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1091 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1092 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
1093 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
1094 if (status < 0)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1095 goto Done;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1096 }
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1097
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1098 /* 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
1099 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
1100 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
1101 if (s == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1102 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1103 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
1104 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
1105 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
1106 if (s == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1107 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1108
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1109 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
1110 if (s == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1111 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1112 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
1113 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
1114 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
1115 if (temp == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1116 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1117
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1118 /* 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
1119 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
1120 if (s == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1121 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
1122 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
1123 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
1124
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1125 Done:
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1126 Py_XDECREF(pieces);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1127 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
1128 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
1129 return result;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1130 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1131
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1132 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
1133 dict_length(PyDictObject *mp)
1256
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 return mp->ma_used;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1136 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1137
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1138 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1139 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
1140 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1141 PyObject *v;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1330
diff changeset
1142 long hash;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1143 PyDictEntry *ep;
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
1144 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
1145 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
1146 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1147 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
1148 if (hash == -1)
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
1149 return NULL;
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
1150 }
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1151 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
1152 if (ep == NULL)
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1153 return NULL;
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1154 v = ep->me_value;
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36511
diff changeset
1155 if (v == NULL) {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36511
diff changeset
1156 if (!PyDict_CheckExact(mp)) {
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36511
diff changeset
1157 /* Look up __missing__ method if we're a subclass. */
54368
7b4b921f3335 plug ref leak
Benjamin Peterson <benjamin@python.org>
parents: 54367
diff changeset
1158 PyObject *missing, *res;
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36511
diff changeset
1159 static PyObject *missing_str = NULL;
54367
ba6376dff6c4 correctly handle descrs with __missing__
Benjamin Peterson <benjamin@python.org>
parents: 52292
diff changeset
1160 missing = _PyObject_LookupSpecial((PyObject *)mp,
ba6376dff6c4 correctly handle descrs with __missing__
Benjamin Peterson <benjamin@python.org>
parents: 52292
diff changeset
1161 "__missing__",
ba6376dff6c4 correctly handle descrs with __missing__
Benjamin Peterson <benjamin@python.org>
parents: 52292
diff changeset
1162 &missing_str);
54368
7b4b921f3335 plug ref leak
Benjamin Peterson <benjamin@python.org>
parents: 54367
diff changeset
1163 if (missing != NULL) {
7b4b921f3335 plug ref leak
Benjamin Peterson <benjamin@python.org>
parents: 54367
diff changeset
1164 res = PyObject_CallFunctionObjArgs(missing,
7b4b921f3335 plug ref leak
Benjamin Peterson <benjamin@python.org>
parents: 54367
diff changeset
1165 key, NULL);
7b4b921f3335 plug ref leak
Benjamin Peterson <benjamin@python.org>
parents: 54367
diff changeset
1166 Py_DECREF(missing);
7b4b921f3335 plug ref leak
Benjamin Peterson <benjamin@python.org>
parents: 54367
diff changeset
1167 return res;
7b4b921f3335 plug ref leak
Benjamin Peterson <benjamin@python.org>
parents: 54367
diff changeset
1168 }
54367
ba6376dff6c4 correctly handle descrs with __missing__
Benjamin Peterson <benjamin@python.org>
parents: 52292
diff changeset
1169 else if (PyErr_Occurred())
ba6376dff6c4 correctly handle descrs with __missing__
Benjamin Peterson <benjamin@python.org>
parents: 52292
diff changeset
1170 return NULL;
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36511
diff changeset
1171 }
40257
9ab7a7736ca0 Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents: 40225
diff changeset
1172 set_key_error(key);
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36511
diff changeset
1173 return NULL;
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 36511
diff changeset
1174 }
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1175 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1176 Py_INCREF(v);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1177 return v;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1178 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1179
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1180 static int
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1181 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
1182 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1183 if (w == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1184 return PyDict_DelItem((PyObject *)mp, v);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1185 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1186 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
1187 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1188
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1189 static PyMappingMethods dict_as_mapping = {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1190 (lenfunc)dict_length, /*mp_length*/
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1191 (binaryfunc)dict_subscript, /*mp_subscript*/
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1192 (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
1193 };
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1194
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1195 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1196 dict_keys(register PyDictObject *mp)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1197 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1198 register PyObject *v;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1199 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
1200 PyDictEntry *ep;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1201 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
1202
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1203 again:
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1204 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
1205 v = PyList_New(n);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1206 if (v == NULL)
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1207 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
1208 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
1209 /* 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
1210 * 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
1211 */
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1212 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
1213 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
1214 }
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1215 ep = mp->ma_table;
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1216 mask = mp->ma_mask;
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1217 for (i = 0, j = 0; i <= mask; i++) {
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1218 if (ep[i].me_value != NULL) {
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1219 PyObject *key = ep[i].me_key;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1220 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
1221 PyList_SET_ITEM(v, j, key);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1222 j++;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1223 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1224 }
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1225 assert(j == n);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1226 return v;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1227 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1228
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1229 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1230 dict_values(register PyDictObject *mp)
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1231 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1232 register PyObject *v;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1233 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
1234 PyDictEntry *ep;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1235 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
1236
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1237 again:
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1238 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
1239 v = PyList_New(n);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1240 if (v == NULL)
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1241 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
1242 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
1243 /* 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
1244 * 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
1245 */
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1246 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
1247 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
1248 }
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1249 ep = mp->ma_table;
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1250 mask = mp->ma_mask;
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1251 for (i = 0, j = 0; i <= mask; i++) {
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1252 if (ep[i].me_value != NULL) {
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1253 PyObject *value = ep[i].me_value;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1254 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
1255 PyList_SET_ITEM(v, j, value);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1256 j++;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1257 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1258 }
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1259 assert(j == n);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1260 return v;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1261 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1262
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1263 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1264 dict_items(register PyDictObject *mp)
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1265 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1266 register PyObject *v;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1267 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
1268 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
1269 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
1270 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
1271
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1272 /* 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
1273 * 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
1274 * 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
1275 */
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1276 again:
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1277 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
1278 v = PyList_New(n);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1279 if (v == NULL)
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1280 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
1281 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
1282 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
1283 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
1284 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
1285 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
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 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
1288 }
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1289 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
1290 /* 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
1291 * 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
1292 */
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1293 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
1294 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
1295 }
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1296 /* Nothing we do below makes any function calls. */
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1297 ep = mp->ma_table;
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1298 mask = mp->ma_mask;
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1299 for (i = 0, j = 0; i <= mask; i++) {
31689
0fb5066fc2a7 Factor out a double lookup.
Raymond Hettinger <python@rcn.com>
parents: 31683
diff changeset
1300 if ((value=ep[i].me_value) != NULL) {
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
1301 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
1302 item = PyList_GET_ITEM(v, j);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1303 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
1304 PyTuple_SET_ITEM(item, 0, key);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1305 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
1306 PyTuple_SET_ITEM(item, 1, value);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1307 j++;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1308 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1309 }
17535
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1310 assert(j == n);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1311 return v;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1312 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1313
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
1314 static PyObject *
26583
8991f78817c7 SF 548651: Fix the METH_CLASS implementation.
Tim Peters <tim.peters@gmail.com>
parents: 26562
diff changeset
1315 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
1316 {
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1317 PyObject *seq;
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1318 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
1319 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
1320 PyObject *key;
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1321 PyObject *d;
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1322 int status;
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1323
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26599
diff changeset
1324 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
1325 return NULL;
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1326
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1327 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
1328 if (d == NULL)
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1329 return NULL;
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1330
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
1331 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
1332 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
1333 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
1334 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
1335 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
1336 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
1337
47847
1dd10b04db47 Issue 3230: Do not the set specific size macro.
Raymond Hettinger <python@rcn.com>
parents: 47829
diff changeset
1338 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
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 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
1342 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
1343 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
1344 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
1345 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
1346 }
9f17a9a5b5ae Optimize dict.fromkeys() with dict inputs. Useful for resetting bag/muliset counts for example.
Raymond Hettinger <python@rcn.com>
parents: 43515
diff changeset
1347 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
1348 }
9f17a9a5b5ae Optimize dict.fromkeys() with dict inputs. Useful for resetting bag/muliset counts for example.
Raymond Hettinger <python@rcn.com>
parents: 43515
diff changeset
1349
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
1350 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
1351 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
1352 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
1353 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
1354 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
1355
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
1356 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
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 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
1360 Py_INCREF(key);
41270
15a7e0b7e425 Add test and fix for fromkeys() optional argument.
Raymond Hettinger <python@rcn.com>
parents: 41248
diff changeset
1361 Py_INCREF(value);
15a7e0b7e425 Add test and fix for fromkeys() optional argument.
Raymond Hettinger <python@rcn.com>
parents: 41248
diff changeset
1362 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
1363 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
1364 }
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
1365 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
1366 }
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
1367
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1368 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
1369 if (it == NULL){
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1370 Py_DECREF(d);
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1371 return NULL;
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1372 }
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1373
43810
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1374 if (PyDict_CheckExact(d)) {
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1375 while ((key = PyIter_Next(it)) != NULL) {
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1376 status = PyDict_SetItem(d, key, value);
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1377 Py_DECREF(key);
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1378 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
1379 goto Fail;
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1380 }
43810
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1381 } else {
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1382 while ((key = PyIter_Next(it)) != NULL) {
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1383 status = PyObject_SetItem(d, key, value);
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1384 Py_DECREF(key);
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1385 if (status < 0)
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1386 goto Fail;
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1387 }
26437
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
43810
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1390 if (PyErr_Occurred())
ac12ac88aeea Optimize common case for dict.fromkeys().
Raymond Hettinger <python@rcn.com>
parents: 43783
diff changeset
1391 goto Fail;
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1392 Py_DECREF(it);
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1393 return d;
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1394
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1395 Fail:
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1396 Py_DECREF(it);
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1397 Py_DECREF(d);
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1398 return NULL;
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1399 }
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1400
31616
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1401 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
1402 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
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 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
1405 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
1406
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1407 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
1408 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
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 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
1411 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
1412 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
1413 else
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1414 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
1415 }
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1416 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
1417 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
1418 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
1419 }
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1420
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1421 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
1422 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
1423 {
31908
058821d8aefd * Add unittests for iterators that report their length
Raymond Hettinger <python@rcn.com>
parents: 31702
diff changeset
1424 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
1425 Py_RETURN_NONE;
058821d8aefd * Add unittests for iterators that report their length
Raymond Hettinger <python@rcn.com>
parents: 31702
diff changeset
1426 return NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1427 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1428
19150
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1429 /* Update unconditionally replaces existing items.
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1430 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
1431 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
1432
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1433 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
1434
21348
12310d643d8d SF bug #491415 PyDict_UpdateFromSeq2() unused
Tim Peters <tim.peters@gmail.com>
parents: 21155
diff changeset
1435 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
1436 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
1437 */
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1438
21348
12310d643d8d SF bug #491415 PyDict_UpdateFromSeq2() unused
Tim Peters <tim.peters@gmail.com>
parents: 21155
diff changeset
1439 int
20683
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1440 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
1441 {
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1442 PyObject *it; /* iter(seq2) */
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1443 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
1444 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
1445 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
1446
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1447 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
1448 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
1449 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
1450
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1451 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
1452 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
1453 return -1;
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1454
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1455 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
1456 PyObject *key, *value;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1457 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
1458
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1459 fast = NULL;
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1460 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
1461 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
1462 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
1463 goto Fail;
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1464 break;
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1465 }
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1466
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1467 /* 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
1468 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
1469 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
1470 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
1471 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
1472 "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
1473 "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
1474 i);
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1475 goto Fail;
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1476 }
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1477 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
1478 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
1479 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
1480 "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
1481 "has length %zd; 2 is required",
36511
1596ff5c79b5 Revert 42400.
Martin v. Löwis <martin@v.loewis.de>
parents: 36510
diff changeset
1482 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
1483 goto Fail;
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1484 }
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1485
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1486 /* 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
1487 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
1488 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
1489 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
1490 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
1491 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
1492 goto Fail;
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1493 }
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1494 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
1495 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
1496 }
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1497
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1498 i = 0;
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1499 goto Return;
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1500 Fail:
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1501 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
1502 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
1503 i = -1;
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1504 Return:
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1505 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
1506 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
1507 }
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1508
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1509 int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1510 PyDict_Update(PyObject *a, PyObject *b)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1511 {
19150
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1512 return PyDict_Merge(a, b, 1);
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1513 }
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1514
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1515 int
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1516 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
1517 {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1518 register PyDictObject *mp, *other;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1519 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
1520 PyDictEntry *entry;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1521
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1522 /* 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
1523 * 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
1524 * 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
1525 * 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
1526 */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1527 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
1528 PyErr_BadInternalCall();
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1529 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1530 }
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1531 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
1532 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
1533 other = (PyDictObject*)b;
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1534 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
1535 /* 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
1536 return 0;
34955
9339c99a2982 SF patch #1200051: Small optimization for PyDict_Merge()
Raymond Hettinger <python@rcn.com>
parents: 34909
diff changeset
1537 if (mp->ma_used == 0)
9339c99a2982 SF patch #1200051: Small optimization for PyDict_Merge()
Raymond Hettinger <python@rcn.com>
parents: 34909
diff changeset
1538 /* 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
1539 * 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
1540 * skips the unnecessary test.
9339c99a2982 SF patch #1200051: Small optimization for PyDict_Merge()
Raymond Hettinger <python@rcn.com>
parents: 34909
diff changeset
1541 */
9339c99a2982 SF patch #1200051: Small optimization for PyDict_Merge()
Raymond Hettinger <python@rcn.com>
parents: 34909
diff changeset
1542 override = 1;
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1543 /* 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
1544 * 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
1545 * 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
1546 */
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1547 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
1548 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
1549 return -1;
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1550 }
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1551 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
1552 entry = &other->ma_table[i];
19150
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1553 if (entry->me_value != NULL &&
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1554 (override ||
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1555 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
1556 Py_INCREF(entry->me_key);
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1557 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
1558 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
1559 (long)entry->me_hash,
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1560 entry->me_value) != 0)
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1561 return -1;
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1562 }
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1563 }
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1564 }
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1565 else {
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1566 /* 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
1567 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
1568 PyObject *iter;
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1569 PyObject *key, *value;
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1570 int status;
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1571
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1572 if (keys == NULL)
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1573 /* 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
1574 * 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
1575 * 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
1576 * 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
1577 */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1578 return -1;
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1579
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1580 iter = PyObject_GetIter(keys);
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1581 Py_DECREF(keys);
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1582 if (iter == NULL)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1583 return -1;
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1584
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1585 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
1586 if (!override && PyDict_GetItem(a, key) != NULL) {
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1587 Py_DECREF(key);
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1588 continue;
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1589 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1590 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
1591 if (value == NULL) {
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1592 Py_DECREF(iter);
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1593 Py_DECREF(key);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1594 return -1;
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1595 }
19150
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1596 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
1597 Py_DECREF(key);
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1598 Py_DECREF(value);
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1599 if (status < 0) {
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1600 Py_DECREF(iter);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1601 return -1;
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1602 }
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1603 }
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1604 Py_DECREF(iter);
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1605 if (PyErr_Occurred())
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1606 /* Iterator completed, via error */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1607 return -1;
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
1608 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1609 return 0;
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
1610 }
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
1611
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
1612 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1613 dict_copy(register PyDictObject *mp)
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
1614 {
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
1615 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
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
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 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
1619 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
1620 {
31620
47890c395eba Factor out code common to PyDict_Copy() and PyDict_Merge().
Raymond Hettinger <python@rcn.com>
parents: 31616
diff changeset
1621 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
1622
e6214ad2a60c Add PyDict_Copy() function to C API for dicts. It returns a new
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
1623 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
1624 PyErr_BadInternalCall();
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
1625 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
1626 }
31620
47890c395eba Factor out code common to PyDict_Copy() and PyDict_Merge().
Raymond Hettinger <python@rcn.com>
parents: 31616
diff changeset
1627 copy = PyDict_New();
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
1628 if (copy == NULL)
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
1629 return NULL;
31620
47890c395eba Factor out code common to PyDict_Copy() and PyDict_Merge().
Raymond Hettinger <python@rcn.com>
parents: 31616
diff changeset
1630 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
1631 return copy;
47890c395eba Factor out code common to PyDict_Copy() and PyDict_Merge().
Raymond Hettinger <python@rcn.com>
parents: 31616
diff changeset
1632 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
1633 return NULL;
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
1634 }
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
1635
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1636 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
1637 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
1638 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1639 if (mp == NULL || !PyDict_Check(mp)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1640 PyErr_BadInternalCall();
31545
391011840a03 Oops. Return -1 to distinguish error from empty dict.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 31174
diff changeset
1641 return -1;
1398
1d841587aede Added getmappingsize(). (Needed by previous checkin of posixmodule.c)
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
1642 }
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1643 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
1644 }
1d841587aede Added getmappingsize(). (Needed by previous checkin of posixmodule.c)
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
1645
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1646 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
1647 PyDict_Keys(PyObject *mp)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1648 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1649 if (mp == NULL || !PyDict_Check(mp)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1650 PyErr_BadInternalCall();
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1651 return NULL;
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1652 }
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1653 return dict_keys((PyDictObject *)mp);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1654 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1655
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1656 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
1657 PyDict_Values(PyObject *mp)
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1658 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1659 if (mp == NULL || !PyDict_Check(mp)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1660 PyErr_BadInternalCall();
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1661 return NULL;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1662 }
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1663 return dict_values((PyDictObject *)mp);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1664 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1665
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1666 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
1667 PyDict_Items(PyObject *mp)
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1668 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1669 if (mp == NULL || !PyDict_Check(mp)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1670 PyErr_BadInternalCall();
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1671 return NULL;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1672 }
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1673 return dict_items((PyDictObject *)mp);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1674 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1675
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1676 /* 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
1677 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
1678 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
1679 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
1680 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
1681 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
1682 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
1683
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1684 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1685 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
1686 {
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1687 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
1688 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
1689 Py_ssize_t i;
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1690 int cmp;
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1691
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
1692 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
1693 PyObject *thiskey, *thisaval, *thisbval;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1694 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
1695 continue;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1696 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
1697 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
1698 if (akey != NULL) {
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1699 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
1700 if (cmp < 0) {
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1701 Py_DECREF(thiskey);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1702 goto Fail;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1703 }
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1704 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
1705 i > a->ma_mask ||
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1706 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
1707 {
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1708 /* 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
1709 * 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
1710 * find its associated value anymore; or
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1711 * 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
1712 * 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
1713 */
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1714 Py_DECREF(thiskey);
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1715 continue;
15974
5b9103e62fac Rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15708
diff changeset
1716 }
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1717 }
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1718
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1719 /* 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
1720 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
1721 assert(thisaval);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1722 Py_INCREF(thisaval); /* keep alive */
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1723 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1724 if (thisbval == NULL)
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1725 cmp = 0;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1726 else {
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1727 /* both dicts have thiskey: same values? */
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1728 cmp = PyObject_RichCompareBool(
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1729 thisaval, thisbval, Py_EQ);
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 Py_DECREF(thiskey);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1732 Py_DECREF(thisaval);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1733 goto Fail;
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1734 }
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1735 }
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1736 if (cmp == 0) {
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1737 /* New winner. */
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1738 Py_XDECREF(akey);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1739 Py_XDECREF(aval);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1740 akey = thiskey;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1741 aval = thisaval;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1742 }
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1743 else {
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1744 Py_DECREF(thiskey);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1745 Py_DECREF(thisaval);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1746 }
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1747 }
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1748 *pval = aval;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1749 return akey;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1750
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1751 Fail:
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1752 Py_XDECREF(akey);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1753 Py_XDECREF(aval);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1754 *pval = NULL;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1755 return NULL;
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1756 }
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 static int
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1759 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
1760 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1761 PyObject *adiff, *bdiff, *aval, *bval;
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1762 int res;
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1763
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1764 /* Compare lengths first */
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1765 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
1766 return -1; /* a is shorter */
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1767 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
1768 return 1; /* b is shorter */
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1769
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1770 /* Same length -- check all keys */
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1771 bdiff = bval = NULL;
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1772 adiff = characterize(a, b, &aval);
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1773 if (adiff == NULL) {
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1774 assert(!aval);
17771
fb103e3da2ae Repair typo in comment.
Tim Peters <tim.peters@gmail.com>
parents: 17756
diff changeset
1775 /* 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
1776 * must be equal.
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1777 */
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1778 res = PyErr_Occurred() ? -1 : 0;
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 }
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1781 bdiff = characterize(b, a, &bval);
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1782 if (bdiff == NULL && PyErr_Occurred()) {
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1783 assert(!bval);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1784 res = -1;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1785 goto Finished;
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 = 0;
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1788 if (bdiff) {
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1789 /* 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
1790 * 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
1791 * 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
1792 */
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1793 res = PyObject_Compare(adiff, bdiff);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1794 }
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1795 if (res == 0 && bval != NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1796 res = PyObject_Compare(aval, bval);
17756
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1797
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1798 Finished:
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1799 Py_XDECREF(adiff);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1800 Py_XDECREF(bdiff);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1801 Py_XDECREF(aval);
a06a704260e9 SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents: 17704
diff changeset
1802 Py_XDECREF(bval);
4586
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1803 return res;
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1804 }
b3d2990d8a4a Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1805
17704
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1806 /* 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
1807 * 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
1808 * Uses only Py_EQ comparison.
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1809 */
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1810 static int
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1811 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
1812 {
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
1813 Py_ssize_t i;
17704
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1814
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1815 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
1816 /* 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
1817 return 0;
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
1818
17704
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1819 /* 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
1820 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
1821 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
1822 if (aval != NULL) {
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1823 int cmp;
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1824 PyObject *bval;
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1825 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
1826 /* 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
1827 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
1828 Py_INCREF(aval);
39745
08ca476f8a34 Fix SF bug #1546288, crash in dict_equal.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39376
diff changeset
1829 /* ditto for key */
08ca476f8a34 Fix SF bug #1546288, crash in dict_equal.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39376
diff changeset
1830 Py_INCREF(key);
17704
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1831 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
1832 Py_DECREF(key);
17704
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1833 if (bval == NULL) {
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1834 Py_DECREF(aval);
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1835 return 0;
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 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
1838 Py_DECREF(aval);
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1839 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
1840 return cmp;
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1841 }
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 return 1;
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1844 }
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 static PyObject *
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1847 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
1848 {
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1849 int cmp;
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1850 PyObject *res;
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1851
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1852 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
1853 res = Py_NotImplemented;
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1854 }
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1855 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
1856 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
1857 if (cmp < 0)
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1858 return NULL;
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1859 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
1860 }
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
1861 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
1862 /* Py3K warning if comparison isn't == or != */
46573
aef3501d8e77 Use PyErr_WarnPy3k throughout
Benjamin Peterson <benjamin@python.org>
parents: 46076
diff changeset
1863 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
1864 "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
1865 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
1866 }
17775
dce1f53451a5 Restore dicts' tp_compare slot, and change dict_richcompare to say it
Tim Peters <tim.peters@gmail.com>
parents: 17771
diff changeset
1867 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
1868 }
17704
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1869 Py_INCREF(res);
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1870 return res;
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1871 }
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
1872
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1873 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1874 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
1875 {
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1876 long hash;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1877 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
1878
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1879 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
1880 (hash = ((PyStringObject *) key)->ob_shash) == -1) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1881 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
1882 if (hash == -1)
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
1883 return NULL;
a820dead06e6 Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
1884 }
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1885 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
1886 if (ep == NULL)
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1887 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
1888 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
1889 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1890
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1891 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1892 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
1893 {
46573
aef3501d8e77 Use PyErr_WarnPy3k throughout
Benjamin Peterson <benjamin@python.org>
parents: 46076
diff changeset
1894 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
1895 "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
1896 return NULL;
a0ee3b2ba361 Add -3 option to the interpreter to warn about features that are
Neal Norwitz <nnorwitz@gmail.com>
parents: 41270
diff changeset
1897 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
1898 }
a0ee3b2ba361 Add -3 option to the interpreter to warn about features that are
Neal Norwitz <nnorwitz@gmail.com>
parents: 41270
diff changeset
1899
a0ee3b2ba361 Add -3 option to the interpreter to warn about features that are
Neal Norwitz <nnorwitz@gmail.com>
parents: 41270
diff changeset
1900 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1901 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
1902 {
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1903 PyObject *key;
6488
37c8d7f967c7 dict_get(): Fixed a couple of stupid mistakes which caused crashes.
Barry Warsaw <barry@python.org>
parents: 6395
diff changeset
1904 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
1905 PyObject *val = NULL;
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1906 long hash;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1907 PyDictEntry *ep;
6395
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1908
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26599
diff changeset
1909 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
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
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1912 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
1913 (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
1914 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
1915 if (hash == -1)
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1916 return NULL;
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1917 }
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1918 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
1919 if (ep == NULL)
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1920 return NULL;
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1921 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
1922 if (val == NULL)
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1923 val = failobj;
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1924 Py_INCREF(val);
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1925 return val;
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1926 }
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1927
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1928
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
1929 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1930 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
1931 {
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1932 PyObject *key;
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1933 PyObject *failobj = Py_None;
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1934 PyObject *val = NULL;
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1935 long hash;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1936 PyDictEntry *ep;
13892
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1937
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26599
diff changeset
1938 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
1939 return NULL;
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1940
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1941 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
1942 (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
1943 hash = PyObject_Hash(key);
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1944 if (hash == -1)
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1945 return NULL;
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1946 }
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1947 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
1948 if (ep == NULL)
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1949 return NULL;
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1950 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
1951 if (val == NULL) {
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1952 val = failobj;
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1953 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
1954 val = NULL;
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1955 }
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1956 Py_XINCREF(val);
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1957 return val;
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1958 }
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1959
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1960
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
1961 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1962 dict_clear(register PyDictObject *mp)
5209
2ec9c933afb8 Add clear() method to dictionary objects.
Guido van Rossum <guido@python.org>
parents: 5059
diff changeset
1963 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1964 PyDict_Clear((PyObject *)mp);
31908
058821d8aefd * Add unittests for iterators that report their length
Raymond Hettinger <python@rcn.com>
parents: 31702
diff changeset
1965 Py_RETURN_NONE;
5209
2ec9c933afb8 Add clear() method to dictionary objects.
Guido van Rossum <guido@python.org>
parents: 5059
diff changeset
1966 }
2ec9c933afb8 Add clear() method to dictionary objects.
Guido van Rossum <guido@python.org>
parents: 5059
diff changeset
1967
15520
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
1968 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1969 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
1970 {
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1971 long hash;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
1972 PyDictEntry *ep;
22994
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1973 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
1974 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
1975
28211
92e3b611dc9f SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents: 27902
diff changeset
1976 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
1977 return NULL;
22994
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1978 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
1979 if (deflt) {
92e3b611dc9f SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents: 27902
diff changeset
1980 Py_INCREF(deflt);
92e3b611dc9f SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents: 27902
diff changeset
1981 return deflt;
92e3b611dc9f SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents: 27902
diff changeset
1982 }
22994
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1983 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
1984 "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
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 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1987 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
1988 (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
1989 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
1990 if (hash == -1)
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1991 return NULL;
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1992 }
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1993 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
1994 if (ep == NULL)
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
1995 return NULL;
22994
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
1996 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
1997 if (deflt) {
92e3b611dc9f SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents: 27902
diff changeset
1998 Py_INCREF(deflt);
92e3b611dc9f SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents: 27902
diff changeset
1999 return deflt;
92e3b611dc9f SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents: 27902
diff changeset
2000 }
40257
9ab7a7736ca0 Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents: 40225
diff changeset
2001 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
2002 return NULL;
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
2003 }
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
2004 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
2005 Py_INCREF(dummy);
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
2006 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
2007 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
2008 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
2009 mp->ma_used--;
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
2010 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
2011 return old_value;
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
2012 }
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
2013
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
2014 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2015 dict_popitem(PyDictObject *mp)
15520
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2016 {
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
2017 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
2018 PyDictEntry *ep;
15520
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2019 PyObject *res;
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2020
18015
0c1a04e98134 dict_popitem(): Repaired last-second 2.1 comment, which misidentified the
Tim Peters <tim.peters@gmail.com>
parents: 18014
diff changeset
2021 /* 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
2022 * 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
2023 * 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
2024 * 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
2025 * 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
2026 * 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
2027 * 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
2028 * 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
2029 */
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
2030 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
2031 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
2032 return NULL;
17536
d2da681ccb54 Tim pointed out a remaining vulnerability in popitem(): the
Guido van Rossum <guido@python.org>
parents: 17535
diff changeset
2033 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
2034 Py_DECREF(res);
d2da681ccb54 Tim pointed out a remaining vulnerability in popitem(): the
Guido van Rossum <guido@python.org>
parents: 17535
diff changeset
2035 PyErr_SetString(PyExc_KeyError,
d2da681ccb54 Tim pointed out a remaining vulnerability in popitem(): the
Guido van Rossum <guido@python.org>
parents: 17535
diff changeset
2036 "popitem(): dictionary is empty");
d2da681ccb54 Tim pointed out a remaining vulnerability in popitem(): the
Guido van Rossum <guido@python.org>
parents: 17535
diff changeset
2037 return NULL;
d2da681ccb54 Tim pointed out a remaining vulnerability in popitem(): the
Guido van Rossum <guido@python.org>
parents: 17535
diff changeset
2038 }
15520
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2039 /* 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
2040 * 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
2041 * 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
2042 * 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
2043 * 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
2044 */
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2045 ep = &mp->ma_table[0];
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2046 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
2047 i = ep->me_hash;
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
2048 /* 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
2049 * 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
2050 * 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
2051 * 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
2052 */
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
2053 if (i > mp->ma_mask || i < 1)
15520
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2054 i = 1; /* skip slot 0 */
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2055 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
2056 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
2057 if (i > mp->ma_mask)
15520
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2058 i = 1;
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2059 }
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2060 }
17535
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
2061 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
2062 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
2063 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
2064 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
2065 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
2066 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
2067 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
2068 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
2069 return res;
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2070 }
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2071
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2072 static int
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2073 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
2074 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2075 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
2076 PyObject *pk;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2077 PyObject *pv;
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 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
2080 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
2081 Py_VISIT(pv);
12990
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 return 0;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2084 }
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 static int
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2087 dict_tp_clear(PyObject *op)
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2088 {
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2089 PyDict_Clear(op);
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2090 return 0;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2091 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2092
15560
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2093
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2094 extern PyTypeObject PyDictIterKey_Type; /* Forward */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2095 extern PyTypeObject PyDictIterValue_Type; /* Forward */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2096 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
2097 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
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_iterkeys(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, &PyDictIterKey_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_itervalues(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, &PyDictIterValue_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
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2111 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2112 dict_iteritems(PyDictObject *dict)
17644
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2113 {
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2114 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
2115 }
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2116
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47294
diff changeset
2117 static PyObject *
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47294
diff changeset
2118 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
2119 {
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47294
diff changeset
2120 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
2121
47829
7eb3737657d1 Corrected inconsistencies in sizeof tests and addressed issue pointed
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47527
diff changeset
2122 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
2123 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
2124 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
2125 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
2126 }
17644
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2127
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2128 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
2129 "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
2130
31172
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
2131 PyDoc_STRVAR(contains__doc__,
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
2132 "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
2133
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
2134 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
2135
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47294
diff changeset
2136 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
2137 "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
2138
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2139 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
2140 "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
2141
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2142 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
2143 "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
2144
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2145 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
2146 "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
2147 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
2148
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2149 PyDoc_STRVAR(popitem__doc__,
15560
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2150 "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
2151 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
2152
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2153 PyDoc_STRVAR(keys__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2154 "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
2155
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2156 PyDoc_STRVAR(items__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2157 "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
2158
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2159 PyDoc_STRVAR(values__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2160 "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
2161
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2162 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
2163 "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
2164 "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
2165 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
2166 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
2167
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
2168 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
2169 "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
2170 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
2171
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2172 PyDoc_STRVAR(clear__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2173 "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
2174
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2175 PyDoc_STRVAR(copy__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2176 "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
2177
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2178 PyDoc_STRVAR(iterkeys__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2179 "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
2180
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2181 PyDoc_STRVAR(itervalues__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2182 "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
2183
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2184 PyDoc_STRVAR(iteritems__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2185 "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
2186
58256
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2187 /* Forward */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2188 static PyObject *dictkeys_new(PyObject *);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2189 static PyObject *dictitems_new(PyObject *);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2190 static PyObject *dictvalues_new(PyObject *);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2191
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2192 PyDoc_STRVAR(viewkeys__doc__,
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2193 "D.viewkeys() -> a set-like object providing a view on D's keys");
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2194 PyDoc_STRVAR(viewitems__doc__,
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2195 "D.viewitems() -> a set-like object providing a view on D's items");
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2196 PyDoc_STRVAR(viewvalues__doc__,
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2197 "D.viewvalues() -> an object providing a view on D's values");
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2198
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
2199 static PyMethodDef mapp_methods[] = {
41833
8c3578934df6 Whitespace cleanup
Neal Norwitz <nnorwitz@gmail.com>
parents: 41832
diff changeset
2200 {"__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
2201 contains__doc__},
31174
88e80a04328a Simplify previous checkin -- a new function was not needed.
Raymond Hettinger <python@rcn.com>
parents: 31172
diff changeset
2202 {"__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
2203 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
2204 {"__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
2205 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
2206 {"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
2207 has_key__doc__},
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2208 {"get", (PyCFunction)dict_get, METH_VARARGS,
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2209 get__doc__},
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2210 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2211 setdefault_doc__},
28211
92e3b611dc9f SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents: 27902
diff changeset
2212 {"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
2213 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
2214 {"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
2215 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
2216 {"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
2217 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
2218 {"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
2219 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
2220 {"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
2221 values__doc__},
58256
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2222 {"viewkeys", (PyCFunction)dictkeys_new, METH_NOARGS,
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2223 viewkeys__doc__},
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2224 {"viewitems", (PyCFunction)dictitems_new, METH_NOARGS,
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2225 viewitems__doc__},
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2226 {"viewvalues", (PyCFunction)dictvalues_new, METH_NOARGS,
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2227 viewvalues__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
2228 {"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
2229 update__doc__},
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
2230 {"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
2231 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
2232 {"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
2233 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
2234 {"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
2235 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
2236 {"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
2237 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
2238 {"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
2239 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
2240 {"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
2241 iteritems__doc__},
15560
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2242 {NULL, NULL} /* sentinel */
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2243 };
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2244
38540
d7dd528427c0 Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents: 38536
diff changeset
2245 /* 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
2246 int
b2fd7af17186 Expose dict_contains() and PyDict_Contains() with is about 10% faster
Raymond Hettinger <python@rcn.com>
parents: 30265
diff changeset
2247 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
2248 {
bf94af396b1e Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents: 17536
diff changeset
2249 long hash;
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2250 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
2251 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
2252
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
2253 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
2254 (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
2255 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
2256 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
2257 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
2258 }
38536
175cbdf9a5fb [ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents: 38504
diff changeset
2259 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
2260 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
2261 }
bf94af396b1e Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents: 17536
diff changeset
2262
40864
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
2263 /* 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
2264 int
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
2265 _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
2266 {
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2267 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
2268 PyDictEntry *ep;
40864
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
2269
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
2270 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
2271 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
2272 }
b0f8e9b0cc19 Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents: 40756
diff changeset
2273
17594
bf94af396b1e Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents: 17536
diff changeset
2274 /* 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
2275 static PySequenceMethods dict_as_sequence = {
37304
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2276 0, /* sq_length */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2277 0, /* sq_concat */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2278 0, /* sq_repeat */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2279 0, /* sq_item */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2280 0, /* sq_slice */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2281 0, /* sq_ass_item */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2282 0, /* sq_ass_slice */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2283 PyDict_Contains, /* sq_contains */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2284 0, /* sq_inplace_concat */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2285 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
2286 };
bf94af396b1e Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents: 17536
diff changeset
2287
17644
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2288 static PyObject *
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2289 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
2290 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2291 PyObject *self;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2292
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2293 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
2294 self = type->tp_alloc(type, 0);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2295 if (self != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2296 PyDictObject *d = (PyDictObject *)self;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2297 /* 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
2298 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
2299 INIT_NONZERO_DICT_SLOTS(d);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2300 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
2301 /* 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
2302 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
2303 _PyObject_GC_UNTRACK(d);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2304 #ifdef SHOW_CONVERSION_COUNTS
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2305 ++created;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2306 #endif
52288
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
2307 #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
2308 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
2309 count_tracked++;
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
2310 else
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
2311 count_untracked++;
0e498a9fde3c Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
2312 #endif
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2313 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2314 return self;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2315 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2316
19658
2fa615dfc464 Make dictionary() a real constructor. Accepts at most one argument, "a
Tim Peters <tim.peters@gmail.com>
parents: 19581
diff changeset
2317 static int
2fa615dfc464 Make dictionary() a real constructor. Accepts at most one argument, "a
Tim Peters <tim.peters@gmail.com>
parents: 19581
diff changeset
2318 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
2319 {
31616
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
2320 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
2321 }
2fa615dfc464 Make dictionary() a real constructor. Accepts at most one argument, "a
Tim Peters <tim.peters@gmail.com>
parents: 19581
diff changeset
2322
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2323 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2324 dict_iter(PyDictObject *dict)
17644
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2325 {
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2326 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
2327 }
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2328
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2329 PyDoc_STRVAR(dictionary_doc,
20754
272336648d1f Rename "dictionary" (type and constructor) to "dict".
Tim Peters <tim.peters@gmail.com>
parents: 20724
diff changeset
2330 "dict() -> new empty dictionary.\n"
272336648d1f Rename "dictionary" (type and constructor) to "dict".
Tim Peters <tim.peters@gmail.com>
parents: 20724
diff changeset
2331 "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
2332 " (key, value) pairs.\n"
20754
272336648d1f Rename "dictionary" (type and constructor) to "dict".
Tim Peters <tim.peters@gmail.com>
parents: 20724
diff changeset
2333 "dict(seq) -> new dictionary initialized as if via:\n"
20724
f224632d0260 dictionary() constructor:
Tim Peters <tim.peters@gmail.com>
parents: 20683
diff changeset
2334 " d = {}\n"
f224632d0260 dictionary() constructor:
Tim Peters <tim.peters@gmail.com>
parents: 20683
diff changeset
2335 " 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
2336 " d[k] = v\n"
08b720cd387e Patch #642500 with slight modifications: allow keyword arguments in
Just van Rossum <just@letterror.com>
parents: 25115
diff changeset
2337 "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
2338 " 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
2339
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
2340 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
2341 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
2342 "dict",
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2343 sizeof(PyDictObject),
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2344 0,
15974
5b9103e62fac Rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15708
diff changeset
2345 (destructor)dict_dealloc, /* tp_dealloc */
5b9103e62fac Rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15708
diff changeset
2346 (printfunc)dict_print, /* tp_print */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2347 0, /* tp_getattr */
15974
5b9103e62fac Rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15708
diff changeset
2348 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
2349 (cmpfunc)dict_compare, /* tp_compare */
15974
5b9103e62fac Rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15708
diff changeset
2350 (reprfunc)dict_repr, /* tp_repr */
5b9103e62fac Rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15708
diff changeset
2351 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
2352 &dict_as_sequence, /* tp_as_sequence */
15974
5b9103e62fac Rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15708
diff changeset
2353 &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
2354 (hashfunc)PyObject_HashNotImplemented, /* tp_hash */
15974
5b9103e62fac Rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15708
diff changeset
2355 0, /* tp_call */
5b9103e62fac Rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15708
diff changeset
2356 0, /* tp_str */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2357 PyObject_GenericGetAttr, /* tp_getattro */
15974
5b9103e62fac Rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15708
diff changeset
2358 0, /* tp_setattro */
5b9103e62fac Rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15708
diff changeset
2359 0, /* tp_as_buffer */
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
2360 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
2361 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
2362 dictionary_doc, /* tp_doc */
37304
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2363 dict_traverse, /* tp_traverse */
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2364 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
2365 dict_richcompare, /* tp_richcompare */
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2366 0, /* tp_weaklistoffset */
17644
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2367 (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
2368 0, /* tp_iternext */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2369 mapp_methods, /* tp_methods */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2370 0, /* tp_members */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2371 0, /* tp_getset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2372 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2373 0, /* tp_dict */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2374 0, /* tp_descr_get */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2375 0, /* tp_descr_set */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2376 0, /* tp_dictoffset */
37304
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 36657
diff changeset
2377 dict_init, /* tp_init */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2378 PyType_GenericAlloc, /* tp_alloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2379 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
2380 PyObject_GC_Del, /* tp_free */
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2381 };
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2382
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
2383 /* 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
2384
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
2385 PyObject *
26599
ecea8410178e Constify char* API. Fixes #651363. 2.2 candidate.
Martin v. Löwis <martin@v.loewis.de>
parents: 26583
diff changeset
2386 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
2387 {
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
2388 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
2389 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
2390 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
2391 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
2392 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
2393 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
2394 return rv;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2395 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2396
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2397 int
26599
ecea8410178e Constify char* API. Fixes #651363. 2.2 candidate.
Martin v. Löwis <martin@v.loewis.de>
parents: 26583
diff changeset
2398 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
2399 {
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
2400 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
2401 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
2402 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
2403 if (kv == NULL)
5561
4dcf26fe4da5 Moved PyObject_{Get,Set}Attr to object.c.
Guido van Rossum <guido@python.org>
parents: 5540
diff changeset
2404 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
2405 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
2406 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
2407 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
2408 return err;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2409 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2410
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2411 int
26599
ecea8410178e Constify char* API. Fixes #651363. 2.2 candidate.
Martin v. Löwis <martin@v.loewis.de>
parents: 26583
diff changeset
2412 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
2413 {
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
2414 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
2415 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
2416 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
2417 if (kv == NULL)
5561
4dcf26fe4da5 Moved PyObject_{Get,Set}Attr to object.c.
Guido van Rossum <guido@python.org>
parents: 5540
diff changeset
2418 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
2419 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
2420 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
2421 return err;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2422 }
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2423
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2424 /* Dictionary iterator types */
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2425
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2426 typedef struct {
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2427 PyObject_HEAD
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2428 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
2429 Py_ssize_t di_used;
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
2430 Py_ssize_t di_pos;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2431 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
2432 Py_ssize_t len;
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2433 } dictiterobject;
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 static PyObject *
43515
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2436 dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2437 {
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2438 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
2439 di = PyObject_GC_New(dictiterobject, itertype);
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2440 if (di == NULL)
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2441 return NULL;
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2442 Py_INCREF(dict);
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2443 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
2444 di->di_used = dict->ma_used;
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2445 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
2446 di->len = dict->ma_used;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2447 if (itertype == &PyDictIterItem_Type) {
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2448 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2449 if (di->di_result == NULL) {
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2450 Py_DECREF(di);
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2451 return NULL;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2452 }
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2453 }
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2454 else
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2455 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
2456 _PyObject_GC_TRACK(di);
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2457 return (PyObject *)di;
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2458 }
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2459
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2460 static void
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2461 dictiter_dealloc(dictiterobject *di)
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2462 {
24259
2f60f90ce309 Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
2463 Py_XDECREF(di->di_dict);
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2464 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
2465 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
2466 }
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
2467
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
2468 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
2469 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
2470 {
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
2471 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
2472 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
2473 return 0;
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2474 }
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2475
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2476 static PyObject *
31683
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2477 dictiter_len(dictiterobject *di)
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2478 {
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
2479 Py_ssize_t len = 0;
31908
058821d8aefd * Add unittests for iterators that report their length
Raymond Hettinger <python@rcn.com>
parents: 31702
diff changeset
2480 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
2481 len = di->len;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
2482 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
2483 }
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2484
36484
20f75d5e79e9 Renamed _length_cue() to __length_hint__(). See:
Armin Rigo <arigo@tunes.org>
parents: 36164
diff changeset
2485 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
2486
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2487 static PyMethodDef dictiter_methods[] = {
36484
20f75d5e79e9 Renamed _length_cue() to __length_hint__(). See:
Armin Rigo <arigo@tunes.org>
parents: 36164
diff changeset
2488 {"__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
2489 {NULL, NULL} /* sentinel */
31683
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2490 };
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2491
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2492 static PyObject *dictiter_iternextkey(dictiterobject *di)
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2493 {
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2494 PyObject *key;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
2495 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
2496 register PyDictEntry *ep;
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2497 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
2498
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2499 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
2500 return NULL;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2501 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
2502
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2503 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
2504 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
2505 "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
2506 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
2507 return NULL;
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
2508 }
24259
2f60f90ce309 Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
2509
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2510 i = di->di_pos;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2511 if (i < 0)
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2512 goto fail;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2513 ep = d->ma_table;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2514 mask = d->ma_mask;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2515 while (i <= mask && ep[i].me_value == NULL)
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2516 i++;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2517 di->di_pos = i+1;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2518 if (i > mask)
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2519 goto fail;
31683
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2520 di->len--;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2521 key = ep[i].me_key;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2522 Py_INCREF(key);
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2523 return key;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2524
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2525 fail:
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2526 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
2527 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
2528 return NULL;
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2529 }
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2530
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2531 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
2532 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
2533 "dictionary-keyiterator", /* tp_name */
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2534 sizeof(dictiterobject), /* tp_basicsize */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2535 0, /* tp_itemsize */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2536 /* methods */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2537 (destructor)dictiter_dealloc, /* tp_dealloc */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2538 0, /* tp_print */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2539 0, /* tp_getattr */
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2540 0, /* tp_setattr */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2541 0, /* tp_compare */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2542 0, /* tp_repr */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2543 0, /* tp_as_number */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2544 0, /* tp_as_sequence */
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2545 0, /* tp_as_mapping */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2546 0, /* tp_hash */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2547 0, /* tp_call */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2548 0, /* tp_str */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2549 PyObject_GenericGetAttr, /* tp_getattro */
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2550 0, /* tp_setattro */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2551 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
2552 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
2553 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
2554 (traverseproc)dictiter_traverse, /* tp_traverse */
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2555 0, /* tp_clear */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2556 0, /* tp_richcompare */
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2557 0, /* tp_weaklistoffset */
28320
fd5ef7003469 Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents: 28308
diff changeset
2558 PyObject_SelfIter, /* tp_iter */
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2559 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2560 dictiter_methods, /* tp_methods */
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2561 0,
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2562 };
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2563
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2564 static PyObject *dictiter_iternextvalue(dictiterobject *di)
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2565 {
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2566 PyObject *value;
38499
494cab563c11 Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents: 37756
diff changeset
2567 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
2568 register PyDictEntry *ep;
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2569 PyDictObject *d = di->di_dict;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2570
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2571 if (d == NULL)
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2572 return NULL;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2573 assert (PyDict_Check(d));
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2574
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2575 if (di->di_used != d->ma_used) {
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2576 PyErr_SetString(PyExc_RuntimeError,
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2577 "dictionary changed size during iteration");
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2578 di->di_used = -1; /* Make this state sticky */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2579 return NULL;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2580 }
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2581
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2582 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
2583 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
2584 if (i < 0 || i > mask)
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2585 goto fail;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2586 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
2587 while ((value=ep[i].me_value) == NULL) {
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2588 i++;
31702
36a2d0e8a993 GCC was complaining that 'value' in dictiter_iternextvalue() wasn't
Guido van Rossum <guido@python.org>
parents: 31689
diff changeset
2589 if (i > mask)
36a2d0e8a993 GCC was complaining that 'value' in dictiter_iternextvalue() wasn't
Guido van Rossum <guido@python.org>
parents: 31689
diff changeset
2590 goto fail;
36a2d0e8a993 GCC was complaining that 'value' in dictiter_iternextvalue() wasn't
Guido van Rossum <guido@python.org>
parents: 31689
diff changeset
2591 }
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2592 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
2593 di->len--;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2594 Py_INCREF(value);
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2595 return value;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2596
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2597 fail:
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2598 Py_DECREF(d);
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2599 di->di_dict = NULL;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2600 return NULL;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2601 }
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2602
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2603 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
2604 PyVarObject_HEAD_INIT(&PyType_Type, 0)
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2605 "dictionary-valueiterator", /* tp_name */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2606 sizeof(dictiterobject), /* tp_basicsize */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2607 0, /* tp_itemsize */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2608 /* methods */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2609 (destructor)dictiter_dealloc, /* tp_dealloc */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2610 0, /* tp_print */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2611 0, /* tp_getattr */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2612 0, /* tp_setattr */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2613 0, /* tp_compare */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2614 0, /* tp_repr */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2615 0, /* tp_as_number */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2616 0, /* tp_as_sequence */
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2617 0, /* tp_as_mapping */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2618 0, /* tp_hash */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2619 0, /* tp_call */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2620 0, /* tp_str */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2621 PyObject_GenericGetAttr, /* tp_getattro */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2622 0, /* tp_setattro */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2623 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
2624 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2625 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
2626 (traverseproc)dictiter_traverse, /* tp_traverse */
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2627 0, /* tp_clear */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2628 0, /* tp_richcompare */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2629 0, /* tp_weaklistoffset */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2630 PyObject_SelfIter, /* tp_iter */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2631 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2632 dictiter_methods, /* tp_methods */
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2633 0,
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2634 };
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2635
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2636 static PyObject *dictiter_iternextitem(dictiterobject *di)
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2637 {
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2638 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
2639 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
2640 register PyDictEntry *ep;
8c5e60ddaa82 Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents: 43378
diff changeset
2641 PyDictObject *d = di->di_dict;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2642
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2643 if (d == NULL)
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2644 return NULL;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2645 assert (PyDict_Check(d));
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2646
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2647 if (di->di_used != d->ma_used) {
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2648 PyErr_SetString(PyExc_RuntimeError,
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2649 "dictionary changed size during iteration");
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2650 di->di_used = -1; /* Make this state sticky */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2651 return NULL;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2652 }
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2653
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2654 i = di->di_pos;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2655 if (i < 0)
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2656 goto fail;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2657 ep = d->ma_table;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2658 mask = d->ma_mask;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2659 while (i <= mask && ep[i].me_value == NULL)
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2660 i++;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2661 di->di_pos = i+1;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2662 if (i > mask)
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2663 goto fail;
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 if (result->ob_refcnt == 1) {
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2666 Py_INCREF(result);
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2667 Py_DECREF(PyTuple_GET_ITEM(result, 0));
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2668 Py_DECREF(PyTuple_GET_ITEM(result, 1));
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2669 } else {
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2670 result = PyTuple_New(2);
36164
50886d42b444 Fixed English in a comment; trimmed trailing whitespace;
Tim Peters <tim.peters@gmail.com>
parents: 35636
diff changeset
2671 if (result == NULL)
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2672 return NULL;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2673 }
31683
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2674 di->len--;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2675 key = ep[i].me_key;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2676 value = ep[i].me_value;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2677 Py_INCREF(key);
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2678 Py_INCREF(value);
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2679 PyTuple_SET_ITEM(result, 0, key);
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2680 PyTuple_SET_ITEM(result, 1, value);
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2681 return result;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2682
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2683 fail:
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2684 Py_DECREF(d);
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2685 di->di_dict = NULL;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2686 return NULL;
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2687 }
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2688
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2689 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
2690 PyVarObject_HEAD_INIT(&PyType_Type, 0)
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2691 "dictionary-itemiterator", /* tp_name */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2692 sizeof(dictiterobject), /* tp_basicsize */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2693 0, /* tp_itemsize */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2694 /* methods */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2695 (destructor)dictiter_dealloc, /* tp_dealloc */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2696 0, /* tp_print */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2697 0, /* tp_getattr */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2698 0, /* tp_setattr */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2699 0, /* tp_compare */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2700 0, /* tp_repr */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2701 0, /* tp_as_number */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2702 0, /* tp_as_sequence */
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2703 0, /* tp_as_mapping */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2704 0, /* tp_hash */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2705 0, /* tp_call */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2706 0, /* tp_str */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2707 PyObject_GenericGetAttr, /* tp_getattro */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2708 0, /* tp_setattro */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2709 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
2710 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2711 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
2712 (traverseproc)dictiter_traverse, /* tp_traverse */
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2713 0, /* tp_clear */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2714 0, /* tp_richcompare */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2715 0, /* tp_weaklistoffset */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2716 PyObject_SelfIter, /* tp_iter */
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2717 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2718 dictiter_methods, /* tp_methods */
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2719 0,
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2720 };
58256
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2721
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2722 /***********************************************/
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2723 /* View objects for keys(), items(), values(). */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2724 /***********************************************/
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2725
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2726 /* The instance lay-out is the same for all three; but the type differs. */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2727
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2728 typedef struct {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2729 PyObject_HEAD
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2730 PyDictObject *dv_dict;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2731 } dictviewobject;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2732
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2733
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2734 static void
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2735 dictview_dealloc(dictviewobject *dv)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2736 {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2737 Py_XDECREF(dv->dv_dict);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2738 PyObject_GC_Del(dv);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2739 }
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2740
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2741 static int
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2742 dictview_traverse(dictviewobject *dv, visitproc visit, void *arg)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2743 {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2744 Py_VISIT(dv->dv_dict);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2745 return 0;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2746 }
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2747
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2748 static Py_ssize_t
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2749 dictview_len(dictviewobject *dv)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2750 {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2751 Py_ssize_t len = 0;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2752 if (dv->dv_dict != NULL)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2753 len = dv->dv_dict->ma_used;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2754 return len;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2755 }
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2756
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2757 static PyObject *
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2758 dictview_new(PyObject *dict, PyTypeObject *type)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2759 {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2760 dictviewobject *dv;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2761 if (dict == NULL) {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2762 PyErr_BadInternalCall();
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2763 return NULL;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2764 }
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2765 if (!PyDict_Check(dict)) {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2766 /* XXX Get rid of this restriction later */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2767 PyErr_Format(PyExc_TypeError,
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2768 "%s() requires a dict argument, not '%s'",
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2769 type->tp_name, dict->ob_type->tp_name);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2770 return NULL;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2771 }
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2772 dv = PyObject_GC_New(dictviewobject, type);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2773 if (dv == NULL)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2774 return NULL;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2775 Py_INCREF(dict);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2776 dv->dv_dict = (PyDictObject *)dict;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2777 _PyObject_GC_TRACK(dv);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2778 return (PyObject *)dv;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2779 }
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2780
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2781 /* TODO(guido): The views objects are not complete:
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2782
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2783 * support more set operations
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2784 * support arbitrary mappings?
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2785 - either these should be static or exported in dictobject.h
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2786 - if public then they should probably be in builtins
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2787 */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2788
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2789 /* Return 1 if self is a subset of other, iterating over self;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2790 0 if not; -1 if an error occurred. */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2791 static int
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2792 all_contained_in(PyObject *self, PyObject *other)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2793 {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2794 PyObject *iter = PyObject_GetIter(self);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2795 int ok = 1;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2796
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2797 if (iter == NULL)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2798 return -1;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2799 for (;;) {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2800 PyObject *next = PyIter_Next(iter);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2801 if (next == NULL) {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2802 if (PyErr_Occurred())
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2803 ok = -1;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2804 break;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2805 }
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2806 ok = PySequence_Contains(other, next);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2807 Py_DECREF(next);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2808 if (ok <= 0)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2809 break;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2810 }
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2811 Py_DECREF(iter);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2812 return ok;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2813 }
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2814
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2815 static PyObject *
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2816 dictview_richcompare(PyObject *self, PyObject *other, int op)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2817 {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2818 Py_ssize_t len_self, len_other;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2819 int ok;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2820 PyObject *result;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2821
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2822 assert(self != NULL);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2823 assert(PyDictViewSet_Check(self));
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2824 assert(other != NULL);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2825
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2826 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other)) {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2827 Py_INCREF(Py_NotImplemented);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2828 return Py_NotImplemented;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2829 }
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2830
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2831 len_self = PyObject_Size(self);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2832 if (len_self < 0)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2833 return NULL;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2834 len_other = PyObject_Size(other);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2835 if (len_other < 0)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2836 return NULL;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2837
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2838 ok = 0;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2839 switch(op) {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2840
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2841 case Py_NE:
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2842 case Py_EQ:
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2843 if (len_self == len_other)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2844 ok = all_contained_in(self, other);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2845 if (op == Py_NE && ok >= 0)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2846 ok = !ok;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2847 break;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2848
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2849 case Py_LT:
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2850 if (len_self < len_other)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2851 ok = all_contained_in(self, other);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2852 break;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2853
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2854 case Py_LE:
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2855 if (len_self <= len_other)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2856 ok = all_contained_in(self, other);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2857 break;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2858
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2859 case Py_GT:
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2860 if (len_self > len_other)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2861 ok = all_contained_in(other, self);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2862 break;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2863
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2864 case Py_GE:
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2865 if (len_self >= len_other)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2866 ok = all_contained_in(other, self);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2867 break;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2868
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2869 }
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2870 if (ok < 0)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2871 return NULL;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2872 result = ok ? Py_True : Py_False;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2873 Py_INCREF(result);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2874 return result;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2875 }
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2876
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2877 static PyObject *
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2878 dictview_repr(dictviewobject *dv)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2879 {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2880 PyObject *seq;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2881 PyObject *seq_str;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2882 PyObject *result;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2883
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2884 seq = PySequence_List((PyObject *)dv);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2885 if (seq == NULL)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2886 return NULL;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2887
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2888 seq_str = PyObject_Repr(seq);
58265
e30fcb956b5f Fixed repr of dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 58256
diff changeset
2889 result = PyString_FromFormat("%s(%s)", Py_TYPE(dv)->tp_name,
e30fcb956b5f Fixed repr of dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 58256
diff changeset
2890 PyString_AS_STRING(seq_str));
58256
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2891 Py_DECREF(seq_str);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2892 Py_DECREF(seq);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2893 return result;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2894 }
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2895
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2896 /*** dict_keys ***/
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2897
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2898 static PyObject *
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2899 dictkeys_iter(dictviewobject *dv)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2900 {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2901 if (dv->dv_dict == NULL) {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2902 Py_RETURN_NONE;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2903 }
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2904 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2905 }
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2906
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2907 static int
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2908 dictkeys_contains(dictviewobject *dv, PyObject *obj)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2909 {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2910 if (dv->dv_dict == NULL)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2911 return 0;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2912 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2913 }
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2914
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2915 static PySequenceMethods dictkeys_as_sequence = {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2916 (lenfunc)dictview_len, /* sq_length */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2917 0, /* sq_concat */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2918 0, /* sq_repeat */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2919 0, /* sq_item */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2920 0, /* sq_slice */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2921 0, /* sq_ass_item */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2922 0, /* sq_ass_slice */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2923 (objobjproc)dictkeys_contains, /* sq_contains */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2924 };
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2925
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2926 static PyObject*
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2927 dictviews_sub(PyObject* self, PyObject *other)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2928 {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2929 PyObject *result = PySet_New(self);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2930 PyObject *tmp;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2931 if (result == NULL)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2932 return NULL;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2933
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2934 tmp = PyObject_CallMethod(result, "difference_update", "O", other);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2935 if (tmp == NULL) {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2936 Py_DECREF(result);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2937 return NULL;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2938 }
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2939
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2940 Py_DECREF(tmp);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2941 return result;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2942 }
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2943
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2944 static PyObject*
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2945 dictviews_and(PyObject* self, PyObject *other)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2946 {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2947 PyObject *result = PySet_New(self);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2948 PyObject *tmp;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2949 if (result == NULL)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2950 return NULL;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2951
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2952 tmp = PyObject_CallMethod(result, "intersection_update", "O", other);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2953 if (tmp == NULL) {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2954 Py_DECREF(result);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2955 return NULL;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2956 }
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2957
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2958 Py_DECREF(tmp);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2959 return result;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2960 }
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2961
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2962 static PyObject*
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2963 dictviews_or(PyObject* self, PyObject *other)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2964 {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2965 PyObject *result = PySet_New(self);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2966 PyObject *tmp;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2967 if (result == NULL)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2968 return NULL;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2969
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2970 tmp = PyObject_CallMethod(result, "update", "O", other);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2971 if (tmp == NULL) {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2972 Py_DECREF(result);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2973 return NULL;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2974 }
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2975
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2976 Py_DECREF(tmp);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2977 return result;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2978 }
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2979
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2980 static PyObject*
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2981 dictviews_xor(PyObject* self, PyObject *other)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2982 {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2983 PyObject *result = PySet_New(self);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2984 PyObject *tmp;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2985 if (result == NULL)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2986 return NULL;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2987
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2988 tmp = PyObject_CallMethod(result, "symmetric_difference_update", "O",
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2989 other);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2990 if (tmp == NULL) {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2991 Py_DECREF(result);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2992 return NULL;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2993 }
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2994
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2995 Py_DECREF(tmp);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2996 return result;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2997 }
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2998
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
2999 static PyNumberMethods dictviews_as_number = {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3000 0, /*nb_add*/
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3001 (binaryfunc)dictviews_sub, /*nb_subtract*/
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3002 0, /*nb_multiply*/
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3003 0, /*nb_remainder*/
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3004 0, /*nb_divmod*/
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3005 0, /*nb_power*/
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3006 0, /*nb_negative*/
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3007 0, /*nb_positive*/
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3008 0, /*nb_absolute*/
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3009 0, /*nb_bool*/
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3010 0, /*nb_invert*/
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3011 0, /*nb_lshift*/
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3012 0, /*nb_rshift*/
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3013 (binaryfunc)dictviews_and, /*nb_and*/
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3014 (binaryfunc)dictviews_xor, /*nb_xor*/
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3015 (binaryfunc)dictviews_or, /*nb_or*/
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3016 };
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3017
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3018 static PyMethodDef dictkeys_methods[] = {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3019 {NULL, NULL} /* sentinel */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3020 };
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3021
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3022 PyTypeObject PyDictKeys_Type = {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3023 PyVarObject_HEAD_INIT(&PyType_Type, 0)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3024 "dict_keys", /* tp_name */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3025 sizeof(dictviewobject), /* tp_basicsize */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3026 0, /* tp_itemsize */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3027 /* methods */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3028 (destructor)dictview_dealloc, /* tp_dealloc */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3029 0, /* tp_print */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3030 0, /* tp_getattr */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3031 0, /* tp_setattr */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3032 0, /* tp_reserved */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3033 (reprfunc)dictview_repr, /* tp_repr */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3034 &dictviews_as_number, /* tp_as_number */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3035 &dictkeys_as_sequence, /* tp_as_sequence */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3036 0, /* tp_as_mapping */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3037 0, /* tp_hash */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3038 0, /* tp_call */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3039 0, /* tp_str */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3040 PyObject_GenericGetAttr, /* tp_getattro */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3041 0, /* tp_setattro */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3042 0, /* tp_as_buffer */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3043 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3044 0, /* tp_doc */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3045 (traverseproc)dictview_traverse, /* tp_traverse */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3046 0, /* tp_clear */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3047 dictview_richcompare, /* tp_richcompare */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3048 0, /* tp_weaklistoffset */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3049 (getiterfunc)dictkeys_iter, /* tp_iter */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3050 0, /* tp_iternext */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3051 dictkeys_methods, /* tp_methods */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3052 0,
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3053 };
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3054
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3055 static PyObject *
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3056 dictkeys_new(PyObject *dict)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3057 {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3058 return dictview_new(dict, &PyDictKeys_Type);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3059 }
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3060
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3061 /*** dict_items ***/
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3062
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3063 static PyObject *
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3064 dictitems_iter(dictviewobject *dv)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3065 {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3066 if (dv->dv_dict == NULL) {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3067 Py_RETURN_NONE;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3068 }
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3069 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3070 }
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3071
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3072 static int
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3073 dictitems_contains(dictviewobject *dv, PyObject *obj)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3074 {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3075 PyObject *key, *value, *found;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3076 if (dv->dv_dict == NULL)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3077 return 0;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3078 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3079 return 0;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3080 key = PyTuple_GET_ITEM(obj, 0);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3081 value = PyTuple_GET_ITEM(obj, 1);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3082 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3083 if (found == NULL) {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3084 if (PyErr_Occurred())
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3085 return -1;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3086 return 0;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3087 }
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3088 return PyObject_RichCompareBool(value, found, Py_EQ);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3089 }
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3090
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3091 static PySequenceMethods dictitems_as_sequence = {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3092 (lenfunc)dictview_len, /* sq_length */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3093 0, /* sq_concat */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3094 0, /* sq_repeat */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3095 0, /* sq_item */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3096 0, /* sq_slice */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3097 0, /* sq_ass_item */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3098 0, /* sq_ass_slice */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3099 (objobjproc)dictitems_contains, /* sq_contains */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3100 };
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3101
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3102 static PyMethodDef dictitems_methods[] = {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3103 {NULL, NULL} /* sentinel */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3104 };
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3105
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3106 PyTypeObject PyDictItems_Type = {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3107 PyVarObject_HEAD_INIT(&PyType_Type, 0)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3108 "dict_items", /* tp_name */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3109 sizeof(dictviewobject), /* tp_basicsize */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3110 0, /* tp_itemsize */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3111 /* methods */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3112 (destructor)dictview_dealloc, /* tp_dealloc */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3113 0, /* tp_print */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3114 0, /* tp_getattr */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3115 0, /* tp_setattr */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3116 0, /* tp_reserved */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3117 (reprfunc)dictview_repr, /* tp_repr */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3118 &dictviews_as_number, /* tp_as_number */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3119 &dictitems_as_sequence, /* tp_as_sequence */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3120 0, /* tp_as_mapping */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3121 0, /* tp_hash */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3122 0, /* tp_call */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3123 0, /* tp_str */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3124 PyObject_GenericGetAttr, /* tp_getattro */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3125 0, /* tp_setattro */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3126 0, /* tp_as_buffer */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3127 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3128 0, /* tp_doc */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3129 (traverseproc)dictview_traverse, /* tp_traverse */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3130 0, /* tp_clear */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3131 dictview_richcompare, /* tp_richcompare */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3132 0, /* tp_weaklistoffset */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3133 (getiterfunc)dictitems_iter, /* tp_iter */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3134 0, /* tp_iternext */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3135 dictitems_methods, /* tp_methods */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3136 0,
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3137 };
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3138
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3139 static PyObject *
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3140 dictitems_new(PyObject *dict)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3141 {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3142 return dictview_new(dict, &PyDictItems_Type);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3143 }
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3144
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3145 /*** dict_values ***/
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3146
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3147 static PyObject *
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3148 dictvalues_iter(dictviewobject *dv)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3149 {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3150 if (dv->dv_dict == NULL) {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3151 Py_RETURN_NONE;
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3152 }
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3153 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3154 }
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3155
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3156 static PySequenceMethods dictvalues_as_sequence = {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3157 (lenfunc)dictview_len, /* sq_length */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3158 0, /* sq_concat */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3159 0, /* sq_repeat */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3160 0, /* sq_item */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3161 0, /* sq_slice */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3162 0, /* sq_ass_item */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3163 0, /* sq_ass_slice */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3164 (objobjproc)0, /* sq_contains */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3165 };
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3166
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3167 static PyMethodDef dictvalues_methods[] = {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3168 {NULL, NULL} /* sentinel */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3169 };
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3170
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3171 PyTypeObject PyDictValues_Type = {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3172 PyVarObject_HEAD_INIT(&PyType_Type, 0)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3173 "dict_values", /* tp_name */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3174 sizeof(dictviewobject), /* tp_basicsize */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3175 0, /* tp_itemsize */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3176 /* methods */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3177 (destructor)dictview_dealloc, /* tp_dealloc */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3178 0, /* tp_print */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3179 0, /* tp_getattr */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3180 0, /* tp_setattr */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3181 0, /* tp_reserved */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3182 (reprfunc)dictview_repr, /* tp_repr */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3183 0, /* tp_as_number */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3184 &dictvalues_as_sequence, /* tp_as_sequence */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3185 0, /* tp_as_mapping */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3186 0, /* tp_hash */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3187 0, /* tp_call */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3188 0, /* tp_str */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3189 PyObject_GenericGetAttr, /* tp_getattro */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3190 0, /* tp_setattro */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3191 0, /* tp_as_buffer */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3192 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3193 0, /* tp_doc */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3194 (traverseproc)dictview_traverse, /* tp_traverse */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3195 0, /* tp_clear */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3196 0, /* tp_richcompare */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3197 0, /* tp_weaklistoffset */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3198 (getiterfunc)dictvalues_iter, /* tp_iter */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3199 0, /* tp_iternext */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3200 dictvalues_methods, /* tp_methods */
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3201 0,
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3202 };
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3203
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3204 static PyObject *
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3205 dictvalues_new(PyObject *dict)
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3206 {
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3207 return dictview_new(dict, &PyDictValues_Type);
d9805a96351c Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55437
diff changeset
3208 }