annotate Objects/dictobject.c @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents 78ec18f5cb45
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1
10461
4abc5abaec9a Vladimir Marangozov contributed updated comments.
Guido van Rossum <guido@python.org>
parents: 9487
diff changeset
2 /* Dictionary object implementation using a hash table */
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1256
diff changeset
3
28946
dbdeb734ff97 Add a reference to dictnotes.txt. It does no good if you don't know it's
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
4 /* The distribution includes a separate file, Objects/dictnotes.txt,
36164
50886d42b444 Fixed English in a comment; trimmed trailing whitespace;
Tim Peters <tim.peters@gmail.com>
parents: 35636
diff changeset
5 describing explorations into dictionary design and optimization.
28946
dbdeb734ff97 Add a reference to dictnotes.txt. It does no good if you don't know it's
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
6 It covers typical dictionary use patterns, the parameters for
dbdeb734ff97 Add a reference to dictnotes.txt. It does no good if you don't know it's
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
7 tuning dictionaries, and several ideas for possible optimizations.
dbdeb734ff97 Add a reference to dictnotes.txt. It does no good if you don't know it's
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
8 */
dbdeb734ff97 Add a reference to dictnotes.txt. It does no good if you don't know it's
Raymond Hettinger <python@rcn.com>
parents: 28320
diff changeset
9
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
10
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
11 /*
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
12 There are four kinds of slots in the table:
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
13
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
14 1. Unused. me_key == me_value == NULL
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
15 Does not hold an active (key, value) pair now and never did. Unused can
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
16 transition to Active upon key insertion. This is the only case in which
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
17 me_key is NULL, and is each slot's initial state.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
18
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
19 2. Active. me_key != NULL and me_key != dummy and me_value != NULL
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
20 Holds an active (key, value) pair. Active can transition to Dummy or
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
21 Pending upon key deletion (for combined and split tables respectively).
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
22 This is the only case in which me_value != NULL.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
23
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
24 3. Dummy. me_key == dummy and me_value == NULL
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
25 Previously held an active (key, value) pair, but that was deleted and an
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
26 active pair has not yet overwritten the slot. Dummy can transition to
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
27 Active upon key insertion. Dummy slots cannot be made Unused again
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
28 (cannot have me_key set to NULL), else the probe sequence in case of
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
29 collision would have no way to know they were once active.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
30
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
31 4. Pending. Not yet inserted or deleted from a split-table.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
32 key != NULL, key != dummy and value == NULL
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
33
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
34 The DictObject can be in one of two forms.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
35 Either:
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
36 A combined table:
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
37 ma_values == NULL, dk_refcnt == 1.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
38 Values are stored in the me_value field of the PyDictKeysObject.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
39 Slot kind 4 is not allowed i.e.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
40 key != NULL, key != dummy and value == NULL is illegal.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
41 Or:
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
42 A split table:
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
43 ma_values != NULL, dk_refcnt >= 1
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
44 Values are stored in the ma_values array.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
45 Only string (unicode) keys are allowed, no <dummy> keys are present.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
46
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
47 Note: .popitem() abuses the me_hash field of an Unused or Dummy slot to
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
48 hold a search finger. The me_hash field of Unused or Dummy slots has no
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
49 meaning otherwise. As a consequence of this popitem always converts the dict
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
50 to the combined-table form.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
51 */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
52
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
53 /* PyDict_MINSIZE_SPLIT is the minimum size of a split dictionary.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
54 * It must be a power of 2, and at least 4.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
55 * Resizing of split dictionaries is very rare, so the saving memory is more
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
56 * important than the cost of resizing.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
57 */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
58 #define PyDict_MINSIZE_SPLIT 4
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
59
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
60 /* PyDict_MINSIZE_COMBINED is the starting size for any new, non-split dict.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
61 * 8 allows dicts with no more than 5 active entries; experiments suggested
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
62 * this suffices for the majority of dicts (consisting mostly of usually-small
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
63 * dicts created to pass keyword arguments).
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
64 * Making this 8, rather than 4 reduces the number of resizes for most
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
65 * dictionaries, without any significant extra memory use.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
66 */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
67 #define PyDict_MINSIZE_COMBINED 8
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
68
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
69 #include "Python.h"
44252
444da08f78a4 Fixed issue #1564: The set implementation should special-case PyUnicode instead of PyString
Christian Heimes <christian@cheimes.de>
parents: 44099
diff changeset
70 #include "stringlib/eq.h"
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
71
87470
78ec18f5cb45 Issue #19674: inspect.signature() now produces a correct signature
Larry Hastings <larry@hastings.org>
parents: 87265
diff changeset
72 /*[clinic]
78ec18f5cb45 Issue #19674: inspect.signature() now produces a correct signature
Larry Hastings <larry@hastings.org>
parents: 87265
diff changeset
73 class dict
78ec18f5cb45 Issue #19674: inspect.signature() now produces a correct signature
Larry Hastings <larry@hastings.org>
parents: 87265
diff changeset
74 [clinic]*/
78ec18f5cb45 Issue #19674: inspect.signature() now produces a correct signature
Larry Hastings <larry@hastings.org>
parents: 87265
diff changeset
75 /*[clinic checksum: da39a3ee5e6b4b0d3255bfef95601890afd80709]*/
78ec18f5cb45 Issue #19674: inspect.signature() now produces a correct signature
Larry Hastings <larry@hastings.org>
parents: 87265
diff changeset
76
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
77 typedef struct {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
78 /* Cached hash code of me_key. */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
79 Py_hash_t me_hash;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
80 PyObject *me_key;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
81 PyObject *me_value; /* This field is only meaningful for combined tables */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
82 } PyDictKeyEntry;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
83
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
84 typedef PyDictKeyEntry *(*dict_lookup_func)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
85 (PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject ***value_addr);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
86
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
87 struct _dictkeysobject {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
88 Py_ssize_t dk_refcnt;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
89 Py_ssize_t dk_size;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
90 dict_lookup_func dk_lookup;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
91 Py_ssize_t dk_usable;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
92 PyDictKeyEntry dk_entries[1];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
93 };
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
94
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
95
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
96 /*
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
97 To ensure the lookup algorithm terminates, there must be at least one Unused
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
98 slot (NULL key) in the table.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
99 To avoid slowing down lookups on a near-full table, we resize the table when
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
100 it's USABLE_FRACTION (currently two-thirds) full.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
101 */
5053
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
102
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
103 #define PERTURB_SHIFT 5
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
104
5053
219bdd8afb38 Changed the lookup algorithm again, based on Reimer Behrends's post.
Guido van Rossum <guido@python.org>
parents: 5038
diff changeset
105 /*
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
106 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
107 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
108 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
109 cases:
17973
2f2ee5daa60d Implement an old idea of Christian Tismer's: use polynomial division
Tim Peters <tim.peters@gmail.com>
parents: 17964
diff changeset
110
39657
ebad59c7233e Got test_mutants.py working. One set of changes was straightforward:
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
111 >>> map(hash, (0, 1, 2, 3))
ebad59c7233e Got test_mutants.py working. One set of changes was straightforward:
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
112 [0, 1, 2, 3]
ebad59c7233e Got test_mutants.py working. One set of changes was straightforward:
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
113 >>> map(hash, ("namea", "nameb", "namec", "named"))
ebad59c7233e Got test_mutants.py working. One set of changes was straightforward:
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
114 [-1658398457, -1658398460, -1658398459, -1658398462]
ebad59c7233e Got test_mutants.py working. One set of changes was straightforward:
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
115 >>>
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
116
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
117 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
118 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
119 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
120 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
121 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
122
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
123 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
124 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
125 the last i bits of the hash code is also vulnerable: for example, consider
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
126 the list [i << 16 for i in range(20000)] as a set of keys. Since ints are
39657
ebad59c7233e Got test_mutants.py working. One set of changes was straightforward:
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
127 their own hash codes, and this fits in a dict of size 2**15, the last 15 bits
ebad59c7233e Got test_mutants.py working. One set of changes was straightforward:
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
128 of every 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
129
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
130 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
131 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
132 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
133 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
134 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
135 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
136
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
137 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
138 recurrence:
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
139
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
140 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
141
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
142 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
143 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
144 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
145 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
146 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
147 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
148 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
149 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
150
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
151 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
152
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
153 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
154 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
155 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
156 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
157 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
158 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
159
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
160 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
161 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
162 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
163
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
164 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
165 perturb >>= PERTURB_SHIFT;
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
166 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
167
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
168 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
169 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
170 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
171 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
172 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
173 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
174 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
175 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
176
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
177 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
178 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
179 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
180 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
181 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
182 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
183
39657
ebad59c7233e Got test_mutants.py working. One set of changes was straightforward:
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
184 Historical: Reimer Behrends contributed the idea of using a polynomial-based
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
185 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
186 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
187 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
188 efficient way to get the high bits of the hash code into play. This scheme
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
189 also gave excellent collision statistics, but was more expensive: two
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
190 if-tests were required inside the loop; computing "the next" index took about
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
191 the same number of operations but without as much potential parallelism
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
192 (e.g., computing 5*j can go on at the same time as computing 1+perturb in the
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
193 above, and then shifting perturb can be done while the table index is being
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
194 masked); and the PyDictObject struct required a member to hold the table's
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
195 polynomial. In Tim's experiments the current scheme ran faster, produced
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
196 equally good collision statistics, needed less code & used less memory.
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
197
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
198 */
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
199
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
200 /* Object used as dummy key to fill deleted entries
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
201 * This could be any unique object,
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
202 * use a custom type in order to minimise coupling.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
203 */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
204 static PyObject _dummy_struct;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
205
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
206 #define dummy (&_dummy_struct)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
207
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36657
diff changeset
208 #ifdef Py_REF_DEBUG
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36657
diff changeset
209 PyObject *
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36657
diff changeset
210 _PyDict_Dummy(void)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36657
diff changeset
211 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
212 return dummy;
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36657
diff changeset
213 }
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36657
diff changeset
214 #endif
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36657
diff changeset
215
14310
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
216 /* forward declarations */
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
217 static PyDictKeyEntry *lookdict(PyDictObject *mp, PyObject *key,
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
218 Py_hash_t hash, PyObject ***value_addr);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
219 static PyDictKeyEntry *lookdict_unicode(PyDictObject *mp, PyObject *key,
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
220 Py_hash_t hash, PyObject ***value_addr);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
221 static PyDictKeyEntry *
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
222 lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
223 Py_hash_t hash, PyObject ***value_addr);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
224 static PyDictKeyEntry *lookdict_split(PyDictObject *mp, PyObject *key,
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
225 Py_hash_t hash, PyObject ***value_addr);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
226
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
227 static int dictresize(PyDictObject *mp, Py_ssize_t minused);
17920
16005b285576 SF patch #425242: Patch which "inlines" small dictionaries.
Tim Peters <tim.peters@gmail.com>
parents: 17861
diff changeset
228
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
229 /* Dictionary reuse scheme to save calls to malloc, free, and memset */
45192
142a11b3057e Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60568-60598,60600-60616 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44999
diff changeset
230 #ifndef PyDict_MAXFREELIST
142a11b3057e Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60568-60598,60600-60616 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44999
diff changeset
231 #define PyDict_MAXFREELIST 80
142a11b3057e Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60568-60598,60600-60616 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44999
diff changeset
232 #endif
142a11b3057e Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60568-60598,60600-60616 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44999
diff changeset
233 static PyDictObject *free_list[PyDict_MAXFREELIST];
142a11b3057e Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60568-60598,60600-60616 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44999
diff changeset
234 static int numfree = 0;
31677
7bb030066ea4 Dictionary optimizations:
Raymond Hettinger <python@rcn.com>
parents: 31620
diff changeset
235
73551
910986542a75 Issue #13389: Full garbage collection passes now clear the freelists for
Antoine Pitrou <solipsis@pitrou.net>
parents: 73104
diff changeset
236 int
910986542a75 Issue #13389: Full garbage collection passes now clear the freelists for
Antoine Pitrou <solipsis@pitrou.net>
parents: 73104
diff changeset
237 PyDict_ClearFreeList(void)
45244
2a821e001a2b Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617-60678 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45192
diff changeset
238 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
239 PyDictObject *op;
73551
910986542a75 Issue #13389: Full garbage collection passes now clear the freelists for
Antoine Pitrou <solipsis@pitrou.net>
parents: 73104
diff changeset
240 int ret = numfree;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
241 while (numfree) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
242 op = free_list[--numfree];
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
243 assert(PyDict_CheckExact(op));
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
244 PyObject_GC_Del(op);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
245 }
73551
910986542a75 Issue #13389: Full garbage collection passes now clear the freelists for
Antoine Pitrou <solipsis@pitrou.net>
parents: 73104
diff changeset
246 return ret;
910986542a75 Issue #13389: Full garbage collection passes now clear the freelists for
Antoine Pitrou <solipsis@pitrou.net>
parents: 73104
diff changeset
247 }
910986542a75 Issue #13389: Full garbage collection passes now clear the freelists for
Antoine Pitrou <solipsis@pitrou.net>
parents: 73104
diff changeset
248
77577
d63a80abfbec Issue #14785: Add sys._debugmallocstats() to help debug low-level memory allocation issues
David Malcolm <dmalcolm@redhat.com>
parents: 76907
diff changeset
249 /* Print summary info about the state of the optimized allocator */
d63a80abfbec Issue #14785: Add sys._debugmallocstats() to help debug low-level memory allocation issues
David Malcolm <dmalcolm@redhat.com>
parents: 76907
diff changeset
250 void
d63a80abfbec Issue #14785: Add sys._debugmallocstats() to help debug low-level memory allocation issues
David Malcolm <dmalcolm@redhat.com>
parents: 76907
diff changeset
251 _PyDict_DebugMallocStats(FILE *out)
d63a80abfbec Issue #14785: Add sys._debugmallocstats() to help debug low-level memory allocation issues
David Malcolm <dmalcolm@redhat.com>
parents: 76907
diff changeset
252 {
d63a80abfbec Issue #14785: Add sys._debugmallocstats() to help debug low-level memory allocation issues
David Malcolm <dmalcolm@redhat.com>
parents: 76907
diff changeset
253 _PyDebugAllocatorStats(out,
d63a80abfbec Issue #14785: Add sys._debugmallocstats() to help debug low-level memory allocation issues
David Malcolm <dmalcolm@redhat.com>
parents: 76907
diff changeset
254 "free PyDictObject", numfree, sizeof(PyDictObject));
d63a80abfbec Issue #14785: Add sys._debugmallocstats() to help debug low-level memory allocation issues
David Malcolm <dmalcolm@redhat.com>
parents: 76907
diff changeset
255 }
d63a80abfbec Issue #14785: Add sys._debugmallocstats() to help debug low-level memory allocation issues
David Malcolm <dmalcolm@redhat.com>
parents: 76907
diff changeset
256
d63a80abfbec Issue #14785: Add sys._debugmallocstats() to help debug low-level memory allocation issues
David Malcolm <dmalcolm@redhat.com>
parents: 76907
diff changeset
257
73551
910986542a75 Issue #13389: Full garbage collection passes now clear the freelists for
Antoine Pitrou <solipsis@pitrou.net>
parents: 73104
diff changeset
258 void
910986542a75 Issue #13389: Full garbage collection passes now clear the freelists for
Antoine Pitrou <solipsis@pitrou.net>
parents: 73104
diff changeset
259 PyDict_Fini(void)
910986542a75 Issue #13389: Full garbage collection passes now clear the freelists for
Antoine Pitrou <solipsis@pitrou.net>
parents: 73104
diff changeset
260 {
910986542a75 Issue #13389: Full garbage collection passes now clear the freelists for
Antoine Pitrou <solipsis@pitrou.net>
parents: 73104
diff changeset
261 PyDict_ClearFreeList();
45244
2a821e001a2b Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617-60678 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45192
diff changeset
262 }
2a821e001a2b Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617-60678 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45192
diff changeset
263
76892
10e8b97d0fd7 Make the reference counting of dictkeys objects participate in refleak hunting
Antoine Pitrou <solipsis@pitrou.net>
parents: 76666
diff changeset
264 #define DK_DEBUG_INCREF _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA
10e8b97d0fd7 Make the reference counting of dictkeys objects participate in refleak hunting
Antoine Pitrou <solipsis@pitrou.net>
parents: 76666
diff changeset
265 #define DK_DEBUG_DECREF _Py_DEC_REFTOTAL _Py_REF_DEBUG_COMMA
10e8b97d0fd7 Make the reference counting of dictkeys objects participate in refleak hunting
Antoine Pitrou <solipsis@pitrou.net>
parents: 76666
diff changeset
266
10e8b97d0fd7 Make the reference counting of dictkeys objects participate in refleak hunting
Antoine Pitrou <solipsis@pitrou.net>
parents: 76666
diff changeset
267 #define DK_INCREF(dk) (DK_DEBUG_INCREF ++(dk)->dk_refcnt)
10e8b97d0fd7 Make the reference counting of dictkeys objects participate in refleak hunting
Antoine Pitrou <solipsis@pitrou.net>
parents: 76666
diff changeset
268 #define DK_DECREF(dk) if (DK_DEBUG_DECREF (--(dk)->dk_refcnt) == 0) free_keys_object(dk)
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
269 #define DK_SIZE(dk) ((dk)->dk_size)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
270 #define DK_MASK(dk) (((dk)->dk_size)-1)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
271 #define IS_POWER_OF_2(x) (((x) & (x-1)) == 0)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
272
77740
1120041f2df4 Issue #15055: update dictnotes.txt. Patch by Mark Shannon.
Antoine Pitrou <solipsis@pitrou.net>
parents: 77577
diff changeset
273 /* USABLE_FRACTION is the maximum dictionary load.
1120041f2df4 Issue #15055: update dictnotes.txt. Patch by Mark Shannon.
Antoine Pitrou <solipsis@pitrou.net>
parents: 77577
diff changeset
274 * Currently set to (2n+1)/3. Increasing this ratio makes dictionaries more
1120041f2df4 Issue #15055: update dictnotes.txt. Patch by Mark Shannon.
Antoine Pitrou <solipsis@pitrou.net>
parents: 77577
diff changeset
275 * dense resulting in more collisions. Decreasing it improves sparseness
1120041f2df4 Issue #15055: update dictnotes.txt. Patch by Mark Shannon.
Antoine Pitrou <solipsis@pitrou.net>
parents: 77577
diff changeset
276 * at the expense of spreading entries over more cache lines and at the
1120041f2df4 Issue #15055: update dictnotes.txt. Patch by Mark Shannon.
Antoine Pitrou <solipsis@pitrou.net>
parents: 77577
diff changeset
277 * cost of total memory consumed.
1120041f2df4 Issue #15055: update dictnotes.txt. Patch by Mark Shannon.
Antoine Pitrou <solipsis@pitrou.net>
parents: 77577
diff changeset
278 *
1120041f2df4 Issue #15055: update dictnotes.txt. Patch by Mark Shannon.
Antoine Pitrou <solipsis@pitrou.net>
parents: 77577
diff changeset
279 * USABLE_FRACTION must obey the following:
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
280 * (0 < USABLE_FRACTION(n) < n) for all n >= 2
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
281 *
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
282 * USABLE_FRACTION should be very quick to calculate.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
283 * Fractions around 5/8 to 2/3 seem to work well in practice.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
284 */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
285
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
286 /* Use (2n+1)/3 rather than 2n+3 because: it makes no difference for
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
287 * combined tables (the two fractions round to the same number n < ),
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
288 * but 2*4/3 is 2 whereas (2*4+1)/3 is 3 which potentially saves quite
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
289 * a lot of space for small, split tables */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
290 #define USABLE_FRACTION(n) ((((n) << 1)+1)/3)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
291
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
292 /* Alternative fraction that is otherwise close enough to (2n+1)/3 to make
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
293 * little difference. 8 * 2/3 == 8 * 5/8 == 5. 16 * 2/3 == 16 * 5/8 == 10.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
294 * 32 * 2/3 = 21, 32 * 5/8 = 20.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
295 * Its advantage is that it is faster to compute on machines with slow division.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
296 * #define USABLE_FRACTION(n) (((n) >> 1) + ((n) >> 2) - ((n) >> 3))
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
297 */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
298
84673
5beb1f12f19f Cleanup dictobject.c
Victor Stinner <victor.stinner@gmail.com>
parents: 84672
diff changeset
299 /* GROWTH_RATE. Growth rate upon hitting maximum load.
5beb1f12f19f Cleanup dictobject.c
Victor Stinner <victor.stinner@gmail.com>
parents: 84672
diff changeset
300 * Currently set to used*2 + capacity/2.
5beb1f12f19f Cleanup dictobject.c
Victor Stinner <victor.stinner@gmail.com>
parents: 84672
diff changeset
301 * This means that dicts double in size when growing without deletions,
83812
cd2457463eeb Issue #17563: Fix dict resize performance regression.
Raymond Hettinger <python@rcn.com>
parents: 82476
diff changeset
302 * but have more head room when the number of deletions is on a par with the
cd2457463eeb Issue #17563: Fix dict resize performance regression.
Raymond Hettinger <python@rcn.com>
parents: 82476
diff changeset
303 * number of insertions.
cd2457463eeb Issue #17563: Fix dict resize performance regression.
Raymond Hettinger <python@rcn.com>
parents: 82476
diff changeset
304 * Raising this to used*4 doubles memory consumption depending on the size of
77740
1120041f2df4 Issue #15055: update dictnotes.txt. Patch by Mark Shannon.
Antoine Pitrou <solipsis@pitrou.net>
parents: 77577
diff changeset
305 * the dictionary, but results in half the number of resizes, less effort to
83812
cd2457463eeb Issue #17563: Fix dict resize performance regression.
Raymond Hettinger <python@rcn.com>
parents: 82476
diff changeset
306 * resize.
cd2457463eeb Issue #17563: Fix dict resize performance regression.
Raymond Hettinger <python@rcn.com>
parents: 82476
diff changeset
307 * GROWTH_RATE was set to used*4 up to version 3.2.
cd2457463eeb Issue #17563: Fix dict resize performance regression.
Raymond Hettinger <python@rcn.com>
parents: 82476
diff changeset
308 * GROWTH_RATE was set to used*2 in version 3.3.0
77740
1120041f2df4 Issue #15055: update dictnotes.txt. Patch by Mark Shannon.
Antoine Pitrou <solipsis@pitrou.net>
parents: 77577
diff changeset
309 */
83812
cd2457463eeb Issue #17563: Fix dict resize performance regression.
Raymond Hettinger <python@rcn.com>
parents: 82476
diff changeset
310 #define GROWTH_RATE(d) (((d)->ma_used*2)+((d)->ma_keys->dk_size>>1))
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
311
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
312 #define ENSURE_ALLOWS_DELETIONS(d) \
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
313 if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
314 (d)->ma_keys->dk_lookup = lookdict_unicode; \
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
315 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
316
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
317 /* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
318 * (which cannot fail and thus can do no allocation).
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
319 */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
320 static PyDictKeysObject empty_keys_struct = {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
321 2, /* dk_refcnt 1 for this struct, 1 for dummy_struct */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
322 1, /* dk_size */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
323 lookdict_split, /* dk_lookup */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
324 0, /* dk_usable (immutable) */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
325 {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
326 { 0, 0, 0 } /* dk_entries (empty) */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
327 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
328 };
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
329
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
330 static PyObject *empty_values[1] = { NULL };
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
331
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
332 #define Py_EMPTY_KEYS &empty_keys_struct
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
333
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
334 static PyDictKeysObject *new_keys_object(Py_ssize_t size)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
335 {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
336 PyDictKeysObject *dk;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
337 Py_ssize_t i;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
338 PyDictKeyEntry *ep0;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
339
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
340 assert(size >= PyDict_MINSIZE_SPLIT);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
341 assert(IS_POWER_OF_2(size));
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
342 dk = PyMem_MALLOC(sizeof(PyDictKeysObject) +
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
343 sizeof(PyDictKeyEntry) * (size-1));
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
344 if (dk == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
345 PyErr_NoMemory();
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
346 return NULL;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
347 }
76892
10e8b97d0fd7 Make the reference counting of dictkeys objects participate in refleak hunting
Antoine Pitrou <solipsis@pitrou.net>
parents: 76666
diff changeset
348 DK_DEBUG_INCREF dk->dk_refcnt = 1;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
349 dk->dk_size = size;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
350 dk->dk_usable = USABLE_FRACTION(size);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
351 ep0 = &dk->dk_entries[0];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
352 /* Hash value of slot 0 is used by popitem, so it must be initialized */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
353 ep0->me_hash = 0;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
354 for (i = 0; i < size; i++) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
355 ep0[i].me_key = NULL;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
356 ep0[i].me_value = NULL;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
357 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
358 dk->dk_lookup = lookdict_unicode_nodummy;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
359 return dk;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
360 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
361
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
362 static void
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
363 free_keys_object(PyDictKeysObject *keys)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
364 {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
365 PyDictKeyEntry *entries = &keys->dk_entries[0];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
366 Py_ssize_t i, n;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
367 for (i = 0, n = DK_SIZE(keys); i < n; i++) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
368 Py_XDECREF(entries[i].me_key);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
369 Py_XDECREF(entries[i].me_value);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
370 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
371 PyMem_FREE(keys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
372 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
373
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
374 #define new_values(size) PyMem_NEW(PyObject *, size)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
375
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
376 #define free_values(values) PyMem_FREE(values)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
377
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
378 /* Consumes a reference to the keys object */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
379 static PyObject *
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
380 new_dict(PyDictKeysObject *keys, PyObject **values)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
381 {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
382 PyDictObject *mp;
84509
ba766323b53a Issue #18408: Fix PyDict_New() to handle correctly new_keys_object() failure
Victor Stinner <victor.stinner@gmail.com>
parents: 83813
diff changeset
383 assert(keys != NULL);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
384 if (numfree) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
385 mp = free_list[--numfree];
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
386 assert (mp != NULL);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
387 assert (Py_TYPE(mp) == &PyDict_Type);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
388 _Py_NewReference((PyObject *)mp);
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
389 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
390 else {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
391 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
392 if (mp == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
393 DK_DECREF(keys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
394 free_values(values);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
395 return NULL;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
396 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
397 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
398 mp->ma_keys = keys;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
399 mp->ma_values = values;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
400 mp->ma_used = 0;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
401 return (PyObject *)mp;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
402 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
403
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
404 /* Consumes a reference to the keys object */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
405 static PyObject *
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
406 new_dict_with_shared_keys(PyDictKeysObject *keys)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
407 {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
408 PyObject **values;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
409 Py_ssize_t i, size;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
410
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
411 size = DK_SIZE(keys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
412 values = new_values(size);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
413 if (values == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
414 DK_DECREF(keys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
415 return PyErr_NoMemory();
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
416 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
417 for (i = 0; i < size; i++) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
418 values[i] = NULL;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
419 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
420 return new_dict(keys, values);
45244
2a821e001a2b Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617-60678 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45192
diff changeset
421 }
2a821e001a2b Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617-60678 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45192
diff changeset
422
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
423 PyObject *
13660
a1ce07218781 Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
424 PyDict_New(void)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
425 {
84509
ba766323b53a Issue #18408: Fix PyDict_New() to handle correctly new_keys_object() failure
Victor Stinner <victor.stinner@gmail.com>
parents: 83813
diff changeset
426 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE_COMBINED);
ba766323b53a Issue #18408: Fix PyDict_New() to handle correctly new_keys_object() failure
Victor Stinner <victor.stinner@gmail.com>
parents: 83813
diff changeset
427 if (keys == NULL)
ba766323b53a Issue #18408: Fix PyDict_New() to handle correctly new_keys_object() failure
Victor Stinner <victor.stinner@gmail.com>
parents: 83813
diff changeset
428 return NULL;
ba766323b53a Issue #18408: Fix PyDict_New() to handle correctly new_keys_object() failure
Victor Stinner <victor.stinner@gmail.com>
parents: 83813
diff changeset
429 return new_dict(keys, NULL);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
430 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
431
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
432 /*
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
433 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
434 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
435 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
436 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
437
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
438 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
439 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
440
10461
4abc5abaec9a Vladimir Marangozov contributed updated comments.
Guido van Rossum <guido@python.org>
parents: 9487
diff changeset
441 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
442
39657
ebad59c7233e Got test_mutants.py working. One set of changes was straightforward:
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
443 The details in this version are due to Tim Peters, building on many past
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
444 contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and
39657
ebad59c7233e Got test_mutants.py working. One set of changes was straightforward:
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
445 Christian Tismer.
14310
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
446
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
447 lookdict() is general-purpose, and may return NULL if (and only if) a
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
448 comparison raises an exception (this was new in Python 2.5).
43392
ef223546b4bd Switch dict lookup around to optimizing for unicode.
Guido van Rossum <guido@python.org>
parents: 43389
diff changeset
449 lookdict_unicode() below is specialized to string keys, comparison of which can
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
450 never raise an exception; that function can never return NULL.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
451 lookdict_unicode_nodummy is further specialized for string keys that cannot be
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
452 the <dummy> value.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
453 For both, when the key isn't found a PyDictEntry* is returned
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
454 where the key would have been found, *value_addr points to the matching value
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
455 slot.
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
456 */
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
457 static PyDictKeyEntry *
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
458 lookdict(PyDictObject *mp, PyObject *key,
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
459 Py_hash_t hash, PyObject ***value_addr)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
460 {
85157
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
461 size_t i;
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
462 size_t perturb;
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
463 PyDictKeyEntry *freeslot;
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
464 size_t mask;
76907
93748e2d64e3 Issue #14417: Mutating a dict during lookup now restarts the lookup instead of raising a RuntimeError (undoes issue #14205).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76892
diff changeset
465 PyDictKeyEntry *ep0;
85157
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
466 PyDictKeyEntry *ep;
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
467 int cmp;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
468 PyObject *startkey;
18014
9f97167f1643 New collision resolution scheme: no polynomials, simpler, faster, less
Tim Peters <tim.peters@gmail.com>
parents: 17973
diff changeset
469
76907
93748e2d64e3 Issue #14417: Mutating a dict during lookup now restarts the lookup instead of raising a RuntimeError (undoes issue #14205).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76892
diff changeset
470 top:
93748e2d64e3 Issue #14417: Mutating a dict during lookup now restarts the lookup instead of raising a RuntimeError (undoes issue #14205).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76892
diff changeset
471 mask = DK_MASK(mp->ma_keys);
93748e2d64e3 Issue #14417: Mutating a dict during lookup now restarts the lookup instead of raising a RuntimeError (undoes issue #14205).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76892
diff changeset
472 ep0 = &mp->ma_keys->dk_entries[0];
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
473 i = (size_t)hash & mask;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
474 ep = &ep0[i];
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
475 if (ep->me_key == NULL || ep->me_key == key) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
476 *value_addr = &ep->me_value;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
477 return ep;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
478 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
479 if (ep->me_key == dummy)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
480 freeslot = ep;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
481 else {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
482 if (ep->me_hash == hash) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
483 startkey = ep->me_key;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
484 Py_INCREF(startkey);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
485 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
486 Py_DECREF(startkey);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
487 if (cmp < 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
488 return NULL;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
489 if (ep0 == mp->ma_keys->dk_entries && ep->me_key == startkey) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
490 if (cmp > 0) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
491 *value_addr = &ep->me_value;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
492 return ep;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
493 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
494 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
495 else {
76907
93748e2d64e3 Issue #14417: Mutating a dict during lookup now restarts the lookup instead of raising a RuntimeError (undoes issue #14205).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76892
diff changeset
496 /* The dict was mutated, restart */
93748e2d64e3 Issue #14417: Mutating a dict during lookup now restarts the lookup instead of raising a RuntimeError (undoes issue #14205).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76892
diff changeset
497 goto top;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
498 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
499 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
500 freeslot = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
501 }
17973
2f2ee5daa60d Implement an old idea of Christian Tismer's: use polynomial division
Tim Peters <tim.peters@gmail.com>
parents: 17964
diff changeset
502
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
503 /* In the loop, me_key == dummy is by far (factor of 100s) the
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
504 least likely outcome, so test for that last. */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
505 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
506 i = (i << 2) + i + perturb + 1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
507 ep = &ep0[i & mask];
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
508 if (ep->me_key == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
509 if (freeslot == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
510 *value_addr = &ep->me_value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
511 return ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
512 } else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
513 *value_addr = &freeslot->me_value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
514 return freeslot;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
515 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
516 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
517 if (ep->me_key == key) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
518 *value_addr = &ep->me_value;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
519 return ep;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
520 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
521 if (ep->me_hash == hash && ep->me_key != dummy) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
522 startkey = ep->me_key;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
523 Py_INCREF(startkey);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
524 cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
525 Py_DECREF(startkey);
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
526 if (cmp < 0) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
527 *value_addr = NULL;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
528 return NULL;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
529 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
530 if (ep0 == mp->ma_keys->dk_entries && ep->me_key == startkey) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
531 if (cmp > 0) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
532 *value_addr = &ep->me_value;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
533 return ep;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
534 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
535 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
536 else {
76907
93748e2d64e3 Issue #14417: Mutating a dict during lookup now restarts the lookup instead of raising a RuntimeError (undoes issue #14205).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76892
diff changeset
537 /* The dict was mutated, restart */
93748e2d64e3 Issue #14417: Mutating a dict during lookup now restarts the lookup instead of raising a RuntimeError (undoes issue #14205).
Antoine Pitrou <solipsis@pitrou.net>
parents: 76892
diff changeset
538 goto top;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
539 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
540 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
541 else if (ep->me_key == dummy && freeslot == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
542 freeslot = ep;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
543 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
544 assert(0); /* NOT REACHED */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
545 return 0;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
546 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
547
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
548 /* Specialized version for string-only keys */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
549 static PyDictKeyEntry *
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
550 lookdict_unicode(PyDictObject *mp, PyObject *key,
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
551 Py_hash_t hash, PyObject ***value_addr)
14310
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
552 {
85157
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
553 size_t i;
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
554 size_t perturb;
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
555 PyDictKeyEntry *freeslot;
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
556 size_t mask = DK_MASK(mp->ma_keys);
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
557 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
85157
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
558 PyDictKeyEntry *ep;
14310
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
559
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
560 /* Make sure this function doesn't have to handle non-unicode keys,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
561 including subclasses of str; e.g., one reason to subclass
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
562 unicodes is to override __eq__, and for speed we don't cater to
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
563 that here. */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
564 if (!PyUnicode_CheckExact(key)) {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
565 mp->ma_keys->dk_lookup = lookdict;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
566 return lookdict(mp, key, hash, value_addr);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
567 }
72464
698fa089ce70 Issue #1621: Fix undefined behaviour in bytes.__hash__, str.__hash__, tuple.__hash__, frozenset.__hash__ and set indexing operations.
Mark Dickinson <mdickinson@enthought.com>
parents: 71810
diff changeset
568 i = (size_t)hash & mask;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
569 ep = &ep0[i];
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
570 if (ep->me_key == NULL || ep->me_key == key) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
571 *value_addr = &ep->me_value;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
572 return ep;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
573 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
574 if (ep->me_key == dummy)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
575 freeslot = ep;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
576 else {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
577 if (ep->me_hash == hash && unicode_eq(ep->me_key, key)) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
578 *value_addr = &ep->me_value;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
579 return ep;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
580 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
581 freeslot = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
582 }
17973
2f2ee5daa60d Implement an old idea of Christian Tismer's: use polynomial division
Tim Peters <tim.peters@gmail.com>
parents: 17964
diff changeset
583
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
584 /* In the loop, me_key == dummy is by far (factor of 100s) the
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
585 least likely outcome, so test for that last. */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
586 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
587 i = (i << 2) + i + perturb + 1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
588 ep = &ep0[i & mask];
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
589 if (ep->me_key == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
590 if (freeslot == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
591 *value_addr = &ep->me_value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
592 return ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
593 } else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
594 *value_addr = &freeslot->me_value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
595 return freeslot;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
596 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
597 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
598 if (ep->me_key == key
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
599 || (ep->me_hash == hash
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
600 && ep->me_key != dummy
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
601 && unicode_eq(ep->me_key, key))) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
602 *value_addr = &ep->me_value;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
603 return ep;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
604 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
605 if (ep->me_key == dummy && freeslot == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
606 freeslot = ep;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
607 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
608 assert(0); /* NOT REACHED */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
609 return 0;
14310
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
610 }
2db807f345bc Slight performance hack that also avoids requiring the existence of thread
Fred Drake <fdrake@acm.org>
parents: 14307
diff changeset
611
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
612 /* Faster version of lookdict_unicode when it is known that no <dummy> keys
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
613 * will be present. */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
614 static PyDictKeyEntry *
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
615 lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key,
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
616 Py_hash_t hash, PyObject ***value_addr)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
617 {
85157
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
618 size_t i;
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
619 size_t perturb;
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
620 size_t mask = DK_MASK(mp->ma_keys);
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
621 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
85157
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
622 PyDictKeyEntry *ep;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
623
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
624 /* Make sure this function doesn't have to handle non-unicode keys,
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
625 including subclasses of str; e.g., one reason to subclass
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
626 unicodes is to override __eq__, and for speed we don't cater to
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
627 that here. */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
628 if (!PyUnicode_CheckExact(key)) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
629 mp->ma_keys->dk_lookup = lookdict;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
630 return lookdict(mp, key, hash, value_addr);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
631 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
632 i = (size_t)hash & mask;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
633 ep = &ep0[i];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
634 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
635 if (ep->me_key == NULL || ep->me_key == key ||
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
636 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
637 *value_addr = &ep->me_value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
638 return ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
639 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
640 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
641 i = (i << 2) + i + perturb + 1;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
642 ep = &ep0[i & mask];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
643 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
644 if (ep->me_key == NULL || ep->me_key == key ||
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
645 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
646 *value_addr = &ep->me_value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
647 return ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
648 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
649 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
650 assert(0); /* NOT REACHED */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
651 return 0;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
652 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
653
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
654 /* Version of lookdict for split tables.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
655 * All split tables and only split tables use this lookup function.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
656 * Split tables only contain unicode keys and no dummy keys,
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
657 * so algorithm is the same as lookdict_unicode_nodummy.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
658 */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
659 static PyDictKeyEntry *
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
660 lookdict_split(PyDictObject *mp, PyObject *key,
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
661 Py_hash_t hash, PyObject ***value_addr)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
662 {
85157
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
663 size_t i;
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
664 size_t perturb;
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
665 size_t mask = DK_MASK(mp->ma_keys);
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
666 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
85157
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
667 PyDictKeyEntry *ep;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
668
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
669 if (!PyUnicode_CheckExact(key)) {
76503
34b6998efd2c fix instance dicts with str subclasses (#13903)
Benjamin Peterson <benjamin@python.org>
parents: 76489
diff changeset
670 ep = lookdict(mp, key, hash, value_addr);
34b6998efd2c fix instance dicts with str subclasses (#13903)
Benjamin Peterson <benjamin@python.org>
parents: 76489
diff changeset
671 /* lookdict expects a combined-table, so fix value_addr */
34b6998efd2c fix instance dicts with str subclasses (#13903)
Benjamin Peterson <benjamin@python.org>
parents: 76489
diff changeset
672 i = ep - ep0;
34b6998efd2c fix instance dicts with str subclasses (#13903)
Benjamin Peterson <benjamin@python.org>
parents: 76489
diff changeset
673 *value_addr = &mp->ma_values[i];
34b6998efd2c fix instance dicts with str subclasses (#13903)
Benjamin Peterson <benjamin@python.org>
parents: 76489
diff changeset
674 return ep;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
675 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
676 i = (size_t)hash & mask;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
677 ep = &ep0[i];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
678 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
679 if (ep->me_key == NULL || ep->me_key == key ||
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
680 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
681 *value_addr = &mp->ma_values[i];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
682 return ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
683 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
684 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
685 i = (i << 2) + i + perturb + 1;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
686 ep = &ep0[i & mask];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
687 assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key));
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
688 if (ep->me_key == NULL || ep->me_key == key ||
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
689 (ep->me_hash == hash && unicode_eq(ep->me_key, key))) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
690 *value_addr = &mp->ma_values[i & mask];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
691 return ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
692 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
693 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
694 assert(0); /* NOT REACHED */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
695 return 0;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
696 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
697
60931
79128efe8a99 prevent the dict constructor from accepting non-string keyword args #8419
Benjamin Peterson <benjamin@python.org>
parents: 59224
diff changeset
698 int
79128efe8a99 prevent the dict constructor from accepting non-string keyword args #8419
Benjamin Peterson <benjamin@python.org>
parents: 59224
diff changeset
699 _PyDict_HasOnlyStringKeys(PyObject *dict)
79128efe8a99 prevent the dict constructor from accepting non-string keyword args #8419
Benjamin Peterson <benjamin@python.org>
parents: 59224
diff changeset
700 {
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61184
diff changeset
701 Py_ssize_t pos = 0;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61184
diff changeset
702 PyObject *key, *value;
66220
379029714e24 handle dict subclasses gracefully in PyArg_ValidateKeywordArguments
Benjamin Peterson <benjamin@python.org>
parents: 65753
diff changeset
703 assert(PyDict_Check(dict));
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61184
diff changeset
704 /* Shortcut */
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
705 if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict)
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61184
diff changeset
706 return 1;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61184
diff changeset
707 while (PyDict_Next(dict, &pos, &key, &value))
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61184
diff changeset
708 if (!PyUnicode_Check(key))
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61184
diff changeset
709 return 0;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61184
diff changeset
710 return 1;
60931
79128efe8a99 prevent the dict constructor from accepting non-string keyword args #8419
Benjamin Peterson <benjamin@python.org>
parents: 59224
diff changeset
711 }
79128efe8a99 prevent the dict constructor from accepting non-string keyword args #8419
Benjamin Peterson <benjamin@python.org>
parents: 59224
diff changeset
712
52291
94bd0666ffa7 Merged revisions 70546 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51971
diff changeset
713 #define MAINTAIN_TRACKING(mp, key, value) \
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
714 do { \
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
715 if (!_PyObject_GC_IS_TRACKED(mp)) { \
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
716 if (_PyObject_GC_MAY_BE_TRACKED(key) || \
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
717 _PyObject_GC_MAY_BE_TRACKED(value)) { \
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
718 _PyObject_GC_TRACK(mp); \
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
719 } \
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
720 } \
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
721 } while(0)
52291
94bd0666ffa7 Merged revisions 70546 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51971
diff changeset
722
94bd0666ffa7 Merged revisions 70546 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51971
diff changeset
723 void
94bd0666ffa7 Merged revisions 70546 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51971
diff changeset
724 _PyDict_MaybeUntrack(PyObject *op)
94bd0666ffa7 Merged revisions 70546 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51971
diff changeset
725 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
726 PyDictObject *mp;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
727 PyObject *value;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
728 Py_ssize_t i, size;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
729
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
730 if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
731 return;
52291
94bd0666ffa7 Merged revisions 70546 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51971
diff changeset
732
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
733 mp = (PyDictObject *) op;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
734 size = DK_SIZE(mp->ma_keys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
735 if (_PyDict_HasSplitTable(mp)) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
736 for (i = 0; i < size; i++) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
737 if ((value = mp->ma_values[i]) == NULL)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
738 continue;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
739 if (_PyObject_GC_MAY_BE_TRACKED(value)) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
740 assert(!_PyObject_GC_MAY_BE_TRACKED(
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
741 mp->ma_keys->dk_entries[i].me_key));
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
742 return;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
743 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
744 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
745 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
746 else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
747 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
748 for (i = 0; i < size; i++) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
749 if ((value = ep0[i].me_value) == NULL)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
750 continue;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
751 if (_PyObject_GC_MAY_BE_TRACKED(value) ||
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
752 _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key))
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
753 return;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
754 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
755 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
756 _PyObject_GC_UNTRACK(op);
52291
94bd0666ffa7 Merged revisions 70546 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51971
diff changeset
757 }
94bd0666ffa7 Merged revisions 70546 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51971
diff changeset
758
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
759 /* Internal function to find slot for an item from its hash
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
760 * when it is known that the key is not present in the dict.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
761 */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
762 static PyDictKeyEntry *
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
763 find_empty_slot(PyDictObject *mp, PyObject *key, Py_hash_t hash,
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
764 PyObject ***value_addr)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
765 {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
766 size_t i;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
767 size_t perturb;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
768 size_t mask = DK_MASK(mp->ma_keys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
769 PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
770 PyDictKeyEntry *ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
771
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
772 assert(key != NULL);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
773 if (!PyUnicode_CheckExact(key))
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
774 mp->ma_keys->dk_lookup = lookdict;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
775 i = hash & mask;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
776 ep = &ep0[i];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
777 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
778 i = (i << 2) + i + perturb + 1;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
779 ep = &ep0[i & mask];
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
780 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
781 assert(ep->me_value == NULL);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
782 if (mp->ma_values)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
783 *value_addr = &mp->ma_values[i & mask];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
784 else
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
785 *value_addr = &ep->me_value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
786 return ep;
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
787 }
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
788
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
789 static int
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
790 insertion_resize(PyDictObject *mp)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
791 {
83812
cd2457463eeb Issue #17563: Fix dict resize performance regression.
Raymond Hettinger <python@rcn.com>
parents: 82476
diff changeset
792 return dictresize(mp, GROWTH_RATE(mp));
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
793 }
75302
90572ccda12c Issue #13521: dict.setdefault() now does only one lookup for the given key, making it "atomic" for many purposes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74044
diff changeset
794
90572ccda12c Issue #13521: dict.setdefault() now does only one lookup for the given key, making it "atomic" for many purposes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74044
diff changeset
795 /*
90572ccda12c Issue #13521: dict.setdefault() now does only one lookup for the given key, making it "atomic" for many purposes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74044
diff changeset
796 Internal routine to insert a new item into the table.
90572ccda12c Issue #13521: dict.setdefault() now does only one lookup for the given key, making it "atomic" for many purposes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74044
diff changeset
797 Used both by the internal resize routine and by the public insert routine.
90572ccda12c Issue #13521: dict.setdefault() now does only one lookup for the given key, making it "atomic" for many purposes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74044
diff changeset
798 Returns -1 if an error occurred, or 0 on success.
90572ccda12c Issue #13521: dict.setdefault() now does only one lookup for the given key, making it "atomic" for many purposes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74044
diff changeset
799 */
90572ccda12c Issue #13521: dict.setdefault() now does only one lookup for the given key, making it "atomic" for many purposes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74044
diff changeset
800 static int
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
801 insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value)
75302
90572ccda12c Issue #13521: dict.setdefault() now does only one lookup for the given key, making it "atomic" for many purposes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74044
diff changeset
802 {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
803 PyObject *old_value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
804 PyObject **value_addr;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
805 PyDictKeyEntry *ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
806 assert(key != dummy);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
807
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
808 if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
809 if (insertion_resize(mp) < 0)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
810 return -1;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
811 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
812
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
813 ep = mp->ma_keys->dk_lookup(mp, key, hash, &value_addr);
75302
90572ccda12c Issue #13521: dict.setdefault() now does only one lookup for the given key, making it "atomic" for many purposes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74044
diff changeset
814 if (ep == NULL) {
90572ccda12c Issue #13521: dict.setdefault() now does only one lookup for the given key, making it "atomic" for many purposes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74044
diff changeset
815 return -1;
90572ccda12c Issue #13521: dict.setdefault() now does only one lookup for the given key, making it "atomic" for many purposes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74044
diff changeset
816 }
76666
c5fd332e5857 change insertdict to not steal references (#13903)
Benjamin Peterson <benjamin@python.org>
parents: 76535
diff changeset
817 Py_INCREF(value);
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
818 MAINTAIN_TRACKING(mp, key, value);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
819 old_value = *value_addr;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
820 if (old_value != NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
821 assert(ep->me_key != NULL && ep->me_key != dummy);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
822 *value_addr = value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
823 Py_DECREF(old_value); /* which **CAN** re-enter */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
824 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
825 else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
826 if (ep->me_key == NULL) {
76666
c5fd332e5857 change insertdict to not steal references (#13903)
Benjamin Peterson <benjamin@python.org>
parents: 76535
diff changeset
827 Py_INCREF(key);
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
828 if (mp->ma_keys->dk_usable <= 0) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
829 /* Need to resize. */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
830 if (insertion_resize(mp) < 0) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
831 Py_DECREF(key);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
832 Py_DECREF(value);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
833 return -1;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
834 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
835 ep = find_empty_slot(mp, key, hash, &value_addr);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
836 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
837 mp->ma_keys->dk_usable--;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
838 assert(mp->ma_keys->dk_usable >= 0);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
839 ep->me_key = key;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
840 ep->me_hash = hash;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
841 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
842 else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
843 if (ep->me_key == dummy) {
76666
c5fd332e5857 change insertdict to not steal references (#13903)
Benjamin Peterson <benjamin@python.org>
parents: 76535
diff changeset
844 Py_INCREF(key);
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
845 ep->me_key = key;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
846 ep->me_hash = hash;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
847 Py_DECREF(dummy);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
848 } else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
849 assert(_PyDict_HasSplitTable(mp));
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
850 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
851 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
852 mp->ma_used++;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
853 *value_addr = value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
854 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
855 assert(ep->me_key != NULL && ep->me_key != dummy);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
856 assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
857 return 0;
75302
90572ccda12c Issue #13521: dict.setdefault() now does only one lookup for the given key, making it "atomic" for many purposes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74044
diff changeset
858 }
90572ccda12c Issue #13521: dict.setdefault() now does only one lookup for the given key, making it "atomic" for many purposes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74044
diff changeset
859
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
860 /*
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
861 Internal routine used by dictresize() to insert an item which is
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
862 known to be absent from the dict. This routine also assumes that
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
863 the dict contains no deleted entries. Besides the performance benefit,
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
864 using insertdict() in dictresize() is dangerous (SF bug #1456209).
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
865 Note that no refcounts are changed by this routine; if needed, the caller
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
866 is responsible for incref'ing `key` and `value`.
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
867 Neither mp->ma_used nor k->dk_usable are modified by this routine; the caller
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
868 must set them correctly
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
869 */
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
870 static void
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
871 insertdict_clean(PyDictObject *mp, PyObject *key, Py_hash_t hash,
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
872 PyObject *value)
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
873 {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
874 size_t i;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
875 size_t perturb;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
876 PyDictKeysObject *k = mp->ma_keys;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
877 size_t mask = (size_t)DK_SIZE(k)-1;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
878 PyDictKeyEntry *ep0 = &k->dk_entries[0];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
879 PyDictKeyEntry *ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
880
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
881 assert(k->dk_lookup != NULL);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
882 assert(value != NULL);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
883 assert(key != NULL);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
884 assert(key != dummy);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
885 assert(PyUnicode_CheckExact(key) || k->dk_lookup == lookdict);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
886 i = hash & mask;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
887 ep = &ep0[i];
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
888 for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
889 i = (i << 2) + i + perturb + 1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
890 ep = &ep0[i & mask];
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
891 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
892 assert(ep->me_value == NULL);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
893 ep->me_key = key;
65494
e8969fb822fc make hashes always the size of pointers; introduce Py_hash_t #9778
Benjamin Peterson <benjamin@python.org>
parents: 64377
diff changeset
894 ep->me_hash = hash;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
895 ep->me_value = value;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
896 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
897
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
898 /*
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
899 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
900 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
901 actually be smaller than the old one.
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
902 If a table is split (its keys and hashes are shared, its values are not),
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
903 then the values are temporarily copied into the table, it is resized as
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
904 a combined table, then the me_value slots in the old table are NULLed out.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
905 After resizing a table is always combined,
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
906 but can be resplit by make_keys_shared().
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
907 */
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
908 static int
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
909 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
910 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
911 Py_ssize_t newsize;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
912 PyDictKeysObject *oldkeys;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
913 PyObject **oldvalues;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
914 Py_ssize_t i, oldsize;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
915
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
916 /* Find the smallest table size > minused. */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
917 for (newsize = PyDict_MINSIZE_COMBINED;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
918 newsize <= minused && newsize > 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
919 newsize <<= 1)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
920 ;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
921 if (newsize <= 0) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
922 PyErr_NoMemory();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
923 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
924 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
925 oldkeys = mp->ma_keys;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
926 oldvalues = mp->ma_values;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
927 /* Allocate a new table. */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
928 mp->ma_keys = new_keys_object(newsize);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
929 if (mp->ma_keys == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
930 mp->ma_keys = oldkeys;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
931 return -1;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
932 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
933 if (oldkeys->dk_lookup == lookdict)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
934 mp->ma_keys->dk_lookup = lookdict;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
935 oldsize = DK_SIZE(oldkeys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
936 mp->ma_values = NULL;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
937 /* If empty then nothing to copy so just return */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
938 if (oldsize == 1) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
939 assert(oldkeys == Py_EMPTY_KEYS);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
940 DK_DECREF(oldkeys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
941 return 0;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
942 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
943 /* Main loop below assumes we can transfer refcount to new keys
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
944 * and that value is stored in me_value.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
945 * Increment ref-counts and copy values here to compensate
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
946 * This (resizing a split table) should be relatively rare */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
947 if (oldvalues != NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
948 for (i = 0; i < oldsize; i++) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
949 if (oldvalues[i] != NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
950 Py_INCREF(oldkeys->dk_entries[i].me_key);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
951 oldkeys->dk_entries[i].me_value = oldvalues[i];
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
952 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
953 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
954 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
955 /* Main loop */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
956 for (i = 0; i < oldsize; i++) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
957 PyDictKeyEntry *ep = &oldkeys->dk_entries[i];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
958 if (ep->me_value != NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
959 assert(ep->me_key != dummy);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
960 insertdict_clean(mp, ep->me_key, ep->me_hash, ep->me_value);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
961 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
962 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
963 mp->ma_keys->dk_usable -= mp->ma_used;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
964 if (oldvalues != NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
965 /* NULL out me_value slot in oldkeys, in case it was shared */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
966 for (i = 0; i < oldsize; i++)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
967 oldkeys->dk_entries[i].me_value = NULL;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
968 assert(oldvalues != empty_values);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
969 free_values(oldvalues);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
970 DK_DECREF(oldkeys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
971 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
972 else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
973 assert(oldkeys->dk_lookup != lookdict_split);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
974 if (oldkeys->dk_lookup != lookdict_unicode_nodummy) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
975 PyDictKeyEntry *ep0 = &oldkeys->dk_entries[0];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
976 for (i = 0; i < oldsize; i++) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
977 if (ep0[i].me_key == dummy)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
978 Py_DECREF(dummy);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
979 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
980 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
981 assert(oldkeys->dk_refcnt == 1);
76892
10e8b97d0fd7 Make the reference counting of dictkeys objects participate in refleak hunting
Antoine Pitrou <solipsis@pitrou.net>
parents: 76666
diff changeset
982 DK_DEBUG_DECREF PyMem_FREE(oldkeys);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
983 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
984 return 0;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
985 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
986
76535
5d5b72a71898 distiguish between refusing to creating shared keys and error (#13903)
Benjamin Peterson <benjamin@python.org>
parents: 76530
diff changeset
987 /* Returns NULL if unable to split table.
5d5b72a71898 distiguish between refusing to creating shared keys and error (#13903)
Benjamin Peterson <benjamin@python.org>
parents: 76530
diff changeset
988 * A NULL return does not necessarily indicate an error */
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
989 static PyDictKeysObject *
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
990 make_keys_shared(PyObject *op)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
991 {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
992 Py_ssize_t i;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
993 Py_ssize_t size;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
994 PyDictObject *mp = (PyDictObject *)op;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
995
76535
5d5b72a71898 distiguish between refusing to creating shared keys and error (#13903)
Benjamin Peterson <benjamin@python.org>
parents: 76530
diff changeset
996 if (!PyDict_CheckExact(op))
5d5b72a71898 distiguish between refusing to creating shared keys and error (#13903)
Benjamin Peterson <benjamin@python.org>
parents: 76530
diff changeset
997 return NULL;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
998 if (!_PyDict_HasSplitTable(mp)) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
999 PyDictKeyEntry *ep0;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1000 PyObject **values;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1001 assert(mp->ma_keys->dk_refcnt == 1);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1002 if (mp->ma_keys->dk_lookup == lookdict) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1003 return NULL;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1004 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1005 else if (mp->ma_keys->dk_lookup == lookdict_unicode) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1006 /* Remove dummy keys */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1007 if (dictresize(mp, DK_SIZE(mp->ma_keys)))
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1008 return NULL;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1009 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1010 assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1011 /* Copy values into a new array */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1012 ep0 = &mp->ma_keys->dk_entries[0];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1013 size = DK_SIZE(mp->ma_keys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1014 values = new_values(size);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1015 if (values == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1016 PyErr_SetString(PyExc_MemoryError,
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1017 "Not enough memory to allocate new values array");
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1018 return NULL;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1019 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1020 for (i = 0; i < size; i++) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1021 values[i] = ep0[i].me_value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1022 ep0[i].me_value = NULL;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1023 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1024 mp->ma_keys->dk_lookup = lookdict_split;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1025 mp->ma_values = values;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1026 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1027 DK_INCREF(mp->ma_keys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1028 return mp->ma_keys;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1029 }
44349
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1030
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1031 PyObject *
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1032 _PyDict_NewPresized(Py_ssize_t minused)
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1033 {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1034 Py_ssize_t newsize;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1035 PyDictKeysObject *new_keys;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1036 for (newsize = PyDict_MINSIZE_COMBINED;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1037 newsize <= minused && newsize > 0;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1038 newsize <<= 1)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1039 ;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1040 new_keys = new_keys_object(newsize);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1041 if (new_keys == NULL)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1042 return NULL;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1043 return new_dict(new_keys, NULL);
44349
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1044 }
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44252
diff changeset
1045
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1046 /* Note that, for historical reasons, PyDict_GetItem() suppresses all errors
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1047 * that may occur (originally dicts supported only string keys, and exceptions
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1048 * weren't possible). So, while the original intent was that a NULL return
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1049 * meant the key wasn't present, in reality it can mean that, or that an error
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1050 * (suppressed) occurred while computing the key's hash, or that some error
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1051 * (suppressed) occurred when comparing keys in the dict's internal probe
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1052 * sequence. A nasty example of the latter is when a Python-coded comparison
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1053 * function hits a stack-depth error, which can cause this to return NULL
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1054 * even if the key is present.
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1055 */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1056 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
1057 PyDict_GetItem(PyObject *op, PyObject *key)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1058 {
65494
e8969fb822fc make hashes always the size of pointers; introduce Py_hash_t #9778
Benjamin Peterson <benjamin@python.org>
parents: 64377
diff changeset
1059 Py_hash_t hash;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1060 PyDictObject *mp = (PyDictObject *)op;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1061 PyDictKeyEntry *ep;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1062 PyThreadState *tstate;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1063 PyObject **value_addr;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1064
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1065 if (!PyDict_Check(op))
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1066 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1067 if (!PyUnicode_CheckExact(key) ||
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72464
diff changeset
1068 (hash = ((PyASCIIObject *) key)->hash) == -1)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1069 {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1070 hash = PyObject_Hash(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1071 if (hash == -1) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1072 PyErr_Clear();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1073 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1074 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1075 }
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1076
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1077 /* We can arrive here with a NULL tstate during initialization: try
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1078 running "python -Wi" for an example related to string interning.
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1079 Let's just hope that no exception occurs then... This must be
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1080 _PyThreadState_Current and not PyThreadState_GET() because in debug
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1081 mode, the latter complains if tstate is NULL. */
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61184
diff changeset
1082 tstate = (PyThreadState*)_Py_atomic_load_relaxed(
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61184
diff changeset
1083 &_PyThreadState_Current);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1084 if (tstate != NULL && tstate->curexc_type != NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1085 /* preserve the existing exception */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1086 PyObject *err_type, *err_value, *err_tb;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1087 PyErr_Fetch(&err_type, &err_value, &err_tb);
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1088 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1089 /* ignore errors */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1090 PyErr_Restore(err_type, err_value, err_tb);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1091 if (ep == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1092 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1093 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1094 else {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1095 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1096 if (ep == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1097 PyErr_Clear();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1098 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1099 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1100 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1101 return *value_addr;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1102 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1103
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39575
diff changeset
1104 /* Variant of PyDict_GetItem() that doesn't suppress exceptions.
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39575
diff changeset
1105 This returns NULL *with* an exception set if an exception occurred.
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39575
diff changeset
1106 It returns NULL *without* an exception set if the key wasn't present.
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39575
diff changeset
1107 */
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39575
diff changeset
1108 PyObject *
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39575
diff changeset
1109 PyDict_GetItemWithError(PyObject *op, PyObject *key)
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39575
diff changeset
1110 {
65494
e8969fb822fc make hashes always the size of pointers; introduce Py_hash_t #9778
Benjamin Peterson <benjamin@python.org>
parents: 64377
diff changeset
1111 Py_hash_t hash;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1112 PyDictObject*mp = (PyDictObject *)op;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1113 PyDictKeyEntry *ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1114 PyObject **value_addr;
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39575
diff changeset
1115
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1116 if (!PyDict_Check(op)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1117 PyErr_BadInternalCall();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1118 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1119 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1120 if (!PyUnicode_CheckExact(key) ||
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72464
diff changeset
1121 (hash = ((PyASCIIObject *) key)->hash) == -1)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1122 {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1123 hash = PyObject_Hash(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1124 if (hash == -1) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1125 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1126 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1127 }
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39575
diff changeset
1128
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1129 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1130 if (ep == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1131 return NULL;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1132 return *value_addr;
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39575
diff changeset
1133 }
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39575
diff changeset
1134
76303
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 76105
diff changeset
1135 PyObject *
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 76105
diff changeset
1136 _PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key)
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 76105
diff changeset
1137 {
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 76105
diff changeset
1138 PyObject *kv;
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 76105
diff changeset
1139 kv = _PyUnicode_FromId(key); /* borrowed */
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 76105
diff changeset
1140 if (kv == NULL)
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 76105
diff changeset
1141 return NULL;
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 76105
diff changeset
1142 return PyDict_GetItemWithError(dp, kv);
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 76105
diff changeset
1143 }
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 76105
diff changeset
1144
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1145 /* Fast version of global value lookup.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1146 * Lookup in globals, then builtins.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1147 */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1148 PyObject *
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1149 _PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1150 {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1151 PyObject *x;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1152 if (PyUnicode_CheckExact(key)) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1153 PyObject **value_addr;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1154 Py_hash_t hash = ((PyASCIIObject *)key)->hash;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1155 if (hash != -1) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1156 PyDictKeyEntry *e;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1157 e = globals->ma_keys->dk_lookup(globals, key, hash, &value_addr);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1158 if (e == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1159 return NULL;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1160 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1161 x = *value_addr;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1162 if (x != NULL)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1163 return x;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1164 e = builtins->ma_keys->dk_lookup(builtins, key, hash, &value_addr);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1165 if (e == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1166 return NULL;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1167 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1168 x = *value_addr;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1169 return x;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1170 }
75302
90572ccda12c Issue #13521: dict.setdefault() now does only one lookup for the given key, making it "atomic" for many purposes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74044
diff changeset
1171 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1172 x = PyDict_GetItemWithError((PyObject *)globals, key);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1173 if (x != NULL)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1174 return x;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1175 if (PyErr_Occurred())
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1176 return NULL;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1177 return PyDict_GetItemWithError((PyObject *)builtins, key);
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
75302
90572ccda12c Issue #13521: dict.setdefault() now does only one lookup for the given key, making it "atomic" for many purposes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74044
diff changeset
1180 /* CAUTION: PyDict_SetItem() must guarantee that it won't resize the
90572ccda12c Issue #13521: dict.setdefault() now does only one lookup for the given key, making it "atomic" for many purposes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74044
diff changeset
1181 * dictionary if it's merely replacing the value for an existing key.
90572ccda12c Issue #13521: dict.setdefault() now does only one lookup for the given key, making it "atomic" for many purposes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74044
diff changeset
1182 * This means that it's safe to loop over a dictionary with PyDict_Next()
90572ccda12c Issue #13521: dict.setdefault() now does only one lookup for the given key, making it "atomic" for many purposes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74044
diff changeset
1183 * and occasionally replace a value -- but you can't insert new keys or
90572ccda12c Issue #13521: dict.setdefault() now does only one lookup for the given key, making it "atomic" for many purposes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74044
diff changeset
1184 * remove them.
90572ccda12c Issue #13521: dict.setdefault() now does only one lookup for the given key, making it "atomic" for many purposes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74044
diff changeset
1185 */
90572ccda12c Issue #13521: dict.setdefault() now does only one lookup for the given key, making it "atomic" for many purposes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74044
diff changeset
1186 int
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1187 PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value)
75302
90572ccda12c Issue #13521: dict.setdefault() now does only one lookup for the given key, making it "atomic" for many purposes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74044
diff changeset
1188 {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1189 PyDictObject *mp;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1190 Py_hash_t hash;
75302
90572ccda12c Issue #13521: dict.setdefault() now does only one lookup for the given key, making it "atomic" for many purposes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74044
diff changeset
1191 if (!PyDict_Check(op)) {
90572ccda12c Issue #13521: dict.setdefault() now does only one lookup for the given key, making it "atomic" for many purposes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74044
diff changeset
1192 PyErr_BadInternalCall();
90572ccda12c Issue #13521: dict.setdefault() now does only one lookup for the given key, making it "atomic" for many purposes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74044
diff changeset
1193 return -1;
90572ccda12c Issue #13521: dict.setdefault() now does only one lookup for the given key, making it "atomic" for many purposes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74044
diff changeset
1194 }
90572ccda12c Issue #13521: dict.setdefault() now does only one lookup for the given key, making it "atomic" for many purposes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74044
diff changeset
1195 assert(key);
90572ccda12c Issue #13521: dict.setdefault() now does only one lookup for the given key, making it "atomic" for many purposes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74044
diff changeset
1196 assert(value);
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1197 mp = (PyDictObject *)op;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1198 if (!PyUnicode_CheckExact(key) ||
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1199 (hash = ((PyASCIIObject *) key)->hash) == -1)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1200 {
75302
90572ccda12c Issue #13521: dict.setdefault() now does only one lookup for the given key, making it "atomic" for many purposes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74044
diff changeset
1201 hash = PyObject_Hash(key);
90572ccda12c Issue #13521: dict.setdefault() now does only one lookup for the given key, making it "atomic" for many purposes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74044
diff changeset
1202 if (hash == -1)
90572ccda12c Issue #13521: dict.setdefault() now does only one lookup for the given key, making it "atomic" for many purposes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74044
diff changeset
1203 return -1;
90572ccda12c Issue #13521: dict.setdefault() now does only one lookup for the given key, making it "atomic" for many purposes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74044
diff changeset
1204 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1205
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1206 /* insertdict() handles any resizing that might be necessary */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1207 return insertdict(mp, key, hash, value);
75302
90572ccda12c Issue #13521: dict.setdefault() now does only one lookup for the given key, making it "atomic" for many purposes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74044
diff changeset
1208 }
90572ccda12c Issue #13521: dict.setdefault() now does only one lookup for the given key, making it "atomic" for many purposes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 74044
diff changeset
1209
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1210 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
1211 PyDict_DelItem(PyObject *op, PyObject *key)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1212 {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1213 PyDictObject *mp;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1214 Py_hash_t hash;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1215 PyDictKeyEntry *ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1216 PyObject *old_key, *old_value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1217 PyObject **value_addr;
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
1218
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1219 if (!PyDict_Check(op)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1220 PyErr_BadInternalCall();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1221 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1222 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1223 assert(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1224 if (!PyUnicode_CheckExact(key) ||
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72464
diff changeset
1225 (hash = ((PyASCIIObject *) key)->hash) == -1) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1226 hash = PyObject_Hash(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1227 if (hash == -1)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1228 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1229 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1230 mp = (PyDictObject *)op;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1231 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1232 if (ep == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1233 return -1;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1234 if (*value_addr == NULL) {
85502
bfaf3a2907bd Factor-out the common code for setting a KeyError.
Raymond Hettinger <python@rcn.com>
parents: 85157
diff changeset
1235 _PyErr_SetKeyError(key);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1236 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1237 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1238 old_value = *value_addr;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1239 *value_addr = NULL;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1240 mp->ma_used--;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1241 if (!_PyDict_HasSplitTable(mp)) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1242 ENSURE_ALLOWS_DELETIONS(mp);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1243 old_key = ep->me_key;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1244 Py_INCREF(dummy);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1245 ep->me_key = dummy;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1246 Py_DECREF(old_key);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1247 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1248 Py_DECREF(old_value);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1249 return 0;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1250 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1251
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1252 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
1253 PyDict_Clear(PyObject *op)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1254 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1255 PyDictObject *mp;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1256 PyDictKeysObject *oldkeys;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1257 PyObject **oldvalues;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1258 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
1259
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1260 if (!PyDict_Check(op))
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1261 return;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1262 mp = ((PyDictObject *)op);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1263 oldkeys = mp->ma_keys;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1264 oldvalues = mp->ma_values;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1265 if (oldvalues == empty_values)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1266 return;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1267 /* Empty the dict... */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1268 DK_INCREF(Py_EMPTY_KEYS);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1269 mp->ma_keys = Py_EMPTY_KEYS;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1270 mp->ma_values = empty_values;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1271 mp->ma_used = 0;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1272 /* ...then clear the keys and values */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1273 if (oldvalues != NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1274 n = DK_SIZE(oldkeys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1275 for (i = 0; i < n; i++)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1276 Py_CLEAR(oldvalues[i]);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1277 free_values(oldvalues);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1278 DK_DECREF(oldkeys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1279 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1280 else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1281 assert(oldkeys->dk_refcnt == 1);
76892
10e8b97d0fd7 Make the reference counting of dictkeys objects participate in refleak hunting
Antoine Pitrou <solipsis@pitrou.net>
parents: 76666
diff changeset
1282 DK_DECREF(oldkeys);
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1283 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1284 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1285
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1286 /* Returns -1 if no more items (or op is not a dict),
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1287 * index of item otherwise. Stores value in pvalue
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1288 */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1289 Py_LOCAL_INLINE(Py_ssize_t)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1290 dict_next(PyObject *op, Py_ssize_t i, PyObject **pvalue)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1291 {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1292 Py_ssize_t mask, offset;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1293 PyDictObject *mp;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1294 PyObject **value_ptr;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1295
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1296
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1297 if (!PyDict_Check(op))
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1298 return -1;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1299 mp = (PyDictObject *)op;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1300 if (i < 0)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1301 return -1;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1302 if (mp->ma_values) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1303 value_ptr = &mp->ma_values[i];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1304 offset = sizeof(PyObject *);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1305 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1306 else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1307 value_ptr = &mp->ma_keys->dk_entries[i].me_value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1308 offset = sizeof(PyDictKeyEntry);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1309 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1310 mask = DK_MASK(mp->ma_keys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1311 while (i <= mask && *value_ptr == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1312 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1313 i++;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1314 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1315 if (i > mask)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1316 return -1;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1317 if (pvalue)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1318 *pvalue = *value_ptr;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1319 return i;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1320 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1321
27901
55adf08a213a cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents: 26818
diff changeset
1322 /*
55adf08a213a cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents: 26818
diff changeset
1323 * 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
1324 *
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1325 * 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
1326 * PyObject *key, *value;
55adf08a213a cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents: 26818
diff changeset
1327 * 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
1328 * 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
1329 * 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
1330 * }
55adf08a213a cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents: 26818
diff changeset
1331 *
55adf08a213a cPickle.c, load_build(): Taught cPickle how to pick apart
Tim Peters <tim.peters@gmail.com>
parents: 26818
diff changeset
1332 * 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
1333 * 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
1334 * 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
1335 * 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
1336 */
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1337 int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1338 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
1339 {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1340 PyDictObject *mp;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1341 Py_ssize_t i = dict_next(op, *ppos, pvalue);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1342 if (i < 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1343 return 0;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1344 mp = (PyDictObject *)op;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1345 *ppos = i+1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1346 if (pkey)
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1347 *pkey = mp->ma_keys->dk_entries[i].me_key;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1348 return 1;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1349 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1350
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1351 /* Internal version of PyDict_Next that returns a hash value in addition
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1352 * to the key and value.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1353 */
40893
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40823
diff changeset
1354 int
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1355 _PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey,
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1356 PyObject **pvalue, Py_hash_t *phash)
40893
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40823
diff changeset
1357 {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1358 PyDictObject *mp;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1359 Py_ssize_t i = dict_next(op, *ppos, pvalue);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1360 if (i < 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1361 return 0;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1362 mp = (PyDictObject *)op;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1363 *ppos = i+1;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1364 *phash = mp->ma_keys->dk_entries[i].me_hash;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1365 if (pkey)
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1366 *pkey = mp->ma_keys->dk_entries[i].me_key;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1367 return 1;
40893
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40823
diff changeset
1368 }
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40823
diff changeset
1369
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1370 /* Methods */
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1371
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1372 static void
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1373 dict_dealloc(PyDictObject *mp)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1374 {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1375 PyObject **values = mp->ma_values;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1376 PyDictKeysObject *keys = mp->ma_keys;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1377 Py_ssize_t i, n;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1378 PyObject_GC_UnTrack(mp);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1379 Py_TRASHCAN_SAFE_BEGIN(mp)
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1380 if (values != NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1381 if (values != empty_values) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1382 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1383 Py_XDECREF(values[i]);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1384 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1385 free_values(values);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1386 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1387 DK_DECREF(keys);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1388 }
84674
b7c6d203c9fb Issue #18408: dict_new() now fails on new_keys_object() error
Victor Stinner <victor.stinner@gmail.com>
parents: 84673
diff changeset
1389 else if (keys != NULL) {
76892
10e8b97d0fd7 Make the reference counting of dictkeys objects participate in refleak hunting
Antoine Pitrou <solipsis@pitrou.net>
parents: 76666
diff changeset
1390 assert(keys->dk_refcnt == 1);
10e8b97d0fd7 Make the reference counting of dictkeys objects participate in refleak hunting
Antoine Pitrou <solipsis@pitrou.net>
parents: 76666
diff changeset
1391 DK_DECREF(keys);
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1392 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1393 if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1394 free_list[numfree++] = mp;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1395 else
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1396 Py_TYPE(mp)->tp_free((PyObject *)mp);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1397 Py_TRASHCAN_SAFE_END(mp)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1398 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1399
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1400
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1401 static PyObject *
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
1402 dict_repr(PyDictObject *mp)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1403 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1404 Py_ssize_t i;
87265
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1405 PyObject *key = NULL, *value = NULL;
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1406 _PyUnicodeWriter writer;
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1407 int first;
7985
ab4f64561fba Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents: 6490
diff changeset
1408
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1409 i = Py_ReprEnter((PyObject *)mp);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1410 if (i != 0) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1411 return i > 0 ? PyUnicode_FromString("{...}") : NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1412 }
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1413
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1414 if (mp->ma_used == 0) {
87265
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1415 Py_ReprLeave((PyObject *)mp);
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1416 return PyUnicode_FromString("{}");
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1417 }
7985
ab4f64561fba Use Py_Repr{Enter,Leave} to display recursive dictionaries in finite space.
Guido van Rossum <guido@python.org>
parents: 6490
diff changeset
1418
87265
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1419 _PyUnicodeWriter_Init(&writer);
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1420 writer.overallocate = 1;
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1421 /* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1422 writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1423
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1424 if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1425 goto error;
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1426
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1427 /* Do repr() on each key+value pair, and insert ": " between them.
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1428 Note that repr may mutate the dict. */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1429 i = 0;
87265
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1430 first = 1;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1431 while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
87265
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1432 PyObject *s;
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1433 int res;
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1434
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1435 /* Prevent repr from deleting key or value during key format. */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1436 Py_INCREF(key);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1437 Py_INCREF(value);
87265
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1438
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1439 if (!first) {
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1440 if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1441 goto error;
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1442 }
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1443 first = 0;
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1444
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1445 s = PyObject_Repr(key);
84702
161de66cae49 Issue #18408: Fix dict_repr(), don't call PyObject_Repr() with an exception set
Victor Stinner <victor.stinner@gmail.com>
parents: 84674
diff changeset
1446 if (s == NULL)
87265
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1447 goto error;
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1448 res = _PyUnicodeWriter_WriteStr(&writer, s);
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1449 Py_DECREF(s);
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1450 if (res < 0)
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1451 goto error;
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1452
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1453 if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1454 goto error;
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1455
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1456 s = PyObject_Repr(value);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1457 if (s == NULL)
87265
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1458 goto error;
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1459 res = _PyUnicodeWriter_WriteStr(&writer, s);
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1460 Py_DECREF(s);
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1461 if (res < 0)
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1462 goto error;
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1463
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1464 Py_CLEAR(key);
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1465 Py_CLEAR(value);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1466 }
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 18032
diff changeset
1467
87265
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1468 writer.overallocate = 0;
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1469 if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1470 goto error;
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1471
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1472 Py_ReprLeave((PyObject *)mp);
87265
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1473
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1474 return _PyUnicodeWriter_Finish(&writer);
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1475
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1476 error:
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1477 Py_ReprLeave((PyObject *)mp);
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1478 _PyUnicodeWriter_Dealloc(&writer);
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1479 Py_XDECREF(key);
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1480 Py_XDECREF(value);
3a354b879d1f Issue #19646: repr(dict) now uses _PyUnicodeWriter API for better performances
Victor Stinner <victor.stinner@gmail.com>
parents: 87245
diff changeset
1481 return NULL;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1482 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1483
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1484 static Py_ssize_t
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
1485 dict_length(PyDictObject *mp)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1486 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1487 return mp->ma_used;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1488 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1489
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1490 static PyObject *
85157
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
1491 dict_subscript(PyDictObject *mp, PyObject *key)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1492 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1493 PyObject *v;
65494
e8969fb822fc make hashes always the size of pointers; introduce Py_hash_t #9778
Benjamin Peterson <benjamin@python.org>
parents: 64377
diff changeset
1494 Py_hash_t hash;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1495 PyDictKeyEntry *ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1496 PyObject **value_addr;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1497
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1498 if (!PyUnicode_CheckExact(key) ||
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72464
diff changeset
1499 (hash = ((PyASCIIObject *) key)->hash) == -1) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1500 hash = PyObject_Hash(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1501 if (hash == -1)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1502 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1503 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1504 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1505 if (ep == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1506 return NULL;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1507 v = *value_addr;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1508 if (v == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1509 if (!PyDict_CheckExact(mp)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1510 /* Look up __missing__ method if we're a subclass. */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1511 PyObject *missing, *res;
74578
7d96679c5d46 use the static identifier api for looking up special methods
Benjamin Peterson <benjamin@python.org>
parents: 74045
diff changeset
1512 _Py_IDENTIFIER(__missing__);
7d96679c5d46 use the static identifier api for looking up special methods
Benjamin Peterson <benjamin@python.org>
parents: 74045
diff changeset
1513 missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1514 if (missing != NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1515 res = PyObject_CallFunctionObjArgs(missing,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1516 key, NULL);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1517 Py_DECREF(missing);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1518 return res;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1519 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1520 else if (PyErr_Occurred())
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1521 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1522 }
85502
bfaf3a2907bd Factor-out the common code for setting a KeyError.
Raymond Hettinger <python@rcn.com>
parents: 85157
diff changeset
1523 _PyErr_SetKeyError(key);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1524 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1525 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1526 else
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1527 Py_INCREF(v);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1528 return v;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1529 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1530
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1531 static int
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
1532 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
1533 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1534 if (w == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1535 return PyDict_DelItem((PyObject *)mp, v);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1536 else
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1537 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
1538 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1539
5505
47fa00877924 Renamed from mappingobject.c to dictobject.c.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1540 static PyMappingMethods dict_as_mapping = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1541 (lenfunc)dict_length, /*mp_length*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1542 (binaryfunc)dict_subscript, /*mp_subscript*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1543 (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
1544 };
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1545
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1546 static PyObject *
85157
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
1547 dict_keys(PyDictObject *mp)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1548 {
85157
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
1549 PyObject *v;
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
1550 Py_ssize_t i, j;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1551 PyDictKeyEntry *ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1552 Py_ssize_t size, n, offset;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1553 PyObject **value_ptr;
17535
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1554
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1555 again:
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1556 n = mp->ma_used;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1557 v = PyList_New(n);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1558 if (v == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1559 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1560 if (n != mp->ma_used) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1561 /* Durnit. The allocations caused the dict to resize.
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1562 * Just start over, this shouldn't normally happen.
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1563 */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1564 Py_DECREF(v);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1565 goto again;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1566 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1567 ep = &mp->ma_keys->dk_entries[0];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1568 size = DK_SIZE(mp->ma_keys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1569 if (mp->ma_values) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1570 value_ptr = mp->ma_values;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1571 offset = sizeof(PyObject *);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1572 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1573 else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1574 value_ptr = &ep[0].me_value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1575 offset = sizeof(PyDictKeyEntry);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1576 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1577 for (i = 0, j = 0; i < size; i++) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1578 if (*value_ptr != NULL) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1579 PyObject *key = ep[i].me_key;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1580 Py_INCREF(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1581 PyList_SET_ITEM(v, j, key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1582 j++;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1583 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1584 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1585 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1586 assert(j == n);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1587 return v;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1588 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1589
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1590 static PyObject *
85157
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
1591 dict_values(PyDictObject *mp)
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1592 {
85157
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
1593 PyObject *v;
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
1594 Py_ssize_t i, j;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1595 Py_ssize_t size, n, offset;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1596 PyObject **value_ptr;
17535
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1597
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1598 again:
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1599 n = mp->ma_used;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1600 v = PyList_New(n);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1601 if (v == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1602 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1603 if (n != mp->ma_used) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1604 /* Durnit. The allocations caused the dict to resize.
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1605 * Just start over, this shouldn't normally happen.
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1606 */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1607 Py_DECREF(v);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1608 goto again;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1609 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1610 size = DK_SIZE(mp->ma_keys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1611 if (mp->ma_values) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1612 value_ptr = mp->ma_values;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1613 offset = sizeof(PyObject *);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1614 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1615 else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1616 value_ptr = &mp->ma_keys->dk_entries[0].me_value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1617 offset = sizeof(PyDictKeyEntry);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1618 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1619 for (i = 0, j = 0; i < size; i++) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1620 PyObject *value = *value_ptr;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1621 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1622 if (value != NULL) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1623 Py_INCREF(value);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1624 PyList_SET_ITEM(v, j, value);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1625 j++;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1626 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1627 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1628 assert(j == n);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1629 return v;
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1630 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1631
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
1632 static PyObject *
85157
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
1633 dict_items(PyDictObject *mp)
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1634 {
85157
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
1635 PyObject *v;
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
1636 Py_ssize_t i, j, n;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1637 Py_ssize_t size, offset;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1638 PyObject *item, *key;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1639 PyDictKeyEntry *ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1640 PyObject **value_ptr;
17535
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1641
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1642 /* Preallocate the list of tuples, to avoid allocations during
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1643 * the loop over the items, which could trigger GC, which
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1644 * could resize the dict. :-(
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1645 */
17535
83c7b4b75668 Tentative fix for a problem that Tim discovered at the last moment,
Guido van Rossum <guido@python.org>
parents: 17115
diff changeset
1646 again:
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1647 n = mp->ma_used;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1648 v = PyList_New(n);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1649 if (v == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1650 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1651 for (i = 0; i < n; i++) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1652 item = PyTuple_New(2);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1653 if (item == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1654 Py_DECREF(v);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1655 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1656 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1657 PyList_SET_ITEM(v, i, item);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1658 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1659 if (n != mp->ma_used) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1660 /* Durnit. The allocations caused the dict to resize.
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1661 * Just start over, this shouldn't normally happen.
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1662 */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1663 Py_DECREF(v);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1664 goto again;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1665 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1666 /* Nothing we do below makes any function calls. */
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1667 ep = mp->ma_keys->dk_entries;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1668 size = DK_SIZE(mp->ma_keys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1669 if (mp->ma_values) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1670 value_ptr = mp->ma_values;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1671 offset = sizeof(PyObject *);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1672 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1673 else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1674 value_ptr = &ep[0].me_value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1675 offset = sizeof(PyDictKeyEntry);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1676 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1677 for (i = 0, j = 0; i < size; i++) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1678 PyObject *value = *value_ptr;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1679 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1680 if (value != NULL) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1681 key = ep[i].me_key;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1682 item = PyList_GET_ITEM(v, j);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1683 Py_INCREF(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1684 PyTuple_SET_ITEM(item, 0, key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1685 Py_INCREF(value);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1686 PyTuple_SET_ITEM(item, 1, value);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1687 j++;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1688 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1689 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1690 assert(j == n);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1691 return v;
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1692 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1693
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
1694 static PyObject *
26583
8991f78817c7 SF 548651: Fix the METH_CLASS implementation.
Tim Peters <tim.peters@gmail.com>
parents: 26562
diff changeset
1695 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
1696 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1697 PyObject *seq;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1698 PyObject *value = Py_None;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1699 PyObject *it; /* iter(seq) */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1700 PyObject *key;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1701 PyObject *d;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1702 int status;
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1703
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1704 if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1705 return NULL;
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1706
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1707 d = PyObject_CallObject(cls, NULL);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1708 if (d == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1709 return NULL;
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1710
80119
e9da658cdddb avoid a function call with redundant checks for dict size
Benjamin Peterson <benjamin@python.org>
parents: 80109
diff changeset
1711 if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
80109
a965eac352ec only fast-path fromkeys() when the constructor returns a empty dict (closes #16345)
Benjamin Peterson <benjamin@python.org>
parents: 75302
diff changeset
1712 if (PyDict_CheckExact(seq)) {
a965eac352ec only fast-path fromkeys() when the constructor returns a empty dict (closes #16345)
Benjamin Peterson <benjamin@python.org>
parents: 75302
diff changeset
1713 PyDictObject *mp = (PyDictObject *)d;
a965eac352ec only fast-path fromkeys() when the constructor returns a empty dict (closes #16345)
Benjamin Peterson <benjamin@python.org>
parents: 75302
diff changeset
1714 PyObject *oldvalue;
a965eac352ec only fast-path fromkeys() when the constructor returns a empty dict (closes #16345)
Benjamin Peterson <benjamin@python.org>
parents: 75302
diff changeset
1715 Py_ssize_t pos = 0;
a965eac352ec only fast-path fromkeys() when the constructor returns a empty dict (closes #16345)
Benjamin Peterson <benjamin@python.org>
parents: 75302
diff changeset
1716 PyObject *key;
a965eac352ec only fast-path fromkeys() when the constructor returns a empty dict (closes #16345)
Benjamin Peterson <benjamin@python.org>
parents: 75302
diff changeset
1717 Py_hash_t hash;
43815
85cf334b32c3 Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
1718
80109
a965eac352ec only fast-path fromkeys() when the constructor returns a empty dict (closes #16345)
Benjamin Peterson <benjamin@python.org>
parents: 75302
diff changeset
1719 if (dictresize(mp, Py_SIZE(seq))) {
73103
df24a8b57148 Issue #13018: Fix reference leaks in error paths in dictobject.c.
Petri Lehtinen <petri@digip.org>
parents: 68586
diff changeset
1720 Py_DECREF(d);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1721 return NULL;
73103
df24a8b57148 Issue #13018: Fix reference leaks in error paths in dictobject.c.
Petri Lehtinen <petri@digip.org>
parents: 68586
diff changeset
1722 }
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40962
diff changeset
1723
80109
a965eac352ec only fast-path fromkeys() when the constructor returns a empty dict (closes #16345)
Benjamin Peterson <benjamin@python.org>
parents: 75302
diff changeset
1724 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
a965eac352ec only fast-path fromkeys() when the constructor returns a empty dict (closes #16345)
Benjamin Peterson <benjamin@python.org>
parents: 75302
diff changeset
1725 if (insertdict(mp, key, hash, value)) {
a965eac352ec only fast-path fromkeys() when the constructor returns a empty dict (closes #16345)
Benjamin Peterson <benjamin@python.org>
parents: 75302
diff changeset
1726 Py_DECREF(d);
a965eac352ec only fast-path fromkeys() when the constructor returns a empty dict (closes #16345)
Benjamin Peterson <benjamin@python.org>
parents: 75302
diff changeset
1727 return NULL;
a965eac352ec only fast-path fromkeys() when the constructor returns a empty dict (closes #16345)
Benjamin Peterson <benjamin@python.org>
parents: 75302
diff changeset
1728 }
a965eac352ec only fast-path fromkeys() when the constructor returns a empty dict (closes #16345)
Benjamin Peterson <benjamin@python.org>
parents: 75302
diff changeset
1729 }
a965eac352ec only fast-path fromkeys() when the constructor returns a empty dict (closes #16345)
Benjamin Peterson <benjamin@python.org>
parents: 75302
diff changeset
1730 return d;
73103
df24a8b57148 Issue #13018: Fix reference leaks in error paths in dictobject.c.
Petri Lehtinen <petri@digip.org>
parents: 68586
diff changeset
1731 }
80109
a965eac352ec only fast-path fromkeys() when the constructor returns a empty dict (closes #16345)
Benjamin Peterson <benjamin@python.org>
parents: 75302
diff changeset
1732 if (PyAnySet_CheckExact(seq)) {
a965eac352ec only fast-path fromkeys() when the constructor returns a empty dict (closes #16345)
Benjamin Peterson <benjamin@python.org>
parents: 75302
diff changeset
1733 PyDictObject *mp = (PyDictObject *)d;
a965eac352ec only fast-path fromkeys() when the constructor returns a empty dict (closes #16345)
Benjamin Peterson <benjamin@python.org>
parents: 75302
diff changeset
1734 Py_ssize_t pos = 0;
a965eac352ec only fast-path fromkeys() when the constructor returns a empty dict (closes #16345)
Benjamin Peterson <benjamin@python.org>
parents: 75302
diff changeset
1735 PyObject *key;
a965eac352ec only fast-path fromkeys() when the constructor returns a empty dict (closes #16345)
Benjamin Peterson <benjamin@python.org>
parents: 75302
diff changeset
1736 Py_hash_t hash;
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40962
diff changeset
1737
80109
a965eac352ec only fast-path fromkeys() when the constructor returns a empty dict (closes #16345)
Benjamin Peterson <benjamin@python.org>
parents: 75302
diff changeset
1738 if (dictresize(mp, PySet_GET_SIZE(seq))) {
73103
df24a8b57148 Issue #13018: Fix reference leaks in error paths in dictobject.c.
Petri Lehtinen <petri@digip.org>
parents: 68586
diff changeset
1739 Py_DECREF(d);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1740 return NULL;
73103
df24a8b57148 Issue #13018: Fix reference leaks in error paths in dictobject.c.
Petri Lehtinen <petri@digip.org>
parents: 68586
diff changeset
1741 }
80109
a965eac352ec only fast-path fromkeys() when the constructor returns a empty dict (closes #16345)
Benjamin Peterson <benjamin@python.org>
parents: 75302
diff changeset
1742
a965eac352ec only fast-path fromkeys() when the constructor returns a empty dict (closes #16345)
Benjamin Peterson <benjamin@python.org>
parents: 75302
diff changeset
1743 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
a965eac352ec only fast-path fromkeys() when the constructor returns a empty dict (closes #16345)
Benjamin Peterson <benjamin@python.org>
parents: 75302
diff changeset
1744 if (insertdict(mp, key, hash, value)) {
a965eac352ec only fast-path fromkeys() when the constructor returns a empty dict (closes #16345)
Benjamin Peterson <benjamin@python.org>
parents: 75302
diff changeset
1745 Py_DECREF(d);
a965eac352ec only fast-path fromkeys() when the constructor returns a empty dict (closes #16345)
Benjamin Peterson <benjamin@python.org>
parents: 75302
diff changeset
1746 return NULL;
a965eac352ec only fast-path fromkeys() when the constructor returns a empty dict (closes #16345)
Benjamin Peterson <benjamin@python.org>
parents: 75302
diff changeset
1747 }
a965eac352ec only fast-path fromkeys() when the constructor returns a empty dict (closes #16345)
Benjamin Peterson <benjamin@python.org>
parents: 75302
diff changeset
1748 }
a965eac352ec only fast-path fromkeys() when the constructor returns a empty dict (closes #16345)
Benjamin Peterson <benjamin@python.org>
parents: 75302
diff changeset
1749 return d;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1750 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1751 }
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40962
diff changeset
1752
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1753 it = PyObject_GetIter(seq);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1754 if (it == NULL){
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1755 Py_DECREF(d);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1756 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1757 }
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1758
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1759 if (PyDict_CheckExact(d)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1760 while ((key = PyIter_Next(it)) != NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1761 status = PyDict_SetItem(d, key, value);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1762 Py_DECREF(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1763 if (status < 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1764 goto Fail;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1765 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1766 } else {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1767 while ((key = PyIter_Next(it)) != NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1768 status = PyObject_SetItem(d, key, value);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1769 Py_DECREF(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1770 if (status < 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1771 goto Fail;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1772 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1773 }
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1774
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1775 if (PyErr_Occurred())
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1776 goto Fail;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1777 Py_DECREF(it);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1778 return d;
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1779
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1780 Fail:
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1781 Py_DECREF(it);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1782 Py_DECREF(d);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1783 return NULL;
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1784 }
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1785
31616
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1786 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
1787 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
1788 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1789 PyObject *arg = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1790 int result = 0;
31616
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1791
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1792 if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1793 result = -1;
31616
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1794
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1795 else if (arg != NULL) {
72926
db4aa878cea2 Port SetAttrString/HasAttrString to SetAttrId/GetAttrId.
Martin v. Löwis <martin@v.loewis.de>
parents: 72925
diff changeset
1796 _Py_IDENTIFIER(keys);
db4aa878cea2 Port SetAttrString/HasAttrString to SetAttrId/GetAttrId.
Martin v. Löwis <martin@v.loewis.de>
parents: 72925
diff changeset
1797 if (_PyObject_HasAttrId(arg, &PyId_keys))
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1798 result = PyDict_Merge(self, arg, 1);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1799 else
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1800 result = PyDict_MergeFromSeq2(self, arg, 1);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1801 }
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61184
diff changeset
1802 if (result == 0 && kwds != NULL) {
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61184
diff changeset
1803 if (PyArg_ValidateKeywordArguments(kwds))
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61184
diff changeset
1804 result = PyDict_Merge(self, kwds, 1);
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61184
diff changeset
1805 else
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61184
diff changeset
1806 result = -1;
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 61184
diff changeset
1807 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1808 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
1809 }
5b3898d9ba59 SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
Raymond Hettinger <python@rcn.com>
parents: 31545
diff changeset
1810
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
1811 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
1812 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
1813 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1814 if (dict_update_common(self, args, kwds, "update") != -1)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1815 Py_RETURN_NONE;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1816 return NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1817 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1818
19150
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1819 /* Update unconditionally replaces existing items.
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1820 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
1821 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
1822
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1823 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
1824
21348
12310d643d8d SF bug #491415 PyDict_UpdateFromSeq2() unused
Tim Peters <tim.peters@gmail.com>
parents: 21155
diff changeset
1825 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
1826 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
1827 */
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1828
21348
12310d643d8d SF bug #491415 PyDict_UpdateFromSeq2() unused
Tim Peters <tim.peters@gmail.com>
parents: 21155
diff changeset
1829 int
20683
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1830 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
1831 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1832 PyObject *it; /* iter(seq2) */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1833 Py_ssize_t i; /* index into seq2 of current element */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1834 PyObject *item; /* seq2[i] */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1835 PyObject *fast; /* item as a 2-tuple or 2-list */
20683
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1836
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1837 assert(d != NULL);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1838 assert(PyDict_Check(d));
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1839 assert(seq2 != NULL);
20683
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1840
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1841 it = PyObject_GetIter(seq2);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1842 if (it == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1843 return -1;
20683
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1844
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1845 for (i = 0; ; ++i) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1846 PyObject *key, *value;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1847 Py_ssize_t n;
20683
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1848
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1849 fast = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1850 item = PyIter_Next(it);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1851 if (item == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1852 if (PyErr_Occurred())
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1853 goto Fail;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1854 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1855 }
20683
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1856
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1857 /* Convert item to sequence, and verify length 2. */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1858 fast = PySequence_Fast(item, "");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1859 if (fast == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1860 if (PyErr_ExceptionMatches(PyExc_TypeError))
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1861 PyErr_Format(PyExc_TypeError,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1862 "cannot convert dictionary update "
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1863 "sequence element #%zd to a sequence",
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1864 i);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1865 goto Fail;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1866 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1867 n = PySequence_Fast_GET_SIZE(fast);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1868 if (n != 2) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1869 PyErr_Format(PyExc_ValueError,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1870 "dictionary update sequence element #%zd "
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1871 "has length %zd; 2 is required",
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1872 i, n);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1873 goto Fail;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1874 }
20683
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1875
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1876 /* Update/merge with this (key, value) pair. */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1877 key = PySequence_Fast_GET_ITEM(fast, 0);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1878 value = PySequence_Fast_GET_ITEM(fast, 1);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1879 if (override || PyDict_GetItem(d, key) == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1880 int status = PyDict_SetItem(d, key, value);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1881 if (status < 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1882 goto Fail;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1883 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1884 Py_DECREF(fast);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1885 Py_DECREF(item);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1886 }
20683
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1887
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1888 i = 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1889 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
1890 Fail:
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1891 Py_XDECREF(item);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1892 Py_XDECREF(fast);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1893 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
1894 Return:
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1895 Py_DECREF(it);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1896 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
1897 }
505dd34e0eeb Generalize dictionary() to accept a sequence of 2-sequences. At the
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1898
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1899 int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1900 PyDict_Update(PyObject *a, PyObject *b)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1901 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1902 return PyDict_Merge(a, b, 1);
19150
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1903 }
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1904
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1905 int
3f34f724f014 Add PyDict_Merge(a, b, override):
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1906 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
1907 {
85157
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
1908 PyDictObject *mp, *other;
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
1909 Py_ssize_t i, n;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1910 PyDictKeyEntry *entry;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
1911
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1912 /* We accept for the argument either a concrete dictionary object,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1913 * or an abstract "mapping" object. For the former, we can do
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1914 * things quite efficiently. For the latter, we only require that
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1915 * PyMapping_Keys() and PyObject_GetItem() be supported.
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1916 */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1917 if (a == NULL || !PyDict_Check(a) || b == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1918 PyErr_BadInternalCall();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1919 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1920 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1921 mp = (PyDictObject*)a;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1922 if (PyDict_Check(b)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1923 other = (PyDictObject*)b;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1924 if (other == mp || other->ma_used == 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1925 /* a.update(a) or a.update({}); nothing to do */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1926 return 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1927 if (mp->ma_used == 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1928 /* Since the target dict is empty, PyDict_GetItem()
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1929 * always returns NULL. Setting override to 1
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1930 * skips the unnecessary test.
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1931 */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1932 override = 1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1933 /* Do one big resize at the start, rather than
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1934 * incrementally resizing as we insert new items. Expect
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1935 * that there will be no (or few) overlapping keys.
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1936 */
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1937 if (mp->ma_keys->dk_usable * 3 < other->ma_used * 2)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1938 if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1939 return -1;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1940 for (i = 0, n = DK_SIZE(other->ma_keys); i < n; i++) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1941 PyObject *value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1942 entry = &other->ma_keys->dk_entries[i];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1943 if (other->ma_values)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1944 value = other->ma_values[i];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1945 else
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1946 value = entry->me_value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1947
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1948 if (value != NULL &&
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1949 (override ||
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1950 PyDict_GetItem(a, entry->me_key) == NULL)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1951 if (insertdict(mp, entry->me_key,
65494
e8969fb822fc make hashes always the size of pointers; introduce Py_hash_t #9778
Benjamin Peterson <benjamin@python.org>
parents: 64377
diff changeset
1952 entry->me_hash,
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
1953 value) != 0)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1954 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1955 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1956 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1957 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1958 else {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1959 /* Do it the generic, slower way */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1960 PyObject *keys = PyMapping_Keys(b);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1961 PyObject *iter;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1962 PyObject *key, *value;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1963 int status;
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1964
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1965 if (keys == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1966 /* Docstring says this is equivalent to E.keys() so
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1967 * if E doesn't have a .keys() method we want
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1968 * AttributeError to percolate up. Might as well
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1969 * do the same for any other error.
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1970 */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1971 return -1;
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1972
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1973 iter = PyObject_GetIter(keys);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1974 Py_DECREF(keys);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1975 if (iter == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1976 return -1;
18283
1f1d2c50b3c7 dict_update(): Generalize this method so {}.update() accepts any
Barry Warsaw <barry@python.org>
parents: 18131
diff changeset
1977
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1978 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1979 if (!override && PyDict_GetItem(a, key) != NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1980 Py_DECREF(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1981 continue;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1982 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1983 value = PyObject_GetItem(b, key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1984 if (value == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1985 Py_DECREF(iter);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1986 Py_DECREF(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1987 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1988 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1989 status = PyDict_SetItem(a, key, value);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1990 Py_DECREF(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1991 Py_DECREF(value);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1992 if (status < 0) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1993 Py_DECREF(iter);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1994 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1995 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1996 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1997 Py_DECREF(iter);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1998 if (PyErr_Occurred())
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
1999 /* Iterator completed, via error */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2000 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2001 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2002 return 0;
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
2003 }
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
2004
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
2005 static PyObject *
85157
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
2006 dict_copy(PyDictObject *mp)
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
2007 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2008 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
2009 }
e6214ad2a60c Add PyDict_Copy() function to C API for dicts. It returns a new
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
2010
e6214ad2a60c Add PyDict_Copy() function to C API for dicts. It returns a new
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
2011 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
2012 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
2013 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2014 PyObject *copy;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2015 PyDictObject *mp;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2016 Py_ssize_t i, n;
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
2017
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2018 if (o == NULL || !PyDict_Check(o)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2019 PyErr_BadInternalCall();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2020 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2021 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2022 mp = (PyDictObject *)o;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2023 if (_PyDict_HasSplitTable(mp)) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2024 PyDictObject *split_copy;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2025 PyObject **newvalues = new_values(DK_SIZE(mp->ma_keys));
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2026 if (newvalues == NULL)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2027 return PyErr_NoMemory();
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2028 split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2029 if (split_copy == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2030 free_values(newvalues);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2031 return NULL;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2032 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2033 split_copy->ma_values = newvalues;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2034 split_copy->ma_keys = mp->ma_keys;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2035 split_copy->ma_used = mp->ma_used;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2036 DK_INCREF(mp->ma_keys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2037 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2038 PyObject *value = mp->ma_values[i];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2039 Py_XINCREF(value);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2040 split_copy->ma_values[i] = value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2041 }
76517
507a6703d6a3 fix dict gc tracking (#13903)
Benjamin Peterson <benjamin@python.org>
parents: 76503
diff changeset
2042 if (_PyObject_GC_IS_TRACKED(mp))
507a6703d6a3 fix dict gc tracking (#13903)
Benjamin Peterson <benjamin@python.org>
parents: 76503
diff changeset
2043 _PyObject_GC_TRACK(split_copy);
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2044 return (PyObject *)split_copy;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2045 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2046 copy = PyDict_New();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2047 if (copy == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2048 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2049 if (PyDict_Merge(copy, o, 1) == 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2050 return copy;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2051 Py_DECREF(copy);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2052 return NULL;
5658
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
2053 }
1265ca44f04b Added dict.absorb() and dict.copy().
Guido van Rossum <guido@python.org>
parents: 5609
diff changeset
2054
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2055 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
2056 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
2057 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2058 if (mp == NULL || !PyDict_Check(mp)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2059 PyErr_BadInternalCall();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2060 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2061 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2062 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
2063 }
1d841587aede Added getmappingsize(). (Needed by previous checkin of posixmodule.c)
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
2064
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
2065 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
2066 PyDict_Keys(PyObject *mp)
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2067 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2068 if (mp == NULL || !PyDict_Check(mp)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2069 PyErr_BadInternalCall();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2070 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2071 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2072 return dict_keys((PyDictObject *)mp);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2073 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2074
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
2075 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
2076 PyDict_Values(PyObject *mp)
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
2077 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2078 if (mp == NULL || !PyDict_Check(mp)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2079 PyErr_BadInternalCall();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2080 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2081 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2082 return dict_values((PyDictObject *)mp);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
2083 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
2084
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
2085 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
2086 PyDict_Items(PyObject *mp)
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
2087 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2088 if (mp == NULL || !PyDict_Check(mp)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2089 PyErr_BadInternalCall();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2090 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2091 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2092 return dict_items((PyDictObject *)mp);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
2093 }
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
2094
17704
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
2095 /* 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
2096 * 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
2097 * Uses only Py_EQ comparison.
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
2098 */
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
2099 static int
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
2100 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
2101 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2102 Py_ssize_t i;
17704
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
2103
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2104 if (a->ma_used != b->ma_used)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2105 /* can't be equal if # of entries differ */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2106 return 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2107 /* Same # of entries -- check all of 'em. Exit early on any diff. */
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2108 for (i = 0; i < DK_SIZE(a->ma_keys); i++) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2109 PyDictKeyEntry *ep = &a->ma_keys->dk_entries[i];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2110 PyObject *aval;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2111 if (a->ma_values)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2112 aval = a->ma_values[i];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2113 else
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2114 aval = ep->me_value;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2115 if (aval != NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2116 int cmp;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2117 PyObject *bval;
80701
d12785ecca72 Issue #16562: Optimize dict equality testing.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80120
diff changeset
2118 PyObject **vaddr;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2119 PyObject *key = ep->me_key;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2120 /* temporarily bump aval's refcount to ensure it stays
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2121 alive until we're done with it */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2122 Py_INCREF(aval);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2123 /* ditto for key */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2124 Py_INCREF(key);
80701
d12785ecca72 Issue #16562: Optimize dict equality testing.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80120
diff changeset
2125 /* reuse the known hash value */
d12785ecca72 Issue #16562: Optimize dict equality testing.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80120
diff changeset
2126 if ((b->ma_keys->dk_lookup)(b, key, ep->me_hash, &vaddr) == NULL)
d12785ecca72 Issue #16562: Optimize dict equality testing.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80120
diff changeset
2127 bval = NULL;
d12785ecca72 Issue #16562: Optimize dict equality testing.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80120
diff changeset
2128 else
d12785ecca72 Issue #16562: Optimize dict equality testing.
Antoine Pitrou <solipsis@pitrou.net>
parents: 80120
diff changeset
2129 bval = *vaddr;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2130 Py_DECREF(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2131 if (bval == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2132 Py_DECREF(aval);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2133 if (PyErr_Occurred())
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2134 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2135 return 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2136 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2137 cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2138 Py_DECREF(aval);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2139 if (cmp <= 0) /* error or not equal */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2140 return cmp;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2141 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2142 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2143 return 1;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2144 }
17704
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
2145
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
2146 static PyObject *
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
2147 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
2148 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2149 int cmp;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2150 PyObject *res;
17704
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
2151
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2152 if (!PyDict_Check(v) || !PyDict_Check(w)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2153 res = Py_NotImplemented;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2154 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2155 else if (op == Py_EQ || op == Py_NE) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2156 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2157 if (cmp < 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2158 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2159 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2160 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2161 else
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2162 res = Py_NotImplemented;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2163 Py_INCREF(res);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2164 return res;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2165 }
17704
b41f6ef6ea8f SF patch #421922: Implement rich comparison for dicts.
Tim Peters <tim.peters@gmail.com>
parents: 17656
diff changeset
2166
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 85502
diff changeset
2167 /*[clinic]
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 85502
diff changeset
2168
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 85502
diff changeset
2169 @coexist
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 85502
diff changeset
2170 dict.__contains__
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 85502
diff changeset
2171
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 85502
diff changeset
2172 key: object
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 85502
diff changeset
2173 /
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 85502
diff changeset
2174
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 85502
diff changeset
2175 True if D has a key k, else False"
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 85502
diff changeset
2176 [clinic]*/
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 85502
diff changeset
2177
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 85502
diff changeset
2178 PyDoc_STRVAR(dict___contains____doc__,
87470
78ec18f5cb45 Issue #19674: inspect.signature() now produces a correct signature
Larry Hastings <larry@hastings.org>
parents: 87265
diff changeset
2179 "__contains__(key)\n"
78ec18f5cb45 Issue #19674: inspect.signature() now produces a correct signature
Larry Hastings <larry@hastings.org>
parents: 87265
diff changeset
2180 "True if D has a key k, else False\"");
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 85502
diff changeset
2181
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 85502
diff changeset
2182 #define DICT___CONTAINS___METHODDEF \
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 85502
diff changeset
2183 {"__contains__", (PyCFunction)dict___contains__, METH_O|METH_COEXIST, dict___contains____doc__},
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 85502
diff changeset
2184
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
2185 static PyObject *
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 85502
diff changeset
2186 dict___contains__(PyObject *self, PyObject *key)
87470
78ec18f5cb45 Issue #19674: inspect.signature() now produces a correct signature
Larry Hastings <larry@hastings.org>
parents: 87265
diff changeset
2187 /*[clinic checksum: 3bbac5ce898ae630d9668fa1c8b3afb645ff22e8]*/
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2188 {
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 85502
diff changeset
2189 register PyDictObject *mp = (PyDictObject *)self;
65494
e8969fb822fc make hashes always the size of pointers; introduce Py_hash_t #9778
Benjamin Peterson <benjamin@python.org>
parents: 64377
diff changeset
2190 Py_hash_t hash;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2191 PyDictKeyEntry *ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2192 PyObject **value_addr;
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
2193
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2194 if (!PyUnicode_CheckExact(key) ||
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72464
diff changeset
2195 (hash = ((PyASCIIObject *) key)->hash) == -1) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2196 hash = PyObject_Hash(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2197 if (hash == -1)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2198 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2199 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2200 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2201 if (ep == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2202 return NULL;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2203 return PyBool_FromLong(*value_addr != NULL);
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2204 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2205
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
2206 static PyObject *
85157
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
2207 dict_get(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
2208 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2209 PyObject *key;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2210 PyObject *failobj = Py_None;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2211 PyObject *val = NULL;
65494
e8969fb822fc make hashes always the size of pointers; introduce Py_hash_t #9778
Benjamin Peterson <benjamin@python.org>
parents: 64377
diff changeset
2212 Py_hash_t hash;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2213 PyDictKeyEntry *ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2214 PyObject **value_addr;
6395
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
2215
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2216 if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2217 return NULL;
6395
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
2218
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2219 if (!PyUnicode_CheckExact(key) ||
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72464
diff changeset
2220 (hash = ((PyASCIIObject *) key)->hash) == -1) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2221 hash = PyObject_Hash(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2222 if (hash == -1)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2223 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2224 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2225 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2226 if (ep == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2227 return NULL;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2228 val = *value_addr;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2229 if (val == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2230 val = failobj;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2231 Py_INCREF(val);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2232 return val;
6395
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
2233 }
22dae2637e08 dict_get(): New method for item access with different semantics than
Barry Warsaw <barry@python.org>
parents: 6351
diff changeset
2234
82542
a0b750ea3397 Add PyDict_SetDefault. (closes #17327)
Benjamin Peterson <benjamin@python.org>
parents: 82477
diff changeset
2235 PyObject *
a0b750ea3397 Add PyDict_SetDefault. (closes #17327)
Benjamin Peterson <benjamin@python.org>
parents: 82477
diff changeset
2236 PyDict_SetDefault(PyObject *d, PyObject *key, PyObject *defaultobj)
13892
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
2237 {
82542
a0b750ea3397 Add PyDict_SetDefault. (closes #17327)
Benjamin Peterson <benjamin@python.org>
parents: 82477
diff changeset
2238 PyDictObject *mp = (PyDictObject *)d;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2239 PyObject *val = NULL;
65494
e8969fb822fc make hashes always the size of pointers; introduce Py_hash_t #9778
Benjamin Peterson <benjamin@python.org>
parents: 64377
diff changeset
2240 Py_hash_t hash;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2241 PyDictKeyEntry *ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2242 PyObject **value_addr;
13892
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
2243
82542
a0b750ea3397 Add PyDict_SetDefault. (closes #17327)
Benjamin Peterson <benjamin@python.org>
parents: 82477
diff changeset
2244 if (!PyDict_Check(d)) {
a0b750ea3397 Add PyDict_SetDefault. (closes #17327)
Benjamin Peterson <benjamin@python.org>
parents: 82477
diff changeset
2245 PyErr_BadInternalCall();
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2246 return NULL;
82542
a0b750ea3397 Add PyDict_SetDefault. (closes #17327)
Benjamin Peterson <benjamin@python.org>
parents: 82477
diff changeset
2247 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2248 if (!PyUnicode_CheckExact(key) ||
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72464
diff changeset
2249 (hash = ((PyASCIIObject *) key)->hash) == -1) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2250 hash = PyObject_Hash(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2251 if (hash == -1)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2252 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2253 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2254 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2255 if (ep == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2256 return NULL;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2257 val = *value_addr;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2258 if (val == NULL) {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2259 if (mp->ma_keys->dk_usable <= 0) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2260 /* Need to resize. */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2261 if (insertion_resize(mp) < 0)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2262 return NULL;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2263 ep = find_empty_slot(mp, key, hash, &value_addr);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2264 }
82542
a0b750ea3397 Add PyDict_SetDefault. (closes #17327)
Benjamin Peterson <benjamin@python.org>
parents: 82477
diff changeset
2265 Py_INCREF(defaultobj);
82476
1a589001d752 fix possible setdefault refleak (closes #17328)
Benjamin Peterson <benjamin@python.org>
parents: 80120
diff changeset
2266 Py_INCREF(key);
82542
a0b750ea3397 Add PyDict_SetDefault. (closes #17327)
Benjamin Peterson <benjamin@python.org>
parents: 82477
diff changeset
2267 MAINTAIN_TRACKING(mp, key, defaultobj);
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2268 ep->me_key = key;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2269 ep->me_hash = hash;
82542
a0b750ea3397 Add PyDict_SetDefault. (closes #17327)
Benjamin Peterson <benjamin@python.org>
parents: 82477
diff changeset
2270 *value_addr = defaultobj;
a0b750ea3397 Add PyDict_SetDefault. (closes #17327)
Benjamin Peterson <benjamin@python.org>
parents: 82477
diff changeset
2271 val = defaultobj;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2272 mp->ma_keys->dk_usable--;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2273 mp->ma_used++;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2274 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2275 return val;
13892
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
2276 }
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
2277
82542
a0b750ea3397 Add PyDict_SetDefault. (closes #17327)
Benjamin Peterson <benjamin@python.org>
parents: 82477
diff changeset
2278 static PyObject *
a0b750ea3397 Add PyDict_SetDefault. (closes #17327)
Benjamin Peterson <benjamin@python.org>
parents: 82477
diff changeset
2279 dict_setdefault(PyDictObject *mp, PyObject *args)
a0b750ea3397 Add PyDict_SetDefault. (closes #17327)
Benjamin Peterson <benjamin@python.org>
parents: 82477
diff changeset
2280 {
a0b750ea3397 Add PyDict_SetDefault. (closes #17327)
Benjamin Peterson <benjamin@python.org>
parents: 82477
diff changeset
2281 PyObject *key, *val;
a0b750ea3397 Add PyDict_SetDefault. (closes #17327)
Benjamin Peterson <benjamin@python.org>
parents: 82477
diff changeset
2282 PyObject *defaultobj = Py_None;
a0b750ea3397 Add PyDict_SetDefault. (closes #17327)
Benjamin Peterson <benjamin@python.org>
parents: 82477
diff changeset
2283
a0b750ea3397 Add PyDict_SetDefault. (closes #17327)
Benjamin Peterson <benjamin@python.org>
parents: 82477
diff changeset
2284 if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &defaultobj))
a0b750ea3397 Add PyDict_SetDefault. (closes #17327)
Benjamin Peterson <benjamin@python.org>
parents: 82477
diff changeset
2285 return NULL;
a0b750ea3397 Add PyDict_SetDefault. (closes #17327)
Benjamin Peterson <benjamin@python.org>
parents: 82477
diff changeset
2286
82548
ca9a85c36e09 fix warning (closes #17327)
Benjamin Peterson <benjamin@python.org>
parents: 82542
diff changeset
2287 val = PyDict_SetDefault((PyObject *)mp, key, defaultobj);
82542
a0b750ea3397 Add PyDict_SetDefault. (closes #17327)
Benjamin Peterson <benjamin@python.org>
parents: 82477
diff changeset
2288 Py_XINCREF(val);
a0b750ea3397 Add PyDict_SetDefault. (closes #17327)
Benjamin Peterson <benjamin@python.org>
parents: 82477
diff changeset
2289 return val;
a0b750ea3397 Add PyDict_SetDefault. (closes #17327)
Benjamin Peterson <benjamin@python.org>
parents: 82477
diff changeset
2290 }
13892
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
2291
2190b7599837 Barry's patch to implement the new setdefault() method.
Guido van Rossum <guido@python.org>
parents: 13660
diff changeset
2292 static PyObject *
85157
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
2293 dict_clear(PyDictObject *mp)
5209
2ec9c933afb8 Add clear() method to dictionary objects.
Guido van Rossum <guido@python.org>
parents: 5059
diff changeset
2294 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2295 PyDict_Clear((PyObject *)mp);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2296 Py_RETURN_NONE;
5209
2ec9c933afb8 Add clear() method to dictionary objects.
Guido van Rossum <guido@python.org>
parents: 5059
diff changeset
2297 }
2ec9c933afb8 Add clear() method to dictionary objects.
Guido van Rossum <guido@python.org>
parents: 5059
diff changeset
2298
15520
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2299 static PyObject *
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
2300 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
2301 {
65494
e8969fb822fc make hashes always the size of pointers; introduce Py_hash_t #9778
Benjamin Peterson <benjamin@python.org>
parents: 64377
diff changeset
2302 Py_hash_t hash;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2303 PyObject *old_value, *old_key;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2304 PyObject *key, *deflt = NULL;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2305 PyDictKeyEntry *ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2306 PyObject **value_addr;
22994
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
2307
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2308 if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2309 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2310 if (mp->ma_used == 0) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2311 if (deflt) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2312 Py_INCREF(deflt);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2313 return deflt;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2314 }
85502
bfaf3a2907bd Factor-out the common code for setting a KeyError.
Raymond Hettinger <python@rcn.com>
parents: 85157
diff changeset
2315 _PyErr_SetKeyError(key);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2316 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2317 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2318 if (!PyUnicode_CheckExact(key) ||
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72464
diff changeset
2319 (hash = ((PyASCIIObject *) key)->hash) == -1) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2320 hash = PyObject_Hash(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2321 if (hash == -1)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2322 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2323 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2324 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2325 if (ep == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2326 return NULL;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2327 old_value = *value_addr;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2328 if (old_value == NULL) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2329 if (deflt) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2330 Py_INCREF(deflt);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2331 return deflt;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2332 }
85502
bfaf3a2907bd Factor-out the common code for setting a KeyError.
Raymond Hettinger <python@rcn.com>
parents: 85157
diff changeset
2333 _PyErr_SetKeyError(key);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2334 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2335 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2336 *value_addr = NULL;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2337 mp->ma_used--;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2338 if (!_PyDict_HasSplitTable(mp)) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2339 ENSURE_ALLOWS_DELETIONS(mp);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2340 old_key = ep->me_key;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2341 Py_INCREF(dummy);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2342 ep->me_key = dummy;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2343 Py_DECREF(old_key);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2344 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2345 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
2346 }
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
2347
196dfa7ed348 Add Raymond Hettinger's d.pop(). See SF patch 539949.
Guido van Rossum <guido@python.org>
parents: 22973
diff changeset
2348 static PyObject *
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
2349 dict_popitem(PyDictObject *mp)
15520
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2350 {
65494
e8969fb822fc make hashes always the size of pointers; introduce Py_hash_t #9778
Benjamin Peterson <benjamin@python.org>
parents: 64377
diff changeset
2351 Py_hash_t i = 0;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2352 PyDictKeyEntry *ep;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2353 PyObject *res;
15520
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2354
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2355
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2356 /* Allocate the result tuple before checking the size. Believe it
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2357 * or not, this allocation could trigger a garbage collection which
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2358 * could empty the dict, so if we checked the size first and that
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2359 * happened, the result would be an infinite loop (searching for an
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2360 * entry that no longer exists). Note that the usual popitem()
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2361 * idiom is "while d: k, v = d.popitem()". so needing to throw the
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2362 * tuple away if the dict *is* empty isn't a significant
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2363 * inefficiency -- possible, but unlikely in practice.
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2364 */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2365 res = PyTuple_New(2);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2366 if (res == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2367 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2368 if (mp->ma_used == 0) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2369 Py_DECREF(res);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2370 PyErr_SetString(PyExc_KeyError,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2371 "popitem(): dictionary is empty");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2372 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2373 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2374 /* Convert split table to combined table */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2375 if (mp->ma_keys->dk_lookup == lookdict_split) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2376 if (dictresize(mp, DK_SIZE(mp->ma_keys))) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2377 Py_DECREF(res);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2378 return NULL;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2379 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2380 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2381 ENSURE_ALLOWS_DELETIONS(mp);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2382 /* Set ep to "the first" dict entry with a value. We abuse the hash
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2383 * field of slot 0 to hold a search finger:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2384 * If slot 0 has a value, use slot 0.
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2385 * Else slot 0 is being used to hold a search finger,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2386 * and we use its hash value as the first index to look.
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2387 */
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2388 ep = &mp->ma_keys->dk_entries[0];
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2389 if (ep->me_value == NULL) {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2390 Py_ssize_t mask = DK_MASK(mp->ma_keys);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2391 i = ep->me_hash;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2392 /* The hash field may be a real hash value, or it may be a
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2393 * legit search finger, or it may be a once-legit search
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2394 * finger that's out of bounds now because it wrapped around
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2395 * or the table shrunk -- simply make sure it's in bounds now.
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2396 */
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2397 if (i > mask || i < 1)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2398 i = 1; /* skip slot 0 */
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2399 while ((ep = &mp->ma_keys->dk_entries[i])->me_value == NULL) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2400 i++;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2401 if (i > mask)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2402 i = 1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2403 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2404 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2405 PyTuple_SET_ITEM(res, 0, ep->me_key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2406 PyTuple_SET_ITEM(res, 1, ep->me_value);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2407 Py_INCREF(dummy);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2408 ep->me_key = dummy;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2409 ep->me_value = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2410 mp->ma_used--;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2411 assert(mp->ma_keys->dk_entries[0].me_value == NULL);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2412 mp->ma_keys->dk_entries[0].me_hash = i + 1; /* next place to start */
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2413 return res;
15520
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2414 }
8854d9d5add5 Add popitem() -- SF patch #102733.
Guido van Rossum <guido@python.org>
parents: 15463
diff changeset
2415
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2416 static int
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2417 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
2418 {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2419 Py_ssize_t i, n;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2420 PyDictObject *mp = (PyDictObject *)op;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2421 if (mp->ma_keys->dk_lookup == lookdict) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2422 for (i = 0; i < DK_SIZE(mp->ma_keys); i++) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2423 if (mp->ma_keys->dk_entries[i].me_value != NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2424 Py_VISIT(mp->ma_keys->dk_entries[i].me_value);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2425 Py_VISIT(mp->ma_keys->dk_entries[i].me_key);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2426 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2427 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2428 } else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2429 if (mp->ma_values != NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2430 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2431 Py_VISIT(mp->ma_values[i]);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2432 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2433 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2434 else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2435 for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2436 Py_VISIT(mp->ma_keys->dk_entries[i].me_value);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2437 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2438 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2439 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2440 return 0;
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2441 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2442
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2443 static int
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2444 dict_tp_clear(PyObject *op)
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2445 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2446 PyDict_Clear(op);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2447 return 0;
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2448 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
2449
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
2450 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
2451
47466
58f4b60773fe Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 45514
diff changeset
2452 static PyObject *
58f4b60773fe Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 45514
diff changeset
2453 dict_sizeof(PyDictObject *mp)
58f4b60773fe Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 45514
diff changeset
2454 {
76530
b044e0568be2 Account for shared keys in type's __sizeof__ (#13903).
Martin v. Loewis <martin@v.loewis.de>
parents: 76517
diff changeset
2455 Py_ssize_t size, res;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2456
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2457 size = DK_SIZE(mp->ma_keys);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2458 res = sizeof(PyDictObject);
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2459 if (mp->ma_values)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2460 res += size * sizeof(PyObject*);
76530
b044e0568be2 Account for shared keys in type's __sizeof__ (#13903).
Martin v. Loewis <martin@v.loewis.de>
parents: 76517
diff changeset
2461 /* If the dictionary is split, the keys portion is accounted-for
b044e0568be2 Account for shared keys in type's __sizeof__ (#13903).
Martin v. Loewis <martin@v.loewis.de>
parents: 76517
diff changeset
2462 in the type object. */
b044e0568be2 Account for shared keys in type's __sizeof__ (#13903).
Martin v. Loewis <martin@v.loewis.de>
parents: 76517
diff changeset
2463 if (mp->ma_keys->dk_refcnt == 1)
b044e0568be2 Account for shared keys in type's __sizeof__ (#13903).
Martin v. Loewis <martin@v.loewis.de>
parents: 76517
diff changeset
2464 res += sizeof(PyDictKeysObject) + (size-1) * sizeof(PyDictKeyEntry);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2465 return PyLong_FromSsize_t(res);
47466
58f4b60773fe Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 45514
diff changeset
2466 }
58f4b60773fe Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 45514
diff changeset
2467
76530
b044e0568be2 Account for shared keys in type's __sizeof__ (#13903).
Martin v. Loewis <martin@v.loewis.de>
parents: 76517
diff changeset
2468 Py_ssize_t
b044e0568be2 Account for shared keys in type's __sizeof__ (#13903).
Martin v. Loewis <martin@v.loewis.de>
parents: 76517
diff changeset
2469 _PyDict_KeysSize(PyDictKeysObject *keys)
b044e0568be2 Account for shared keys in type's __sizeof__ (#13903).
Martin v. Loewis <martin@v.loewis.de>
parents: 76517
diff changeset
2470 {
b044e0568be2 Account for shared keys in type's __sizeof__ (#13903).
Martin v. Loewis <martin@v.loewis.de>
parents: 76517
diff changeset
2471 return sizeof(PyDictKeysObject) + (DK_SIZE(keys)-1) * sizeof(PyDictKeyEntry);
47466
58f4b60773fe Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 45514
diff changeset
2472 }
58f4b60773fe Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 45514
diff changeset
2473
31172
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31019
diff changeset
2474 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
2475
47466
58f4b60773fe Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 45514
diff changeset
2476 PyDoc_STRVAR(sizeof__doc__,
58f4b60773fe Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 45514
diff changeset
2477 "D.__sizeof__() -> size of D in memory, in bytes");
58f4b60773fe Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 45514
diff changeset
2478
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2479 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
2480 "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
2481
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2482 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
2483 "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
2484
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2485 PyDoc_STRVAR(pop__doc__,
49493
bdf4da5d04ad merge from trunk
Benjamin Peterson <benjamin@python.org>
parents: 48680
diff changeset
2486 "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
2487 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
2488
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2489 PyDoc_STRVAR(popitem__doc__,
15560
143646bafd1b Add long-overdue docstrings to dict methods.
Tim Peters <tim.peters@gmail.com>
parents: 15554
diff changeset
2490 "D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\
49493
bdf4da5d04ad merge from trunk
Benjamin Peterson <benjamin@python.org>
parents: 48680
diff changeset
2491 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
2492
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2493 PyDoc_STRVAR(update__doc__,
83714
61214ebd347d Touch up grammar for dict.update() docstring.
Brett Cannon <brett@python.org>
parents: 82548
diff changeset
2494 "D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
61214ebd347d Touch up grammar for dict.update() docstring.
Brett Cannon <brett@python.org>
parents: 82548
diff changeset
2495 If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
61214ebd347d Touch up grammar for dict.update() docstring.
Brett Cannon <brett@python.org>
parents: 82548
diff changeset
2496 If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
61214ebd347d Touch up grammar for dict.update() docstring.
Brett Cannon <brett@python.org>
parents: 82548
diff changeset
2497 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
2498
26437
57d03375aead SF Patch 643443. Added dict.fromkeys(iterable, value=None), a class
Raymond Hettinger <python@rcn.com>
parents: 26366
diff changeset
2499 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
2500 "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
2501 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
2502
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2503 PyDoc_STRVAR(clear__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2504 "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
2505
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2506 PyDoc_STRVAR(copy__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2507 "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
2508
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
2509 /* Forward */
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
2510 static PyObject *dictkeys_new(PyObject *);
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
2511 static PyObject *dictitems_new(PyObject *);
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
2512 static PyObject *dictvalues_new(PyObject *);
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
2513
42381
9e84b4868984 Fix the docstrings for keys(), items(), values() (especially the latter).
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
2514 PyDoc_STRVAR(keys__doc__,
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2515 "D.keys() -> a set-like object providing a view on D's keys");
42381
9e84b4868984 Fix the docstrings for keys(), items(), values() (especially the latter).
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
2516 PyDoc_STRVAR(items__doc__,
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2517 "D.items() -> a set-like object providing a view on D's items");
42381
9e84b4868984 Fix the docstrings for keys(), items(), values() (especially the latter).
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
2518 PyDoc_STRVAR(values__doc__,
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2519 "D.values() -> an object providing a view on D's values");
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
2520
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
2521 static PyMethodDef mapp_methods[] = {
86465
8fde1a2c94dc Issue #16612: Add "Argument Clinic", a compile-time preprocessor
Larry Hastings <larry@hastings.org>
parents: 85502
diff changeset
2522 DICT___CONTAINS___METHODDEF
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2523 {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2524 getitem__doc__},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2525 {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2526 sizeof__doc__},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2527 {"get", (PyCFunction)dict_get, METH_VARARGS,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2528 get__doc__},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2529 {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2530 setdefault_doc__},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2531 {"pop", (PyCFunction)dict_pop, METH_VARARGS,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2532 pop__doc__},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2533 {"popitem", (PyCFunction)dict_popitem, METH_NOARGS,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2534 popitem__doc__},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2535 {"keys", (PyCFunction)dictkeys_new, METH_NOARGS,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2536 keys__doc__},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2537 {"items", (PyCFunction)dictitems_new, METH_NOARGS,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2538 items__doc__},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2539 {"values", (PyCFunction)dictvalues_new, METH_NOARGS,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2540 values__doc__},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2541 {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2542 update__doc__},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2543 {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2544 fromkeys__doc__},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2545 {"clear", (PyCFunction)dict_clear, METH_NOARGS,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2546 clear__doc__},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2547 {"copy", (PyCFunction)dict_copy, METH_NOARGS,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2548 copy__doc__},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2549 {NULL, NULL} /* sentinel */
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2550 };
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2551
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
2552 /* 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
2553 int
b2fd7af17186 Expose dict_contains() and PyDict_Contains() with is about 10% faster
Raymond Hettinger <python@rcn.com>
parents: 30265
diff changeset
2554 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
2555 {
65494
e8969fb822fc make hashes always the size of pointers; introduce Py_hash_t #9778
Benjamin Peterson <benjamin@python.org>
parents: 64377
diff changeset
2556 Py_hash_t hash;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2557 PyDictObject *mp = (PyDictObject *)op;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2558 PyDictKeyEntry *ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2559 PyObject **value_addr;
17594
bf94af396b1e Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents: 17536
diff changeset
2560
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2561 if (!PyUnicode_CheckExact(key) ||
72475
8beaa9a37387 Implement PEP 393.
Martin v. Löwis <martin@v.loewis.de>
parents: 72464
diff changeset
2562 (hash = ((PyASCIIObject *) key)->hash) == -1) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2563 hash = PyObject_Hash(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2564 if (hash == -1)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2565 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2566 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2567 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2568 return (ep == NULL) ? -1 : (*value_addr != 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
2569 }
bf94af396b1e Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents: 17536
diff changeset
2570
40893
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40823
diff changeset
2571 /* Internal version of PyDict_Contains used when the hash value is already known */
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40823
diff changeset
2572 int
65494
e8969fb822fc make hashes always the size of pointers; introduce Py_hash_t #9778
Benjamin Peterson <benjamin@python.org>
parents: 64377
diff changeset
2573 _PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash)
40893
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40823
diff changeset
2574 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2575 PyDictObject *mp = (PyDictObject *)op;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2576 PyDictKeyEntry *ep;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2577 PyObject **value_addr;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2578
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2579 ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2580 return (ep == NULL) ? -1 : (*value_addr != NULL);
40893
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40823
diff changeset
2581 }
32c4897b5d46 Merged revisions 53623-53858 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40823
diff changeset
2582
17594
bf94af396b1e Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents: 17536
diff changeset
2583 /* 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
2584 static PySequenceMethods dict_as_sequence = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2585 0, /* sq_length */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2586 0, /* sq_concat */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2587 0, /* sq_repeat */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2588 0, /* sq_item */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2589 0, /* sq_slice */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2590 0, /* sq_ass_item */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2591 0, /* sq_ass_slice */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2592 PyDict_Contains, /* sq_contains */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2593 0, /* sq_inplace_concat */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2594 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
2595 };
bf94af396b1e Implement, test and document "key in dict" and "key not in dict".
Guido van Rossum <guido@python.org>
parents: 17536
diff changeset
2596
17644
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2597 static PyObject *
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2598 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
2599 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2600 PyObject *self;
84673
5beb1f12f19f Cleanup dictobject.c
Victor Stinner <victor.stinner@gmail.com>
parents: 84672
diff changeset
2601 PyDictObject *d;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2602
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2603 assert(type != NULL && type->tp_alloc != NULL);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2604 self = type->tp_alloc(type, 0);
84673
5beb1f12f19f Cleanup dictobject.c
Victor Stinner <victor.stinner@gmail.com>
parents: 84672
diff changeset
2605 if (self == NULL)
5beb1f12f19f Cleanup dictobject.c
Victor Stinner <victor.stinner@gmail.com>
parents: 84672
diff changeset
2606 return NULL;
5beb1f12f19f Cleanup dictobject.c
Victor Stinner <victor.stinner@gmail.com>
parents: 84672
diff changeset
2607 d = (PyDictObject *)self;
84674
b7c6d203c9fb Issue #18408: dict_new() now fails on new_keys_object() error
Victor Stinner <victor.stinner@gmail.com>
parents: 84673
diff changeset
2608
84673
5beb1f12f19f Cleanup dictobject.c
Victor Stinner <victor.stinner@gmail.com>
parents: 84672
diff changeset
2609 /* The object has been implicitly tracked by tp_alloc */
5beb1f12f19f Cleanup dictobject.c
Victor Stinner <victor.stinner@gmail.com>
parents: 84672
diff changeset
2610 if (type == &PyDict_Type)
5beb1f12f19f Cleanup dictobject.c
Victor Stinner <victor.stinner@gmail.com>
parents: 84672
diff changeset
2611 _PyObject_GC_UNTRACK(d);
84674
b7c6d203c9fb Issue #18408: dict_new() now fails on new_keys_object() error
Victor Stinner <victor.stinner@gmail.com>
parents: 84673
diff changeset
2612
b7c6d203c9fb Issue #18408: dict_new() now fails on new_keys_object() error
Victor Stinner <victor.stinner@gmail.com>
parents: 84673
diff changeset
2613 d->ma_used = 0;
b7c6d203c9fb Issue #18408: dict_new() now fails on new_keys_object() error
Victor Stinner <victor.stinner@gmail.com>
parents: 84673
diff changeset
2614 d->ma_keys = new_keys_object(PyDict_MINSIZE_COMBINED);
b7c6d203c9fb Issue #18408: dict_new() now fails on new_keys_object() error
Victor Stinner <victor.stinner@gmail.com>
parents: 84673
diff changeset
2615 if (d->ma_keys == NULL) {
b7c6d203c9fb Issue #18408: dict_new() now fails on new_keys_object() error
Victor Stinner <victor.stinner@gmail.com>
parents: 84673
diff changeset
2616 Py_DECREF(self);
b7c6d203c9fb Issue #18408: dict_new() now fails on new_keys_object() error
Victor Stinner <victor.stinner@gmail.com>
parents: 84673
diff changeset
2617 return NULL;
b7c6d203c9fb Issue #18408: dict_new() now fails on new_keys_object() error
Victor Stinner <victor.stinner@gmail.com>
parents: 84673
diff changeset
2618 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2619 return self;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2620 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2621
19658
2fa615dfc464 Make dictionary() a real constructor. Accepts at most one argument, "a
Tim Peters <tim.peters@gmail.com>
parents: 19581
diff changeset
2622 static int
2fa615dfc464 Make dictionary() a real constructor. Accepts at most one argument, "a
Tim Peters <tim.peters@gmail.com>
parents: 19581
diff changeset
2623 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
2624 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2625 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
2626 }
2fa615dfc464 Make dictionary() a real constructor. Accepts at most one argument, "a
Tim Peters <tim.peters@gmail.com>
parents: 19581
diff changeset
2627
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18283
diff changeset
2628 static PyObject *
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
2629 dict_iter(PyDictObject *dict)
17644
5dbfe8316236 Add experimental iterkeys(), itervalues(), iteritems() to dict
Guido van Rossum <guido@python.org>
parents: 17617
diff changeset
2630 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2631 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
2632 }
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2633
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22994
diff changeset
2634 PyDoc_STRVAR(dictionary_doc,
59224
8d48f9b59689 Merged revisions 78515-78516,78522 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55584
diff changeset
2635 "dict() -> new empty dictionary\n"
20754
272336648d1f Rename "dictionary" (type and constructor) to "dict".
Tim Peters <tim.peters@gmail.com>
parents: 20724
diff changeset
2636 "dict(mapping) -> new dictionary initialized from a mapping object's\n"
59224
8d48f9b59689 Merged revisions 78515-78516,78522 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55584
diff changeset
2637 " (key, value) pairs\n"
8d48f9b59689 Merged revisions 78515-78516,78522 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55584
diff changeset
2638 "dict(iterable) -> new dictionary initialized as if via:\n"
20724
f224632d0260 dictionary() constructor:
Tim Peters <tim.peters@gmail.com>
parents: 20683
diff changeset
2639 " d = {}\n"
59224
8d48f9b59689 Merged revisions 78515-78516,78522 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55584
diff changeset
2640 " 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
2641 " d[k] = v\n"
08b720cd387e Patch #642500 with slight modifications: allow keyword arguments in
Just van Rossum <just@letterror.com>
parents: 25115
diff changeset
2642 "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
2643 " 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
2644
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
2645 PyTypeObject PyDict_Type = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2646 PyVarObject_HEAD_INIT(&PyType_Type, 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2647 "dict",
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2648 sizeof(PyDictObject),
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2649 0,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2650 (destructor)dict_dealloc, /* tp_dealloc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2651 0, /* tp_print */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2652 0, /* tp_getattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2653 0, /* tp_setattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2654 0, /* tp_reserved */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2655 (reprfunc)dict_repr, /* tp_repr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2656 0, /* tp_as_number */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2657 &dict_as_sequence, /* tp_as_sequence */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2658 &dict_as_mapping, /* tp_as_mapping */
65512
17c96aaf93c8 Remove unneeded casts to hashfunc.
Georg Brandl <georg@python.org>
parents: 65498
diff changeset
2659 PyObject_HashNotImplemented, /* tp_hash */
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2660 0, /* tp_call */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2661 0, /* tp_str */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2662 PyObject_GenericGetAttr, /* tp_getattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2663 0, /* tp_setattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2664 0, /* tp_as_buffer */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2665 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2666 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2667 dictionary_doc, /* tp_doc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2668 dict_traverse, /* tp_traverse */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2669 dict_tp_clear, /* tp_clear */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2670 dict_richcompare, /* tp_richcompare */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2671 0, /* tp_weaklistoffset */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2672 (getiterfunc)dict_iter, /* tp_iter */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2673 0, /* tp_iternext */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2674 mapp_methods, /* tp_methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2675 0, /* tp_members */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2676 0, /* tp_getset */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2677 0, /* tp_base */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2678 0, /* tp_dict */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2679 0, /* tp_descr_get */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2680 0, /* tp_descr_set */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2681 0, /* tp_dictoffset */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2682 dict_init, /* tp_init */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2683 PyType_GenericAlloc, /* tp_alloc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2684 dict_new, /* tp_new */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2685 PyObject_GC_Del, /* tp_free */
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2686 };
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2687
75959
c67f3681e032 Issue #14383: Add _PyDict_GetItemId() and _PyDict_SetItemId() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 75445
diff changeset
2688 PyObject *
c67f3681e032 Issue #14383: Add _PyDict_GetItemId() and _PyDict_SetItemId() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 75445
diff changeset
2689 _PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
c67f3681e032 Issue #14383: Add _PyDict_GetItemId() and _PyDict_SetItemId() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 75445
diff changeset
2690 {
c67f3681e032 Issue #14383: Add _PyDict_GetItemId() and _PyDict_SetItemId() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 75445
diff changeset
2691 PyObject *kv;
c67f3681e032 Issue #14383: Add _PyDict_GetItemId() and _PyDict_SetItemId() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 75445
diff changeset
2692 kv = _PyUnicode_FromId(key); /* borrowed */
84799
4a69dbe71aeb Issue #18520: Fix _PyDict_GetItemId(), suppress _PyUnicode_FromId() error
Victor Stinner <victor.stinner@gmail.com>
parents: 84702
diff changeset
2693 if (kv == NULL) {
4a69dbe71aeb Issue #18520: Fix _PyDict_GetItemId(), suppress _PyUnicode_FromId() error
Victor Stinner <victor.stinner@gmail.com>
parents: 84702
diff changeset
2694 PyErr_Clear();
75959
c67f3681e032 Issue #14383: Add _PyDict_GetItemId() and _PyDict_SetItemId() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 75445
diff changeset
2695 return NULL;
84799
4a69dbe71aeb Issue #18520: Fix _PyDict_GetItemId(), suppress _PyUnicode_FromId() error
Victor Stinner <victor.stinner@gmail.com>
parents: 84702
diff changeset
2696 }
75959
c67f3681e032 Issue #14383: Add _PyDict_GetItemId() and _PyDict_SetItemId() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 75445
diff changeset
2697 return PyDict_GetItem(dp, kv);
c67f3681e032 Issue #14383: Add _PyDict_GetItemId() and _PyDict_SetItemId() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 75445
diff changeset
2698 }
c67f3681e032 Issue #14383: Add _PyDict_GetItemId() and _PyDict_SetItemId() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 75445
diff changeset
2699
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
2700 /* 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
2701
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5310
diff changeset
2702 PyObject *
26599
ecea8410178e Constify char* API. Fixes #651363. 2.2 candidate.
Martin v. Löwis <martin@v.loewis.de>
parents: 26583
diff changeset
2703 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
2704 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2705 PyObject *kv, *rv;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2706 kv = PyUnicode_FromString(key);
84672
ac4e8e6a7436 Issue #18408: Fix PyDict_GetItemString(), suppress PyUnicode_FromString() error
Victor Stinner <victor.stinner@gmail.com>
parents: 84509
diff changeset
2707 if (kv == NULL) {
ac4e8e6a7436 Issue #18408: Fix PyDict_GetItemString(), suppress PyUnicode_FromString() error
Victor Stinner <victor.stinner@gmail.com>
parents: 84509
diff changeset
2708 PyErr_Clear();
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2709 return NULL;
84672
ac4e8e6a7436 Issue #18408: Fix PyDict_GetItemString(), suppress PyUnicode_FromString() error
Victor Stinner <victor.stinner@gmail.com>
parents: 84509
diff changeset
2710 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2711 rv = PyDict_GetItem(v, kv);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2712 Py_DECREF(kv);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2713 return rv;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2714 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2715
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2716 int
75959
c67f3681e032 Issue #14383: Add _PyDict_GetItemId() and _PyDict_SetItemId() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 75445
diff changeset
2717 _PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
c67f3681e032 Issue #14383: Add _PyDict_GetItemId() and _PyDict_SetItemId() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 75445
diff changeset
2718 {
c67f3681e032 Issue #14383: Add _PyDict_GetItemId() and _PyDict_SetItemId() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 75445
diff changeset
2719 PyObject *kv;
c67f3681e032 Issue #14383: Add _PyDict_GetItemId() and _PyDict_SetItemId() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 75445
diff changeset
2720 kv = _PyUnicode_FromId(key); /* borrowed */
c67f3681e032 Issue #14383: Add _PyDict_GetItemId() and _PyDict_SetItemId() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 75445
diff changeset
2721 if (kv == NULL)
c67f3681e032 Issue #14383: Add _PyDict_GetItemId() and _PyDict_SetItemId() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 75445
diff changeset
2722 return -1;
c67f3681e032 Issue #14383: Add _PyDict_GetItemId() and _PyDict_SetItemId() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 75445
diff changeset
2723 return PyDict_SetItem(v, kv, item);
c67f3681e032 Issue #14383: Add _PyDict_GetItemId() and _PyDict_SetItemId() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 75445
diff changeset
2724 }
c67f3681e032 Issue #14383: Add _PyDict_GetItemId() and _PyDict_SetItemId() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 75445
diff changeset
2725
c67f3681e032 Issue #14383: Add _PyDict_GetItemId() and _PyDict_SetItemId() functions
Victor Stinner <victor.stinner@gmail.com>
parents: 75445
diff changeset
2726 int
26599
ecea8410178e Constify char* API. Fixes #651363. 2.2 candidate.
Martin v. Löwis <martin@v.loewis.de>
parents: 26583
diff changeset
2727 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
2728 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2729 PyObject *kv;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2730 int err;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2731 kv = PyUnicode_FromString(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2732 if (kv == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2733 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2734 PyUnicode_InternInPlace(&kv); /* XXX Should we really? */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2735 err = PyDict_SetItem(v, kv, item);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2736 Py_DECREF(kv);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2737 return err;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2738 }
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2739
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2740 int
86968
69071054b42f Issue #19512: Add a new _PyDict_DelItemId() function, similar to
Victor Stinner <victor.stinner@gmail.com>
parents: 86465
diff changeset
2741 _PyDict_DelItemId(PyObject *v, _Py_Identifier *key)
69071054b42f Issue #19512: Add a new _PyDict_DelItemId() function, similar to
Victor Stinner <victor.stinner@gmail.com>
parents: 86465
diff changeset
2742 {
69071054b42f Issue #19512: Add a new _PyDict_DelItemId() function, similar to
Victor Stinner <victor.stinner@gmail.com>
parents: 86465
diff changeset
2743 PyObject *kv = _PyUnicode_FromId(key); /* borrowed */
69071054b42f Issue #19512: Add a new _PyDict_DelItemId() function, similar to
Victor Stinner <victor.stinner@gmail.com>
parents: 86465
diff changeset
2744 if (kv == NULL)
69071054b42f Issue #19512: Add a new _PyDict_DelItemId() function, similar to
Victor Stinner <victor.stinner@gmail.com>
parents: 86465
diff changeset
2745 return -1;
69071054b42f Issue #19512: Add a new _PyDict_DelItemId() function, similar to
Victor Stinner <victor.stinner@gmail.com>
parents: 86465
diff changeset
2746 return PyDict_DelItem(v, kv);
69071054b42f Issue #19512: Add a new _PyDict_DelItemId() function, similar to
Victor Stinner <victor.stinner@gmail.com>
parents: 86465
diff changeset
2747 }
69071054b42f Issue #19512: Add a new _PyDict_DelItemId() function, similar to
Victor Stinner <victor.stinner@gmail.com>
parents: 86465
diff changeset
2748
69071054b42f Issue #19512: Add a new _PyDict_DelItemId() function, similar to
Victor Stinner <victor.stinner@gmail.com>
parents: 86465
diff changeset
2749 int
26599
ecea8410178e Constify char* API. Fixes #651363. 2.2 candidate.
Martin v. Löwis <martin@v.loewis.de>
parents: 26583
diff changeset
2750 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
2751 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2752 PyObject *kv;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2753 int err;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2754 kv = PyUnicode_FromString(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2755 if (kv == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2756 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2757 err = PyDict_DelItem(v, kv);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2758 Py_DECREF(kv);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2759 return err;
1256
7aa9613ffd36 Generalized version of dictionaries, with compatibility hacks.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2760 }
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2761
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2762 /* Dictionary iterator types */
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2763
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2764 typedef struct {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2765 PyObject_HEAD
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2766 PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2767 Py_ssize_t di_used;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2768 Py_ssize_t di_pos;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2769 PyObject* di_result; /* reusable result tuple for iteritems */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2770 Py_ssize_t len;
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2771 } dictiterobject;
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2772
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2773 static PyObject *
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43393
diff changeset
2774 dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2775 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2776 dictiterobject *di;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2777 di = PyObject_GC_New(dictiterobject, itertype);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2778 if (di == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2779 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2780 Py_INCREF(dict);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2781 di->di_dict = dict;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2782 di->di_used = dict->ma_used;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2783 di->di_pos = 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2784 di->len = dict->ma_used;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2785 if (itertype == &PyDictIterItem_Type) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2786 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2787 if (di->di_result == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2788 Py_DECREF(di);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2789 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2790 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2791 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2792 else
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2793 di->di_result = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2794 _PyObject_GC_TRACK(di);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2795 return (PyObject *)di;
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2796 }
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2797
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2798 static void
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2799 dictiter_dealloc(dictiterobject *di)
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2800 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2801 Py_XDECREF(di->di_dict);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2802 Py_XDECREF(di->di_result);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2803 PyObject_GC_Del(di);
50553
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49493
diff changeset
2804 }
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49493
diff changeset
2805
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49493
diff changeset
2806 static int
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49493
diff changeset
2807 dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49493
diff changeset
2808 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2809 Py_VISIT(di->di_dict);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2810 Py_VISIT(di->di_result);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2811 return 0;
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2812 }
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2813
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2814 static PyObject *
31683
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2815 dictiter_len(dictiterobject *di)
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2816 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2817 Py_ssize_t len = 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2818 if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2819 len = di->len;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2820 return PyLong_FromSize_t(len);
31683
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2821 }
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2822
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
2823 PyDoc_STRVAR(length_hint_doc,
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
2824 "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
2825
76091
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
2826 static PyObject *
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
2827 dictiter_reduce(dictiterobject *di);
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
2828
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
2829 PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
2830
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35350
diff changeset
2831 static PyMethodDef dictiter_methods[] = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2832 {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2833 length_hint_doc},
76091
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
2834 {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS,
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
2835 reduce_doc},
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2836 {NULL, NULL} /* sentinel */
31683
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2837 };
c32381f4dc6f Make the new dictionary iterators transparent with respect to length.
Raymond Hettinger <python@rcn.com>
parents: 31680
diff changeset
2838
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2839 static PyObject *dictiter_iternextkey(dictiterobject *di)
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2840 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2841 PyObject *key;
85157
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
2842 Py_ssize_t i, mask, offset;
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
2843 PyDictKeysObject *k;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2844 PyDictObject *d = di->di_dict;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2845 PyObject **value_ptr;
17617
aac65f50df6f Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents: 17597
diff changeset
2846
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2847 if (d == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2848 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2849 assert (PyDict_Check(d));
24259
2f60f90ce309 Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
2850
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2851 if (di->di_used != d->ma_used) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2852 PyErr_SetString(PyExc_RuntimeError,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2853 "dictionary changed size during iteration");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2854 di->di_used = -1; /* Make this state sticky */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2855 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2856 }
24259
2f60f90ce309 Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
2857
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2858 i = di->di_pos;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2859 if (i < 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2860 goto fail;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2861 k = d->ma_keys;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2862 if (d->ma_values) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2863 value_ptr = &d->ma_values[i];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2864 offset = sizeof(PyObject *);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2865 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2866 else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2867 value_ptr = &k->dk_entries[i].me_value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2868 offset = sizeof(PyDictKeyEntry);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2869 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2870 mask = DK_SIZE(k)-1;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2871 while (i <= mask && *value_ptr == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2872 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2873 i++;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2874 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2875 di->di_pos = i+1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2876 if (i > mask)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2877 goto fail;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2878 di->len--;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2879 key = k->dk_entries[i].me_key;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2880 Py_INCREF(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2881 return key;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2882
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2883 fail:
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2884 Py_DECREF(d);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2885 di->di_dict = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2886 return NULL;
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2887 }
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
2888
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2889 PyTypeObject PyDictIterKey_Type = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2890 PyVarObject_HEAD_INIT(&PyType_Type, 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2891 "dict_keyiterator", /* tp_name */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2892 sizeof(dictiterobject), /* tp_basicsize */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2893 0, /* tp_itemsize */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2894 /* methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2895 (destructor)dictiter_dealloc, /* tp_dealloc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2896 0, /* tp_print */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2897 0, /* tp_getattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2898 0, /* tp_setattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2899 0, /* tp_reserved */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2900 0, /* tp_repr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2901 0, /* tp_as_number */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2902 0, /* tp_as_sequence */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2903 0, /* tp_as_mapping */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2904 0, /* tp_hash */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2905 0, /* tp_call */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2906 0, /* tp_str */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2907 PyObject_GenericGetAttr, /* tp_getattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2908 0, /* tp_setattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2909 0, /* tp_as_buffer */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2910 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2911 0, /* tp_doc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2912 (traverseproc)dictiter_traverse, /* tp_traverse */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2913 0, /* tp_clear */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2914 0, /* tp_richcompare */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2915 0, /* tp_weaklistoffset */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2916 PyObject_SelfIter, /* tp_iter */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2917 (iternextfunc)dictiter_iternextkey, /* tp_iternext */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2918 dictiter_methods, /* tp_methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2919 0,
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2920 };
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2921
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2922 static PyObject *dictiter_iternextvalue(dictiterobject *di)
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2923 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2924 PyObject *value;
85157
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
2925 Py_ssize_t i, mask, offset;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2926 PyDictObject *d = di->di_dict;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2927 PyObject **value_ptr;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2928
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2929 if (d == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2930 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2931 assert (PyDict_Check(d));
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2932
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2933 if (di->di_used != d->ma_used) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2934 PyErr_SetString(PyExc_RuntimeError,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2935 "dictionary changed size during iteration");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2936 di->di_used = -1; /* Make this state sticky */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2937 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2938 }
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2939
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2940 i = di->di_pos;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2941 mask = DK_SIZE(d->ma_keys)-1;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2942 if (i < 0 || i > mask)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2943 goto fail;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2944 if (d->ma_values) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2945 value_ptr = &d->ma_values[i];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2946 offset = sizeof(PyObject *);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2947 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2948 else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2949 value_ptr = &d->ma_keys->dk_entries[i].me_value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2950 offset = sizeof(PyDictKeyEntry);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2951 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2952 while (i <= mask && *value_ptr == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2953 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2954 i++;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2955 if (i > mask)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2956 goto fail;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2957 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2958 di->di_pos = i+1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2959 di->len--;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
2960 value = *value_ptr;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2961 Py_INCREF(value);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2962 return value;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2963
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2964 fail:
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2965 Py_DECREF(d);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2966 di->di_dict = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2967 return NULL;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2968 }
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2969
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
2970 PyTypeObject PyDictIterValue_Type = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2971 PyVarObject_HEAD_INIT(&PyType_Type, 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2972 "dict_valueiterator", /* tp_name */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2973 sizeof(dictiterobject), /* tp_basicsize */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2974 0, /* tp_itemsize */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2975 /* methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2976 (destructor)dictiter_dealloc, /* tp_dealloc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2977 0, /* tp_print */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2978 0, /* tp_getattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2979 0, /* tp_setattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2980 0, /* tp_reserved */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2981 0, /* tp_repr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2982 0, /* tp_as_number */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2983 0, /* tp_as_sequence */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2984 0, /* tp_as_mapping */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2985 0, /* tp_hash */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2986 0, /* tp_call */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2987 0, /* tp_str */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2988 PyObject_GenericGetAttr, /* tp_getattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2989 0, /* tp_setattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2990 0, /* tp_as_buffer */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2991 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2992 0, /* tp_doc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2993 (traverseproc)dictiter_traverse, /* tp_traverse */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2994 0, /* tp_clear */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2995 0, /* tp_richcompare */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2996 0, /* tp_weaklistoffset */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2997 PyObject_SelfIter, /* tp_iter */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2998 (iternextfunc)dictiter_iternextvalue, /* tp_iternext */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
2999 dictiter_methods, /* tp_methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3000 0,
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17595
diff changeset
3001 };
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
3002
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
3003 static PyObject *dictiter_iternextitem(dictiterobject *di)
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
3004 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3005 PyObject *key, *value, *result = di->di_result;
85157
e7f6cef7a4cc Issue #18722: Remove uses of the "register" keyword in C code.
Antoine Pitrou <solipsis@pitrou.net>
parents: 84799
diff changeset
3006 Py_ssize_t i, mask, offset;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3007 PyDictObject *d = di->di_dict;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3008 PyObject **value_ptr;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
3009
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3010 if (d == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3011 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3012 assert (PyDict_Check(d));
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
3013
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3014 if (di->di_used != d->ma_used) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3015 PyErr_SetString(PyExc_RuntimeError,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3016 "dictionary changed size during iteration");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3017 di->di_used = -1; /* Make this state sticky */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3018 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3019 }
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
3020
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3021 i = di->di_pos;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3022 if (i < 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3023 goto fail;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3024 mask = DK_SIZE(d->ma_keys)-1;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3025 if (d->ma_values) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3026 value_ptr = &d->ma_values[i];
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3027 offset = sizeof(PyObject *);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3028 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3029 else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3030 value_ptr = &d->ma_keys->dk_entries[i].me_value;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3031 offset = sizeof(PyDictKeyEntry);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3032 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3033 while (i <= mask && *value_ptr == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3034 value_ptr = (PyObject **)(((char *)value_ptr) + offset);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3035 i++;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3036 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3037 di->di_pos = i+1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3038 if (i > mask)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3039 goto fail;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
3040
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3041 if (result->ob_refcnt == 1) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3042 Py_INCREF(result);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3043 Py_DECREF(PyTuple_GET_ITEM(result, 0));
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3044 Py_DECREF(PyTuple_GET_ITEM(result, 1));
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3045 } else {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3046 result = PyTuple_New(2);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3047 if (result == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3048 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3049 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3050 di->len--;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3051 key = d->ma_keys->dk_entries[i].me_key;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3052 value = *value_ptr;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3053 Py_INCREF(key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3054 Py_INCREF(value);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3055 PyTuple_SET_ITEM(result, 0, key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3056 PyTuple_SET_ITEM(result, 1, value);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3057 return result;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
3058
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
3059 fail:
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3060 Py_DECREF(d);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3061 di->di_dict = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3062 return NULL;
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
3063 }
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
3064
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
3065 PyTypeObject PyDictIterItem_Type = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3066 PyVarObject_HEAD_INIT(&PyType_Type, 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3067 "dict_itemiterator", /* tp_name */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3068 sizeof(dictiterobject), /* tp_basicsize */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3069 0, /* tp_itemsize */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3070 /* methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3071 (destructor)dictiter_dealloc, /* tp_dealloc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3072 0, /* tp_print */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3073 0, /* tp_getattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3074 0, /* tp_setattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3075 0, /* tp_reserved */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3076 0, /* tp_repr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3077 0, /* tp_as_number */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3078 0, /* tp_as_sequence */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3079 0, /* tp_as_mapping */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3080 0, /* tp_hash */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3081 0, /* tp_call */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3082 0, /* tp_str */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3083 PyObject_GenericGetAttr, /* tp_getattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3084 0, /* tp_setattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3085 0, /* tp_as_buffer */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3086 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3087 0, /* tp_doc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3088 (traverseproc)dictiter_traverse, /* tp_traverse */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3089 0, /* tp_clear */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3090 0, /* tp_richcompare */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3091 0, /* tp_weaklistoffset */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3092 PyObject_SelfIter, /* tp_iter */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3093 (iternextfunc)dictiter_iternextitem, /* tp_iternext */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3094 dictiter_methods, /* tp_methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3095 0,
31680
bb3bccb3b77d Optimize dictionary iterators.
Raymond Hettinger <python@rcn.com>
parents: 31677
diff changeset
3096 };
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3097
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3098
76091
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3099 static PyObject *
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3100 dictiter_reduce(dictiterobject *di)
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3101 {
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3102 PyObject *list;
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3103 dictiterobject tmp;
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3104
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3105 list = PyList_New(0);
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3106 if (!list)
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3107 return NULL;
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3108
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3109 /* copy the itertor state */
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3110 tmp = *di;
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3111 Py_XINCREF(tmp.di_dict);
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3112
76091
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3113 /* iterate the temporary into a list */
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3114 for(;;) {
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3115 PyObject *element = 0;
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3116 if (Py_TYPE(di) == &PyDictIterItem_Type)
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3117 element = dictiter_iternextitem(&tmp);
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3118 else if (Py_TYPE(di) == &PyDictIterKey_Type)
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3119 element = dictiter_iternextkey(&tmp);
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3120 else if (Py_TYPE(di) == &PyDictIterValue_Type)
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3121 element = dictiter_iternextvalue(&tmp);
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3122 else
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3123 assert(0);
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3124 if (element) {
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3125 if (PyList_Append(list, element)) {
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3126 Py_DECREF(element);
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3127 Py_DECREF(list);
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3128 Py_XDECREF(tmp.di_dict);
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3129 return NULL;
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3130 }
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3131 Py_DECREF(element);
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3132 } else
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3133 break;
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3134 }
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3135 Py_XDECREF(tmp.di_dict);
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3136 /* check for error */
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3137 if (tmp.di_dict != NULL) {
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3138 /* we have an error */
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3139 Py_DECREF(list);
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3140 return NULL;
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3141 }
76105
fb907ee4fdfa Rename _PyIter_GetBuiltin to _PyObject_GetBuiltin, and do not include it in the stable ABI.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76091
diff changeset
3142 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list);
76091
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3143 }
4ff234337e24 Issue #14288: Serialization support for builtin iterators.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 75959
diff changeset
3144
40806
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3145 /***********************************************/
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3146 /* View objects for keys(), items(), values(). */
40806
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3147 /***********************************************/
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3148
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3149 /* The instance lay-out is the same for all three; but the type differs. */
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3150
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3151 typedef struct {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3152 PyObject_HEAD
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3153 PyDictObject *dv_dict;
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3154 } dictviewobject;
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3155
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3156
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3157 static void
40806
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3158 dictview_dealloc(dictviewobject *dv)
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3159 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3160 Py_XDECREF(dv->dv_dict);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3161 PyObject_GC_Del(dv);
50553
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49493
diff changeset
3162 }
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49493
diff changeset
3163
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49493
diff changeset
3164 static int
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49493
diff changeset
3165 dictview_traverse(dictviewobject *dv, visitproc visit, void *arg)
badb471dbaeb Merged revisions 68132 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49493
diff changeset
3166 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3167 Py_VISIT(dv->dv_dict);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3168 return 0;
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3169 }
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3170
40805
eaedf84eabcd Endow dict views with a proper length method.
Guido van Rossum <guido@python.org>
parents: 40804
diff changeset
3171 static Py_ssize_t
40806
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3172 dictview_len(dictviewobject *dv)
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3173 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3174 Py_ssize_t len = 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3175 if (dv->dv_dict != NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3176 len = dv->dv_dict->ma_used;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3177 return len;
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3178 }
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3179
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3180 static PyObject *
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3181 dictview_new(PyObject *dict, PyTypeObject *type)
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3182 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3183 dictviewobject *dv;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3184 if (dict == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3185 PyErr_BadInternalCall();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3186 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3187 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3188 if (!PyDict_Check(dict)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3189 /* XXX Get rid of this restriction later */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3190 PyErr_Format(PyExc_TypeError,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3191 "%s() requires a dict argument, not '%s'",
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3192 type->tp_name, dict->ob_type->tp_name);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3193 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3194 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3195 dv = PyObject_GC_New(dictviewobject, type);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3196 if (dv == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3197 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3198 Py_INCREF(dict);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3199 dv->dv_dict = (PyDictObject *)dict;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3200 _PyObject_GC_TRACK(dv);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3201 return (PyObject *)dv;
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3202 }
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3203
40953
94c4e36c3124 Add TODO
Neal Norwitz <nnorwitz@gmail.com>
parents: 40909
diff changeset
3204 /* TODO(guido): The views objects are not complete:
94c4e36c3124 Add TODO
Neal Norwitz <nnorwitz@gmail.com>
parents: 40909
diff changeset
3205
94c4e36c3124 Add TODO
Neal Norwitz <nnorwitz@gmail.com>
parents: 40909
diff changeset
3206 * support more set operations
94c4e36c3124 Add TODO
Neal Norwitz <nnorwitz@gmail.com>
parents: 40909
diff changeset
3207 * support arbitrary mappings?
94c4e36c3124 Add TODO
Neal Norwitz <nnorwitz@gmail.com>
parents: 40909
diff changeset
3208 - either these should be static or exported in dictobject.h
94c4e36c3124 Add TODO
Neal Norwitz <nnorwitz@gmail.com>
parents: 40909
diff changeset
3209 - if public then they should probably be in builtins
94c4e36c3124 Add TODO
Neal Norwitz <nnorwitz@gmail.com>
parents: 40909
diff changeset
3210 */
94c4e36c3124 Add TODO
Neal Norwitz <nnorwitz@gmail.com>
parents: 40909
diff changeset
3211
42799
d02610b0a9f3 Patch with Keir Mierle: add rich comparisons between dict views and sets,
Guido van Rossum <guido@python.org>
parents: 42750
diff changeset
3212 /* Return 1 if self is a subset of other, iterating over self;
d02610b0a9f3 Patch with Keir Mierle: add rich comparisons between dict views and sets,
Guido van Rossum <guido@python.org>
parents: 42750
diff changeset
3213 0 if not; -1 if an error occurred. */
40823
50330d18d1cd Make dict.keys() and dict.items() comparable to sets, using == and !=.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
3214 static int
50330d18d1cd Make dict.keys() and dict.items() comparable to sets, using == and !=.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
3215 all_contained_in(PyObject *self, PyObject *other)
50330d18d1cd Make dict.keys() and dict.items() comparable to sets, using == and !=.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
3216 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3217 PyObject *iter = PyObject_GetIter(self);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3218 int ok = 1;
40823
50330d18d1cd Make dict.keys() and dict.items() comparable to sets, using == and !=.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
3219
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3220 if (iter == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3221 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3222 for (;;) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3223 PyObject *next = PyIter_Next(iter);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3224 if (next == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3225 if (PyErr_Occurred())
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3226 ok = -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3227 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3228 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3229 ok = PySequence_Contains(other, next);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3230 Py_DECREF(next);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3231 if (ok <= 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3232 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3233 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3234 Py_DECREF(iter);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3235 return ok;
40823
50330d18d1cd Make dict.keys() and dict.items() comparable to sets, using == and !=.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
3236 }
50330d18d1cd Make dict.keys() and dict.items() comparable to sets, using == and !=.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
3237
50330d18d1cd Make dict.keys() and dict.items() comparable to sets, using == and !=.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
3238 static PyObject *
50330d18d1cd Make dict.keys() and dict.items() comparable to sets, using == and !=.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
3239 dictview_richcompare(PyObject *self, PyObject *other, int op)
50330d18d1cd Make dict.keys() and dict.items() comparable to sets, using == and !=.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
3240 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3241 Py_ssize_t len_self, len_other;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3242 int ok;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3243 PyObject *result;
42799
d02610b0a9f3 Patch with Keir Mierle: add rich comparisons between dict views and sets,
Guido van Rossum <guido@python.org>
parents: 42750
diff changeset
3244
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3245 assert(self != NULL);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3246 assert(PyDictViewSet_Check(self));
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3247 assert(other != NULL);
40823
50330d18d1cd Make dict.keys() and dict.items() comparable to sets, using == and !=.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
3248
71810
d0b0fcbb40db Replace Py_NotImplemented returns with the macro form Py_RETURN_NOTIMPLEMENTED.
Brian Curtin <brian@python.org>
parents: 68586
diff changeset
3249 if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other))
d0b0fcbb40db Replace Py_NotImplemented returns with the macro form Py_RETURN_NOTIMPLEMENTED.
Brian Curtin <brian@python.org>
parents: 68586
diff changeset
3250 Py_RETURN_NOTIMPLEMENTED;
42799
d02610b0a9f3 Patch with Keir Mierle: add rich comparisons between dict views and sets,
Guido van Rossum <guido@python.org>
parents: 42750
diff changeset
3251
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3252 len_self = PyObject_Size(self);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3253 if (len_self < 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3254 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3255 len_other = PyObject_Size(other);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3256 if (len_other < 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3257 return NULL;
42799
d02610b0a9f3 Patch with Keir Mierle: add rich comparisons between dict views and sets,
Guido van Rossum <guido@python.org>
parents: 42750
diff changeset
3258
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3259 ok = 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3260 switch(op) {
42799
d02610b0a9f3 Patch with Keir Mierle: add rich comparisons between dict views and sets,
Guido van Rossum <guido@python.org>
parents: 42750
diff changeset
3261
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3262 case Py_NE:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3263 case Py_EQ:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3264 if (len_self == len_other)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3265 ok = all_contained_in(self, other);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3266 if (op == Py_NE && ok >= 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3267 ok = !ok;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3268 break;
42799
d02610b0a9f3 Patch with Keir Mierle: add rich comparisons between dict views and sets,
Guido van Rossum <guido@python.org>
parents: 42750
diff changeset
3269
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3270 case Py_LT:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3271 if (len_self < len_other)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3272 ok = all_contained_in(self, other);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3273 break;
42799
d02610b0a9f3 Patch with Keir Mierle: add rich comparisons between dict views and sets,
Guido van Rossum <guido@python.org>
parents: 42750
diff changeset
3274
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3275 case Py_LE:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3276 if (len_self <= len_other)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3277 ok = all_contained_in(self, other);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3278 break;
42799
d02610b0a9f3 Patch with Keir Mierle: add rich comparisons between dict views and sets,
Guido van Rossum <guido@python.org>
parents: 42750
diff changeset
3279
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3280 case Py_GT:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3281 if (len_self > len_other)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3282 ok = all_contained_in(other, self);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3283 break;
42799
d02610b0a9f3 Patch with Keir Mierle: add rich comparisons between dict views and sets,
Guido van Rossum <guido@python.org>
parents: 42750
diff changeset
3284
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3285 case Py_GE:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3286 if (len_self >= len_other)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3287 ok = all_contained_in(other, self);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3288 break;
42799
d02610b0a9f3 Patch with Keir Mierle: add rich comparisons between dict views and sets,
Guido van Rossum <guido@python.org>
parents: 42750
diff changeset
3289
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3290 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3291 if (ok < 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3292 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3293 result = ok ? Py_True : Py_False;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3294 Py_INCREF(result);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3295 return result;
40823
50330d18d1cd Make dict.keys() and dict.items() comparable to sets, using == and !=.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
3296 }
50330d18d1cd Make dict.keys() and dict.items() comparable to sets, using == and !=.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
3297
51971
5ad88b919318 Give dict views a helpful __repr__.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
3298 static PyObject *
5ad88b919318 Give dict views a helpful __repr__.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
3299 dictview_repr(dictviewobject *dv)
5ad88b919318 Give dict views a helpful __repr__.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
3300 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3301 PyObject *seq;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3302 PyObject *result;
51971
5ad88b919318 Give dict views a helpful __repr__.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
3303
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3304 seq = PySequence_List((PyObject *)dv);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3305 if (seq == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3306 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3307
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3308 result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3309 Py_DECREF(seq);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3310 return result;
51971
5ad88b919318 Give dict views a helpful __repr__.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
3311 }
5ad88b919318 Give dict views a helpful __repr__.
Raymond Hettinger <python@rcn.com>
parents: 51271
diff changeset
3312
40806
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3313 /*** dict_keys ***/
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3314
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3315 static PyObject *
40806
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3316 dictkeys_iter(dictviewobject *dv)
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3317 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3318 if (dv->dv_dict == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3319 Py_RETURN_NONE;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3320 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3321 return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
40806
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3322 }
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3323
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3324 static int
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3325 dictkeys_contains(dictviewobject *dv, PyObject *obj)
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3326 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3327 if (dv->dv_dict == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3328 return 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3329 return PyDict_Contains((PyObject *)dv->dv_dict, obj);
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3330 }
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3331
40805
eaedf84eabcd Endow dict views with a proper length method.
Guido van Rossum <guido@python.org>
parents: 40804
diff changeset
3332 static PySequenceMethods dictkeys_as_sequence = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3333 (lenfunc)dictview_len, /* sq_length */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3334 0, /* sq_concat */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3335 0, /* sq_repeat */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3336 0, /* sq_item */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3337 0, /* sq_slice */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3338 0, /* sq_ass_item */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3339 0, /* sq_ass_slice */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3340 (objobjproc)dictkeys_contains, /* sq_contains */
40805
eaedf84eabcd Endow dict views with a proper length method.
Guido van Rossum <guido@python.org>
parents: 40804
diff changeset
3341 };
eaedf84eabcd Endow dict views with a proper length method.
Guido van Rossum <guido@python.org>
parents: 40804
diff changeset
3342
42805
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3343 static PyObject*
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3344 dictviews_sub(PyObject* self, PyObject *other)
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3345 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3346 PyObject *result = PySet_New(self);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3347 PyObject *tmp;
72925
7109f31300fb Rename _Py_identifier to _Py_IDENTIFIER.
Martin v. Löwis <martin@v.loewis.de>
parents: 72914
diff changeset
3348 _Py_IDENTIFIER(difference_update);
72835
5e33219492bc Add API for static strings, primarily good for identifiers.
Martin v. Löwis <martin@v.loewis.de>
parents: 72475
diff changeset
3349
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3350 if (result == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3351 return NULL;
42805
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3352
72835
5e33219492bc Add API for static strings, primarily good for identifiers.
Martin v. Löwis <martin@v.loewis.de>
parents: 72475
diff changeset
3353 tmp = _PyObject_CallMethodId(result, &PyId_difference_update, "O", other);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3354 if (tmp == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3355 Py_DECREF(result);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3356 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3357 }
42805
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3358
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3359 Py_DECREF(tmp);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3360 return result;
42805
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3361 }
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3362
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3363 static PyObject*
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3364 dictviews_and(PyObject* self, PyObject *other)
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3365 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3366 PyObject *result = PySet_New(self);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3367 PyObject *tmp;
72925
7109f31300fb Rename _Py_identifier to _Py_IDENTIFIER.
Martin v. Löwis <martin@v.loewis.de>
parents: 72914
diff changeset
3368 _Py_IDENTIFIER(intersection_update);
72835
5e33219492bc Add API for static strings, primarily good for identifiers.
Martin v. Löwis <martin@v.loewis.de>
parents: 72475
diff changeset
3369
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3370 if (result == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3371 return NULL;
42805
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3372
72835
5e33219492bc Add API for static strings, primarily good for identifiers.
Martin v. Löwis <martin@v.loewis.de>
parents: 72475
diff changeset
3373 tmp = _PyObject_CallMethodId(result, &PyId_intersection_update, "O", other);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3374 if (tmp == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3375 Py_DECREF(result);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3376 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3377 }
42805
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3378
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3379 Py_DECREF(tmp);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3380 return result;
42805
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3381 }
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3382
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3383 static PyObject*
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3384 dictviews_or(PyObject* self, PyObject *other)
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3385 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3386 PyObject *result = PySet_New(self);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3387 PyObject *tmp;
72926
db4aa878cea2 Port SetAttrString/HasAttrString to SetAttrId/GetAttrId.
Martin v. Löwis <martin@v.loewis.de>
parents: 72925
diff changeset
3388 _Py_IDENTIFIER(update);
72914
d5c05e90be4c dictviews_or() uses _Py_identifier
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72835
diff changeset
3389
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3390 if (result == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3391 return NULL;
42805
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3392
72914
d5c05e90be4c dictviews_or() uses _Py_identifier
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72835
diff changeset
3393 tmp = _PyObject_CallMethodId(result, &PyId_update, "O", other);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3394 if (tmp == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3395 Py_DECREF(result);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3396 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3397 }
42805
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3398
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3399 Py_DECREF(tmp);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3400 return result;
42805
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3401 }
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3402
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3403 static PyObject*
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3404 dictviews_xor(PyObject* self, PyObject *other)
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3405 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3406 PyObject *result = PySet_New(self);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3407 PyObject *tmp;
72925
7109f31300fb Rename _Py_identifier to _Py_IDENTIFIER.
Martin v. Löwis <martin@v.loewis.de>
parents: 72914
diff changeset
3408 _Py_IDENTIFIER(symmetric_difference_update);
72835
5e33219492bc Add API for static strings, primarily good for identifiers.
Martin v. Löwis <martin@v.loewis.de>
parents: 72475
diff changeset
3409
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3410 if (result == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3411 return NULL;
42805
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3412
72835
5e33219492bc Add API for static strings, primarily good for identifiers.
Martin v. Löwis <martin@v.loewis.de>
parents: 72475
diff changeset
3413 tmp = _PyObject_CallMethodId(result, &PyId_symmetric_difference_update, "O",
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3414 other);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3415 if (tmp == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3416 Py_DECREF(result);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3417 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3418 }
42805
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3419
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3420 Py_DECREF(tmp);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3421 return result;
42805
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3422 }
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3423
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3424 static PyNumberMethods dictviews_as_number = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3425 0, /*nb_add*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3426 (binaryfunc)dictviews_sub, /*nb_subtract*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3427 0, /*nb_multiply*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3428 0, /*nb_remainder*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3429 0, /*nb_divmod*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3430 0, /*nb_power*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3431 0, /*nb_negative*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3432 0, /*nb_positive*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3433 0, /*nb_absolute*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3434 0, /*nb_bool*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3435 0, /*nb_invert*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3436 0, /*nb_lshift*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3437 0, /*nb_rshift*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3438 (binaryfunc)dictviews_and, /*nb_and*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3439 (binaryfunc)dictviews_xor, /*nb_xor*/
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3440 (binaryfunc)dictviews_or, /*nb_or*/
42805
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3441 };
3cd60f744f8e Keir Mierle's set operations for dict views (keys/items only of course).
Guido van Rossum <guido@python.org>
parents: 42799
diff changeset
3442
64377
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3443 static PyObject*
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3444 dictviews_isdisjoint(PyObject *self, PyObject *other)
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3445 {
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3446 PyObject *it;
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3447 PyObject *item = NULL;
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3448
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3449 if (self == other) {
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3450 if (dictview_len((dictviewobject *)self) == 0)
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3451 Py_RETURN_TRUE;
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3452 else
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3453 Py_RETURN_FALSE;
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3454 }
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3455
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3456 /* Iterate over the shorter object (only if other is a set,
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3457 * because PySequence_Contains may be expensive otherwise): */
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3458 if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) {
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3459 Py_ssize_t len_self = dictview_len((dictviewobject *)self);
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3460 Py_ssize_t len_other = PyObject_Size(other);
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3461 if (len_other == -1)
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3462 return NULL;
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3463
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3464 if ((len_other > len_self)) {
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3465 PyObject *tmp = other;
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3466 other = self;
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3467 self = tmp;
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3468 }
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3469 }
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3470
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3471 it = PyObject_GetIter(other);
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3472 if (it == NULL)
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3473 return NULL;
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3474
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3475 while ((item = PyIter_Next(it)) != NULL) {
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3476 int contains = PySequence_Contains(self, item);
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3477 Py_DECREF(item);
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3478 if (contains == -1) {
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3479 Py_DECREF(it);
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3480 return NULL;
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3481 }
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3482
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3483 if (contains) {
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3484 Py_DECREF(it);
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3485 Py_RETURN_FALSE;
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3486 }
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3487 }
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3488 Py_DECREF(it);
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3489 if (PyErr_Occurred())
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3490 return NULL; /* PyIter_Next raised an exception. */
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3491 Py_RETURN_TRUE;
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3492 }
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3493
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3494 PyDoc_STRVAR(isdisjoint_doc,
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3495 "Return True if the view and the given iterable have a null intersection.");
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3496
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3497 static PyMethodDef dictkeys_methods[] = {
64377
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3498 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3499 isdisjoint_doc},
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3500 {NULL, NULL} /* sentinel */
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3501 };
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3502
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3503 PyTypeObject PyDictKeys_Type = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3504 PyVarObject_HEAD_INIT(&PyType_Type, 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3505 "dict_keys", /* tp_name */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3506 sizeof(dictviewobject), /* tp_basicsize */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3507 0, /* tp_itemsize */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3508 /* methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3509 (destructor)dictview_dealloc, /* tp_dealloc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3510 0, /* tp_print */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3511 0, /* tp_getattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3512 0, /* tp_setattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3513 0, /* tp_reserved */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3514 (reprfunc)dictview_repr, /* tp_repr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3515 &dictviews_as_number, /* tp_as_number */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3516 &dictkeys_as_sequence, /* tp_as_sequence */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3517 0, /* tp_as_mapping */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3518 0, /* tp_hash */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3519 0, /* tp_call */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3520 0, /* tp_str */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3521 PyObject_GenericGetAttr, /* tp_getattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3522 0, /* tp_setattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3523 0, /* tp_as_buffer */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3524 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3525 0, /* tp_doc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3526 (traverseproc)dictview_traverse, /* tp_traverse */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3527 0, /* tp_clear */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3528 dictview_richcompare, /* tp_richcompare */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3529 0, /* tp_weaklistoffset */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3530 (getiterfunc)dictkeys_iter, /* tp_iter */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3531 0, /* tp_iternext */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3532 dictkeys_methods, /* tp_methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3533 0,
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3534 };
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3535
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3536 static PyObject *
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3537 dictkeys_new(PyObject *dict)
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3538 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3539 return dictview_new(dict, &PyDictKeys_Type);
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3540 }
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3541
40806
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3542 /*** dict_items ***/
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3543
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3544 static PyObject *
40806
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3545 dictitems_iter(dictviewobject *dv)
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3546 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3547 if (dv->dv_dict == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3548 Py_RETURN_NONE;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3549 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3550 return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
40806
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3551 }
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3552
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3553 static int
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3554 dictitems_contains(dictviewobject *dv, PyObject *obj)
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3555 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3556 PyObject *key, *value, *found;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3557 if (dv->dv_dict == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3558 return 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3559 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3560 return 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3561 key = PyTuple_GET_ITEM(obj, 0);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3562 value = PyTuple_GET_ITEM(obj, 1);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3563 found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3564 if (found == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3565 if (PyErr_Occurred())
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3566 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3567 return 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3568 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3569 return PyObject_RichCompareBool(value, found, Py_EQ);
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3570 }
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3571
40805
eaedf84eabcd Endow dict views with a proper length method.
Guido van Rossum <guido@python.org>
parents: 40804
diff changeset
3572 static PySequenceMethods dictitems_as_sequence = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3573 (lenfunc)dictview_len, /* sq_length */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3574 0, /* sq_concat */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3575 0, /* sq_repeat */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3576 0, /* sq_item */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3577 0, /* sq_slice */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3578 0, /* sq_ass_item */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3579 0, /* sq_ass_slice */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3580 (objobjproc)dictitems_contains, /* sq_contains */
40805
eaedf84eabcd Endow dict views with a proper length method.
Guido van Rossum <guido@python.org>
parents: 40804
diff changeset
3581 };
eaedf84eabcd Endow dict views with a proper length method.
Guido van Rossum <guido@python.org>
parents: 40804
diff changeset
3582
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3583 static PyMethodDef dictitems_methods[] = {
64377
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3584 {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O,
0260e04d2cfc Issue #9212: Added the missing isdisjoint method to the dict_keys and
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 61466
diff changeset
3585 isdisjoint_doc},
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3586 {NULL, NULL} /* sentinel */
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3587 };
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3588
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3589 PyTypeObject PyDictItems_Type = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3590 PyVarObject_HEAD_INIT(&PyType_Type, 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3591 "dict_items", /* tp_name */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3592 sizeof(dictviewobject), /* tp_basicsize */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3593 0, /* tp_itemsize */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3594 /* methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3595 (destructor)dictview_dealloc, /* tp_dealloc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3596 0, /* tp_print */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3597 0, /* tp_getattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3598 0, /* tp_setattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3599 0, /* tp_reserved */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3600 (reprfunc)dictview_repr, /* tp_repr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3601 &dictviews_as_number, /* tp_as_number */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3602 &dictitems_as_sequence, /* tp_as_sequence */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3603 0, /* tp_as_mapping */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3604 0, /* tp_hash */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3605 0, /* tp_call */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3606 0, /* tp_str */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3607 PyObject_GenericGetAttr, /* tp_getattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3608 0, /* tp_setattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3609 0, /* tp_as_buffer */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3610 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3611 0, /* tp_doc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3612 (traverseproc)dictview_traverse, /* tp_traverse */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3613 0, /* tp_clear */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3614 dictview_richcompare, /* tp_richcompare */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3615 0, /* tp_weaklistoffset */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3616 (getiterfunc)dictitems_iter, /* tp_iter */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3617 0, /* tp_iternext */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3618 dictitems_methods, /* tp_methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3619 0,
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3620 };
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3621
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3622 static PyObject *
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3623 dictitems_new(PyObject *dict)
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3624 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3625 return dictview_new(dict, &PyDictItems_Type);
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3626 }
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3627
40806
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3628 /*** dict_values ***/
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3629
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3630 static PyObject *
40806
7da2ae1a4f70 Implement __contains__ for dict_keys and dict_items.
Guido van Rossum <guido@python.org>
parents: 40805
diff changeset
3631 dictvalues_iter(dictviewobject *dv)
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3632 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3633 if (dv->dv_dict == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3634 Py_RETURN_NONE;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3635 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3636 return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3637 }
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3638
40805
eaedf84eabcd Endow dict views with a proper length method.
Guido van Rossum <guido@python.org>
parents: 40804
diff changeset
3639 static PySequenceMethods dictvalues_as_sequence = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3640 (lenfunc)dictview_len, /* sq_length */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3641 0, /* sq_concat */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3642 0, /* sq_repeat */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3643 0, /* sq_item */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3644 0, /* sq_slice */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3645 0, /* sq_ass_item */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3646 0, /* sq_ass_slice */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3647 (objobjproc)0, /* sq_contains */
40805
eaedf84eabcd Endow dict views with a proper length method.
Guido van Rossum <guido@python.org>
parents: 40804
diff changeset
3648 };
eaedf84eabcd Endow dict views with a proper length method.
Guido van Rossum <guido@python.org>
parents: 40804
diff changeset
3649
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3650 static PyMethodDef dictvalues_methods[] = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3651 {NULL, NULL} /* sentinel */
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3652 };
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3653
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3654 PyTypeObject PyDictValues_Type = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3655 PyVarObject_HEAD_INIT(&PyType_Type, 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3656 "dict_values", /* tp_name */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3657 sizeof(dictviewobject), /* tp_basicsize */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3658 0, /* tp_itemsize */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3659 /* methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3660 (destructor)dictview_dealloc, /* tp_dealloc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3661 0, /* tp_print */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3662 0, /* tp_getattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3663 0, /* tp_setattr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3664 0, /* tp_reserved */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3665 (reprfunc)dictview_repr, /* tp_repr */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3666 0, /* tp_as_number */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3667 &dictvalues_as_sequence, /* tp_as_sequence */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3668 0, /* tp_as_mapping */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3669 0, /* tp_hash */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3670 0, /* tp_call */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3671 0, /* tp_str */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3672 PyObject_GenericGetAttr, /* tp_getattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3673 0, /* tp_setattro */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3674 0, /* tp_as_buffer */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3675 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3676 0, /* tp_doc */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3677 (traverseproc)dictview_traverse, /* tp_traverse */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3678 0, /* tp_clear */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3679 0, /* tp_richcompare */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3680 0, /* tp_weaklistoffset */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3681 (getiterfunc)dictvalues_iter, /* tp_iter */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3682 0, /* tp_iternext */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3683 dictvalues_methods, /* tp_methods */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3684 0,
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3685 };
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3686
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3687 static PyObject *
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3688 dictvalues_new(PyObject *dict)
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3689 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59224
diff changeset
3690 return dictview_new(dict, &PyDictValues_Type);
40804
435ed641a333 Very preliminary work on dict views.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3691 }
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3692
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3693 /* Returns NULL if cannot allocate a new PyDictKeysObject,
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3694 but does not set an error */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3695 PyDictKeysObject *
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3696 _PyDict_NewKeysForClass(void)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3697 {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3698 PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE_SPLIT);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3699 if (keys == NULL)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3700 PyErr_Clear();
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3701 else
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3702 keys->dk_lookup = lookdict_split;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3703 return keys;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3704 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3705
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3706 #define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3707
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3708 PyObject *
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3709 PyObject_GenericGetDict(PyObject *obj, void *context)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3710 {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3711 PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3712 if (dictptr == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3713 PyErr_SetString(PyExc_AttributeError,
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3714 "This object has no __dict__");
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3715 return NULL;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3716 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3717 dict = *dictptr;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3718 if (dict == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3719 PyTypeObject *tp = Py_TYPE(obj);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3720 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3721 DK_INCREF(CACHED_KEYS(tp));
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3722 *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp));
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3723 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3724 else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3725 *dictptr = dict = PyDict_New();
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3726 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3727 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3728 Py_XINCREF(dict);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3729 return dict;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3730 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3731
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3732 int
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3733 _PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr,
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3734 PyObject *key, PyObject *value)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3735 {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3736 PyObject *dict;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3737 int res;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3738 PyDictKeysObject *cached;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3739
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3740 assert(dictptr != NULL);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3741 if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3742 assert(dictptr != NULL);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3743 dict = *dictptr;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3744 if (dict == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3745 DK_INCREF(cached);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3746 dict = new_dict_with_shared_keys(cached);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3747 if (dict == NULL)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3748 return -1;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3749 *dictptr = dict;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3750 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3751 if (value == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3752 res = PyDict_DelItem(dict, key);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3753 if (cached != ((PyDictObject *)dict)->ma_keys) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3754 CACHED_KEYS(tp) = NULL;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3755 DK_DECREF(cached);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3756 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3757 } else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3758 res = PyDict_SetItem(dict, key, value);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3759 if (cached != ((PyDictObject *)dict)->ma_keys) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3760 /* Either update tp->ht_cached_keys or delete it */
76535
5d5b72a71898 distiguish between refusing to creating shared keys and error (#13903)
Benjamin Peterson <benjamin@python.org>
parents: 76530
diff changeset
3761 if (cached->dk_refcnt == 1) {
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3762 CACHED_KEYS(tp) = make_keys_shared(dict);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3763 } else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3764 CACHED_KEYS(tp) = NULL;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3765 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3766 DK_DECREF(cached);
76535
5d5b72a71898 distiguish between refusing to creating shared keys and error (#13903)
Benjamin Peterson <benjamin@python.org>
parents: 76530
diff changeset
3767 if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
5d5b72a71898 distiguish between refusing to creating shared keys and error (#13903)
Benjamin Peterson <benjamin@python.org>
parents: 76530
diff changeset
3768 return -1;
76485
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3769 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3770 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3771 } else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3772 dict = *dictptr;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3773 if (dict == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3774 dict = PyDict_New();
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3775 if (dict == NULL)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3776 return -1;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3777 *dictptr = dict;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3778 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3779 if (value == NULL) {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3780 res = PyDict_DelItem(dict, key);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3781 } else {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3782 res = PyDict_SetItem(dict, key, value);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3783 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3784 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3785 return res;
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3786 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3787
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3788 void
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3789 _PyDictKeys_DecRef(PyDictKeysObject *keys)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3790 {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3791 DK_DECREF(keys);
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3792 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3793
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3794
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3795 /* ARGSUSED */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3796 static PyObject *
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3797 dummy_repr(PyObject *op)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3798 {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3799 return PyUnicode_FromString("<dummy key>");
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3800 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3801
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3802 /* ARGUSED */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3803 static void
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3804 dummy_dealloc(PyObject* ignore)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3805 {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3806 /* This should never get called, but we also don't want to SEGV if
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3807 * we accidentally decref dummy-key out of existence.
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3808 */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3809 Py_FatalError("deallocating <dummy key>");
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3810 }
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3811
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3812 static PyTypeObject PyDictDummy_Type = {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3813 PyVarObject_HEAD_INIT(&PyType_Type, 0)
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3814 "<dummy key> type",
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3815 0,
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3816 0,
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3817 dummy_dealloc, /*tp_dealloc*/ /*never called*/
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3818 0, /*tp_print*/
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3819 0, /*tp_getattr*/
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3820 0, /*tp_setattr*/
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3821 0, /*tp_reserved*/
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3822 dummy_repr, /*tp_repr*/
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3823 0, /*tp_as_number*/
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3824 0, /*tp_as_sequence*/
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3825 0, /*tp_as_mapping*/
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3826 0, /*tp_hash */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3827 0, /*tp_call */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3828 0, /*tp_str */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3829 0, /*tp_getattro */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3830 0, /*tp_setattro */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3831 0, /*tp_as_buffer */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3832 Py_TPFLAGS_DEFAULT, /*tp_flags */
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3833 };
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3834
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3835 static PyObject _dummy_struct = {
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3836 _PyObject_EXTRA_INIT
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3837 2, &PyDictDummy_Type
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3838 };
6e5855854a2e Implement PEP 412: Key-sharing dictionaries (closes #13903)
Benjamin Peterson <benjamin@python.org>
parents: 76303
diff changeset
3839