Mercurial > cpython-lru_cache-weakref
annotate Objects/dictobject.c @ 65035:87f4b32905df 2.7
Cosmetic fix to use print function.
| author | Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp> |
|---|---|
| date | Fri, 01 Oct 2010 10:48:47 +0000 |
| parents | fd25030223b0 |
| children | 0a2ec174fbcd |
| rev | line source |
|---|---|
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 |
|
10461
4abc5abaec9a
Vladimir Marangozov contributed updated comments.
Guido van Rossum <guido@python.org>
parents:
9487
diff
changeset
|
2 /* Dictionary object implementation using a hash table */ |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1256
diff
changeset
|
3 |
|
28946
dbdeb734ff97
Add a reference to dictnotes.txt. It does no good if you don't know it's
Raymond Hettinger <python@rcn.com>
parents:
28320
diff
changeset
|
4 /* The distribution includes a separate file, Objects/dictnotes.txt, |
|
36164
50886d42b444
Fixed English in a comment; trimmed trailing whitespace;
Tim Peters <tim.peters@gmail.com>
parents:
35636
diff
changeset
|
5 describing explorations into dictionary design and optimization. |
|
28946
dbdeb734ff97
Add a reference to dictnotes.txt. It does no good if you don't know it's
Raymond Hettinger <python@rcn.com>
parents:
28320
diff
changeset
|
6 It covers typical dictionary use patterns, the parameters for |
|
dbdeb734ff97
Add a reference to dictnotes.txt. It does no good if you don't know it's
Raymond Hettinger <python@rcn.com>
parents:
28320
diff
changeset
|
7 tuning dictionaries, and several ideas for possible optimizations. |
|
dbdeb734ff97
Add a reference to dictnotes.txt. It does no good if you don't know it's
Raymond Hettinger <python@rcn.com>
parents:
28320
diff
changeset
|
8 */ |
|
dbdeb734ff97
Add a reference to dictnotes.txt. It does no good if you don't know it's
Raymond Hettinger <python@rcn.com>
parents:
28320
diff
changeset
|
9 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
10 #include "Python.h" |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 |
|
5053
219bdd8afb38
Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents:
5038
diff
changeset
|
12 |
|
40257
9ab7a7736ca0
Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents:
40225
diff
changeset
|
13 /* Set a key error with the specified argument, wrapping it in a |
|
9ab7a7736ca0
Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents:
40225
diff
changeset
|
14 * tuple automatically so that tuple keys are not unpacked as the |
|
9ab7a7736ca0
Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents:
40225
diff
changeset
|
15 * exception arguments. */ |
|
9ab7a7736ca0
Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents:
40225
diff
changeset
|
16 static void |
|
9ab7a7736ca0
Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents:
40225
diff
changeset
|
17 set_key_error(PyObject *arg) |
|
9ab7a7736ca0
Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents:
40225
diff
changeset
|
18 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
19 PyObject *tup; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
20 tup = PyTuple_Pack(1, arg); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
21 if (!tup) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
22 return; /* caller will expect error to be set anyway */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
23 PyErr_SetObject(PyExc_KeyError, tup); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
24 Py_DECREF(tup); |
|
40257
9ab7a7736ca0
Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents:
40225
diff
changeset
|
25 } |
|
9ab7a7736ca0
Bug #1576657: when setting a KeyError for a tuple key, make sure that
Georg Brandl <georg@python.org>
parents:
40225
diff
changeset
|
26 |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
27 /* Define this out if you don't want conversion statistics on exit. */ |
|
14310
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
28 #undef SHOW_CONVERSION_COUNTS |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
29 |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
30 /* See large comment block below. This must be >= 1. */ |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
31 #define PERTURB_SHIFT 5 |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
32 |
|
5053
219bdd8afb38
Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents:
5038
diff
changeset
|
33 /* |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
34 Major subtleties ahead: Most hash schemes depend on having a "good" hash |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
35 function, in the sense of simulating randomness. Python doesn't: its most |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
36 important hash functions (for strings and ints) are very regular in common |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
37 cases: |
|
17973
2f2ee5daa60d
Implement an old idea of Christian Tismer's: use polynomial division
Tim Peters <tim.peters@gmail.com>
parents:
17964
diff
changeset
|
38 |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
39 >>> map(hash, (0, 1, 2, 3)) |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
40 [0, 1, 2, 3] |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
41 >>> map(hash, ("namea", "nameb", "namec", "named")) |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
42 [-1658398457, -1658398460, -1658398459, -1658398462] |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
43 >>> |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
44 |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
45 This isn't necessarily bad! To the contrary, in a table of size 2**i, taking |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
46 the low-order i bits as the initial table index is extremely fast, and there |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
47 are no collisions at all for dicts indexed by a contiguous range of ints. |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
48 The same is approximately true when keys are "consecutive" strings. So this |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
49 gives better-than-random behavior in common cases, and that's very desirable. |
|
17973
2f2ee5daa60d
Implement an old idea of Christian Tismer's: use polynomial division
Tim Peters <tim.peters@gmail.com>
parents:
17964
diff
changeset
|
50 |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
51 OTOH, when collisions occur, the tendency to fill contiguous slices of the |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
52 hash table makes a good collision resolution strategy crucial. Taking only |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
53 the last i bits of the hash code is also vulnerable: for example, consider |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
54 [i << 16 for i in range(20000)] as a set of keys. Since ints are their own |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
55 hash codes, and this fits in a dict of size 2**15, the last 15 bits of every |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
56 hash code are all 0: they *all* map to the same table index. |
|
17973
2f2ee5daa60d
Implement an old idea of Christian Tismer's: use polynomial division
Tim Peters <tim.peters@gmail.com>
parents:
17964
diff
changeset
|
57 |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
58 But catering to unusual cases should not slow the usual ones, so we just take |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
59 the last i bits anyway. It's up to collision resolution to do the rest. If |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
60 we *usually* find the key we're looking for on the first try (and, it turns |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
61 out, we usually do -- the table load factor is kept under 2/3, so the odds |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
62 are solidly in our favor), then it makes best sense to keep the initial index |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
63 computation dirt cheap. |
|
17973
2f2ee5daa60d
Implement an old idea of Christian Tismer's: use polynomial division
Tim Peters <tim.peters@gmail.com>
parents:
17964
diff
changeset
|
64 |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
65 The first half of collision resolution is to visit table indices via this |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
66 recurrence: |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
67 |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
68 j = ((5*j) + 1) mod 2**i |
|
17973
2f2ee5daa60d
Implement an old idea of Christian Tismer's: use polynomial division
Tim Peters <tim.peters@gmail.com>
parents:
17964
diff
changeset
|
69 |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
70 For any initial j in range(2**i), repeating that 2**i times generates each |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
71 int in range(2**i) exactly once (see any text on random-number generation for |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
72 proof). By itself, this doesn't help much: like linear probing (setting |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
73 j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
74 order. This would be bad, except that's not the only thing we do, and it's |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
75 actually *good* in the common cases where hash keys are consecutive. In an |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
76 example that's really too small to make this entirely clear, for a table of |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
77 size 2**3 the order of indices is: |
|
17973
2f2ee5daa60d
Implement an old idea of Christian Tismer's: use polynomial division
Tim Peters <tim.peters@gmail.com>
parents:
17964
diff
changeset
|
78 |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
79 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating] |
|
17973
2f2ee5daa60d
Implement an old idea of Christian Tismer's: use polynomial division
Tim Peters <tim.peters@gmail.com>
parents:
17964
diff
changeset
|
80 |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
81 If two things come in at index 5, the first place we look after is index 2, |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
82 not 6, so if another comes in at index 6 the collision at 5 didn't hurt it. |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
83 Linear probing is deadly in this case because there the fixed probe order |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
84 is the *same* as the order consecutive keys are likely to arrive. But it's |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
85 extremely unlikely hash codes will follow a 5*j+1 recurrence by accident, |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
86 and certain that consecutive hash codes do not. |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
87 |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
88 The other half of the strategy is to get the other bits of the hash code |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
89 into play. This is done by initializing a (unsigned) vrbl "perturb" to the |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
90 full hash code, and changing the recurrence to: |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
91 |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
92 j = (5*j) + 1 + perturb; |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
93 perturb >>= PERTURB_SHIFT; |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
94 use j % 2**i as the next table index; |
|
17920
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
95 |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
96 Now the probe sequence depends (eventually) on every bit in the hash code, |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
97 and the pseudo-scrambling property of recurring on 5*j+1 is more valuable, |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
98 because it quickly magnifies small differences in the bits that didn't affect |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
99 the initial index. Note that because perturb is unsigned, if the recurrence |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
100 is executed often enough perturb eventually becomes and remains 0. At that |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
101 point (very rarely reached) the recurrence is on (just) 5*j+1 again, and |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
102 that's certain to find an empty slot eventually (since it generates every int |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
103 in range(2**i), and we make sure there's always at least one empty slot). |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
104 |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
105 Selecting a good value for PERTURB_SHIFT is a balancing act. You want it |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
106 small so that the high bits of the hash code continue to affect the probe |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
107 sequence across iterations; but you want it large so that in really bad cases |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
108 the high-order hash bits have an effect on early iterations. 5 was "the |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
109 best" in minimizing total collisions across experiments Tim Peters ran (on |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
110 both normal and pathological cases), but 4 and 6 weren't significantly worse. |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
111 |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
112 Historical: Reimer Behrends contributed the idea of using a polynomial-based |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
113 approach, using repeated multiplication by x in GF(2**n) where an irreducible |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
114 polynomial for each table size was chosen such that x was a primitive root. |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
115 Christian Tismer later extended that to use division by x instead, as an |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
116 efficient way to get the high bits of the hash code into play. This scheme |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
117 also gave excellent collision statistics, but was more expensive: two |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
118 if-tests were required inside the loop; computing "the next" index took about |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
119 the same number of operations but without as much potential parallelism |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
120 (e.g., computing 5*j can go on at the same time as computing 1+perturb in the |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
121 above, and then shifting perturb can be done while the table index is being |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
122 masked); and the PyDictObject struct required a member to hold the table's |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
123 polynomial. In Tim's experiments the current scheme ran faster, produced |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
124 equally good collision statistics, needed less code & used less memory. |
|
38499
494cab563c11
Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents:
37756
diff
changeset
|
125 |
|
494cab563c11
Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents:
37756
diff
changeset
|
126 Theoretical Python 2.5 headache: hash codes are only C "long", but |
|
494cab563c11
Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents:
37756
diff
changeset
|
127 sizeof(Py_ssize_t) > sizeof(long) may be possible. In that case, and if a |
|
494cab563c11
Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents:
37756
diff
changeset
|
128 dict is genuinely huge, then only the slots directly reachable via indexing |
|
494cab563c11
Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents:
37756
diff
changeset
|
129 by a C long can be the first slot in a probe sequence. The probe sequence |
|
494cab563c11
Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents:
37756
diff
changeset
|
130 will still eventually reach every slot in the table, but the collision rate |
|
494cab563c11
Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents:
37756
diff
changeset
|
131 on initial probes may be much higher than this scheme was designed for. |
|
494cab563c11
Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents:
37756
diff
changeset
|
132 Getting a hash code as fat as Py_ssize_t is the only real cure. But in |
|
494cab563c11
Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents:
37756
diff
changeset
|
133 practice, this probably won't make a lick of difference for many years (at |
|
494cab563c11
Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents:
37756
diff
changeset
|
134 which point everyone will have terabytes of RAM on 64-bit boxes). |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
135 */ |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
137 /* Object used as dummy key to fill deleted entries */ |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
138 static PyObject *dummy = NULL; /* Initialized by first call to newPyDictObject() */ |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
139 |
|
37638
65ecb83ea7dd
Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents:
37304
diff
changeset
|
140 #ifdef Py_REF_DEBUG |
|
65ecb83ea7dd
Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents:
37304
diff
changeset
|
141 PyObject * |
|
65ecb83ea7dd
Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents:
37304
diff
changeset
|
142 _PyDict_Dummy(void) |
|
65ecb83ea7dd
Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents:
37304
diff
changeset
|
143 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
144 return dummy; |
|
37638
65ecb83ea7dd
Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents:
37304
diff
changeset
|
145 } |
|
65ecb83ea7dd
Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents:
37304
diff
changeset
|
146 #endif |
|
65ecb83ea7dd
Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents:
37304
diff
changeset
|
147 |
|
14310
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
148 /* forward declarations */ |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
149 static PyDictEntry * |
|
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
150 lookdict_string(PyDictObject *mp, PyObject *key, long hash); |
|
14310
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
151 |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
152 #ifdef SHOW_CONVERSION_COUNTS |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
153 static long created = 0L; |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
154 static long converted = 0L; |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
155 |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
156 static void |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
157 show_counts(void) |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
158 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
159 fprintf(stderr, "created %ld string dicts\n", created); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
160 fprintf(stderr, "converted %ld to normal dicts\n", converted); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
161 fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created); |
|
14310
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
162 } |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
163 #endif |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
164 |
|
45217
5ac605c0f27c
Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents:
45191
diff
changeset
|
165 /* Debug statistic to compare allocations with reuse through the free list */ |
|
5ac605c0f27c
Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents:
45191
diff
changeset
|
166 #undef SHOW_ALLOC_COUNT |
|
5ac605c0f27c
Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents:
45191
diff
changeset
|
167 #ifdef SHOW_ALLOC_COUNT |
|
5ac605c0f27c
Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents:
45191
diff
changeset
|
168 static size_t count_alloc = 0; |
|
5ac605c0f27c
Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents:
45191
diff
changeset
|
169 static size_t count_reuse = 0; |
|
5ac605c0f27c
Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents:
45191
diff
changeset
|
170 |
|
5ac605c0f27c
Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents:
45191
diff
changeset
|
171 static void |
|
5ac605c0f27c
Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents:
45191
diff
changeset
|
172 show_alloc(void) |
|
5ac605c0f27c
Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents:
45191
diff
changeset
|
173 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
174 fprintf(stderr, "Dict allocations: %" PY_FORMAT_SIZE_T "d\n", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
175 count_alloc); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
176 fprintf(stderr, "Dict reuse through freelist: %" PY_FORMAT_SIZE_T |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
177 "d\n", count_reuse); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
178 fprintf(stderr, "%.2f%% reuse rate\n\n", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
179 (100.0*count_reuse/(count_alloc+count_reuse))); |
|
45217
5ac605c0f27c
Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents:
45191
diff
changeset
|
180 } |
|
5ac605c0f27c
Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents:
45191
diff
changeset
|
181 #endif |
|
5ac605c0f27c
Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents:
45191
diff
changeset
|
182 |
|
52288
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
50424
diff
changeset
|
183 /* Debug statistic to count GC tracking of dicts */ |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
50424
diff
changeset
|
184 #ifdef SHOW_TRACK_COUNT |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
50424
diff
changeset
|
185 static Py_ssize_t count_untracked = 0; |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
50424
diff
changeset
|
186 static Py_ssize_t count_tracked = 0; |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
50424
diff
changeset
|
187 |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
50424
diff
changeset
|
188 static void |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
50424
diff
changeset
|
189 show_track(void) |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
50424
diff
changeset
|
190 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
191 fprintf(stderr, "Dicts created: %" PY_FORMAT_SIZE_T "d\n", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
192 count_tracked + count_untracked); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
193 fprintf(stderr, "Dicts tracked by the GC: %" PY_FORMAT_SIZE_T |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
194 "d\n", count_tracked); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
195 fprintf(stderr, "%.2f%% dict tracking rate\n\n", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
196 (100.0*count_tracked/(count_untracked+count_tracked))); |
|
52288
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
50424
diff
changeset
|
197 } |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
50424
diff
changeset
|
198 #endif |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
50424
diff
changeset
|
199 |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
50424
diff
changeset
|
200 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
201 /* Initialization macros. |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
202 There are two ways to create a dict: PyDict_New() is the main C API |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
203 function, and the tp_new slot maps to dict_new(). In the latter case we |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
204 can save a little time over what PyDict_New does because it's guaranteed |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
205 that the PyDictObject struct is already zeroed out. |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
206 Everyone except dict_new() should use EMPTY_TO_MINSIZE (unless they have |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
207 an excellent reason not to). |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
208 */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
209 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
210 #define INIT_NONZERO_DICT_SLOTS(mp) do { \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
211 (mp)->ma_table = (mp)->ma_smalltable; \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
212 (mp)->ma_mask = PyDict_MINSIZE - 1; \ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
213 } while(0) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
214 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
215 #define EMPTY_TO_MINSIZE(mp) do { \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
216 memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
217 (mp)->ma_used = (mp)->ma_fill = 0; \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
218 INIT_NONZERO_DICT_SLOTS(mp); \ |
|
17920
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
219 } while(0) |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
220 |
|
31677
7bb030066ea4
Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents:
31620
diff
changeset
|
221 /* Dictionary reuse scheme to save calls to malloc, free, and memset */ |
|
45191
920a7633ae08
Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents:
44949
diff
changeset
|
222 #ifndef PyDict_MAXFREELIST |
|
920a7633ae08
Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents:
44949
diff
changeset
|
223 #define PyDict_MAXFREELIST 80 |
|
920a7633ae08
Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents:
44949
diff
changeset
|
224 #endif |
|
920a7633ae08
Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents:
44949
diff
changeset
|
225 static PyDictObject *free_list[PyDict_MAXFREELIST]; |
|
920a7633ae08
Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents:
44949
diff
changeset
|
226 static int numfree = 0; |
|
31677
7bb030066ea4
Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents:
31620
diff
changeset
|
227 |
|
45229
262263737bce
Deallocate content of the dict free list on interpreter shutdown
Christian Heimes <christian@cheimes.de>
parents:
45217
diff
changeset
|
228 void |
|
262263737bce
Deallocate content of the dict free list on interpreter shutdown
Christian Heimes <christian@cheimes.de>
parents:
45217
diff
changeset
|
229 PyDict_Fini(void) |
|
262263737bce
Deallocate content of the dict free list on interpreter shutdown
Christian Heimes <christian@cheimes.de>
parents:
45217
diff
changeset
|
230 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
231 PyDictObject *op; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
232 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
233 while (numfree) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
234 op = free_list[--numfree]; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
235 assert(PyDict_CheckExact(op)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
236 PyObject_GC_Del(op); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
237 } |
|
45229
262263737bce
Deallocate content of the dict free list on interpreter shutdown
Christian Heimes <christian@cheimes.de>
parents:
45217
diff
changeset
|
238 } |
|
262263737bce
Deallocate content of the dict free list on interpreter shutdown
Christian Heimes <christian@cheimes.de>
parents:
45217
diff
changeset
|
239 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
240 PyObject * |
|
13660
a1ce07218781
Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents:
13620
diff
changeset
|
241 PyDict_New(void) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
242 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
243 register PyDictObject *mp; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
244 if (dummy == NULL) { /* Auto-initialize dummy */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
245 dummy = PyString_FromString("<dummy key>"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
246 if (dummy == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
247 return NULL; |
|
14310
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
248 #ifdef SHOW_CONVERSION_COUNTS |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
249 Py_AtExit(show_counts); |
|
14310
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
250 #endif |
|
45217
5ac605c0f27c
Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents:
45191
diff
changeset
|
251 #ifdef SHOW_ALLOC_COUNT |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
252 Py_AtExit(show_alloc); |
|
45217
5ac605c0f27c
Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents:
45191
diff
changeset
|
253 #endif |
|
52288
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
50424
diff
changeset
|
254 #ifdef SHOW_TRACK_COUNT |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
255 Py_AtExit(show_track); |
|
52288
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
50424
diff
changeset
|
256 #endif |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
257 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
258 if (numfree) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
259 mp = free_list[--numfree]; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
260 assert (mp != NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
261 assert (Py_TYPE(mp) == &PyDict_Type); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
262 _Py_NewReference((PyObject *)mp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
263 if (mp->ma_fill) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
264 EMPTY_TO_MINSIZE(mp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
265 } else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
266 /* At least set ma_table and ma_mask; these are wrong |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
267 if an empty but presized dict is added to freelist */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
268 INIT_NONZERO_DICT_SLOTS(mp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
269 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
270 assert (mp->ma_used == 0); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
271 assert (mp->ma_table == mp->ma_smalltable); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
272 assert (mp->ma_mask == PyDict_MINSIZE - 1); |
|
45217
5ac605c0f27c
Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents:
45191
diff
changeset
|
273 #ifdef SHOW_ALLOC_COUNT |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
274 count_reuse++; |
|
45217
5ac605c0f27c
Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents:
45191
diff
changeset
|
275 #endif |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
276 } else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
277 mp = PyObject_GC_New(PyDictObject, &PyDict_Type); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
278 if (mp == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
279 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
280 EMPTY_TO_MINSIZE(mp); |
|
45217
5ac605c0f27c
Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents:
45191
diff
changeset
|
281 #ifdef SHOW_ALLOC_COUNT |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
282 count_alloc++; |
|
45217
5ac605c0f27c
Added some statistics code to dict and list object code. I wanted to test how a larger freelist affects the reusage of freed objects. Contrary to my gut feelings 80 objects is more than fine for small apps. I haven't profiled a large app yet.
Christian Heimes <christian@cheimes.de>
parents:
45191
diff
changeset
|
283 #endif |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
284 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
285 mp->ma_lookup = lookdict_string; |
|
52288
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
50424
diff
changeset
|
286 #ifdef SHOW_TRACK_COUNT |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
287 count_untracked++; |
|
52288
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
50424
diff
changeset
|
288 #endif |
|
14310
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
289 #ifdef SHOW_CONVERSION_COUNTS |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
290 ++created; |
|
14310
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
291 #endif |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
292 return (PyObject *)mp; |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
293 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
294 |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
295 /* |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
296 The basic lookup function used by all operations. |
|
5053
219bdd8afb38
Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents:
5038
diff
changeset
|
297 This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4. |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
298 Open addressing is preferred over chaining since the link overhead for |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
299 chaining would be substantial (100% with typical malloc overhead). |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
300 |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
301 The initial probe index is computed as hash mod the table size. Subsequent |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
302 probe indices are computed as explained earlier. |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
303 |
|
10461
4abc5abaec9a
Vladimir Marangozov contributed updated comments.
Guido van Rossum <guido@python.org>
parents:
9487
diff
changeset
|
304 All arithmetic on hash should ignore overflow. |
|
5053
219bdd8afb38
Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents:
5038
diff
changeset
|
305 |
|
18014
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
306 (The details in this version are due to Tim Peters, building on many past |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
307 contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and |
|
9f97167f1643
New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents:
17973
diff
changeset
|
308 Christian Tismer). |
|
14310
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
309 |
|
38540
d7dd528427c0
Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents:
38536
diff
changeset
|
310 lookdict() is general-purpose, and may return NULL if (and only if) a |
|
d7dd528427c0
Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents:
38536
diff
changeset
|
311 comparison raises an exception (this was new in Python 2.5). |
|
d7dd528427c0
Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents:
38536
diff
changeset
|
312 lookdict_string() below is specialized to string keys, comparison of which can |
|
d7dd528427c0
Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents:
38536
diff
changeset
|
313 never raise an exception; that function can never return NULL. For both, when |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
314 the key isn't found a PyDictEntry* is returned for which the me_value field is |
|
38540
d7dd528427c0
Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents:
38536
diff
changeset
|
315 NULL; this is the slot in the dict at which the key would have been found, and |
|
d7dd528427c0
Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents:
38536
diff
changeset
|
316 the caller can (if it wishes) add the <key, value> pair to the returned |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
317 PyDictEntry*. |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
318 */ |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
319 static PyDictEntry * |
|
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
320 lookdict(PyDictObject *mp, PyObject *key, register long hash) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
321 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
322 register size_t i; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
323 register size_t perturb; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
324 register PyDictEntry *freeslot; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
325 register size_t mask = (size_t)mp->ma_mask; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
326 PyDictEntry *ep0 = mp->ma_table; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
327 register PyDictEntry *ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
328 register int cmp; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
329 PyObject *startkey; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
330 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
331 i = (size_t)hash & mask; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
332 ep = &ep0[i]; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
333 if (ep->me_key == NULL || ep->me_key == key) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
334 return ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
335 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
336 if (ep->me_key == dummy) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
337 freeslot = ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
338 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
339 if (ep->me_hash == hash) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
340 startkey = ep->me_key; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
341 Py_INCREF(startkey); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
342 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
343 Py_DECREF(startkey); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
344 if (cmp < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
345 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
346 if (ep0 == mp->ma_table && ep->me_key == startkey) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
347 if (cmp > 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
348 return ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
349 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
350 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
351 /* The compare did major nasty stuff to the |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
352 * dict: start over. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
353 * XXX A clever adversary could prevent this |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
354 * XXX from terminating. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
355 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
356 return lookdict(mp, key, hash); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
357 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
358 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
359 freeslot = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
360 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
361 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
362 /* In the loop, me_key == dummy is by far (factor of 100s) the |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
363 least likely outcome, so test for that last. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
364 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
365 i = (i << 2) + i + perturb + 1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
366 ep = &ep0[i & mask]; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
367 if (ep->me_key == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
368 return freeslot == NULL ? ep : freeslot; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
369 if (ep->me_key == key) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
370 return ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
371 if (ep->me_hash == hash && ep->me_key != dummy) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
372 startkey = ep->me_key; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
373 Py_INCREF(startkey); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
374 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
375 Py_DECREF(startkey); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
376 if (cmp < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
377 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
378 if (ep0 == mp->ma_table && ep->me_key == startkey) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
379 if (cmp > 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
380 return ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
381 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
382 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
383 /* The compare did major nasty stuff to the |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
384 * dict: start over. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
385 * XXX A clever adversary could prevent this |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
386 * XXX from terminating. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
387 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
388 return lookdict(mp, key, hash); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
389 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
390 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
391 else if (ep->me_key == dummy && freeslot == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
392 freeslot = ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
393 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
394 assert(0); /* NOT REACHED */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
395 return 0; |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
396 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
397 |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
398 /* |
|
14310
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
399 * Hacked up version of lookdict which can assume keys are always strings; |
|
38540
d7dd528427c0
Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents:
38536
diff
changeset
|
400 * this assumption allows testing for errors during PyObject_RichCompareBool() |
|
d7dd528427c0
Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents:
38536
diff
changeset
|
401 * to be dropped; string-string comparisons never raise exceptions. This also |
|
d7dd528427c0
Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents:
38536
diff
changeset
|
402 * means we don't need to go through PyObject_RichCompareBool(); we can always |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47421
diff
changeset
|
403 * use _PyString_Eq() directly. |
|
14310
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
404 * |
|
38540
d7dd528427c0
Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents:
38536
diff
changeset
|
405 * This is valuable because dicts with only string keys are very common. |
|
14310
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
406 */ |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
407 static PyDictEntry * |
|
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
408 lookdict_string(PyDictObject *mp, PyObject *key, register long hash) |
|
14310
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
409 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
410 register size_t i; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
411 register size_t perturb; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
412 register PyDictEntry *freeslot; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
413 register size_t mask = (size_t)mp->ma_mask; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
414 PyDictEntry *ep0 = mp->ma_table; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
415 register PyDictEntry *ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
416 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
417 /* Make sure this function doesn't have to handle non-string keys, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
418 including subclasses of str; e.g., one reason to subclass |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
419 strings is to override __eq__, and for speed we don't cater to |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
420 that here. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
421 if (!PyString_CheckExact(key)) { |
|
14310
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
422 #ifdef SHOW_CONVERSION_COUNTS |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
423 ++converted; |
|
14310
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
424 #endif |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
425 mp->ma_lookup = lookdict; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
426 return lookdict(mp, key, hash); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
427 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
428 i = hash & mask; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
429 ep = &ep0[i]; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
430 if (ep->me_key == NULL || ep->me_key == key) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
431 return ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
432 if (ep->me_key == dummy) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
433 freeslot = ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
434 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
435 if (ep->me_hash == hash && _PyString_Eq(ep->me_key, key)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
436 return ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
437 freeslot = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
438 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
439 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
440 /* In the loop, me_key == dummy is by far (factor of 100s) the |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
441 least likely outcome, so test for that last. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
442 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
443 i = (i << 2) + i + perturb + 1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
444 ep = &ep0[i & mask]; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
445 if (ep->me_key == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
446 return freeslot == NULL ? ep : freeslot; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
447 if (ep->me_key == key |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
448 || (ep->me_hash == hash |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
449 && ep->me_key != dummy |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
450 && _PyString_Eq(ep->me_key, key))) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
451 return ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
452 if (ep->me_key == dummy && freeslot == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
453 freeslot = ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
454 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
455 assert(0); /* NOT REACHED */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
456 return 0; |
|
14310
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
457 } |
|
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
458 |
|
52288
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
50424
diff
changeset
|
459 #ifdef SHOW_TRACK_COUNT |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
50424
diff
changeset
|
460 #define INCREASE_TRACK_COUNT \ |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
461 (count_tracked++, count_untracked--); |
|
52288
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
50424
diff
changeset
|
462 #define DECREASE_TRACK_COUNT \ |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
463 (count_tracked--, count_untracked++); |
|
52288
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
50424
diff
changeset
|
464 #else |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
50424
diff
changeset
|
465 #define INCREASE_TRACK_COUNT |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
50424
diff
changeset
|
466 #define DECREASE_TRACK_COUNT |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
50424
diff
changeset
|
467 #endif |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
50424
diff
changeset
|
468 |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
50424
diff
changeset
|
469 #define MAINTAIN_TRACKING(mp, key, value) \ |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
470 do { \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
471 if (!_PyObject_GC_IS_TRACKED(mp)) { \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
472 if (_PyObject_GC_MAY_BE_TRACKED(key) || \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
473 _PyObject_GC_MAY_BE_TRACKED(value)) { \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
474 _PyObject_GC_TRACK(mp); \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
475 INCREASE_TRACK_COUNT \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
476 } \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
477 } \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
478 } while(0) |
|
52288
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
50424
diff
changeset
|
479 |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
50424
diff
changeset
|
480 void |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
50424
diff
changeset
|
481 _PyDict_MaybeUntrack(PyObject *op) |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
50424
diff
changeset
|
482 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
483 PyDictObject *mp; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
484 PyObject *value; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
485 Py_ssize_t mask, i; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
486 PyDictEntry *ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
487 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
488 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
489 return; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
490 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
491 mp = (PyDictObject *) op; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
492 ep = mp->ma_table; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
493 mask = mp->ma_mask; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
494 for (i = 0; i <= mask; i++) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
495 if ((value = ep[i].me_value) == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
496 continue; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
497 if (_PyObject_GC_MAY_BE_TRACKED(value) || |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
498 _PyObject_GC_MAY_BE_TRACKED(ep[i].me_key)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
499 return; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
500 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
501 DECREASE_TRACK_COUNT |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
502 _PyObject_GC_UNTRACK(op); |
|
52288
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
50424
diff
changeset
|
503 } |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
50424
diff
changeset
|
504 |
|
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
50424
diff
changeset
|
505 |
|
14310
2db807f345bc
Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents:
14307
diff
changeset
|
506 /* |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
507 Internal routine to insert a new item into the table. |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
508 Used both by the internal resize routine and by the public insert routine. |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
509 Eats a reference to key and one to value. |
|
38540
d7dd528427c0
Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents:
38536
diff
changeset
|
510 Returns -1 if an error occurred, or 0 on success. |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
511 */ |
|
38536
175cbdf9a5fb
[ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents:
38504
diff
changeset
|
512 static int |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
513 insertdict(register PyDictObject *mp, PyObject *key, long hash, PyObject *value) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
514 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
515 PyObject *old_value; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
516 register PyDictEntry *ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
517 typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
518 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
519 assert(mp->ma_lookup != NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
520 ep = mp->ma_lookup(mp, key, hash); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
521 if (ep == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
522 Py_DECREF(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
523 Py_DECREF(value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
524 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
525 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
526 MAINTAIN_TRACKING(mp, key, value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
527 if (ep->me_value != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
528 old_value = ep->me_value; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
529 ep->me_value = value; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
530 Py_DECREF(old_value); /* which **CAN** re-enter */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
531 Py_DECREF(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
532 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
533 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
534 if (ep->me_key == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
535 mp->ma_fill++; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
536 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
537 assert(ep->me_key == dummy); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
538 Py_DECREF(dummy); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
539 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
540 ep->me_key = key; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
541 ep->me_hash = (Py_ssize_t)hash; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
542 ep->me_value = value; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
543 mp->ma_used++; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
544 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
545 return 0; |
|
38536
175cbdf9a5fb
[ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents:
38504
diff
changeset
|
546 } |
|
175cbdf9a5fb
[ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents:
38504
diff
changeset
|
547 |
|
175cbdf9a5fb
[ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents:
38504
diff
changeset
|
548 /* |
|
175cbdf9a5fb
[ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents:
38504
diff
changeset
|
549 Internal routine used by dictresize() to insert an item which is |
|
175cbdf9a5fb
[ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents:
38504
diff
changeset
|
550 known to be absent from the dict. This routine also assumes that |
|
175cbdf9a5fb
[ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents:
38504
diff
changeset
|
551 the dict contains no deleted entries. Besides the performance benefit, |
|
175cbdf9a5fb
[ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents:
38504
diff
changeset
|
552 using insertdict() in dictresize() is dangerous (SF bug #1456209). |
|
38540
d7dd528427c0
Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents:
38536
diff
changeset
|
553 Note that no refcounts are changed by this routine; if needed, the caller |
|
d7dd528427c0
Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents:
38536
diff
changeset
|
554 is responsible for incref'ing `key` and `value`. |
|
38536
175cbdf9a5fb
[ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents:
38504
diff
changeset
|
555 */ |
|
175cbdf9a5fb
[ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents:
38504
diff
changeset
|
556 static void |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
557 insertdict_clean(register PyDictObject *mp, PyObject *key, long hash, |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
558 PyObject *value) |
|
38536
175cbdf9a5fb
[ 1497053 ] Let dicts propagate the exceptions in user __eq__().
Armin Rigo <arigo@tunes.org>
parents:
38504
diff
changeset
|
559 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
560 register size_t i; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
561 register size_t perturb; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
562 register size_t mask = (size_t)mp->ma_mask; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
563 PyDictEntry *ep0 = mp->ma_table; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
564 register PyDictEntry *ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
565 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
566 MAINTAIN_TRACKING(mp, key, value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
567 i = hash & mask; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
568 ep = &ep0[i]; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
569 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
570 i = (i << 2) + i + perturb + 1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
571 ep = &ep0[i & mask]; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
572 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
573 assert(ep->me_value == NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
574 mp->ma_fill++; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
575 ep->me_key = key; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
576 ep->me_hash = (Py_ssize_t)hash; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
577 ep->me_value = value; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
578 mp->ma_used++; |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
579 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
580 |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
581 /* |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
582 Restructure the table by allocating a new table and reinserting all |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
583 items again. When entries have been deleted, the new table may |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
584 actually be smaller than the old one. |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
585 */ |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
586 static int |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
587 dictresize(PyDictObject *mp, Py_ssize_t minused) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
588 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
589 Py_ssize_t newsize; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
590 PyDictEntry *oldtable, *newtable, *ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
591 Py_ssize_t i; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
592 int is_oldtable_malloced; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
593 PyDictEntry small_copy[PyDict_MINSIZE]; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
594 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
595 assert(minused >= 0); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
596 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
597 /* Find the smallest table size > minused. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
598 for (newsize = PyDict_MINSIZE; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
599 newsize <= minused && newsize > 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
600 newsize <<= 1) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
601 ; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
602 if (newsize <= 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
603 PyErr_NoMemory(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
604 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
605 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
606 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
607 /* Get space for a new table. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
608 oldtable = mp->ma_table; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
609 assert(oldtable != NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
610 is_oldtable_malloced = oldtable != mp->ma_smalltable; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
611 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
612 if (newsize == PyDict_MINSIZE) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
613 /* A large table is shrinking, or we can't get any smaller. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
614 newtable = mp->ma_smalltable; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
615 if (newtable == oldtable) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
616 if (mp->ma_fill == mp->ma_used) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
617 /* No dummies, so no point doing anything. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
618 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
619 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
620 /* We're not going to resize it, but rebuild the |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
621 table anyway to purge old dummy entries. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
622 Subtle: This is *necessary* if fill==size, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
623 as lookdict needs at least one virgin slot to |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
624 terminate failing searches. If fill < size, it's |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
625 merely desirable, as dummies slow searches. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
626 assert(mp->ma_fill > mp->ma_used); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
627 memcpy(small_copy, oldtable, sizeof(small_copy)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
628 oldtable = small_copy; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
629 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
630 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
631 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
632 newtable = PyMem_NEW(PyDictEntry, newsize); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
633 if (newtable == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
634 PyErr_NoMemory(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
635 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
636 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
637 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
638 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
639 /* Make the dict empty, using the new table. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
640 assert(newtable != oldtable); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
641 mp->ma_table = newtable; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
642 mp->ma_mask = newsize - 1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
643 memset(newtable, 0, sizeof(PyDictEntry) * newsize); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
644 mp->ma_used = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
645 i = mp->ma_fill; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
646 mp->ma_fill = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
647 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
648 /* Copy the data over; this is refcount-neutral for active entries; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
649 dummy entries aren't copied over, of course */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
650 for (ep = oldtable; i > 0; ep++) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
651 if (ep->me_value != NULL) { /* active entry */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
652 --i; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
653 insertdict_clean(mp, ep->me_key, (long)ep->me_hash, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
654 ep->me_value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
655 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
656 else if (ep->me_key != NULL) { /* dummy entry */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
657 --i; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
658 assert(ep->me_key == dummy); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
659 Py_DECREF(ep->me_key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
660 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
661 /* else key == value == NULL: nothing to do */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
662 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
663 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
664 if (is_oldtable_malloced) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
665 PyMem_DEL(oldtable); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
666 return 0; |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
667 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
668 |
|
44339
7ac117b3be37
Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary.
Raymond Hettinger <python@rcn.com>
parents:
44056
diff
changeset
|
669 /* Create a new dictionary pre-sized to hold an estimated number of elements. |
|
7ac117b3be37
Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary.
Raymond Hettinger <python@rcn.com>
parents:
44056
diff
changeset
|
670 Underestimates are okay because the dictionary will resize as necessary. |
|
7ac117b3be37
Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary.
Raymond Hettinger <python@rcn.com>
parents:
44056
diff
changeset
|
671 Overestimates just mean the dictionary will be more sparse than usual. |
|
7ac117b3be37
Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary.
Raymond Hettinger <python@rcn.com>
parents:
44056
diff
changeset
|
672 */ |
|
7ac117b3be37
Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary.
Raymond Hettinger <python@rcn.com>
parents:
44056
diff
changeset
|
673 |
|
7ac117b3be37
Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary.
Raymond Hettinger <python@rcn.com>
parents:
44056
diff
changeset
|
674 PyObject * |
|
7ac117b3be37
Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary.
Raymond Hettinger <python@rcn.com>
parents:
44056
diff
changeset
|
675 _PyDict_NewPresized(Py_ssize_t minused) |
|
7ac117b3be37
Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary.
Raymond Hettinger <python@rcn.com>
parents:
44056
diff
changeset
|
676 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
677 PyObject *op = PyDict_New(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
678 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
679 if (minused>5 && op != NULL && dictresize((PyDictObject *)op, minused) == -1) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
680 Py_DECREF(op); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
681 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
682 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
683 return op; |
|
44339
7ac117b3be37
Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary.
Raymond Hettinger <python@rcn.com>
parents:
44056
diff
changeset
|
684 } |
|
7ac117b3be37
Give meaning to the oparg for BUILD_MAP: estimated size of the dictionary.
Raymond Hettinger <python@rcn.com>
parents:
44056
diff
changeset
|
685 |
|
38540
d7dd528427c0
Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents:
38536
diff
changeset
|
686 /* Note that, for historical reasons, PyDict_GetItem() suppresses all errors |
|
d7dd528427c0
Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents:
38536
diff
changeset
|
687 * that may occur (originally dicts supported only string keys, and exceptions |
|
d7dd528427c0
Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents:
38536
diff
changeset
|
688 * weren't possible). So, while the original intent was that a NULL return |
| 39376 | 689 * meant the key wasn't present, in reality it can mean that, or that an error |
|
38540
d7dd528427c0
Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents:
38536
diff
changeset
|
690 * (suppressed) occurred while computing the key's hash, or that some error |
|
d7dd528427c0
Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents:
38536
diff
changeset
|
691 * (suppressed) occurred when comparing keys in the dict's internal probe |
|
d7dd528427c0
Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents:
38536
diff
changeset
|
692 * sequence. A nasty example of the latter is when a Python-coded comparison |
|
d7dd528427c0
Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents:
38536
diff
changeset
|
693 * function hits a stack-depth error, which can cause this to return NULL |
|
d7dd528427c0
Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents:
38536
diff
changeset
|
694 * even if the key is present. |
|
d7dd528427c0
Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents:
38536
diff
changeset
|
695 */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
696 PyObject * |
|
13341
3f1bd4782628
Removed Py_PROTO and switched to ANSI C declarations in the dict
Tim Peters <tim.peters@gmail.com>
parents:
13265
diff
changeset
|
697 PyDict_GetItem(PyObject *op, PyObject *key) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
698 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
699 long hash; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
700 PyDictObject *mp = (PyDictObject *)op; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
701 PyDictEntry *ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
702 PyThreadState *tstate; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
703 if (!PyDict_Check(op)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
704 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
705 if (!PyString_CheckExact(key) || |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
706 (hash = ((PyStringObject *) key)->ob_shash) == -1) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
707 { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
708 hash = PyObject_Hash(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
709 if (hash == -1) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
710 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
711 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
712 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
713 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
714 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
715 /* We can arrive here with a NULL tstate during initialization: try |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
716 running "python -Wi" for an example related to string interning. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
717 Let's just hope that no exception occurs then... This must be |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
718 _PyThreadState_Current and not PyThreadState_GET() because in debug |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
719 mode, the latter complains if tstate is NULL. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
720 tstate = _PyThreadState_Current; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
721 if (tstate != NULL && tstate->curexc_type != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
722 /* preserve the existing exception */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
723 PyObject *err_type, *err_value, *err_tb; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
724 PyErr_Fetch(&err_type, &err_value, &err_tb); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
725 ep = (mp->ma_lookup)(mp, key, hash); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
726 /* ignore errors */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
727 PyErr_Restore(err_type, err_value, err_tb); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
728 if (ep == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
729 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
730 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
731 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
732 ep = (mp->ma_lookup)(mp, key, hash); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
733 if (ep == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
734 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
735 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
736 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
737 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
738 return ep->me_value; |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
739 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
740 |
|
17535
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
741 /* CAUTION: PyDict_SetItem() must guarantee that it won't resize the |
|
36164
50886d42b444
Fixed English in a comment; trimmed trailing whitespace;
Tim Peters <tim.peters@gmail.com>
parents:
35636
diff
changeset
|
742 * dictionary if it's merely replacing the value for an existing key. |
|
50886d42b444
Fixed English in a comment; trimmed trailing whitespace;
Tim Peters <tim.peters@gmail.com>
parents:
35636
diff
changeset
|
743 * This means that it's safe to loop over a dictionary with PyDict_Next() |
|
50886d42b444
Fixed English in a comment; trimmed trailing whitespace;
Tim Peters <tim.peters@gmail.com>
parents:
35636
diff
changeset
|
744 * and occasionally replace a value -- but you can't insert new keys or |
|
50886d42b444
Fixed English in a comment; trimmed trailing whitespace;
Tim Peters <tim.peters@gmail.com>
parents:
35636
diff
changeset
|
745 * remove them. |
|
17535
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
746 */ |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
747 int |
|
13341
3f1bd4782628
Removed Py_PROTO and switched to ANSI C declarations in the dict
Tim Peters <tim.peters@gmail.com>
parents:
13265
diff
changeset
|
748 PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
749 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
750 register PyDictObject *mp; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
751 register long hash; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
752 register Py_ssize_t n_used; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
753 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
754 if (!PyDict_Check(op)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
755 PyErr_BadInternalCall(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
756 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
757 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
758 assert(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
759 assert(value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
760 mp = (PyDictObject *)op; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
761 if (PyString_CheckExact(key)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
762 hash = ((PyStringObject *)key)->ob_shash; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
763 if (hash == -1) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
764 hash = PyObject_Hash(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
765 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
766 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
767 hash = PyObject_Hash(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
768 if (hash == -1) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
769 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
770 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
771 assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
772 n_used = mp->ma_used; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
773 Py_INCREF(value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
774 Py_INCREF(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
775 if (insertdict(mp, key, hash, value) != 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
776 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
777 /* If we added a key, we can safely resize. Otherwise just return! |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
778 * If fill >= 2/3 size, adjust size. Normally, this doubles or |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
779 * quaduples the size, but it's also possible for the dict to shrink |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
780 * (if ma_fill is much larger than ma_used, meaning a lot of dict |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
781 * keys have been * deleted). |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
782 * |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
783 * Quadrupling the size improves average dictionary sparseness |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
784 * (reducing collisions) at the cost of some memory and iteration |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
785 * speed (which loops over every possible entry). It also halves |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
786 * the number of expensive resize operations in a growing dictionary. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
787 * |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
788 * Very large dictionaries (over 50K items) use doubling instead. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
789 * This may help applications with severe memory constraints. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
790 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
791 if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
792 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
793 return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used); |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
794 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
795 |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
796 int |
|
13341
3f1bd4782628
Removed Py_PROTO and switched to ANSI C declarations in the dict
Tim Peters <tim.peters@gmail.com>
parents:
13265
diff
changeset
|
797 PyDict_DelItem(PyObject *op, PyObject *key) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
798 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
799 register PyDictObject *mp; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
800 register long hash; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
801 register PyDictEntry *ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
802 PyObject *old_value, *old_key; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
803 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
804 if (!PyDict_Check(op)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
805 PyErr_BadInternalCall(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
806 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
807 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
808 assert(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
809 if (!PyString_CheckExact(key) || |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
810 (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
811 hash = PyObject_Hash(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
812 if (hash == -1) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
813 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
814 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
815 mp = (PyDictObject *)op; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
816 ep = (mp->ma_lookup)(mp, key, hash); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
817 if (ep == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
818 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
819 if (ep->me_value == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
820 set_key_error(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
821 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
822 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
823 old_key = ep->me_key; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
824 Py_INCREF(dummy); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
825 ep->me_key = dummy; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
826 old_value = ep->me_value; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
827 ep->me_value = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
828 mp->ma_used--; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
829 Py_DECREF(old_value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
830 Py_DECREF(old_key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
831 return 0; |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
832 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
833 |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
834 void |
|
13341
3f1bd4782628
Removed Py_PROTO and switched to ANSI C declarations in the dict
Tim Peters <tim.peters@gmail.com>
parents:
13265
diff
changeset
|
835 PyDict_Clear(PyObject *op) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
836 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
837 PyDictObject *mp; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
838 PyDictEntry *ep, *table; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
839 int table_is_malloced; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
840 Py_ssize_t fill; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
841 PyDictEntry small_copy[PyDict_MINSIZE]; |
|
17920
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
842 #ifdef Py_DEBUG |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
843 Py_ssize_t i, n; |
|
17920
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
844 #endif |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
845 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
846 if (!PyDict_Check(op)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
847 return; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
848 mp = (PyDictObject *)op; |
|
17920
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
849 #ifdef Py_DEBUG |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
850 n = mp->ma_mask + 1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
851 i = 0; |
|
17920
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
852 #endif |
|
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
853 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
854 table = mp->ma_table; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
855 assert(table != NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
856 table_is_malloced = table != mp->ma_smalltable; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
857 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
858 /* This is delicate. During the process of clearing the dict, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
859 * decrefs can cause the dict to mutate. To avoid fatal confusion |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
860 * (voice of experience), we have to make the dict empty before |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
861 * clearing the slots, and never refer to anything via mp->xxx while |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
862 * clearing. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
863 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
864 fill = mp->ma_fill; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
865 if (table_is_malloced) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
866 EMPTY_TO_MINSIZE(mp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
867 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
868 else if (fill > 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
869 /* It's a small table with something that needs to be cleared. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
870 * Afraid the only safe way is to copy the dict entries into |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
871 * another small table first. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
872 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
873 memcpy(small_copy, table, sizeof(small_copy)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
874 table = small_copy; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
875 EMPTY_TO_MINSIZE(mp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
876 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
877 /* else it's a small table that's already empty */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
878 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
879 /* Now we can finally clear things. If C had refcounts, we could |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
880 * assert that the refcount on table is 1 now, i.e. that this function |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
881 * has unique access to it, so decref side-effects can't alter it. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
882 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
883 for (ep = table; fill > 0; ++ep) { |
|
17920
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
884 #ifdef Py_DEBUG |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
885 assert(i < n); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
886 ++i; |
|
17920
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
887 #endif |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
888 if (ep->me_key) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
889 --fill; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
890 Py_DECREF(ep->me_key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
891 Py_XDECREF(ep->me_value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
892 } |
|
17920
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
893 #ifdef Py_DEBUG |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
894 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
895 assert(ep->me_value == NULL); |
|
17920
16005b285576
SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents:
17861
diff
changeset
|
896 #endif |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
897 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
898 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
899 if (table_is_malloced) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
900 PyMem_DEL(table); |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
901 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
902 |
|
27901
55adf08a213a
cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents:
26818
diff
changeset
|
903 /* |
|
55adf08a213a
cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents:
26818
diff
changeset
|
904 * Iterate over a dict. Use like so: |
|
55adf08a213a
cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents:
26818
diff
changeset
|
905 * |
|
38499
494cab563c11
Convert relevant dict internals to Py_ssize_t.
Tim Peters <tim.peters@gmail.com>
parents:
37756
diff
changeset
|
906 * Py_ssize_t i; |
|
27901
55adf08a213a
cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents:
26818
diff
changeset
|
907 * PyObject *key, *value; |
|
55adf08a213a
cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents:
26818
diff
changeset
|
908 * i = 0; # important! i should not otherwise be changed by you |
|
27902
81b2bcbfe39b
Add closing ) in comment
Neal Norwitz <nnorwitz@gmail.com>
parents:
27901
diff
changeset
|
909 * while (PyDict_Next(yourdict, &i, &key, &value)) { |
|
27901
55adf08a213a
cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents:
26818
diff
changeset
|
910 * Refer to borrowed references in key and value. |
|
55adf08a213a
cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents:
26818
diff
changeset
|
911 * } |
|
55adf08a213a
cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents:
26818
diff
changeset
|
912 * |
|
55adf08a213a
cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents:
26818
diff
changeset
|
913 * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that |
|
17115
e1214f9e12a3
Make PyDict_Next safe to use for loops that merely modify the values
Tim Peters <tim.peters@gmail.com>
parents:
15974
diff
changeset
|
914 * mutates the dict. One exception: it is safe if the loop merely changes |
|
e1214f9e12a3
Make PyDict_Next safe to use for loops that merely modify the values
Tim Peters <tim.peters@gmail.com>
parents:
15974
diff
changeset
|
915 * the values associated with the keys (but doesn't insert new keys or |
|
e1214f9e12a3
Make PyDict_Next safe to use for loops that merely modify the values
Tim Peters <tim.peters@gmail.com>
parents:
15974
diff
changeset
|
916 * delete keys), via PyDict_SetItem(). |
|
e1214f9e12a3
Make PyDict_Next safe to use for loops that merely modify the values
Tim Peters <tim.peters@gmail.com>
parents:
15974
diff
changeset
|
917 */ |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
918 int |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36484
diff
changeset
|
919 PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
920 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
921 register Py_ssize_t i; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
922 register Py_ssize_t mask; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
923 register PyDictEntry *ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
924 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
925 if (!PyDict_Check(op)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
926 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
927 i = *ppos; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
928 if (i < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
929 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
930 ep = ((PyDictObject *)op)->ma_table; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
931 mask = ((PyDictObject *)op)->ma_mask; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
932 while (i <= mask && ep[i].me_value == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
933 i++; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
934 *ppos = i+1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
935 if (i > mask) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
936 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
937 if (pkey) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
938 *pkey = ep[i].me_key; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
939 if (pvalue) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
940 *pvalue = ep[i].me_value; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
941 return 1; |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
942 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
943 |
|
40864
b0f8e9b0cc19
Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents:
40756
diff
changeset
|
944 /* Internal version of PyDict_Next that returns a hash value in addition to the key and value.*/ |
|
b0f8e9b0cc19
Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents:
40756
diff
changeset
|
945 int |
|
b0f8e9b0cc19
Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents:
40756
diff
changeset
|
946 _PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, long *phash) |
|
b0f8e9b0cc19
Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents:
40756
diff
changeset
|
947 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
948 register Py_ssize_t i; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
949 register Py_ssize_t mask; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
950 register PyDictEntry *ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
951 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
952 if (!PyDict_Check(op)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
953 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
954 i = *ppos; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
955 if (i < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
956 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
957 ep = ((PyDictObject *)op)->ma_table; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
958 mask = ((PyDictObject *)op)->ma_mask; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
959 while (i <= mask && ep[i].me_value == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
960 i++; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
961 *ppos = i+1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
962 if (i > mask) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
963 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
964 *phash = (long)(ep[i].me_hash); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
965 if (pkey) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
966 *pkey = ep[i].me_key; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
967 if (pvalue) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
968 *pvalue = ep[i].me_value; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
969 return 1; |
|
40864
b0f8e9b0cc19
Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents:
40756
diff
changeset
|
970 } |
|
b0f8e9b0cc19
Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents:
40756
diff
changeset
|
971 |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
972 /* Methods */ |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
973 |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
974 static void |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
975 dict_dealloc(register PyDictObject *mp) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
976 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
977 register PyDictEntry *ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
978 Py_ssize_t fill = mp->ma_fill; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
979 PyObject_GC_UnTrack(mp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
980 Py_TRASHCAN_SAFE_BEGIN(mp) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
981 for (ep = mp->ma_table; fill > 0; ep++) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
982 if (ep->me_key) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
983 --fill; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
984 Py_DECREF(ep->me_key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
985 Py_XDECREF(ep->me_value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
986 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
987 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
988 if (mp->ma_table != mp->ma_smalltable) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
989 PyMem_DEL(mp->ma_table); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
990 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
991 free_list[numfree++] = mp; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
992 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
993 Py_TYPE(mp)->tp_free((PyObject *)mp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
994 Py_TRASHCAN_SAFE_END(mp) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
995 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
996 |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
997 static int |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
998 dict_print(register PyDictObject *mp, register FILE *fp, register int flags) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
999 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1000 register Py_ssize_t i; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1001 register Py_ssize_t any; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1002 int status; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1003 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1004 status = Py_ReprEnter((PyObject*)mp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1005 if (status != 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1006 if (status < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1007 return status; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1008 Py_BEGIN_ALLOW_THREADS |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1009 fprintf(fp, "{...}"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1010 Py_END_ALLOW_THREADS |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1011 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1012 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1013 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1014 Py_BEGIN_ALLOW_THREADS |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1015 fprintf(fp, "{"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1016 Py_END_ALLOW_THREADS |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1017 any = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1018 for (i = 0; i <= mp->ma_mask; i++) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1019 PyDictEntry *ep = mp->ma_table + i; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1020 PyObject *pvalue = ep->me_value; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1021 if (pvalue != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1022 /* Prevent PyObject_Repr from deleting value during |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1023 key format */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1024 Py_INCREF(pvalue); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1025 if (any++ > 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1026 Py_BEGIN_ALLOW_THREADS |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1027 fprintf(fp, ", "); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1028 Py_END_ALLOW_THREADS |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1029 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1030 if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1031 Py_DECREF(pvalue); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1032 Py_ReprLeave((PyObject*)mp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1033 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1034 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1035 Py_BEGIN_ALLOW_THREADS |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1036 fprintf(fp, ": "); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1037 Py_END_ALLOW_THREADS |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1038 if (PyObject_Print(pvalue, fp, 0) != 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1039 Py_DECREF(pvalue); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1040 Py_ReprLeave((PyObject*)mp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1041 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1042 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1043 Py_DECREF(pvalue); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1044 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1045 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1046 Py_BEGIN_ALLOW_THREADS |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1047 fprintf(fp, "}"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1048 Py_END_ALLOW_THREADS |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1049 Py_ReprLeave((PyObject*)mp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1050 return 0; |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1051 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1052 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
1053 static PyObject * |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
1054 dict_repr(PyDictObject *mp) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1055 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1056 Py_ssize_t i; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1057 PyObject *s, *temp, *colon = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1058 PyObject *pieces = NULL, *result = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1059 PyObject *key, *value; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1060 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1061 i = Py_ReprEnter((PyObject *)mp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1062 if (i != 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1063 return i > 0 ? PyString_FromString("{...}") : NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1064 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1065 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1066 if (mp->ma_used == 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1067 result = PyString_FromString("{}"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1068 goto Done; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1069 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1070 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1071 pieces = PyList_New(0); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1072 if (pieces == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1073 goto Done; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1074 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1075 colon = PyString_FromString(": "); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1076 if (colon == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1077 goto Done; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1078 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1079 /* Do repr() on each key+value pair, and insert ": " between them. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1080 Note that repr may mutate the dict. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1081 i = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1082 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1083 int status; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1084 /* Prevent repr from deleting value during key format. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1085 Py_INCREF(value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1086 s = PyObject_Repr(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1087 PyString_Concat(&s, colon); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1088 PyString_ConcatAndDel(&s, PyObject_Repr(value)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1089 Py_DECREF(value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1090 if (s == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1091 goto Done; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1092 status = PyList_Append(pieces, s); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1093 Py_DECREF(s); /* append created a new ref */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1094 if (status < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1095 goto Done; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1096 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1097 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1098 /* Add "{}" decorations to the first and last items. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1099 assert(PyList_GET_SIZE(pieces) > 0); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1100 s = PyString_FromString("{"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1101 if (s == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1102 goto Done; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1103 temp = PyList_GET_ITEM(pieces, 0); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1104 PyString_ConcatAndDel(&s, temp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1105 PyList_SET_ITEM(pieces, 0, s); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1106 if (s == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1107 goto Done; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1108 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1109 s = PyString_FromString("}"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1110 if (s == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1111 goto Done; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1112 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1113 PyString_ConcatAndDel(&temp, s); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1114 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1115 if (temp == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1116 goto Done; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1117 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1118 /* Paste them all together with ", " between. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1119 s = PyString_FromString(", "); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1120 if (s == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1121 goto Done; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1122 result = _PyString_Join(s, pieces); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1123 Py_DECREF(s); |
|
18129
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
1124 |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18032
diff
changeset
|
1125 Done: |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1126 Py_XDECREF(pieces); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1127 Py_XDECREF(colon); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1128 Py_ReprLeave((PyObject *)mp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1129 return result; |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1130 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1131 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36484
diff
changeset
|
1132 static Py_ssize_t |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
1133 dict_length(PyDictObject *mp) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1134 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1135 return mp->ma_used; |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1136 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1137 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
1138 static PyObject * |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
1139 dict_subscript(PyDictObject *mp, register PyObject *key) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1140 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1141 PyObject *v; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1142 long hash; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1143 PyDictEntry *ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1144 assert(mp->ma_table != NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1145 if (!PyString_CheckExact(key) || |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1146 (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1147 hash = PyObject_Hash(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1148 if (hash == -1) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1149 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1150 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1151 ep = (mp->ma_lookup)(mp, key, hash); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1152 if (ep == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1153 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1154 v = ep->me_value; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1155 if (v == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1156 if (!PyDict_CheckExact(mp)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1157 /* Look up __missing__ method if we're a subclass. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1158 PyObject *missing, *res; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1159 static PyObject *missing_str = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1160 missing = _PyObject_LookupSpecial((PyObject *)mp, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1161 "__missing__", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1162 &missing_str); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1163 if (missing != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1164 res = PyObject_CallFunctionObjArgs(missing, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1165 key, NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1166 Py_DECREF(missing); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1167 return res; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1168 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1169 else if (PyErr_Occurred()) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1170 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1171 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1172 set_key_error(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1173 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1174 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1175 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1176 Py_INCREF(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1177 return v; |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1178 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1179 |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1180 static int |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
1181 dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1182 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1183 if (w == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1184 return PyDict_DelItem((PyObject *)mp, v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1185 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1186 return PyDict_SetItem((PyObject *)mp, v, w); |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1187 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1188 |
|
5505
47fa00877924
Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1189 static PyMappingMethods dict_as_mapping = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1190 (lenfunc)dict_length, /*mp_length*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1191 (binaryfunc)dict_subscript, /*mp_subscript*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1192 (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/ |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1193 }; |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1194 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
1195 static PyObject * |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
1196 dict_keys(register PyDictObject *mp) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1197 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1198 register PyObject *v; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1199 register Py_ssize_t i, j; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1200 PyDictEntry *ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1201 Py_ssize_t mask, n; |
|
17535
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
1202 |
|
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
1203 again: |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1204 n = mp->ma_used; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1205 v = PyList_New(n); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1206 if (v == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1207 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1208 if (n != mp->ma_used) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1209 /* Durnit. The allocations caused the dict to resize. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1210 * Just start over, this shouldn't normally happen. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1211 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1212 Py_DECREF(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1213 goto again; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1214 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1215 ep = mp->ma_table; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1216 mask = mp->ma_mask; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1217 for (i = 0, j = 0; i <= mask; i++) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1218 if (ep[i].me_value != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1219 PyObject *key = ep[i].me_key; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1220 Py_INCREF(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1221 PyList_SET_ITEM(v, j, key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1222 j++; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1223 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1224 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1225 assert(j == n); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1226 return v; |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1227 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1228 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
1229 static PyObject * |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
1230 dict_values(register PyDictObject *mp) |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1231 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1232 register PyObject *v; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1233 register Py_ssize_t i, j; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1234 PyDictEntry *ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1235 Py_ssize_t mask, n; |
|
17535
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
1236 |
|
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
1237 again: |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1238 n = mp->ma_used; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1239 v = PyList_New(n); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1240 if (v == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1241 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1242 if (n != mp->ma_used) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1243 /* Durnit. The allocations caused the dict to resize. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1244 * Just start over, this shouldn't normally happen. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1245 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1246 Py_DECREF(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1247 goto again; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1248 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1249 ep = mp->ma_table; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1250 mask = mp->ma_mask; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1251 for (i = 0, j = 0; i <= mask; i++) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1252 if (ep[i].me_value != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1253 PyObject *value = ep[i].me_value; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1254 Py_INCREF(value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1255 PyList_SET_ITEM(v, j, value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1256 j++; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1257 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1258 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1259 assert(j == n); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1260 return v; |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1261 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1262 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
1263 static PyObject * |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
1264 dict_items(register PyDictObject *mp) |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1265 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1266 register PyObject *v; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1267 register Py_ssize_t i, j, n; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1268 Py_ssize_t mask; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1269 PyObject *item, *key, *value; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1270 PyDictEntry *ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1271 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1272 /* Preallocate the list of tuples, to avoid allocations during |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1273 * the loop over the items, which could trigger GC, which |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1274 * could resize the dict. :-( |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1275 */ |
|
17535
83c7b4b75668
Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents:
17115
diff
changeset
|
1276 again: |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1277 n = mp->ma_used; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1278 v = PyList_New(n); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1279 if (v == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1280 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1281 for (i = 0; i < n; i++) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1282 item = PyTuple_New(2); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1283 if (item == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1284 Py_DECREF(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1285 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1286 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1287 PyList_SET_ITEM(v, i, item); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1288 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1289 if (n != mp->ma_used) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1290 /* Durnit. The allocations caused the dict to resize. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1291 * Just start over, this shouldn't normally happen. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1292 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1293 Py_DECREF(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1294 goto again; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1295 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1296 /* Nothing we do below makes any function calls. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1297 ep = mp->ma_table; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1298 mask = mp->ma_mask; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1299 for (i = 0, j = 0; i <= mask; i++) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1300 if ((value=ep[i].me_value) != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1301 key = ep[i].me_key; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1302 item = PyList_GET_ITEM(v, j); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1303 Py_INCREF(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1304 PyTuple_SET_ITEM(item, 0, key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1305 Py_INCREF(value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1306 PyTuple_SET_ITEM(item, 1, value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1307 j++; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1308 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1309 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1310 assert(j == n); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1311 return v; |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1312 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1313 |
|
5658
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1314 static PyObject * |
|
26583
8991f78817c7
SF 548651: Fix the METH_CLASS implementation.
Tim Peters <tim.peters@gmail.com>
parents:
26562
diff
changeset
|
1315 dict_fromkeys(PyObject *cls, PyObject *args) |
|
26437
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
1316 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1317 PyObject *seq; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1318 PyObject *value = Py_None; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1319 PyObject *it; /* iter(seq) */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1320 PyObject *key; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1321 PyObject *d; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1322 int status; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1323 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1324 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1325 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1326 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1327 d = PyObject_CallObject(cls, NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1328 if (d == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1329 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1330 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1331 if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1332 PyDictObject *mp = (PyDictObject *)d; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1333 PyObject *oldvalue; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1334 Py_ssize_t pos = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1335 PyObject *key; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1336 long hash; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1337 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1338 if (dictresize(mp, Py_SIZE(seq))) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1339 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1340 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1341 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1342 Py_INCREF(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1343 Py_INCREF(value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1344 if (insertdict(mp, key, hash, value)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1345 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1346 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1347 return d; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1348 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1349 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1350 if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1351 PyDictObject *mp = (PyDictObject *)d; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1352 Py_ssize_t pos = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1353 PyObject *key; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1354 long hash; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1355 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1356 if (dictresize(mp, PySet_GET_SIZE(seq))) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1357 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1358 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1359 while (_PySet_NextEntry(seq, &pos, &key, &hash)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1360 Py_INCREF(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1361 Py_INCREF(value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1362 if (insertdict(mp, key, hash, value)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1363 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1364 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1365 return d; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1366 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1367 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1368 it = PyObject_GetIter(seq); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1369 if (it == NULL){ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1370 Py_DECREF(d); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1371 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1372 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1373 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1374 if (PyDict_CheckExact(d)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1375 while ((key = PyIter_Next(it)) != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1376 status = PyDict_SetItem(d, key, value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1377 Py_DECREF(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1378 if (status < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1379 goto Fail; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1380 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1381 } else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1382 while ((key = PyIter_Next(it)) != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1383 status = PyObject_SetItem(d, key, value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1384 Py_DECREF(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1385 if (status < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1386 goto Fail; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1387 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1388 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1389 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1390 if (PyErr_Occurred()) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1391 goto Fail; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1392 Py_DECREF(it); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1393 return d; |
|
26437
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
1394 |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
1395 Fail: |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1396 Py_DECREF(it); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1397 Py_DECREF(d); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1398 return NULL; |
|
26437
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
1399 } |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
1400 |
|
31616
5b3898d9ba59
SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents:
31545
diff
changeset
|
1401 static int |
|
5b3898d9ba59
SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents:
31545
diff
changeset
|
1402 dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname) |
|
5b3898d9ba59
SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents:
31545
diff
changeset
|
1403 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1404 PyObject *arg = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1405 int result = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1406 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1407 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1408 result = -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1409 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1410 else if (arg != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1411 if (PyObject_HasAttrString(arg, "keys")) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1412 result = PyDict_Merge(self, arg, 1); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1413 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1414 result = PyDict_MergeFromSeq2(self, arg, 1); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1415 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1416 if (result == 0 && kwds != NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1417 result = PyDict_Merge(self, kwds, 1); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1418 return result; |
|
31616
5b3898d9ba59
SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents:
31545
diff
changeset
|
1419 } |
|
5b3898d9ba59
SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents:
31545
diff
changeset
|
1420 |
|
26437
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
1421 static PyObject * |
|
31616
5b3898d9ba59
SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents:
31545
diff
changeset
|
1422 dict_update(PyObject *self, PyObject *args, PyObject *kwds) |
|
5658
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1423 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1424 if (dict_update_common(self, args, kwds, "update") != -1) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1425 Py_RETURN_NONE; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1426 return NULL; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1427 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1428 |
|
19150
3f34f724f014
Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1429 /* Update unconditionally replaces existing items. |
|
3f34f724f014
Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1430 Merge has a 3rd argument 'override'; if set, it acts like Update, |
|
20683
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1431 otherwise it leaves existing items unchanged. |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1432 |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1433 PyDict_{Update,Merge} update/merge from a mapping object. |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1434 |
|
21348
12310d643d8d
SF bug #491415 PyDict_UpdateFromSeq2() unused
Tim Peters <tim.peters@gmail.com>
parents:
21155
diff
changeset
|
1435 PyDict_MergeFromSeq2 updates/merges from any iterable object |
|
20683
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1436 producing iterable objects of length 2. |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1437 */ |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1438 |
|
21348
12310d643d8d
SF bug #491415 PyDict_UpdateFromSeq2() unused
Tim Peters <tim.peters@gmail.com>
parents:
21155
diff
changeset
|
1439 int |
|
20683
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1440 PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override) |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1441 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1442 PyObject *it; /* iter(seq2) */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1443 Py_ssize_t i; /* index into seq2 of current element */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1444 PyObject *item; /* seq2[i] */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1445 PyObject *fast; /* item as a 2-tuple or 2-list */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1446 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1447 assert(d != NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1448 assert(PyDict_Check(d)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1449 assert(seq2 != NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1450 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1451 it = PyObject_GetIter(seq2); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1452 if (it == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1453 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1454 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1455 for (i = 0; ; ++i) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1456 PyObject *key, *value; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1457 Py_ssize_t n; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1458 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1459 fast = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1460 item = PyIter_Next(it); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1461 if (item == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1462 if (PyErr_Occurred()) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1463 goto Fail; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1464 break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1465 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1466 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1467 /* Convert item to sequence, and verify length 2. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1468 fast = PySequence_Fast(item, ""); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1469 if (fast == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1470 if (PyErr_ExceptionMatches(PyExc_TypeError)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1471 PyErr_Format(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1472 "cannot convert dictionary update " |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1473 "sequence element #%zd to a sequence", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1474 i); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1475 goto Fail; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1476 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1477 n = PySequence_Fast_GET_SIZE(fast); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1478 if (n != 2) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1479 PyErr_Format(PyExc_ValueError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1480 "dictionary update sequence element #%zd " |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1481 "has length %zd; 2 is required", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1482 i, n); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1483 goto Fail; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1484 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1485 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1486 /* Update/merge with this (key, value) pair. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1487 key = PySequence_Fast_GET_ITEM(fast, 0); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1488 value = PySequence_Fast_GET_ITEM(fast, 1); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1489 if (override || PyDict_GetItem(d, key) == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1490 int status = PyDict_SetItem(d, key, value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1491 if (status < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1492 goto Fail; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1493 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1494 Py_DECREF(fast); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1495 Py_DECREF(item); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1496 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1497 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1498 i = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1499 goto Return; |
|
20683
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1500 Fail: |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1501 Py_XDECREF(item); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1502 Py_XDECREF(fast); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1503 i = -1; |
|
20683
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1504 Return: |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1505 Py_DECREF(it); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1506 return Py_SAFE_DOWNCAST(i, Py_ssize_t, int); |
|
20683
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1507 } |
|
505dd34e0eeb
Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
1508 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1509 int |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1510 PyDict_Update(PyObject *a, PyObject *b) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
1511 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1512 return PyDict_Merge(a, b, 1); |
|
19150
3f34f724f014
Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1513 } |
|
3f34f724f014
Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1514 |
|
3f34f724f014
Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1515 int |
|
3f34f724f014
Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1516 PyDict_Merge(PyObject *a, PyObject *b, int override) |
|
3f34f724f014
Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1517 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1518 register PyDictObject *mp, *other; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1519 register Py_ssize_t i; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1520 PyDictEntry *entry; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1521 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1522 /* We accept for the argument either a concrete dictionary object, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1523 * or an abstract "mapping" object. For the former, we can do |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1524 * things quite efficiently. For the latter, we only require that |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1525 * PyMapping_Keys() and PyObject_GetItem() be supported. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1526 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1527 if (a == NULL || !PyDict_Check(a) || b == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1528 PyErr_BadInternalCall(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1529 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1530 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1531 mp = (PyDictObject*)a; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1532 if (PyDict_Check(b)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1533 other = (PyDictObject*)b; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1534 if (other == mp || other->ma_used == 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1535 /* a.update(a) or a.update({}); nothing to do */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1536 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1537 if (mp->ma_used == 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1538 /* Since the target dict is empty, PyDict_GetItem() |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1539 * always returns NULL. Setting override to 1 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1540 * skips the unnecessary test. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1541 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1542 override = 1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1543 /* Do one big resize at the start, rather than |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1544 * incrementally resizing as we insert new items. Expect |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1545 * that there will be no (or few) overlapping keys. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1546 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1547 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1548 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1549 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1550 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1551 for (i = 0; i <= other->ma_mask; i++) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1552 entry = &other->ma_table[i]; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1553 if (entry->me_value != NULL && |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1554 (override || |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1555 PyDict_GetItem(a, entry->me_key) == NULL)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1556 Py_INCREF(entry->me_key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1557 Py_INCREF(entry->me_value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1558 if (insertdict(mp, entry->me_key, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1559 (long)entry->me_hash, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1560 entry->me_value) != 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1561 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1562 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1563 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1564 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1565 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1566 /* Do it the generic, slower way */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1567 PyObject *keys = PyMapping_Keys(b); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1568 PyObject *iter; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1569 PyObject *key, *value; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1570 int status; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1571 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1572 if (keys == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1573 /* Docstring says this is equivalent to E.keys() so |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1574 * if E doesn't have a .keys() method we want |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1575 * AttributeError to percolate up. Might as well |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1576 * do the same for any other error. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1577 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1578 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1579 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1580 iter = PyObject_GetIter(keys); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1581 Py_DECREF(keys); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1582 if (iter == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1583 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1584 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1585 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1586 if (!override && PyDict_GetItem(a, key) != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1587 Py_DECREF(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1588 continue; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1589 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1590 value = PyObject_GetItem(b, key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1591 if (value == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1592 Py_DECREF(iter); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1593 Py_DECREF(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1594 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1595 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1596 status = PyDict_SetItem(a, key, value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1597 Py_DECREF(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1598 Py_DECREF(value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1599 if (status < 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1600 Py_DECREF(iter); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1601 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1602 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1603 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1604 Py_DECREF(iter); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1605 if (PyErr_Occurred()) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1606 /* Iterator completed, via error */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1607 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1608 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1609 return 0; |
|
5658
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1610 } |
|
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1611 |
|
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1612 static PyObject * |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
1613 dict_copy(register PyDictObject *mp) |
|
5658
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1614 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1615 return PyDict_Copy((PyObject*)mp); |
|
12126
e6214ad2a60c
Add PyDict_Copy() function to C API for dicts. It returns a new
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11999
diff
changeset
|
1616 } |
|
e6214ad2a60c
Add PyDict_Copy() function to C API for dicts. It returns a new
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11999
diff
changeset
|
1617 |
|
e6214ad2a60c
Add PyDict_Copy() function to C API for dicts. It returns a new
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11999
diff
changeset
|
1618 PyObject * |
|
13341
3f1bd4782628
Removed Py_PROTO and switched to ANSI C declarations in the dict
Tim Peters <tim.peters@gmail.com>
parents:
13265
diff
changeset
|
1619 PyDict_Copy(PyObject *o) |
|
12126
e6214ad2a60c
Add PyDict_Copy() function to C API for dicts. It returns a new
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11999
diff
changeset
|
1620 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1621 PyObject *copy; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1622 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1623 if (o == NULL || !PyDict_Check(o)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1624 PyErr_BadInternalCall(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1625 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1626 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1627 copy = PyDict_New(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1628 if (copy == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1629 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1630 if (PyDict_Merge(copy, o, 1) == 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1631 return copy; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1632 Py_DECREF(copy); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1633 return NULL; |
|
5658
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1634 } |
|
1265ca44f04b
Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents:
5609
diff
changeset
|
1635 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36484
diff
changeset
|
1636 Py_ssize_t |
|
13341
3f1bd4782628
Removed Py_PROTO and switched to ANSI C declarations in the dict
Tim Peters <tim.peters@gmail.com>
parents:
13265
diff
changeset
|
1637 PyDict_Size(PyObject *mp) |
|
1398
1d841587aede
Added getmappingsize(). (Needed by previous checkin of posixmodule.c)
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
1638 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1639 if (mp == NULL || !PyDict_Check(mp)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1640 PyErr_BadInternalCall(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1641 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1642 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1643 return ((PyDictObject *)mp)->ma_used; |
|
1398
1d841587aede
Added getmappingsize(). (Needed by previous checkin of posixmodule.c)
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
1644 } |
|
1d841587aede
Added getmappingsize(). (Needed by previous checkin of posixmodule.c)
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
1645 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
1646 PyObject * |
|
13341
3f1bd4782628
Removed Py_PROTO and switched to ANSI C declarations in the dict
Tim Peters <tim.peters@gmail.com>
parents:
13265
diff
changeset
|
1647 PyDict_Keys(PyObject *mp) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1648 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1649 if (mp == NULL || !PyDict_Check(mp)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1650 PyErr_BadInternalCall(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1651 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1652 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1653 return dict_keys((PyDictObject *)mp); |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1654 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1655 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
1656 PyObject * |
|
13341
3f1bd4782628
Removed Py_PROTO and switched to ANSI C declarations in the dict
Tim Peters <tim.peters@gmail.com>
parents:
13265
diff
changeset
|
1657 PyDict_Values(PyObject *mp) |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1658 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1659 if (mp == NULL || !PyDict_Check(mp)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1660 PyErr_BadInternalCall(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1661 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1662 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1663 return dict_values((PyDictObject *)mp); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1664 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1665 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
1666 PyObject * |
|
13341
3f1bd4782628
Removed Py_PROTO and switched to ANSI C declarations in the dict
Tim Peters <tim.peters@gmail.com>
parents:
13265
diff
changeset
|
1667 PyDict_Items(PyObject *mp) |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1668 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1669 if (mp == NULL || !PyDict_Check(mp)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1670 PyErr_BadInternalCall(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1671 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1672 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1673 return dict_items((PyDictObject *)mp); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1674 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1675 |
|
4586
b3d2990d8a4a
Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1676 /* Subroutine which returns the smallest key in a for which b's value |
|
b3d2990d8a4a
Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1677 is different or absent. The value is returned too, through the |
|
17756
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1678 pval argument. Both are NULL if no key in a is found for which b's status |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1679 differs. The refcounts on (and only on) non-NULL *pval and function return |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1680 values must be decremented by the caller (characterize() increments them |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1681 to ensure that mutating comparison and PyDict_GetItem calls can't delete |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1682 them before the caller is done looking at them). */ |
|
4586
b3d2990d8a4a
Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1683 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
1684 static PyObject * |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
1685 characterize(PyDictObject *a, PyDictObject *b, PyObject **pval) |
|
4586
b3d2990d8a4a
Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1686 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1687 PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1688 PyObject *aval = NULL; /* a[akey] */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1689 Py_ssize_t i; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1690 int cmp; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1691 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1692 for (i = 0; i <= a->ma_mask; i++) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1693 PyObject *thiskey, *thisaval, *thisbval; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1694 if (a->ma_table[i].me_value == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1695 continue; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1696 thiskey = a->ma_table[i].me_key; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1697 Py_INCREF(thiskey); /* keep alive across compares */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1698 if (akey != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1699 cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1700 if (cmp < 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1701 Py_DECREF(thiskey); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1702 goto Fail; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1703 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1704 if (cmp > 0 || |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1705 i > a->ma_mask || |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1706 a->ma_table[i].me_value == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1707 { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1708 /* Not the *smallest* a key; or maybe it is |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1709 * but the compare shrunk the dict so we can't |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1710 * find its associated value anymore; or |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1711 * maybe it is but the compare deleted the |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1712 * a[thiskey] entry. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1713 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1714 Py_DECREF(thiskey); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1715 continue; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1716 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1717 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1718 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1719 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1720 thisaval = a->ma_table[i].me_value; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1721 assert(thisaval); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1722 Py_INCREF(thisaval); /* keep alive */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1723 thisbval = PyDict_GetItem((PyObject *)b, thiskey); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1724 if (thisbval == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1725 cmp = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1726 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1727 /* both dicts have thiskey: same values? */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1728 cmp = PyObject_RichCompareBool( |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1729 thisaval, thisbval, Py_EQ); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1730 if (cmp < 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1731 Py_DECREF(thiskey); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1732 Py_DECREF(thisaval); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1733 goto Fail; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1734 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1735 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1736 if (cmp == 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1737 /* New winner. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1738 Py_XDECREF(akey); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1739 Py_XDECREF(aval); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1740 akey = thiskey; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1741 aval = thisaval; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1742 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1743 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1744 Py_DECREF(thiskey); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1745 Py_DECREF(thisaval); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1746 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1747 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1748 *pval = aval; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1749 return akey; |
|
17756
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1750 |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1751 Fail: |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1752 Py_XDECREF(akey); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1753 Py_XDECREF(aval); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1754 *pval = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1755 return NULL; |
|
4586
b3d2990d8a4a
Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1756 } |
|
b3d2990d8a4a
Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1757 |
|
b3d2990d8a4a
Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1758 static int |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
1759 dict_compare(PyDictObject *a, PyDictObject *b) |
|
4586
b3d2990d8a4a
Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1760 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1761 PyObject *adiff, *bdiff, *aval, *bval; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1762 int res; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1763 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1764 /* Compare lengths first */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1765 if (a->ma_used < b->ma_used) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1766 return -1; /* a is shorter */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1767 else if (a->ma_used > b->ma_used) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1768 return 1; /* b is shorter */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1769 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1770 /* Same length -- check all keys */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1771 bdiff = bval = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1772 adiff = characterize(a, b, &aval); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1773 if (adiff == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1774 assert(!aval); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1775 /* Either an error, or a is a subset with the same length so |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1776 * must be equal. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1777 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1778 res = PyErr_Occurred() ? -1 : 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1779 goto Finished; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1780 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1781 bdiff = characterize(b, a, &bval); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1782 if (bdiff == NULL && PyErr_Occurred()) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1783 assert(!bval); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1784 res = -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1785 goto Finished; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1786 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1787 res = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1788 if (bdiff) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1789 /* bdiff == NULL "should be" impossible now, but perhaps |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1790 * the last comparison done by the characterize() on a had |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1791 * the side effect of making the dicts equal! |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1792 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1793 res = PyObject_Compare(adiff, bdiff); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1794 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1795 if (res == 0 && bval != NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1796 res = PyObject_Compare(aval, bval); |
|
17756
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1797 |
|
a06a704260e9
SF bug #422121 Insecurities in dict comparison.
Tim Peters <tim.peters@gmail.com>
parents:
17704
diff
changeset
|
1798 Finished: |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1799 Py_XDECREF(adiff); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1800 Py_XDECREF(bdiff); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1801 Py_XDECREF(aval); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1802 Py_XDECREF(bval); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1803 return res; |
|
4586
b3d2990d8a4a
Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1804 } |
|
b3d2990d8a4a
Experimental new implementation of dictionary comparison. This
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1805 |
|
17704
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1806 /* Return 1 if dicts equal, 0 if not, -1 if error. |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1807 * Gets out as soon as any difference is detected. |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1808 * Uses only Py_EQ comparison. |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1809 */ |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1810 static int |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
1811 dict_equal(PyDictObject *a, PyDictObject *b) |
|
17704
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1812 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1813 Py_ssize_t i; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1814 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1815 if (a->ma_used != b->ma_used) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1816 /* can't be equal if # of entries differ */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1817 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1818 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1819 /* Same # of entries -- check all of 'em. Exit early on any diff. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1820 for (i = 0; i <= a->ma_mask; i++) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1821 PyObject *aval = a->ma_table[i].me_value; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1822 if (aval != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1823 int cmp; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1824 PyObject *bval; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1825 PyObject *key = a->ma_table[i].me_key; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1826 /* temporarily bump aval's refcount to ensure it stays |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1827 alive until we're done with it */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1828 Py_INCREF(aval); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1829 /* ditto for key */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1830 Py_INCREF(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1831 bval = PyDict_GetItem((PyObject *)b, key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1832 Py_DECREF(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1833 if (bval == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1834 Py_DECREF(aval); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1835 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1836 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1837 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1838 Py_DECREF(aval); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1839 if (cmp <= 0) /* error or not equal */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1840 return cmp; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1841 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1842 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1843 return 1; |
|
17704
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1844 } |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1845 |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1846 static PyObject * |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1847 dict_richcompare(PyObject *v, PyObject *w, int op) |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1848 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1849 int cmp; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1850 PyObject *res; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1851 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1852 if (!PyDict_Check(v) || !PyDict_Check(w)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1853 res = Py_NotImplemented; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1854 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1855 else if (op == Py_EQ || op == Py_NE) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1856 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1857 if (cmp < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1858 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1859 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1860 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1861 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1862 /* Py3K warning if comparison isn't == or != */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1863 if (PyErr_WarnPy3k("dict inequality comparisons not supported " |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1864 "in 3.x", 1) < 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1865 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1866 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1867 res = Py_NotImplemented; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1868 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1869 Py_INCREF(res); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1870 return res; |
|
17704
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1871 } |
|
b41f6ef6ea8f
SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents:
17656
diff
changeset
|
1872 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
1873 static PyObject * |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
1874 dict_contains(register PyDictObject *mp, PyObject *key) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1875 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1876 long hash; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1877 PyDictEntry *ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1878 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1879 if (!PyString_CheckExact(key) || |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1880 (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1881 hash = PyObject_Hash(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1882 if (hash == -1) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1883 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1884 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1885 ep = (mp->ma_lookup)(mp, key, hash); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1886 if (ep == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1887 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1888 return PyBool_FromLong(ep->me_value != NULL); |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1889 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1890 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
1891 static PyObject * |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
1892 dict_has_key(register PyDictObject *mp, PyObject *key) |
|
41832
a0ee3b2ba361
Add -3 option to the interpreter to warn about features that are
Neal Norwitz <nnorwitz@gmail.com>
parents:
41270
diff
changeset
|
1893 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1894 if (PyErr_WarnPy3k("dict.has_key() not supported in 3.x; " |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1895 "use the in operator", 1) < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1896 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1897 return dict_contains(mp, key); |
|
41832
a0ee3b2ba361
Add -3 option to the interpreter to warn about features that are
Neal Norwitz <nnorwitz@gmail.com>
parents:
41270
diff
changeset
|
1898 } |
|
a0ee3b2ba361
Add -3 option to the interpreter to warn about features that are
Neal Norwitz <nnorwitz@gmail.com>
parents:
41270
diff
changeset
|
1899 |
|
a0ee3b2ba361
Add -3 option to the interpreter to warn about features that are
Neal Norwitz <nnorwitz@gmail.com>
parents:
41270
diff
changeset
|
1900 static PyObject * |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
1901 dict_get(register PyDictObject *mp, PyObject *args) |
|
6395
22dae2637e08
dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents:
6351
diff
changeset
|
1902 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1903 PyObject *key; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1904 PyObject *failobj = Py_None; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1905 PyObject *val = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1906 long hash; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1907 PyDictEntry *ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1908 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1909 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1910 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1911 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1912 if (!PyString_CheckExact(key) || |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1913 (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1914 hash = PyObject_Hash(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1915 if (hash == -1) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1916 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1917 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1918 ep = (mp->ma_lookup)(mp, key, hash); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1919 if (ep == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1920 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1921 val = ep->me_value; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1922 if (val == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1923 val = failobj; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1924 Py_INCREF(val); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1925 return val; |
|
6395
22dae2637e08
dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents:
6351
diff
changeset
|
1926 } |
|
22dae2637e08
dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents:
6351
diff
changeset
|
1927 |
|
22dae2637e08
dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents:
6351
diff
changeset
|
1928 |
|
22dae2637e08
dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents:
6351
diff
changeset
|
1929 static PyObject * |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
1930 dict_setdefault(register PyDictObject *mp, PyObject *args) |
|
13892
2190b7599837
Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents:
13660
diff
changeset
|
1931 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1932 PyObject *key; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1933 PyObject *failobj = Py_None; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1934 PyObject *val = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1935 long hash; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1936 PyDictEntry *ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1937 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1938 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1939 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1940 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1941 if (!PyString_CheckExact(key) || |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1942 (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1943 hash = PyObject_Hash(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1944 if (hash == -1) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1945 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1946 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1947 ep = (mp->ma_lookup)(mp, key, hash); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1948 if (ep == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1949 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1950 val = ep->me_value; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1951 if (val == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1952 val = failobj; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1953 if (PyDict_SetItem((PyObject*)mp, key, failobj)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1954 val = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1955 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1956 Py_XINCREF(val); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1957 return val; |
|
13892
2190b7599837
Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents:
13660
diff
changeset
|
1958 } |
|
2190b7599837
Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents:
13660
diff
changeset
|
1959 |
|
2190b7599837
Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents:
13660
diff
changeset
|
1960 |
|
2190b7599837
Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents:
13660
diff
changeset
|
1961 static PyObject * |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
1962 dict_clear(register PyDictObject *mp) |
|
5209
2ec9c933afb8
Add clear() method to dictionary objects.
Guido van Rossum <guido@python.org>
parents:
5059
diff
changeset
|
1963 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1964 PyDict_Clear((PyObject *)mp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1965 Py_RETURN_NONE; |
|
5209
2ec9c933afb8
Add clear() method to dictionary objects.
Guido van Rossum <guido@python.org>
parents:
5059
diff
changeset
|
1966 } |
|
2ec9c933afb8
Add clear() method to dictionary objects.
Guido van Rossum <guido@python.org>
parents:
5059
diff
changeset
|
1967 |
|
15520
8854d9d5add5
Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents:
15463
diff
changeset
|
1968 static PyObject * |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
1969 dict_pop(PyDictObject *mp, PyObject *args) |
|
22994
196dfa7ed348
Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents:
22973
diff
changeset
|
1970 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1971 long hash; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1972 PyDictEntry *ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1973 PyObject *old_value, *old_key; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1974 PyObject *key, *deflt = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1975 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1976 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1977 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1978 if (mp->ma_used == 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1979 if (deflt) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1980 Py_INCREF(deflt); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1981 return deflt; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1982 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1983 PyErr_SetString(PyExc_KeyError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1984 "pop(): dictionary is empty"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1985 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1986 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1987 if (!PyString_CheckExact(key) || |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1988 (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1989 hash = PyObject_Hash(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1990 if (hash == -1) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1991 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1992 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1993 ep = (mp->ma_lookup)(mp, key, hash); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1994 if (ep == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1995 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1996 if (ep->me_value == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1997 if (deflt) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1998 Py_INCREF(deflt); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
1999 return deflt; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2000 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2001 set_key_error(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2002 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2003 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2004 old_key = ep->me_key; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2005 Py_INCREF(dummy); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2006 ep->me_key = dummy; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2007 old_value = ep->me_value; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2008 ep->me_value = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2009 mp->ma_used--; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2010 Py_DECREF(old_key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2011 return old_value; |
|
22994
196dfa7ed348
Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents:
22973
diff
changeset
|
2012 } |
|
196dfa7ed348
Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents:
22973
diff
changeset
|
2013 |
|
196dfa7ed348
Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents:
22973
diff
changeset
|
2014 static PyObject * |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
2015 dict_popitem(PyDictObject *mp) |
|
15520
8854d9d5add5
Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents:
15463
diff
changeset
|
2016 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2017 Py_ssize_t i = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2018 PyDictEntry *ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2019 PyObject *res; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2020 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2021 /* Allocate the result tuple before checking the size. Believe it |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2022 * or not, this allocation could trigger a garbage collection which |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2023 * could empty the dict, so if we checked the size first and that |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2024 * happened, the result would be an infinite loop (searching for an |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2025 * entry that no longer exists). Note that the usual popitem() |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2026 * idiom is "while d: k, v = d.popitem()". so needing to throw the |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2027 * tuple away if the dict *is* empty isn't a significant |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2028 * inefficiency -- possible, but unlikely in practice. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2029 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2030 res = PyTuple_New(2); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2031 if (res == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2032 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2033 if (mp->ma_used == 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2034 Py_DECREF(res); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2035 PyErr_SetString(PyExc_KeyError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2036 "popitem(): dictionary is empty"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2037 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2038 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2039 /* Set ep to "the first" dict entry with a value. We abuse the hash |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2040 * field of slot 0 to hold a search finger: |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2041 * If slot 0 has a value, use slot 0. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2042 * Else slot 0 is being used to hold a search finger, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2043 * and we use its hash value as the first index to look. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2044 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2045 ep = &mp->ma_table[0]; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2046 if (ep->me_value == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2047 i = ep->me_hash; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2048 /* The hash field may be a real hash value, or it may be a |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2049 * legit search finger, or it may be a once-legit search |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2050 * finger that's out of bounds now because it wrapped around |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2051 * or the table shrunk -- simply make sure it's in bounds now. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2052 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2053 if (i > mp->ma_mask || i < 1) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2054 i = 1; /* skip slot 0 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2055 while ((ep = &mp->ma_table[i])->me_value == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2056 i++; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2057 if (i > mp->ma_mask) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2058 i = 1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2059 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2060 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2061 PyTuple_SET_ITEM(res, 0, ep->me_key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2062 PyTuple_SET_ITEM(res, 1, ep->me_value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2063 Py_INCREF(dummy); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2064 ep->me_key = dummy; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2065 ep->me_value = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2066 mp->ma_used--; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2067 assert(mp->ma_table[0].me_value == NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2068 mp->ma_table[0].me_hash = i + 1; /* next place to start */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2069 return res; |
|
15520
8854d9d5add5
Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents:
15463
diff
changeset
|
2070 } |
|
8854d9d5add5
Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents:
15463
diff
changeset
|
2071 |
|
12990
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
2072 static int |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
2073 dict_traverse(PyObject *op, visitproc visit, void *arg) |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
2074 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2075 Py_ssize_t i = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2076 PyObject *pk; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2077 PyObject *pv; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2078 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2079 while (PyDict_Next(op, &i, &pk, &pv)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2080 Py_VISIT(pk); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2081 Py_VISIT(pv); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2082 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2083 return 0; |
|
12990
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
2084 } |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
2085 |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
2086 static int |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
2087 dict_tp_clear(PyObject *op) |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
2088 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2089 PyDict_Clear(op); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2090 return 0; |
|
12990
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
2091 } |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
2092 |
|
15560
143646bafd1b
Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents:
15554
diff
changeset
|
2093 |
|
31680
bb3bccb3b77d
Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents:
31677
diff
changeset
|
2094 extern PyTypeObject PyDictIterKey_Type; /* Forward */ |
|
bb3bccb3b77d
Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents:
31677
diff
changeset
|
2095 extern PyTypeObject PyDictIterValue_Type; /* Forward */ |
|
bb3bccb3b77d
Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents:
31677
diff
changeset
|
2096 extern PyTypeObject PyDictIterItem_Type; /* Forward */ |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
2097 static PyObject *dictiter_new(PyDictObject *, PyTypeObject *); |
|
17644
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
2098 |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
2099 static PyObject * |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
2100 dict_iterkeys(PyDictObject *dict) |
|
17644
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
2101 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2102 return dictiter_new(dict, &PyDictIterKey_Type); |
|
17644
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
2103 } |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
2104 |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
2105 static PyObject * |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
2106 dict_itervalues(PyDictObject *dict) |
|
17644
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
2107 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2108 return dictiter_new(dict, &PyDictIterValue_Type); |
|
17644
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
2109 } |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
2110 |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
2111 static PyObject * |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
2112 dict_iteritems(PyDictObject *dict) |
|
17644
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
2113 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2114 return dictiter_new(dict, &PyDictIterItem_Type); |
|
17644
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
2115 } |
|
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
2116 |
|
47419
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47294
diff
changeset
|
2117 static PyObject * |
|
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47294
diff
changeset
|
2118 dict_sizeof(PyDictObject *mp) |
|
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47294
diff
changeset
|
2119 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2120 Py_ssize_t res; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2121 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2122 res = sizeof(PyDictObject); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2123 if (mp->ma_table != mp->ma_smalltable) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2124 res = res + (mp->ma_mask + 1) * sizeof(PyDictEntry); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2125 return PyInt_FromSsize_t(res); |
|
47419
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47294
diff
changeset
|
2126 } |
|
17644
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
2127 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
2128 PyDoc_STRVAR(has_key__doc__, |
|
30265
51744279458f
SF patch #798467: Update docstring of has_key for bool changes
Raymond Hettinger <python@rcn.com>
parents:
29003
diff
changeset
|
2129 "D.has_key(k) -> True if D has a key k, else False"); |
|
15560
143646bafd1b
Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents:
15554
diff
changeset
|
2130 |
|
31172
e4fb6164158c
* Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents:
31019
diff
changeset
|
2131 PyDoc_STRVAR(contains__doc__, |
|
e4fb6164158c
* Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents:
31019
diff
changeset
|
2132 "D.__contains__(k) -> True if D has a key k, else False"); |
|
e4fb6164158c
* Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents:
31019
diff
changeset
|
2133 |
|
e4fb6164158c
* Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents:
31019
diff
changeset
|
2134 PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]"); |
|
e4fb6164158c
* Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents:
31019
diff
changeset
|
2135 |
|
47419
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47294
diff
changeset
|
2136 PyDoc_STRVAR(sizeof__doc__, |
|
47421
a65f095b27f3
Some style nits. Also clarify in the docstrings what __sizeof__ does.
Georg Brandl <georg@python.org>
parents:
47419
diff
changeset
|
2137 "D.__sizeof__() -> size of D in memory, in bytes"); |
|
47419
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47294
diff
changeset
|
2138 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
2139 PyDoc_STRVAR(get__doc__, |
|
25115
4ea081100305
In doc strings, use 'k in D' rather than D.has_key(k).
Guido van Rossum <guido@python.org>
parents:
24901
diff
changeset
|
2140 "D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None."); |
|
15560
143646bafd1b
Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents:
15554
diff
changeset
|
2141 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
2142 PyDoc_STRVAR(setdefault_doc__, |
|
25115
4ea081100305
In doc strings, use 'k in D' rather than D.has_key(k).
Guido van Rossum <guido@python.org>
parents:
24901
diff
changeset
|
2143 "D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D"); |
|
15560
143646bafd1b
Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents:
15554
diff
changeset
|
2144 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
2145 PyDoc_STRVAR(pop__doc__, |
|
49677
82ccc7e77ec5
Merged revisions 66801,66803-66804,66813,66854-66856,66866,66870-66872,66874,66887,66903,66905,66911,66913,66927,66932,66938,66942,66962,66964,66973-66974,66977,66992,66998-66999,67002,67005,67007,67028,67040-67041,67044,67070,67089,67091,67101,67117-67119,67123-67124 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48209
diff
changeset
|
2146 "D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n\ |
|
28211
92e3b611dc9f
SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents:
27902
diff
changeset
|
2147 If key is not found, d is returned if given, otherwise KeyError is raised"); |
|
22994
196dfa7ed348
Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents:
22973
diff
changeset
|
2148 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
2149 PyDoc_STRVAR(popitem__doc__, |
|
15560
143646bafd1b
Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents:
15554
diff
changeset
|
2150 "D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\ |
|
49677
82ccc7e77ec5
Merged revisions 66801,66803-66804,66813,66854-66856,66866,66870-66872,66874,66887,66903,66905,66911,66913,66927,66932,66938,66942,66962,66964,66973-66974,66977,66992,66998-66999,67002,67005,67007,67028,67040-67041,67044,67070,67089,67091,67101,67117-67119,67123-67124 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48209
diff
changeset
|
2151 2-tuple; but raise KeyError if D is empty."); |
|
15560
143646bafd1b
Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents:
15554
diff
changeset
|
2152 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
2153 PyDoc_STRVAR(keys__doc__, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
2154 "D.keys() -> list of D's keys"); |
|
15560
143646bafd1b
Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents:
15554
diff
changeset
|
2155 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
2156 PyDoc_STRVAR(items__doc__, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
2157 "D.items() -> list of D's (key, value) pairs, as 2-tuples"); |
|
15560
143646bafd1b
Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents:
15554
diff
changeset
|
2158 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
2159 PyDoc_STRVAR(values__doc__, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
2160 "D.values() -> list of D's values"); |
|
15560
143646bafd1b
Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents:
15554
diff
changeset
|
2161 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
2162 PyDoc_STRVAR(update__doc__, |
|
49677
82ccc7e77ec5
Merged revisions 66801,66803-66804,66813,66854-66856,66866,66870-66872,66874,66887,66903,66905,66911,66913,66927,66932,66938,66942,66962,66964,66973-66974,66977,66992,66998-66999,67002,67005,67007,67028,67040-67041,67044,67070,67089,67091,67101,67117-67119,67123-67124 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48209
diff
changeset
|
2163 "D.update(E, **F) -> None. Update D from dict/iterable E and F.\n" |
|
82ccc7e77ec5
Merged revisions 66801,66803-66804,66813,66854-66856,66866,66870-66872,66874,66887,66903,66905,66911,66913,66927,66932,66938,66942,66962,66964,66973-66974,66977,66992,66998-66999,67002,67005,67007,67028,67040-67041,67044,67070,67089,67091,67101,67117-67119,67123-67124 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48209
diff
changeset
|
2164 "If E has a .keys() method, does: for k in E: D[k] = E[k]\n\ |
|
82ccc7e77ec5
Merged revisions 66801,66803-66804,66813,66854-66856,66866,66870-66872,66874,66887,66903,66905,66911,66913,66927,66932,66938,66942,66962,66964,66973-66974,66977,66992,66998-66999,67002,67005,67007,67028,67040-67041,67044,67070,67089,67091,67101,67117-67119,67123-67124 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48209
diff
changeset
|
2165 If E lacks .keys() method, does: for (k, v) in E: D[k] = v\n\ |
|
82ccc7e77ec5
Merged revisions 66801,66803-66804,66813,66854-66856,66866,66870-66872,66874,66887,66903,66905,66911,66913,66927,66932,66938,66942,66962,66964,66973-66974,66977,66992,66998-66999,67002,67005,67007,67028,67040-67041,67044,67070,67089,67091,67101,67117-67119,67123-67124 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48209
diff
changeset
|
2166 In either case, this is followed by: for k in F: D[k] = F[k]"); |
|
15560
143646bafd1b
Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents:
15554
diff
changeset
|
2167 |
|
26437
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
2168 PyDoc_STRVAR(fromkeys__doc__, |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
2169 "dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\ |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
2170 v defaults to None."); |
|
57d03375aead
SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents:
26366
diff
changeset
|
2171 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
2172 PyDoc_STRVAR(clear__doc__, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
2173 "D.clear() -> None. Remove all items from D."); |
|
15560
143646bafd1b
Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents:
15554
diff
changeset
|
2174 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
2175 PyDoc_STRVAR(copy__doc__, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
2176 "D.copy() -> a shallow copy of D"); |
|
15560
143646bafd1b
Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents:
15554
diff
changeset
|
2177 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
2178 PyDoc_STRVAR(iterkeys__doc__, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
2179 "D.iterkeys() -> an iterator over the keys of D"); |
|
17644
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
2180 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
2181 PyDoc_STRVAR(itervalues__doc__, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
2182 "D.itervalues() -> an iterator over the values of D"); |
|
17644
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
2183 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
2184 PyDoc_STRVAR(iteritems__doc__, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
2185 "D.iteritems() -> an iterator over the (key, value) items of D"); |
|
17644
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
2186 |
|
58256
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2187 /* Forward */ |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2188 static PyObject *dictkeys_new(PyObject *); |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2189 static PyObject *dictitems_new(PyObject *); |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2190 static PyObject *dictvalues_new(PyObject *); |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2191 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2192 PyDoc_STRVAR(viewkeys__doc__, |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2193 "D.viewkeys() -> a set-like object providing a view on D's keys"); |
|
58256
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2194 PyDoc_STRVAR(viewitems__doc__, |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2195 "D.viewitems() -> a set-like object providing a view on D's items"); |
|
58256
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2196 PyDoc_STRVAR(viewvalues__doc__, |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2197 "D.viewvalues() -> an object providing a view on D's values"); |
|
58256
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2198 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
2199 static PyMethodDef mapp_methods[] = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2200 {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2201 contains__doc__}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2202 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2203 getitem__doc__}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2204 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2205 sizeof__doc__}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2206 {"has_key", (PyCFunction)dict_has_key, METH_O, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2207 has_key__doc__}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2208 {"get", (PyCFunction)dict_get, METH_VARARGS, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2209 get__doc__}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2210 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2211 setdefault_doc__}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2212 {"pop", (PyCFunction)dict_pop, METH_VARARGS, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2213 pop__doc__}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2214 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2215 popitem__doc__}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2216 {"keys", (PyCFunction)dict_keys, METH_NOARGS, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2217 keys__doc__}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2218 {"items", (PyCFunction)dict_items, METH_NOARGS, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2219 items__doc__}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2220 {"values", (PyCFunction)dict_values, METH_NOARGS, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2221 values__doc__}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2222 {"viewkeys", (PyCFunction)dictkeys_new, METH_NOARGS, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2223 viewkeys__doc__}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2224 {"viewitems", (PyCFunction)dictitems_new, METH_NOARGS, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2225 viewitems__doc__}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2226 {"viewvalues", (PyCFunction)dictvalues_new, METH_NOARGS, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2227 viewvalues__doc__}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2228 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2229 update__doc__}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2230 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2231 fromkeys__doc__}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2232 {"clear", (PyCFunction)dict_clear, METH_NOARGS, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2233 clear__doc__}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2234 {"copy", (PyCFunction)dict_copy, METH_NOARGS, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2235 copy__doc__}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2236 {"iterkeys", (PyCFunction)dict_iterkeys, METH_NOARGS, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2237 iterkeys__doc__}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2238 {"itervalues", (PyCFunction)dict_itervalues, METH_NOARGS, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2239 itervalues__doc__}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2240 {"iteritems", (PyCFunction)dict_iteritems, METH_NOARGS, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2241 iteritems__doc__}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2242 {NULL, NULL} /* sentinel */ |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2243 }; |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2244 |
|
38540
d7dd528427c0
Armin committed his patch while I was reviewing it (I'm sure
Tim Peters <tim.peters@gmail.com>
parents:
38536
diff
changeset
|
2245 /* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */ |
|
31019
b2fd7af17186
Expose dict_contains() and PyDict_Contains() with is about 10% faster
Raymond Hettinger <python@rcn.com>
parents:
30265
diff
changeset
|
2246 int |
|
b2fd7af17186
Expose dict_contains() and PyDict_Contains() with is about 10% faster
Raymond Hettinger <python@rcn.com>
parents:
30265
diff
changeset
|
2247 PyDict_Contains(PyObject *op, PyObject *key) |
|
17594
bf94af396b1e
Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents:
17536
diff
changeset
|
2248 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2249 long hash; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2250 PyDictObject *mp = (PyDictObject *)op; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2251 PyDictEntry *ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2252 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2253 if (!PyString_CheckExact(key) || |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2254 (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2255 hash = PyObject_Hash(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2256 if (hash == -1) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2257 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2258 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2259 ep = (mp->ma_lookup)(mp, key, hash); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2260 return ep == NULL ? -1 : (ep->me_value != NULL); |
|
17594
bf94af396b1e
Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents:
17536
diff
changeset
|
2261 } |
|
bf94af396b1e
Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents:
17536
diff
changeset
|
2262 |
|
40864
b0f8e9b0cc19
Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents:
40756
diff
changeset
|
2263 /* Internal version of PyDict_Contains used when the hash value is already known */ |
|
b0f8e9b0cc19
Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents:
40756
diff
changeset
|
2264 int |
|
b0f8e9b0cc19
Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents:
40756
diff
changeset
|
2265 _PyDict_Contains(PyObject *op, PyObject *key, long hash) |
|
b0f8e9b0cc19
Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents:
40756
diff
changeset
|
2266 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2267 PyDictObject *mp = (PyDictObject *)op; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2268 PyDictEntry *ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2269 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2270 ep = (mp->ma_lookup)(mp, key, hash); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2271 return ep == NULL ? -1 : (ep->me_value != NULL); |
|
40864
b0f8e9b0cc19
Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents:
40756
diff
changeset
|
2272 } |
|
b0f8e9b0cc19
Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash().
Raymond Hettinger <python@rcn.com>
parents:
40756
diff
changeset
|
2273 |
|
17594
bf94af396b1e
Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents:
17536
diff
changeset
|
2274 /* Hack to implement "key in dict" */ |
|
bf94af396b1e
Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents:
17536
diff
changeset
|
2275 static PySequenceMethods dict_as_sequence = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2276 0, /* sq_length */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2277 0, /* sq_concat */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2278 0, /* sq_repeat */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2279 0, /* sq_item */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2280 0, /* sq_slice */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2281 0, /* sq_ass_item */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2282 0, /* sq_ass_slice */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2283 PyDict_Contains, /* sq_contains */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2284 0, /* sq_inplace_concat */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2285 0, /* sq_inplace_repeat */ |
|
17594
bf94af396b1e
Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents:
17536
diff
changeset
|
2286 }; |
|
bf94af396b1e
Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents:
17536
diff
changeset
|
2287 |
|
17644
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
2288 static PyObject * |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
2289 dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
2290 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2291 PyObject *self; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2292 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2293 assert(type != NULL && type->tp_alloc != NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2294 self = type->tp_alloc(type, 0); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2295 if (self != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2296 PyDictObject *d = (PyDictObject *)self; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2297 /* It's guaranteed that tp->alloc zeroed out the struct. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2298 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2299 INIT_NONZERO_DICT_SLOTS(d); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2300 d->ma_lookup = lookdict_string; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2301 /* The object has been implicitely tracked by tp_alloc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2302 if (type == &PyDict_Type) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2303 _PyObject_GC_UNTRACK(d); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
2304 #ifdef SHOW_CONVERSION_COUNTS |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2305 ++created; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
2306 #endif |
|
52288
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
50424
diff
changeset
|
2307 #ifdef SHOW_TRACK_COUNT |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2308 if (_PyObject_GC_IS_TRACKED(d)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2309 count_tracked++; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2310 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2311 count_untracked++; |
|
52288
0e498a9fde3c
Issue #4688: Add a heuristic so that tuples and dicts containing only
Antoine Pitrou <solipsis@pitrou.net>
parents:
50424
diff
changeset
|
2312 #endif |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2313 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2314 return self; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
2315 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
2316 |
|
19658
2fa615dfc464
Make dictionary() a real constructor. Accepts at most one argument, "a
Tim Peters <tim.peters@gmail.com>
parents:
19581
diff
changeset
|
2317 static int |
|
2fa615dfc464
Make dictionary() a real constructor. Accepts at most one argument, "a
Tim Peters <tim.peters@gmail.com>
parents:
19581
diff
changeset
|
2318 dict_init(PyObject *self, PyObject *args, PyObject *kwds) |
|
2fa615dfc464
Make dictionary() a real constructor. Accepts at most one argument, "a
Tim Peters <tim.peters@gmail.com>
parents:
19581
diff
changeset
|
2319 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2320 return dict_update_common(self, args, kwds, "dict"); |
|
19658
2fa615dfc464
Make dictionary() a real constructor. Accepts at most one argument, "a
Tim Peters <tim.peters@gmail.com>
parents:
19581
diff
changeset
|
2321 } |
|
2fa615dfc464
Make dictionary() a real constructor. Accepts at most one argument, "a
Tim Peters <tim.peters@gmail.com>
parents:
19581
diff
changeset
|
2322 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18283
diff
changeset
|
2323 static PyObject * |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
2324 dict_iter(PyDictObject *dict) |
|
17644
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
2325 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2326 return dictiter_new(dict, &PyDictIterKey_Type); |
|
17644
5dbfe8316236
Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
2327 } |
|
17597
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2328 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22994
diff
changeset
|
2329 PyDoc_STRVAR(dictionary_doc, |
|
59205
1c6fc70f49d1
#8030: more docstring fix for builtin types.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59198
diff
changeset
|
2330 "dict() -> new empty dictionary\n" |
|
20754
272336648d1f
Rename "dictionary" (type and constructor) to "dict".
Tim Peters <tim.peters@gmail.com>
parents:
20724
diff
changeset
|
2331 "dict(mapping) -> new dictionary initialized from a mapping object's\n" |
|
59205
1c6fc70f49d1
#8030: more docstring fix for builtin types.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59198
diff
changeset
|
2332 " (key, value) pairs\n" |
|
59198
799bc4964a1d
#8030: make builtin type docstrings more consistent: use "iterable" instead of "seq(uence)", use "new" to show that set() always returns a new object.
Georg Brandl <georg@python.org>
parents:
58265
diff
changeset
|
2333 "dict(iterable) -> new dictionary initialized as if via:\n" |
|
20724
f224632d0260
dictionary() constructor:
Tim Peters <tim.peters@gmail.com>
parents:
20683
diff
changeset
|
2334 " d = {}\n" |
|
59198
799bc4964a1d
#8030: make builtin type docstrings more consistent: use "iterable" instead of "seq(uence)", use "new" to show that set() always returns a new object.
Georg Brandl <georg@python.org>
parents:
58265
diff
changeset
|
2335 " for k, v in iterable:\n" |
|
26366
08b720cd387e
Patch #642500 with slight modifications: allow keyword arguments in
Just van Rossum <just@letterror.com>
parents:
25115
diff
changeset
|
2336 " d[k] = v\n" |
|
08b720cd387e
Patch #642500 with slight modifications: allow keyword arguments in
Just van Rossum <just@letterror.com>
parents:
25115
diff
changeset
|
2337 "dict(**kwargs) -> new dictionary initialized with the name=value pairs\n" |
|
08b720cd387e
Patch #642500 with slight modifications: allow keyword arguments in
Just van Rossum <just@letterror.com>
parents:
25115
diff
changeset
|
2338 " in the keyword argument list. For example: dict(one=1, two=2)"); |
|
19658
2fa615dfc464
Make dictionary() a real constructor. Accepts at most one argument, "a
Tim Peters <tim.peters@gmail.com>
parents:
19581
diff
changeset
|
2339 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
2340 PyTypeObject PyDict_Type = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2341 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2342 "dict", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2343 sizeof(PyDictObject), |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2344 0, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2345 (destructor)dict_dealloc, /* tp_dealloc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2346 (printfunc)dict_print, /* tp_print */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2347 0, /* tp_getattr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2348 0, /* tp_setattr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2349 (cmpfunc)dict_compare, /* tp_compare */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2350 (reprfunc)dict_repr, /* tp_repr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2351 0, /* tp_as_number */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2352 &dict_as_sequence, /* tp_as_sequence */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2353 &dict_as_mapping, /* tp_as_mapping */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2354 (hashfunc)PyObject_HashNotImplemented, /* tp_hash */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2355 0, /* tp_call */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2356 0, /* tp_str */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2357 PyObject_GenericGetAttr, /* tp_getattro */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2358 0, /* tp_setattro */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2359 0, /* tp_as_buffer */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2360 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2361 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2362 dictionary_doc, /* tp_doc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2363 dict_traverse, /* tp_traverse */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2364 dict_tp_clear, /* tp_clear */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2365 dict_richcompare, /* tp_richcompare */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2366 0, /* tp_weaklistoffset */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2367 (getiterfunc)dict_iter, /* tp_iter */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2368 0, /* tp_iternext */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2369 mapp_methods, /* tp_methods */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2370 0, /* tp_members */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2371 0, /* tp_getset */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2372 0, /* tp_base */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2373 0, /* tp_dict */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2374 0, /* tp_descr_get */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2375 0, /* tp_descr_set */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2376 0, /* tp_dictoffset */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2377 dict_init, /* tp_init */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2378 PyType_GenericAlloc, /* tp_alloc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2379 dict_new, /* tp_new */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2380 PyObject_GC_Del, /* tp_free */ |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2381 }; |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2382 |
|
5540
a0cc005eccd7
Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents:
5505
diff
changeset
|
2383 /* For backward compatibility with old dictionary interface */ |
|
a0cc005eccd7
Got rid of all the last_name_* bogosities. I don't think the
Guido van Rossum <guido@python.org>
parents:
5505
diff
changeset
|
2384 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5310
diff
changeset
|
2385 PyObject * |
|
26599
ecea8410178e
Constify char* API. Fixes #651363. 2.2 candidate.
Martin v. Löwis <martin@v.loewis.de>
parents:
26583
diff
changeset
|
2386 PyDict_GetItemString(PyObject *v, const char *key) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2387 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2388 PyObject *kv, *rv; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2389 kv = PyString_FromString(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2390 if (kv == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2391 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2392 rv = PyDict_GetItem(v, kv); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2393 Py_DECREF(kv); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2394 return rv; |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2395 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2396 |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2397 int |
|
26599
ecea8410178e
Constify char* API. Fixes #651363. 2.2 candidate.
Martin v. Löwis <martin@v.loewis.de>
parents:
26583
diff
changeset
|
2398 PyDict_SetItemString(PyObject *v, const char *key, PyObject *item) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2399 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2400 PyObject *kv; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2401 int err; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2402 kv = PyString_FromString(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2403 if (kv == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2404 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2405 PyString_InternInPlace(&kv); /* XXX Should we really? */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2406 err = PyDict_SetItem(v, kv, item); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2407 Py_DECREF(kv); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2408 return err; |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2409 } |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2410 |
|
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2411 int |
|
26599
ecea8410178e
Constify char* API. Fixes #651363. 2.2 candidate.
Martin v. Löwis <martin@v.loewis.de>
parents:
26583
diff
changeset
|
2412 PyDict_DelItemString(PyObject *v, const char *key) |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2413 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2414 PyObject *kv; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2415 int err; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2416 kv = PyString_FromString(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2417 if (kv == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2418 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2419 err = PyDict_DelItem(v, kv); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2420 Py_DECREF(kv); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2421 return err; |
|
1256
7aa9613ffd36
Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2422 } |
|
17597
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2423 |
|
31680
bb3bccb3b77d
Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents:
31677
diff
changeset
|
2424 /* Dictionary iterator types */ |
|
17597
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2425 |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2426 typedef struct { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2427 PyObject_HEAD |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2428 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2429 Py_ssize_t di_used; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2430 Py_ssize_t di_pos; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2431 PyObject* di_result; /* reusable result tuple for iteritems */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2432 Py_ssize_t len; |
|
17597
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2433 } dictiterobject; |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2434 |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2435 static PyObject * |
|
43515
8c5e60ddaa82
Remove file-level typedefs that were inconsistently used throughout the file.
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
2436 dictiter_new(PyDictObject *dict, PyTypeObject *itertype) |
|
17597
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2437 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2438 dictiterobject *di; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2439 di = PyObject_GC_New(dictiterobject, itertype); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2440 if (di == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2441 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2442 Py_INCREF(dict); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2443 di->di_dict = dict; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2444 di->di_used = dict->ma_used; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2445 di->di_pos = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2446 di->len = dict->ma_used; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2447 if (itertype == &PyDictIterItem_Type) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2448 di->di_result = PyTuple_Pack(2, Py_None, Py_None); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2449 if (di->di_result == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2450 Py_DECREF(di); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2451 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2452 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2453 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2454 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2455 di->di_result = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2456 _PyObject_GC_TRACK(di); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2457 return (PyObject *)di; |
|
17597
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2458 } |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2459 |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2460 static void |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2461 dictiter_dealloc(dictiterobject *di) |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2462 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2463 Py_XDECREF(di->di_dict); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2464 Py_XDECREF(di->di_result); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2465 PyObject_GC_Del(di); |
|
50424
df3923c36ec5
Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49677
diff
changeset
|
2466 } |
|
df3923c36ec5
Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49677
diff
changeset
|
2467 |
|
df3923c36ec5
Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49677
diff
changeset
|
2468 static int |
|
df3923c36ec5
Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49677
diff
changeset
|
2469 dictiter_traverse(dictiterobject *di, visitproc visit, void *arg) |
|
df3923c36ec5
Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49677
diff
changeset
|
2470 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2471 Py_VISIT(di->di_dict); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2472 Py_VISIT(di->di_result); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2473 return 0; |
|
17597
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2474 } |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2475 |
|
35636
e22334b1da67
Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents:
35350
diff
changeset
|
2476 static PyObject * |
|
31683
c32381f4dc6f
Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents:
31680
diff
changeset
|
2477 dictiter_len(dictiterobject *di) |
|
c32381f4dc6f
Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents:
31680
diff
changeset
|
2478 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2479 Py_ssize_t len = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2480 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2481 len = di->len; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2482 return PyInt_FromSize_t(len); |
|
31683
c32381f4dc6f
Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents:
31680
diff
changeset
|
2483 } |
|
c32381f4dc6f
Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents:
31680
diff
changeset
|
2484 |
|
36484
20f75d5e79e9
Renamed _length_cue() to __length_hint__(). See:
Armin Rigo <arigo@tunes.org>
parents:
36164
diff
changeset
|
2485 PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
|
35636
e22334b1da67
Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents:
35350
diff
changeset
|
2486 |
|
e22334b1da67
Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents:
35350
diff
changeset
|
2487 static PyMethodDef dictiter_methods[] = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2488 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS, length_hint_doc}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2489 {NULL, NULL} /* sentinel */ |
|
31683
c32381f4dc6f
Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents:
31680
diff
changeset
|
2490 }; |
|
c32381f4dc6f
Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents:
31680
diff
changeset
|
2491 |
|
31680
bb3bccb3b77d
Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents:
31677
diff
changeset
|
2492 static PyObject *dictiter_iternextkey(dictiterobject *di) |
|
17597
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2493 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2494 PyObject *key; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2495 register Py_ssize_t i, mask; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2496 register PyDictEntry *ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2497 PyDictObject *d = di->di_dict; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2498 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2499 if (d == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2500 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2501 assert (PyDict_Check(d)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2502 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2503 if (di->di_used != d->ma_used) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2504 PyErr_SetString(PyExc_RuntimeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2505 "dictionary changed size during iteration"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2506 di->di_used = -1; /* Make this state sticky */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2507 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2508 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2509 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2510 i = di->di_pos; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2511 if (i < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2512 goto fail; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2513 ep = d->ma_table; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2514 mask = d->ma_mask; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2515 while (i <= mask && ep[i].me_value == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2516 i++; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2517 di->di_pos = i+1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2518 if (i > mask) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2519 goto fail; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2520 di->len--; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2521 key = ep[i].me_key; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2522 Py_INCREF(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2523 return key; |
|
31680
bb3bccb3b77d
Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents:
31677
diff
changeset
|
2524 |
|
bb3bccb3b77d
Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents:
31677
diff
changeset
|
2525 fail: |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2526 Py_DECREF(d); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2527 di->di_dict = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2528 return NULL; |
|
17597
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2529 } |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2530 |
|
31680
bb3bccb3b77d
Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents:
31677
diff
changeset
|
2531 PyTypeObject PyDictIterKey_Type = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2532 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2533 "dictionary-keyiterator", /* tp_name */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2534 sizeof(dictiterobject), /* tp_basicsize */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2535 0, /* tp_itemsize */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2536 /* methods */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2537 (destructor)dictiter_dealloc, /* tp_dealloc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2538 0, /* tp_print */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2539 0, /* tp_getattr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2540 0, /* tp_setattr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2541 0, /* tp_compare */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2542 0, /* tp_repr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2543 0, /* tp_as_number */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2544 0, /* tp_as_sequence */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2545 0, /* tp_as_mapping */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2546 0, /* tp_hash */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2547 0, /* tp_call */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2548 0, /* tp_str */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2549 PyObject_GenericGetAttr, /* tp_getattro */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2550 0, /* tp_setattro */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2551 0, /* tp_as_buffer */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2552 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2553 0, /* tp_doc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2554 (traverseproc)dictiter_traverse, /* tp_traverse */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2555 0, /* tp_clear */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2556 0, /* tp_richcompare */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2557 0, /* tp_weaklistoffset */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2558 PyObject_SelfIter, /* tp_iter */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2559 (iternextfunc)dictiter_iternextkey, /* tp_iternext */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2560 dictiter_methods, /* tp_methods */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2561 0, |
|
31680
bb3bccb3b77d
Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents:
31677
diff
changeset
|
2562 }; |
|
bb3bccb3b77d
Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents:
31677
diff
changeset
|
2563 |
|
bb3bccb3b77d
Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents:
31677
diff
changeset
|
2564 static PyObject *dictiter_iternextvalue(dictiterobject *di) |
|
bb3bccb3b77d
Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents:
31677
diff
changeset
|
2565 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2566 PyObject *value; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2567 register Py_ssize_t i, mask; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2568 register PyDictEntry *ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2569 PyDictObject *d = di->di_dict; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2570 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2571 if (d == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2572 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2573 assert (PyDict_Check(d)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2574 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2575 if (di->di_used != d->ma_used) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2576 PyErr_SetString(PyExc_RuntimeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2577 "dictionary changed size during iteration"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2578 di->di_used = -1; /* Make this state sticky */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2579 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2580 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2581 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2582 i = di->di_pos; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2583 mask = d->ma_mask; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2584 if (i < 0 || i > mask) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2585 goto fail; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2586 ep = d->ma_table; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2587 while ((value=ep[i].me_value) == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2588 i++; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2589 if (i > mask) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2590 goto fail; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2591 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2592 di->di_pos = i+1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2593 di->len--; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2594 Py_INCREF(value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2595 return value; |
|
31680
bb3bccb3b77d
Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents:
31677
diff
changeset
|
2596 |
|
bb3bccb3b77d
Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents:
31677
diff
changeset
|
2597 fail: |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2598 Py_DECREF(d); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2599 di->di_dict = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2600 return NULL; |
|
31680
bb3bccb3b77d
Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents:
31677
diff
changeset
|
2601 } |
|
bb3bccb3b77d
Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents:
31677
diff
changeset
|
2602 |
|
bb3bccb3b77d
Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents:
31677
diff
changeset
|
2603 PyTypeObject PyDictIterValue_Type = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2604 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2605 "dictionary-valueiterator", /* tp_name */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2606 sizeof(dictiterobject), /* tp_basicsize */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2607 0, /* tp_itemsize */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2608 /* methods */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2609 (destructor)dictiter_dealloc, /* tp_dealloc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2610 0, /* tp_print */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2611 0, /* tp_getattr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2612 0, /* tp_setattr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2613 0, /* tp_compare */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2614 0, /* tp_repr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2615 0, /* tp_as_number */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2616 0, /* tp_as_sequence */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2617 0, /* tp_as_mapping */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2618 0, /* tp_hash */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2619 0, /* tp_call */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2620 0, /* tp_str */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2621 PyObject_GenericGetAttr, /* tp_getattro */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2622 0, /* tp_setattro */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2623 0, /* tp_as_buffer */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2624 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2625 0, /* tp_doc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2626 (traverseproc)dictiter_traverse, /* tp_traverse */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2627 0, /* tp_clear */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2628 0, /* tp_richcompare */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2629 0, /* tp_weaklistoffset */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2630 PyObject_SelfIter, /* tp_iter */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2631 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2632 dictiter_methods, /* tp_methods */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2633 0, |
|
17597
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17595
diff
changeset
|
2634 }; |
|
31680
bb3bccb3b77d
Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents:
31677
diff
changeset
|
2635 |
|
bb3bccb3b77d
Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents:
31677
diff
changeset
|
2636 static PyObject *dictiter_iternextitem(dictiterobject *di) |
|
bb3bccb3b77d
Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents:
31677
diff
changeset
|
2637 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2638 PyObject *key, *value, *result = di->di_result; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2639 register Py_ssize_t i, mask; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2640 register PyDictEntry *ep; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2641 PyDictObject *d = di->di_dict; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2642 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2643 if (d == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2644 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2645 assert (PyDict_Check(d)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2646 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2647 if (di->di_used != d->ma_used) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2648 PyErr_SetString(PyExc_RuntimeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2649 "dictionary changed size during iteration"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2650 di->di_used = -1; /* Make this state sticky */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2651 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2652 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2653 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2654 i = di->di_pos; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2655 if (i < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2656 goto fail; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2657 ep = d->ma_table; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2658 mask = d->ma_mask; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2659 while (i <= mask && ep[i].me_value == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2660 i++; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2661 di->di_pos = i+1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2662 if (i > mask) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2663 goto fail; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2664 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2665 if (result->ob_refcnt == 1) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2666 Py_INCREF(result); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2667 Py_DECREF(PyTuple_GET_ITEM(result, 0)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2668 Py_DECREF(PyTuple_GET_ITEM(result, 1)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2669 } else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2670 result = PyTuple_New(2); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2671 if (result == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2672 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2673 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2674 di->len--; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2675 key = ep[i].me_key; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2676 value = ep[i].me_value; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2677 Py_INCREF(key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2678 Py_INCREF(value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2679 PyTuple_SET_ITEM(result, 0, key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2680 PyTuple_SET_ITEM(result, 1, value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2681 return result; |
|
31680
bb3bccb3b77d
Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents:
31677
diff
changeset
|
2682 |
|
bb3bccb3b77d
Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents:
31677
diff
changeset
|
2683 fail: |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2684 Py_DECREF(d); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2685 di->di_dict = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2686 return NULL; |
|
31680
bb3bccb3b77d
Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents:
31677
diff
changeset
|
2687 } |
|
bb3bccb3b77d
Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents:
31677
diff
changeset
|
2688 |
|
bb3bccb3b77d
Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents:
31677
diff
changeset
|
2689 PyTypeObject PyDictIterItem_Type = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2690 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2691 "dictionary-itemiterator", /* tp_name */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2692 sizeof(dictiterobject), /* tp_basicsize */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2693 0, /* tp_itemsize */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2694 /* methods */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2695 (destructor)dictiter_dealloc, /* tp_dealloc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2696 0, /* tp_print */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2697 0, /* tp_getattr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2698 0, /* tp_setattr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2699 0, /* tp_compare */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2700 0, /* tp_repr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2701 0, /* tp_as_number */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2702 0, /* tp_as_sequence */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2703 0, /* tp_as_mapping */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2704 0, /* tp_hash */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2705 0, /* tp_call */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2706 0, /* tp_str */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2707 PyObject_GenericGetAttr, /* tp_getattro */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2708 0, /* tp_setattro */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2709 0, /* tp_as_buffer */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2710 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2711 0, /* tp_doc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2712 (traverseproc)dictiter_traverse, /* tp_traverse */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2713 0, /* tp_clear */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2714 0, /* tp_richcompare */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2715 0, /* tp_weaklistoffset */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2716 PyObject_SelfIter, /* tp_iter */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2717 (iternextfunc)dictiter_iternextitem, /* tp_iternext */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2718 dictiter_methods, /* tp_methods */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2719 0, |
|
31680
bb3bccb3b77d
Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents:
31677
diff
changeset
|
2720 }; |
|
58256
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2721 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2722 /***********************************************/ |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2723 /* View objects for keys(), items(), values(). */ |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2724 /***********************************************/ |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2725 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2726 /* The instance lay-out is the same for all three; but the type differs. */ |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2727 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2728 typedef struct { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2729 PyObject_HEAD |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2730 PyDictObject *dv_dict; |
|
58256
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2731 } dictviewobject; |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2732 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2733 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2734 static void |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2735 dictview_dealloc(dictviewobject *dv) |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2736 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2737 Py_XDECREF(dv->dv_dict); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2738 PyObject_GC_Del(dv); |
|
58256
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2739 } |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2740 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2741 static int |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2742 dictview_traverse(dictviewobject *dv, visitproc visit, void *arg) |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2743 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2744 Py_VISIT(dv->dv_dict); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2745 return 0; |
|
58256
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2746 } |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2747 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2748 static Py_ssize_t |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2749 dictview_len(dictviewobject *dv) |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2750 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2751 Py_ssize_t len = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2752 if (dv->dv_dict != NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2753 len = dv->dv_dict->ma_used; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2754 return len; |
|
58256
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2755 } |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2756 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2757 static PyObject * |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2758 dictview_new(PyObject *dict, PyTypeObject *type) |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2759 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2760 dictviewobject *dv; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2761 if (dict == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2762 PyErr_BadInternalCall(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2763 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2764 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2765 if (!PyDict_Check(dict)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2766 /* XXX Get rid of this restriction later */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2767 PyErr_Format(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2768 "%s() requires a dict argument, not '%s'", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2769 type->tp_name, dict->ob_type->tp_name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2770 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2771 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2772 dv = PyObject_GC_New(dictviewobject, type); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2773 if (dv == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2774 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2775 Py_INCREF(dict); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2776 dv->dv_dict = (PyDictObject *)dict; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2777 _PyObject_GC_TRACK(dv); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2778 return (PyObject *)dv; |
|
58256
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2779 } |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2780 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2781 /* TODO(guido): The views objects are not complete: |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2782 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2783 * support more set operations |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2784 * support arbitrary mappings? |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2785 - either these should be static or exported in dictobject.h |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2786 - if public then they should probably be in builtins |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2787 */ |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2788 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2789 /* Return 1 if self is a subset of other, iterating over self; |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2790 0 if not; -1 if an error occurred. */ |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2791 static int |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2792 all_contained_in(PyObject *self, PyObject *other) |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2793 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2794 PyObject *iter = PyObject_GetIter(self); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2795 int ok = 1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2796 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2797 if (iter == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2798 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2799 for (;;) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2800 PyObject *next = PyIter_Next(iter); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2801 if (next == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2802 if (PyErr_Occurred()) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2803 ok = -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2804 break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2805 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2806 ok = PySequence_Contains(other, next); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2807 Py_DECREF(next); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2808 if (ok <= 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2809 break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2810 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2811 Py_DECREF(iter); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2812 return ok; |
|
58256
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2813 } |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2814 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2815 static PyObject * |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2816 dictview_richcompare(PyObject *self, PyObject *other, int op) |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2817 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2818 Py_ssize_t len_self, len_other; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2819 int ok; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2820 PyObject *result; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2821 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2822 assert(self != NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2823 assert(PyDictViewSet_Check(self)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2824 assert(other != NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2825 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2826 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2827 Py_INCREF(Py_NotImplemented); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2828 return Py_NotImplemented; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2829 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2830 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2831 len_self = PyObject_Size(self); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2832 if (len_self < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2833 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2834 len_other = PyObject_Size(other); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2835 if (len_other < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2836 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2837 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2838 ok = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2839 switch(op) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2840 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2841 case Py_NE: |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2842 case Py_EQ: |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2843 if (len_self == len_other) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2844 ok = all_contained_in(self, other); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2845 if (op == Py_NE && ok >= 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2846 ok = !ok; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2847 break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2848 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2849 case Py_LT: |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2850 if (len_self < len_other) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2851 ok = all_contained_in(self, other); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2852 break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2853 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2854 case Py_LE: |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2855 if (len_self <= len_other) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2856 ok = all_contained_in(self, other); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2857 break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2858 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2859 case Py_GT: |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2860 if (len_self > len_other) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2861 ok = all_contained_in(other, self); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2862 break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2863 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2864 case Py_GE: |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2865 if (len_self >= len_other) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2866 ok = all_contained_in(other, self); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2867 break; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2868 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2869 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2870 if (ok < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2871 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2872 result = ok ? Py_True : Py_False; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2873 Py_INCREF(result); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2874 return result; |
|
58256
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2875 } |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2876 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2877 static PyObject * |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2878 dictview_repr(dictviewobject *dv) |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2879 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2880 PyObject *seq; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2881 PyObject *seq_str; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2882 PyObject *result; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2883 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2884 seq = PySequence_List((PyObject *)dv); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2885 if (seq == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2886 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2887 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2888 seq_str = PyObject_Repr(seq); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2889 result = PyString_FromFormat("%s(%s)", Py_TYPE(dv)->tp_name, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2890 PyString_AS_STRING(seq_str)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2891 Py_DECREF(seq_str); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2892 Py_DECREF(seq); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2893 return result; |
|
58256
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2894 } |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2895 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2896 /*** dict_keys ***/ |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2897 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2898 static PyObject * |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2899 dictkeys_iter(dictviewobject *dv) |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2900 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2901 if (dv->dv_dict == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2902 Py_RETURN_NONE; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2903 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2904 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type); |
|
58256
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2905 } |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2906 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2907 static int |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2908 dictkeys_contains(dictviewobject *dv, PyObject *obj) |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2909 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2910 if (dv->dv_dict == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2911 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2912 return PyDict_Contains((PyObject *)dv->dv_dict, obj); |
|
58256
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2913 } |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2914 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2915 static PySequenceMethods dictkeys_as_sequence = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2916 (lenfunc)dictview_len, /* sq_length */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2917 0, /* sq_concat */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2918 0, /* sq_repeat */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2919 0, /* sq_item */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2920 0, /* sq_slice */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2921 0, /* sq_ass_item */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2922 0, /* sq_ass_slice */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2923 (objobjproc)dictkeys_contains, /* sq_contains */ |
|
58256
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2924 }; |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2925 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2926 static PyObject* |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2927 dictviews_sub(PyObject* self, PyObject *other) |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2928 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2929 PyObject *result = PySet_New(self); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2930 PyObject *tmp; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2931 if (result == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2932 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2933 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2934 tmp = PyObject_CallMethod(result, "difference_update", "O", other); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2935 if (tmp == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2936 Py_DECREF(result); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2937 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2938 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2939 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2940 Py_DECREF(tmp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2941 return result; |
|
58256
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2942 } |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2943 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2944 static PyObject* |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2945 dictviews_and(PyObject* self, PyObject *other) |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2946 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2947 PyObject *result = PySet_New(self); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2948 PyObject *tmp; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2949 if (result == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2950 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2951 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2952 tmp = PyObject_CallMethod(result, "intersection_update", "O", other); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2953 if (tmp == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2954 Py_DECREF(result); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2955 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2956 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2957 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2958 Py_DECREF(tmp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2959 return result; |
|
58256
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2960 } |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2961 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2962 static PyObject* |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2963 dictviews_or(PyObject* self, PyObject *other) |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2964 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2965 PyObject *result = PySet_New(self); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2966 PyObject *tmp; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2967 if (result == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2968 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2969 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2970 tmp = PyObject_CallMethod(result, "update", "O", other); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2971 if (tmp == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2972 Py_DECREF(result); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2973 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2974 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2975 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2976 Py_DECREF(tmp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2977 return result; |
|
58256
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2978 } |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2979 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2980 static PyObject* |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2981 dictviews_xor(PyObject* self, PyObject *other) |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2982 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2983 PyObject *result = PySet_New(self); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2984 PyObject *tmp; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2985 if (result == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2986 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2987 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2988 tmp = PyObject_CallMethod(result, "symmetric_difference_update", "O", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2989 other); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2990 if (tmp == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2991 Py_DECREF(result); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2992 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2993 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2994 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2995 Py_DECREF(tmp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
2996 return result; |
|
58256
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2997 } |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2998 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
2999 static PyNumberMethods dictviews_as_number = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3000 0, /*nb_add*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3001 (binaryfunc)dictviews_sub, /*nb_subtract*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3002 0, /*nb_multiply*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3003 0, /*nb_divide*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3004 0, /*nb_remainder*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3005 0, /*nb_divmod*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3006 0, /*nb_power*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3007 0, /*nb_negative*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3008 0, /*nb_positive*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3009 0, /*nb_absolute*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3010 0, /*nb_nonzero*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3011 0, /*nb_invert*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3012 0, /*nb_lshift*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3013 0, /*nb_rshift*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3014 (binaryfunc)dictviews_and, /*nb_and*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3015 (binaryfunc)dictviews_xor, /*nb_xor*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3016 (binaryfunc)dictviews_or, /*nb_or*/ |
|
58256
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3017 }; |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3018 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3019 static PyMethodDef dictkeys_methods[] = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3020 {NULL, NULL} /* sentinel */ |
|
58256
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3021 }; |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3022 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3023 PyTypeObject PyDictKeys_Type = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3024 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3025 "dict_keys", /* tp_name */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3026 sizeof(dictviewobject), /* tp_basicsize */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3027 0, /* tp_itemsize */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3028 /* methods */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3029 (destructor)dictview_dealloc, /* tp_dealloc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3030 0, /* tp_print */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3031 0, /* tp_getattr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3032 0, /* tp_setattr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3033 0, /* tp_reserved */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3034 (reprfunc)dictview_repr, /* tp_repr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3035 &dictviews_as_number, /* tp_as_number */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3036 &dictkeys_as_sequence, /* tp_as_sequence */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3037 0, /* tp_as_mapping */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3038 0, /* tp_hash */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3039 0, /* tp_call */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3040 0, /* tp_str */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3041 PyObject_GenericGetAttr, /* tp_getattro */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3042 0, /* tp_setattro */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3043 0, /* tp_as_buffer */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3044 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3045 Py_TPFLAGS_CHECKTYPES, /* tp_flags */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3046 0, /* tp_doc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3047 (traverseproc)dictview_traverse, /* tp_traverse */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3048 0, /* tp_clear */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3049 dictview_richcompare, /* tp_richcompare */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3050 0, /* tp_weaklistoffset */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3051 (getiterfunc)dictkeys_iter, /* tp_iter */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3052 0, /* tp_iternext */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3053 dictkeys_methods, /* tp_methods */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3054 0, |
|
58256
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3055 }; |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3056 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3057 static PyObject * |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3058 dictkeys_new(PyObject *dict) |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3059 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3060 return dictview_new(dict, &PyDictKeys_Type); |
|
58256
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3061 } |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3062 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3063 /*** dict_items ***/ |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3064 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3065 static PyObject * |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3066 dictitems_iter(dictviewobject *dv) |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3067 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3068 if (dv->dv_dict == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3069 Py_RETURN_NONE; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3070 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3071 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type); |
|
58256
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3072 } |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3073 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3074 static int |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3075 dictitems_contains(dictviewobject *dv, PyObject *obj) |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3076 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3077 PyObject *key, *value, *found; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3078 if (dv->dv_dict == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3079 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3080 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3081 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3082 key = PyTuple_GET_ITEM(obj, 0); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3083 value = PyTuple_GET_ITEM(obj, 1); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3084 found = PyDict_GetItem((PyObject *)dv->dv_dict, key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3085 if (found == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3086 if (PyErr_Occurred()) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3087 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3088 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3089 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3090 return PyObject_RichCompareBool(value, found, Py_EQ); |
|
58256
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3091 } |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3092 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3093 static PySequenceMethods dictitems_as_sequence = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3094 (lenfunc)dictview_len, /* sq_length */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3095 0, /* sq_concat */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3096 0, /* sq_repeat */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3097 0, /* sq_item */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3098 0, /* sq_slice */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3099 0, /* sq_ass_item */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3100 0, /* sq_ass_slice */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3101 (objobjproc)dictitems_contains, /* sq_contains */ |
|
58256
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3102 }; |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3103 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3104 static PyMethodDef dictitems_methods[] = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3105 {NULL, NULL} /* sentinel */ |
|
58256
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3106 }; |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3107 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3108 PyTypeObject PyDictItems_Type = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3109 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3110 "dict_items", /* tp_name */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3111 sizeof(dictviewobject), /* tp_basicsize */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3112 0, /* tp_itemsize */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3113 /* methods */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3114 (destructor)dictview_dealloc, /* tp_dealloc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3115 0, /* tp_print */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3116 0, /* tp_getattr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3117 0, /* tp_setattr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3118 0, /* tp_reserved */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3119 (reprfunc)dictview_repr, /* tp_repr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3120 &dictviews_as_number, /* tp_as_number */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3121 &dictitems_as_sequence, /* tp_as_sequence */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3122 0, /* tp_as_mapping */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3123 0, /* tp_hash */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3124 0, /* tp_call */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3125 0, /* tp_str */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3126 PyObject_GenericGetAttr, /* tp_getattro */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3127 0, /* tp_setattro */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3128 0, /* tp_as_buffer */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3129 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3130 Py_TPFLAGS_CHECKTYPES, /* tp_flags */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3131 0, /* tp_doc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3132 (traverseproc)dictview_traverse, /* tp_traverse */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3133 0, /* tp_clear */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3134 dictview_richcompare, /* tp_richcompare */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3135 0, /* tp_weaklistoffset */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3136 (getiterfunc)dictitems_iter, /* tp_iter */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3137 0, /* tp_iternext */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3138 dictitems_methods, /* tp_methods */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3139 0, |
|
58256
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3140 }; |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3141 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3142 static PyObject * |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3143 dictitems_new(PyObject *dict) |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3144 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3145 return dictview_new(dict, &PyDictItems_Type); |
|
58256
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3146 } |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3147 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3148 /*** dict_values ***/ |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3149 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3150 static PyObject * |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3151 dictvalues_iter(dictviewobject *dv) |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3152 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3153 if (dv->dv_dict == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3154 Py_RETURN_NONE; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3155 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3156 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type); |
|
58256
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3157 } |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3158 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3159 static PySequenceMethods dictvalues_as_sequence = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3160 (lenfunc)dictview_len, /* sq_length */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3161 0, /* sq_concat */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3162 0, /* sq_repeat */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3163 0, /* sq_item */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3164 0, /* sq_slice */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3165 0, /* sq_ass_item */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3166 0, /* sq_ass_slice */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3167 (objobjproc)0, /* sq_contains */ |
|
58256
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3168 }; |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3169 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3170 static PyMethodDef dictvalues_methods[] = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3171 {NULL, NULL} /* sentinel */ |
|
58256
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3172 }; |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3173 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3174 PyTypeObject PyDictValues_Type = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3175 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3176 "dict_values", /* tp_name */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3177 sizeof(dictviewobject), /* tp_basicsize */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3178 0, /* tp_itemsize */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3179 /* methods */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3180 (destructor)dictview_dealloc, /* tp_dealloc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3181 0, /* tp_print */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3182 0, /* tp_getattr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3183 0, /* tp_setattr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3184 0, /* tp_reserved */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3185 (reprfunc)dictview_repr, /* tp_repr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3186 0, /* tp_as_number */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3187 &dictvalues_as_sequence, /* tp_as_sequence */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3188 0, /* tp_as_mapping */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3189 0, /* tp_hash */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3190 0, /* tp_call */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3191 0, /* tp_str */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3192 PyObject_GenericGetAttr, /* tp_getattro */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3193 0, /* tp_setattro */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3194 0, /* tp_as_buffer */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3195 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3196 0, /* tp_doc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3197 (traverseproc)dictview_traverse, /* tp_traverse */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3198 0, /* tp_clear */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3199 0, /* tp_richcompare */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3200 0, /* tp_weaklistoffset */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3201 (getiterfunc)dictvalues_iter, /* tp_iter */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3202 0, /* tp_iternext */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3203 dictvalues_methods, /* tp_methods */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3204 0, |
|
58256
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3205 }; |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3206 |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3207 static PyObject * |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3208 dictvalues_new(PyObject *dict) |
|
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3209 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
61207
diff
changeset
|
3210 return dictview_new(dict, &PyDictValues_Type); |
|
58256
d9805a96351c
Issue #1967: Backport dictionary views.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55437
diff
changeset
|
3211 } |
