Mercurial > cpython-lru_cache-weakref
annotate Objects/dictobject.c @ 27998:e27f58a50737 legacy-trunk
SF bug #690377: Dud footnote reference in distutils docs
| author | Raymond Hettinger <python@rcn.com> |
|---|---|
| date | Fri, 21 Feb 2003 02:47:09 +0000 |
| parents | 81b2bcbfe39b |
| children | 92e3b611dc9f |
| 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 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
4 #include "Python.h" |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
6 typedef PyDictEntry dictentry; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
7 typedef PyDictObject dictobject; |
|
5053
219bdd8afb38
Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents:
5038
diff
changeset
|
8 |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
9 /* 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
|
10 #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
|
11 |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
12 /* 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
|
13 #define PERTURB_SHIFT 5 |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
14 |
|
5053
219bdd8afb38
Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents:
5038
diff
changeset
|
15 /* |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
16 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
|
17 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
|
18 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
|
19 cases: |
|
17973
2f2ee5daa60d
Implement an old idea of Christian Tismer's: use polynomial division
Tim Peters <tim.peters@gmail.com>
parents:
17964
diff
changeset
|
20 |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
21 >>> 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
|
22 [0, 1, 2, 3] |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
23 >>> 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
|
24 [-1658398457, -1658398460, -1658398459, -1658398462] |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
25 >>> |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
26 |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
27 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
|
28 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
|
29 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
|
30 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
|
31 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
|
32 |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
33 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
|
34 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
|
35 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
|
36 [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
|
37 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
|
38 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
|
39 |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
40 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
|
41 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
|
42 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
|
43 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
|
44 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
|
45 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
|
46 |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
47 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
|
48 recurrence: |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
49 |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
50 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
|
51 |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
52 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
|
53 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
|
54 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
|
55 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
|
56 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
|
57 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
|
58 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
|
59 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
|
60 |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
61 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
|
62 |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
63 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
|
64 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
|
65 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
|
66 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
|
67 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
|
68 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
|
69 |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
70 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
|
71 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
|
72 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
|
73 |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
74 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
|
75 perturb >>= PERTURB_SHIFT; |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
76 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
|
77 |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
78 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
|
79 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
|
80 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
|
81 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
|
82 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
|
83 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
|
84 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
|
85 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
|
86 |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
87 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
|
88 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
|
89 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
|
90 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
|
91 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
|
92 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
|
93 |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
94 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
|
95 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
|
96 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
|
97 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
|
98 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
|
99 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
|
100 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
|
101 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
|
102 (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
|
103 above, and then shifting perturb can be done while the table index is being |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
104 masked); and the dictobject struct required a member to hold the table's |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
105 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
|
106 equally good collision statistics, needed less code & used less memory. |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
107 */ |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
108 |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
109 /* Object used as dummy key to fill deleted entries */ |
|
5505
47fa00877924
Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
110 static PyObject *dummy; /* Initialized by first call to newdictobject() */ |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
111 |
|
14310
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
112 /* forward declarations */ |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
113 static dictentry * |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
114 lookdict_string(dictobject *mp, PyObject *key, long hash); |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
115 |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
116 #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
|
117 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
|
118 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
|
119 |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
120 static void |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
121 show_counts(void) |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
122 { |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
123 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
|
124 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
|
125 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
|
126 } |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
127 #endif |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
128 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
129 /* Initialization macros. |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
130 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
|
131 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
|
132 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
|
133 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
|
134 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
|
135 an excellent reason not to). |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
136 */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
137 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
138 #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
|
139 (mp)->ma_table = (mp)->ma_smalltable; \ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
140 (mp)->ma_mask = PyDict_MINSIZE - 1; \ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
141 } while(0) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
142 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
143 #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
|
144 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
|
145 (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
|
146 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
|
147 } while(0) |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
148 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
149 PyObject * |
|
13660
a1ce07218781
Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents:
13620
diff
changeset
|
150 PyDict_New(void) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
151 { |
|
5505
47fa00877924
Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
152 register dictobject *mp; |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
153 if (dummy == NULL) { /* Auto-initialize dummy */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
154 dummy = PyString_FromString("<dummy key>"); |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
155 if (dummy == NULL) |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
156 return NULL; |
|
14310
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
157 #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
|
158 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
|
159 #endif |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
160 } |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19301
diff
changeset
|
161 mp = PyObject_GC_New(dictobject, &PyDict_Type); |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
162 if (mp == NULL) |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
163 return NULL; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
164 EMPTY_TO_MINSIZE(mp); |
|
14310
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
165 mp->ma_lookup = lookdict_string; |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
166 #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
|
167 ++created; |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
168 #endif |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19301
diff
changeset
|
169 _PyObject_GC_TRACK(mp); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
170 return (PyObject *)mp; |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
171 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
172 |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
173 /* |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
174 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
|
175 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
|
176 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
|
177 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
|
178 |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
179 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
|
180 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
|
181 |
|
10461
4abc5abaec9a
Vladimir Marangozov contributed updated comments.
Guido van Rossum <guido@python.org>
parents:
9487
diff
changeset
|
182 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
|
183 |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
184 (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
|
185 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
|
186 Christian Tismer). |
|
14310
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
187 |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
188 This function must never return NULL; failures are indicated by returning |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
189 a dictentry* for which the me_value field is NULL. Exceptions are never |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
190 reported by this function, and outstanding exceptions are maintained. |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
191 */ |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
192 |
|
5505
47fa00877924
Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
193 static dictentry * |
|
13341
3f1bd4782628
Removed Py_PROTO and switched to ANSI C declarations in the dict
Tim Peters <tim.peters@gmail.com>
parents:
13265
diff
changeset
|
194 lookdict(dictobject *mp, PyObject *key, register long hash) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
195 { |
|
5053
219bdd8afb38
Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents:
5038
diff
changeset
|
196 register int i; |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
197 register unsigned int perturb; |
|
6105
77fb880eca49
Made lookdict nearly twice as fast, resulting in a 5% overall
Guido van Rossum <guido@python.org>
parents:
5784
diff
changeset
|
198 register dictentry *freeslot; |
|
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
|
199 register unsigned int mask = mp->ma_mask; |
|
5505
47fa00877924
Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
200 dictentry *ep0 = mp->ma_table; |
|
47fa00877924
Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
201 register dictentry *ep; |
|
18022
bd1c4fe977cb
lookdict: Reduce obfuscating code duplication with a judicious goto.
Tim Peters <tim.peters@gmail.com>
parents:
18019
diff
changeset
|
202 register int restore_error; |
|
bd1c4fe977cb
lookdict: Reduce obfuscating code duplication with a judicious goto.
Tim Peters <tim.peters@gmail.com>
parents:
18019
diff
changeset
|
203 register int checked_error; |
|
14307
b24db770dffd
Clear errors raised by PyObject_Compare() without losing any existing
Fred Drake <fdrake@acm.org>
parents:
13892
diff
changeset
|
204 register int cmp; |
|
b24db770dffd
Clear errors raised by PyObject_Compare() without losing any existing
Fred Drake <fdrake@acm.org>
parents:
13892
diff
changeset
|
205 PyObject *err_type, *err_value, *err_tb; |
|
18023
8d2bbbbf2cb9
lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents:
18022
diff
changeset
|
206 PyObject *startkey; |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
207 |
|
17815
d6df8d371b67
Get rid of the superstitious "~" in dict hashing's "i = (~hash) & mask".
Tim Peters <tim.peters@gmail.com>
parents:
17775
diff
changeset
|
208 i = hash & mask; |
|
5059
192d571cc461
Small lookmapping nits:
Guido van Rossum <guido@python.org>
parents:
5056
diff
changeset
|
209 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
|
210 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
|
211 return ep; |
|
18022
bd1c4fe977cb
lookdict: Reduce obfuscating code duplication with a judicious goto.
Tim Peters <tim.peters@gmail.com>
parents:
18019
diff
changeset
|
212 |
|
bd1c4fe977cb
lookdict: Reduce obfuscating code duplication with a judicious goto.
Tim Peters <tim.peters@gmail.com>
parents:
18019
diff
changeset
|
213 restore_error = checked_error = 0; |
|
5053
219bdd8afb38
Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents:
5038
diff
changeset
|
214 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
|
215 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
|
216 else { |
|
14307
b24db770dffd
Clear errors raised by PyObject_Compare() without losing any existing
Fred Drake <fdrake@acm.org>
parents:
13892
diff
changeset
|
217 if (ep->me_hash == hash) { |
|
b24db770dffd
Clear errors raised by PyObject_Compare() without losing any existing
Fred Drake <fdrake@acm.org>
parents:
13892
diff
changeset
|
218 /* error can't have been checked yet */ |
|
b24db770dffd
Clear errors raised by PyObject_Compare() without losing any existing
Fred Drake <fdrake@acm.org>
parents:
13892
diff
changeset
|
219 checked_error = 1; |
|
b24db770dffd
Clear errors raised by PyObject_Compare() without losing any existing
Fred Drake <fdrake@acm.org>
parents:
13892
diff
changeset
|
220 if (PyErr_Occurred()) { |
|
b24db770dffd
Clear errors raised by PyObject_Compare() without losing any existing
Fred Drake <fdrake@acm.org>
parents:
13892
diff
changeset
|
221 restore_error = 1; |
|
b24db770dffd
Clear errors raised by PyObject_Compare() without losing any existing
Fred Drake <fdrake@acm.org>
parents:
13892
diff
changeset
|
222 PyErr_Fetch(&err_type, &err_value, &err_tb); |
|
b24db770dffd
Clear errors raised by PyObject_Compare() without losing any existing
Fred Drake <fdrake@acm.org>
parents:
13892
diff
changeset
|
223 } |
|
18023
8d2bbbbf2cb9
lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents:
18022
diff
changeset
|
224 startkey = ep->me_key; |
|
8d2bbbbf2cb9
lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents:
18022
diff
changeset
|
225 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ); |
|
18022
bd1c4fe977cb
lookdict: Reduce obfuscating code duplication with a judicious goto.
Tim Peters <tim.peters@gmail.com>
parents:
18019
diff
changeset
|
226 if (cmp < 0) |
| 15974 | 227 PyErr_Clear(); |
|
18023
8d2bbbbf2cb9
lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents:
18022
diff
changeset
|
228 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
|
229 if (cmp > 0) |
|
8d2bbbbf2cb9
lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents:
18022
diff
changeset
|
230 goto Done; |
|
8d2bbbbf2cb9
lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents:
18022
diff
changeset
|
231 } |
|
8d2bbbbf2cb9
lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents:
18022
diff
changeset
|
232 else { |
|
8d2bbbbf2cb9
lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents:
18022
diff
changeset
|
233 /* 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
|
234 * dict: start over. |
|
8d2bbbbf2cb9
lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents:
18022
diff
changeset
|
235 * 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
|
236 * XXX from terminating. |
|
8d2bbbbf2cb9
lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents:
18022
diff
changeset
|
237 */ |
|
8d2bbbbf2cb9
lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents:
18022
diff
changeset
|
238 ep = lookdict(mp, key, hash); |
|
8d2bbbbf2cb9
lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents:
18022
diff
changeset
|
239 goto Done; |
|
8d2bbbbf2cb9
lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents:
18022
diff
changeset
|
240 } |
|
6105
77fb880eca49
Made lookdict nearly twice as fast, resulting in a 5% overall
Guido van Rossum <guido@python.org>
parents:
5784
diff
changeset
|
241 } |
|
77fb880eca49
Made lookdict nearly twice as fast, resulting in a 5% overall
Guido van Rossum <guido@python.org>
parents:
5784
diff
changeset
|
242 freeslot = NULL; |
| 5005 | 243 } |
|
17973
2f2ee5daa60d
Implement an old idea of Christian Tismer's: use polynomial division
Tim Peters <tim.peters@gmail.com>
parents:
17964
diff
changeset
|
244 |
|
17816
8f5ce58119e0
Aggressive reordering of dict comparisons. In case of collision, it stands
Tim Peters <tim.peters@gmail.com>
parents:
17815
diff
changeset
|
245 /* 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
|
246 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
|
247 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
|
248 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
|
249 ep = &ep0[i & mask]; |
|
5053
219bdd8afb38
Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents:
5038
diff
changeset
|
250 if (ep->me_key == NULL) { |
|
18022
bd1c4fe977cb
lookdict: Reduce obfuscating code duplication with a judicious goto.
Tim Peters <tim.peters@gmail.com>
parents:
18019
diff
changeset
|
251 if (freeslot != NULL) |
|
bd1c4fe977cb
lookdict: Reduce obfuscating code duplication with a judicious goto.
Tim Peters <tim.peters@gmail.com>
parents:
18019
diff
changeset
|
252 ep = freeslot; |
|
bd1c4fe977cb
lookdict: Reduce obfuscating code duplication with a judicious goto.
Tim Peters <tim.peters@gmail.com>
parents:
18019
diff
changeset
|
253 break; |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
254 } |
|
18022
bd1c4fe977cb
lookdict: Reduce obfuscating code duplication with a judicious goto.
Tim Peters <tim.peters@gmail.com>
parents:
18019
diff
changeset
|
255 if (ep->me_key == key) |
|
bd1c4fe977cb
lookdict: Reduce obfuscating code duplication with a judicious goto.
Tim Peters <tim.peters@gmail.com>
parents:
18019
diff
changeset
|
256 break; |
|
bd1c4fe977cb
lookdict: Reduce obfuscating code duplication with a judicious goto.
Tim Peters <tim.peters@gmail.com>
parents:
18019
diff
changeset
|
257 if (ep->me_hash == hash && ep->me_key != dummy) { |
|
14307
b24db770dffd
Clear errors raised by PyObject_Compare() without losing any existing
Fred Drake <fdrake@acm.org>
parents:
13892
diff
changeset
|
258 if (!checked_error) { |
|
b24db770dffd
Clear errors raised by PyObject_Compare() without losing any existing
Fred Drake <fdrake@acm.org>
parents:
13892
diff
changeset
|
259 checked_error = 1; |
|
b24db770dffd
Clear errors raised by PyObject_Compare() without losing any existing
Fred Drake <fdrake@acm.org>
parents:
13892
diff
changeset
|
260 if (PyErr_Occurred()) { |
|
b24db770dffd
Clear errors raised by PyObject_Compare() without losing any existing
Fred Drake <fdrake@acm.org>
parents:
13892
diff
changeset
|
261 restore_error = 1; |
|
b24db770dffd
Clear errors raised by PyObject_Compare() without losing any existing
Fred Drake <fdrake@acm.org>
parents:
13892
diff
changeset
|
262 PyErr_Fetch(&err_type, &err_value, |
|
b24db770dffd
Clear errors raised by PyObject_Compare() without losing any existing
Fred Drake <fdrake@acm.org>
parents:
13892
diff
changeset
|
263 &err_tb); |
|
b24db770dffd
Clear errors raised by PyObject_Compare() without losing any existing
Fred Drake <fdrake@acm.org>
parents:
13892
diff
changeset
|
264 } |
|
b24db770dffd
Clear errors raised by PyObject_Compare() without losing any existing
Fred Drake <fdrake@acm.org>
parents:
13892
diff
changeset
|
265 } |
|
18023
8d2bbbbf2cb9
lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents:
18022
diff
changeset
|
266 startkey = ep->me_key; |
|
8d2bbbbf2cb9
lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents:
18022
diff
changeset
|
267 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ); |
|
18022
bd1c4fe977cb
lookdict: Reduce obfuscating code duplication with a judicious goto.
Tim Peters <tim.peters@gmail.com>
parents:
18019
diff
changeset
|
268 if (cmp < 0) |
| 15974 | 269 PyErr_Clear(); |
|
18023
8d2bbbbf2cb9
lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents:
18022
diff
changeset
|
270 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
|
271 if (cmp > 0) |
|
8d2bbbbf2cb9
lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents:
18022
diff
changeset
|
272 break; |
|
8d2bbbbf2cb9
lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents:
18022
diff
changeset
|
273 } |
|
8d2bbbbf2cb9
lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents:
18022
diff
changeset
|
274 else { |
|
8d2bbbbf2cb9
lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents:
18022
diff
changeset
|
275 /* 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
|
276 * dict: start over. |
|
8d2bbbbf2cb9
lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents:
18022
diff
changeset
|
277 * 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
|
278 * XXX from terminating. |
|
8d2bbbbf2cb9
lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents:
18022
diff
changeset
|
279 */ |
|
8d2bbbbf2cb9
lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents:
18022
diff
changeset
|
280 ep = lookdict(mp, key, hash); |
|
8d2bbbbf2cb9
lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents:
18022
diff
changeset
|
281 break; |
|
8d2bbbbf2cb9
lookdict: stop more insane core-dump mutating comparison cases. Should
Tim Peters <tim.peters@gmail.com>
parents:
18022
diff
changeset
|
282 } |
|
5053
219bdd8afb38
Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents:
5038
diff
changeset
|
283 } |
|
17816
8f5ce58119e0
Aggressive reordering of dict comparisons. In case of collision, it stands
Tim Peters <tim.peters@gmail.com>
parents:
17815
diff
changeset
|
284 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
|
285 freeslot = ep; |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
286 } |
|
18022
bd1c4fe977cb
lookdict: Reduce obfuscating code duplication with a judicious goto.
Tim Peters <tim.peters@gmail.com>
parents:
18019
diff
changeset
|
287 |
|
bd1c4fe977cb
lookdict: Reduce obfuscating code duplication with a judicious goto.
Tim Peters <tim.peters@gmail.com>
parents:
18019
diff
changeset
|
288 Done: |
|
bd1c4fe977cb
lookdict: Reduce obfuscating code duplication with a judicious goto.
Tim Peters <tim.peters@gmail.com>
parents:
18019
diff
changeset
|
289 if (restore_error) |
|
bd1c4fe977cb
lookdict: Reduce obfuscating code duplication with a judicious goto.
Tim Peters <tim.peters@gmail.com>
parents:
18019
diff
changeset
|
290 PyErr_Restore(err_type, err_value, err_tb); |
|
bd1c4fe977cb
lookdict: Reduce obfuscating code duplication with a judicious goto.
Tim Peters <tim.peters@gmail.com>
parents:
18019
diff
changeset
|
291 return ep; |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
292 } |
|
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 /* |
|
14310
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
295 * Hacked up version of lookdict which can assume keys are always strings; |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
296 * this assumption allows testing for errors during PyObject_Compare() to |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
297 * be dropped; string-string comparisons never raise exceptions. This also |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
298 * means we don't need to go through PyObject_Compare(); we can always use |
|
17964
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17963
diff
changeset
|
299 * _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
|
300 * |
|
19904
58d50094bf80
Changed the dict implementation to take "string shortcuts" only when
Tim Peters <tim.peters@gmail.com>
parents:
19662
diff
changeset
|
301 * This is valuable because the general-case error handling in lookdict() is |
|
58d50094bf80
Changed the dict implementation to take "string shortcuts" only when
Tim Peters <tim.peters@gmail.com>
parents:
19662
diff
changeset
|
302 * expensive, and dicts with pure-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
|
303 */ |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
304 static dictentry * |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
305 lookdict_string(dictobject *mp, PyObject *key, register long hash) |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
306 { |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
307 register int i; |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
308 register unsigned int perturb; |
|
14310
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
309 register dictentry *freeslot; |
|
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
|
310 register unsigned int mask = mp->ma_mask; |
|
14310
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
311 dictentry *ep0 = mp->ma_table; |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
312 register dictentry *ep; |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
313 |
|
19904
58d50094bf80
Changed the dict implementation to take "string shortcuts" only when
Tim Peters <tim.peters@gmail.com>
parents:
19662
diff
changeset
|
314 /* 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
|
315 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
|
316 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
|
317 that here. */ |
|
58d50094bf80
Changed the dict implementation to take "string shortcuts" only when
Tim Peters <tim.peters@gmail.com>
parents:
19662
diff
changeset
|
318 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
|
319 #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
|
320 ++converted; |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
321 #endif |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
322 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
|
323 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
|
324 } |
|
17815
d6df8d371b67
Get rid of the superstitious "~" in dict hashing's "i = (~hash) & mask".
Tim Peters <tim.peters@gmail.com>
parents:
17775
diff
changeset
|
325 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
|
326 ep = &ep0[i]; |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
327 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
|
328 return ep; |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
329 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
|
330 freeslot = ep; |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
331 else { |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
332 if (ep->me_hash == hash |
|
17964
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17963
diff
changeset
|
333 && _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
|
334 return ep; |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
335 } |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
336 freeslot = NULL; |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
337 } |
|
17973
2f2ee5daa60d
Implement an old idea of Christian Tismer's: use polynomial division
Tim Peters <tim.peters@gmail.com>
parents:
17964
diff
changeset
|
338 |
|
17816
8f5ce58119e0
Aggressive reordering of dict comparisons. In case of collision, it stands
Tim Peters <tim.peters@gmail.com>
parents:
17815
diff
changeset
|
339 /* In the loop, me_key == dummy is by far (factor of 100s) the |
|
8f5ce58119e0
Aggressive reordering of dict comparisons. In case of collision, it stands
Tim Peters <tim.peters@gmail.com>
parents:
17815
diff
changeset
|
340 least likely outcome, so test for that last. */ |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
341 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
342 i = (i << 2) + i + perturb + 1; |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
343 ep = &ep0[i & mask]; |
|
17816
8f5ce58119e0
Aggressive reordering of dict comparisons. In case of collision, it stands
Tim Peters <tim.peters@gmail.com>
parents:
17815
diff
changeset
|
344 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
|
345 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
|
346 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
|
347 || (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
|
348 && ep->me_key != dummy |
|
17964
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17963
diff
changeset
|
349 && _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
|
350 return ep; |
|
17920
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
351 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
|
352 freeslot = ep; |
|
14310
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
353 } |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
354 } |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
355 |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
356 /* |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
357 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
|
358 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
|
359 Eats a reference to key and one to value. |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
360 */ |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
361 static 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
|
362 insertdict(register dictobject *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
|
363 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
364 PyObject *old_value; |
|
5505
47fa00877924
Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
365 register dictentry *ep; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
366 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
367 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
368 assert(mp->ma_lookup != NULL); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
369 ep = mp->ma_lookup(mp, key, hash); |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
370 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
|
371 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
|
372 ep->me_value = value; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
373 Py_DECREF(old_value); /* which **CAN** re-enter */ |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
374 Py_DECREF(key); |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
375 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
376 else { |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
377 if (ep->me_key == NULL) |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
378 mp->ma_fill++; |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
379 else |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
380 Py_DECREF(ep->me_key); |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
381 ep->me_key = key; |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
382 ep->me_hash = 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
|
383 ep->me_value = value; |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
384 mp->ma_used++; |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
385 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
386 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
387 |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
388 /* |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
389 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
|
390 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
|
391 actually be smaller than the old one. |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
392 */ |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
393 static 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
|
394 dictresize(dictobject *mp, int minused) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
395 { |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
396 int newsize; |
|
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
|
397 dictentry *oldtable, *newtable, *ep; |
|
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
|
398 int i; |
|
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
|
399 int is_oldtable_malloced; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
400 dictentry small_copy[PyDict_MINSIZE]; |
| 17861 | 401 |
| 402 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
|
403 |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
404 /* 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
|
405 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
|
406 newsize <= minused && newsize > 0; |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
407 newsize <<= 1) |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
408 ; |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
409 if (newsize <= 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
410 PyErr_NoMemory(); |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
411 return -1; |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
412 } |
|
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
|
413 |
|
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
|
414 /* 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
|
415 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
|
416 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
|
417 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
|
418 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
419 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
|
420 /* 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
|
421 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
|
422 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
|
423 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
|
424 /* 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
|
425 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
|
426 } |
|
95baa333ee43
dictresize(): Rebuild small tables if there are any dummies, not just if
Tim Peters <tim.peters@gmail.com>
parents:
17962
diff
changeset
|
427 /* 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
|
428 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
|
429 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
|
430 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
|
431 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
|
432 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
|
433 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
|
434 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
|
435 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
|
436 } |
|
17920
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
437 } |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
438 else { |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
439 newtable = PyMem_NEW(dictentry, newsize); |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
440 if (newtable == NULL) { |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
441 PyErr_NoMemory(); |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
442 return -1; |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
443 } |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
444 } |
|
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
|
445 |
|
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
|
446 /* 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
|
447 assert(newtable != oldtable); |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
448 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
|
449 mp->ma_mask = newsize - 1; |
|
17920
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
450 memset(newtable, 0, sizeof(dictentry) * newsize); |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
451 mp->ma_used = 0; |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
452 i = mp->ma_fill; |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
453 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
|
454 |
|
17852
ba97111a6ad9
Speed dictresize by collapsing its two passes into one; the reason given
Tim Peters <tim.peters@gmail.com>
parents:
17816
diff
changeset
|
455 /* 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
|
456 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
|
457 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
|
458 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
|
459 --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
|
460 insertdict(mp, ep->me_key, ep->me_hash, ep->me_value); |
|
17920
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
461 } |
|
17852
ba97111a6ad9
Speed dictresize by collapsing its two passes into one; the reason given
Tim Peters <tim.peters@gmail.com>
parents:
17816
diff
changeset
|
462 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
|
463 --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
|
464 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
|
465 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
|
466 } |
|
17852
ba97111a6ad9
Speed dictresize by collapsing its two passes into one; the reason given
Tim Peters <tim.peters@gmail.com>
parents:
17816
diff
changeset
|
467 /* 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
|
468 } |
|
2053
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
1848
diff
changeset
|
469 |
|
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
|
470 if (is_oldtable_malloced) |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12126
diff
changeset
|
471 PyMem_DEL(oldtable); |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
472 return 0; |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
473 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
474 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
475 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
|
476 PyDict_GetItem(PyObject *op, PyObject *key) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
477 { |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
478 long hash; |
|
14310
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
479 dictobject *mp = (dictobject *)op; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
480 if (!PyDict_Check(op)) { |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
481 return NULL; |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
482 } |
|
19904
58d50094bf80
Changed the dict implementation to take "string shortcuts" only when
Tim Peters <tim.peters@gmail.com>
parents:
19662
diff
changeset
|
483 if (!PyString_CheckExact(key) || |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
484 (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
|
485 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
486 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
|
487 if (hash == -1) { |
|
a23b75138bb0
Make sure that PyDict_GetItem[String]() *never* raises an exception.
Guido van Rossum <guido@python.org>
parents:
7985
diff
changeset
|
488 PyErr_Clear(); |
|
5038
a820dead06e6
Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents:
5005
diff
changeset
|
489 return NULL; |
|
8326
a23b75138bb0
Make sure that PyDict_GetItem[String]() *never* raises an exception.
Guido van Rossum <guido@python.org>
parents:
7985
diff
changeset
|
490 } |
|
5038
a820dead06e6
Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents:
5005
diff
changeset
|
491 } |
|
14310
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
492 return (mp->ma_lookup)(mp, key, hash)->me_value; |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
493 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
494 |
|
17535
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
495 /* CAUTION: PyDict_SetItem() must guarantee that it won't resize the |
|
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
496 * dictionary if it is merely replacing the value for an existing key. |
|
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
497 * This is means that it's safe to loop over a dictionary with |
|
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
498 * PyDict_Next() and occasionally replace a value -- but you can't |
|
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
499 * insert new keys or remove them. |
|
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
500 */ |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
501 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
|
502 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
|
503 { |
|
5505
47fa00877924
Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
504 register dictobject *mp; |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
505 register long hash; |
|
17535
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
506 register int n_used; |
|
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
507 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
508 if (!PyDict_Check(op)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
509 PyErr_BadInternalCall(); |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
510 return -1; |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
511 } |
|
5505
47fa00877924
Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
512 mp = (dictobject *)op; |
|
19904
58d50094bf80
Changed the dict implementation to take "string shortcuts" only when
Tim Peters <tim.peters@gmail.com>
parents:
19662
diff
changeset
|
513 if (PyString_CheckExact(key)) { |
|
24901
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24280
diff
changeset
|
514 hash = ((PyStringObject *)key)->ob_shash; |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24280
diff
changeset
|
515 if (hash == -1) |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24280
diff
changeset
|
516 hash = PyObject_Hash(key); |
| 5005 | 517 } |
|
22651
f3a416b178c4
Remove the CACHE_HASH and INTERN_STRINGS preprocessor symbols.
Tim Peters <tim.peters@gmail.com>
parents:
22625
diff
changeset
|
518 else { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
519 hash = PyObject_Hash(key); |
| 5005 | 520 if (hash == -1) |
| 521 return -1; | |
| 522 } | |
|
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
|
523 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
|
524 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
|
525 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
|
526 Py_INCREF(key); |
|
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
527 insertdict(mp, key, hash, value); |
|
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
528 /* If we added a key, we can safely resize. Otherwise skip this! |
|
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
529 * If fill >= 2/3 size, adjust size. Normally, this doubles the |
|
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
|
530 * size, but it's also possible for the dict to shrink (if ma_fill is |
|
e1214f9e12a3
Make PyDict_Next safe to use for loops that merely modify the values
Tim Peters <tim.peters@gmail.com>
parents:
15974
diff
changeset
|
531 * much larger than ma_used, meaning a lot of dict keys have been |
|
e1214f9e12a3
Make PyDict_Next safe to use for loops that merely modify the values
Tim Peters <tim.peters@gmail.com>
parents:
15974
diff
changeset
|
532 * deleted). |
|
e1214f9e12a3
Make PyDict_Next safe to use for loops that merely modify the values
Tim Peters <tim.peters@gmail.com>
parents:
15974
diff
changeset
|
533 */ |
|
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
|
534 if (mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2) { |
|
17535
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
535 if (dictresize(mp, mp->ma_used*2) != 0) |
|
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
536 return -1; |
|
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
537 } |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
538 return 0; |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
539 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
540 |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
541 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
|
542 PyDict_DelItem(PyObject *op, PyObject *key) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
543 { |
|
5505
47fa00877924
Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
544 register dictobject *mp; |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
545 register long hash; |
|
5505
47fa00877924
Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
546 register dictentry *ep; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
547 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
|
548 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
549 if (!PyDict_Check(op)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
550 PyErr_BadInternalCall(); |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
551 return -1; |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
552 } |
|
19904
58d50094bf80
Changed the dict implementation to take "string shortcuts" only when
Tim Peters <tim.peters@gmail.com>
parents:
19662
diff
changeset
|
553 if (!PyString_CheckExact(key) || |
|
22651
f3a416b178c4
Remove the CACHE_HASH and INTERN_STRINGS preprocessor symbols.
Tim Peters <tim.peters@gmail.com>
parents:
22625
diff
changeset
|
554 (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
555 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
|
556 if (hash == -1) |
|
a820dead06e6
Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents:
5005
diff
changeset
|
557 return -1; |
|
a820dead06e6
Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents:
5005
diff
changeset
|
558 } |
|
5505
47fa00877924
Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
559 mp = (dictobject *)op; |
|
14310
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
560 ep = (mp->ma_lookup)(mp, key, hash); |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
561 if (ep->me_value == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
562 PyErr_SetObject(PyExc_KeyError, key); |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
563 return -1; |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
564 } |
|
2053
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
1848
diff
changeset
|
565 old_key = ep->me_key; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
566 Py_INCREF(dummy); |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
567 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
|
568 old_value = ep->me_value; |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
569 ep->me_value = NULL; |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
570 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
|
571 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
|
572 Py_DECREF(old_key); |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
573 return 0; |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
574 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
575 |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
576 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
|
577 PyDict_Clear(PyObject *op) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
578 { |
|
17920
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
579 dictobject *mp; |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
580 dictentry *ep, *table; |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
581 int table_is_malloced; |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
582 int fill; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
583 dictentry small_copy[PyDict_MINSIZE]; |
|
17920
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
584 #ifdef Py_DEBUG |
|
2053
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
1848
diff
changeset
|
585 int i, n; |
|
17920
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
586 #endif |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
587 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
588 if (!PyDict_Check(op)) |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
589 return; |
|
5505
47fa00877924
Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
590 mp = (dictobject *)op; |
|
17920
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
591 #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
|
592 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
|
593 i = 0; |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
594 #endif |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
595 |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
596 table = mp->ma_table; |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
597 assert(table != NULL); |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
598 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
|
599 |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
600 /* 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
|
601 * 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
|
602 * (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
|
603 * 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
|
604 * clearing. |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
605 */ |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
606 fill = mp->ma_fill; |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
607 if (table_is_malloced) |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
608 EMPTY_TO_MINSIZE(mp); |
|
17920
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
609 |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
610 else if (fill > 0) { |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
611 /* 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
|
612 * 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
|
613 * another small table first. |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
614 */ |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
615 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
|
616 table = small_copy; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
617 EMPTY_TO_MINSIZE(mp); |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
618 } |
|
17920
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
619 /* 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
|
620 |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
621 /* 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
|
622 * 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
|
623 * 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
|
624 */ |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
625 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
|
626 #ifdef Py_DEBUG |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
627 assert(i < n); |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
628 ++i; |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
629 #endif |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
630 if (ep->me_key) { |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
631 --fill; |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
632 Py_DECREF(ep->me_key); |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
633 Py_XDECREF(ep->me_value); |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
634 } |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
635 #ifdef Py_DEBUG |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
636 else |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
637 assert(ep->me_value == NULL); |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
638 #endif |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
639 } |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
640 |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
641 if (table_is_malloced) |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
642 PyMem_DEL(table); |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
643 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
644 |
|
27901
55adf08a213a
cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents:
26818
diff
changeset
|
645 /* |
|
55adf08a213a
cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents:
26818
diff
changeset
|
646 * 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
|
647 * |
|
55adf08a213a
cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents:
26818
diff
changeset
|
648 * int i; |
|
55adf08a213a
cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents:
26818
diff
changeset
|
649 * PyObject *key, *value; |
|
55adf08a213a
cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents:
26818
diff
changeset
|
650 * 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
|
651 * 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
|
652 * 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
|
653 * } |
|
55adf08a213a
cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents:
26818
diff
changeset
|
654 * |
|
55adf08a213a
cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents:
26818
diff
changeset
|
655 * 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
|
656 * 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
|
657 * 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
|
658 * 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
|
659 */ |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
660 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
|
661 PyDict_Next(PyObject *op, int *ppos, PyObject **pkey, PyObject **pvalue) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
662 { |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
663 int i; |
|
5505
47fa00877924
Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
664 register dictobject *mp; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
665 if (!PyDict_Check(op)) |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
666 return 0; |
|
5505
47fa00877924
Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
667 mp = (dictobject *)op; |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
668 i = *ppos; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
669 if (i < 0) |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
670 return 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
|
671 while (i <= mp->ma_mask && mp->ma_table[i].me_value == NULL) |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
672 i++; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
673 *ppos = i+1; |
|
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
|
674 if (i > mp->ma_mask) |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
675 return 0; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
676 if (pkey) |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
677 *pkey = mp->ma_table[i].me_key; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
678 if (pvalue) |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
679 *pvalue = mp->ma_table[i].me_value; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
680 return 1; |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
681 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
682 |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
683 /* Methods */ |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
684 |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
685 static 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
|
686 dict_dealloc(register dictobject *mp) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
687 { |
|
5505
47fa00877924
Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
688 register dictentry *ep; |
|
17920
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
689 int 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
|
690 PyObject_GC_UnTrack(mp); |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11838
diff
changeset
|
691 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
|
692 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
|
693 if (ep->me_key) { |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
694 --fill; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
695 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
|
696 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
|
697 } |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
698 } |
|
17920
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
699 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
|
700 PyMem_DEL(mp->ma_table); |
|
20277
2983b54256c7
Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents:
19904
diff
changeset
|
701 mp->ob_type->tp_free((PyObject *)mp); |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11838
diff
changeset
|
702 Py_TRASHCAN_SAFE_END(mp) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
703 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
704 |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
705 static 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
|
706 dict_print(register dictobject *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
|
707 { |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
708 register int i; |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
709 register int any; |
|
7985
ab4f64561fba
Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents:
6490
diff
changeset
|
710 |
|
ab4f64561fba
Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents:
6490
diff
changeset
|
711 i = Py_ReprEnter((PyObject*)mp); |
|
ab4f64561fba
Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents:
6490
diff
changeset
|
712 if (i != 0) { |
|
ab4f64561fba
Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents:
6490
diff
changeset
|
713 if (i < 0) |
|
ab4f64561fba
Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents:
6490
diff
changeset
|
714 return i; |
|
ab4f64561fba
Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents:
6490
diff
changeset
|
715 fprintf(fp, "{...}"); |
|
ab4f64561fba
Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents:
6490
diff
changeset
|
716 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
|
717 } |
|
ab4f64561fba
Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents:
6490
diff
changeset
|
718 |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
719 fprintf(fp, "{"); |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
720 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
|
721 for (i = 0; i <= mp->ma_mask; i++) { |
|
18017
facff5f50fcc
Coredumpers from Michael Hudson, mutating dicts while printing or
Tim Peters <tim.peters@gmail.com>
parents:
18015
diff
changeset
|
722 dictentry *ep = mp->ma_table + i; |
|
facff5f50fcc
Coredumpers from Michael Hudson, mutating dicts while printing or
Tim Peters <tim.peters@gmail.com>
parents:
18015
diff
changeset
|
723 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
|
724 if (pvalue != NULL) { |
|
facff5f50fcc
Coredumpers from Michael Hudson, mutating dicts while printing or
Tim Peters <tim.peters@gmail.com>
parents:
18015
diff
changeset
|
725 /* 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
|
726 key format */ |
|
facff5f50fcc
Coredumpers from Michael Hudson, mutating dicts while printing or
Tim Peters <tim.peters@gmail.com>
parents:
18015
diff
changeset
|
727 Py_INCREF(pvalue); |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
728 if (any++ > 0) |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
729 fprintf(fp, ", "); |
|
7985
ab4f64561fba
Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents:
6490
diff
changeset
|
730 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
|
731 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
|
732 Py_ReprLeave((PyObject*)mp); |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
733 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
|
734 } |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
735 fprintf(fp, ": "); |
|
18019
ca7e2e1ae8df
Finish the dict->string coredump fix. Need sleep.
Tim Peters <tim.peters@gmail.com>
parents:
18017
diff
changeset
|
736 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
|
737 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
|
738 Py_ReprLeave((PyObject*)mp); |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
739 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
|
740 } |
|
18017
facff5f50fcc
Coredumpers from Michael Hudson, mutating dicts while printing or
Tim Peters <tim.peters@gmail.com>
parents:
18015
diff
changeset
|
741 Py_DECREF(pvalue); |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
742 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
743 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
744 fprintf(fp, "}"); |
|
7985
ab4f64561fba
Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents:
6490
diff
changeset
|
745 Py_ReprLeave((PyObject*)mp); |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
746 return 0; |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
747 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
748 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
749 static 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
|
750 dict_repr(dictobject *mp) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
751 { |
|
18131
85e3fa1eb836
dict_repr: Reuse one of the int vars (minor code simplification).
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
752 int i; |
|
18129
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
753 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
|
754 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
|
755 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
|
756 |
|
18129
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
757 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
|
758 if (i != 0) { |
|
18129
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
759 return i > 0 ? PyString_FromString("{...}") : NULL; |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
760 } |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
761 |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
762 if (mp->ma_used == 0) { |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
763 result = PyString_FromString("{}"); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
764 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
|
765 } |
|
ab4f64561fba
Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents:
6490
diff
changeset
|
766 |
|
18129
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
767 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
|
768 if (pieces == NULL) |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
769 goto Done; |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
770 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
771 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
|
772 if (colon == NULL) |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
773 goto Done; |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
774 |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
775 /* 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
|
776 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
|
777 i = 0; |
|
85e3fa1eb836
dict_repr: Reuse one of the int vars (minor code simplification).
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
778 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
|
779 int status; |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
780 /* 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
|
781 Py_INCREF(value); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
782 s = PyObject_Repr(key); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
783 PyString_Concat(&s, colon); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
784 PyString_ConcatAndDel(&s, PyObject_Repr(value)); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
785 Py_DECREF(value); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
786 if (s == NULL) |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
787 goto Done; |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
788 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
|
789 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
|
790 if (status < 0) |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
791 goto Done; |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
792 } |
|
18129
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
793 |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
794 /* 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
|
795 assert(PyList_GET_SIZE(pieces) > 0); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
796 s = PyString_FromString("{"); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
797 if (s == NULL) |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
798 goto Done; |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
799 temp = PyList_GET_ITEM(pieces, 0); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
800 PyString_ConcatAndDel(&s, temp); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
801 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
|
802 if (s == NULL) |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
803 goto Done; |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
804 |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
805 s = PyString_FromString("}"); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
806 if (s == NULL) |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
807 goto Done; |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
808 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
809 PyString_ConcatAndDel(&temp, s); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
810 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
|
811 if (temp == NULL) |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
812 goto Done; |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
813 |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
814 /* Paste them all together with ", " between. */ |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
815 s = PyString_FromString(", "); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
816 if (s == NULL) |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
817 goto Done; |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
818 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
|
819 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
|
820 |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
821 Done: |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
822 Py_XDECREF(pieces); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
823 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
|
824 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
|
825 return result; |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
826 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
827 |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
828 static 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
|
829 dict_length(dictobject *mp) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
830 { |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
831 return mp->ma_used; |
|
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 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
834 static 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
|
835 dict_subscript(dictobject *mp, register PyObject *key) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
836 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
837 PyObject *v; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1330
diff
changeset
|
838 long hash; |
|
17920
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
839 assert(mp->ma_table != NULL); |
|
19904
58d50094bf80
Changed the dict implementation to take "string shortcuts" only when
Tim Peters <tim.peters@gmail.com>
parents:
19662
diff
changeset
|
840 if (!PyString_CheckExact(key) || |
|
22651
f3a416b178c4
Remove the CACHE_HASH and INTERN_STRINGS preprocessor symbols.
Tim Peters <tim.peters@gmail.com>
parents:
22625
diff
changeset
|
841 (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
842 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
|
843 if (hash == -1) |
|
a820dead06e6
Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents:
5005
diff
changeset
|
844 return NULL; |
|
a820dead06e6
Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents:
5005
diff
changeset
|
845 } |
|
14310
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
846 v = (mp->ma_lookup)(mp, key, hash) -> me_value; |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
847 if (v == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
848 PyErr_SetObject(PyExc_KeyError, key); |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
849 else |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
850 Py_INCREF(v); |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
851 return v; |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
852 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
853 |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
854 static 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
|
855 dict_ass_sub(dictobject *mp, PyObject *v, PyObject *w) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
856 { |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
857 if (w == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
858 return PyDict_DelItem((PyObject *)mp, v); |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
859 else |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
860 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
|
861 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
862 |
|
5505
47fa00877924
Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
863 static PyMappingMethods dict_as_mapping = { |
|
47fa00877924
Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
864 (inquiry)dict_length, /*mp_length*/ |
|
47fa00877924
Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
865 (binaryfunc)dict_subscript, /*mp_subscript*/ |
|
47fa00877924
Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
866 (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
|
867 }; |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
868 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
869 static PyObject * |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19150
diff
changeset
|
870 dict_keys(register dictobject *mp) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
871 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
872 register PyObject *v; |
|
17535
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
873 register int i, j, n; |
|
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
874 |
|
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
875 again: |
|
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
876 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
|
877 v = PyList_New(n); |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
878 if (v == NULL) |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
879 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
|
880 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
|
881 /* 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
|
882 * 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
|
883 */ |
|
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
884 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
|
885 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
|
886 } |
|
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
|
887 for (i = 0, j = 0; i <= mp->ma_mask; i++) { |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
888 if (mp->ma_table[i].me_value != NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
889 PyObject *key = mp->ma_table[i].me_key; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
890 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
|
891 PyList_SET_ITEM(v, j, key); |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
892 j++; |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
893 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
894 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
895 return v; |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
896 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
897 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
898 static PyObject * |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19150
diff
changeset
|
899 dict_values(register dictobject *mp) |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
900 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
901 register PyObject *v; |
|
17535
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
902 register int i, j, n; |
|
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
903 |
|
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
904 again: |
|
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
905 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
|
906 v = PyList_New(n); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
907 if (v == NULL) |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
908 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
|
909 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
|
910 /* 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
|
911 * 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
|
912 */ |
|
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
913 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
|
914 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
|
915 } |
|
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
|
916 for (i = 0, j = 0; i <= mp->ma_mask; i++) { |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
917 if (mp->ma_table[i].me_value != NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
918 PyObject *value = mp->ma_table[i].me_value; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
919 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
|
920 PyList_SET_ITEM(v, j, value); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
921 j++; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
922 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
923 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
924 return v; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
925 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
926 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
927 static PyObject * |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19150
diff
changeset
|
928 dict_items(register dictobject *mp) |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
929 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
930 register PyObject *v; |
|
17535
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
931 register int i, j, n; |
|
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
932 PyObject *item, *key, *value; |
|
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
933 |
|
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
934 /* 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
|
935 * 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
|
936 * 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
|
937 */ |
|
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
938 again: |
|
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
939 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
|
940 v = PyList_New(n); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
941 if (v == NULL) |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
942 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
|
943 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
|
944 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
|
945 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
|
946 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
|
947 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
|
948 } |
|
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
949 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
|
950 } |
|
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
951 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
|
952 /* 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
|
953 * 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
|
954 */ |
|
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
955 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
|
956 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
|
957 } |
|
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
958 /* Nothing we do below makes any function calls. */ |
|
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
|
959 for (i = 0, j = 0; i <= mp->ma_mask; i++) { |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
960 if (mp->ma_table[i].me_value != 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
|
961 key = mp->ma_table[i].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
|
962 value = mp->ma_table[i].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
|
963 item = PyList_GET_ITEM(v, j); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
964 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
|
965 PyTuple_SET_ITEM(item, 0, key); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
966 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
|
967 PyTuple_SET_ITEM(item, 1, value); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
968 j++; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
969 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
970 } |
|
17535
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
971 assert(j == n); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
972 return v; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
973 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
974 |
|
5658
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
975 static PyObject * |
|
26583
8991f78817c7
SF 548651: Fix the METH_CLASS implementation.
Tim Peters <tim.peters@gmail.com>
parents:
26562
diff
changeset
|
976 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
|
977 { |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
978 PyObject *seq; |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
979 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
|
980 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
|
981 PyObject *key; |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
982 PyObject *d; |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
983 int status; |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
984 |
|
26818
e094a03a80ff
SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents:
26599
diff
changeset
|
985 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
|
986 return NULL; |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
987 |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
988 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
|
989 if (d == NULL) |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
990 return NULL; |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
991 |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
992 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
|
993 if (it == NULL){ |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
994 Py_DECREF(d); |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
995 return NULL; |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
996 } |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
997 |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
998 for (;;) { |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
999 key = PyIter_Next(it); |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
1000 if (key == NULL) { |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
1001 if (PyErr_Occurred()) |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
1002 goto Fail; |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
1003 break; |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
1004 } |
|
26562
851c629205d2
Remove assumption that cls is a subclass of dict.
Raymond Hettinger <python@rcn.com>
parents:
26529
diff
changeset
|
1005 status = PyObject_SetItem(d, key, value); |
|
26437
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
1006 Py_DECREF(key); |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
1007 if (status < 0) |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
1008 goto Fail; |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
1009 } |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
1010 |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
1011 Py_DECREF(it); |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
1012 return d; |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
1013 |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
1014 Fail: |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
1015 Py_DECREF(it); |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
1016 Py_DECREF(d); |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
1017 return NULL; |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
1018 } |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
1019 |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
1020 static PyObject * |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19150
diff
changeset
|
1021 dict_update(PyObject *mp, PyObject *other) |
|
5658
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1022 { |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1023 if (PyDict_Update(mp, other) < 0) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1024 return NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1025 Py_INCREF(Py_None); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1026 return Py_None; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1027 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1028 |
|
19150
3f34f724f014
Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1029 /* Update unconditionally replaces existing items. |
|
3f34f724f014
Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1030 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
|
1031 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
|
1032 |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1033 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
|
1034 |
|
21348
12310d643d8d
SF bug #491415 PyDict_UpdateFromSeq2() unused
Tim Peters <tim.peters@gmail.com>
parents:
21155
diff
changeset
|
1035 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
|
1036 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
|
1037 */ |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1038 |
|
21348
12310d643d8d
SF bug #491415 PyDict_UpdateFromSeq2() unused
Tim Peters <tim.peters@gmail.com>
parents:
21155
diff
changeset
|
1039 int |
|
20683
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1040 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
|
1041 { |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1042 PyObject *it; /* iter(seq2) */ |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1043 int i; /* index into seq2 of current element */ |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1044 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
|
1045 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
|
1046 |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1047 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
|
1048 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
|
1049 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
|
1050 |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1051 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
|
1052 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
|
1053 return -1; |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1054 |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1055 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
|
1056 PyObject *key, *value; |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1057 int n; |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1058 |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1059 fast = NULL; |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1060 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
|
1061 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
|
1062 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
|
1063 goto Fail; |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1064 break; |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1065 } |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1066 |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1067 /* 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
|
1068 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
|
1069 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
|
1070 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
|
1071 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
|
1072 "cannot convert dictionary update " |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1073 "sequence element #%d to a sequence", |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1074 i); |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1075 goto Fail; |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1076 } |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1077 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
|
1078 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
|
1079 PyErr_Format(PyExc_ValueError, |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1080 "dictionary update sequence element #%d " |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1081 "has length %d; 2 is required", |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1082 i, n); |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1083 goto Fail; |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1084 } |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1085 |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1086 /* 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
|
1087 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
|
1088 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
|
1089 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
|
1090 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
|
1091 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
|
1092 goto Fail; |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1093 } |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1094 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
|
1095 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
|
1096 } |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1097 |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1098 i = 0; |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1099 goto Return; |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1100 Fail: |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1101 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
|
1102 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
|
1103 i = -1; |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1104 Return: |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1105 Py_DECREF(it); |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1106 return i; |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1107 } |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1108 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1109 int |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1110 PyDict_Update(PyObject *a, PyObject *b) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1111 { |
|
19150
3f34f724f014
Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1112 return PyDict_Merge(a, b, 1); |
|
3f34f724f014
Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1113 } |
|
3f34f724f014
Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1114 |
|
3f34f724f014
Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1115 int |
|
3f34f724f014
Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1116 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
|
1117 { |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1118 register PyDictObject *mp, *other; |
|
5658
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1119 register int i; |
|
17535
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
1120 dictentry *entry; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1121 |
|
18283
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1122 /* 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
|
1123 * 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
|
1124 * 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
|
1125 * 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
|
1126 */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1127 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
|
1128 PyErr_BadInternalCall(); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1129 return -1; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1130 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1131 mp = (dictobject*)a; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1132 if (PyDict_Check(b)) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1133 other = (dictobject*)b; |
|
18283
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1134 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
|
1135 /* 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
|
1136 return 0; |
|
18283
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1137 /* 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
|
1138 * 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
|
1139 * 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
|
1140 */ |
|
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1141 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) { |
|
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1142 if (dictresize(mp, (mp->ma_used + other->ma_used)*3/2) != 0) |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1143 return -1; |
|
18283
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1144 } |
|
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1145 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
|
1146 entry = &other->ma_table[i]; |
|
19150
3f34f724f014
Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1147 if (entry->me_value != NULL && |
|
3f34f724f014
Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1148 (override || |
|
3f34f724f014
Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1149 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
|
1150 Py_INCREF(entry->me_key); |
|
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1151 Py_INCREF(entry->me_value); |
|
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1152 insertdict(mp, entry->me_key, entry->me_hash, |
|
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1153 entry->me_value); |
|
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1154 } |
|
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1155 } |
|
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1156 } |
|
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1157 else { |
|
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1158 /* 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
|
1159 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
|
1160 PyObject *iter; |
|
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1161 PyObject *key, *value; |
|
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1162 int status; |
|
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1163 |
|
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1164 if (keys == NULL) |
|
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1165 /* 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
|
1166 * 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
|
1167 * 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
|
1168 * 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
|
1169 */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1170 return -1; |
|
18283
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1171 |
|
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1172 iter = PyObject_GetIter(keys); |
|
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1173 Py_DECREF(keys); |
|
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1174 if (iter == NULL) |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1175 return -1; |
|
18283
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1176 |
|
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1177 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
|
1178 if (!override && PyDict_GetItem(a, key) != NULL) { |
|
3f34f724f014
Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1179 Py_DECREF(key); |
|
3f34f724f014
Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1180 continue; |
|
3f34f724f014
Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1181 } |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1182 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
|
1183 if (value == NULL) { |
|
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1184 Py_DECREF(iter); |
|
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1185 Py_DECREF(key); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1186 return -1; |
|
18283
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1187 } |
|
19150
3f34f724f014
Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1188 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
|
1189 Py_DECREF(key); |
|
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1190 Py_DECREF(value); |
|
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1191 if (status < 0) { |
|
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1192 Py_DECREF(iter); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1193 return -1; |
|
18283
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1194 } |
|
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1195 } |
|
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1196 Py_DECREF(iter); |
|
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1197 if (PyErr_Occurred()) |
|
1f1d2c50b3c7
dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents:
18131
diff
changeset
|
1198 /* Iterator completed, via error */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1199 return -1; |
|
5658
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1200 } |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1201 return 0; |
|
5658
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1202 } |
|
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1203 |
|
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1204 static PyObject * |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19150
diff
changeset
|
1205 dict_copy(register dictobject *mp) |
|
5658
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1206 { |
|
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
|
1207 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
|
1208 } |
|
e6214ad2a60c
Add PyDict_Copy() function to C API for dicts. It returns a new
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11999
diff
changeset
|
1209 |
|
e6214ad2a60c
Add PyDict_Copy() function to C API for dicts. It returns a new
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11999
diff
changeset
|
1210 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
|
1211 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
|
1212 { |
|
e6214ad2a60c
Add PyDict_Copy() function to C API for dicts. It returns a new
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11999
diff
changeset
|
1213 register dictobject *mp; |
|
5658
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1214 register int i; |
|
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1215 dictobject *copy; |
|
17535
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
1216 dictentry *entry; |
|
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
|
1217 |
|
e6214ad2a60c
Add PyDict_Copy() function to C API for dicts. It returns a new
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11999
diff
changeset
|
1218 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
|
1219 PyErr_BadInternalCall(); |
|
5658
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1220 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
|
1221 } |
|
e6214ad2a60c
Add PyDict_Copy() function to C API for dicts. It returns a new
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11999
diff
changeset
|
1222 mp = (dictobject *)o; |
|
5658
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1223 copy = (dictobject *)PyDict_New(); |
|
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1224 if (copy == NULL) |
|
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1225 return NULL; |
|
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1226 if (mp->ma_used > 0) { |
|
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1227 if (dictresize(copy, mp->ma_used*3/2) != 0) |
|
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1228 return NULL; |
|
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
|
1229 for (i = 0; i <= mp->ma_mask; i++) { |
|
5658
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1230 entry = &mp->ma_table[i]; |
|
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1231 if (entry->me_value != NULL) { |
|
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1232 Py_INCREF(entry->me_key); |
|
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1233 Py_INCREF(entry->me_value); |
|
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1234 insertdict(copy, entry->me_key, entry->me_hash, |
|
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1235 entry->me_value); |
|
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1236 } |
|
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1237 } |
|
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1238 } |
|
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1239 return (PyObject *)copy; |
|
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1240 } |
|
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1241 |
|
1398
1d841587aede
Added getmappingsize(). (Needed by previous checkin of posixmodule.c)
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
1242 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
|
1243 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
|
1244 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
1245 if (mp == NULL || !PyDict_Check(mp)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
1246 PyErr_BadInternalCall(); |
|
1413
c914b10bf2ed
* timemodule.c: Add hack for Solaris 2.
Guido van Rossum <guido@python.org>
parents:
1406
diff
changeset
|
1247 return 0; |
|
1398
1d841587aede
Added getmappingsize(). (Needed by previous checkin of posixmodule.c)
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
1248 } |
|
5505
47fa00877924
Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1249 return ((dictobject *)mp)->ma_used; |
|
1398
1d841587aede
Added getmappingsize(). (Needed by previous checkin of posixmodule.c)
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
1250 } |
|
1d841587aede
Added getmappingsize(). (Needed by previous checkin of posixmodule.c)
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
1251 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
1252 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
|
1253 PyDict_Keys(PyObject *mp) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1254 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
1255 if (mp == NULL || !PyDict_Check(mp)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
1256 PyErr_BadInternalCall(); |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1257 return NULL; |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1258 } |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19150
diff
changeset
|
1259 return dict_keys((dictobject *)mp); |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1260 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1261 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
1262 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
|
1263 PyDict_Values(PyObject *mp) |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1264 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
1265 if (mp == NULL || !PyDict_Check(mp)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
1266 PyErr_BadInternalCall(); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1267 return NULL; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1268 } |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19150
diff
changeset
|
1269 return dict_values((dictobject *)mp); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1270 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1271 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
1272 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
|
1273 PyDict_Items(PyObject *mp) |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1274 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
1275 if (mp == NULL || !PyDict_Check(mp)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
1276 PyErr_BadInternalCall(); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1277 return NULL; |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1278 } |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19150
diff
changeset
|
1279 return dict_items((dictobject *)mp); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1280 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1281 |
|
4586
b3d2990d8a4a
Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1282 /* 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
|
1283 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
|
1284 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
|
1285 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
|
1286 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
|
1287 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
|
1288 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
|
1289 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
1290 static 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
|
1291 characterize(dictobject *a, dictobject *b, PyObject **pval) |
|
4586
b3d2990d8a4a
Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1292 { |
|
17756
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1293 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
|
1294 PyObject *aval = NULL; /* a[akey] */ |
| 15974 | 1295 int i, cmp; |
|
4586
b3d2990d8a4a
Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1296 |
|
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
|
1297 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
|
1298 PyObject *thiskey, *thisaval, *thisbval; |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1299 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
|
1300 continue; |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1301 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
|
1302 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
|
1303 if (akey != NULL) { |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1304 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
|
1305 if (cmp < 0) { |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1306 Py_DECREF(thiskey); |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1307 goto Fail; |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1308 } |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1309 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
|
1310 i > a->ma_mask || |
|
17756
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1311 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
|
1312 { |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1313 /* 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
|
1314 * 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
|
1315 * find its associated value anymore; or |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1316 * 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
|
1317 * a[thiskey] entry. |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1318 */ |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1319 Py_DECREF(thiskey); |
|
4586
b3d2990d8a4a
Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1320 continue; |
| 15974 | 1321 } |
|
17756
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1322 } |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1323 |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1324 /* 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
|
1325 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
|
1326 assert(thisaval); |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1327 Py_INCREF(thisaval); /* keep alive */ |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1328 thisbval = PyDict_GetItem((PyObject *)b, thiskey); |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1329 if (thisbval == NULL) |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1330 cmp = 0; |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1331 else { |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1332 /* both dicts have thiskey: same values? */ |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1333 cmp = PyObject_RichCompareBool( |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1334 thisaval, thisbval, Py_EQ); |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1335 if (cmp < 0) { |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1336 Py_DECREF(thiskey); |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1337 Py_DECREF(thisaval); |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1338 goto Fail; |
|
4586
b3d2990d8a4a
Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1339 } |
|
b3d2990d8a4a
Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1340 } |
|
17756
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1341 if (cmp == 0) { |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1342 /* New winner. */ |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1343 Py_XDECREF(akey); |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1344 Py_XDECREF(aval); |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1345 akey = thiskey; |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1346 aval = thisaval; |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1347 } |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1348 else { |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1349 Py_DECREF(thiskey); |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1350 Py_DECREF(thisaval); |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1351 } |
|
4586
b3d2990d8a4a
Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1352 } |
|
17756
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1353 *pval = aval; |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1354 return akey; |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1355 |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1356 Fail: |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1357 Py_XDECREF(akey); |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1358 Py_XDECREF(aval); |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1359 *pval = NULL; |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1360 return NULL; |
|
4586
b3d2990d8a4a
Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1361 } |
|
b3d2990d8a4a
Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1362 |
|
b3d2990d8a4a
Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1363 static 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
|
1364 dict_compare(dictobject *a, dictobject *b) |
|
4586
b3d2990d8a4a
Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1365 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
1366 PyObject *adiff, *bdiff, *aval, *bval; |
|
4586
b3d2990d8a4a
Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1367 int res; |
|
b3d2990d8a4a
Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1368 |
|
b3d2990d8a4a
Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1369 /* Compare lengths first */ |
|
b3d2990d8a4a
Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1370 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
|
1371 return -1; /* a is shorter */ |
|
b3d2990d8a4a
Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1372 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
|
1373 return 1; /* b is shorter */ |
|
17756
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1374 |
|
4586
b3d2990d8a4a
Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1375 /* Same length -- check all keys */ |
|
17756
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1376 bdiff = bval = NULL; |
|
4586
b3d2990d8a4a
Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1377 adiff = characterize(a, b, &aval); |
|
17756
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1378 if (adiff == NULL) { |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1379 assert(!aval); |
|
17771
fb103e3da2ae
Repair typo in comment.
Tim Peters <tim.peters@gmail.com>
parents:
17756
diff
changeset
|
1380 /* 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
|
1381 * must be equal. |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1382 */ |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1383 res = PyErr_Occurred() ? -1 : 0; |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1384 goto Finished; |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1385 } |
|
4586
b3d2990d8a4a
Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1386 bdiff = characterize(b, a, &bval); |
|
17756
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1387 if (bdiff == NULL && PyErr_Occurred()) { |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1388 assert(!bval); |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1389 res = -1; |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1390 goto Finished; |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1391 } |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1392 res = 0; |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1393 if (bdiff) { |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1394 /* 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
|
1395 * 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
|
1396 * 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
|
1397 */ |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1398 res = PyObject_Compare(adiff, bdiff); |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1399 } |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1400 if (res == 0 && bval != NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
1401 res = PyObject_Compare(aval, bval); |
|
17756
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1402 |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1403 Finished: |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1404 Py_XDECREF(adiff); |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1405 Py_XDECREF(bdiff); |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1406 Py_XDECREF(aval); |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1407 Py_XDECREF(bval); |
|
4586
b3d2990d8a4a
Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1408 return res; |
|
b3d2990d8a4a
Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1409 } |
|
b3d2990d8a4a
Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1410 |
|
17704
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1411 /* 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
|
1412 * 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
|
1413 * Uses only Py_EQ comparison. |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1414 */ |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1415 static int |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1416 dict_equal(dictobject *a, dictobject *b) |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1417 { |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1418 int i; |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1419 |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1420 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
|
1421 /* 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
|
1422 return 0; |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
1423 |
|
17704
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1424 /* 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
|
1425 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
|
1426 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
|
1427 if (aval != NULL) { |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1428 int cmp; |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1429 PyObject *bval; |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1430 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
|
1431 /* 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
|
1432 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
|
1433 Py_INCREF(aval); |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1434 bval = PyDict_GetItem((PyObject *)b, key); |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1435 if (bval == NULL) { |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1436 Py_DECREF(aval); |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1437 return 0; |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1438 } |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1439 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
|
1440 Py_DECREF(aval); |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1441 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
|
1442 return cmp; |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1443 } |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1444 } |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1445 return 1; |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1446 } |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1447 |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1448 static PyObject * |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1449 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
|
1450 { |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1451 int cmp; |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1452 PyObject *res; |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1453 |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1454 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
|
1455 res = Py_NotImplemented; |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1456 } |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1457 else if (op == Py_EQ || op == Py_NE) { |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1458 cmp = dict_equal((dictobject *)v, (dictobject *)w); |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1459 if (cmp < 0) |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1460 return NULL; |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1461 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
|
1462 } |
|
17775
dce1f53451a5
Restore dicts' tp_compare slot, and change dict_richcompare to say it
Tim Peters <tim.peters@gmail.com>
parents:
17771
diff
changeset
|
1463 else |
|
dce1f53451a5
Restore dicts' tp_compare slot, and change dict_richcompare to say it
Tim Peters <tim.peters@gmail.com>
parents:
17771
diff
changeset
|
1464 res = Py_NotImplemented; |
|
17704
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1465 Py_INCREF(res); |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1466 return res; |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1467 } |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1468 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
1469 static PyObject * |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19150
diff
changeset
|
1470 dict_has_key(register dictobject *mp, PyObject *key) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1471 { |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1472 long hash; |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1473 register long ok; |
|
19904
58d50094bf80
Changed the dict implementation to take "string shortcuts" only when
Tim Peters <tim.peters@gmail.com>
parents:
19662
diff
changeset
|
1474 if (!PyString_CheckExact(key) || |
|
22651
f3a416b178c4
Remove the CACHE_HASH and INTERN_STRINGS preprocessor symbols.
Tim Peters <tim.peters@gmail.com>
parents:
22625
diff
changeset
|
1475 (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
1476 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
|
1477 if (hash == -1) |
|
a820dead06e6
Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents:
5005
diff
changeset
|
1478 return NULL; |
|
a820dead06e6
Forget keeping track of whether a dictionary contains all interned
Guido van Rossum <guido@python.org>
parents:
5005
diff
changeset
|
1479 } |
|
17920
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
1480 ok = (mp->ma_lookup)(mp, key, hash)->me_value != NULL; |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22651
diff
changeset
|
1481 return PyBool_FromLong(ok); |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1482 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1483 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
1484 static 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
|
1485 dict_get(register dictobject *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
|
1486 { |
|
22dae2637e08
dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents:
6351
diff
changeset
|
1487 PyObject *key; |
|
6488
37c8d7f967c7
dict_get(): Fixed a couple of stupid mistakes which caused crashes.
Barry Warsaw <barry@python.org>
parents:
6395
diff
changeset
|
1488 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
|
1489 PyObject *val = NULL; |
|
22dae2637e08
dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents:
6351
diff
changeset
|
1490 long hash; |
|
22dae2637e08
dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents:
6351
diff
changeset
|
1491 |
|
26818
e094a03a80ff
SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents:
26599
diff
changeset
|
1492 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
|
1493 return NULL; |
|
22dae2637e08
dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents:
6351
diff
changeset
|
1494 |
|
19904
58d50094bf80
Changed the dict implementation to take "string shortcuts" only when
Tim Peters <tim.peters@gmail.com>
parents:
19662
diff
changeset
|
1495 if (!PyString_CheckExact(key) || |
|
22651
f3a416b178c4
Remove the CACHE_HASH and INTERN_STRINGS preprocessor symbols.
Tim Peters <tim.peters@gmail.com>
parents:
22625
diff
changeset
|
1496 (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
|
1497 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
|
1498 if (hash == -1) |
|
22dae2637e08
dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents:
6351
diff
changeset
|
1499 return NULL; |
|
22dae2637e08
dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents:
6351
diff
changeset
|
1500 } |
|
14310
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
1501 val = (mp->ma_lookup)(mp, key, hash)->me_value; |
|
6488
37c8d7f967c7
dict_get(): Fixed a couple of stupid mistakes which caused crashes.
Barry Warsaw <barry@python.org>
parents:
6395
diff
changeset
|
1502 |
|
6395
22dae2637e08
dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents:
6351
diff
changeset
|
1503 if (val == NULL) |
|
22dae2637e08
dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents:
6351
diff
changeset
|
1504 val = failobj; |
|
22dae2637e08
dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents:
6351
diff
changeset
|
1505 Py_INCREF(val); |
|
22dae2637e08
dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents:
6351
diff
changeset
|
1506 return val; |
|
22dae2637e08
dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents:
6351
diff
changeset
|
1507 } |
|
22dae2637e08
dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents:
6351
diff
changeset
|
1508 |
|
22dae2637e08
dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents:
6351
diff
changeset
|
1509 |
|
22dae2637e08
dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents:
6351
diff
changeset
|
1510 static PyObject * |
|
13892
2190b7599837
Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents:
13660
diff
changeset
|
1511 dict_setdefault(register dictobject *mp, PyObject *args) |
|
2190b7599837
Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents:
13660
diff
changeset
|
1512 { |
|
2190b7599837
Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents:
13660
diff
changeset
|
1513 PyObject *key; |
|
2190b7599837
Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents:
13660
diff
changeset
|
1514 PyObject *failobj = Py_None; |
|
2190b7599837
Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents:
13660
diff
changeset
|
1515 PyObject *val = NULL; |
|
2190b7599837
Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents:
13660
diff
changeset
|
1516 long hash; |
|
2190b7599837
Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents:
13660
diff
changeset
|
1517 |
|
26818
e094a03a80ff
SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents:
26599
diff
changeset
|
1518 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
|
1519 return NULL; |
|
2190b7599837
Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents:
13660
diff
changeset
|
1520 |
|
19904
58d50094bf80
Changed the dict implementation to take "string shortcuts" only when
Tim Peters <tim.peters@gmail.com>
parents:
19662
diff
changeset
|
1521 if (!PyString_CheckExact(key) || |
|
22651
f3a416b178c4
Remove the CACHE_HASH and INTERN_STRINGS preprocessor symbols.
Tim Peters <tim.peters@gmail.com>
parents:
22625
diff
changeset
|
1522 (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
|
1523 hash = PyObject_Hash(key); |
|
2190b7599837
Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents:
13660
diff
changeset
|
1524 if (hash == -1) |
|
2190b7599837
Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents:
13660
diff
changeset
|
1525 return NULL; |
|
2190b7599837
Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents:
13660
diff
changeset
|
1526 } |
|
14310
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
1527 val = (mp->ma_lookup)(mp, key, hash)->me_value; |
|
13892
2190b7599837
Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents:
13660
diff
changeset
|
1528 if (val == NULL) { |
|
2190b7599837
Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents:
13660
diff
changeset
|
1529 val = failobj; |
|
2190b7599837
Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents:
13660
diff
changeset
|
1530 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
|
1531 val = NULL; |
|
2190b7599837
Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents:
13660
diff
changeset
|
1532 } |
|
2190b7599837
Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents:
13660
diff
changeset
|
1533 Py_XINCREF(val); |
|
2190b7599837
Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents:
13660
diff
changeset
|
1534 return val; |
|
2190b7599837
Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents:
13660
diff
changeset
|
1535 } |
|
2190b7599837
Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents:
13660
diff
changeset
|
1536 |
|
2190b7599837
Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents:
13660
diff
changeset
|
1537 |
|
2190b7599837
Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents:
13660
diff
changeset
|
1538 static PyObject * |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19150
diff
changeset
|
1539 dict_clear(register dictobject *mp) |
|
5209
2ec9c933afb8
Add clear() method to dictionary objects.
Guido van Rossum <guido@python.org>
parents:
5059
diff
changeset
|
1540 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
1541 PyDict_Clear((PyObject *)mp); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
1542 Py_INCREF(Py_None); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
1543 return Py_None; |
|
5209
2ec9c933afb8
Add clear() method to dictionary objects.
Guido van Rossum <guido@python.org>
parents:
5059
diff
changeset
|
1544 } |
|
2ec9c933afb8
Add clear() method to dictionary objects.
Guido van Rossum <guido@python.org>
parents:
5059
diff
changeset
|
1545 |
|
15520
8854d9d5add5
Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents:
15463
diff
changeset
|
1546 static PyObject * |
|
22994
196dfa7ed348
Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents:
22973
diff
changeset
|
1547 dict_pop(dictobject *mp, PyObject *key) |
|
196dfa7ed348
Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents:
22973
diff
changeset
|
1548 { |
|
196dfa7ed348
Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents:
22973
diff
changeset
|
1549 long hash; |
|
196dfa7ed348
Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents:
22973
diff
changeset
|
1550 dictentry *ep; |
|
196dfa7ed348
Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents:
22973
diff
changeset
|
1551 PyObject *old_value, *old_key; |
|
196dfa7ed348
Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents:
22973
diff
changeset
|
1552 |
|
196dfa7ed348
Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents:
22973
diff
changeset
|
1553 if (mp->ma_used == 0) { |
|
196dfa7ed348
Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents:
22973
diff
changeset
|
1554 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
|
1555 "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
|
1556 return NULL; |
|
196dfa7ed348
Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents:
22973
diff
changeset
|
1557 } |
|
196dfa7ed348
Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents:
22973
diff
changeset
|
1558 if (!PyString_CheckExact(key) || |
|
196dfa7ed348
Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents:
22973
diff
changeset
|
1559 (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
|
196dfa7ed348
Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents:
22973
diff
changeset
|
1560 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
|
1561 if (hash == -1) |
|
196dfa7ed348
Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents:
22973
diff
changeset
|
1562 return NULL; |
|
196dfa7ed348
Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents:
22973
diff
changeset
|
1563 } |
|
196dfa7ed348
Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents:
22973
diff
changeset
|
1564 ep = (mp->ma_lookup)(mp, key, hash); |
|
196dfa7ed348
Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents:
22973
diff
changeset
|
1565 if (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
|
1566 PyErr_SetObject(PyExc_KeyError, key); |
|
196dfa7ed348
Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents:
22973
diff
changeset
|
1567 return NULL; |
|
196dfa7ed348
Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents:
22973
diff
changeset
|
1568 } |
|
196dfa7ed348
Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents:
22973
diff
changeset
|
1569 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
|
1570 Py_INCREF(dummy); |
|
196dfa7ed348
Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents:
22973
diff
changeset
|
1571 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
|
1572 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
|
1573 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
|
1574 mp->ma_used--; |
|
196dfa7ed348
Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents:
22973
diff
changeset
|
1575 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
|
1576 return old_value; |
|
196dfa7ed348
Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents:
22973
diff
changeset
|
1577 } |
|
196dfa7ed348
Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents:
22973
diff
changeset
|
1578 |
|
196dfa7ed348
Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents:
22973
diff
changeset
|
1579 static PyObject * |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19150
diff
changeset
|
1580 dict_popitem(dictobject *mp) |
|
15520
8854d9d5add5
Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents:
15463
diff
changeset
|
1581 { |
|
8854d9d5add5
Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents:
15463
diff
changeset
|
1582 int i = 0; |
|
8854d9d5add5
Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents:
15463
diff
changeset
|
1583 dictentry *ep; |
|
8854d9d5add5
Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents:
15463
diff
changeset
|
1584 PyObject *res; |
|
8854d9d5add5
Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents:
15463
diff
changeset
|
1585 |
|
18015
0c1a04e98134
dict_popitem(): Repaired last-second 2.1 comment, which misidentified the
Tim Peters <tim.peters@gmail.com>
parents:
18014
diff
changeset
|
1586 /* 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
|
1587 * 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
|
1588 * 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
|
1589 * 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
|
1590 * 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
|
1591 * idiom is "while d: k, v = d.popitem()". so needing to throw the |
|
0c1a04e98134
dict_popitem(): Repaired last-second 2.1 comment, which misidentified the
Tim Peters <tim.peters@gmail.com>
parents:
18014
diff
changeset
|
1592 * tuple away if the dict *is* empty isn't a significant |
|
0c1a04e98134
dict_popitem(): Repaired last-second 2.1 comment, which misidentified the
Tim Peters <tim.peters@gmail.com>
parents:
18014
diff
changeset
|
1593 * 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
|
1594 */ |
|
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
1595 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
|
1596 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
|
1597 return NULL; |
|
17536
d2da681ccb54
Tim pointed out a remaining vulnerability in popitem(): the
Guido van Rossum <guido@python.org>
parents:
17535
diff
changeset
|
1598 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
|
1599 Py_DECREF(res); |
|
d2da681ccb54
Tim pointed out a remaining vulnerability in popitem(): the
Guido van Rossum <guido@python.org>
parents:
17535
diff
changeset
|
1600 PyErr_SetString(PyExc_KeyError, |
|
d2da681ccb54
Tim pointed out a remaining vulnerability in popitem(): the
Guido van Rossum <guido@python.org>
parents:
17535
diff
changeset
|
1601 "popitem(): dictionary is empty"); |
|
d2da681ccb54
Tim pointed out a remaining vulnerability in popitem(): the
Guido van Rossum <guido@python.org>
parents:
17535
diff
changeset
|
1602 return NULL; |
|
d2da681ccb54
Tim pointed out a remaining vulnerability in popitem(): the
Guido van Rossum <guido@python.org>
parents:
17535
diff
changeset
|
1603 } |
|
15520
8854d9d5add5
Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents:
15463
diff
changeset
|
1604 /* 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
|
1605 * 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
|
1606 * 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
|
1607 * Else slot 0 is being used to hold a search finger, |
|
8854d9d5add5
Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents:
15463
diff
changeset
|
1608 * and we use its hash value as the first index to look. |
|
8854d9d5add5
Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents:
15463
diff
changeset
|
1609 */ |
|
8854d9d5add5
Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents:
15463
diff
changeset
|
1610 ep = &mp->ma_table[0]; |
|
8854d9d5add5
Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents:
15463
diff
changeset
|
1611 if (ep->me_value == NULL) { |
|
8854d9d5add5
Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents:
15463
diff
changeset
|
1612 i = (int)ep->me_hash; |
|
17920
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
1613 /* 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
|
1614 * 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
|
1615 * 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
|
1616 * 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
|
1617 */ |
|
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
|
1618 if (i > mp->ma_mask || i < 1) |
|
15520
8854d9d5add5
Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents:
15463
diff
changeset
|
1619 i = 1; /* skip slot 0 */ |
|
8854d9d5add5
Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents:
15463
diff
changeset
|
1620 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
|
1621 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
|
1622 if (i > mp->ma_mask) |
|
15520
8854d9d5add5
Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents:
15463
diff
changeset
|
1623 i = 1; |
|
8854d9d5add5
Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents:
15463
diff
changeset
|
1624 } |
|
8854d9d5add5
Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents:
15463
diff
changeset
|
1625 } |
|
17535
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
1626 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
|
1627 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
|
1628 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
|
1629 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
|
1630 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
|
1631 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
|
1632 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
|
1633 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
|
1634 return res; |
|
8854d9d5add5
Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents:
15463
diff
changeset
|
1635 } |
|
8854d9d5add5
Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents:
15463
diff
changeset
|
1636 |
|
12990
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
1637 static int |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
1638 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
|
1639 { |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
1640 int i = 0, err; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
1641 PyObject *pk; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
1642 PyObject *pv; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
1643 |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
1644 while (PyDict_Next(op, &i, &pk, &pv)) { |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
1645 err = visit(pk, arg); |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
1646 if (err) |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
1647 return err; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
1648 err = visit(pv, arg); |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
1649 if (err) |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
1650 return err; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
1651 } |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
1652 return 0; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
1653 } |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
1654 |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
1655 static int |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
1656 dict_tp_clear(PyObject *op) |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
1657 { |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
1658 PyDict_Clear(op); |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
1659 return 0; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
1660 } |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
1661 |
|
15560
143646bafd1b
Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents:
15554
diff
changeset
|
1662 |
|
24280
1598a1e473e2
staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24259
diff
changeset
|
1663 static PyObject *dictiter_new(dictobject *, binaryfunc); |
|
17644
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1664 |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1665 static PyObject * |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1666 select_key(PyObject *key, PyObject *value) |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1667 { |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1668 Py_INCREF(key); |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1669 return key; |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1670 } |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1671 |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1672 static PyObject * |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1673 select_value(PyObject *key, PyObject *value) |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1674 { |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1675 Py_INCREF(value); |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1676 return value; |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1677 } |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1678 |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1679 static PyObject * |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1680 select_item(PyObject *key, PyObject *value) |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1681 { |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1682 PyObject *res = PyTuple_New(2); |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1683 |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1684 if (res != NULL) { |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1685 Py_INCREF(key); |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1686 Py_INCREF(value); |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1687 PyTuple_SET_ITEM(res, 0, key); |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1688 PyTuple_SET_ITEM(res, 1, value); |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1689 } |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1690 return res; |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1691 } |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1692 |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1693 static PyObject * |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19150
diff
changeset
|
1694 dict_iterkeys(dictobject *dict) |
|
17644
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1695 { |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1696 return dictiter_new(dict, select_key); |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1697 } |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1698 |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1699 static PyObject * |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19150
diff
changeset
|
1700 dict_itervalues(dictobject *dict) |
|
17644
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1701 { |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1702 return dictiter_new(dict, select_value); |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1703 } |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1704 |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1705 static PyObject * |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19150
diff
changeset
|
1706 dict_iteritems(dictobject *dict) |
|
17644
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1707 { |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1708 return dictiter_new(dict, select_item); |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1709 } |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1710 |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1711 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
1712 PyDoc_STRVAR(has_key__doc__, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
1713 "D.has_key(k) -> 1 if D has a key k, else 0"); |
|
15560
143646bafd1b
Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents:
15554
diff
changeset
|
1714 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
1715 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
|
1716 "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
|
1717 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
1718 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
|
1719 "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
|
1720 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
1721 PyDoc_STRVAR(pop__doc__, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
1722 "D.pop(k) -> v, remove specified key and return the corresponding value"); |
|
22994
196dfa7ed348
Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents:
22973
diff
changeset
|
1723 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
1724 PyDoc_STRVAR(popitem__doc__, |
|
15560
143646bafd1b
Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents:
15554
diff
changeset
|
1725 "D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
1726 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
|
1727 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
1728 PyDoc_STRVAR(keys__doc__, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
1729 "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
|
1730 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
1731 PyDoc_STRVAR(items__doc__, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
1732 "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
|
1733 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
1734 PyDoc_STRVAR(values__doc__, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
1735 "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
|
1736 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
1737 PyDoc_STRVAR(update__doc__, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
1738 "D.update(E) -> None. Update D from E: for k in E.keys(): D[k] = E[k]"); |
|
15560
143646bafd1b
Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents:
15554
diff
changeset
|
1739 |
|
26437
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
1740 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
|
1741 "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
|
1742 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
|
1743 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
1744 PyDoc_STRVAR(clear__doc__, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
1745 "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
|
1746 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
1747 PyDoc_STRVAR(copy__doc__, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
1748 "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
|
1749 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
1750 PyDoc_STRVAR(iterkeys__doc__, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
1751 "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
|
1752 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
1753 PyDoc_STRVAR(itervalues__doc__, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
1754 "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
|
1755 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
1756 PyDoc_STRVAR(iteritems__doc__, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
1757 "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
|
1758 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
1759 static PyMethodDef mapp_methods[] = { |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19150
diff
changeset
|
1760 {"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
|
1761 has_key__doc__}, |
|
143646bafd1b
Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents:
15554
diff
changeset
|
1762 {"get", (PyCFunction)dict_get, METH_VARARGS, |
|
143646bafd1b
Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents:
15554
diff
changeset
|
1763 get__doc__}, |
|
143646bafd1b
Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents:
15554
diff
changeset
|
1764 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS, |
|
143646bafd1b
Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents:
15554
diff
changeset
|
1765 setdefault_doc__}, |
|
22994
196dfa7ed348
Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents:
22973
diff
changeset
|
1766 {"pop", (PyCFunction)dict_pop, METH_O, |
|
26366
08b720cd387e
Patch #642500 with slight modifications: allow keyword arguments in
Just van Rossum <just@letterror.com>
parents:
25115
diff
changeset
|
1767 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
|
1768 {"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
|
1769 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
|
1770 {"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
|
1771 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
|
1772 {"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
|
1773 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
|
1774 {"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
|
1775 values__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
|
1776 {"update", (PyCFunction)dict_update, METH_O, |
|
15560
143646bafd1b
Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents:
15554
diff
changeset
|
1777 update__doc__}, |
|
26437
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
1778 {"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
|
1779 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
|
1780 {"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
|
1781 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
|
1782 {"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
|
1783 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
|
1784 {"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
|
1785 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
|
1786 {"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
|
1787 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
|
1788 {"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
|
1789 iteritems__doc__}, |
|
15560
143646bafd1b
Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents:
15554
diff
changeset
|
1790 {NULL, NULL} /* sentinel */ |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1791 }; |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1792 |
|
17594
bf94af396b1e
Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents:
17536
diff
changeset
|
1793 static int |
|
bf94af396b1e
Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents:
17536
diff
changeset
|
1794 dict_contains(dictobject *mp, PyObject *key) |
|
bf94af396b1e
Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents:
17536
diff
changeset
|
1795 { |
|
bf94af396b1e
Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents:
17536
diff
changeset
|
1796 long hash; |
|
bf94af396b1e
Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents:
17536
diff
changeset
|
1797 |
|
19904
58d50094bf80
Changed the dict implementation to take "string shortcuts" only when
Tim Peters <tim.peters@gmail.com>
parents:
19662
diff
changeset
|
1798 if (!PyString_CheckExact(key) || |
|
22651
f3a416b178c4
Remove the CACHE_HASH and INTERN_STRINGS preprocessor symbols.
Tim Peters <tim.peters@gmail.com>
parents:
22625
diff
changeset
|
1799 (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
|
1800 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
|
1801 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
|
1802 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
|
1803 } |
|
17920
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
1804 return (mp->ma_lookup)(mp, key, hash)->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
|
1805 } |
|
bf94af396b1e
Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents:
17536
diff
changeset
|
1806 |
|
bf94af396b1e
Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents:
17536
diff
changeset
|
1807 /* 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
|
1808 static PySequenceMethods dict_as_sequence = { |
|
bf94af396b1e
Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents:
17536
diff
changeset
|
1809 0, /* sq_length */ |
|
bf94af396b1e
Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents:
17536
diff
changeset
|
1810 0, /* sq_concat */ |
|
bf94af396b1e
Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents:
17536
diff
changeset
|
1811 0, /* sq_repeat */ |
|
bf94af396b1e
Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents:
17536
diff
changeset
|
1812 0, /* sq_item */ |
|
bf94af396b1e
Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents:
17536
diff
changeset
|
1813 0, /* sq_slice */ |
|
bf94af396b1e
Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents:
17536
diff
changeset
|
1814 0, /* sq_ass_item */ |
|
bf94af396b1e
Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents:
17536
diff
changeset
|
1815 0, /* sq_ass_slice */ |
|
bf94af396b1e
Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents:
17536
diff
changeset
|
1816 (objobjproc)dict_contains, /* sq_contains */ |
|
bf94af396b1e
Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents:
17536
diff
changeset
|
1817 0, /* sq_inplace_concat */ |
|
bf94af396b1e
Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents:
17536
diff
changeset
|
1818 0, /* sq_inplace_repeat */ |
|
bf94af396b1e
Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents:
17536
diff
changeset
|
1819 }; |
|
bf94af396b1e
Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents:
17536
diff
changeset
|
1820 |
|
17644
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1821 static PyObject * |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1822 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
|
1823 { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1824 PyObject *self; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1825 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1826 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
|
1827 self = type->tp_alloc(type, 0); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1828 if (self != NULL) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1829 PyDictObject *d = (PyDictObject *)self; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1830 /* 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
|
1831 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
|
1832 INIT_NONZERO_DICT_SLOTS(d); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1833 d->ma_lookup = lookdict_string; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1834 #ifdef SHOW_CONVERSION_COUNTS |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1835 ++created; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1836 #endif |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1837 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1838 return self; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1839 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1840 |
|
19658
2fa615dfc464
Make dictionary() a real constructor. Accepts at most one argument, "a
Tim Peters <tim.peters@gmail.com>
parents:
19581
diff
changeset
|
1841 static int |
|
2fa615dfc464
Make dictionary() a real constructor. Accepts at most one argument, "a
Tim Peters <tim.peters@gmail.com>
parents:
19581
diff
changeset
|
1842 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
|
1843 { |
|
2fa615dfc464
Make dictionary() a real constructor. Accepts at most one argument, "a
Tim Peters <tim.peters@gmail.com>
parents:
19581
diff
changeset
|
1844 PyObject *arg = NULL; |
|
20683
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1845 int result = 0; |
|
19658
2fa615dfc464
Make dictionary() a real constructor. Accepts at most one argument, "a
Tim Peters <tim.peters@gmail.com>
parents:
19581
diff
changeset
|
1846 |
|
26818
e094a03a80ff
SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents:
26599
diff
changeset
|
1847 if (!PyArg_UnpackTuple(args, "dict", 0, 1, &arg)) |
|
20683
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1848 result = -1; |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1849 |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1850 else if (arg != NULL) { |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1851 if (PyObject_HasAttrString(arg, "keys")) |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1852 result = PyDict_Merge(self, arg, 1); |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1853 else |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1854 result = PyDict_MergeFromSeq2(self, arg, 1); |
|
19658
2fa615dfc464
Make dictionary() a real constructor. Accepts at most one argument, "a
Tim Peters <tim.peters@gmail.com>
parents:
19581
diff
changeset
|
1855 } |
|
26366
08b720cd387e
Patch #642500 with slight modifications: allow keyword arguments in
Just van Rossum <just@letterror.com>
parents:
25115
diff
changeset
|
1856 if (result == 0 && kwds != NULL) |
|
08b720cd387e
Patch #642500 with slight modifications: allow keyword arguments in
Just van Rossum <just@letterror.com>
parents:
25115
diff
changeset
|
1857 result = PyDict_Merge(self, kwds, 1); |
|
20683
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1858 return result; |
|
19658
2fa615dfc464
Make dictionary() a real constructor. Accepts at most one argument, "a
Tim Peters <tim.peters@gmail.com>
parents:
19581
diff
changeset
|
1859 } |
|
2fa615dfc464
Make dictionary() a real constructor. Accepts at most one argument, "a
Tim Peters <tim.peters@gmail.com>
parents:
19581
diff
changeset
|
1860 |
|
21155
bca53a37a8a5
Fix of SF bug #475877 (Mutable subtype instances are hashable).
Guido van Rossum <guido@python.org>
parents:
20754
diff
changeset
|
1861 static long |
|
bca53a37a8a5
Fix of SF bug #475877 (Mutable subtype instances are hashable).
Guido van Rossum <guido@python.org>
parents:
20754
diff
changeset
|
1862 dict_nohash(PyObject *self) |
|
bca53a37a8a5
Fix of SF bug #475877 (Mutable subtype instances are hashable).
Guido van Rossum <guido@python.org>
parents:
20754
diff
changeset
|
1863 { |
|
bca53a37a8a5
Fix of SF bug #475877 (Mutable subtype instances are hashable).
Guido van Rossum <guido@python.org>
parents:
20754
diff
changeset
|
1864 PyErr_SetString(PyExc_TypeError, "dict objects are unhashable"); |
|
bca53a37a8a5
Fix of SF bug #475877 (Mutable subtype instances are hashable).
Guido van Rossum <guido@python.org>
parents:
20754
diff
changeset
|
1865 return -1; |
|
bca53a37a8a5
Fix of SF bug #475877 (Mutable subtype instances are hashable).
Guido van Rossum <guido@python.org>
parents:
20754
diff
changeset
|
1866 } |
|
bca53a37a8a5
Fix of SF bug #475877 (Mutable subtype instances are hashable).
Guido van Rossum <guido@python.org>
parents:
20754
diff
changeset
|
1867 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1868 static PyObject * |
|
17644
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1869 dict_iter(dictobject *dict) |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1870 { |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1871 return dictiter_new(dict, select_key); |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1872 } |
|
17597
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
1873 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
1874 PyDoc_STRVAR(dictionary_doc, |
|
20754
272336648d1f
Rename "dictionary" (type and constructor) to "dict".
Tim Peters <tim.peters@gmail.com>
parents:
20724
diff
changeset
|
1875 "dict() -> new empty dictionary.\n" |
|
272336648d1f
Rename "dictionary" (type and constructor) to "dict".
Tim Peters <tim.peters@gmail.com>
parents:
20724
diff
changeset
|
1876 "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
|
1877 " (key, value) pairs.\n" |
|
20754
272336648d1f
Rename "dictionary" (type and constructor) to "dict".
Tim Peters <tim.peters@gmail.com>
parents:
20724
diff
changeset
|
1878 "dict(seq) -> new dictionary initialized as if via:\n" |
|
20724
f224632d0260
dictionary() constructor:
Tim Peters <tim.peters@gmail.com>
parents:
20683
diff
changeset
|
1879 " d = {}\n" |
|
f224632d0260
dictionary() constructor:
Tim Peters <tim.peters@gmail.com>
parents:
20683
diff
changeset
|
1880 " 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
|
1881 " d[k] = v\n" |
|
08b720cd387e
Patch #642500 with slight modifications: allow keyword arguments in
Just van Rossum <just@letterror.com>
parents:
25115
diff
changeset
|
1882 "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
|
1883 " 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
|
1884 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
1885 PyTypeObject PyDict_Type = { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
1886 PyObject_HEAD_INIT(&PyType_Type) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1887 0, |
|
20754
272336648d1f
Rename "dictionary" (type and constructor) to "dict".
Tim Peters <tim.peters@gmail.com>
parents:
20724
diff
changeset
|
1888 "dict", |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19301
diff
changeset
|
1889 sizeof(dictobject), |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1890 0, |
| 15974 | 1891 (destructor)dict_dealloc, /* tp_dealloc */ |
| 1892 (printfunc)dict_print, /* tp_print */ | |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1893 0, /* tp_getattr */ |
| 15974 | 1894 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
|
1895 (cmpfunc)dict_compare, /* tp_compare */ |
| 15974 | 1896 (reprfunc)dict_repr, /* tp_repr */ |
| 1897 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
|
1898 &dict_as_sequence, /* tp_as_sequence */ |
| 15974 | 1899 &dict_as_mapping, /* tp_as_mapping */ |
|
21155
bca53a37a8a5
Fix of SF bug #475877 (Mutable subtype instances are hashable).
Guido van Rossum <guido@python.org>
parents:
20754
diff
changeset
|
1900 dict_nohash, /* tp_hash */ |
| 15974 | 1901 0, /* tp_call */ |
| 1902 0, /* tp_str */ | |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1903 PyObject_GenericGetAttr, /* tp_getattro */ |
| 15974 | 1904 0, /* tp_setattro */ |
| 1905 0, /* tp_as_buffer */ | |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19301
diff
changeset
|
1906 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1907 Py_TPFLAGS_BASETYPE, /* 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
|
1908 dictionary_doc, /* tp_doc */ |
| 15974 | 1909 (traverseproc)dict_traverse, /* tp_traverse */ |
| 1910 (inquiry)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
|
1911 dict_richcompare, /* tp_richcompare */ |
|
17597
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
1912 0, /* tp_weaklistoffset */ |
|
17644
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1913 (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
|
1914 0, /* tp_iternext */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1915 mapp_methods, /* tp_methods */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1916 0, /* tp_members */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1917 0, /* tp_getset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1918 0, /* tp_base */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1919 0, /* tp_dict */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1920 0, /* tp_descr_get */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1921 0, /* tp_descr_set */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1922 0, /* tp_dictoffset */ |
|
19658
2fa615dfc464
Make dictionary() a real constructor. Accepts at most one argument, "a
Tim Peters <tim.peters@gmail.com>
parents:
19581
diff
changeset
|
1923 (initproc)dict_init, /* tp_init */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1924 PyType_GenericAlloc, /* tp_alloc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1925 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
|
1926 PyObject_GC_Del, /* tp_free */ |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1927 }; |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1928 |
|
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
|
1929 /* 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
|
1930 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
1931 PyObject * |
|
26599
ecea8410178e
Constify char* API. Fixes #651363. 2.2 candidate.
Martin v. Löwis <martin@v.loewis.de>
parents:
26583
diff
changeset
|
1932 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
|
1933 { |
|
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
|
1934 PyObject *kv, *rv; |
|
a0cc005eccd7
Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents:
5505
diff
changeset
|
1935 kv = PyString_FromString(key); |
|
a0cc005eccd7
Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents:
5505
diff
changeset
|
1936 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
|
1937 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
|
1938 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
|
1939 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
|
1940 return rv; |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1941 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1942 |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1943 int |
|
26599
ecea8410178e
Constify char* API. Fixes #651363. 2.2 candidate.
Martin v. Löwis <martin@v.loewis.de>
parents:
26583
diff
changeset
|
1944 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
|
1945 { |
|
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
|
1946 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
|
1947 int err; |
|
a0cc005eccd7
Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents:
5505
diff
changeset
|
1948 kv = PyString_FromString(key); |
|
a0cc005eccd7
Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents:
5505
diff
changeset
|
1949 if (kv == NULL) |
|
5561
4dcf26fe4da5
Moved PyObject_{Get,Set}Attr to object.c.
Guido van Rossum <guido@python.org>
parents:
5540
diff
changeset
|
1950 return -1; |
|
6351
3864df294979
Don't intern the key string for getitem and delitem.
Guido van Rossum <guido@python.org>
parents:
6105
diff
changeset
|
1951 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
|
1952 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
|
1953 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
|
1954 return err; |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1955 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1956 |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1957 int |
|
26599
ecea8410178e
Constify char* API. Fixes #651363. 2.2 candidate.
Martin v. Löwis <martin@v.loewis.de>
parents:
26583
diff
changeset
|
1958 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
|
1959 { |
|
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
|
1960 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
|
1961 int err; |
|
a0cc005eccd7
Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents:
5505
diff
changeset
|
1962 kv = PyString_FromString(key); |
|
a0cc005eccd7
Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents:
5505
diff
changeset
|
1963 if (kv == NULL) |
|
5561
4dcf26fe4da5
Moved PyObject_{Get,Set}Attr to object.c.
Guido van Rossum <guido@python.org>
parents:
5540
diff
changeset
|
1964 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
|
1965 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
|
1966 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
|
1967 return err; |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1968 } |
|
17597
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
1969 |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
1970 /* Dictionary iterator type */ |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
1971 |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
1972 extern PyTypeObject PyDictIter_Type; /* Forward */ |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
1973 |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
1974 typedef struct { |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
1975 PyObject_HEAD |
|
24259
2f60f90ce309
Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
1976 dictobject *di_dict; /* Set to NULL when iterator is exhausted */ |
|
17656
a6698a016afe
Mchael Hudson pointed out that the code for detecting changes in
Guido van Rossum <guido@python.org>
parents:
17644
diff
changeset
|
1977 int di_used; |
|
17597
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
1978 int di_pos; |
|
17644
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1979 binaryfunc di_select; |
|
17597
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
1980 } dictiterobject; |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
1981 |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
1982 static PyObject * |
|
17644
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1983 dictiter_new(dictobject *dict, binaryfunc select) |
|
17597
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
1984 { |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
1985 dictiterobject *di; |
|
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
|
1986 di = PyObject_New(dictiterobject, &PyDictIter_Type); |
|
17597
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
1987 if (di == NULL) |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
1988 return NULL; |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
1989 Py_INCREF(dict); |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
1990 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
|
1991 di->di_used = dict->ma_used; |
|
17597
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
1992 di->di_pos = 0; |
|
17644
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
1993 di->di_select = select; |
|
17597
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
1994 return (PyObject *)di; |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
1995 } |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
1996 |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
1997 static void |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
1998 dictiter_dealloc(dictiterobject *di) |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
1999 { |
|
24259
2f60f90ce309
Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
2000 Py_XDECREF(di->di_dict); |
|
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
|
2001 PyObject_Del(di); |
|
17597
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2002 } |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2003 |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2004 static PyObject * |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2005 dictiter_getiter(PyObject *it) |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2006 { |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2007 Py_INCREF(it); |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2008 return it; |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2009 } |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2010 |
|
17617
aac65f50df6f
Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents:
17597
diff
changeset
|
2011 static PyObject *dictiter_iternext(dictiterobject *di) |
|
17597
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2012 { |
|
17644
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
2013 PyObject *key, *value; |
|
17617
aac65f50df6f
Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents:
17597
diff
changeset
|
2014 |
|
24259
2f60f90ce309
Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
2015 if (di->di_dict == NULL) |
|
2f60f90ce309
Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
2016 return NULL; |
|
2f60f90ce309
Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
2017 |
|
17656
a6698a016afe
Mchael Hudson pointed out that the code for detecting changes in
Guido van Rossum <guido@python.org>
parents:
17644
diff
changeset
|
2018 if (di->di_used != di->di_dict->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
|
2019 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
|
2020 "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
|
2021 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
|
2022 return NULL; |
|
aac65f50df6f
Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents:
17597
diff
changeset
|
2023 } |
|
24259
2f60f90ce309
Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
2024 if (PyDict_Next((PyObject *)(di->di_dict), &di->di_pos, &key, &value)) |
|
17644
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
2025 return (*di->di_select)(key, value); |
|
24259
2f60f90ce309
Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
2026 |
|
2f60f90ce309
Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
2027 Py_DECREF(di->di_dict); |
|
2f60f90ce309
Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
2028 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
|
2029 return NULL; |
|
17597
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2030 } |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2031 |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2032 PyTypeObject PyDictIter_Type = { |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2033 PyObject_HEAD_INIT(&PyType_Type) |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2034 0, /* ob_size */ |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2035 "dictionary-iterator", /* tp_name */ |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2036 sizeof(dictiterobject), /* tp_basicsize */ |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2037 0, /* tp_itemsize */ |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2038 /* methods */ |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2039 (destructor)dictiter_dealloc, /* tp_dealloc */ |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2040 0, /* tp_print */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
2041 0, /* tp_getattr */ |
|
17597
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2042 0, /* tp_setattr */ |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2043 0, /* tp_compare */ |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2044 0, /* tp_repr */ |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2045 0, /* tp_as_number */ |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2046 0, /* tp_as_sequence */ |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2047 0, /* tp_as_mapping */ |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2048 0, /* tp_hash */ |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2049 0, /* tp_call */ |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2050 0, /* tp_str */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
2051 PyObject_GenericGetAttr, /* tp_getattro */ |
|
17597
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2052 0, /* tp_setattro */ |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2053 0, /* tp_as_buffer */ |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2054 Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2055 0, /* tp_doc */ |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2056 0, /* tp_traverse */ |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2057 0, /* tp_clear */ |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2058 0, /* tp_richcompare */ |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2059 0, /* tp_weaklistoffset */ |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2060 (getiterfunc)dictiter_getiter, /* 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
|
2061 (iternextfunc)dictiter_iternext, /* tp_iternext */ |
|
24259
2f60f90ce309
Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
2062 0, /* tp_methods */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
2063 0, /* tp_members */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
2064 0, /* tp_getset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
2065 0, /* tp_base */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
2066 0, /* tp_dict */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
2067 0, /* tp_descr_get */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
2068 0, /* tp_descr_set */ |
|
17597
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2069 }; |
